/*

utils.js

Collection of utility JavaScript functions.

Author: Chris Trueman.

2000-11-17

*/

	//
	// trim
	//
	// Remove leading and triling whitespace.
	//
	function trim(src)
	{
		var s = 0;
		var e = 0;
		
		for(s = 0; s < src.length - 1; s++)
		{
			if(src.charAt(s) != ' ')
			{
				break;
			}
		}
		
		for(e = src.length; e > s; e--)
		{
			if(src.charAt(e - 1) != ' ')
			{
				break;
			}
		}
		
		return(src.substring(s, e));
	}

	// _Utils_MakeFullyVisible
	//
	// Make the selected HTML object completely visible including adjustments 
	// for scroll bars.  Usually used for hidden DIVs.
	//
	function _Utils_MakeFullyVisible(itemname) {
		var item = document.all(itemname);
		var theBody = document.body;

		// Make the item visible.
		item.style.top = 0;
		item.style.left = 0;
		item.style.display="block";

		// Relocate the item to an offset from the mouse position.
		item.style.pixelTop = event.clientY + theBody.scrollTop + 10;
		item.style.pixelLeft = event.clientX + theBody.scrollLeft + 10;
		
		// Make sure the item doesn't appear off the visible screen
		/*
		alert("pixTop: " + menu.style.pixelTop + "\n" +
					"posTop: " + menu.style.posTop + "\n" + 
					"dHeight: " + menu.clientHeight + "\n" +
					"pixBot: " + menu.style.pixelBottom + "\n" +
					"posBot: " + menu.style.posBottom + "\n" +
					"bHeight: " + document.body.clientHeight + "\n" 
					);
		*/
		if (item.style.pixelTop + item.clientHeight > theBody.clientHeight + theBody.scrollTop) {
			item.style.pixelTop = theBody.clientHeight - item.clientHeight - 5 + theBody.scrollTop;
		}
		
		if (item.style.pixelLeft + item.clientWidth > theBody.clientWidth + theBody.scrollLeft) { 
			item.style.pixelLeft = theBody.clientWidth - item.clientWidth - 5 + theBody.scrollLeft;
		}
	}

	function utils_ShowHide(id){
		var obj = document.getElementById(id);
		if (obj != null)
		{
			if (obj.style.display == "")
			{
				obj.style.display = "none";
			}
			else
			{
				obj.style.display = "";
			}
		}
	}

	function setAllCheckboxes(pchecked, pPrefix){
		var myForm = document.forms[0]; 
		for(i = 0; i < myForm.elements.length; i++) { 
			var myEl = myForm.elements[i]
			if(myEl.type == "checkbox") {
				if(myEl.id.indexOf(pPrefix) == 0) { 
					myEl.checked = pchecked
				}	
			} 
		} 
	}
	
	function toggleReadOnly(fieldID)
	{
		try
		{
			var field = document.getElementById(fieldID);
			if (field.readOnly) {
				field.style.color = "#000000"
                field.setAttribute("readOnly", "");
			} else {
				field.style.color = "#999999"
				field.setAttribute("readOnly", "true");
			}
		}
		catch(ex) {}
	}
	
	function isValidDate(dateStr) {
		// Checks if time is in HH:MM:SS AM/PM format.
		// The seconds and AM/PM are optional.
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

		var matchArray = dateStr.match(datePat); // is the format ok?
		if (matchArray == null) {
		alert(dateStr + " Date is not in a valid format.")
		return false;
		}
		month = matchArray[1]; // parse date into variables
		day = matchArray[3];
		year = matchArray[4];
		if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
		}
		if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		return false;
		}
		if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
		alert("February " + year + " doesn't have " + day + " days!");
		return false;
		}
		}
		return true;
	}
	
	function isValidTime(timeStr) {
		// Checks if time is in HH:MM:SS AM/PM format.
		// The seconds and AM/PM are optional.
		var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

		var matchArray = timeStr.match(timePat);
		if (matchArray == null) {
		alert("Time is not in a valid format.");
		return false;
		}
		hour = matchArray[1];
		minute = matchArray[2];
		second = matchArray[4];
		ampm = matchArray[6];

		if (second=="") { second = null; }
		if (ampm=="") { ampm = null }

		if (hour < 0  || hour > 23) {
		alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
		}
		//if (hour <= 12 && ampm == null) {
		//if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
		//alert("You must specify AM or PM.");
		//return false;
		//}
		//}
		if  (hour > 12 && ampm != null) {
		alert("You can't specify AM or PM for military time.");
		return false;
		}
		if (minute < 0 || minute > 59) {
		alert ("Minute must be between 0 and 59.");
		return false;
		}
		if (second != null && (second < 0 || second > 59)) {
		alert ("Second must be between 0 and 59.");
		return false;
		}
		return true;
	}

	
	function toggleEdit(fieldID) 
	{
			var field = document.getElementById(fieldID);
			if (field.readOnly || field.disabled) {
				field.style.color = "#000000"
                //field.setAttribute("readOnly", "");
				field.readOnly = false;
				field.disabled = false;
			} else {
				field.style.color = "#999999"
				//field.setAttribute("readOnly", "true");
				field.readOnly = true;
				field.disabled = true;
			}

	}

	function toggleDivVisibility(divName)
	{
		try
		{
			var hiddenDiv = document.getElementById(divName);
			var style = hiddenDiv.style;
			style.display = (style.display == "none" ? "" : "none");	
		}
		catch(ex) {}
	}
	function setDivVisibility(divName, visible)
	{
		try
		{
			var hiddenDiv = document.getElementById(divName);
			//alert(divName + hiddenDiv);
			var style = hiddenDiv.style;
			style.display = (visible ? "" : "none");	
		}
		catch(ex) {}
	}

	function toggleStartEnd(elConvDate)
	{
		try
		{
			var convDate = document.getElementById(elConvDate)
			if (convDate.value == "CD")		//Show start and end date only if Custom Date Range is selected
			{
				setDivVisibility("FromDateLabel", true);
				setDivVisibility("FromDateInput", true);
				setDivVisibility("ToDateLabel", true);
				setDivVisibility("ToDateInput", true);
			}
			else
			{
				setDivVisibility("FromDateLabel", false);
				setDivVisibility("FromDateInput", false);
				setDivVisibility("ToDateLabel", false);
				setDivVisibility("ToDateInput", false);
			}
		}
		catch(ex) {}
	}

	function updateWdTextOld(pdate, pspan){
		var dt = new Date(pdate);

		if (isNaN(dt)) return;

		var text = "";
		switch(dt.getDay()){
		case 0: text = 'Sunday'; break;
		case 1: text = 'Monday'; break;
		case 2: text = 'Tuesday'; break;
		case 3: text = 'Wednesday'; break;
		case 4: text = 'Thursday'; break;
		case 5: text = 'Friday'; break;
		case 6: text = 'Saturday'; break;
		}

		document.getElementById(pspan).innerText = text;
	}

	function updateWdText(pdate, pspan){
		//Split date, does not matter whether month and day are reversed.  The getDayname requires zero padding.
		pdate = zeroPadDate(pdate);
        dt = new Date.fromString(pdate);
		if (isNaN(dt)) return;
        var text = dt.getDayName(false);    
		document.getElementById(pspan).innerHTML = text;
	}

	function ShowModalWindow(page, width, height){
		
			// Netscape Modal dialog call
			windowRef = window.open(page, "popup", "modal=yes,alwaysRaised=yes,height=" + height + ",width=" + width);

		
	}
	
    function checkClosedDays(date) 
    {   
        var dateString = new Date(date).asString();    
        for (i = 0; i < eventArray.length; i++) 
        {   
            if(dateString == eventArray[i][0])
            {                   
                return [true,eventArray[i][1]];
            }   
        }   
        return [true,''];        
    }
	

    function GetXmlHttpObject()
    {
	    var xmlHttp=null;
	    try
	    {
		    // Firefox, Opera 8.0+, Safari
		    xmlHttp=new XMLHttpRequest();
	    }
	    catch (e)
	    {
		    // Internet Explorer
		    try
		    {
			    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		    }
		    catch (e)
		    {
			    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		    }
	    }
	    return xmlHttp;
    }

	
	var LastDebugUpdateMode
    function DebugUpdateSessionLocaleVariable(locid,applid,mode)
    {
		//alert ("updateing session: loc: " + locid + " applid: " + applid );
	    var xmlHttp=GetXmlHttpObject();	
	    if (xmlHttp==null)
	    {
		    return;
	    } 
	    xmlHttp.onreadystatechange = function()
	    {
		    if(xmlHttp.readyState==4)
		    {
				//alert("done");
				if(LastDebugUpdateMode == 1)
				{
    				window.location.reload(true);	
				}
				else
				{
				    window.location = "/navigator"
				}
		    }			
	    }				
	    var url="..\\misc\\updateLocAppl.asp";
	    url=url+"?loc="+locid+"&applid="+applid+"&mode="+mode;
	    LastDebugUpdateMode = mode;
		//alert("url: " + url);
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);
    }

	function dismissMSG(adID,userID,objType,divName)
	{
		var xmlHttp=GetXmlHttpObject();	
//		alert ("dismissing ad: " + adID + " userID: " + userID);
		if (xmlHttp==null)
		{
			
		} 
		else
		{
			xmlHttp.onreadystatechange = function()
			{
				if(xmlHttp.readyState==4)
				{
//					alert(xmlHttp.responseText);
				}			
			}				
			var url = "../MSGBlockMSG.asp";
			url=url+"?msgID="+adID+"&id="+userID+"&type="+objType+"&dt="+Date();
			var adDiv = document.getElementById(divName);
			adDiv.innerHTML="";
			adDiv.style.display = 'none';

			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}

	}


	function markAdAsInvalid(adID)
	{
		var dumb;
//		var xmlHttp=GetXmlHttpObject();	
//		if (xmlHttp==null)
//		{
//			
//		} 
//		else
//		{
//			xmlHttp.onreadystatechange = function()
//			{
//				if(xmlHttp.readyState==4)
//				{
//				}			
//			}				
//			var url = "../MsgMarkAsInvalid.asp";
//			url=url+"?msgID="+adID+"&dt="+Date();
//			xmlHttp.open("GET",url,true);
//			xmlHttp.send(null);
//		}	
//	
	}


	function onAdImageLoadError(source,adDivName,adID)
	{
		//markAdAsInvalid(adID);
		source.onError ="";
		source.style.display = 'none'
		document.getElementById(adDivName).style.display='none';
		return true;
	}


	function SubAdImageLoadDone(source)
	{
		//cancel the timeout
		//this subWebImageLoadTimeoutID 
		clearTimeout(subWebImageLoadTimeoutID)
		//alert ("load done clearing timeout: " + subWebImageLoadTimeoutID);
	}

	function ImageLoadTimeout(imageDivName)
	{
		var div = document.getElementById(imageDivName);
		div.style.display='none';
		var imgName = imageDivName + "_img"
		var img = document.getElementById(imgName);		
		img.src = "";
//		markAdAsInvalid(img.getAttribute("adid"));
	}

	// helper function to add required zero characters to fixed length fields
	function zeroPadDate(pDate) {
			var delimChar = (pDate.indexOf("/") != -1) ? "/" : "-"; 
			var aSplitDate = pDate.split(delimChar);
			return zeroPad(aSplitDate[0])+delimChar+zeroPad(aSplitDate[1])+delimChar+aSplitDate[2]
	}

	// helper function to add required zero characters to fixed length fields
	function zeroPad(num) {
			var s = '0'+num;
			return s.substring(s.length-2)
	}
