var _currentDhtmlPop;
var minX=0;
var maxX=0;
var minY=0;
var 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){
	return _snapWithinBounds(minY,val1,val2,maxY);
}
function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

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.
	e = e || window.event;
	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 offsetPageX;
	var offsetPageY;
	
	if (document.documentElement.scrollTop >=0 || document.body.scrollTop >= 0) {
		offsetPageX=Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
		offsetPageY=Math.max(document.documentElement.scrollTop,document.body.scrollTop);
	}
	else 
	{
		offsetPageX=window.pageXOffset;
		offsetPageY=window.pageYOffset;
	}

	var tempX = 0;
	var originalX = 0;
	var tempY = 0;
	var 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+offsetPageY-(obj.clientHeight);


	tempX = originalX = (document.all) ? (e.clientX + offsetPageX) : (e.pageX);
	tempY = originalY = (document.all) ? (e.clientY + offsetPageY) : (e.pageY);
	
	var wWidth = 0, wHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		wHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		wHeight = document.documentElement.clientHeight+18;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		wHeight = document.body.clientHeight+24;
	}

	var xTemp=(document.body.clientWidth-895)/2;
	var xVar=e.clientX-xTemp;
	var yVar=(maxY-wHeight)+(e.clientY-65);

	obj.style.top  = (yVar) + 'px';

if(xVar <700) {
	obj.style.left = (xVar - 160) + 'px';
} else {

	obj.style.left = (xVar - 500) + 'px';
}


	obj.style.visibility='visible';

	if(typeof cleanupPop == 'function'){cleanupPop();}
}


function showDhtmlPopForSpecs( 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.
	e = e || window.event;
	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 offsetPageX;
	var offsetPageY;
	
	if (document.documentElement.scrollTop >=0 || document.body.scrollTop >= 0) {
		offsetPageX=Math.max(document.documentElement.scrollLeft,document.body.scrollLeft);
		offsetPageY=Math.max(document.documentElement.scrollTop,document.body.scrollTop);
	}
	else 
	{
		offsetPageX=window.pageXOffset;
		offsetPageY=window.pageYOffset;
	}

	var tempX = 0;
	var originalX = 0;
	var tempY = 0;
	var 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+offsetPageY-(obj.clientHeight);


	tempX = originalX = (document.all) ? (e.clientX + offsetPageX) : (e.pageX);
	tempY = originalY = (document.all) ? (e.clientY + offsetPageY) : (e.pageY);
	
	var wWidth = 0, wHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		wHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		wHeight = document.documentElement.clientHeight+18;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		wHeight = document.body.clientHeight+24;
	}

	var xTemp=(document.body.clientWidth-895)/2;
	var xVar=e.clientX-xTemp;
	var yVar=(maxY-wHeight)+(e.clientY-65);

	obj.style.top  = (yVar) + 'px';
	obj.style.left = (xVar - 160) + 'px';
	obj.style.visibility='visible';

	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];
	header.innerHTML=counterTest++;
	
	return (popID);
}
function hideDhtmlPop(ref){
	if(_currentDhtmlPop){_currentDhtmlPop.style.display='none';}
}

function pushCenter (objref) {
	var obj = document.getElementById(objref);
	obj.style.top  = parseInt((document.body.clientHeight - obj.clientHeight)/2)+ 'px';
	obj.style.left = (document.body.clientWidth - obj.clientWidth)/2+ 'px';
}
