﻿// utility functions
var Page = new Array();
var Global = new Array;

Global.InSubmit = false;
Global.InAjax = false;
Global.CheckIdComplete = false;

function Browser()
{
	var ua, s, i;
	this.isIE	= false;
	this.IsOpera = false;
	this.isNS	= false;
	this.version = null;
	ua = navigator.userAgent;
	
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	s = "Opera";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isOpera = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0)
	{		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}
	// Treat any other "Gecko" browser as NS 6.1.
	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}

Global.Browser = new Browser();
function DocAll(szId, IgnoreNull)
{
	var obj;
	if (Global.Browser.isIE)
	{
		obj = document.all(szId);
	}
	else
	{
		obj = document.getElementById(szId);
	}
	
	if (obj == null && (typeof(IgnoreNull) == "undefined" || !IgnoreNull))
	{	
		dbgMsg(szId + "of type " + typeof(szId) +  "was not found" );
	}
	return obj;
}

Global.ErrMessages = "";
Global.showDebug = false;
Global.debugWait = self.setTimeout("showDbg()",5000);
Global.divDebug = null;

function btnMO(objOrId, bMO)
{
	try
	{
		var objBtn = getObj(objOrId);
		InitBtn(objBtn);
		if (Global.InSubmit || objBtn.btnDisabled) return;
		btnMOExec(objBtn, bMO);
	}
	catch (e)
	{
		alert(e.msg)
		//do nothing;
	}
}

function btnMOExec(objBtn, bMO)
{
	if (IsImgBtn(objBtn))
	{
		objBtn.src = objBtn.stub + (bMO ? "_mo" : "") + ".gif";
		objBtn.style.cursor = (bMO ? (Global.Browser.isIE ? "hand" : "pointer") : "default");
	}
	else
	{
		objBtn.className = objBtn.stub + (bMO ? "_mo" : "");
		//dbg_Msg(objBtn.className);
	}
}

function InitBtn(objBtn)
{
	if (typeof(objBtn.init) != "undefined") return;
	var bIsImgBtn = IsImgBtn(objBtn);
	var Stub;
	if (bIsImgBtn)
	{
		var n = objBtn.src.toLowerCase().indexOf(".gif")
		Stub =  objBtn.src.substr(0,n);
	}
	else
	{
		Stub = objBtn.className;
	}
	var iStubEnd = Stub.toLowerCase().indexOf("_dis");
	objBtn.btnDisabled = (iStubEnd > -1);
	objBtn.stub = (objBtn.btnDisabled ? Stub.substr(0, iStubEnd) : Stub);
	objBtn.init = true;
}

function IsImgBtn(objBtn)
{
	if (typeof(objBtn.IsImg) != "undefined") return objBtn.IsImg;
	var szTagName = objBtn.tagName.toUpperCase();
	if (szTagName == "IMG") 
	{
		objBtn.IsImg = true;
		return true;
	}
	if ( (szTagName == "INPUT") && objBtn.type.toUpperCase() == "IMAGE") 
	{
		objBtn.IsImg = true;
		return true;
	}
	objBtn.IsImg = false;
	return false;
}

function showDbg()
{
	if (Global.divDebug == null) 
	{
		Global.divDebug = document.getElementById("divDebug", true);
	}
	if (Global.divDebug == null) 
	{
		Global.divDebug = addElement("DIV", document.body, "divDebug", "", "PopUp", false);
		Global.divDebug.style.position = "absolute";
		Global.divDebug.style.top = "200px";
		Global.divDebug.style.left = "300px";
		Global.divDebug.style.width = "300px";
		Global.divDebug.style.display = "";
		Global.divDebug.onclick = "Global.divDebug.style.display='none'";
	}
	Global.divDebug.style.display = (Global.ErrMessages != "" ? "" : "none");
	Global.divDebug.innerHTML = Global.ErrMessages;
	closeTimeout(Global.debugWait);
}	

Global.ErrMessages = "";
function dbgMsg(NewMsg)
{
	if (Page.Loaded)
	{
		if (! Global.showDebug) return;
		showDbg();
		var Msg = Global.ErrMessages + Global.divDebug.innerHTML;
		Global.ErrMessages = "";
		if (Msg.length > 2000)
		{
			Msg = Msg.substr(Msg.indexOf("<p>", 400))
		}
		Msg += "<p>" + NewMsg + "</p>";
		Global.divDebug.innerHTML = Msg;
	}
	else
	{
		Global.ErrMessages += "<p>" + NewMsg + "</p>";
	}
}

function Extract(szIn, szStart, szEnd, szNotFound)
{
	//extracts a substring from szIn that was found between szStart, szEnd
	//if szStart = null or szEnd = null, then begining || end of string
	//are used respectively.
	if ((szIn == null) || (szIn.length==0))
	{
		return szNotFound;
	}
	var szX = szIn;
	var n1; 
	if (szStart != null && szStart.length > 0)
	{
		n1 = Instr(null, szX, szStart);
		if (n1 < 1 )
		{
			return szNotFound;
		}
		n1 += szStart.length;
	}
	else
	{
		n1 = 1;
	}
	var n2;
	if (szEnd != null && szEnd.length > 0)
	{
		n2 = Instr(n1, szX, szEnd);
		if (n2 < 1)
		{
			return szNotFound;
		}
	}
	else
	{
		n2 = szIn.length + 1;
	}
	var szOut;
	if ( (n2-n1) == 0)
	{
		szOut = "";
	}
	else
	{
		szOut = Mid(szX, n1, n2-n1);
	}
	return szOut;
}

function getObj(objOrId, IgnoreNull)
{
	var obj = (typeof(objOrId) == "object" ? objOrId : obj = document.getElementById(objOrId));
	if (obj == null && (typeof(IgnoreNull) == "undefined" || !IgnoreNull))
	{	
		dbgMsg(objOrId + " of was not found" );
	}
	return obj;
}

function btnDisable(objOrId, bDisabled)
{
	if (objOrId == null)
	{
		dbgMsg("Passed null button");
		return;
	}

	var objBtn = getObj(objOrId);
	if (objBtn == null) 
	{
		Message("Could not find button: " + objOrId);
		return;
	}
	InitBtn(objBtn)
	if (typeof(objBtn.inMO) == "undefined") objBtn.inMO = false
	
	if (IsImgBtn(objBtn))
	{
		objBtn.src = objBtn.stub + (bDisabled ? "_dis" : (objBtn.inMO ? "_mo" : "")) + ".gif";
	}
	else
	{
		objBtn.className = objBtn.stub + (bDisabled ?  "_dis" : (objBtn.inMO ? "_mo" : ""));
	}
	objBtn.btnDisabled = bDisabled;
	if (bDisabled) objBtn.inMO = false;  // a disabled button can't be inMO.
	objBtn.disabled = (objBtn.tagName.toUpperCase() == "INPUT" ? bDisabled : false);  //disable postback for disabled input buttons.
}

function LTrim( value )
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function Trim( value ) 
{
	return LTrim(RTrim(value));
}

function TrapEnterKey(e, szEvalIfEnter)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (iAsc == 13)
	{
		eval(szEvalIfEnter);				
		return false;	
	}
}

function addElement(szType, Parent, ChildId, szHtml, cssClass, bDisplay) 
{
	var objParent = getObj(Parent);
	var objChild = DocAll(ChildId, true);
	if (typeof(bDisplay) == "undefined") var bDisplay = false;
	if (objChild == null)
	{
		objChild = document.createElement(szType);
		if (ChildId != null)
		{
			objChild.setAttribute('id', ChildId);
		}
	}
	if (! bDisplay)
	{
		objChild.style.display = "none";
	}
	
	if (cssClass != null && cssClass != "")
	{
		objChild.className = cssClass;
	}
	objParent.appendChild(objChild);
	if (szHtml != null && szHtml != "")
	{
		objChild.innerHTML = szHtml;
	}
	return objChild;
}

function closeTimeout(obj)
{
	if (obj == null) return;
	self.clearTimeout(obj);
	obj = null;
}

function removeElement(objOrId)
{
	var obj = getObj(objOrId);
	obj.parentNode.removeChild(obj); 
}

Object.prototype.p	= function()
{
	return this.parentNode;
}

function PageLoaded()
{
	Page.Loaded = true;
	clearTimeout(Page.LoadedTimer);
}

function disableControl(obj, bDisable)
{
	obj.className = (bDisable ? "dis" : "");
	obj.disabled = bDisable;
}

function SetVisible(szId, bState)
{
	var szDisplay = "";
	if (!bState)
	{
		szDisplay = "none";
	}
	DocAll(szId).style.display = szDisplay;
}

function IsNullOrEmpty(x)
{
	return (x == null || x == "" );
}

function Replace(szX, szFind, szReplace)
{
	var n = szX.indexOf(szFind);
	while (n > -1)
	{
		szX = szX.substr(0,n-1) + szReplace + szX.substr(n)
		n = szX.indexOf(szFind, n + szReplace.length);
	}
	return szX;
}

function TrapEnterKey(e, EvalIfEnter)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (iAsc == 13)
	{
		eval(EvalIfEnter);				
		return false;	
	}
}	

function InputNumeric(e)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (TestNavChars(iAsc)) return true;
	if (e.shiftKey) return false;
	switch (iAsc)
	{
		case 190:  //keyboard decimal
		case 110:  //numpad decimal
			if (event.srcElement.value.indexOf(".") < 0)
			{	
				//avoids double periods
				return true;
			}
			return false;
			break;
	}
	//absorb any character that has not yet been trapped
	//and is NOT a number
	return NumberKeysOnly(iAsc);
}

function InputInteger(e)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (TestNavChars(iAsc)) return true;
	if (e.shiftKey) return false;
	//absorb any character that has not yet been trapped
	//and is NOT a number
	return NumberKeysOnly(iAsc);
}

function TestNavChars(iAsc)
{
	switch (iAsc)
	{
		case  8: return true; break; //backspace
		case  9: return true; break; //tab
		case 35: return true; break; //end
		case 36: return true; break; //home
		case 37: return true; break; //left cursor
		case 38: return true; break; //up cursor
		case 39: return true; break; //right cursor
		case 40: return true; break; //down cursor
		case 46: return true; break; //delete
		case 144: return true; break; //numlock
	}
	return false;
}

function PhoneKey(e, szExtId)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	var bIsPhone = (szExtId != null);
	if (TestNavChars(iAsc)) return true;
	
	switch (iAsc)
	{
		case 190: return true; break; //decimal
		case 32:
			return KeySubstitute(bIsPhone ? "." : ""); break; //space
		case 189:
			return KeySubstitute(bIsPhone ? "." : ""); break;  //dash
		case 191:
			return KeySubstitute(bIsPhone ? "." : ""); break; //slash
		case 88:
			if (bIsPhone)
			{
				DocAll(szExtId).focus();
				return false;
			} break;
	}
	return !e.shiftKey && NumberKeysOnly(iAsc);
}

function NumberKeysOnly(iAsc)
{
	if (iAsc < 48 || ((iAsc > 57) && (iAsc < 96)) || (iAsc >105))
	{
		return false;
	}
	return true;	
}

function KeySubstitute(NewKey)
{
	if (NewKey == "") return false;
	var range=document.selection.createRange();
	range.length = 0;
	range.text = NewKey;
	return false;
}


function InputText(e)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (TestNavChars(iAsc)) return true;
	return TestTextChar(iAsc);
}

function TestTextChar(iAsc)
{
	if (iAsc == 13 ) return true;
	switch (iAsc)
	{
		case 34: 
		case 37: 
		case 39: case 222:
			return false;
			break;
		case 16: case 186: case 219: case 220: case 221: 
			return true;
			break;
	}	
	if (iAsc > 30 || iAsc < 123) return true; 	
	return false;
}

function PasteText(obj)
{
	Global.PasteObj = obj;
	Global.PriorText = obj.value;
	Global.timerPasteValidate = self.setTimeout("PasteValidate()", 300);
	return true;
}

function PasteValidate()
{
	closeTimeout(Global.timerPasteValidate);
	var szIn = Global.PasteObj.value;
	var szOut = "";
	if ( (szIn.length - Global.PriorText.length) > 2000 ) 
	{	
		Global.PasteObj.value = Global.PriorText;
		return;
	}
	for (var i=0; i< szIn.length; i++)
	{
		var x = szIn.charCodeAt(i);
		if (TestTextChar(x))
		{
			szOut += szIn.substr(i,1);
		}
	}
	Global.PasteObj.value = szOut;
}

function InputAlphaNumeric(e, OtherCharCodes)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (typeof(OtherCharCodes) != "undefined")
	{
		var OtherChars = OtherCharCodes.split(",");
		for (var i=0; i< OtherChars.length; i++)
		{
			//dbgMsg(iAsc + ":" + OtherChars[i]);
			if (OtherChars[i] == iAsc) return true;
		}
	}
	if (TestNavChars(iAsc)) return true;
	if (iAsc == 191) return true;
	if (iAsc == 32) return true; // space
	if (!e.shiftKey && NumberKeysOnly(iAsc)) return true;
	if (iAsc > 64 && iAsc < 91) return true; //A-Z
	if (iAsc > 95 && iAsc < 123) return true; //a-z'
	return false;
}

function InputDate(e)
{
	var iAsc = (window.event ? e.keyCode : e.which);
	if (TestNavChars(iAsc)) return true;
	if (iAsc == 32 || iAsc == 45 || iAsc == 46) return KeySubstitute("/"); 
	if (iAsc == 191) return true;
	//test for numeric
	if (iAsc > 47 && iAsc < 58) return true;
	return false;
}

function MatchInList(szList, szItem, szDelim)
{
	if (szList == "" || typeof(szItem)=="undefined" )
	{
		return false;
	}
	var szArrList = szList.toLowerCase().split((typeof(szDelim)=="undefined" ? "," : szDelim));
	var bReturn = false;
	var szTest = szItem.toLowerCase();
	for (i=0; i< szArrList.length && !bReturn; i++)
	{
		bReturn = (szTest == szArrList[i]);
	}
	return bReturn;
}

//gets the absolute XY position of a control as an array 
function AbsPos(objOrId) 
{
	var objElement = getObj(objOrId);
	iTop = objElement.offsetTop			// Get top position from the parent object
	iLeft = objElement.offsetLeft		   // Get left position from the parent object
	while(objElement.offsetParent!=null) 
	{ 
		// Parse the parent hierarchy up to the document element
		objParent = objElement.offsetParent // Get parent object reference
		iTop += objParent.offsetTop			// Add parent top position
		iLeft += objParent.offsetLeft		// Add parent left position
		objElement = objParent
	}
	// Return Position as an array
	var arrRet = new Array(2);
	arrRet["left"] = iLeft;
	arrRet["top"] = iTop;
	return arrRet;
}

function AbsTop(objOrId) 
{
	var objElement = getObj(objOrId);
	iTop = objElement.offsetTop			// Get top position from the parent object
	while(objElement.offsetParent!=null) 
	{ 
		// Parse the parent hierarchy up to the document element
		objParent = objElement.offsetParent // Get parent object reference
		iTop += objParent.offsetTop			// Add parent top position
		objElement = objParent
	}
	return iTop;
}

function AbsLeft(objOrId) 
{
	var objElement = getObj(objOrId);
	iLeft = objElement.offsetLeft			// Get Left position from the parent object
	while(objElement.offsetParent!=null) 
	{ 
		// Parse the parent hierarchy up to the document element
		objParent = objElement.offsetParent	// Get parent object reference
		iLeft += objParent.offsetLeft		// Add parent Left position
		objElement = objParent
	}
	return iLeft;
}

function IsPosInteger(szValue)
{
	if (szValue == null || szValue == "") return null;
	var bCont = true;
	var szNumbers = "0123456789";
	for (var i=0; i<szValue.length && bCont; i++)
	{
		bCont = szNumbers.indexOf(szValue.substr(i,1)) > -1;
	}
	return bCont;
}

function setOpacity(obj, value) 
{
	if (! Global.Browser.isIE)	
	{ 
		obj.style.opacity = value/100; 
	}
	else
	{
		obj.style.filter = 'alpha(opacity=' + value + ')';
	}
}

function clearOpacity(obj)
{
	if (! Global.Browser.isIE)	
	{ 
		obj.style.opacity = "";
	}
	else
	{
		obj.style.filter = "";
	}
}

Global.FadeTimer = null;
Global.FadeObj = null
function StartFade(FadeObj, incr)
{
	Global.FadeObj = FadeObj;
	Global.FadeTimer = setTimeout("Fade(" + (100-incr) + "," + incr +  ")", 100);
}

function Fade(percent, incr)
{
	percent = percent - incr;
	if (percent < 4)
	{
		StopFade("none", Global.FadeObj);
		return
	}
	setOpacity(Global.FadeObj, percent);
	Global.FadeTimer = setTimeout("Fade(" + percent + "," + incr + ")", 20);
}

function StopFade(displayStyle, obj)
{
	closeTimeout(Global.FadeTimer);
	obj.style.display = displayStyle;
	clearOpacity(obj)
}

function StripAlpha(szDim)
{
	if ((szDim == "") || (szDim == null)) return 0;
	
	var szOut = "";
	var szNumbers = "0123456789";
	if (typeof(szDim) == "number") return szDim;
	for (var i=0; i< szDim.length; i++)
	{
		var szChar = szDim.substr(i,1);
		if (szNumbers.indexOf(szChar) > -1)
		{
			szOut += szChar;
		}
	}
	return Number(szOut);
}

function CheckPostBackId_Callback(objReturn)
{
	//this function runs once on every page
	//if the postback id is less than the current id
	//the page is refreshed to itself to get a current view.
	Global.InAjax = false;
	Global.InSubmit = false;	
	if (objReturn.value == false)
	{
		SetLocation(window.top.location);
	}
	Global.CheckIdComplete = true;
}

function SetLocationIf(ClickObj, url)
{
	if (ClickObj.disabled) return false;
	window.top.location = url;
	return false;
}

function GoTo(loc)
{
	window.top.location = loc;
}

function SetLocation(url)
{
	window.top.location = url;
}

function LTrim( value )
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function GetEventPos(e) 
{
	var arrRet = new Array();
	
	var theEvent = null;
	if (typeof(e)=="undefined")
	{
		theEvent = this.event || (((this.ownerDocument || this.document || this).parentWindow || window).event);
	}
	else
	{
		theEvent = e;
	}
	
	if (Global.Browser.isIE) 
	{ 
		arrRet["left"] = theEvent.clientX + document.body.scrollLeft
		arrRet["top"] = theEvent.clientY + document.body.scrollTop
	} 
	else 
	{ 
		arrRet["left"] = theEvent.pageX
		arrRet["top"] = theEvent.pageY
	}  
	return arrRet;
}

function PositionPopUp(element, relatedElement, margin, xOffset, yOffset) 
{
	if (!margin) var margin=5;
	if (!xOffset) var xOffset = 5;
	if (!yOffset) var yOffset = 5;
	
	var Result = GetScrollAndPosition(element.offsetWidth, element.offsetHeight, getObj(relatedElement), margin, xOffset, yOffset)
	
	element.style.left = Result.left
	element.style.top  = Result.top
	
	if (Result.scroll)
	{
		window.scrollTo(Result.ScrollLeft, Result.ScrollTop);
	}
}

function GetScrollAndPosition(ElementWidth, ElementHeight, relatedElement, margin, xOffset, yOffset)
{
	if (!margin) var margin=5;
	if (!xOffset) var xOffset = 5;
	if (!yOffset) var yOffset = 5;
	
	var posRelated = AbsPos(relatedElement);
	var ScrollTop = GetScrollTop();
	var ScrollLeft = GetScrollLeft();
	
	var NewScrollTop;
	var NewScrollLeft;
	
	var yPosAB = new Array();
	yPosAB[0] = posRelated.top + relatedElement.offsetHeight + yOffset;
	yPosAB[1] = posRelated.top - ElementHeight - yOffset;
	
	var xPosAB = new Array();
	xPosAB[0] = posRelated.left + relatedElement.offsetWidth + xOffset;
	xPosAB[1] = posRelated.left - ElementWidth - xOffset;
	
	var WindowHeight = GetWindowHeight();
	var WindowWidth = GetWindowWidth();
	
	var PosY = -1;
	var iEnd = (   ( (yPosAB[1] - margin) < 0 ) ? 1 : 2 );
	var bYScroll = true;
	for (var i=0; i < 2 && PosY < 0 == true; i++)
	{
		if (  ( yPosAB[i] < (ScrollTop + margin) ) || ( yPosAB[i] > (ScrollTop + WindowHeight - ElementHeight - margin) )  )
		{
			PosY = - yPosAB[i];
			NewScrollTop = yPosAB[i] + ElementHeight + margin - GetWindowHeight();
		}
		else
		{
			bYScroll = false;
			NewScrollTop = ScrollTop;
			PosY = yPosAB[i];
		}
	}
	PosY = Math.abs(PosY);
	
	var PosX = -1;
	iEnd = (xPosAB[1] - margin < 0 ? 1 : 2);
	var bXScroll = true;
	for (var i=0; i < iEnd && PosX < 0 == true; i++)
	{
		if (  ( xPosAB[i] < (ScrollLeft + margin) ) || ( xPosAB[i] > (ScrollLeft + WindowWidth - ElementWidth - margin) )  )
		{
			PosX = - xPosAB[i];
			NewScrollLeft = xPosAB[i] + ElementWidth + margin - GetWindowWidth();
		}
		else
		{
			NewScrollLeft = ScrollLeft;
			bXScroll = false;
			PosX = xPosAB[i];
		}
	}
	PosX = Math.abs(PosX);
	
	var Result = new Array();
	Result["scroll"] = (bYScroll || bXScroll);
	Result["top"] = PosY;
	Result["left"] = PosX;
	Result["ScrollLeft"] = NewScrollLeft;
	Result["ScrollTop"] = NewScrollTop;
	return Result;
}

function GetWindowHeight() 
{
	return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight || 0;
}

function GetWindowWidth() 
{
	return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth || 0;
}

function GetScrollTop() 
{
	return window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0;
}

function GetScrollLeft() 
{
	return window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft || 0;
}

function RightStr(szX, NumChars)
{
	var iLen = szX.length;
	if (iLen < NumChars) return szX;
	return szX.substr(iLen-NumChars);
}

function LeftStr(szX, NumChars)
{
	var iLen = szX.length;
	if (iLen < NumChars) return szX;
	return szX.substr(0, NumChars);
}


function SelectDDLByText(ddl, Text, noMatchIndex)
{
	for (var i=0; i< ddl.options.length; i++)
	{
		if (ddl.options[i].text == Text)
		{	
			ddl.selectedIndex = i;
			return true;
		}
	}
	if (typeof(noMatchIndex) != "undefined") if (noMatchIndex != null) ddl.selectedIndex = noMatchIndex;
	return false;
}

function SelectDDLByValue(ddl, value, noMatchIndex)
{
	for (var i=0; i< ddl.options.length; i++)
	{
		if (ddl.options[i].value == value)
		{
			ddl.selectedIndex = i;
			return true;
		}
	}
	if (typeof(noMatchIndex) != "undefined") if (noMatchIndex != null) ddl.selectedIndex = noMatchIndex;
	return false;
}

function CopyOptions(From, To, StartIndex)
{
	To.options.length = 0;
	var iIndex = 0;
	if (typeof(StartIndex) == "undefined") var StartIndex = 0;
	for (var i=StartIndex; i < From.options.length; i++)
	{
		To.options[iIndex++] = new Option(From.options[i].text, From.options[i].value);
	}
}

function PseudoControl(szId, szType)
{
	this.type = "pseudo";
	this.ControlType = szType;
	this.id = szId;
	
	if (! MatchInList("rbl,ddl,cbl", szType, ","))
	{
		Message("Unexpected PseudoControl Type: " + szType);
	}
	this.evaluate();
}

function FixListTable(objOrId)
{
	var tbl = getObj(objOrId);
	tbl.cellSpacing = 0;
	tbl.cellPadding = 0;

	for (var iRow =0; iRow < tbl.rows.length; iRow++)
	{
		var Row = tbl.rows[iRow];
		for (var iCell = 0; iCell < Row.cells.length; iCell++)
		{
			var szClass = (Row.cells[iCell].className);
			Row.cells[iCell].className = "rblCell"
		}
	}
}

PseudoControl.prototype.SetValue = function (szValue)
{
	switch (this.ControlType)
	{
		case 'ddl':
			var objControl = DocAll(this.id);
			for (var i=0; i < szValue.length; i++)
			{
				objControl.options[i].selected = (szValue.substr(i,1) == "1");
			}
			break;
		
		case 'rbl':
		case 'cbl':
			for (var i=0; i < szValue.length; i++)
			{
				var objLI = DocAll(this.id + "_" + i);
				objLI.checked = (szValue.substr(i,1) == "1");
			}
			break;
			
		default:
			Message("Unexpected PseudoControl.type: " + this.ControlType);
			break;
	}
}


PseudoControl.prototype.lock  = function(bLocked)
{
	switch (this.ControlType)
	{
		case 'ddl':
			DocAll(this.id).disabled = bLocked;
			break;
		
		case 'rbl':
		case 'cbl':
			var bContinue = true;
			for (var i=0; bContinue; i++)
			{
				var objLI = DocAll(this.id + "_" + i, true);
				bContinue = (objLI != null);
				if (bContinue) objLI.disabled = bLocked;
			}
			break;
	}
}

PseudoControl.prototype.evaluate = function()
{
	var szValue = "_";
	var bListCheck = false;
	
	switch (this.ControlType)
	{
		case "ddl":
			var objControl = DocAll(this.id);
			for (var i=0; i < objControl.options.length; i++)
			{
				szValue += (objControl.options[i].selected ? "1" : "0"); 		
			}
			break;		
		
		case "rbl":
		case "cbl":
			
			var bContinue = true;
			for (var i=0; bContinue; i++)
			{
				var objLI = DocAll(this.id + "_" + i, true);
				bContinue = (objLI != null);
				if (bContinue)
				{
					szValue += (objLI.checked ? "1" : "0"); 
				}
			}
			break;
			
	}
	szValue = szValue.substr(1);
	this.value = szValue;
	return szValue;
}

//float stuff
var szFloats = "";
var szArrFloatY = new Array();

function Float(szId, y)
{
	var objFloat = DocAll(szId);
	szArrFloatY[szId] = (typeof(y)=='undefined' ? 5 : y );
	if (Global.Browser.isIE || Global.Browser.isOpera)
	{	
		window.attachEvent("onscroll", DoFloats);
	}
	else if (Global.Browser.isNS)
	{	
		window.addEventListener("onscroll", DoFloats, true);
	}
	szFloats += (szFloats == "" ? "" : "~" )  + szId;
	DoFloats(szId);
}

function Unfloat(szId)
{
	var szArrFloat = szFloats.split('~');
	var szNewFloats = "";
	for (var i=0; i< szArrFloat.length; i++)
	{
		if (szArrFloat[i] != szId)
		{
			szNewFloats += (i==0 ? "" : "~") + szArrFloat[i];
		}
	}
	
	szFloats = szNewFloats;
	if (szFloats == "")
	{
		if (Global.Browser.isIE) 
		{	
			window.detachEvent("onscroll", DoFloats);
		}
		else if (Global.Browser.isNS)
		{	
			window.removeEventListener("onscroll", DoFloats, true);
		}
	}
}

function DoFloats()
{
	if (szFloats != "")
	{
		var szArrFloats = szFloats.split("~");
		var iTop = (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop);
		for (var i=0; i< szArrFloats.length; i++)
		{
			DocAll(szArrFloats[i]).style.top = iTop + szArrFloatY[szArrFloats[i]];
		}
	}
}
//end floatstuff


function PageDirtyChecker()
{
	this.Controls = new Array();
	this.Values = new Array();
	this.IdIndex = new Array();
	this.Dirty = false;
}

PageDirtyChecker.prototype.Restore = PageDirtyChecker_Restore;
function PageDirtyChecker_Restore()
{
	for (iCtrl = 0; iCtrl< this.Controls.length; iCtrl ++)
	{
		switch (this.Controls[iCtrl].type)
		{
			case "checkbox":
				this.Controls[iCtrl].checked = this.Values[iCtrl];
				break;
			
			case "rblHandler":
			case "pseudo":
				this.Controls[iCtrl].SetValue(this.Values[iCtrl]);
				break;
				
			default:
				this.Controls[iCtrl].value = this.Values[iCtrl];
				break;
		}
	}
	this.Dirty = false;
}

PageDirtyChecker.prototype.Clear = function(ExceptList)
{
	var szExcept = "";
	if (typeof(ExceptList) != "undefined")
	{
		szExcept = "," + ExceptList + ",";
	}
	
	for (iCtrl = 0; iCtrl< this.Controls.length; iCtrl ++)
	{
		if (! MatchInList(szExcept, this.Controls[iCtrl].id, ",") )
		{
			switch (this.Controls[iCtrl].type)
			{
				case "checkbox":
					this.Controls[iCtrl].checked = false;
					break;
				case "rblHandler":
					this.Controls[iCtrl].SetValue("");
					break;
				case "pseudo":
					this.Controls[iCtrl].SetValue("0");
					break;
				default:
					this.Controls[iCtrl].value = "";
					break;
			}
		}
	}
	this.Dirty = true;
}


PageDirtyChecker.prototype.Lock = function (bLocked, ExceptList)
{
	var szExcept = "";
	if (typeof(ExceptList) != "undefined")
	{
		szExcept = "," + ExceptList + ",";
	}

	for (iCtrl = 0; iCtrl< this.Controls.length; iCtrl ++)
	{
		if (! MatchInList(szExcept, this.Controls[iCtrl].id, ",") )
		{
			switch (this.Controls[iCtrl].type)
			{
				case "checkbox":
					this.Controls[iCtrl].disabled = bLocked;
					break;
				
				case "pseudo":
					//this.Controls[iCtrl].Lock(bLocked);
					break;
					
				default:
					this.Controls[iCtrl].disabled = bLocked;
					this.Controls[iCtrl].className = (bLocked ? "dis" : "");
					break;
			}
		}
	}
	this.Dirty = true;
}

PageDirtyChecker.prototype.add = function(objOrId)
{
	var objControl = getObj(objOrId);
	if (objControl == null)
	{
		dbgMsg("null control" + objOrId);
		 return;
	}
	if (typeof(this.IdIndex[objControl.id]) == "undefined")
	{
		var iLen = this.Controls.length;
		this.IdIndex[objControl.id] = iLen;
		this.Controls[iLen] = objControl;
		switch (objControl.type)
		{
			case "checkbox":
				this.Values[iLen] = objControl.checked
				break;
			case "pseudo":
				this.Values[iLen] = objControl.evaluate();
				break;
			default:
				this.Values[iLen] = objControl.value;
				break;
		}
		if (this.Values[iLen] == null)
		{
			Message("PageDirtyChecker Value Error: " + objControl.id);
		}
	}
	else
	{
		Message("Duplicate PageDirtyChecker() control: " + objControl.id);
	}
}

PageDirtyChecker.prototype.IsDirty = function()
{
	for (var iCtrl=0; iCtrl<this.Controls.length; iCtrl++)
	{
		var szValue;
		switch (this.Controls[iCtrl].type)
		{
			case "checkbox":
				szValue = this.Controls[iCtrl].checked;
				break;
			case "pseudo":
				var objCtrl = this.Controls[iCtrl];
				szValue = objCtrl.evaluate();
				break;
			default:
				szValue = this.Controls[iCtrl].value;
				break;
		}	
		//alert(this.Controls[iCtrl].id + "=" + szValue);			
		this.Dirty = (this.Values[iCtrl] != szValue);
		if (this.Dirty)
		{
			return true;
		}
	}
	return false;
}

function btnClick(objBtn, szAction)
{
	if (Global.InSubmit || objBtn.disabled) return false;
	eval(szAction);
	return false
}


function isNumeric(szValue)
{
	if (szValue == null || szValue == "") return null;
	var bCont = true;
	var iDec = 0;
	var szNumbers = "0123456789";
	for (var i=0; i<szValue.length && bCont; i++)
	{
		var szX = szValue.substr(i,1);
		if (szX == ".")
		{
			iDec++;
			bCont = (iDec < 2);
		}
		else
		{
			bCont = szNumbers.indexOf(szX) > -1;
		}
	}
	//a_lert(szValue + (bCont ? " is Numeric" : " is NOT numeric") );
	return bCont;
}

function ButtonManager()
{
	this.IdIndex = new Array();
	this.Buttons = new Array();
	this.UnLockedState = new Array();
}

ButtonManager.prototype.add = function (objButton, szAction)
{
	if (typeof(objButton) == 'undefined' || objButton == null)
	{
		Message("ButtonManager cannot add null button");
		//this is for a break point:
		return;
	}
	
	this.UnLockedState[objButton.id] = objBtn.disabled;
	
	if (typeof(this.IdIndex[objButton.id]) == "undefined")
	{
		var iLen = this.Buttons.length;
		this.IdIndex[objButton.Id] = iLen;
		this.Buttons[iLen] = objButton;
	}
	else
	{
		Message("ButtonManager() error: Duplicate button " + objButton.id);
	}
}

ButtonManager.prototype.Enable = function (bEnabled)
{
	for (var i=0; i< this.Buttons.length; i++)
	{
		var szDisabled = (bEnabled ? "" : "_dis");
		this.enabler(this.Buttons[i], bEnabled, i);			
	}
}

ButtonManager.prototype.enabler = function(objButton, bEnabled, i)
{
	var szDisabled = (bEnabled ? "" : "_dis");
	switch (objButton.tagName.toUpperCase())
	{
		case "INPUT":
		case "IMG":
			objButton.src = objBtn.stub + szDisabled + ".gif";
			break;
	
		case "A":
		case "SPAN":
			objButton.className = objBtn.stub + szDisabled;
			break;
		
		default:
			Message("Unexpected ButtonManager Tag type: " + objButton.tagName);
			break;
	}
	objButton.disabled = !bEnabled;
	objButton.disabled = !bEnabled;
}

ButtonManager.prototype.Lock = function (bLockButtons)
{
	for (var i=0; i< this.Buttons.length; i++)
	{
		var objButton = this.Buttons[i];
		var bDisable = (bLockButtons ? true : this.UnLockedState[this.Buttons[i].id])
		this.enabler(objButton, !bDisable, i);		
	}
}

function Mid(szString, iStart, length)
{
	if (szString == null)
	{
		Message("Mid: Start index cannot be null.");
		return;
	}
	if (iStart < 1)
	{
		Message("Mid: Start index cannot be negative.");
		return;
	}
	if (szString.length < iStart)
	{
		return "";
	}
	if (length)
	{
		var iLen = length;
		if ((iStart + length -1) > szString.length)
		{
			iLen = szString.length - iStart + 1;
		}
		//same as visual basic (string starts at index 1)
		var tmpstr = szString.substr(iStart-1, iLen);
		return tmpstr;
	}
	try
	{
		var tmpstr = szString.subst(iStart-1);
		return tmpstr;
	}
	catch(e)
	{
		Message("Mid Error: " + e);
		return;
	}
}

function Instr(iStart, szString, szX)
{
	//same as visual basic (string starts at index 1)
	if (szString == null)
	{
		Message("Instr: search string was null.");
		return;
	}
	if (szString.Length < iStart)
	{
		Message("Instr: Start index was past end of the string.");
		return;
	}
	
	if (iStart == null)
	{
		iStart = 1;
	}
	
	if (iStart < 1)
	{
		Message("Instr: Start index cannot be negative.");
	}
	var iOut = szString.indexOf(szX, iStart-1);
	if (iOut > -1)
	{
		return iOut+1;
	}
	return -1;
}

String.prototype.Extract = function(start, end)
{
	return Extract(this, start, end, null);
}

Array.prototype.HasMatch = function(MatchItem)
{
	var bFound = false;
	for (var i=0; i < this.length && !bFound; i++)
	{
		bFound = (this[i] == MatchItem);
	}
	return bFound;
}

Array.prototype.indexOf = function(match, start)
{
	if (typeof(start) == "undefined") start = 0;
	var index = -1;
	if (start < this.length)
	{
		for (i = start; i< this.length && index < 0; i++)
		{
			if (this[i] == match) index = i;
		}
	}
	return index;
}

function ArrayMatch(arrObj, szMatch)
{
	if (arrObj == null)
	{
		return false;
	}
	return arrObj.HasMatch(szMatch);
}

Global.Buttons = new ButtonManager();

function closeTimeout(obj)
{
	if (obj == null) return;
	self.clearTimeout(obj);
	obj = null;
}

function Message(x)
{
	alert(x);
}

function Freezer()
{
	this.divFreeze = document.createElement("div")
	this.divFreeze.className = "FreezeFrameOff";
	this.divFreeze.innerHTML = "&nbsp;";
	document.body.appendChild(this.divFreeze);
	
	this.divWait = document.createElement("div");
	this.divWait.className = "PopUpOuter"
	this.divWait.innerHTML = "<img src='images/spacer.gif' alt='' width='185' height='75' border='0' />";
	document.body.appendChild(this.divWait);

	this.divWaitInner = document.createElement("div");
	this.divWaitInner.className = "PopUpInner";
	this.divWaitInner.innerHTML = "<table width='140' cellpadding=0 cellspacing='0' border='0'><tr><td style='height:35px; padding:5px;' valign='middle' align='center'><img src='images/wait.gif' alt='' width='32' height='16'/></td><td style='padding-top:10px;' align='center' class='lbl' valign='middle'>Please wait&nbsp;&hellip;&nbsp;</td></tr></table>";
	document.body.appendChild(this.divWaitInner);
}

Freezer.prototype.show = function (bOn) 
{
	this.divFreeze.className = 'FreezePane' + (bOn ? "On" : "Off") ;
	var iTop = (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop) + 230;
	this.divWait.style.top = iTop;
	this.divWaitInner.style.top = iTop + 20;
	this.divWait.style.display= (bOn ? "block" : "none");	
	this.divWaitInner.style.display = (bOn ?  "block" : "none");	
}

Global.Freeze = null;
function ShowWaiting()
{
	if (Global.Freeze == null)
	{
		Global.Freeze = new Freezer();
	}
	Global.InSubmit = true;
	Global.Freeze.show(true)
}

function CloseWaiting()
{
	Global.Freeze.show(false);
	Global.InSubmit = false;
	Global.InAjax = false;	
	if (typeof(Global.Buttons) != "undefined")
	{
		Global.Buttons.Lock(false);
	}
}

function AddPageControls(btnList)
{
	var arr = btnList.split(',');
	for (i=0; i< arr.length; i++)
	{
		var id = arr[i];
		Page[id] = DocAll(id, true);
		if (Page[id] == null)
		{
			//dis_dbgMsg("Control '" + id + "' was not found");
		}
	}
}

function rblHandler(hiddenControl, id, count, onChange) 
{
	this.id = id;
	this.hidden = getObj(hiddenControl);
	this.count = count;
	this.OnChange = onChange;
	this.type = "rblHandler";
	this.SetValue(this.hidden.value);
}

rblHandler.prototype.SetValue = function(NewValue)
{
	this.value = NewValue;
	this.hidden.value = NewValue;
	for (var i=0; i < this.count; i++)				
	{
		var obj = DocAll("rbl" + this.id + "_" + i);
		obj.checked = (this.hidden.value == obj.value);						
	}
}


rblHandler.prototype.click = function()
{
	this.value = "";															
	for (var i=0; i < this.count && this.value == ""; i++)				
	{																						
		var obj = DocAll("rbl" + this.id + "_" + i);																	
		if (obj.checked)																	
		{																					
			this.value = obj.value;	
		}																					
	}
	if (this.value != this.hidden.value)
	{
		eval(this.OnChange);
		this.hidden.value = this.value;	
	}
}

function FormatError(bError, szError, szOnClick)
{
	return (bError ? FormatErrorMessage(szError, "", szOnClick) : "")
}

function FormatErrorMessage(szError, szCtrlName, szOnClick)
{
	if (szError == "") return "";
	szOut = "<p class='redMsg'";
	if (szOnClick != null && szOnClick != "")
	{
		szOut += " onMouseover=\"this.className='redMsg_mo'\" onMouseout=\"this.className='redMsg'\" onclick=\"" + szOnClick + "\"";
	}
	szOut += ">"
	if (szCtrlName != "") 
	{
		szOut += szCtrlName + " ";
	}
	szOut += szError + "</p>"; 
	return szOut;
}

function FixNull(Value, ValueIfNull)
{
	if (Value==null || Value == "") return ValueIfNull;
	return Value;
}

function ctrlDisable(objOrId, bDisabled, styleDisabled, styleEnabled)
{
	obj = getObj(objOrId);
	obj.className = (bDisabled ? styleDisabled : styleEnabled);
	obj.disabled = bDisabled;
}

Page.Resizer = null;
function PageResize()
{
	if (Page.Resizer == null) return;
	Page.Resizer();
}

function NullSplit(x, parseChar)
{
	if (x==null || x.length == 0) return null
	return x.split(parseChar);
}

Page.LoadedTimer = self.setTimeout("PageLoaded()", 1000);

function AutoLogOff()
{
	var szHtml = "<div id=\"divAutoLogOffx\" style=\"position:absolute; width: 302px; left: 400px; top: 180px; border: solid 4px white;\">"
	+ "<div style=\"width: 270px; height: 81px; background: white; padding: 10px; border: solid 6px #ccDDFF;\">"
	+ "<p onmousedown=\"dragStart(event,'divAutoLogOffx');\" style=\"font-size: 12px; font-family: Arial\">"
	+ "There has been no activity for ten minutes. You will be automatically logged off in thirty seconds.</p>"
	+ "<div style=\"float:left;clear:left;padding-top: 15px; padding-left: 10px;\">"
	+ "<img alt=\"wait\" src=\"images/Wait2.GIF\" /></div>"
	+ "<div  style=\"float:left; width: 130px; padding-top: 23px; padding-left: 52px\">"
	+ "<input id=\"btnStayLoggedIn\" onclick=\"return StayLoggedIn(this);\" onmouseout=\"btnMO(this, false);\" onmouseover=\"btnMO(this,true);\""
	+ "src=\"images/btnStayLoggedIn.gif\" type=\"image\" />"
	+ "</div>"
	+ "</div>"
	+ "</div>";
	
	Page.divAutoLogOff = addElement("div", document.body, "divALO", szHtml, null, true);
	Page.AutoLogOffTimer = self.setTimeout("SetLocation('Login.aspx');", 600000);
}


function PrevNext(name, id, container, count)
{
	this.container = new Array();
	this.busy = true;
	this.count = count;
	this.id = id;
	this.name = name;
	this.SelectedIndex = 0;
	this.OnClick = null;
	this.HasTitleLinks = false;
	this.Item = new Array();
	
	var arrCont = container.split("|");
	
	for (var iPage=0; iPage< count; iPage++)
	{
		this.Item[iPage] = getObj(this.id + "_" + iPage);
	}
	
	for (var i=0; i< arrCont.length; i++)
	{
		var pn = "";
		this.container[i] = getObj(arrCont[i]);
		for (var iPage=0; iPage< count; iPage++)
		{
			pn += "<div id=\"_" + this.name + "_" + i + "_" + iPage + "\" class=\"pnItem" + (iPage==0 ? "_dis" : "") + "\" onclick=\"" + 
				this.name + ".click(" + iPage + ");\" onMouseover=\"btnMO(this,true)\"" +
				" onMouseOut=\"btnMO(this,false)\">" + (iPage+1) + "</div>";
		}
		pn += "&nbsp;&nbsp;<div id=\"" + this.name + "_" + i + "_prev\" class='pnPN_dis' onclick=\"" + 
				this.name + ".prev();\" onMouseover=\"btnMO(this,true)\"" +
				" onMouseOut=\"btnMO(this,false)\">Previous</div>" + 
				"<div id=\"" + this.name +  "_" + i + "_next\" class='pnPN' onclick=\"" + 
				this.name + ".next();\" onMouseover=\"btnMO(this,true)\"" +
				" onMouseOut=\"btnMO(this,false)\">Next</div>";
	
		this.container[i].innerHTML = pn;
	}
	this.busy = false;
}

PrevNext.prototype.BuildTitles = function(TitleContainer, cssClass)
{
	this.busy = true;
	this.TitleContainer = getObj(TitleContainer);
	this.TitleItem = new Array();
	this.HasTitleLinks = true;
	var x = "";
	for (var i=0; i< this.count; i++)
	{
		var id= this.id + "_LI_" + i
		this.TitleItem[i] = id;
		var text = this.Item[i].firstChild.innerHTML;
		x += "<div class='" + cssClass + (i==this.SelectedIndex ? "_dis" : "") + "' id='" + id + 
			"' onmouseover='btnMO(this,true)' onmouseout='btnMO(this,false)' onclick=\"" + this.name +
			 ".click(" + i + ");\">" + text + "</div>";
	}
	
	this.TitleContainer.innerHTML = x;
	for (var i=0; i< this.count; i++)
	{
		this.TitleItem[i] = getObj(this.TitleItem[i]);
	}
	this.busy = false;
}

PrevNext.prototype.click = function(index)
{
	if (index==this.SelectedIndex || this.busy) return;	
	this.busy = true;
	
	this.PreviousIndex = this.SelectedIndex;
	this.SelectedIndex = index;
	this.Item[this.PreviousIndex].style.display = "none";
	this.Item[this.SelectedIndex].style.display = "";
	
	if (this.HasTitleLinks)
	{
		btnDisable(this.TitleItem[this.PreviousIndex], false);
		btnDisable(this.TitleItem[this.SelectedIndex], true);
	}
	
	for (var i=0; i< this.container.length; i++)
	{
		btnDisable("_" + this.name + "_" + i + "_" + this.SelectedIndex, true);
		btnDisable("_" + this.name + "_" + i + "_" + this.PreviousIndex, false);
		btnDisable(this.name + "_" + i + "_next", (this.SelectedIndex == (this.count -1)));
		btnDisable(this.name + "_" + i + "_prev", (this.SelectedIndex == 0));
	}
	
	if (this.OnClick != null) this.OnClick(this);
	this.busy = false;
	
	window.scrollTo(0, AbsTop(this.Item[this.SelectedIndex].id)-40);
}

PrevNext.prototype.prev = function()
{
	if (this.SelectedIndex == 0 || this.busy) return;
	var i=this.SelectedIndex-1;
	this.click(i);
}

PrevNext.prototype.next = function()
{
	if (this.SelectedIndex == this.count-1 || this.busy) return;
	var i = this.SelectedIndex+1
	this.click(i);
}

function TabHandler(name, ClientId, AutoPostBack, TabClickAction, TabType, DefaultTabCSS, ImageFolder)
{
	this.ClientId = ClientId;
	this.SelTabCtrl = getObj(ClientId + "hiddenSelectedTab");
	this.DataCtrl = getObj(ClientId + "hiddenTabData");
	this.name = name;
	this.id = name;
	this.disabled = false;
	this.TabData = new Array()
	var TabDataList = this.DataCtrl.value.split("|");
	this.TabNames = new Array();
	this.TabData = new Array();
	var SelectedTabName = this.SelTabCtrl.value.toLowerCase();
	for (var i=0; i< TabDataList.length; i++)
	{
		var subList = TabDataList[i].split("~");
		this.TabNames[i] = subList[0].toLowerCase();
		if (this.TabNames[i] == SelectedTabName)
		{
			this.SelectedIndex = i;
		}
		this.TabData[i] = subList[2];
	}
	this.Action = TabClickAction;
	this.tagName = "Tab";
	this.TabType = TabType;
	this.AutoPostBack = AutoPostBack;
	if (this.TabType != "Text")
	{
		this.ImageFolder = ImageFolder;
	}
	this.DefaultTabCSS = DefaultTabCSS;
}

TabHandler.prototype.MO = function(ClickTab, TabIndex, bMO)
{
	var TabName = this.TabNames[TabIndex].toLowerCase();
	if (this.disabled || this.SelectedIndex == TabIndex) return;
	if (this.TabType == "Text")
	{
		ClickTab.className = this.DefaultTabCSS + (bMO ? "_mo": "");
	}
	else
	{
		ClickTab.src = this.ImageFolder + this.name + TabName + (bMO ? "_mo" : "") + ".gif";
	}
}

TabHandler.prototype.TabClick = function(ClickTab, TabIndex)
{
	var TabName = this.TabNames[TabIndex].toLowerCase()
	if (this.disabled || this.SelectedIndex == TabIndex) return false;
	
	this.SelTabCtrl.value = TabName;
	
	if (this.Action == null)
	{
		Page.PopIn = false;
		if (this.AutoPostBack)
		{
			document.forms[0].submit();
		}
		else
		{
			window.top.location = this.TabData[TabIndex];
		}
	}
	else 
	{
		this.disabled = true;
		if (this.Action(TabName, TabIndex))
		{
			var PriorTab = DocAll(this.ClientId + this.SelectedIndex);
			if (this.TabType == "Text")
			{	
				PriorTab.className = this.DefaultTabCSS;
				ClickTab.className = this.DefaultTabCSS + "_sel";
			}
			else
			{
				PriorTab.src = this.ImageFolder + this.TabNames[this.SelectedIndex] + "." + this.TabType;
				ClickTab.src = this.ImageFolder + this.TabNames[TabIndex] + "_sel." + this.TabType;
			}
			this.SelectedIndex = TabIndex;
			if (this.AutoPostBack) document.forms[0].submit();
		}
		this.disabled = false;
	}
	return false;
}

TabHandler.prototype.Tab = function()
{
	return this.SelTabCtrl.value;
}

TabHandler.prototype.disable = function(bDisable)
{
	this.disabled = bDisable;
	for (var i=0; i< this.TabNames.length; i++)
	{
		if (i != this.SelectedIndex)
		{
			var Tab = DocAll(this.ClientId + i);
			if (this.TabType == "Text")
			{
				Tab.className = this.DefaultTabCSS + "_dis";			
			}
			else
			{
				Tab.src = this.ImageFolder + this.ClientId + this.TabNames[i] + (bDisable ? "_dis." : ".") + this.TabType;
			}
		}
	}
}

// A better calendar popup. Written by Geoffrey J. Swenson / geewhizbang.com
//
//  Entirely client-side javascript, no server side postback necessary, so it is fast and flexible.
//	Should be compatible with most AJAX frameworks, as all it does is popup and set the value of a page control, usually a text box.
//	Tested with Opera, Firefox, and IE6 / IE7.
//  Includes useful extensions to the javascript Date object, but may be incompatible with code that has also extended the Date object.
// 
//

var CurrentCalendar = null;
function Calendar(AnchorElement, format, SelectedDate)
{
	this.AnchorElement = getObj(AnchorElement);
	this.SelectedDate = null;
	if (typeof(SelectedDate) == "undefined")
	{
		if (IsDate.Check(this.AnchorElement.value))
		{
			this.SelectedDate = IsDate.DateValue;
		}
		else
		{
			this.SelectedDate = Now().start();
		}
	}
	else
	{
		this.SelectedDate = SelectedDate;
	}
	this.CurrentMonth = this.SelectedDate.firstDayOfMonth();
	this.Format = format;
	CurrentCalendar = this;
	this.show()
}

function ShowCal(TbObjOrId, OnSelect)
{
	tb = getObj(TbObjOrId);
	if (tb.disabled) return false;
	new Calendar(tb, "%MM/%DD/%YYYY");
	if (typeof(OnSelect) != undefined)
	{
		CurrentCalendar.OnSelect = OnSelect;
	}
	else
	{
		CurrentCalendar.OnSelect = null;
	}
	return false;
}

Calendar.prototype.BuildButton = function (UpDn, incr)
{
	var szOut = "<td class='Cal_" + UpDn + "' onclick='CurrentCalendar.show(" + incr + ");' onmouseover=\"this.className='Cal_" + UpDn + "_mo';\" onmouseout=\"this.className='Cal_" + UpDn + "';\">&nbsp;</td>";
	return szOut
}

Calendar.prototype.close = function()
{
	document.body.removeChild(this.element);
	document.body.removeChild(this.backgroundElement);
	this.OnSelect();
	CurrentCalendar = null;
}

Calendar.prototype.click = function(iMonth, iDay)
{
	var m = Number(this.CurrentMonth.getMonth());
	m = m + Number(iMonth) + 1;
	var y = this.CurrentMonth.getFullYear();
	if (m > 12)
	{
		m = m-12;
		y++;
	}
	else if (m < 1)
	{
		m = m+12;
		y--;
	}
	this.AnchorElement.value = m + "/" + iDay + "/" + y;
	this.close();
}


Calendar.prototype.CurrentDate = function()
{
	this.AnchorElement.value = Now().format(this.Format);
	this.close();
}

Calendar.prototype.show = function(MonthOffset)
{	
	if (typeof(MonthOffset) == "undefined") var MonthOffset = 0;
	this.CurrentMonth = this.CurrentMonth.addMonths(MonthOffset);
	
	var iDayOfWeek = this.CurrentMonth.getDay();
	var BaseDate;
	var iDisplayRows = 6;
	var iMonth = -1;
	if (iDayOfWeek == 0) 
	{
		iMonth = 0
		BaseDate = this.CurrentMonth;
		iDisplayRows = 5
	}
	else
	{
		BaseDate = this.CurrentMonth.addDays(-iDayOfWeek);
	}
	var iDay = BaseDate.getDate();
	
	var iLastDay = BaseDate.getDaysInMonth();
	
	var iMarkDate = -1
	if (this.CurrentMonth.getFullYear() == this.SelectedDate.getFullYear() && this.CurrentMonth.getMonth() == this.SelectedDate.getMonth())
	{
		iMarkDate = this.SelectedDate.getDate();
	}
	var szHtml = "<table cellpadding=0 cellspacing=0><tr>";
	szHtml += this.BuildButton("dn", -1) + "<td class='Cal_Month'>" + this.CurrentMonth.getMonthName() + "</td>" + this.BuildButton("up", 1) + "<td class='Cal_MYSpacer'>&nbsp;</td>";
	szHtml += this.BuildButton("dn", -12) + "<td class='Cal_Year'>" + this.CurrentMonth.getFullYear()  + "</td>" + this.BuildButton("up", 12);
	szHtml += "</tr></table><table cellpadding=0 cellspacing=0 class='Cal_tblHed'><tr>"
	var arr = new Array("Su","Mo","Tu","We","Th","Fr","Sa")
	for (i=0; i < 7; i++)
	{
		szHtml += "<td class='Cal_TH'>" + arr[i] + "</td>";
	}
	szHtml += "</tr></table>"
	szHtml += "<table cellpadding=0 cellspacing=0>"
	var bContinue = true;
	var iOdd = (iMonth == 0 ? 0 : 1);
	var iRows = 0;
	for (z=0; z < iDisplayRows && bContinue; z++)
	{	
		if (iMonth == 1)
		{
			bContinue = false;
		}
		else
		{	
			iRows++;
			szHtml += "<tr>";
			for (i=0; i < 7; i++)
			{
				szHtml += "<td class='Cal_m' onclick='CurrentCalendar.click(" + iMonth + "," + iDay + ");' onmouseover=\"this.className='Cal_m_mo';\" onmouseout=\"this.className='Cal_m';\">"
				var szMark = "";
				if (iMarkDate > 0 && iMonth == 0 && iMarkDate == iDay)
				{
					szMark = " Cal_Mark";
				}
				szHtml += "<span class='Cal_OE" + iOdd + szMark + "'>" + iDay + "</span></td>";
				iDay++;
				if (iDay > iLastDay)
				{
					iDay = 1;
					iMonth++;
					iOdd = (iMonth == 0 ? 0 : 1);
					iLastDay = this.CurrentMonth.addMonths(iMonth).getDaysInMonth();
				}
			}
			szHtml += "</tr>";
		}
	}
	while (iRows < 6)
	{
		szHtml += "<tr><td colspan='7' class='Cal_BR'></td></tr>";
		iRows++;
	}
	
	szHtml += "<tr><td colspan='7' class='Cal_BtnRow'><span class='Cal_btn' onmouseover=\"this.className='Cal_btn_mo'\" onmouseout=\"this.className='Cal_btn'\" onclick='CurrentCalendar.CurrentDate()'>Today</td></td></tr></table>";
	if (this.element == null)
	{
		this.backgroundElement = addElement("div", document.body, "CalBG_" + this.AnchorElement.id, "<div class='Cal_BgFreeze' onclick='CurrentCalendar.close();'>&nbsp;</div>", "Cal_BgParent", true);
		this.element = addElement("div", document.body, "Cal_" + this.AnchorElement.id, szHtml, "Cal_Outer", true);
	}
	else
	{
		this.element.innerHTML = szHtml;
	}
	this.backgroundElement.style.height = document.body.clientHeight + "px";
	this.backgroundElement.style.width = document.body.clientWidth + "px";
	PositionPopUp(this.element, this.AnchorElement) 
}

// Global Now() function
function Now() 
{
	return new Date();
}

Date.prototype.addMilliseconds = function(ms)
{	
	return new Date(new Date().setTime(this.getTime() + (ms)));	
}

Date.prototype.addSeconds = function(s)
{
	return AddTime(this, 1000, s)
}

Date.prototype.addMinutes = function(m)
{
	return AddTime(this, 60000, m)
}

Date.prototype.addHours = function(h)
{
	return AddTime(this, 3600000, h);
}

Date.prototype.addDays = function(d)
{
	return AddTime(this, 86400000, d);
}

function AddTime (theDate, Incr, howMany)
{
	var Count = Math.abs(Math.round(howMany))
	var incr = (howMany > 0) ? Incr : -Incr;
	for (var i=0; i < Count; i++)
	{
		theDate = theDate.addMilliseconds(incr);	
	}
	return theDate
}

Date.prototype.addWeeks = function(w)
{
	return AddTime(this, 604800000, w)
}

Date.prototype.addMonths = function(mm)
{
	mm = Math.floor(mm)
	if (mm == 0) return this;
	var iMAbs = Math.abs(mm);
	var iSign = (mm < 0 ? -1 : 1)	
	var iYDiff = iSign * Math.floor(iMAbs / 12);
	var iMDiff = iSign * (iMAbs % 12);
	
	var iYear = this.getFullYear()+iYDiff;
	var iMonth = this.getMonth() + iMDiff;
	
	var iDay = this.getDate();
	if (iDay > 28)
	{
		iMonthDays = getDaysInMonth(iMonth, iYear);
		if (iDay > iMonthDays)
		{
			this.setDays(iMonthDays);
		}
	}
	var dt = this.clone();
	dt.setYear(iYear);
	dt.setMonth(iMonth);
	return dt;
}

Date.prototype.addYears = function(yy)
{
	dt.setYear(this.getFullYear()+yy);
}

Date.prototype.getMonthName = function() 
{
	var m = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
	return m[(0 == arguments.length) ? this.getMonth() : arguments[0]]
}

Date.prototype.getMonthAbbreviation = function() 
{
	var m = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	return m[(0 == arguments.length) ? this.getMonth() : arguments[0]]
}

Date.prototype.getDayName = function() 
{
	var d = Array ("Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday","Saturday");
	return d[(0 == arguments.length) ? this.getDay() : arguments[0]];
}

Date.prototype.getDayAbbreviation = function() 
{
	var d = Array ("Sun","Mon", "Tue","Wed","Thu","Fri","Sat");
	return d[(0 == arguments.length) ? this.getDay() : arguments[0]];
}

Date.prototype.getCivilianHours = function() 
{
	return (this.getHours() < 12) ? this.getHours() : this.getHours() - 12;
}
Date.prototype.getMeridiem = function() 
{
	return (this.getHours() < 12) ? "AM" : "PM";
}

Date.prototype.to_s = Date.prototype.toString;
Date.prototype.format = function(fs) 
{
	 fs = fs.replace(/%YYYY/, this.getFullYear().toString());
	 fs = fs.replace(/%YY/, this.getFullYear().toString().substr(2,2));
	 
	 fs = fs.replace(/%MMMM/, this.getMonthName(this.getMonth()).toString());
	 fs = fs.replace(/%MMM/, this.getMonthAbbreviation(this.getMonth()).toString());
	 fs = fs.replace(/%MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : "0" + (this.getMonth() + 1).toString());
	 fs = fs.replace(/%M/, (this.getMonth() + 1).toString());
	 
	 fs = fs.replace(/%DDDD/, this.getDayName(this.getDay()).toString());
	 fs = fs.replace(/%DDD/, this.getDayAbbreviation(this.getDay()).toString());
	 fs = fs.replace(/%DD/, this.getDate() > 9 ? this.getDate().toString() : "0" + this.getDate().toString());
	 fs = fs.replace(/%D/, this.getDate().toString());
	 
	 fs = fs.replace(/%HH/, this.getHours() > 9 ? this.getHours().toString() : "0" + this.getHours().toString());
	 fs = fs.replace(/%H/, this.getHours().toString());
	 fs = fs.replace(/%hh/, this.getCivilianHours() > 9 ? this.getCivilianHours().toString() : "0" + this.getCivilianHours().toString());
	 fs = fs.replace(/%h/, this.getCivilianHours());

	 fs = fs.replace(/%mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : "0" + this.getMinutes().toString());
	 fs = fs.replace(/%m/, this.getMinutes().toString());

	 fs = fs.replace(/%ss/, this.getSeconds() > 9 ? this.getSeconds().toString() : "0" + this.getSeconds().toString());
	 fs = fs.replace(/%s/, this.getSeconds().toString());

	 fs = fs.replace(/%nnn/, this.getMilliseconds().toString());
	 fs = fs.replace(/%p/, this.getMeridiem());
	 return fs;
}

Date.prototype.getDaysInMonth = function() 
{
	var mm = this.getMonth();
	switch (mm)
	{
		case 1:
			return (this.getFullYear() ? 29 : 28);
			break;
		default:
			var m = new Array ( 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
			return m[mm];
			break;
	}
}

Date.prototype.toString = function() 
{
	if (0 == arguments.length || 1 < arguments.length) return this.to_s();
	return this.format(arguments[0].toString());
} 

Date.prototype.isLeapYear = function() 
{
	return isLeapYear(this.getFullYear());
}

function isLeapYear(iYear)
{
	if (0 == iYear % 400) return true;
	if (0 == iYear % 100) return false;
	return (0 == iYear % 4) ? true : false;
}

Date.prototype.start = function() 
{
	var dt = this.clone();
	dt.setHours(0);
	dt.setMinutes(0);
	dt.setSeconds(0);
	dt.setMilliseconds(1);
	return dt;
}

Date.prototype.noon = function() 
{
	var dt = this.clone();
	dt.setHours(12);
	dt.setMinutes(0);
	dt.setSeconds(0);
	dt.setMilliseconds(0);
	return dt;
}

Date.prototype.firstDayOfMonth = function() 
{	
	return new Date(this.getFullYear(), this.getMonth(), 1, 12, 0, 0);
}
Date.prototype.lastDayOfMonth = function() 
{	
	return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth(), 12, 0, 0);
}

Date.prototype.clone = function() 
{
	var dt = new Date();
	dt.setTime(this.getTime());	
	return dt;
}

function DateChecker()
{
	this.DateValue = null;
}

DateChecker.prototype.Check = function(szValue)
{
	if (szValue == "")
	{
		return false;
	}
	this.DateValue = null;
	try
	{
		var dt = new Date(szValue);
		if (isNaN(dt)) return false;
		this.DateValue = dt;
		var szArrDate = szValue.split('/');
		if (szArrDate.length != 3) return false;
		if (dt.getMonth()+1 != Number(szArrDate[0])) return false;
		if (dt.getDate() != Number(szArrDate[1])) return false;
		var FullYear = dt.getFullYear()
		if (FullYear != Number(szArrDate[2])) return false;
		
		if (FullYear < 1995) return false;
		//var dtNow = new Date()
		//if (dtNow.addDays(5000) < dt) return false;
	}
	catch (e)
	{
		return false;
	}
	return true;
}
var IsDate = new DateChecker()