var _currentDhtmlPop
var minX=0, maxX=0
var minY=0, maxY=0

function _snapWithinBounds(min,val1, val2, max){
	if(isNaN(parseFloat(val2))){val2=val1}
	//val2 gives a second possible placement. 
	if( ((val1>=max) & (val2<val1)) || ((val1<=min) & (val1<val2)) ){
		return Math.max( min, Math.min(val2,max) )
	}else{
		return Math.max( min, Math.min(val1,max) )
	}
}
function _snapX(val1,val2){
	return _snapWithinBounds(minX,val1,val2,maxX)
}
function _snapY(val1,val2){
	//if(_currentDhtmlPop.clientHeight > (maxY-minY)){
	//	//buggy, but may be worked with in the future to keep it onscreen.
	//	return _snapWithinBounds(minY,val1,null,minY)
	//}
	return _snapWithinBounds(minY,val1,val2,maxY)
}
function showDhtmlPop( obj, e, anchor, preferredPlacement, onopen, cleanupPop){
	/// sample usage: showDhtmlPop('dhtmlPopID',event,this,3)	
	/// second and third args MUST be event and this when called from an anchor
	/// There are 2 new extra args. References to functions to call on opening and after closing.
	// see the switch() below for the preferredPlacement options.
	if(typeof onopen == 'function'){onopen(anchor,obj)}

	hideDhtmlPop()
	
	obj=document.getElementById(obj)
	if (obj==null) return;
	obj.style.visibility='hidden'
	obj.style.display='inline'
	_currentDhtmlPop=obj

	var tempX = 0, originalX = 0;
	var tempY = 0, originalY = 0;
	var offset=10;
	
	minX=(document.body.clientWidth-964)/2 //For centered-content div pages.
	maxX= minX+964-obj.clientWidth

	minY=document.body.scrollTop
	maxY=document.body.clientHeight+document.body.scrollTop-(obj.clientHeight);
  
	tempX = originalX = (document.all) ? (event.clientX + document.body.scrollLeft - minX) : (e.pageX - minX);
	tempY = originalY = (document.all) ? (event.clientY + document.body.scrollTop) : (e.pageY);

  //height hack for IE7
  version=0
  if (navigator.appVersion.indexOf("MSIE")!=-1) {
    temp=navigator.appVersion.split("MSIE")
    version=parseFloat(temp[1])
  }
  if (version>=7) //NON IE browser will return 0
    tempY += document.documentElement.scrollTop;
  
	switch(preferredPlacement.toLowerCase()){
		case 'side': /// right/left, mainly right, centered vertically. 
			tempX=_snapX(tempX + offset, tempX-obj.clientWidth - offset)
			tempY=_snapY(tempY-(obj.clientHeight/2))
			break;
		//case : left added for Express Service dhtml popup
		case 'left': /// left
			tempX=_snapX(tempX + offset, tempX-obj.clientWidth - offset -200)
			tempY=_snapY(tempY - 200 -(obj.clientHeight/2))
			break;
		case 'above': /// above. 
			tempX=_snapX(tempX-(obj.clientWidth/2)-offset)
			tempY=_snapY(tempY-(obj.clientHeight)-offset, tempY+offset)
			break;
		case 'below': /// below. 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset)
			tempY=_snapY(tempY+offset, tempY-(obj.clientHeight)-offset)
			break;
		case 'belowtallpage': /// belowTallPage. 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset)
			tempY=tempY+offset //_snapY(tempY+offset, tempY-(obj.clientHeight)-offset) //
			break;
		case 'rightside': /// mainly added the condition to fix the popupissue in infiniti preapproval  
			tempX=_snapX(tempX-(obj.clientWidth/2)-offset+350)
			tempY=_snapY(tempY-(obj.clientHeight)-offset, tempY+offset)
			break;
		case 'anchorcenter': /// anchorCenter. 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset) - 200
			tempY=tempY-(obj.clientHeight/2)+offset;
			break;
		case 'leftside': /// mainly added the condition to fix the popupissue in Payment Estimator 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset) - 400;
			tempY=tempY-(obj.clientHeight/2)+offset - 225;
			break;

      //Detect IE
      version=0
      if (navigator.appVersion.indexOf("MSIE")!=-1) {
        temp=navigator.appVersion.split("MSIE")
        version=parseFloat(temp[1])
      }

      // IE
      if (version > 0) {
        // make sure popup is below the start of the visible client area
        if(tempY < document.body.scrollTop) {
          tempY = document.body.scrollTop + 30
        }

        // make sure the bottom of the popup is above the bottom of the visible client area
/*
        if((tempY + obj.clientHeight) > document.body.scrollHeight) {
          tempY = document.body.scrollHeight - obj.clientHeight - 150
        }
*/
        if((tempY + obj.clientHeight) > (document.body.scrollHeight - 200)) {
          tempY = document.body.scrollHeight - obj.clientHeight - 350
        }

      // Non-IE
      } else {
        // make sure popup is below the start of the visible client area
        if(tempY < window.scrollY) {
          tempY = window.scrollY + 30
        }

        // make sure the bottom of the popup is above the bottom of the visible client area
/*
        if((tempY + obj.clientHeight) > (window.scrollY + document.body.scrollHeight)) {
          tempY = window.scrollY + document.body.scrollHeight - obj.clientHeight - 150
        }
*/
        if((tempY + obj.clientHeight) > (window.scrollY + document.body.scrollHeight - 200)) {
          tempY = window.scrollY + document.body.scrollHeight - obj.clientHeight - 350
        }
      }

/*
      //Detect IE5.5+
      version=0
      if (navigator.appVersion.indexOf("MSIE")!=-1) {
        temp=navigator.appVersion.split("MSIE")
        version=parseFloat(temp[1])
      }
      //NON IE browser will return 0
      if (version>=5.5)
        tempX -= 300
*/

      // make sure the left of the popup positive
      if(tempX < 30) { tempX = 30; }

      // make sure the top of the popup is below the global header
      if(tempY < 50) { tempY = 50; }
      
			break;
		case 'anchorabove': /// anchorAbove. 
			tempX=_snapX(tempX-(obj.clientWidth/2)+offset)
			tempY=tempY - obj.clientHeight - offset - 100
			break;
		default:
	}
  
	obj.style.top  = (tempY) + 'px';
	obj.style.left = (tempX) + 'px';
	obj.style.visibility='visible'

	/*with($('mPopShadow').style){
		width=obj.clientWidth+ 'px';
		height=obj.clientHeight+ 'px';
		top=(tempY+8) + 'px';
		left=(tempX+8) + 'px';
		display='block'
	}*/
	if(typeof cleanupPop == 'function'){cleanupPop()}
}
function _getElementNodes(popID){
	var nodes=[]
	var cn=$(popID).childNodes
	for(var i=0;i<cn.length;i++){
		if(cn[i].nodeType==1){ nodes.push(cn[i]) }
	}
	return nodes
}
function setDhtmlContentsTEMPLATE(popID){
	///begin prelim
	var nodes=_getElementNodes(popID)
	var header=nodes[1]
	var panel1=nodes[2]
	var panel2=nodes[3]
	///end prelim
	///begin your code ##########
	header.innerHTML=counterTest++
	
	return (popID)
}
function hideDhtmlPop(ref){
	if(_currentDhtmlPop){_currentDhtmlPop.style.display='none'}
	//if(ref){ref.style.display='none'}
	//$('mPopShadow').style.display='none'
}


