// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
//	utils.js
//
//	Thomas E. George
//	June 15, 2011
//
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

var _isIEBU = document.all ? true : false;

var _bDebugModeU = false;  // debug mode flag (verbose error reporting) - set to false when deployed

var controlU = null;
//var bSearchBoxFocused = false;
var iLastScrollTop = -1
var bForceSearchBoxFocus = false;
var eSubTabs = null;

// --------------------------------------------------------------------------------------------------
function setCookie(cookieName, value, exdays)
{

    try
    {
		var exdate = null;
	
		if(exdays != null)
		{
			exdate = new Date();
			exdate.setDate(exdate.getDate() + exdays);
		}
	
		var c_value = escape(value) + ((exdays == null) ? "" : ";expires="+exdate.toUTCString());
	
		document.cookie = cookieName + "=" + c_value;
    }
    catch(e)
    {
		if(_bDebugModeU)
		{
    		if(e.description && e.description.length != 0) { alert("setCookie(" + cookieName + ", " + value + ") : Exception caught [" + e.description + "]"); }
    	}
    }
    finally
    {
    }
}

// --------------------------------------------------------------------------------------------------
function getCookie(cookieName)
{
	var cookieVal;
	
	cookieVal = ""
	
    try
    {
		var i, x;
		
		var ARRcookies = document.cookie.split(";");

		for(i = 0; i < ARRcookies.length; i++)
		{
			x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			x = x.replace(/^\s+|\s+$/g,"");
		  
			if(x == cookieName)
			{
				cookieVal = unescape(ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1));
				break;
			}
		}
    }
    catch(e)
    {
		if(_bDebugModeU)
		{
    		if(e.description && e.description.length != 0) { alert("getCookie(" + cookieName + ") : Exception caught [" + e.description + "]"); }
    	}
    }
    finally
    {
    }
	
	return(cookieVal);
}

function reloadPage(timeOutMS)
{
	if(timeOutMS < 0)
	{
		timeOutMS = 0;
	}
	
	setTimeout('window.location.reload()', timeOutMS);
}

function focusCtrl(sControlID, timeOutMS)
{
	//alert("in focusCtrl()");
	
	try
	{
		controlU = document.getElementById(sControlID);
		
		if(controlU && (controlU != null))
		{
			//controlU.focus();
			
			//alert("in focusCtrl() calling setTimeout()");
			
			if(timeOutMS < 0)
			{
				timeOutMS = 0;
			}
			
			setTimeout('controlU.focus()', timeOutMS);
		}
	}
	catch(e)
	{
		if(_bDebugModeU)
		{
			if(e.description && e.description.length != 0) { alert("focusCtrl(" + sControlID + ") : '" + e.name + "' Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
}

function focusSearchBox(sControlID, sCookieName) //, bForce)
{
	var rc = false;
	
	try
	{
		var sCookie = getCookie(sCookieName);
		
		//alert("focusSearchBox(" + sControlID + "), cookie '" + sCookieName + "' = '" + sCookie + "'; force=" + bForce);
		
		if(sCookie != "1") //bForce && bForceSearchBoxFocus || , !bSearchBoxFocused
		{
			//alert("focusSearchBox() focususing " + sControlID);
			//alert(_isIEBU);
			
			focusCtrl(sControlID, 5); //'HT_Search_Info' //(_isIEBU ? 0 : 5)
			
			//bSearchBoxFocused = true;
			
			setCookie(sCookieName, "1", null);
			
			//goToLastScrollTop();
			
			rc = true;
		}
		else
		{
			rc = false;
			
			//if(_isIEBU)
			//{
				//setTimeout('controlU = null;', 200);
				
				//eSubTabs = document.getElementById("ntstalli"); //"ntsubtabs");
				//if(eSubTabs && (eSubTabs != null))
				//{
				//	setTimeout('eSubTabs.focus()', 5);
				//}
				
				//alert(document.body.onload);
				//var fOnLoad = document.body.onload;
				//alert(fOnLoad);
				//var fOnLoadStr = fOnLoad.toString();
				//fOnLoadStr = fOnLoadStr.replace("focusSearchBox('HT_Search_Info','HTSearchInfoFocusCtrl');", "");
				//alert(fOnLoadStr);
				//window.onload = fOnLoadStr
				//alert(document.body.onload);
				
				//reloadPage(5);
				//doScrollTop();
				//alert("scrollTop = " + document.body.scrollTop);
				//alert("scrollTop = " + document.documentElement.scrollTop);
			//}
		}
	}
	catch(e)
	{
		if(_bDebugModeU)
		{
			if(e.description && e.description.length != 0) { alert("focusSearchBox(" + sControlID + ") : '" + e.name + "' Exception caught [" + e.description + "]"); }
		}
	}
	finally
	{
	}
	
	//return rc;
}

function setLastScrollTop()
{
	if(document.documentElement != null && document.documentElement.scrollTop != null)
	{
		var scrollTop = document.documentElement.scrollTop;

		//alert("setLastScrollTop() " + scrollTop);
		
		iLastScrollTop = scrollTop;
	}
}

function goToLastScrollTop()
{
	if(document.documentElement != null && document.documentElement.scrollTop != null)
	{
		var scrollTop = document.documentElement.scrollTop;
		
		//alert("goToLastScrollTop() " + scrollTop + " to " + iLastScrollTop);
		
		if(iLastScrollTop >= 0)
		{
			scrollTop = iLastScrollTop;
		}
	}
}

function doScrollTop()
{
	//alert("in doScrollTop()");
	
	if(document.documentElement != null && document.body != null && document.body.scrollTop != null)
	{
		//alert("doScrollTop() scrollTop = " + document.body.scrollTop);
		
		document.documentElement.scrollTop = document.body.scrollTop;
	}
}

