//Global Varables
var arrMasterShowHideDivList = new Array(); //Global array to cache HTML DOM to reduce JS memory footprint.
var objLinkText = {};		//Container for language dependent tooltip, link text generated server side.
//-------------------------------------------------------------------------------------

function showHideDiv(vsDivName, vsShowImageId, vsHideImageId, vsHrefId, vsToolTipCollapsed, vsToolTipExpanded){
	var objEl = document.getElementById(vsDivName);
	
	if (vsShowImageId !== null && vsHideImageId !== null) {
		var bimages = true;
		var objShowImgEl = document.getElementById(vsShowImageId);
		var objHideImgEl = document.getElementById(vsHideImageId);
	}

	if (vsHrefId !== null) {
		var objHrefEl = document.getElementById(vsHrefId);
	}

	if (objEl.style.display == "") {
		objEl.style.display = "none";
		if (bimages) {
			objShowImgEl.style.display = "";
			objHideImgEl.style.display = "none";
		}
		if (objHrefEl) {
			objHrefEl.title = vsToolTipCollapsed;
		}
	}
	else {
		objEl.style.display = "";
		if (bimages) {
			objShowImgEl.style.display = "none";
			objHideImgEl.style.display = "";
		}
		if (objHrefEl) {
			objHrefEl.title = vsToolTipExpanded;
		}
	}
	updateShowHideDiv();
}

//-------------------------------------------------------------------------------------


function InitalizeShowHideLinks(vsShowAllLinkText, vsHideAllLinkText, vsShowAllToolTip, vsHideAllToolTip, vsLanguage){
	objLinkText.showlink = vsShowAllLinkText;
	objLinkText.hidelink = vsHideAllLinkText;
	objLinkText.showtool = vsShowAllToolTip;
	objLinkText.hidetool = vsHideAllToolTip;

	updateShowHideDiv();
}
//-------------------------------------------------------------------------------------


function updateShowHideDiv(){
	var elemHidden = false;
	var elemVisible = false;
	var sHTML;
	
	if (arrMasterShowHideDivList.length <= 0){
		cacheExpandableDivs();
	}
	
	//get status of collapsable divs
	if (arrMasterShowHideDivList.length > 0) {
		for(var i = 0; i < arrMasterShowHideDivList.length; i++){
			if(arrMasterShowHideDivList[i].style.display === "none"){
				elemHidden = true;
			}else{
				elemVisible = true;
			}
		}
	}else{//No expandable divs exist, yet; assume divs will be written and hidden by default.
		elemHidden = true;
	}
	//update show link
	if (elemHidden == true) {
		sHTML = "<a title=\"" + objLinkText.showtool + "\" href=\"javascript:void(0)\" style=\"font-weight:bold;cursor:pointer;cursor:hand\" language=\"JavaScript\" onclick=\"ShowHideAllDiv(true);\">" + objLinkText.showlink + "</a>";
	}else{
		sHTML = objLinkText.showlink;
	}

	//add seperator
	sHTML += " | ";

	//update hide link
	if (elemVisible == true) {
		sHTML += "<a title=\"" + objLinkText.hidetool + "\" href=\"javascript:void(0)\" style=\"font-weight:bold;cursor:pointer;cursor:hand\" language=\"JavaScript\" onclick=\"ShowHideAllDiv(false);\">" + objLinkText.hidelink + "</a>";
	}else{
		sHTML += objLinkText.hidelink;
	}

	document.getElementById("ShowHideAll").innerHTML = sHTML;
}
//-------------------------------------------------------------------------------------

function cacheExpandableDivs() {
		var divList = document.getElementsByTagName("div");
		var nIndex = 0
		for(var i = 0; i < divList.length; i++){
			if (divList[i].id.indexOf('divId_') >= 0){
				arrMasterShowHideDivList[nIndex] = divList[i];
				if (arrMasterShowHideDivList[nIndex].style.display === "none") {
						arrMasterShowHideDivList[nIndex][0] = false;
				}else{
						arrMasterShowHideDivList[nIndex][0] = true;
				}
				nIndex++;
			}
		}
}
//-------------------------------------------------------------------------------------

function ShowHideAllDiv(vbShowAll) {
	var sShowHideFuncTemp, sShowHideFunc;
	
	if (arrMasterShowHideDivList.length <= 0){
		cacheExpandableDivs();
	}
	
	for(var i = 0; i < arrMasterShowHideDivList.length; i++){
		if (arrMasterShowHideDivList[i].id.indexOf('divId_') >= 0)
		{
			var objLinkTemp = document.getElementById('aId_' + arrMasterShowHideDivList[i].id.substring(6))
			var objShowImgTemp = document.getElementById('showimgId_' + arrMasterShowHideDivList[i].id.substring(6))
			var objHideImgTemp = document.getElementById('hideimgId_' + arrMasterShowHideDivList[i].id.substring(6))
			// we need to do this check here, otherwise, links that are already expanded
			// will collapse
			if ((arrMasterShowHideDivList[i].style.display == "none" && vbShowAll === true) || (arrMasterShowHideDivList[i].style.display == "" && vbShowAll === false)){
				// the return of this is a function defined as anonymous which calls the
				// real showHideDiv func, so we need to strip that other func off
				//sShowHideFuncTemp = document.getElementById(divList[i].id.substring(0, divList[i].id.indexOf('divId_')) + '_aId').onclick + '';
				if (objLinkTemp !== null){
					sShowHideFuncTemp = objLinkTemp.onclick + '';
				}else if (objShowImgTemp !== null) {//use the show image element as a backup
					sShowHideFuncTemp = objShowImgTemp.onclick + '';
				}else if (objHideImgTemp !== null) {//use the hide image element as a backup
					sShowHideFuncTemp = objHideImgTemp.onclick + '';
				}
				sShowHideFunc = sShowHideFuncTemp.substring(sShowHideFuncTemp.indexOf('{') + 1, sShowHideFuncTemp.indexOf('}'));
				//if(sShowHideFuncTemp.indexOf("javaScript")==0)
					if(sShowHideFunc == "") sShowHideFunc = sShowHideFuncTemp;
				//alert(sShowHideFunc + "\n" + sShowHideFuncTemp );
				// once we get to the real func, execute it
				eval(sShowHideFunc);
			}
		}
	}

	updateShowHideDiv();
}

//-------------------------------------------------------------------------------------

/*
function hideAllDiv(vsShowAllLinkText, vsHideAllLinkText, vsShowAllToolTip, vsHideAllToolTip)
{
	var sShowHideFuncTemp, sShowHideFunc;
	
	var imgPlus=new Image();
	imgPlus.src="/MPPF/images/plus.gif";

	var divList = document.getElementsByTagName("div");
	
	if (divList.length > 0) 
	{
	
		if (document.getElementById("ShowHideAll"))
		{
			document.getElementById("ShowHideAll").innerHTML = "<a title=\"" + vsShowAllToolTip + "\" href=\"javascript:void(0)\" style=\"cursor:pointer;cursor:hand\" language=\"JavaScript\" onclick=\"showAllDiv('" + objLinkText.showlink + "', '" + vsHideAllLinkText + "', '" + vsShowAllToolTip + "', '" + vsHideAllToolTip + "');\">" + objLinkText.showlink + "</a><br/><br/>"
		}
	}

	for(var i = 0; i < divList.length; i++)
	{
		if (divList[i].id.indexOf('divId_') >= 0)
		{
			// we need to do this check here, otherwise, links that are already collapsed
			// will expand
			if (document.getElementById(divList[i].id).style.display == "")
			{			
				// the return of this is a function defined as anonymous which calls the
				// real showHideDiv func, so we need to strip that other func off
				//sShowHideFuncTemp = document.getElementById(divList[i].id.substring(0, divList[i].id.indexOf('divId_')) + '_aId').onclick + '';
				sShowHideFuncTemp = document.getElementById('aId_' + divList[i].id.substring(6)).onclick + '';
				sShowHideFunc = sShowHideFuncTemp.substring(sShowHideFuncTemp.indexOf('{') + 1, sShowHideFuncTemp.indexOf('}'));
				// once we get to the real func, execute it
				eval(sShowHideFunc);
			}
		}
	}
}
*/
//-------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------
/*
function showAllDelivery(vsShowAllLinkText, vsHideAllLinkText, vsShowAllToolTip, vsHideAllToolTip)
{
	var sShowHideFuncTemp, sShowHideFunc;

	var imgMinus=new Image();
	imgMinus.src="/MPPF/images/minus.gif"

	var divList = document.getElementsByTagName("div");
	
	if (divList.length > 0) 
	{
		if (document.getElementById("DeliveryOfCare"))
		{
			document.getElementById("DeliveryOfCare").innerHTML = "<a title=\"" + vsHideAllToolTip + "\" href=\"javascript:void(0)\" style=\"cursor:pointer;cursor:hand\" language=\"JavaScript\" onclick=\"hideAllDelivery('" + vsShowAllLinkText + "', '" + vsHideAllLinkText + "', '" + vsShowAllToolTip + "', '" + vsHideAllToolTip + "');\">" + vsHideAllLinkText + "</a><br/><br/>"
		}
	}
		
	for(var i = 0; i < divList.length; i++)
	{
		if (divList[i].id.indexOf('divId_') >= 0 && divList[i].id.indexOf('Inspections-') == -1)
		{
			// we need to do this check here, otherwise, links that are already expanded
			// will collapse
			if (document.getElementById(divList[i].id).style.display == "none")
			{
				// the return of this is a function defined as anonymous which calls the
				// real showHideDiv func, so we need to strip that other func off
				//sShowHideFuncTemp = document.getElementById(divList[i].id.substring(0, divList[i].id.indexOf('divId_')) + '_aId').onclick + '';
				sShowHideFuncTemp = document.getElementById('aId_' + divList[i].id.substring(0, divList[i].id.indexOf('divId_'))).onclick + '';
				sShowHideFunc = sShowHideFuncTemp.substring(sShowHideFuncTemp.indexOf('{') + 1, sShowHideFuncTemp.indexOf('}'));
				// once we get to the real func, execute it
				eval(sShowHideFunc);
			}
		}
	}
}

//-------------------------------------------------------------------------------------

function hideAllDelivery(vsShowAllLinkText, vsHideAllLinkText, vsShowAllToolTip, vsHideAllToolTip)
{
	var sShowHideFuncTemp, sShowHideFunc;
	
	var imgPlus=new Image();
	imgPlus.src="/MPPF/images/plus.gif";

	var divList = document.getElementsByTagName("div");
	
	if (divList.length > 0) 
	{
	
		if (document.getElementById("DeliveryOfCare"))
		{
			document.getElementById("DeliveryOfCare").innerHTML = "<a title=\"" + vsShowAllToolTip + "\" href=\"javascript:void(0)\" style=\"cursor:pointer;cursor:hand\" language=\"JavaScript\" onclick=\"showAllDelivery('" + vsShowAllLinkText + "', '" + vsHideAllLinkText + "', '" + vsShowAllToolTip + "', '" + vsHideAllToolTip + "');\">" + vsShowAllLinkText + "</a><br/><br/>"
		}
	}

	for(var i = 0; i < divList.length; i++)
	{
		if (divList[i].id.indexOf('divId_') >= 0 && divList[i].id.indexOf('Inspections-') == -1)
		{
			// we need to do this check here, otherwise, links that are already collapsed
			// will expand
			if (document.getElementById(divList[i].id).style.display == "")
			{			
				// the return of this is a function defined as anonymous which calls the
				// real showHideDiv func, so we need to strip that other func off
				sShowHideFuncTemp = document.getElementById('aId_' + divList[i].id.substring(0, divList[i].id.indexOf('divId_'))).onclick + '';
				sShowHideFunc = sShowHideFuncTemp.substring(sShowHideFuncTemp.indexOf('{') + 1, sShowHideFuncTemp.indexOf('}'));
				// once we get to the real func, execute it
				eval(sShowHideFunc);
			}
		}
	}
}
*/
//-------------------------------------------------------------------------------------

function showHideNHCResults(vsID)
{
	var vDiv="Div-";
	
	// Look for the footer that is associated with this table.
	if (document.getElementById(vDiv + vsID) != null)
	{
		// ...and if it is currently hidden...
		if (document.getElementById(vDiv + vsID).style.display == 'none')
		{
			// ...then toggle it so that it will be displayed...
			showNHCTableRowsBody(vsID);
		}				
		else
		{
			// The elment is currently being displayed, so toggle it to be hidden...
			hideNHCTableRowsBody(vsID);
		}
	} 
}

//-------------------------------------------------------------------------------------
//This routine provides show functionality used to reveal the body of an
//HHA results table.
function showNHCTableRowsBody(vsID)
{
	// Some images do not display correctly in certain browsers. To help 
	// alleviate this issue,  the images are pre-loaded...
	var imgMinus=new Image();
	
	var vTableRow="TableRow-"
	var vImage="Image-"
	var vDiv="Div-"
	var vA="A-"
	
	imgMinus.src="/NHCompare/images/minus.gif"

	var trList = document.getElementsByTagName("tr");
	
	// Look at each table row on the page
	for(var i = 0; i < trList.length; i++)
	{
		// if the TR is a part of the table for the given HHA
		if ( trList[i].id == (vTableRow + vsID) )
		{
			// ...then toggle it so that it will be displayed...
			trList[i].style.display = '';
		}				
	} 
	
	// ...switch the image to collapse
	if (document.getElementById(vImage + vsID) != null)
	{
		document.getElementById(vImage + vsID).src = imgMinus.src;
		document.getElementById(vImage + vsID).title = "Click to hide the results for this Agency.";
	}
	
	// ...switch the title on the link
	if (document.getElementById(vA + vsID) != null)
	{
		document.getElementById(vA + vsID).title = "Click to hide the results for this Agency.";
	} 
				
	// ...reveal the div (footer with footnotes and anchor)
	if (document.getElementById(vDiv + vsID) != null)
	{
		document.getElementById(vDiv + vsID).style.display = '';
	}
			
	return false;
}

//-------------------------------------------------------------------------------------
//This routine provides show functionality used to hide the body of an
//HHA results table.
function hideNHCTableRowsBody(vsID)
{
	// Some images do not display correctly in certain browsers. To help 
	// alleviate this issue,  the images are pre-loaded...
	var imgPlus=new Image();
	
	var vTableRow="TableRow-"
	var vImage="Image-"
	var vDiv="Div-"
	var vA = "A-"
	imgPlus.src="/NHCompare/images/plus.gif"

	var trList = document.getElementsByTagName("tr");
	
	// Look at each table row on the page
	for(var i = 0; i < trList.length; i++)
	{
		// if the TR is a part of the table for the given HHA
		if ( trList[i].id == (vTableRow + vsID) )
		{
			// The elment is currently being displayed, so toggle it to be hidden...
			trList[i].style.display = 'none';
		}
	} 
	
	// ...switch the image to expand
	if (document.getElementById(vImage + vsID) != null)
	{
		document.getElementById(vImage + vsID).src = imgPlus.src;
		document.getElementById(vImage + vsID).title = "Click to show the results for this Agency.";
	}
	
	// ...switch the title on the link
	if (document.getElementById(vA + vsID) != null)
	{
		document.getElementById(vA + vsID).title = "Click to show the results for this Agency.";
	} 
				
	// ...hide the div (footer to the table containing footnote and anchor)
	if (document.getElementById(vDiv + vsID) != null)
	{
		document.getElementById(vDiv + vsID).style.display = 'none';
	}
			
	return false;
}

//-------------------------------------------------------------------------------------
//if checkboxId is being unchecked, then uncheck selectAllcheckboxID 
function UnCheckSelectAll(checkboxId, selectAllCheckboxId) 
{

	if (document.getElementById(checkboxId).checked == false) {
		document.getElementById(selectAllCheckboxId).checked = false;
	}
	
	return false;
}

//NHC Survey added with June 2009 release
//-------------------------------------------------------------------------------------
function SetCookie_openSurvey( url, name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the set variable is expired, make the correct 
	expiration time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;	//currently set for days and converted to milliseconds
	}

	//var expires_date = new Date( today.getTime() + (expires) );
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
	//End of cookie setting

	//Below is to refresh parent window
	//window.location.reload();

	var objSurveyDiv1 = document.getElementById("surveydivid1");
	var objSurveyDiv2 = document.getElementById("surveydivid2");

	if (objSurveyDiv1)
	{
		objSurveyDiv1.parentNode.removeChild(objSurveyDiv1);
	}
	if (objSurveyDiv2)
	{
		objSurveyDiv2.parentNode.removeChild(objSurveyDiv2);
	}
	
	// Use existing function to open up a new window, must be put after parent window refresh
	sWindowName="Surveypopup";
	sWindowAttribute="left=0,top=0,height=450,width=650,menubar=0,resizable=1,scrollbars=1";
	sURL=url;

	MCGSharedJS_OpenNewWindow(sURL, sWindowName, sWindowAttribute);

}



