/** 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
		PopupWindow(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 "";
}

