/** UtilityFunctions.js -- This file contains some general utility JavaScript functions. */

/** Display a browser window with the specified URL. */
function BrowserWindow(sUrl, nWidth, nHeight)
{
	if ((sUrl != null) && (sUrl != undefined) &&
		(nWidth != null) && (nWidth != undefined) &&
		(nHeight != null) && (nHeight != undefined))
	{
		window.open(sUrl, "", "width=" + nWidth.toString() + ",height=" + nHeight.toString() + ",toolbar=1,status=1,directories=1,location=1,menubar=1,scrollbars=1");
	}
}

/** Display a pop-up window with the specified URL. */
function PopupWindow(sUrl, nWidth, nHeight)
{
	if ((sUrl != null) && (sUrl != undefined) &&
		(nWidth != null) && (nWidth != undefined) &&
		(nHeight != null) && (nHeight != undefined))
	{
		window.open(sUrl, "", "width=" + nWidth.toString() + ",height=" + nHeight.toString() + ",toolbar=0,status=0,directories=0,location=0,menubar=0,scrollbars=0");
	}
}

/** Display a sizeable pop-up window with the specified URL. */
function SizeablePopupWindow(sUrl, nWidth, nHeight)
{
	if ((sUrl != null) && (sUrl != undefined) &&
		(nWidth != null) && (nWidth != undefined) &&
		(nHeight != null) && (nHeight != undefined))
	{
		window.open(sUrl, "", "width=" + nWidth.toString() + ",height=" + nHeight.toString() + ",toolbar=0,status=0,directories=0,location=0,menubar=0,scrollbars=1,resizable=1");
	}
}

/** Display an image popup with the specified dimensions */
function PopupImage(sUrl, sImage, nWidth, nHeight)
{
	if ((sUrl != null) && (sUrl != undefined) &&
	    (sImage != null) && (sImage != undefined) &&
		(nWidth != null) && (nWidth != undefined) &&
		(nHeight != null) && (nHeight != undefined))
	{
		// Construct name/value pairs for the image attributes
		var oImage = new NameValuePair("Image", sImage);
		var oWidth = new NameValuePair("Width", nWidth.toString());
		var oHeight = new NameValuePair("Height", nHeight.toString());

		// Construct a URL with parameters
		var sFullUrl = sUrl + "?" + oImage.Serialize() + "&" + oWidth.Serialize() + "&" + oHeight.Serialize();

		// Display the pop-up window
		SizeablePopupWindow(sFullUrl, nWidth, nHeight);
	}
}

/** NameValuePair -- This data structure represents a simple name/value pair. */
function NameValuePair(sName, sValue)
{
	this.m_sName = sName;
	this.m_sValue = sValue;
	this.Serialize = NameValuePair_Serialize;
	this.Deserialize = NameValuePair_Deserialize;
}

function NameValuePair_Serialize()
{
	return escape(this.m_sName + "=" + this.m_sValue);
}

function NameValuePair_Deserialize(sValue)
{
	this.m_sName = "";
	this.m_sValue = "";

	if ((sValue != null) && (sValue != undefined))
	{
		sValue = unescape(sValue);

		if ((sValue != null) && (sValue != undefined))
		{
			var nDelimiter = sValue.indexOf("=");

			if (nDelimiter > -1)
			{
				this.m_sName = sValue.substr(0, nDelimiter);
				this.m_sValue = sValue.substr(nDelimiter + 1, sValue.length);
			}
		}
	}
}

/** GetUrlParameters -- Return an array of name/value pair objects that were found in the Url. */
function GetUrlParameters(sUrl)
{
	var oParameters = new Array();
	
	if ((sUrl != null) && (sUrl != undefined))
	{
		// Find the start of the URL parameters
		var nStartIndex = sUrl.indexOf("?");
		var nEndIndex = -1;
		var sParameter = "";
		var oParameter = null;
		
		if (nStartIndex > -1)
		{
			nStartIndex = nStartIndex + 1;

			while (nStartIndex < sUrl.length)
			{
				// Find the end of the current or last parameter
				nEndIndex = sUrl.indexOf("&", nStartIndex);

				if (nEndIndex < 0)
					nEndIndex = sUrl.length;
				
				// Make sure that there is a current or last parameter
				if (nStartIndex < nEndIndex)
				{
					sParameter = sUrl.substring(nStartIndex, nEndIndex);

					if (sParameter.length > 0)
					{
						oParameter = new NameValuePair("", "");
						oParameter.Deserialize(sParameter);

						if ((oParameter.m_sName.length > 0) || (oParameter.m_sValue.length > 0))
							oParameters[oParameters.length] = oParameter;
					}
				}
				
				// Move to the next parameter (if any)
				nStartIndex = nEndIndex + 1;
			}
		}
	}

	return oParameters;
}

/** GetCookie -- Get the value of a cookie (or null if it does not exist). */
function GetCookie(sName)
{
	var sValue = null;
	var sCookie = document.cookie;
	var sSearch = sName + "=";
	var nStartIndex = sCookie.indexOf(sSearch);
	var nEndIndex = -1;

	if (nStartIndex > -1)
	{
		nStartIndex += sSearch.length;
		nEndIndex = sCookie.indexOf(";", nStartIndex);

		if (nEndIndex > -1)
			sValue = unescape(sCookie.substring(nStartIndex, nEndIndex));
	}

	return sValue;
}

/** SetCookie -- Set the value of a cookie. */
function SetCookie(sName, sValue)
{
	document.cookie = sName + "=" + escape(sValue);
}

/** DeleteCookie -- Delete the cookie. */
function DeleteCookie(sName)
{
	document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

/** ClearSelect -- Clear the select element of all options. */
function ClearSelect(oSelect, bKeepNull)
{
	if (oSelect != null)
	{
    	var oOptions = oSelect.options;
    	
    	if (oOptions != null)
    	{
        	for (var nIndex = 0; nIndex < oOptions.length; )
            {
            	if ((bKeepNull == true) && (oOptions[nIndex].value == "NULL"))
            		nIndex++;
				else
                	oOptions[nIndex] = null;
            }
        }
	}
}

/** FindListIndex -- Find the item in the select element. */
function FindListIndex(oSelect, sItem, bPartialMatchOK)
{
	if (oSelect != null)
	{
    	var oOptions = oSelect.options;
    	
    	if (oOptions != null)
    	{
        	for (var nIndex = 0; nIndex < oOptions.length; nIndex++)
            {
				if (bPartialMatchOK)
				{
					if (oOptions[nIndex].value.indexOf(sItem) > -1)
						return nIndex;
				}
				else
				{
					if (oOptions[nIndex].value == sItem)
						return nIndex;
				}
            }
        }
	}
	return -1;
}

/** AddNull -- Add a NULL element to the select. */
function AddNull(oSelect, nNullSize)
{
	if ((oSelect == null) || (oSelect.options == null))
		return;

	var sNull = "";
	
	for (var nIndex = 0; nIndex < nNullSize; nIndex++)
		sNull = sNull + " ";
	
	oSelect.options[oSelect.options.length] = new Option(sNull, "NULL");
}

/** RemoveNull -- Remove the NULL item from the select element. */
function RemoveNull(oSelect)
{
	if ((oSelect == null) || (oSelect.options == null))
		return;

	for (var nIndex = 0; nIndex < oSelect.options.length; nIndex++)
	{
		var sValue = oSelect.options[nIndex].value.toUpperCase();
		
		if (sValue == "NULL")
		{
			oSelect.options[nIndex] = null;
			return;
		}
	}
}

/** CurrencyString -- Return the value represented as a currency string (ie. 10 -> $10.00). */
function CurrencyString(oValue)
{
	var sValue = "";
	
	if ((oValue != null) && (oValue != undefined))
	{
		var oNumber = new Number(oValue);
		
		if (oNumber != NaN)
		{
    		sValue = oNumber.toString();
    		
    		if ((sValue != null) && (sValue != undefined))
    		{
    			var nDecimal = sValue.indexOf(".");
    			
    			if (nDecimal > -1)
    			{
					if ((sValue.length - nDecimal - 1) < 2)
					{
						while ((sValue.length - nDecimal - 1) < 2)
							sValue += "0";
					}
					else
					{
						if ((sValue.length - nDecimal - 1) > 2)
							sValue = sValue.substr(0, nDecimal + 3);
					}
    			}
    			else
    			{
    				sValue += ".00";
    			}
    		}
		}
	}
	return sValue;
}

/** PadString -- Pad a string of text with spaces to fill in the specified width. The string can be aligned to the "left", "center", or "right". */
function PadString(sString, nWidth, sAlign)
{
	var sResult = "";

	if ((sString != null) && (sString != undefined))
	{
		var nSpaces = nWidth - sString.length;

		if (nSpaces > 0)
		{
			if (sAlign.toUpperCase() == "CENTER")
			{
				
			}
			else if (sAlign.toUpperCase() == "RIGHT")
			{
				for (var nIndex = 0; nIndex < nSpaces; nIndex++)
					sResult += " ";
				sResult += sString;
			}
			else
			{
				sResult += sString;
				for (var nIndex = 0; nIndex < nSpaces; nIndex++)
					sResult += " ";
			}
		}
		else
		{
			sResult = sString.substr(0, nWidth);
		}
	}

	return sResult;
}

/** GetDocumentServer -- Get the server that the current document is on in the form http://www.myserver.com. */
function GetDocumentServer()
{
	var sUrl = document.URL;
	
	if ((sUrl != null) && (sUrl != undefined))
	{
		var nLastPathDelimiter = sUrl.lastIndexOf("/");
		
		if (nLastPathDelimiter >= 0)
			return sUrl.substr(0, nLastPathDelimiter); 
	}
	return "";
}

function EmailValidate(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at) < 0)
		return false
	
	if ((str.indexOf(at) < 0) || (str.indexOf(at) == 0) || (str.indexOf(at) == lstr))
		return false
	
	if ((str.indexOf(dot) < 0) || (str.indexOf(dot) == 0) || (str.indexOf(dot) == lstr))
		return false
	
	if (str.indexOf(at,(lat+1)) != -1)
		return false
	
	if ((str.substring(lat-1,lat) == dot) || (str.substring(lat+1,lat+2) == dot))
		return false
	
	if (str.indexOf(dot,(lat+2)) == -1)
		return false
	
	if (str.indexOf(" ") !=- 1)
		return false
	
	return true					
}

function TrimLeadingZeros(sValue)
{
	var sResult = "";
	var bSearchingForLeadingZeros = true;
	for (var index = 0; index < sValue.length; index++)
	{
		if (bSearchingForLeadingZeros)
		{
			switch (sValue.charAt(index))
			{
				case '1': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '2': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '3': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '4': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '5': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '6': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '7': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '8': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
				case '9': sResult += sValue.charAt(index); bSearchingForLeadingZeros = false; break;
			}
		}
		else
		{
			sResult += sValue.charAt(index);
		}
	}
	
	return sResult;
}

function DateFromString(sText)
{
	var oItems = sText.split("/");
	
	if ((oItems == undefined) || (oItems == null) || (oItems.length != 3))
		return null;
	
	var nMonth = parseInt(TrimLeadingZeros(oItems[0]));
	var nDay = parseInt(TrimLeadingZeros(oItems[1]));
	var nYear = parseInt(oItems[2]);
	
	if (isNaN(nMonth) || isNaN(nDay) || isNaN(nYear))
		return null;
	
	var oDate = new Date(nYear, nMonth - 1, nDay, 0, 0, 0, 0);
	return oDate;
}

function GetTimeText(nTime)
{
	switch (nTime)
	{
		case 0:		return "12:00 AM";
		case 1:		return "1:00 AM";
		case 2:		return "2:00 AM";
		case 3:		return "3:00 AM";
		case 4:		return "4:00 AM";
		case 5:		return "5:00 AM";
		case 6:		return "6:00 AM";
		case 7:		return "7:00 AM";
		case 8:		return "8:00 AM";
		case 9:		return "9:00 AM";
		case 10:	return "10:00 AM";
		case 11:	return "11:00 AM";
		case 12:	return "12:00 PM";
		case 13:	return "1:00 PM";
		case 14:	return "2:00 PM";
		case 15:	return "3:00 PM";
		case 16:	return "4:00 PM";
		case 17:	return "5:00 PM";
		case 18:	return "6:00 PM";
		case 19:	return "7:00 PM";
		case 20:	return "8:00 PM";
		case 21:	return "9:00 PM";
		case 22:	return "10:00 PM";
		case 23:	return "11:00 PM";
	}
	return "Error: " + nTime.toString() + " is not a valid time.";
}

function GetPosition(element) {
	var x = element.offsetLeft;
	var y = element.offsetTop;
	var parent = element.offsetParent;
	while ((parent != undefined) && (parent != null)) {
		if (parent.tagName.toLowerCase() == "body") {
			break;
		}
		x += parent.offsetLeft;
		y += parent.offsetTop;
		parent = parent.offsetParent;
	}
	var position = new Object();
	position.x = x;
	position.y = y;
	return position;
}
