// new formulary object
formulary = new Object();
formulary.links = new Array();
formulary.linkPrefix = "forumlaryDef";
formulary.defPrefix = "formDef";
formulary.offsetX = 6;
formulary.offsetY = 0;
formulary.defPosX = 0;
formulary.defPosX = 0;
formulary.displayTime = 0;
formulary.hideTime = 200;
formulary.displayTimer = '';
formulary.hideTimer = '';
formulary.currentDef = '';
formulary.currentRefArray = new Array();
formulary.defaultHeight = 320;
formulary.shortHeight = 200;
formulary.defaultWidth = 380;
formulary.currentHeight = 0;
formulary.titleArray = ["Tiers", "Restrictions"]

function startFormularyDefs()
{	
	// Add all of the formulary 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(formulary.linkPrefix) == 0 ){	 
			var ele = document.links[i];			
			ele.arrFormularyList = createFormularyDefList(document.links[i].id);		
			ele.onclick = displayFormularyDef;
		}
	}
	
	document.getElementById(formulary.defPrefix + "1").style.display = 'none';
	document.getElementById(formulary.defPrefix + "2").style.display = 'none';
	 
	
	return true;
}

function createFormularyDefList(strFormularyID) {
	// Formulary lists are extracted from the links ID attribute, which takes the form "formulary 1,2,3,4"  	
	var arrRefList = strFormularyID.split(' ');	
	var arrFormularyList = arrRefList[1].split(',');	
	return arrFormularyList
	 
}

function displayFormularyDef(e)
{	
	hideFormDefinition();
	/* used to capture the event in IE or mozilla */
	e = e || window.event;		
	setCurrentRefArray(this.arrFormularyList);
	setCurrentPos(e.clientX, e.clientY);		
	formulary.displayTimer = setTimeout('showFormDefinition()', formulary.displayTime);	
	
	var elemTitle = document.getElementById("formularyDefTitle");
	var intDefID = parseInt(this.arrFormularyList[0]) - 1;
	
	elemTitle.innerHTML = formulary.titleArray[intDefID];
	
	return true;
}

function hideFormularyDef(e)
{
	/* used to capture the event in IE or mozilla */
	e = e || window.event;
	formulary.hideTimer = setTimeout('hideFormDefinition()', formulary.displayTime);
	return true;
}

function setCurrentRefArray(arrFormularys) 
{	
	// Store the array containing the links formularys	
	formulary.currentRefArray = arrFormularys
	return true;
}

function setCurrentPos(x, y)
{
	var scrollX = setScrollX();
	var scrollY = setScrollY();
	formulary.defPosX = x + formulary.offsetX + scrollX;
	formulary.defPosY = y + formulary.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){	
		
		var eleContainer = document.getElementById("formularyDefContainer");		
		
		// Resize height of formulary container if only 1 element exists, else use default height	
		if(formulary.currentRefArray.length < 2) {
			formulary.currentHeight = formulary.shortHeight;
		} else {
			formulary.currentHeight = formulary.defaultHeight;			
		}
		
		eleContainer.style.height = String(formulary.currentHeight) + 'px';
		
		// This child element must be dynamically resized for IE
		var eleContainerChild = document.getElementById("innerContainer");
		eleContainerChild.style.height = String(formulary.currentHeight - 17) + 'px';
		
		
		// Display each of the formularys divs for this link
		for(i=0; i<formulary.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(formulary.defPrefix + formulary.currentRefArray[i]);
			eleDef.style.display = 'block';
		}
		
		
		// Check if formulary 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 = formulary.defPosX + formulary.defaultWidth + 5;		
		if (refRight > win_w) {			
			formulary.defPosX = formulary.defPosX - formulary.defaultWidth - 20;
		}
		
		// check if div will be beyond window height			
		var refBot = (formulary.defPosY - scrollTop) + formulary.currentHeight + 5;			
		var strUserAgent = navigator.userAgent;			
		if (strUserAgent.indexOf("Safari") == -1) {
			// If userAgent is NOT Safari (must be IE, or FF)
			var refBot = (formulary.defPosY - scrollTop) + formulary.currentHeight + 5;
			if (refBot > win_h) {
				formulary.defPosY = formulary.defPosY - formulary.currentHeight  - 5;
			}				
		} else {
			// If userAgent is Safari, make some adjustments			
			win_h += scrollTop;
			if (refBot > win_h) {
				formulary.defPosY = formulary.defPosY - formulary.currentHeight - scrollTop - 5;
			} else {
				formulary.defPosY = formulary.defPosY - scrollTop;
			}		
		}
 
		// Position and show the definition container
		
		eleContainer.style.left = "233px";
		eleContainer.style.top = formulary.defPosY+"px";		
		eleContainer.style.display = 'block';
		
		
	}
	return true;
}

function hideFormDefinition()
{
	if(document.getElementById){
	
		defElem = document.getElementById("innerContainer")
		scrollUp(defElem)
	
		// Hide the formulary container
		elem = document.getElementById("formularyDefContainer")		
		elem.style.display = 'none';		
		
		// Hide each of the currently shown formulary def divs  
		for(i=0; i<formulary.currentRefArray.length; i++) {		
			var eleDef = document.getElementById(formulary.defPrefix + formulary.currentRefArray[i]);
			eleDef.style.display = 'none';
		}	
	}
	
	// Clear the array so that no old items will show up next time
	formulary.currentRefArray = new Array();
		
	return true;
}

function scrollUp(elem) 
{
    elem.scrollTop = 0;
}


/**
  The popup DIV HTML content is generated in the page /p/formulary/index.aspx
*/

 