//rrk 09-23-2005 put this in an include file

//http://www.curiouscat.com/cookies/example.cfm - rrk 09/15/2005
function GetCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var offset = 0;
	while (offset < clen)
	{
		var vpoint = offset + alen;
		if (document.cookie.substring(offset, vpoint) == arg)
		{
			var endstr = document.cookie.indexOf(";", offset);
			if (endstr == -1)
			{
				endstr = document.cookie.length;
			}
			return unescape(document.cookie.substring(vpoint, endstr));
		}
		offset = document.cookie.indexOf(" ", offset) + 1;
		if (offset == 0)
		{
			break;
		}
	}
	return null;
} 
function SetCookie (name, value)
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}
//http://www.curiouscat.com/cookies/example.cfm - rrk 09/15/2005


//RRK 2005 - commonly used HTML/DOM functions

//This function should help with browser compatibility
function FindElement (ID)
{
	if (document.all)
	{
		//IE specific
		return document.all[ID];
	}
	if (document.layers)
	{
		//NN specific
		return document.layers[ID];
	}
	if (document.getElementById)
	{
		//W3C standard
		if (document.getElementsByName(ID).length == 1)
		{
			return document.getElementById(ID);
		}
		else
		{
			return document.getElementsByName(ID);
		}
	}
	return null;
}

// This function removes all entries except what is specified
function RemoveAllFromDropDownList (Element, Exclude)
{
	/*var ShouldRemove = new Array();
	for (var LoopIterator = 0; LoopIterator < document.getElementById(Element).length; LoopIterator++)
	{
		if (document.getElementById(Element).options[LoopIterator].value != Exclude)
		{
			ShouldRemove.push(LoopIterator);
		}
	}
	while (ShouldRemove.length > 0)
	{
		document.getElementById(Element).options.remove(ShouldRemove.pop());
	}
	ShouldRemove = null;*/

	//while the code above works, this is a little more efficient
	for (var LoopIterator = Element.length - 1; LoopIterator >= 0; LoopIterator--)
	{
		if (Element.options[LoopIterator].value != Exclude)
		{
			Element.remove(LoopIterator);
		}
	}
}

//This function finds the select index of a string
function GetListIndex (Element, Search)
{
	for (var Counter = 0; Counter < Element.length; Counter++)
	{
		if (Element.options[Counter].value == Search)
		{
			return Counter;
		}
	}
	return null;
}

//This function replaces event handlers at run time
function ReplaceOnKeyPressIfNotIE ()
{
	if (GetBrowserID() != "Internet Explorer")
	{
		var InputList = document.getElementsByTagName("input");
		var KeyList2 = new Array();
		for (var Counter = InputList.length - 1; Counter >= 0; Counter--)
		{
			if (InputList[Counter].type == "text")
			{
				KeyList2.push(InputList[Counter].id);
			}
		}
		InputList = document.getElementsByTagName("textarea");
		for (var Counter = InputList.length - 1; Counter >= 0; Counter--)
		{
			KeyList2.push(InputList[Counter].id);
		}
		for (var Counter = 0; Counter < KeyList2.length; Counter++)
		{
			if (FindElement(KeyList2[Counter]).onkeypress != null)
			{
				if (FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("KeepOnly") != -1)
				{
					var Parentheses1 = FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("(", FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("KeepOnly"));
					var Parentheses2 = FindElement(KeyList2[Counter]).onkeypress.toString().indexOf(");", FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("KeepOnly"));
					var CheckedFor = FindElement(KeyList2[Counter]).onkeypress.toString().substring(Parentheses1 + 2, Parentheses2 - 1);
					FindElement(KeyList2[Counter]).onkeypress = new Function("event", "return ((new String(\"" + CheckedFor + "\")).indexOf(String.fromCharCode(event.which)) != -1 || event.which == 8 || event.which == 0 || event.ctrlKey || event.modifiers == 2);");
				}
				if (FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("LoseOnly") != -1)
				{
					var Parentheses1 = FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("(", FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("LoseOnly"));
					var Parentheses2 = FindElement(KeyList2[Counter]).onkeypress.toString().indexOf(");", FindElement(KeyList2[Counter]).onkeypress.toString().indexOf("LoseOnly"));
					var CheckedFor = FindElement(KeyList2[Counter]).onkeypress.toString().substring(Parentheses1 + 2, Parentheses2 - 1);
					FindElement(KeyList2[Counter]).onkeypress = new Function("event", "return ((new String(\"" + CheckedFor + "\")).indexOf(String.fromCharCode(event.which)) == -1 || event.which == 8 || event.which == 0 || event.ctrlKey || event.modifiers == 2);");
				}
			}
		}
	}
}

function AddEvent (Element, Property, FunctionName)
{
	try
	{
		Element.attachEvent("on" + Property, FunctionName);
	}
	catch (Exceptor)
	{
		Element.addEventListener(Property, FunctionName, false);
	}
}

//Clears leading and trailing whitespace from an entire form or page
function FormWidePreprocessor (Container)
{
	for (var Counter = 0; Counter < Container.length; Counter++)
	{
		if (Container[Counter].type.indexOf("text") != -1)
		{
			RemoveEndCharacter(Container[Counter], " ");
			RemoveBadSequences(Container[Counter], ("[]||n/a||N/A").split("||"));
		}
		Container[Counter].blur();
	}
}

//Should go up the heirarchy until it find a negative CSS.display or crashes on the root document object
function IsProbablyHidden (Element)
{
	var Current = Element.length == null ? Element : Element[0];
	if (Current.style.display != "" || (Current.style.visibility != "" && Current.style.visibility != "visible"))
	{
		return true;
	}
	while (Current.parentNode != null)
	{
		try
		{
			Current = Current.parentNode;
			if (Current.style.display != "" || (Current.style.visibility != "" && Current.style.visibility != "visible"))
			{
				return true;
			}
		}
		catch (Exceptor)
		{
			return false;
		}
	}
	return false;
}
