// JavaScript Document
function initAll()
{
	initPopup(true); //initPopup done on page load (true)//
	checkLogin();
	checkForFlashContent();
	hideLoader();
	//alert("loaded");
}
//These are general scrpts to be used by all hThemes
////////////Get by Class/////////////////////////////////////////////////
//Function used to get elements by classnames
document.getElementsByClassName = function(cl)
{
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++)
	{
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};
function analyticTrackingCode(theURLmsg)
{
	try
	{
		//Google Analytic Code - Requires this script to be loaded in analytic
		pageTracker._trackPageview(theURLmsg);
	}
	catch(err)
	{
	}
}
/////////AJAX SCRIPT////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

function AJAXreq(AJAXURL, theDivId, genUse)
{
	//User to set some var to be used as needed
	var xhr = false;
	var genUses = "";
	genUses = genUse;
	hideLoader();
	startLoader();
	//AJAX Request//
	//Check for AJAX request type////
	try
	{
		// Firefox, Opera 8.0+, Safari
		xhr = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	//////////////////////////////////
	if (xhr)
	{
		xhr.onreadystatechange = function()
		{
			if (xhr.readyState == 4)
			{
				if (xhr.status == 200)
				{
					if (xhr.responseText)
					{
						//Updates the div by the id that is sent with the function call
						document.getElementById(theDivId).innerHTML = xhr.responseText;
						try
						{
							//To be defined in index file in case additional actions need be performed
							if(genUses)
							{
								//This function is defined as part of the hTheme scripts.js
								changePageFinished(genUses);
							}
						}
						catch(e)
						{
						}
						///Track site usage
						analyticTrackingCode(AJAXURL);

						//Hides the loader bar
						hideLoader();
						//Check if logged in
						checkLogin();
						checkForFlashContent();
					}
				}
				else
				{
					//ERROR only if in error state
					hideLoader();
					//alert("There was a problem with the content request: State = "+xhr.readyState+", Status = "+xhr.status+" -- AJAXreq");
					analyticTrackingCode(AJAXURL+"_ajax_fail_"+xhr.status);
					window.location="";
					//window.open(AJAXURL);
				}
			}
			else
			{
				//Not 4
			}
		};
		//Fire function based on request type
		xhr.open("GET", AJAXURL, true);
		xhr.send(null);
	}
	else
	{
		//ERROR
		hideLoader();
		alert("Sorry, could not create XMLHttpRequest. -- AJAXreq");
		analyticTrackingCode(AJAXURL+"_ajax_fail_"+xhr.status);
		window.location="";
	}
}
//General AJAX, just retuns value

function checkLoginAJAXReq(theURL, theReqType)
{
	var gen_xhr = false;
	var genAJAXOutput = new Object();
	//AJAX Request//
	//Check for AJAX request type////
	try
	{
		// Firefox, Opera 8.0+, Safari
		gen_xhr = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			gen_xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			gen_xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	//////////////////////////////////
	if (gen_xhr)
	{
		gen_xhr.onreadystatechange = function()
		{
			if (gen_xhr.readyState == 4)
			{
				if (gen_xhr.status == 200)
				{
						updateLoginLinks(theReqType, gen_xhr.responseText)
				}
				else
				{
					//ERROR only if in error state
					//alert("There was a problem with the LINK request: State = "+gen_xhr.readyState+", Status = "+gen_xhr.status+" -- checkLoginAJAXReq.");
					//window.location=wwwRoot;
				}
			}
		};
		//Fire function based on request type
		gen_xhr.open("GET", theURL, true);
		gen_xhr.send(null);
	}
	else
	{
		//ERROR
		alert("Sorry, could not create XMLHttpRequest -- checkLoginAJAXReq."+wwwRoot);
		window.location=wwwRoot;
	}


}
////LOGGED CHECKER Using AJAX///
//***This function controls the content for the login link, the portal link and the admin extra menus
//theNavPage is set on the page and is used to set the name of the page for breadcrums
function checkLogin()
{
	//Check
	checkLoginAJAXReq(wwwRoot+"/login/login_links.php", "loginLinks");
	checkLoginAJAXReq(wwwRoot+"/login/login_portal_links.php", "loginPortalLinks");
	checkLoginAJAXReq(wwwRoot+"/admin/admin_links.php", "extraMenus"); //Admin menus
	//checkLoginAJAXReq("admin/admin_options.php?theNav="+theNavPage, "eMenuNavAreas"); //Admin options
}
function updateLoginLinks(theReqType, theRespText)
{
	switch(theReqType)
	{
	case "loginLinks" :
		//Login text link
		var theLoginLink = document.getElementById("loginLink");
		if(theLoginLink != null)
		{
			theLoginLink.innerHTML = theRespText;
		}
	break;
	case "loginPortalLinks" :
		//Portal Button
		var theLoginPortalLink = document.getElementById("loginPortalLink");
		if(theLoginPortalLink != null)
		{
			theLoginPortalLink.innerHTML = theRespText;
		}
	break;
	case "extraMenus" :
		//Extra Admin menu items
		var theExtraMenu = document.getElementById("extraMenu");
		var theeMenuItemArea = document.getElementById("eMenuItemArea");
		if(theExtraMenu != null && theeMenuItemArea  != null)
		{
			if(theRespText == "")
			{
				theExtraMenu.setAttribute("style","display:none");
				try
				{
					theExtraMenu.style.display = "none";
				}
				catch(err)
				{}
			}
			else
			{
				theExtraMenu.setAttribute("style","display:");
				try
				{
					theExtraMenu.style.display = "";
				}
				catch(err)
				{}
			}
			theeMenuItemArea.innerHTML = theRespText;
		}
		//
	break;
	case "eMenuNavAreas" :
		//Extra Admin menu items
		var theeMenuNavArea = document.getElementById("eMenuNavArea");
		if(theeMenuNavArea != null)
		{
			theeMenuNavArea.innerHTML = theRespText;
		}
		//
	break;
	}
}
////

/////////////////////////////////////////////////////////////
///////////////////POPUPS////////////////////////////////////
//Hides popup on load - set in body tag for pages with popups
//intialize or reset popup
function initPopup(loadInit)
{
	var popArea0 = document.getElementsByClassName("popArea");
	var popArea1 = document.getElementsByClassName("popBoxbg");
	//var popCont0 = document.getElementsByClassName("popBoxCont");
	//popCont0.innerHTML = "";
	for (var i = 0; i < popArea0.length; i++)
	{
		popArea0[i].setAttribute("style","display:none");
		try
		{
			popArea0[i].style.display = "none";
		}
		catch(err)
		{}
	}
	for (var i = 0; i < popArea1.length; i++)
	{
		popArea1[i].setAttribute("style","display:none");
		try
		{
			popArea1[i].style.display = "none";
		}
		catch(err)
		{}
	}
	//Function to fire when popUp is closed
	try
	{
		//This function is defined as part of the hTheme scripts.js
		if(loadInit==false)
		{
			popUpClosed();
		}
	}
	catch(err)
	{}
}
//
function showPopup(theUrl, theWidth, theHeight, useScrollBar, popBGColor, popFadeBGColor)
{
	AJAXreq(theUrl, "popBoxCont", "");
	var popArea0 = document.getElementById("popArea");
	//var popArea1 = document.getElementsByClassName("popBoxbg");
	var popArea1 = document.getElementById("popBoxbg");
	var thePopBox = document.getElementById("popBox");
	var thePopBoxCont = document.getElementById("popBoxCont");
	//Display the pop area
	popArea0.setAttribute("style","display:block");
	try
	{
		popArea0.style.display = "block";
	}
	catch(err)
	{}
	//Display the pop area bg
	popArea1.setAttribute("style","display:block;");
	try
	{
		popArea1.style.display = "block";
	}
	catch(err)
	{}
	/* class based loop
	for (var i = 0; i < popArea1.length; i++)
	{
		popArea1[i].setAttribute("style","display:block;");
		try
		{
			popArea1[i].style.display = "block";
		}
		catch(err)
		{}
	}
	*/
	//set the hieght and width and scrolling for the popup box and content box
	//Set defaults
	if(theWidth=="" || theWidth==undefined || theWidth==null)
	{
			theWidth = "720";
	}
	if(theHeight=="" || theHeight==undefined || theHeight==null)
	{
			theHeight = "520";
	}
	if(useScrollBar=="true" || useScrollBar=="1")
	{
		useScrollBar=true;
	}
	else
	{
		useScrollBar=false;
	}
	theWidth = Number(theWidth);
	theHeight = Number(theHeight);
	//
	thePopBox.setAttribute("style","width:"+theWidth+"px; height:"+(theHeight+20)+"px;");
	thePopBoxCont.setAttribute("style","width:"+theWidth+"px; height:"+theHeight+"px;");
	if(useScrollBar)
	{
		thePopBoxCont.setAttribute("style","overflow:scroll;");
	}
	else
	{
		thePopBoxCont.setAttribute("style","overflow:hidden;");
	}
	try
	{
		thePopBox.style.width = theWidth+"px";
		thePopBox.style.height = (theHeight+20)+"px";
		thePopBoxCont.style.width = theWidth+"px";
		thePopBoxCont.style.height = theHeight+"px";
		//set scrollbar as requested
		if(useScrollBar)
		{
			thePopBoxCont.style.overflow = "scroll";
		}
		else
		{
			thePopBoxCont.style.overflow = "hidden";
		}
	}
	catch(err)
	{}
	//
	//Set popBoxbg color
	//alert(popFadeBGColor);
	if(popFadeBGColor!="" && popFadeBGColor!=undefined && popFadeBGColor!=null)
	{
		//popArea1.setAttribute("style","background:"+popFadeBGColor+";");
		try
		{
			popArea1.style.backgroundColor = popFadeBGColor;
		}
		catch(err)
		{}
	}
	else
	{
		//popArea1.setAttribute("style","background:#000000;");
		try
		{
			popArea1.style.backgroundColor = "#000000";
		}
		catch(err)
		{}
	}
	//sET thePopBox COLOR
	if(popBGColor!="" && popBGColor!=undefined && popBGColor!=null)
	{
		//thePopBox.setAttribute("style","background:"+popBGColor+";");
		try
		{
			thePopBox.style.backgroundColor = popBGColor;
		}
		catch(err)
		{}
	}
	else
	{
		//thePopBox.setAttribute("style","background:#FFFFFF;");
		try
		{
			thePopBox.style.backgroundColor = "#FFFFFF";
		}
		catch(err)
		{}
	}
}
function initFlyout(theElmID, theLinkID)
{
	/*//This is to clear all flyouts
	var theElms = document.getElementsByClassName("flyout");
	for(var i = 0; i < theElms.length; i++)
	{
		//disply:none;
		theElms.style.display = "none";
	}
	*/
	//alert(theLinkID);
	var theElmToFly = document.getElementById(theElmID);
	var theElmToClick = theLinkID;
	var theLinkText=new String(theElmToClick.innerHTML);
	var fOut = "+"; // replace this
	var fIn = "-"; // with this
	//theLinkText = theElmToClick.innerHTML;
	/*if (theElmToFly.style.display == "block")
	{
		theElmToFly.style.display = "none";
	}
	else
	{
		theElmToFly.style.display = "block";
	}*/
	if (theElmToFly.style.display == "block")
	{
		theElmToFly.style.display = "none";
	}
	else
	{
		theElmToFly.style.display = "block";
	}
	try
	{
		if(theLinkText.indexOf(fOut) > -1)
		{
			var pos = theLinkText.indexOf(fOut);
			theLinkText = "" + (theLinkText.substring(0, pos) + fIn +
			theLinkText.substring((pos + fOut.length), theLinkText.length));
			theElmToClick.innerHTML = theLinkText;
		}
		else if(theLinkText.indexOf(fIn) > -1)
		{
			var pos = theLinkText.indexOf(fIn);
			theLinkText = "" + (theLinkText.substring(0, pos) + fOut +
			theLinkText.substring((pos + fIn.length), theLinkText.length));
			theElmToClick.innerHTML = theLinkText;
		}
	}
	catch(err)
	{
	}
}
////////////////////////////////////////////////////////////////////
///////////////////////Loader////////////////////////////////////////
var loadInt;
var timeTilLoader = 1000; //mili sec till loader appears
function startLoader()
{
		loadInt = setInterval("timeLoader();", timeTilLoader);
}
///Timer for loader - loade will not appear on short loads (1 sec or less)
function timeLoader()
{
	showLoader();
	clearInterval(loadInt);
}
function showLoader()
{
	var theLoader = document.getElementById("loaderArea");
	theLoader.setAttribute("style","display:block;");
	try
	{
		theLoader.style.display = "block";
	}
	catch(err)
	{}
}
function hideLoader()
{
	var theLoader = document.getElementById("loaderArea");
	theLoader.setAttribute("style","display:none;");
	clearInterval(loadInt);
	try
	{
		theLoader.style.display = "none";
	}
	catch(err)
	{
	}
}
//Check for any flash on the page and set it up////////////////////////////////////////
//Example of div setup
//			<div style="width:284px; height:200px">
//				<div id="flash1" class="flashContent" title="/blocks/flash_blocks/RM000021--MortgageCalculator.swf">
//				<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
//				</div>
//			</div>
function checkForFlashContent()
{
	var theFlashContents = document.getElementsByClassName("flashContent");
	for(var i=0;i<theFlashContents.length;i++)
	{
		swfobject.embedSWF(theFlashContents[i].title, theFlashContents[i].id, "100%", "100%", "9","","",{menu:"false", quality:"high", salign:"cm", wmode:"transparent", swliveconnect:"true", allowscriptaccess:"sameDomain"} );
		//{menu:"false", quality:"high", scale:"noscale", salign:"cm", wmode:"transparent", swliveconnect:"true", allowscriptaccess:"sameDomain"}
	}
}

///////////////////////////////////////////////////////////////////
//Show Div (shows and hides any element)
function showDiv(theDiv, theAct, theHeight)
{
	var theHidee = document.getElementById(theDiv);
	if(theAct == true)
	{
		if(theHeight != "" && theHeight !== null && theHeight !== undefined)
		{
			theHidee.setAttribute("style","height:"+theHeight+";");
		}
		theHidee.setAttribute("style","display:block;");

		try
		{
			if(theHeight != "" && theHeight !== null && theHeight !== undefined)
			{
				theHidee.setAttribute("style","height:"+theHeight+";");
			}
			theHidee.style.display = "block";
		}
		catch(err)
		{}
	}
	else
	{
		theHidee.setAttribute("style","display:none;");
		try
		{
			theHidee.style.display = "none";
		}
		catch(err)
		{}
	}
}