function Sleep(naptime)
{
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();
//	alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
	while(sleeping){
	 alarm = new Date();
	 alarmMSeconds = alarm.getTime();
	 if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
	}      
//	alert("Wakeup!");
}

function ToggleDiv(divid, liId)
{
    var theDiv = document.getElementById(divid);
	var theLi =  document.getElementById(liId);
	if(theDiv == null) return;
	if(theDiv == "undefined") return;
	
	//DEBUG START
	theLi = null;
	//DEBUG END

    curDisplay = theDiv.style.display;
    if(curDisplay.toLowerCase() == "block")
    {
        theDiv.style.display = "none";
		if(theLi != null && theLi != "undefined")
		{
			theLi.className = "collapsed";
		}
    }
    else
    {
        theDiv.style.display = "block";
		if(theLi != null && theLi != "undefined")
		{
			theLi.className = "expanded";
		}
    }
}

function ExpandDiv(divid, liId)
{
    var theDiv = document.getElementById(divid);
	var theLi =  document.getElementById(liId);
	if(theDiv == null) return;
	if(theDiv == "undefined") return;

    curDisplay = theDiv.style.display;
    if(curDisplay.toLowerCase() == "none")
    {
        theDiv.style.display = "block";
		if(theLi != null && theLi != "undefined")
		{
			theLi.className = "expanded";
		}
    }
}


function CapitalizeWords(inputString) 
{
	var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = inputString.toLowerCase();
	stringLen = tmpStr.length;
	if (stringLen > 0)
	{
	  for (i = 0; i < stringLen; i++)
	  {
		if (i == 0)
		{
		  tmpChar = tmpStr.substring(0,1).toUpperCase();
		  postString = tmpStr.substring(1,stringLen);
		  tmpStr = tmpChar + postString;
		}
		else
		{
		  tmpChar = tmpStr.substring(i,i+1);
		  if (tmpChar == " " && i < (stringLen-1))
		  {
		  tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
		  preString = tmpStr.substring(0,i+1);
		  postString = tmpStr.substring(i+2,stringLen);
		  tmpStr = preString + tmpChar + postString;
		  }
		}
	  }
	}
	return tmpStr;
}

function trim(stringToTrim) {
	if(stringToTrim == '') return stringToTrim;
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	if(stringToTrim == '') return stringToTrim;
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	if(stringToTrim == '') return stringToTrim;
	return stringToTrim.replace(/\s+$/,"");
}

function ToLocalTimeString(d)
{
	var l_d = d;
	var offset = l_d.getTimezoneOffset();
	var expdate = l_d.getTime();
	expdate -= offset*60*1000; //expires in offset minutes 
	l_d.setTime(expdate);

	var yyyy = l_d.getFullYear();
	var mm = l_d.getMonth()+1;
	var dd = l_d.getDate();
	var hh = l_d.getHours();
	var nn = l_d.getMinutes();
	var ss = l_d.getSeconds();

	var retVal = yyyy + "-";
	if(mm < 10) retVal += "0";
	retVal += mm + "-";
	if(dd < 10) retVal += "0";
	retVal += dd + " ";

	if(hh < 10) retVal += "0";
	retVal += hh + ":";
	if(nn < 10) retVal += "0";
	retVal += nn + " ";
//	if(ss < 10) retVal += "0";
//	retVal += ss + " ";
	
	return retVal;
}

function handleDropdownSelectionChanged(dropDown)
{
	try
	{
		selectedIndex = dropDown.selectedIndex;
		selectOption(dropDown.options[selectedIndex], dropDown.id);		
	}
	catch (err1)
	{
	}
}
function selectOption(givenOption, dSelectAdd)
{
	try
	{
		val = givenOption.value;
		theDropDown = document.getElementById(dSelectAdd);
		theDropDown.selectedIndex = 0;

		window.open(val, target='_self') ;
	}
	catch (err2)
	{
	}
}


////////   COOKIE FUNCTIONS   ////////////////////////////
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function setCookie(c_name, value, expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie(c_name)
{
	cookieValue=getCookie(c_name);
	if (cookieValue!=null && cookieValue!="")
	{
		return true;
	}
	else 
	{
		return false;
	}
}

