﻿var IE = (document.all) ? 1 : 0;

var QueryString = new Object(); 
var query = location.search.substring(1); 
var pairs = query.split("&"); 

for (var iPair = 0; iPair < pairs.length; iPair++) 
{ 
	var pos = pairs[iPair].indexOf('='); 
	if (pos == -1) continue; 

	var argname = pairs[iPair].substring(0, pos); 
	var value = pairs[iPair].substring(pos + 1); 
	
	QueryString[argname] = unescape(value); 
}

var EncryptedUserId = (QueryString["u"] == undefined) ? "" : QueryString["u"];;

var Images = new function()
{
	this.PreLoad = function()
	{
		var d = document;
		if (d.images)
		{
			if (!d.PreLoadArray)
			{
				document.PreLoadArray = new Array();
			}
			var j = d.PreLoadArray.length, 
				a = this.PreLoad.arguments;
			for (var i = 0; i < a.length; i++)
			{
				if (a[i].indexOf("#") != 0)
				{
					d.PreLoadArray[j] = new Image;
					d.PreLoadArray[j++].src = a[i];
				}
			}
		}
	}
	
	this.Restore = function()
	{
		var i, 
			x, 
			a = document.ImagesArray; 
		for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) 
		{
			x.src=x.oSrc
		}
	}
	
	this.FindObject = function(n, d)
	{
		var p, i, x;
		if (!d)
		{
			d = document;
		}
		
		if ((p = n.indexOf("?")) > 0 && parent.frames.length)
		{
			d = parent.frames[n.substring(p + 1)].document;
			n = n.substring(0, p);
		}
		
		if (!(x = d[n]) && d.all)
		{
			x = d.all[n];
		}
		
		for (i = 0; !x&&i < d.forms.length; i++)
		{
			x = d.forms[i][n];
		}
		for (i = 0; !x && d.layers && i < d.layers.length; i++)
		{
			x = this.FindObject(n, d.layers[i].document);
		}
		
		if (!x && d.getElementById)
		{
			x = d.getElementById(n);
		}
		
		return x;
	}
	
	this.SwapImages = function()
	{
		var j = 0,
			x,
			a = this.SwapImages.arguments;
			
		document.ImagesArray = new Array;
		
		for (var i = 0; i < (a.length -2); i+=3)
		{
			if ((x = this.FindObject(a[i])) != null)
			{
				document.ImagesArray[j++] = x;
				if (!x.oSrc)
				{
					x.oSrc = x.src;
				}
				
				x.src = a[i + 2];
			}
		}
	}
}

var PageUI = new function()
{
	this.Escape = function(pString)
	{
		var RetValue = escape(pString);
		
		while (RetValue.indexOf("+") >= 0)
		{
			RetValue = RetValue.replace("+", "%2B");
		}
		
		return RetValue;
	}
	
	this.Unescape = function(pString)
	{
		var RetValue = unescape(pString);
		
		while (RetValue.indexOf("%2B") >= 0)
		{
			RetValue = RetValue.replace("%2B", "+");
		}
		
		return RetValue;
	}
	
	this.HtmlElements = new function()
	{
		this.FlashTimeOut = null;
		
		this.FlashObjects = function(pLastRun)
		{
			pLastRun = (pLastRun == null) ? false: pLastRun;
			
			var Style1 = "FlashingObject"
			var Style2 = "FlashingObjectOff";
			var AllFlashObjects = document.getElementsByTagName("flashingobject");

			if (AllFlashObjects != null)
			{

				for (iObject = 0; iObject < AllFlashObjects.length; iObject++)
				{
					var FlashObjectParent = AllFlashObjects[iObject].parentNode
					
					if (FlashObjectParent != null)
					{
						if (FlashObjectParent.className.indexOf("Hidden") < 0)
						{
							FlashObjectParent.className = (pLastRun ? Style1 : (FlashObjectParent.className == Style1 ? Style2 : Style1));
						}
					}
				}
				
				if (!pLastRun)
				{
					this.FlashTimeOut = setTimeout("PageUI.HtmlElements.FlashObjects()", 500);
				}
				else
				{
					clearTimeout(this.FlashTimeOut);
				}
			}
		}
		
		this.GetElementId = function(pId, pElement)
		{	
			var RetValue = "";
			var AllElements = document.getElementsByTagName(pElement);
				
			if (AllElements.length > 0)
			{
				for (var iElement = 0; iElement < AllElements.length; iElement++)
				{
					if (AllElements[iElement].id.indexOf(pId) >= 0)
					{
						RetValue = AllElements[iElement].id;
					}
				}
			}
			
			return RetValue;
		}

		this.GetHtmlText = function(pElement)
		{
			var Value = (document.all) ? pElement.innerText : pElement.textContent;
			
			return Value;
		}

		this.GetKeyPress = function(event)
		{
			var keyCode = null;
		
			if (event.which) 
			{
				keyCode = event.which;
			}
			else if (event.keyCode) 
			{
				keyCode = event.keyCode;
			} 
		
			return keyCode;
		}
		
		this.GetObjectAttribute = function(pObject, pName)
		{
			var RetValue = "";
			
			if (pObject != null)
			{
				if (pObject.attributes.getNamedItem(pName) != null)
				{
					RetValue = pObject.attributes.getNamedItem(pName).value;
				}
			}
			
			return RetValue;
		}
		
		this.GetObjectDimensions = function(pObject)
		{
			var Position = this.GetObjectPosition(pObject);
			var objWidth = pObject.offsetWidth;
			var objHeight = pObject.offsetHeight;
			
			return {Top:Position.Top, Left:Position.Left, Height:objHeight, Width:objWidth};
		}
		
		this.GetObjectPosition = function(pObject)
		{
			var iTop = -1;
			var iLeft = -1;
			
			if (pObject != null)
			{
				iTop = pObject.offsetTop;
				iLeft = pObject.offsetLeft;

				while (pObject.offsetParent)
				{
					pObject = pObject.offsetParent;
					iTop += pObject.offsetTop;
					iLeft += pObject.offsetLeft;
				}
			}
			
			return {Top:iTop, Left:iLeft};
		}
		
		this.SetHtmlText = function(pElement, pValue)
		{
			if (document.all)
			{
				pElement.innerText = pValue;
			}
			else
			{
				pElement.textContent = pValue;
			}
		}
		
		this.SelectOption = function(pControlName, pValue)
		{
			var CurrentControl = document.getElementById(pControlName);
			
			if (CurrentControl != null)
			{
				for (var iOptions = 0; iOptions < CurrentControl.length; iOptions ++)
				{
					if (CurrentControl.options[iOptions].value == pValue)
					{
						CurrentControl.options[iOptions].selected = true;
					}
				}
			}
		}
		
		this.Decode = function(pText)
		{
			while (pText.indexOf("&gt;") >= 0)
			{
				pText = pText.replace("&gt;",">");
			}
			while (pText.indexOf("&lt;") >= 0)
			{
				pText = pText.replace("&lt;","<");
			}
			while (pText.indexOf("+") >= 0)
			{
				pText = pText.replace("+"," ");
			}
			
			return pText;
		}
	}
	
	this.Math = new function()
	{
		this.Round = function(pValue, pDecimals)
		{
			var thisDigits = Math.pow(10, pDecimals);
			var Value = pValue * thisDigits;
	 
			Value = Math.round(Value);
			Value = Value / thisDigits;

			return Value;
		}
		
		this.FormatCurrency = function(pNum, pSymbol) 
		{
			pNum = pNum.toString().replace(/\$|\,/g,'');
			if(isNaN(pNum))
			{
				pNum = "0";
			}
				
			var Sign = (pNum == (pNum = Math.abs(pNum)));
			pNum = Math.floor(pNum * 100 + 0.50000000001);
			
			var Cents = pNum % 100;
			pNum = Math.floor(pNum / 100).toString();

			if(Cents < 10)
			{
				Cents = "0" + Cents;
			}
				
			for (var i = 0; i < Math.floor((pNum.length - (1 + i)) / 3); i++)
			{
				pNum = pNum.substring(0,pNum.length - (4*i+3)) + ',' + pNum.substring(pNum.length-(4*i+3));
			}
			
			return (((Sign) ? '' : '-') + (pSymbol ? pSymbol : '$') + pNum + '.' + Cents);
		}
	}
	
	this.Menus = new function()
	{
		this.Click = function(pCell)
		{
			if (pCell != null)
			{
				if (pCell.className.indexOf("Selected") < 0)
				{
					ResetAllMenus();
					ResetAllDivisions();
					
					if (pCell.className == "")
					{
						pCell.className = "Selected";
					}
					else
					{
						pCell.className += " Selected";
					}
					
					var divControl = document.getElementById(pCell.id.replace("tbc","div"));
					if (divControl != null)
					{
						divControl.className = divControl.className.replace("Hidden", "View");
					}
				}
			}
			
			function ResetAllMenus()
			{
				var AllCells = pCell.parentNode.getElementsByTagName("TD");
				
				if (AllCells != null)
				{
					for (var iCell = 0; iCell < AllCells.length; iCell++)
					{
						AllCells[iCell].className = AllCells[iCell].className.replace("Selected","");
					}
				}
			}
			
			function ResetAllDivisions()
			{
				var CellUI = document.getElementById("tdInterfaces");
				
				if (CellUI != null)
				{
					var AllDivs = CellUI.getElementsByTagName("DIV");
					
					if (AllDivs != null)
					{
						for (var iDiv = 0; iDiv < AllDivs.length; iDiv++)
						{
							AllDivs[iDiv].className = AllDivs[iDiv].className.replace("View","Hidden");
						}
					}
				}
			}
		}
		
		this.Hover = function(pCell)
		{
			if (pCell != null)
			{
				pCell.className += "Hover";
			}
		}
		
		this.HoverOut = function(pCell)
		{
			if (pCell != null)
			{
				pCell.className = pCell.className.replace("Hover","");
			}
		}
		
	}
	
	this.Window = new function()
	{
		this.GetMousePosition = function(pEvent)
		{
			var objNetscape = document.getElementById&&!document.all;
			var X = objNetscape ? pEvent.clientX : event.clientX;
			var Y = objNetscape ? pEvent.clientY : event.clientY;

			return {MouseX:X, MouseY:Y};
		}

		this.GetScrollPosition = function()
		{	
			var ScrollTop = 0;
			var ScrollLeft = 0;
			
			if (typeof window.pageYOffset != "undefined") 
			{
				ScrollLeft = window.pageXOffset;
				ScrollTop = window.pageYOffset;
			}
			else if (typeof document.documentElement.scrollTop != "undefined") 
			{
				if (document.documentElement.scrollLeft > 0) ScrollLeft = document.documentElement.scrollLeft;
				if (document.documentElement.scrollTop > 0) ScrollTop = document.documentElement.scrollTop;
			}
			else if (typeof document.body.scrollLeft != "undefined") 
			{
				ScrollLeft = document.body.scrollLeft;
				ScrollTop = document.body.scrollTop;
			}
				
			return {Left:ScrollLeft, Top:ScrollTop};
		}
		
		this.WindowSize = function()
		{
			var w = 0;
			var h = 0;
			
			if(!window.innerWidth)
			{
				if(!(document.documentElement.clientWidth == 0))
				{
					w = document.documentElement.clientWidth;
					h = document.documentElement.clientHeight;
				}
				else
				{
					w = document.body.clientWidth;
					h = document.body.clientHeight;
				}
			}
			else
			{
				w = window.innerWidth;
				h = window.innerHeight;
			}
			
			return {width:w,height:h};
		}
	}

	this.Xml = new function()
	{
		this.Parse = function(pXml)
		{
			var xmlDoc = null;

			try
			{
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = "false";
				xmlDoc.loadXML(pXml);
			}
			catch(e)
			{
				try
				{
					parser = new DOMParser();
					xmlDoc = parser.parseFromString(pXml, "text/xml");
				}
				catch(e)
				{
					alert(e.message);
					return;
				}
			}

			return xmlDoc;
		}
		
		this.GetNodeValue = function(pNode)
		{
			var thisValue = pNode.textContent;
			
			if (IE)
			{
				thisValue = pNode.text;
			}
			
			return thisValue;
		}
	}
	
	this.AddJavaTimer = function(pUrl)
	{
		var Inclusion = "t=";
		var Base = pUrl.replace("&t=","ANDT=").replace("?t=","ANDT=").split("ANDT=");
		var thisDay = new Date();
		var Iteration = parseInt(
			thisDay.getFullYear().toString() + thisDay.getMonth().toString() + thisDay.getDay().toString() + thisDay.getHours().toString() + 
			thisDay.getMinutes().toString() + thisDay.getSeconds().toString() + thisDay.getMilliseconds().toString());

		var RetValue = Base[0] + (Base[0].indexOf("?") > 0 ? "&" : "?") + Inclusion + Iteration;
		
		if (Base.length > 1)
		{
			if (Base[1].indexOf("&") >= 0)
			{
				var Elements = Base[1].substring(Base[1].indexOf("&"));
				RetValue += Elements;
			}
		}

		return RetValue;
	}
	
	this.DoFormPostResult = "";
	
	this.DoFormPostComplete = function()
	{
	}
	
	this.DoFormPost = function()
	{
		var strPageUrl = "";
		var strPost = "";
		var Params = this.DoFormPost.arguments;
		var iParam = 0;
		
		while (iParam < Params.length)
		{
			if (iParam == 0)
			{
				strPageUrl = Params[iParam];
				iParam += 1;
			}
			else
			{
				strPost += (strPost != "" ? "&" : "") + Params[iParam] + "=" + Params[iParam + 1];
				iParam += 2;
			}
		}
		
		var xmlRequest = null;
		
		if (window.XMLHttpRequest)
		{
			xmlRequest = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			try
			{
				xmlRequest = new ActiveXObject("MSXML2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		}
		
		xmlRequest.onreadystatechange = function()
		{
			if (xmlRequest.readyState == 4 && xmlRequest.status == 200)
			{
				PageUI.DoFormPostResult = xmlRequest.responseText;
				PageUI.DoFormPostComplete();
			}
			else if (xmlRequest.readyState == 4)
			{
				PageUI.DoFormPostResult = "failed to retrieve posted response<br/>[state: " + xmlRequest.readyState + " status:" + xmlRequest.status + "]";
				PageUI.DoFormPostComplete();
			}
		}
		
		if (xmlRequest != null)
		{
			xmlRequest.open("POST", strPageUrl, true);
			xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlRequest.send(strPost);
		}
	}
	
	this.DoCallBack = function(strPageURL, pAsync) 
	{		
		strPageURL += (strPageURL.indexOf("?") > 0 ? "&" : "?") + "callback=true";
		
		if (EncryptedUserId != null && EncryptedUserId != "")
		{
			if (strPageURL.indexOf("?u=") < 0 && strPageURL.indexOf("&u=") < 0)
			{
				strPageURL += "&u=" + EncryptedUserId;
			}
		}
		
		strPageURL	= this.AddJavaTimer(strPageURL);
		strObject	= "Microsoft.XMLHTTP";
		strApp		= "application/x-www-form-urlencode";
	    
		var xmlRequest = null;
		
		if (window.XMLHttpRequest)
		{
			xmlRequest = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			try
			{
				xmlRequest = new ActiveXObject("MSXML2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		}
		
		if (pAsync == null) pAsync = true;
//alert(strPageURL);
		if (xmlRequest != null)
		{
			xmlRequest.open("GET", strPageURL, pAsync);	
			xmlRequest.setRequestHeader("Content-Type", strApp);
			xmlRequest.send(null);

			if (pAsync == false)
			{
				while (xmlRequest.readyState != 4)
				{
					window.status = "State=" + xmlRequest.readyState;
				}
				if (xmlRequest.status != 500)
				{
					if (xmlRequest.status == 404)
					{
						window.status = "404: " + strPageURL;
					}
					else
					{
						while (xmlRequest.status != 200)
						{
							window.status = "Status=" + xmlRequest.status;
						}
					}
					
					try
					{
						return xmlRequest.responseText;
					}
					catch (e)
					{
						return "";
					}
				}
				else
				{
					return xmlRequest.RepsonseText;
				}	
			}
		}
	}
	
	this.Navigate = function(pUrl, pUseId, pReference)
	{
		self.location.href = this.WrapUrl(pUrl, pUseId, pReference);
	}
	
	this.WrapUrl = function(pUrl, pUseId, pReference)
	{
		pUseId = (pUseId == null ? true : pUseId);
		
		if (pUseId)
		{
			var UserId = (QueryString["u"] == undefined) ? "" : "u=" + QueryString["u"];
			
			if (pUrl.indexOf("&u=") < 0 && pUrl.indexOf("?u=") < 0 && UserId != "")
			{
				pUrl += (pUrl.indexOf("?") > 0 ? "&" : "?") + UserId;
			}
		}
		
		pUrl = this.AddJavaTimer(pUrl);
		
		if (pReference)
		{
			pUrl += "#" + pReference;
		}
		
		return pUrl;
	}
	
	this.SetOpacity = function(pValue, pControlId)
	{
		var Control = document.getElementById(pControlId);
		if (Control != null)
		{
			var ControlStyle = Control.style;
			
			if (pValue > 0)
			{
				ControlStyle.opacity = (pValue / 100);
				ControlStyle.MozOpacity = (pValue / 100);
				ControlStyle.KhtmlOpacity = (pValue / 100);
				ControlStyle.filter = "alpha(opacity=" + pValue + ")";
			}
			else
			{
				ControlStyle.display = "none";
			}
		}
	}
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, "");
}
