//create object for page properties
//  (this is for items unique to a page)
var Page = new Array();
// create global JS settings
//  (this is for items available on any page)
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;
}
