//**************************
//   Popup DIV Scripts
//**************************

function sizeOrPosition(object,type) 
{
	//Determine scroll position
	var output = 0;
	if (object=="scroll") {
		if (typeof(window.pageYOffset)=='number') { //Netscape compliant
			if (type=="high") output = window.pageYOffset;
			if (type=="wide") output = window.pageXOffset;
		} 
		else if (document.body && (document.body.scrollLeft||document.body.scrollTop)) { //DOM compliant
			if (type=="high") output = document.body.scrollTop;
			if (type=="wide") output = document.body.scrollLeft;
		} 
		else if (document.documentElement && (document.documentElement.scrollLeft||document.documentElement.scrollTop)) { //IE6 standards compliant mode
			if (type=="high") output = document.documentElement.scrollTop;
			if (type=="wide") output = document.documentElement.scrollLeft;
		}
	}
	
	if(object=="window") {
		if (typeof(window.innerWidth)=='number') { //Non-IE
			if (type=="wide") output = window.innerWidth;
			if (type=="high") output = window.innerHeight;
		} 
		else if (document.documentElement && (document.documentElement.clientWidth||document.documentElement.clientHeight)) { //IE 6+ in 'standards compliant mode'
			if (type=="wide") output = document.documentElement.clientWidth;
			if (type=="high") output = document.documentElement.clientHeight;
		} 
		else if (document.body && (document.body.clientWidth||document.body.clientHeight)) { //IE 4 compatible
			if (type=="wide") output = document.body.clientWidth;
			if (type=="high") output = document.body.clientHeight;
		}
	}
	
	if(object=="page") {
		if(window.innerHeight && window.scrollMaxY) { // Firefox
			if (type=="wide") output = window.innerWidth + window.scrollMaxX;
			if (type=="high") output = window.innerHeight + window.scrollMaxY;
		} 
		else if(document.body.scrollHeight>document.body.offsetHeight) { // all but Explorer Mac
			if (type=="wide") output = document.body.scrollWidth;
			if (type=="high") output = document.body.scrollHeight;
		}
		else { //backup
			if (type=="wide") output = document.body.offsetWidth + document.body.offsetLeft;
			if (type=="high") output = document.body.offsetHeight + document.body.offsetTop;
		}
	}

	return output;
}

function popupBox (href, popupBox, popupControl, closeText, normalClose, noCloseImg, fadeTrigger, hideDropDowns, resize) 
{
	if (popupBox!=null)
	{
		var box; 
		 // this is the way the standards work
		 if (document.getElementById) box = document.getElementById( popupBox )
		 // this is the way old msie versions work
		 else if (document.all) box = document.all[popupBox]
		 // this is the way nn4 works
		 else if (document.layers) box = document.layers[popupBox]
	
		if (box!=null)
		{
			windowWidth=sizeOrPosition('window','wide');
			windowHeight=sizeOrPosition('window','high');
			scrollposX=sizeOrPosition('scroll','wide');
			scrollposY=sizeOrPosition('scroll','high');

			if (fadeTrigger=='FadeScreen') fadeScreen(box,windowHeight,windowWidth);
			if (resize==null) toggleLayers( popupBox, hideDropDowns );
			
			//if(contentID!=null) 
			//	{
					//Used, for example, if there is a table in the triggered DIV and you need the image inside the table.
			//		var content;
			//		 if( document.getElementById ) 
			//		 content = document.getElementById( contentID );  
			//		 else if( document.all ) 
			//		 content = document.all[contentID];  
			//		 else if( document.layers ) 
			//		 content = document.layers[contentID]; 
			//	}
			//else content=box;
			
			//if (content!=null) 
			//	{ 
			//	var href = href.href;
			//	  content.innerHTML="<a href=javascript:toggleLayers('" + popupBox + "','popupScreenFade')><div id=\"ProdDetail_popupImgClose\"><img src=\"images/icon_close_15x15.png\" class=\"close\" width=\"15px\" height=\"15px\" alt=\"Close\" hspace=\"4\" />Close</div><img src=\"" + href + "\" class=\"cover\" alt=\"Click to Close (" + title + " by " + author + ")\" /></a>";
			//	}
			//else if (content==null) alert("The script cannot find the place on the page to insert the image.");
			
			var href = href.href;
			if (resize==null) yesNoCloseLinks (href,popupBox,popupControl,closeText,normalClose,noCloseImg,fadeTrigger);

			var width = box.offsetWidth;
			var height = box.offsetHeight;
			
			if ((Math.round((windowHeight/2)-(height/2)+scrollposY)) < 0) box.style.top='0px';
			else box.style.top = Math.round((windowHeight/2)-(height/2)+scrollposY)+'px';

			if (Math.round((windowWidth/2)-(width/2)+scrollposX) < 0) box.style.left = '0px'
			else box.style.left = Math.round((windowWidth/2)-(width/2)+scrollposX)+'px'
			
			box.style.zIndex = '60';
		}
		else alert('Please make sure you have a DIV layer with an ID the same as the second element in the popupBox JS call on the page somewhere. This script will not work without it.');
	}
	else alert('Please make sure you have a DIV layer with an ID the same as the second element in the popupBox JS call on the page somewhere. This script will not work without it.');

	document.returnValue=false;
}

function popupWidget(href, popupBox, iframeDIV, margin, widgetHeight, hideDropDowns, resize)
{
	if (popupBox!=null)
	{
		var box; 
		// this is the way the standards work
		if (document.getElementById) box=document.getElementById( popupBox );
		// this is the way old msie versions work
		else if (document.all) box=document.all[popupBox];
		// this is the way nn4 works
		else if (document.layers) box=document.layers[popupBox];
	
		if (box!=null)
		{
			if (iframeDIV!=null) {
					//Finds DIV to place iframe into.
					var iframeLoc;
					if (document.getElementById) iframeLoc=document.getElementById( iframeDIV );
					else if (document.all) iframeLoc=document.all[iframeDIV];
					else if (document.layers) iframeLoc=document.layers[iframeDIV];
				}
			else iframeLoc=box; //If a DIV for the iframe is not in JS call, then it'll just insert the iframe into the main DIV container.
			//alert("|" +iframeLoc.innerHTML+ "|"); //Checks if iframe is blank or not.

			//Sets all sizing variables.
			margin=Number(margin);
			var closeSize=18;
			//var pageWidth=sizeOrPosition('page','wide'); //Used for reporting in the Alert below.
			//var pageHeight=sizeOrPosition('page','high');
			var windowWidth=sizeOrPosition('window','wide');
			var windowHeight=sizeOrPosition('window','high');
			var scrollposX=sizeOrPosition('scroll','wide');
			var scrollposY=sizeOrPosition('scroll','high');
			var widgetWidth,widgetHeight,widgetIframeHeight;
			
			//Sets actual dimensions of DIV layers.
			if ((windowWidth-(margin*2))<800) widgetWidth=windowWidth-(margin*2);  //by subtracting margin, it creates a natural margin according to the info in the JS call combined with positioning.
			else widgetWidth=800;
			//widgetWidth=windowWidth-(margin*2);
			//if (windowHeight<pageHeight && is_moz) widgetWidth=widgetWidth-12; //Didn't work for some reason. Not sure how to detect the Mozilla browser and compensate for the scrollbar.
			if (widgetHeight=="") widgetHeight=windowHeight-(margin*2);
			widgetIframeHeight= widgetHeight;
			
			//alert("Scroll: y=" +scrollposY+ "; x=" +scrollposX+ "; Window: " +windowWidth+ " x " +windowHeight+ "; Page: " +pageWidth+ " x " +pageHeight+ "; Widget: " +widgetWidth+ " x " +widgetIframeHeight);
	
			//Displays screen fade and box for widget
			if (resize==null) toggleLayers( popupBox, hideDropDowns );
			if (resize=="resizeTrue" && box.style.display=="block") fadeScreen(box,windowHeight,windowWidth,resize);
			else if (resize==null) fadeScreen(box,windowHeight,windowWidth);
			
			//Sets widget box DIV dimensions. Must be after toggleLayers.
			box.style.width= widgetWidth+"px";
			box.style.height= widgetHeight+"px"; 
			box.style.zIndex= '60';
			
			//Begin building iframe.
			//var href = href.href; //commented out since the url is now in the JS call.
			var iframeWidthNow=0;
			
			//If iframe DIV is not blank, it looks to see what the size of the DIV currently is.
			if (iframeLoc.innerHTML!="") iframeWidthNow= iframeLoc.offsetWidth;
			
			//Positioning of the Widget DIV.
			//Makes sure DIV isn't positioned outside of the page borders.
			if ((Math.round((windowHeight/2)-((widgetHeight)/2)+scrollposY)) < 0) boxTopPosition='10px';
			else {boxTopPosition= (scrollposY+margin-closeSize) + 'px'}
	
			if (Math.round((windowWidth/2)-(widgetWidth/2)+scrollposX) < 0) boxLeftPosition='10px';
			else {
				if ((windowWidth-(margin*2))<800) boxLeftPosition=(scrollposX+margin)+'px';
				else boxLeftPosition=Math.round((windowWidth/2)-(widgetWidth/2)+scrollposX)+'px';
			}
			
			//If the current iframe width is not what is best for the page size, it builds/rebuilds the iframe.
			//alert("Current Left Position: " +box.style.left+ "; Position Should Be: " +boxLeftPosition);
			if (box.style.left!=boxLeftPosition && box.style.display=="block") 
			{
				iframeLoc.innerHTML='<iframe src="' + href + '" name="widgetFrame" width="' +widgetWidth+ '" height="' +widgetIframeHeight+ '" frameborder="0" allowtransparency="yes"></iframe>';
				box.style.top= boxTopPosition;
				box.style.left= boxLeftPosition;
			}
			//alert("Box Top: " +box.style.top+ "; Box Left: " +box.style.left);
		}
		else alert('Please make sure you have a DIV layer with an ID the same as the second element in the popupWidget JS call on the page somewhere. This script will not work without it.');
	}
	else alert('Please make sure you have a DIV layer with an ID the same as the second element in the popupWidget JS call on the page somewhere. This script will not work without it.');

	document.returnValue = false;
}

function fadeScreen (box,windowHeight,windowWidth,resize)
{
	windowWidth= Number(windowWidth); //Values passed through variables aren't recognized as numbers in JS.
	windowHeight= Number(windowHeight);
	//Get full page size, even outside of scrolled location
	var pageWidth= sizeOrPosition('page','wide');
	var pageHeight= sizeOrPosition('page','high');
	var fadeWidth, fadeHeight;
	if (document.body) {
		var scrWidth = document.body.scrollWidth;
		var bodyHeight = document.body.offsetHeight;
		var bodyWidth = document.body.offsetWidth;
		//alert("offset: "+document.body.offsetWidth+ "; ScrollW: " +document.body.scrollWidth);
	}
	else {
		var scrWidth = pageWidth;
		var bodyHeight = pageHeight;
		var bodyWidth = pageWidth;
	}
	
	var fadeSecondTime;
	// this is the way the standards work
	if (document.getElementById) fadeSecondTime= document.getElementById( 'popupScreenFade' );
	// this is the way old msie versions work
	else if (document.all) fadeSecondTime= document.all['popupScreenFade'];
	// this is the way nn4 works
	else if (document.layers) fadeSecondTime= document.layers['popupScreenFade'];
	
	if (pageHeight > windowHeight) fadeHeight= pageHeight;
	else fadeHeight= windowHeight;
	if ((windowWidth>=pageWidth) && (is_moz2up==false)) fadeWidth = windowWidth;
	else if (is_moz2up) fadeWidth= scrWidth+12;
	else fadeWidth= pageWidth;
		//alert("Scroll: y=" +scrollposY+ "; x=" +scrollposX+ "; Window: " +windowWidth+ " x " +windowHeight+ "; Page: " +pageWidth+ " x " +pageHeight+ "; ScrollW:" + scrWidth);
		//alert(fadeWidth);
	
	if (fadeSecondTime==null) {	
		var fadeDIV= document.createElement('div');
		fadeDIV.setAttribute ('id','popupScreenFade');
		fadeDIV.style.display = 'block';
		fadeDIV.style.top = '0px';
		fadeDIV.style.left = '0px';
		fadeDIV.style.width = fadeWidth+'px';
		fadeDIV.style.height = fadeHeight+'px';
		fadeDIV.style.zIndex = '50';
		var parentDiv = box.parentNode;
		parentDiv.insertBefore(fadeDIV, box);
	}
	else if (resize==null) {
		toggleLayers( 'popupScreenFade' );
		if ((fadeSecondTime.style.width!=fadeWidth+'px') || (fadeSecondTime.style.height!=fadeHeight+'px')) {
			fadeSecondTime.style.width= fadeWidth+'px';
			fadeSecondTime.style.height= fadeHeight+'px';
			}
		}
	else {
		fadeSecondTime.style.width= fadeWidth+'px';
		fadeSecondTime.style.height= fadeHeight+'px';
	}
}

function toggleLayers(Layer1, Layer2, hideDropDowns) 
{
	var box;  
	 // this is the way the standards work
	 if (document.getElementById) box = document.getElementById( Layer1 );
	 // this is the way old msie versions work
	 else if (document.all) box = document.all[Layer1];
	 // this is the way nn4 works
	 else if (document.layers) box = document.layers[Layer1];
	 
	 if (box!=null) {
		 if (box.style.display==''&&box.offsetWidth!=undefined&&box.offsetHeight!=undefined)
		 box.style.display = (box.offsetWidth!=0&&box.offsetHeight!=0)?'block':'none';  
		 box.style.display = (box.style.display==''||box.style.display=='block')?'none':'block';
		}
	 if (Layer2!='noHideDD') {
			 var box2;  
			 // this is the way the standards work
			 if(document.getElementById) box2 = document.getElementById( Layer2 );  
			 // this is the way old msie versions work
			 else if(document.all) box2 = document.all[Layer2];  
			 // this is the way nn4 works
			 else if(document.layers) box2 = document.layers[Layer2];  
			
			 if(box2 != null) {
				 if(box2.style.display==''&&box2.offsetWidth!=undefined&&box2.offsetHeight!=undefined)
				 box2.style.display = (box2.offsetWidth!=0&&box2.offsetHeight!=0)?'block':'none';  
				 box2.style.display = (box2.style.display==''||box2.style.display=='block')?'none':'block';
				 }
		 }

	 if (Layer2 == 'noHideDD' || hideDropDowns == 'noHideDD' && is_ie!=null ) { }
	 else {
			if (is_ie==true && is_ie7up==false) 
			{
				if (box.style.display == 'block' || box2.style.display == 'block') var action = "hidden";
				else var action = "visible";
				
				for (var S = 0; S < document.forms.length; S++) {
					for (var R = 0; R < document.forms[S].length; R++) {
						if (document.forms[S].elements[R].options) { 
							if (document.forms[S].elements[R].name != "Find_Type")
							document.forms[S].elements[R].style.visibility = action; 
						}
					}
				} 
			}
		 }
}


function yesNoCloseLinks (href, popupBox, popupControl, closeText, closeHrefSubst, CloseImgSrc, fadeTrigger) 
{
	if (popupBox==null) alert("Please enter a DIV id for your layer in the popupBox JS as the second element in the JavaScript call. This script will not work without it.");
	else if (popupControl != null)
	{
		 var SecondTime;
		 // this is the way the standards work
		 if ( document.getElementById ) SecondTime = document.getElementById( popupBox + '_FirstTime' );  
		 // this is the way old msie versions work
		 else if ( document.all ) SecondTime = document.all[popupBox + '_FirstTime'];  
		 // this is the way nn4 works
		 else if ( document.layers ) SecondTime = document.layers[popupBox + '_FirstTime']; 
	
		if (SecondTime == null)
		{
			var buttonLinkClose;
			if (fadeTrigger != 'noFadeScreen' && fadeTrigger != null && fadeTrigger == 'FadeScreen') buttonLinkClose = "toggleLayers('" + popupBox + "','popupScreenFade')";
			else if (fadeTrigger != 'FadeScreen' && fadeTrigger != null) buttonLinkClose = "toggleLayers('" + popupBox + "','" + fadeTrigger + "')";
			else buttonLinkClose = "toggleLayers('" + popupBox + "')";
			  
			if (popupControl == 'yesNoButtons')
			{
				var yesNoButtons;
				if (document.getElementById) yesNoButtons = document.getElementById( 'yesNoButtons' );  
				else if (document.all) yesNoButtons = document.all['yesNoButtons'];  
				else if (document.layers) yesNoButtons = document.layers['yesNoButtons'];
				 
				if (yesNoButtons == null)
				{ alert ('If you want Yes/No buttons, you must have a DIV with the ID "yesNoButtons" somewhere on the page. The buttons will be placed within that DIV. However, if you do not want those buttons, use any other name than yesNoButtons in the element 3 spot of the JavaScript call.')}
				else if (href == null)
				{ alert ("Since you want Yes/No buttons to show, do not leave the href of the link blank. The format should be something similar to:<p><a href='http://yourlink.com' onClick='popupBox(this,'popupDIV')'>text or button</a>")}
				else {
					var buttonYesLink = document.createElement('a');
					buttonYesLink.setAttribute ('id', popupBox + '_FirstTime');
					buttonYesLink.href = href ;
					buttonYesLink.target = "_blank" ;
					var imgYes = document.createElement('IMG');
					imgYes.setAttribute ('id','popupYesNoImg');
					imgYes.setAttribute ('alt','Continue');
					imgYes.src = '/consumer/images/LeaveNotice_Yes.gif';
					imgYes.setAttribute ('onClick', 'javascript:' + buttonLinkClose );
					buttonYesLink.appendChild(imgYes);
					
					var buttonNoLink = document.createElement('a');
					buttonNoLink.href = "javascript:" + buttonLinkClose;
					var imgNo = document.createElement('IMG');
					imgNo.setAttribute ('id','popupYesNoImg');
					imgNo.setAttribute ('alt','Cancel');
					imgNo.src = '/consumer/images/LeaveNotice_No.gif';
					buttonNoLink.appendChild(imgNo);
				
					yesNoButtons.appendChild(buttonYesLink);
					yesNoButtons.appendChild(buttonNoLink);
				}
			}
			else {
				var popupControlDIV;
					//find location on page where we want to insert buttons
				 if (document.getElementById) popupControlDIV = document.getElementById( popupControl );  
				 else if (document.all) popupControlDIV = document.all[ popupControl ];  
				 else if (document.layers) popupControlDIV = document.layers[ popupControl ];  
			
				if (popupControlDIV==null) 
				{alert('To make sure your close links show, you need to have a DIV layer with an ID that is the same as what you have for element 3 in the JavaScript.'); }
				else if (CloseImgSrc == 'noCloseImg') {
					var closeLink = document.createElement('a');
					closeLink.setAttribute ('id',popupBox + '_FirstTime');

					if (closeHrefSubst != null && closeHrefSubst != 'normalCloseYes') closeLink.href = href;
					else closeLink.href = "javascript:" + buttonLinkClose;
					
					if (closeText != null) closeLink.appendChild(document.createTextNode( closeText ));
					else closeLink.appendChild(document.createTextNode( 'Close' ));
				}
				else if (CloseImgSrc != null) {
					var closeLink = document.createElement('a');
					closeLink.setAttribute ('id',popupBox + '_FirstTime');
					if (closeHrefSubst != null && closeHrefSubst != 'normalClose') closeLink.href = closeHrefSubst;
					else closeLink.href = "javascript:" + buttonLinkClose;
					
					var img = document.createElement('img');
					img.src = CloseImgSrc
					img.setAttribute ('class','popupImg');
					img.setAttribute ('alt',closeText);
					closeLink.appendChild(img);
				}
				
				popupControlDIV.appendChild(closeLink);
			
			}	
		}
		else if (popupControl == 'yesNoButtons') document.getElementById(popupBox + '_FirstTime').href=href;
	}
}