// Web.js
// Copyright (c) 2005-2007 PeteSoft, LLC. All Rights Reserved

var fIE;
var fDOM;

var dBugWin = null;

//-------------------------------------------------------------------------
function dBug(Str)
{
	if (0 == 1)
	{
		if (dBugWin == null)
		{
			dBugWin = window.open("blank");
		}
		
		if (dBugWin != null)
		{
			dBugWin.document.writeln(Str + "<br>");
		}
	}	
}

//-------------------------------------------------------------------------
// initialize values
SetupBrowser();

//-------------------------------------------------------------------------
function iGetNum(FldName)
{
	var Value = "";
	var e = GetDocObj(FldName);
	if (e)
	{
		Value = e.value;
	}

	return Num(Value);
}

//-------------------------------------------------------------------------
function oGetNum(FldName)
{
	return Num(oGetStr(FldName));
}

//-------------------------------------------------------------------------
function iGetStr(FldName)
{
	var Value = "";
	var e = GetDocObj(FldName);
	if (e)
	{
		Value = e.value;
	}

	return Value;
}

//-------------------------------------------------------------------------
function iSetStr(FldName, Value)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.value = Value;
	}
}

//-------------------------------------------------------------------------
function oGetStr(FldName)
{
	var Value = "";
	var e = GetDocObj(FldName);
	if (e)
	{
		Value = e.innerHTML;
	}

	return Value;
}

//-------------------------------------------------------------------------
function oSetStr(FldName, Value)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.innerHTML = Value;
	}
}

//-------------------------------------------------------------------------
function iGetDt(FldName)
{
	var Value = iGetStr(FldName);
	return Date.parse(Value);
}

//-------------------------------------------------------------------------
function iGetDtMthYr(FldName)
{
	var dt = new Date();

	var Value = iGetStr(FldName);
	var aParts = Value.split("/");
	if (aParts.length == 1)
	{
		aParts = Value.split(" ");
	}

	if (aParts.length == 2)
	{
		var Month = Num(aParts[0]);
		var Year  = Num(aParts[1]);
		if (Month >= 1 && Month <= 12)
		{
			dt.setFullYear(Year, Month - 1, 1);
			dt.setHours(0, 0, 0, 0);
		}
	}

	return dt;
}

//-------------------------------------------------------------------------
function iGetChk(FldName)
{
	var Checked = false;
	var e = GetDocObj(FldName);
	if (e)
	{
		Checked = e.checked;
	}

	return Checked;
}

//-------------------------------------------------------------------------
function iSetChk(FldName, fChecked)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.checked = fChecked;
	}
}

//-------------------------------------------------------------------------
function iGetChecked(FldName)
{
	var Checked = "";
	var e = GetDocObj(FldName);
	if (e)
	{
		if (e.checked)
		{
			Checked = "checked";
		}
	}
	
	return Checked;
}

//-------------------------------------------------------------------------
function iGetSel(FldName)
{
	var Value = "";
	var e = GetDocObj(FldName);
	if (e && e.selectedIndex >= 0)
	{
		Value = e.options[e.selectedIndex].text;
	}

	return Value;
}

//-------------------------------------------------------------------------
function iGetSelVal(FldName)
{
	var Value = "";
	var e = GetDocObj(FldName);
/*
	if (e && e.selectedIndex >= 0)
	{
		Value = e.options[e.selectedIndex].value;
	}
*/	
	if (e)
	{
		Value = e.value;
	}

	return Value;
}

//-------------------------------------------------------------------------
function iGetSelVal1(FldName)
{
	var Value = "";
	var e = GetDocObj(FldName);
	if (e && e.selectedIndex >= 0)
	{
		Value = e.options[e.selectedIndex].value;
	}

	return Value;
}

//-------------------------------------------------------------------------
function iGetSelIdx(FldName)
{
	var Value = -1;
	var e = GetDocObj(FldName);
	if (e)
	{
		Value = e.selectedIndex;
	}

	return Value;
}

//-------------------------------------------------------------------------
function iGetSelStr(FldName)
{
	return iGetSel(FldName);
}

//-------------------------------------------------------------------------
function iSetSel(FldName, Value)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.value = Value;
	}
}

//-------------------------------------------------------------------------
function iSetSelVal(FldName, Value)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.value = Value;
	}
}

//-------------------------------------------------------------------------
function iSetSelTxt(FldName, Text)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		var iOption;
		for (iOption = 0; iOption < e.options.length; iOption++)
		{
			if (e.options[iOption].text == Text)
			{
				e.options[iOption].selected = true;
				break;
			}
		}
	}
}

//-------------------------------------------------------------------------
function iSetSelIdx(FldName, Value)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.selectedIndex = Value;
	}
}

//-------------------------------------------------------------------------
function ClearOptions(Select)
{
	while (Select.options.length > 0)
	{
		Select.remove(0);
	}
}

//-------------------------------------------------------------------------
function AddOption(Select, Text, Value, CurrText)
{
	var Option = document.createElement("OPTION");
	Option.text = Text;
	Option.value = Value;
	Option.selected = (Text == CurrText);

	//Select.options.add(Option);
	if (fIE)
	{
		Select.add(Option);
	}
	else
	{
		Select.add(Option, null);
	}
}

//-------------------------------------------------------------------------
function iGetDis(FldName)
{
	var fDisabled = false;
	var e = GetDocObj(FldName);
	if (e)
	{
		fDisabled = e.disabled;
	}

	return fDisabled;
}

//-------------------------------------------------------------------------
function iSetDis(FldName, fDisabled)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.disabled = fDisabled;
	}
}

//-------------------------------------------------------------------------
function iGetVis(FldName)
{
	var fVisibile = false;
	var Style = GetStyle(FldName);
	if (Style)
	{
		fVisibile = (Style.visibility == 'visible' || Style.visibility == '');
	}

	return fVisibile;
}

//-------------------------------------------------------------------------
function iSetVis(FldName, fVisibile)
{
	var Style = GetStyle(FldName);
	if (Style)
	{
		Style.visibility = (fVisibile ? 'visible' : 'hidden');
	}
}

//-------------------------------------------------------------------------
function SetFocus(FldName)
{
	var e = GetDocObj(FldName);
	if (e && iGetVis(FldName))
	{
		try
		{
			e.focus();
		}
		catch (e)
		{
		}
	}
}

//-------------------------------------------------------------------------
function SetTarget(FldName, Target)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.target = Target;
	}
}

//-------------------------------------------------------------------------
function SelectText(FldName)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.select();
	}
}

//-------------------------------------------------------------------------
function SetImgSrc(FldName, Src)
{
	var e = GetDocObj(FldName);
	if (e)
	{
		e.src = Src;
	}
}

//-------------------------------------------------------------------------
function GetDocObj(FldName)
{
	var e = null;

	if (FldName != "")
	{
		if (document.getElementById)
		{
			e = document.getElementById(FldName);
		}
		else
		if (document.all)
		{
			e = document.all[FldName];
		}
		else
		if (document.layers)
		{
			e = document.layers[FldName];
		}

		if (e == null && document.forms.length > 0)
		{
			var i;
			for (i = 0; i < document.forms.length && e == null; i++)
			{
				e = eval("document.forms[" + i + "]." + FldName);
			}
		}
	}
	
	return e;
}

//-------------------------------------------------------------------------
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtCur(Value, ZeroMode)
{
	return (arguments.length == 2 ? FmtDollar(Value, ZeroMode) : FmtDollar(Value));
}

//-------------------------------------------------------------------------
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtDollar(Value, ZeroMode)
{
	var Str = "";

	if (arguments.length == 1)
	{
		Str = "$" + FmtDec(Value);
	}
	else
	{
		Str = FmtDec(Value, ZeroMode);
		if (Str != ZeroMode)
		{
			Str = "$" + Str;
		}
	}

	return Str;
}

//-------------------------------------------------------------------------
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtDec(Value, ZeroMode)
{
	var Val = NumRnd(Value, 2);
	var Str = "";

	if (isNaN(Val))
	{
		Val = 0;
	}

	Str = FmtNum(Val, 2);

	if (Str == "0.00" && arguments.length >= 2)
	{
		Str = ZeroMode;
	}

	return Str;
}

//-------------------------------------------------------------------------
// formats the number by adding commas and the parm MaxDecDigits limits
// the number of digits after the decimal, trailing zeros are removed
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtNumVar(Value, MaxDecDigits, ZeroMode)
{
	var Num = (arguments.length == 2 ? FmtNum(Value, MaxDecDigits) : 
									  FmtNum(Value, MaxDecDigits, ZeroMode));
	var Len = Num.length;
	
	while (Num.substr(Num.length - 1) == "0")
	{
		Num = Num.substr(0, Num.length - 1);
	}

	if (Num.substr(Num.length - 1) == ".")
	{
		Num = Num.substr(0, Num.length - 1);
	}
	
	return Num;
}

//-------------------------------------------------------------------------
// formats the number by adding commas, the optional parm DecDigits limits
// the number of digits after the decimal
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtNum(Value, DecDigits, ZeroMode)
{
	if (isNaN(Value))
	{
		Value = 0;
	}

	var Num = Value;
	var fNeg = false;
	var i;

	// keep track of the numbers sign
	if (Num < 0)
	{
		fNeg = true;
		Num = -Num;
	}

	// check for the optional DecDigits parm
	if (arguments.length < 2) // if DecDigits not a parm
	{
		// look for a decimal point to default DecDigits
		var str = "" + Num;
		i = str.indexOf(".");
		if (i >= 0)
		{
			DecDigits = str.length - i - 1;
		}
		else
		{
			// not decimal point and digits
			DecDigits = -1;
		}
	}

	if (DecDigits > 0)
	{
		// round the value
		Num = NumRnd(Num, DecDigits);

		// convert to a string
		Num = "" + Num;
		// if not decimal point yet, add it
		if (Num.indexOf(".") < 0)
		{
			Num += ".";
		}

		// add some extra zeros to fill in the decimal digits
		for (i = 0; i < DecDigits; i++)
		{
			Num += "0";
		}
	}
	else
	{
		// convert to a string
		Num = "" + Num;
	}

	var Str = "";

	// format the decimal point and the digits
	i = Num.indexOf(".");
	if (i >= 0)
	{
		Str = (DecDigits > 0 ? Num.substr(i, DecDigits + 1) : "");
	}
	else
	{
		i = Num.length;
	}

	// format digits left of the decimal
	var Digits = 0;
	for (i = i - 1; i >= 0; i--)
	{
		Str = Num.charAt(i) + Str;

		if (++Digits % 3 == 0 && i > 0)
		{
			Str = "," + Str;
		}
	}

	// remember the the sign of the number
	if (fNeg)
	{
		Str = "-" + Str;
	}

	// check for zero value and Mode
	if (parseFloat(Str) == 0 && arguments.length >= 3)
	{
		Str = ZeroMode;
	}

	return Str;
}

//-------------------------------------------------------------------------
// formats the number by adding commas. the optional parm DecDigits limits
// the number of digits after the decimal
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtPct(Value, DecDigits, ZeroMode)
{
	var Pct = FmtNum(Value, DecDigits, ZeroMode);
	Pct = (Pct == "" ? "" : Pct + "%");
	return Pct;
}

//-------------------------------------------------------------------------
// formats the number trying to use whole fractions (1/8 units or better)
// by adding commas and the parm MaxDecDigits limits
// the number of digits after the decimal, trailing zeros are removed
// ZeroMode (optional), uses value of ZeroMode when Value is zero
//  "-" use '-' when zero
//  ""  use blank when zero
function FmtFract(Value, MaxDecDigits, ZeroMode)
{
	var Sign = (Value > 0 ? 1 : -1);
	var Fract = Sign * Value;
	var Whole = (Fract > 0 ? Math.floor(Fract) : Math.ceil(Fract));
	var Decimal = Fract - Whole;
	var Denominator;
	var Numerator;	
	var fFound = false;
	
	if (Decimal > 0)
	{
		for (Denominator = 2; Denominator <= 8; Denominator++)
		{
			Numerator = NumRnd(Decimal * Denominator, 1);
			if (Numerator == NumRnd(Numerator, 0))
			{
				fFound = true;
				break;
			}
		}
	}
		
	return (fFound ? (Sign < 0 ? "-" : "") + Trim(FmtNum(Whole, 0, "") + " " + Numerator + "/" + Denominator) : FmtNumVar(Value, MaxDecDigits, ZeroMode));
}

//-------------------------------------------------------------------------
// ZeroMode (optional), determines if leading zeros are used on months and days
//  ""  use blank (default)
//  "0" show leading zero 
function FmtDt(dt, ZeroMode)
{
	var Str = "";
	
	if (!isNaN(dt))
	{
		if (arguments.length > 1 && ZeroMode == "0")
		{
			Str = Right("0" + (dt.getMonth() + 1), 2) + "/" + Right("0" + dt.getDate(), 2) + "/" + dt.getFullYear(); 
		}
		else
		{
			Str = (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear(); 
		}
	}
	else
	{
		Str = "N/A";
	}
	
	return Str;
}

//-------------------------------------------------------------------------
function Right(Str, Len)
{
	var s = Str.substr(-1, Len);
	return s;
}

//-------------------------------------------------------------------------
// format the field flushed to the right with spaces
function Fmt(Value, Size)
{
	var Str = Repeat(" ", Size) + Value;
	Str = Str.substr(Str.length - Size);

	return Str;
}


//-------------------------------------------------------------------------
// repeat the Chr Cnt times
function Repeat(Chr, Cnt)
{
	var Str = "";
	while (--Cnt > 0)
	{
		Str += Chr;
	}

	return Str;
}

//-------------------------------------------------------------------------
function NumRnd(Value, DecDigits)
{
	var Power = Math.pow(10, DecDigits);
	var Num = Math.round(Value * Power) / Power;

	return Num;
}

//-------------------------------------------------------------------------
function NumCeil(Value)
{
	return Math.ceil(Value);
}

//-------------------------------------------------------------------------
function Num(Value)
{
  var Num = 0;
	var sNum = "0";
	var fDec = false;
	var Dec = 1.0;
	var fNeg = false;
	var i;
	var Chr;


	if (Value)
	{
		for (i = 0; i < Value.length; i++)
		{
			Chr = Value.charAt(i);
			switch (Chr)
			{
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
					sNum += Chr;
					break;

				case '.':
					if (!fDec)
					{
						sNum += ".";
					}
					fDec = true;
					break;

				case '-':
					fNeg = true;
					break;
			}
		}
	}

	Num = parseFloat(sNum);
	if (fNeg)
	{
		Num = -Num;
	}
	return Num;
}

//-------------------------------------------------------------------------
function FractNum(Value)
{
	var Num = 0;
	if (Value != "")
	{
		try
		{
			var Sign = (Value.indexOf("-") < 0 ? 1 : -1);
			var NewValue = Value.replace("-", "").replace(" ", "+");
			Num = eval(NewValue);
			if (Num == undefined)
			{
				Num = 0;
			}
			else
			{
				Num = Sign * NumRnd(Num, 4);
			}
		}
		catch (Err)
		{
		}
	}

	return Num;
}

//-------------------------------------------------------------------------
function Trim(Str)
{
	var s = Str;

	while (s.substr(0, 1) == " ")
	{
		s = s.substr(1);
	}

	while (s.substr(s.length - 1) == " ")
	{
		s = s.substr(0, s.length - 1);
	}

	return s;
}

//-------------------------------------------------------------------------
function SetupBrowser()
{
	fIE  = (document.all ? true : false);
	fDOM = (document.getElementById ? true : false);
}


var CurrLayer = "";

//-------------------------------------------------------------------------
function ShowLayer(Layr, BesideElement, LeftMargin, fAbove, HideElements)
{
	dBug("ShowLayer " + Layr + " " + CurrLayer);
	if (!IsLockedLayer())
	{
		// there is a bug in Internet Explorer that sends a onMouseOver instead of a onMouseOut when
		// highlighting the text in a textbox with the mouse button down and dragging out of the box
		// this fixes that bug
		
		if (CurrLayer != "")
		{
			HideLayer(CurrLayer);
			CurrLayer = "";
		}
		
		var LayerStyle = GetStyle(Layr);
		if (LayerStyle)
		{
			LayerStyle.visibility = 'visible';
			
			if (Layr != "RelatedCalcs")
			{
				CurrLayer = Layr;
			}

			if (BesideElement)
			{
				LayerStyle.top = GetY(BesideElement) + BesideElement.offsetHeight * (fAbove ? -1 : 1) + "px";
				if (LeftMargin)
				{
					var Sign = String(LeftMargin).substr(0, 1);
					var Margin = Number(LeftMargin);
					if (isNaN(Margin))
					{
						Margin = 0;
					}

					if (Sign == '+' ||
							Sign == '-')
					{
						LayerStyle.left = GetX(BesideElement) + Margin + "px";
					}
					else
					{
						LayerStyle.left = Margin + "px";
					}
				}
				else
				{
					LayerStyle.left = GetX(BesideElement) + "px";
				}
			}

			if (fIE && HideElements)
			{
				for (var i = 0; i < HideElements.length; i++)
				{
					//HideElement(HideElements[i]);
					iSetVis(HideElements[i], false);

				}
			}
		}
	}
}

//-------------------------------------------------------------------------
function HideLayer(Layr, ShowElements)
{
	dBug("HideLayer " + Layr + " " + CurrLayer);
	if (!IsLockedLayer())
	{
		if (Layr != CurrLayer)
		{
			var CurrLayerStyle = GetStyle(CurrLayer);
			if (CurrLayerStyle)
			{
				CurrLayerStyle.visibility = 'hidden';
			}
		}

		var LayerStyle = GetStyle(Layr);
		if (LayerStyle)
		{
			LayerStyle.visibility = 'hidden';
			CurrLayer = "";

			if (fIE && ShowElements)
			{
				for (var i = 0; i < ShowElements.length; i++)
				{
					//ShowElement(ShowElements[i]);
					iSetVis(ShowElements[i], true);
				}
			}
		}
	}
}

var LockedLayer = null;
//-------------------------------------------------------------------------
function LockLayer(Layr)
{
	dBug("LockLayer " + Layr + " " + LockedLayer);
	if (LockedLayer == null)
	{
		LockedLayer = Layr;
	}
}

//-------------------------------------------------------------------------
function UnlockLayer(Layr)
{
	dBug("UnlockLayer " + Layr + " " + LockedLayer);
	if (Layr == LockedLayer)
	{
		LockedLayer = null;
	}
}

//-------------------------------------------------------------------------
function IsLockedLayer()
{
	return (LockedLayer != null);
}

/*
//-------------------------------------------------------------------------
function ShowElement(ElementName)
{
	var Style = GetStyle(ElementName);
	if (Style)
	{
		Style.visibility = 'visible';
	}
}

//-------------------------------------------------------------------------
function HideElement(ElementName)
{
	var Style = GetStyle(ElementName);
	if (Style)
	{
		Style.visibility = 'hidden';
	}
}
*/

//-------------------------------------------------------------------------
function GetStyle(StyleName)
{
	var Style;

	if (fDOM)
	{
		//var e = document.getElementById(StyleName);
		var e = GetDocObj(StyleName);
		if (e)
		{
			Style = e.style;
		}
	}
	else
	{
		Style = (fIE ? document.all[StyleName].style : document.layers[StyleName]);
	}

	return Style;
}

//-------------------------------------------------------------------------
function GetX(Element)
{
	return (Element.x ? Element.x : GetPos(Element, "Left"));
}

//-------------------------------------------------------------------------
function GetY(Element)
{
	return (Element.y ? Element.y : GetPos(Element, "Top"));
}

//-------------------------------------------------------------------------
function GetPos(Element, Which)
{
	var iPos = 0;
	while (Element != null)
	{
		iPos += Element["offset" + Which];
		Element = Element.offsetParent;
	}

	return iPos;
}

//-------------------------------------------------------------------------
function HostCRC(x)
{
	var c = 157;
	var i;
	for (i = 0; i < x.length; i++)
	{
		c ^= x.charCodeAt(i);
	}
	
	return c;
}

//-------------------------------------------------------------------------
function GetDisplay(ElementName)
{
	var fDisplay = false;
	var Style = GetStyle(ElementName);
	if (Style)
	{
		fDisplay = (Style.display != "none");
	}
	
	return fDisplay;
}

//-------------------------------------------------------------------------
function SetDisplay(ElementName, fDisplay)
{
	var Style = GetStyle(ElementName);
	if (Style)
	{
		Style.display = (fDisplay ? "" : "none");
	}
}

//-------------------------------------------------------------------------
function SetDisplayList(ElementNameList, fDisplay)
{
	if (ElementNameList)
	{
		var aElementName = ElementNameList.split(",");
		for (var i = 0; i < aElementName.length; i++)
		{
			SetDisplay(Trim(aElementName[i]), fDisplay);
		}
	}
}

//-------------------------------------------------------------------------
function ShowAndHide(ShowElement, HideElement)
{
	SetDisplay(ShowElement, true);
	SetDisplay(HideElement, false);
}

//-------------------------------------------------------------------------
function DisplayOn(ElementName, fIcon)
{
	var e = document.getElementById(ElementName);
	if (e)
	{
		e.className = "DisplayOn";

		if (arguments.length > 1 && fIcon)
		{
			e.className += "Icon";
		}
	}
}

//-------------------------------------------------------------------------
function DisplayOff(ElementName, fIcon)
{
	var e = document.getElementById(ElementName);
	if (e)
	{
		e.className = "DisplayOff";

		if (arguments.length > 1 && fIcon)
		{
			e.className += "Icon";
		}
	}
}

//-------------------------------------------------------------------------
function IsDisplayOn(ElementName)
{
	var Element = document.getElementById(ElementName);
	return (Element.className == "DisplayOn");
}

//-------------------------------------------------------------------------
function SetClass(ElementName, ClassName)
{
	var Element = document.getElementById(ElementName);
	if (Element)
	{
		Element.className = ClassName;
	}
}

var PrevCursor = "default";
//-------------------------------------------------------------------------
function CursorWait()
{
	PrevCursor = document.body.style.cursor;
	document.body.style.cursor = "wait";
}

//-------------------------------------------------------------------------
function CursorReset()
{
//	document.body.style.cursor = PrevCursor;
	document.body.style.cursor = "default";
}

//-------------------------------------------------------------------------
function ClassOver(Element)
{
	Element.className += "Over";
}

//-------------------------------------------------------------------------
function ClassOut(Element)
{
	var i = Element.className.indexOf("Over");
	Element.className = Element.className.substring(0, i);
}

//-------------------------------------------------------------------------
function OpenPage(Page)
{
	window.location.href = Page;
}

var HighLightID;
var HighLightPrevID = "";

//-------------------------------------------------------------------------
function CheckHighLight()
{
	if (HighLightID != "")
	{
		HighLight(HighLightID);
	}
}

//-------------------------------------------------------------------------
function HighLight(ID)
{
	if (HighLightPrevID != "")
	{
		SetClass(HighLightPrevID, "HighLight");
	}

	SetClass(ID, "HighLightOn");

	HighLightPrevID = ID;
}

//---------------------------------------------------------------------------
function ValidateCurrency(Element)
{
	Element.value = FmtCur(Num(Element.value));
}

//---------------------------------------------------------------------------
function ValidateNum(Element)
{
	Element.value = FmtNum(Num(Element.value), 0);
}

//---------------------------------------------------------------------------
function ValidateRno(Element)
{
	Element.value = Num(Element.value);
}

//---------------------------------------------------------------------------
function ValidateDate(Element)
{
	Element.value = Trim(Element.value);
	
	if (Element.value != "")
	{
		var Dt = new Date(Element.value);
		if (isNaN(Dt))
		{
			alert("Invalid date.");
		}
		else
		{
			//split into date elements, looking for the year
			var a = Element.value.split("/");
			if (a.length >= 3 && a[2].length <= 2)
			{
				Dt.setFullYear(Dt.getFullYear() + 100);
			}

			Element.value = Dt.getMonth() + 1 + "/" + Dt.getDate() + "/" + Dt.getFullYear();
		}
	}
}

//---------------------------------------------------------------------------
function ValidateDateTime(Element)
{
	if (Element.value != "")
	{
		var Dt = new Date(Element.value);
		if (isNaN(Dt))
		{
			alert("Invalid date & time.");
		}
		else
		{
			var b = Element.value.split(" ");
			var a = b[0].split("/");
			if (a.length >= 3 && a[2].length <= 2)
			{
				Dt.setFullYear(Dt.getFullYear() + 100);
			}

			var AmPm = "AM";
			var Hour = Dt.getHours();
			var Min  = Dt.getMinutes();
			var Sec  = Dt.getSeconds();

			var Value = Dt.getMonth() + 1 + "/" + Dt.getDate() + "/" + Dt.getFullYear();

			if (Hour > 0 || Min > 0 || Sec > 0)
			{
				if (Hour >= 12)
				{
					if (Hour > 12 || Min > 0 || Sec > 0)
					{
						AmPm = "PM";
					}
					if (Hour > 12)
					{
						Hour -= 12;
					}
				}
				else
				if (Hour == 0)
				{
					Hour = 12;
				}

				if (Min < 10)
				{
					Min = "0" + Min;
				}

				if (Sec < 10)
				{
					Sec = "0" + Sec;
				}

				Value += " " + Hour + ":" + Min + ":" + Sec + " " + AmPm;
			}

			Element.value = Value;
		}
	}
}

//---------------------------------------------------------------------------
function ValidateMthYr(Element)
{
	var fProblem = false;

	var aParts = Element.value.split("/");
	if (aParts.length == 1)
	{
		aParts = Element.value.split(" ");
	}

	if (aParts.length == 2)
	{
		var Month = Num(aParts[0]);
		var Year  = Num(aParts[1]);
		if (Month >= 1 && Month <= 12)
		{
			Element.value = Month + "/" + Year;
		}
		else
		{
			fProblem = true;
		}
	}
	else
	{
		fProblem = true;
	}

	if (fProblem)
	{
		alert("Invalid Month / Year.");
	}
}

//---------------------------------------------------------------------------
function ValidMthYr(Value)
{
	var fValid = true;

	var aParts = Value.split("/");
	if (aParts.length == 1)
	{
		aParts = Value.split(" ");
	}

	if (aParts.length == 2)
	{
		var Month = Num(aParts[0]);
		var Year  = Num(aParts[1]);
		if (Month >= 1 && Month <= 12)
		{
			Value = Month + "/" + Year;
		}
		else
		{
			fValid = false;
		}
	}
	else
	{
		fValid = false;
	}

	return fValid;
}

//---------------------------------------------------------------------------
function Log10(x)
{
	return Math.log(x) / Math.LN10;
}

//-------------------------------------------------------------------------
function NaN(Num)
{
	return (isNaN(Num) || Num == Infinity || Num == -Infinity ? "n/a" : Num);
}

var cnMilliSecsPerHour = 1000 * 60 * 60;
var cnMilliSecsPerDay  = cnMilliSecsPerHour * 24;

//-------------------------------------------------------------------------
function AddDays(dt, NumDays)
{
	var dtNew = new Date(dt.valueOf());
	var mDate = dtNew.getTime();
		
	mDate += NumDays * cnMilliSecsPerDay;
	dtNew.setTime(mDate);

	var Hours = dtNew.getHours();
	if (Hours != 0)
	{
		var mDate = dtNew.getTime();
		Hours = (Hours < 12 ? -Hours : 24 - Hours);
		mDate += (Hours * cnMilliSecsPerHour);
		dtNew.setTime(mDate);
	}

	return dtNew;
}

//-------------------------------------------------------------------------
function AddMonths(dt, NumMonths)
{
	var dtNew = new Date(dt.valueOf());
	var i;
	for (i = 0; i < NumMonths; i++)
	{
		if (dtNew.getMonth() < 11)
		{
			dtNew.setMonth(dtNew.getMonth() + 1);
		}
		else
		{
			dtNew.setMonth(0);
			dtNew.setFullYear(dtNew.getFullYear() + 1);
		}
	}

	return dtNew;
}

//-------------------------------------------------------------------------
function FmtMthYr(dt)
{
	return (dt.getMonth() + 1) + "/" + dt.getFullYear();
}

//-----------------------------------------------------------------------------
function FocusOnNextField(FieldName)
{
	// find the next field in the form
	var f = GetDocObj(FieldName);
	
	if (f)
	{
		for (var i = 0; i < f.form.elements.length; i++)
		{
			var e = f.form.elements[i];
			if (e == f)
			{
				var fFound = false;
				
				// find the next input element and set the focus
				for (i = i + 1; i < f.form.elements.length; i++)
				{
					var e = f.form.elements[i];
					if (e.type)
					{
						switch (e.type)
						{
							case "button":
							case "checkbox":
							case "file":
							case "password":
							case "radio":
							case "reset":
							case "submit":
							case "text":
							case "textarea":
								if (!e.disabled)
								{
									e.focus();
									fFound = true;
								}
								break;
						}
					}
					
					if (fFound)
					{
						break;
					}
				}
				break;
			}
		}
	}
}

//-----------------------------------------------------------------------------
/*
<script language="javascript" type="text/javascript">Contact("Info", "Petesoft.com", "Contact Us");</script>

<script language="javascript" type="text/javascript">Contact("Info", "Petesoft.com", "Contact Us", "Send us an email");</script>
*/

function Contact(User, Domain, Label, Title)
{
	var Help = "";
	if (Title)
	{
		Help = " title=\"" + Title + "\"";
	}
	
	document.write("<a href=\"" + "mail" + "to:" + User + "@" + Domain + "\"" + Help + ">" + Label + "</a>");
}

//-----------------------------------------------------------------------------
function ContactClass(User, Domain, Label, Class, Title)
{
	var Help = "";
	if (Title)
	{
		Help = " title=\"" + Title + "\"";
	}
	
	document.write("<a href=\"" + "mail" + "to:" + User + "@" + Domain + "\" class=\"" + Class + "\"" + Help + ">" + Label + "</a>");
}

//-----------------------------------------------------------------------------
function BookMarkThisPage()
{
	if (window.external)
	{
		window.external.AddFavorite(location.href, document.title);
	}
	else 
	{
		switch (navigator.appName)
		{
			case "Netscape":
			case "Opera":
				alert("Press Ctrl-D or \nselect Bookmarks | Bookmark This Page");
				break;
			
			default:
				alert("Press Ctrl-D or \nselect Bookmarks | Bookmark This Page\n\nUndefind browser client: " + navigator.appName);
				break;
		}
	}
}

var xmlDoc = null;
//-----------------------------------------------------------------------------
function xmlLoad(Url, LoadFunction)
{
	//load xml file
	
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = false;
		xmlDoc.load(Url);
		LoadFunction();
	}
	else
	// code for Mozilla, Firefox, Opera, etc.
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("","",null);
		if (xmlDoc.load)
		{
			xmlDoc.async = false;
			xmlDoc.load(Url);
			xmlDoc.onload = LoadFunction;
		}
		else
		// code for Safari
		if (window.XMLHttpRequest)
		{
			var Req = new XMLHttpRequest();
			Req.open("GET", Url, false);
			Req.send(null);
			xmlDoc = Req.responseXML;
			LoadFunction();
		}	
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
}

//-----------------------------------------------------------------------------
function xmlGetElementsByTagName(Node, TagName)
{
	var aNodes = new Array();
	
	for (var iChild = 0; iChild < Node.childNodes.length; iChild++)
	{
		var Child = Node.childNodes[iChild];
		
		if (xmlLocalName(Child.nodeName) == TagName)
		{
			aNodes.push(Child);
		}
		
		aNodes = aNodes.concat(xmlGetElementsByTagName(Child, TagName));
	}
	
	return aNodes;
}


//-----------------------------------------------------------------------------
function xmlSearchForTagName(Node, TagName, CallFunction)
{
	for (var iChild = 0; iChild < Node.childNodes.length; iChild++)
	{
		var Child = Node.childNodes[iChild];
		
		var ChildName = xmlLocalName(Child.nodeName);
		if (ChildName == TagName)
		{
			CallFunction(Child);
		}
		else
		{
			xmlSearchForTagName(Child, TagName, CallFunction);
		}
	}
}

//-----------------------------------------------------------------------------
function xmlChildValue(Node, ChildName)
{
	var Value = "";
	var iChild;
	
	for (iChild = 0; iChild < Node.childNodes.length; iChild++)
	{
		var Child = Node.childNodes[iChild];
		
		if (xmlLocalName(Child.nodeName) == ChildName)
		{
			if (Child.firstChild && Child.firstChild.nodeValue)
			{
				Value = Trim(Child.firstChild.nodeValue);
			}
			break;
		}
	}
	
	return Value;
}

//-----------------------------------------------------------------------------
function xmlChildValues(Node, aChildNames)
{
	var aValues = new Array(aChildNames.length);
	
	var iChild;
	var iName;
	
	for (iName = 0; iName < aChildNames.length; iName++)
	{
		aValues[iName] = "";
	}
	
	for (iChild = 0; iChild < Node.childNodes.length; iChild++)
	{
		var Child = Node.childNodes[iChild];
		
		for (iName = 0; iName < aChildNames.length; iName++)
		{
			if (xmlLocalName(Child.nodeName) == aChildNames[iName])
			{
				if (Child.firstChild && Child.firstChild.nodeValue)
				{
					aValues[iName] = Trim(Child.firstChild.nodeValue);
				}
				break;
			}
		}
	}
	
	return aValues;
}

//-----------------------------------------------------------------------------
function xmlLocalName(Name)
{
	var LocalName = Name;
	var i = LocalName.lastIndexOf(":");
	if (i >= 0)
	{
		LocalName = LocalName.substring(i + 1, LocalName.length);
	}
	
	return LocalName;
}

//-----------------------------------------------------------------------------
function GetKeyPressed(Event)
{
	var KeyNum = 0;
	
	if (window.event) // IE
	{
		KeyNum = event.keyCode
	}
	else 
	if (Event.which) // Netscape/Firefox/Opera
	{
		KeyNum = Event.which
	}
	
	return KeyNum;
}
