// new popup object
popup = new Object();
popup.links = new Array();
popup.linkPrefix = "popupDef";
popup.defPrefix = "formDef";
popup.offsetX = 6;
popup.offsetY = 0;
popup.defPosX = 0;
popup.defPosX = 0;
popup.displayTime = 0;
popup.hideTime = 200;
popup.displayTimer = '';
popup.hideTimer = '';
popup.currentDef = '';
popup.currentRefArray = new Array();
popup.defaultHeight = 170;
popup.shortHeight = 170;
popup.defaultWidth = 250;
popup.currentHeight = 0;
popup.titleArray = ["","","","","","","Health Factors*"]

function startPopupDefs()
{	

	// Add all of the popup divs to the scrolling container div
	elem = document.getElementById("innerContainer")
	elem.innerHTML = formPopup.DEFS;		
	
	// For each link that has the correct ID prefix, add the appropriate methods and properties
	for( i = 0; i < document.links.length; i++){	 
		if( document.links[i].id.indexOf(popup.linkPrefix) == 0 ){	
			var ele = document.links[i];			
			ele.arrpopupList = createPopupDefList(document.links[i].id);		
			ele.onclick = displayPopupDef;			
		}
	}
	
	document.getElementById(popup.defPrefix + "1").style.display = 'none';	 
	
	return true;
}

function createPopupDefList(strpopupID) {
	// popup lists are extracted from the links ID attribute, which takes the form "popup_1,2,3,4"  	
	var arrRefList = strpopupID.split('_');	
	var arrpopupList = arrRefList[1].split(',');	
	return arrpopupList
}

function displayPopupDef(e)
{	
	hideFormDefinition();
	 
	/* used to capture the event in IE or mozilla */
	e = e || window.event;		
	setCurrentRefArray(this.arrpopupList);
	setCurrentPos(e.clientX, e.clientY);		
	popup.displayTimer = setTimeout('showFormDefinition()', popup.displayTime);	
	
	var elemTitle = document.getElementById("popupDefTitle");
	var intDefID = parseInt(this.arrpopupList[0]) - 1;
	
	// not needed for this project
	elemTitle.innerHTML = popup.titleArray[intDefID];
	
	return true;
}

function hidePopupDef(e)
{
	
	/* used to capture the event in IE or mozilla */
	e = e || window.event;
	popup.hideTimer = setTimeout('hideFormDefinition()', popup.displayTime);
	return true;
}

function setCurrentRefArray(arrpopups) 
{	
	// Store the array containing the links popups	
	popup.currentRefArray = arrpopups
	return true;
}

function setCurrentPos(x, y)
{
	var scrollX = setScrollX();
	var scrollY = setScrollY();
	popup.defPosX = x + popup.offsetX + scrollX;
	popup.defPosY = y + popup.offsetY + scrollY;
	return true;
}

function setScrollX()
{
	x = '';
	if( window.pageXOffset ){
		x = window.pageXOffset
	}else if( document.documentElement.scrollLeft ){
		x = document.documentElement.scrollLeft
	}else if( document.body.scrollLeft ){
		x = document.body.scrollLeft
	}else{
		x = 0;
	}
	return x;
}

function setScrollY()
{
	y = '';
	if( window.pageYOffset ){
		y = window.pageYOffset
	}else if( document.documentElement.scrollTop ){
		y = document.documentElement.scrollTop
	}else if( document.body.scrollTop ){
		y = document.body.scrollTop
	}else{
		y = 0;
	}
	return y;
}


function showFormDefinition()
{	
	if( document.getElementById){	
		
		// Get the popup container
		var eleContainer = document.getElementById("popupDefContainer");		
		
		// Get the IFRAME container used to cover IE form elements (sheesh!)
		var eleIFrame = document.getElementById("shield1");
		
		// Resize height of popup container if only 1 element exists, else use default height	
		if(popup.currentRefArray.length < 2) {
			popup.currentHeight = popup.shortHeight;
		} else {
			popup.currentHeight = popup.defaultHeight;			
		}
		
		eleContainer.style.height = String(popup.currentHeight) + 'px';
		
		// This child element must be dynamically resized for IE
		var eleContainerChild = document.getElementById("innerContainer");
		eleContainerChild.style.height = String(popup.currentHeight - 17) + 'px';
		
		
		// Display each of the popups divs for this link
		for(i=0; i<popup.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(popup.defPrefix + popup.currentRefArray[i]);
			//alert(popup.defPrefix + popup.currentRefArray[i] + ".style.display = " + eleDef.style.display);
			eleDef.style.display = 'block';
		}
		
		
		// Check if popup div will not be contained within the browser window and make adjustments
		
		// Get window width and height
		if (window.innerWidth != undefined) {
			// For Mozilla
			var win_w = window.innerWidth;
			var win_h = window.innerHeight;
		} else if (document.body.clientWidth != undefined) {
			// For IE  
			var win_w = (document.compatMode=="CSS1Compat")?document.documentElement.clientWidth : document.body.clientWidth; 		
			var win_h = (document.compatMode=="CSS1Compat")?document.documentElement.clientHeight : document.body.clientHeight;
		}
		
		// get vertical scroll position
		var scrollTop = document.body.scrollTop;
		if (scrollTop == 0)
		{
			if (window.pageYOffset)
				scrollTop = window.pageYOffset;
			else
				scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
		
		// check if div will be beyond window width
		var refRight = popup.defPosX + popup.defaultWidth + 5;		
		if (refRight > win_w) {			
			popup.defPosX = popup.defPosX - popup.defaultWidth - 20;
		}
		
		// check if div will be beyond window height			
		var refBot = (popup.defPosY - scrollTop) + popup.currentHeight + 5;			
		var strUserAgent = navigator.userAgent;			
		if (strUserAgent.indexOf("Safari") == -1) {
			// If userAgent is NOT Safari (must be IE, or FF)
			var refBot = (popup.defPosY - scrollTop) + popup.currentHeight + 5;
			if (refBot > win_h) {
				popup.defPosY = popup.defPosY - popup.currentHeight  - 5;
			}				
		} else {
			// If userAgent is Safari, make some adjustments			
			win_h += scrollTop;
			if (refBot > win_h) {
				popup.defPosY = popup.defPosY - popup.currentHeight - scrollTop - 5;
			} else {
				popup.defPosY = popup.defPosY - scrollTop;
			}		
		}

		var OffsetX = 0;
		var OffsetY = 0;
		
		// Position and show the definition container
		eleContainer.style.left = (popup.defPosX + OffsetX) +"px";
		eleContainer.style.top = (popup.defPosY + OffsetY) +"px";		
		eleContainer.style.display = 'block';
		
		// Position and show the IFRAME container
		eleIFrame.style.left = (popup.defPosX + OffsetX) + "px";
		eleIFrame.style.top = (popup.defPosY + OffsetY) + "px";		
		eleIFrame.style.display = 'block';
		
	}
	return true;
}

function hideFormDefinition()
{
	 
	if(document.getElementById){
	
		defElem = document.getElementById("innerContainer")
		scrollUp(defElem)
	
		// Hide the popup container
		elem = document.getElementById("popupDefContainer")		
		elem.style.display = 'none';		
		
		// Hide each of the currently shown popup def divs  
		for(i=0; i<popup.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(popup.defPrefix + popup.currentRefArray[i]);			 
			eleDef.style.display = 'none';
		}	
		
		// Hide the Iframe
		var eleIFrame = document.getElementById("shield1");		
		eleIFrame.style.display = 'none';
	}
	
	// Clear the array so that no old items will show up next time
	popup.currentRefArray = new Array();
		
	return true;
}

function scrollUp(elem) 
{
    elem.scrollTop = 0;
}


/**
  This is our definitions object which will hold the HTML for the popup divs
  the rest of this javascript is generated in "controls/footer_js.ascx"
*/
formPopup = new Object();
formPopup.DEFS = '';

formPopup.DEFS += "<div id=\"formDef1\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		Aunque no tengas dirección de correo electrónico, podrías inscribirte para la muestra gratis de CRESTOR. Para obtener más información, llama al 877-TUCRESTOR."
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef2\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible to sign up for the Getting It Right program. To receive ongoing program mailings, a free 30-day trial certificate, and a \"Check the Facts\" starter kit, call <span class=\"nobr\">877-TUCRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef3\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		Aunque no tengas dirección de correo electrónico, podrías inscribirte para la muestra gratis de CRESTOR. Para obtener más información, llama al 877-TUCRESTOR.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef4\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible to sign up for the CRESTOR patient support program. For more information, call <span class=\"nobr\">877-TUCRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef5\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you may still be eligible to sign up for other off-line patient support resources. For more information, call <span class=\"nobr\">877-TUCRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef6\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		If you don't have an e-mail address, you are still eligible for the CRESTOR rebate. For more information, call <span class=\"nobr\">877-TUCRESTOR</span>.";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";

formPopup.DEFS += "<div id=\"formDef7\" CLASS=\"popupDefinitions\">";
formPopup.DEFS += "	<div class=\"formDef_content\">";
formPopup.DEFS += "		High blood pressure<br>";
formPopup.DEFS += "		Diabetes<br>";
formPopup.DEFS += "		Age<br>";
formPopup.DEFS += "		Family history<br>";
formPopup.DEFS += "		Smoking<br><br>";
formPopup.DEFS += "		<div class='smallText'>*This is not a complete list. Talk to your doctor about other health factors that make managing cholesterol more important.</div>";
formPopup.DEFS += "		</div>";													
formPopup.DEFS += "</div>";


function showpopup(popupDiv, grayoutbackground){
    
   
    
    if(grayoutbackground){
        
        if (document.getElementById('grayout')){
            var grayout = document.getElementById('grayout')
            if(grayout.style.display == "none" || popupDiv.style.display == ""){
                grayout.style.display="block"
            }else{
                grayout.style.display="none"
            }
        }       
    }
    
    if (popupDiv){
      if(popupDiv.style.display == "none" || popupDiv.style.display == ""){
        var dropdowns = document.getElementsByTagName('select')
        var i;
        for (i=0; i<dropdowns.length; i++){
            dropdowns[i].style.visibility="hidden"
        }
        popupDiv.style.display="block"
      }else{
        var dropdowns = document.getElementsByTagName('select')
        var i;
        for (i=0; i<dropdowns.length; i++){
            dropdowns[i].style.visibility="visible"
        }
        popupDiv.style.display="none"
      }
    }
  }
  
// This function is for popups that need tranparency in ie6, the png fix doesn't seem to like display: none, so instead the popup is moved off screen, and then moved on screen
function showpopup2(popupDiv, grayoutbackground, left){
    
   
    
    if(grayoutbackground){
        
        if (document.getElementById('grayout')){
            var grayout = document.getElementById('grayout')
            if(grayout.style.display == "none" || popupDiv.style.left == "" || parseInt(popupDiv.style.left) < 0){
                grayout.style.display="block"
            }else{
                grayout.style.display="none"
            }
        }       
    }
    
    if (popupDiv){
      if((parseInt(popupDiv.style.left) < 0) || (popupDiv.style.left == '')){
        var dropdowns = document.getElementsByTagName('select')
        var i;
        for (i=0; i<dropdowns.length; i++){
            dropdowns[i].style.visibility="hidden"
        }
        popupDiv.style.left=left + 'px';
      }else{
        var dropdowns = document.getElementsByTagName('select')
        var i;
        for (i=0; i<dropdowns.length; i++){
            dropdowns[i].style.visibility="visible"
        }
        popupDiv.style.left=-1000 + 'px';
      }
    }
  }
  