// String prototypes
String.prototype.has = function (pStr){ return (this.indexOf(pStr) != -1); }
String.prototype.hasArg = function (pArg)
{
	var a = pArg, rv = false;
	if (typeof(a) == "string")
		rv = this.has(a);
	else
	{
		//It's an array (of strings)
		var aL = a.length;
		for (var j=0; j<aL && !rv; j++)
			rv = this.has(a[j]);
	}
	return rv;
}
String.prototype.is = function (s)
{ 
	return (this == s); 
}
String.prototype.hasAny = function ()
{
	var a = arguments, l = a.length, rv = false;
	for (var i=0; i<l && !rv; i++)
		rv = this.hasArg(a[i]);
	return rv;
}
String.prototype.hasAll = function ()
{
	var a = arguments, l = a.length;
	for (var i=0; i<l; i++)
	{
		if (!this.hasArg(a[i]))
			return false;
	}
	return true;
}
String.prototype.replaceToken = function(pStr,pTkn,pReplace)
{
	var rv=pStr;
	while(rv.has(pTkn))
		rv=rv.replace(pTkn,pReplace);
        
	return rv;
}

String.prototype.replaceTokens=function()
{
	var rv=this,re,a=arguments,l=a.length;
	for(var i=0;i<l;i++)       
		rv=this.replaceToken(rv,"<#"+(i+1)+"#>",a[i]);
	
	return rv;
}
String.prototype.isEmpty = function()
{
    return (this == null || this == "" || (this.length && this.length == 0));
}

// Global Library.
function GlobalLib()
{
	this.win = window;
	this.doc = document;
	this.aParams = [];
	this.bQueryLoaded = false;
	this.oClient = new ClientInfo();
	this.sBaseUrl = null;
	this.bDevEnvironment = document.domain.has('localhost');
	this.oShadow = null;

    this.addOnLoadListener = function(pFn)
    {
        var w = this.win, d = this.doc, un = 'undefined', fn = pFn;
        if (typeof w.addEventListener != un)
           w.addEventListener('load', fn, false);
        else if (typeof d.addEventListener != un)
           d.addEventListener('load', fn, false);
        else if (typeof w.attachEvent != un)
           w.attachEvent('onload', fn);
        else
        {
           var oFN = window.onload;
           if (typeof w.onload != 'function')
             w.onload = fn;
           else
           {
             window.onload = function()
             {
               oFN();
               fn();
             };
           }
        } 
    }
    
    this.getParentNode = function(pObj)
    {
        var o = pObj.parentNode ? pObj.parentNode : pObj.parentElement;
        return o;
    }

	this.getUIElem = function(pId)
	{
		var s = pId, d = this.doc, e = null,
			aFrms = document.forms, len = aFrms.length;

		for (var i=0; i<len;i++)
		{
			e = aFrms[i].elements[pId];
			if (e)
				break;
		}

		if (!e && d.getElementById)
			e = d.getElementById(s);
		
		if (!e && d.all)
			e = d.all(s);

		return e;
	}
	
	this.redirect = function(pUrl,pReplace)
	{
		var l = this.doc.location;
		if (pReplace)	
			l.replace(pUrl);
		else			
			l.href = pUrl;
	}

	this.gotoAnchor = function(pName)
	{
		var l = this.doc.location, t = l.href.split("#"), n = pName || t[1];
		if (n)
			l.href = t[0] + '#' + n;
	}

    this.insertAfter = function(pParent, pNodeAfter, pNode)
    {
        var ns = pNodeAfter.nextSibling;
        if (ns)
            pParent.insertBefore(pNode, ns)
        else
            pParent.appendChild(pNode);
    } 
    
	this.getFormElem = function(pName, pType)
	{
		var d = this.doc;
		if (!d)
			return null;
			
		var frms = d.forms, ln = frms.length, e, eLen;
		for (var i=0;i<ln;i++)
		{
			e = frms[i].elements;
			eLen = e.length;
			for (var j=0;j<eLen;j++)
			{
				if (e[j].name == pName)
				{
					if (pType)
					{
						if (e[j].type == pType)
							return e[pName];
					}
					else
						return e[j];
				}
			}
		}
	}

    this.enableFormElems = function(pForm, pEnable)
    {
        var oFrm = this.doc.forms[pForm], elems = oFrm ? oFrm.elements : null;
        if (elems && elems.length > 0)
        {
            for (var i=0,len=elems.length; i<len;i++)
                elems[i].disabled = !pEnable;
        }
    }

	this.getQueryValue = function (pKey)
	{
		with (this)
		{
			if (!bQueryLoaded)
				loadParams();

			return aParams[pKey] ? aParams[pKey] : null;
		}
	}
	
	this.loadParams = function ()
	{
		var str = this.doc.location.search;
		if (str.length == 0)
			return;

		str = decodeURI(str.substr(1));
		var ps = str.split("&"), psLen = ps.length;

		for (var i=0; i<psLen; i++)
		{
			var p = ps[i].split("=");
			
			this.aParams[p[0]] = "";
			if (p[1])
			{
				//Escape any apostrophes & replace +'s with space
				var tmp = "", c, len = p[1].length;
				for (var j=0; j<len; j++)
				{
					c = p[1].charAt(j);
					if (c.is("'"))		tmp += "\\'";
					else if (c.is("+"))	tmp += " ";
					else				tmp += c;
				}
				this.aParams[p[0]] = tmp;
			}
		}
		
		this.bQueryLoaded = true;
	}

	this.getOffsetLT = function(pElem)
	{
		var e = this.getUIElem(pElem);
		if (e)
		{
			var rv = [0,0];
			while (e)
			{
				rv[0] += e.offsetLeft;
				rv[1] += e.offsetTop;
				e = e.offsetParent;
			}
			return rv;
		}
		return null;
	}
	
	this.createElement = function(pTag)
	{
	    var oD = this.doc;
	    if (oD.createElement)
	        return oD.createElement(pTag);
	    
	    return null;
	}

	this.openWindow = function(url, name, width, height, toolbar, loc,
						status, scrollbars, resizable, menubar, left, top, customprops)
	{
		var props = "";
		if (width) props += ",width=" + width;
		if (height) props += ",height=" + height;
		if (toolbar) props += ",toolbar=" + toolbar;
		if (loc) props += ",location=" + loc;
		if (status) props += ",status=" + status;
		if (scrollbars) props += ",scrollbars=" + scrollbars;
		if (resizable) props += ",resizable=" + resizable;
		if (menubar) props += ",menubar=" + menubar;
		if (left) props += ",screenX=" + left + ",left=" + left;
		if (top) props += ",screenY=" + top + ",top=" + top;
		if (customprops) props += "," + customprops;
		if (props != "") props = props.substring(1);

		var w = window.open(url, name, props);
		if (!this.oClient.bOpera && w && !w.closed) w.focus();
		return w;
	}
	
	this.getRandomNumber = function()
	{
	    return Math.floor(Math.random()*10001);
	}

	this.showShadow = function(pDisplay)
	{
		if (!this.oShadow)
			this.oShadow = new HTMLLayer('shadow');
			
		var d = this.doc,		
			ht = Math.max(
			        Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
			        Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
			        Math.max(d.body.clientHeight, d.documentElement.clientHeight)
					);

		this.oShadow.setStyle('height', ht + 'px');
	    this.oShadow.show(pDisplay);
	}
	
	this.openDialog = function(pHtml, pWidth, pHeight)
    {
        var oDialog = new Overlay('dialog', 200, 200, true);
        oDialog.sWidth = pWidth;
        oDialog.sHeight = pHeight;
        oDialog.setContent(pHtml);  
        oDialog.display(true);
        oDialog.setPosition();
        return oDialog;
    }

	this.setCBMapping = function(pArray)
	{
		for (var i in pArray)
		{
			var oCB = this.getUIElem(i);
			if (oCB)
			{
				oCB.sHidden = this.getUIElem(pArray[i]);
				oCB.onclick = function()
				{
					this.sHidden.value = this.checked ? 'Y' : 'N';
				}
			}
		}
	};
    
    this.isInArray = function(pArr,pVal,pInd)
	{
		var rv = false, l = pArr.length, av, ind = (typeof(pInd) != "undefined");
		for (var i=0; i<l && !rv; i++)
			rv = ((ind ? pArr[i][pInd] : pArr[i]) == pVal);
		return rv;
	};
    
    this.backPageRedirect = function(pAction)
    {
        var oFrm = document.forms['backToFrm'];
        if (oFrm)
        {
            oFrm.action = pAction;
            oFrm.submit();
        }
        else
            return false;
    };
    
    this.setTextFieldBlur = function(pObj)
    {
    	for (var i in pObj)
    	{
    		var e = new HTML(i);
    		e.sDefValue = pObj[i];
            if (e.elem)
            {
    		    e.elem.parent = e;
    		    e.elem.onfocus = function(){if (this.value == this.parent.sDefValue) this.value = "";};
    		    e.elem.onblur = function(){if (this.value == "") this.value = this.parent.sDefValue;};
            }
    	}
    };
}

// Browser detection.
function ClientInfo()
{
	this.bFirefox = this.bWebTV = this.bOpera = this.bNav = this.bIE = this.bSafari =
	this.bWin = this.bMac = this.bWinXp = this.bXpSp2 = this.bAOL = 
	this.bVista = this.bLinux = false;

	this.iVer = -1;

	var nv = navigator, agt = nv.userAgent.toLowerCase(), i = 0, ver;

	with(this)
	{
		if (agt.has("webtv"))
		{
			bWebTV = true;
			i = agt.indexOf("webtv/") + 6;
		}
		else if (agt.has("firefox"))
		{
			bFirefox = true;
			i = agt.lastIndexOf("firefox") + 8;
		}
		else if (agt.has("safari"))
		{
			bSafari = true;
			i = agt.lastIndexOf("safari") + 7;
		}
		else if(typeof(window.opera)!="undefined")
		{
			bOpera = true;
			i = agt.lastIndexOf("opera") + 6;
		}
		else if (nv.appName.is("Netscape"))
		{
			bNav = true;
			i = agt.lastIndexOf("/") + 1;
		}
		else if (agt.has("msie"))
		{
			bIE = true;
			i = agt.indexOf("msie") + 4;
			if (agt.has('aol') || agt.has('america online'))
				bAOL = true;
		}

		ver = bOpera?window.opera.version():agt.substring(i);
		iVer = parseInt(ver);

		//OS Detection.
		bWin = agt.has("win");
		bWinXp = (bWin && agt.has("windows nt 5.1"));
		bVista = (bWin && agt.has("windows nt 6.0"));
		bXpSp2 = (bWinXp && agt.has("sv1"));
		bMac = agt.has("mac");
	}
}
var _GL = new GlobalLib();

/*** UI Control ****/
function HTML(pName)
{
    this.name = pName;
	this.elem = _GL.getUIElem(pName);

    this.getValue = function()
    {
        var e = this.elem;
        if (e)
            return e.value;
    }
    
    this.setValue = function(pVal)
    {
        var e = this.elem;
        if (e)
            e.value = pVal;
    }
    
	this.show = function(pShow, pNoDisplayChange)
	{
		var e = this.elem;
		if (e)
		{
			e.style.visibility = pShow ? 'visible' : 'hidden';
			if (!pNoDisplayChange)
				e.style.display = pShow ? '' : 'none';
		}
	}

	this.enable = function(pEnable)
	{
		var e = this.elem;
		if (e)
			e.disabled = !pEnable;
	}

	this.setStyle = function(pStyle, pVal)
	{
		var e = this.elem;
		if (e && !e.length && pVal != null)
			eval('e.style.' + pStyle + '="' + pVal + '"');
	}

	this.setClass = function(pClass)
	{
		var e = this.elem;
		if (e)
			e.className = pClass;
	}

	this.getClass = function()
	{
		var e = this.elem;
		if (e)
			return e.className;
		else
			return "";
	}
}

function HTMLLayer(pName)
{
	this.base = HTML;
	this.base(pName);

	this.getValue = function()
	{
		var e = this.elem;
		if (e)
			return e.innerHTML;
		else
			return "";
	};

	this.setValue = function(pVal)
	{
		var e = this.elem;
		if (e)
			e.innerHTML = pVal;
	};
}

function HTMLSelect(pName)
{
	this.base = HTML;
	this.base(pName);
	
	this.clearOptions = function()
	{
		var e = this.elem;
		if (e)
		{
			var opts = e.options;
			while (opts.length > 0)
				opts[opts.length - 1] = null;
		}
	};
	
	this.createOption = function(pName, pText)
	{
		if (this.elem)
		{
			var nOpt = new Option(pText,pName,false,false), opts, lo;
			opts = this.elem.options;
			opts[opts.length] = nOpt;
			idx = opts.length-1;
			return idx;
		}
	};
	
	this.getSelectedValue = function(pTextOnly)
	{
		var idx = this.elem.selectedIndex;
		if (idx > -1)
		{
			opt = this.elem.options[idx];
			if (opt)
				return pTextOnly ? opt.text : opt.value;
		}
	};
	
	this.selectByValue = function(pVal)
	{
		if (this.elem)
		{
			var e = this.elem, opts = e.options, len = opts.length;
			for (var i=0;i<len;i++)
			{
				if (opts[i].value == pVal)
				{
					e.selectedIndex = i;
					break;
				}	
			}
		}
	};
}

function Overlay(pName, pWidth, pHeight, pShadow, pContentDiv,pObj)
{
	this.base = HTMLLayer;
	this.base(pName);

	this.sLeft = this.sTop = -1;
	this.sWidth = pWidth;
	this.sHeight = pHeight;
	this.bShadow = pShadow;
	this.oContentDiv = pContentDiv ? new HTMLLayer(pContentDiv) : null;
	this.sMBHeader = '<div id="ol-header"><#1#><a href="#" id="ol-close" onclick="'+pObj+'.display(false);return false;">close</a></div>';
	this.sMBContent = '<div id="ol-content"><#1#></div>';

	this.display = function(pDisplay)
	{
	    with(this)
	    {
	        var b = pDisplay;
	        
	        if (bShadow)
				_GL.showShadow(b);
	        
	        show(b);
	    }
	           
	}
	
	this.setContent = function(pValue, pHdr)
	{
	    with(this)
	    {
			var s = this.sMBHeader.replaceTokens(pHdr) + this.sMBContent.replaceTokens(pValue);

	        if (oContentDiv)
		        oContentDiv.setValue(s);
		    else
		        setValue(pValue);
		        
		    if (sWidth)
		        setStyle('width',  sWidth + 'px');
		    
		    if (sHeight)
		        setStyle('height', sHeight + 'px');
			
			setPosition();
		}
	}

	this.setPosition = function(pLeft, pTop)
	{
		var l = t = 0;
		if (!pLeft || !pTop)
		{
			var a = this.getCenterPos();
			l = a[0];
			t = a[1]-50;
		}
		else
		{
			l = pLeft;
			t = pTop;
		}
		
		this.setStyle('left', l + 'px');
		this.setStyle('top', parseInt(t) + 'px');
	}

	this.getCenterPos = function()
	{
		var bd = document.body, 
			h = w = l = t = 0;
		
		
		if (document.documentElement)
		{
		    h = document.documentElement.clientHeight;
		    w = document.documentElement.clientWidth;
		}
		else
		{
		    h = window.innerHeight;
			w = window.innerWidth;   
		}
	    
	    var eh = this.sHeight || this.elem.offsetHeight,
	        ew = this.sWidth || this.elem.offsetWidth;
	    
		t = bd.scrollTop + (h - eh)/2;
		if (document.documentElement)
			t += document.documentElement.scrollTop;
		
		l = w/2 - ew/2;
		
		return [l, t];
	}
	
	this.resize = function()
	{
	    var fn = this.sIfName,
	        f = window.frames ? window.frames[fn] : _GL.getUIElem(fn);
	  
	    if (f)
	    {
	        var d=f.document?f.document:f.contentDocument,db=d.body,
	        h=db.offsetHeight ? db.offsetHeight : d.height;

	        f.frameElement.style.height = h +'px';
	        f.frameElement.style.width = '100%';
	    }
	}
}

function OverlayPopup (pName, pWidth, pHeight, pShadow, pUrl, pContentDiv, pObj)
{
    this.base = Overlay;
	this.base(pName, pWidth, pHeight, pShadow,pContentDiv, pObj);
	this.sIfName = this.name + _GL.getRandomNumber();
	
	this.sUrl = pUrl;
	
	this.displayBase = this.display; 
	
	this.display = function(pDisplay, pHdr)
	{
	    with(this)
	    {
	        var b = typeof pDisplay != 'undefined' ? pDisplay : true;
	        setContent(b ? getIframeUI() :  baseUrl + 'images/s.gif', pHdr);
	        displayBase(b);
	        
	        if (b)
	            setPosition();
	    }   
	}
	
	this.close = function()
	{
	    this.display(false);
	}
	
	this.getIframeUI = function()
	{
	    with(this)
	    {
	        var s = '';
	        s+='<iframe frameborder="no" border="0" marginwidth="0" marginheight="0"';
	        s+= ' scrolling="no"';
	        s+=' id="'+ sIfName + '" name="'+ sIfName +'"';
	        s+=' src="'+ sUrl + '"';
	        s+=' width="100%" height="'+sHeight+'"';
            s+='></iframe>';
            
            return s;
        }	
	}
}


/**** AJAX Functions ****/
var oXmlHttp = null, bReqInProg = false;
function Ajax (pName, pUrl, pIsGet, pListners)
{
	this.name = pName;
	this.sUrl = pUrl;
	this.sMethod = pIsGet ? "GET" : "POST";
	this.sPostData = null;
	this.bShowProgress = false;
	this.sShowProgressId = null;
	this.sProgressHtml = '<img src="images/loader_small.gif" border="0" />';
	this.bAsync = true;
	this.iResponseType = 3;
	this.bResponseReady = false;
	this.aListners = pListners;

	this.RESPONSE_JS = 0;
	this.RESPONSE_HTML = 1;
	this.RESPONSE_TEXT = 2;
	this.RESPONSE_JSON = 3;
	
	this.setListner = function(pCallback)
	{
		this.aListners[this.aListners.length] = pCallback;
	}

    this.showProgress = function(pShow)
    {
        var e = _GL.getUIElem(this.sShowProgressId);
        if (e)
            e.innerHTML = pShow ? this.sProgressHtml : '';
    }
    
	this.send = function()
	{
		if (typeof(window.XMLHttpRequest) != "undefined")
			oXmlHttp = new XMLHttpRequest();
		else if (typeof(ActiveXObject) != "undefined")
		{
			var lib = "Microsoft.XMLHTTP";
			oXmlHttp = new ActiveXObject(lib);
		}
		else
		{
			alert("XML HTTP not supported");
			return;
		}

        if (this.bShowProgress)
            this.showProgress(true);
            
		oXmlHttp.open(this.sMethod, this.sUrl, this.bAsync);
		if (this.sMethod == 'POST')
			oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		var req = this;
		oXmlHttp.onreadystatechange = function ()
		{
			req.processResquest(oXmlHttp);
		}

		oXmlHttp.send(this.sPostData);
	}

	this.processResquest = function(pHttp)
	{
		if (pHttp.readyState == 4) 
		{
			//status 200 = OK
			if (this.bShowProgress)
                this.showProgress(false);
           
            this.bShowProgress = false;
            this.sShowProgressId = null;

			var st = pHttp.status;
			if (st == 200) 
			{
				var oResp = new AjaxResponse(this);
				oResp.sResponseText = "" + pHttp.responseText;
				oResp.oResponseXml = pHttp.responseXML;
				oResp.sHeaderText = "" + pHttp.getAllResponseHeaders();
				oResp.process();
				oResp.notify();
			}
			else
			{
				var msg = "Error: " + pHttp.statusText;
				
				if (st == 404)
					msg = "Invalid URL: '" + this.sUrl + "' [System Error: " +  pHttp.statusText + "]";
			
				alert(msg);
			}
			pHttp = null;
			oXmlHTTP = null;
			bReqInprogress = false;
		}
	}
}

function AjaxResponse (pParent)
{
	this.parent = pParent;
	this.sResponseText = null;
	this.oResponseXml = null;
	this.sHeaderText = null;
	this.oData = null;
	this.oJSON = null

	this.process = function()
	{
		var s = this.sResponseText, p = this.parent;
		if (s.length > 0)
		{
		    if (p.iResponseType == p.RESPONSE_JSON)
			{
				try
				{
					this.oJSON = eval('(' + s + ')');
				}
				catch(e)
				{ //Exception for malformed response.
				}
			}
			
			if (p.iResponseType == p.RESPONSE_JS)
				eval(s);
			
			if (s.hasAny("<html", "<body", "<script"))
				p.iResponseType = p.RESPONSE_HTML;
				
			p.bResponseReady = true;
		}	
	}

	this.notify = function()
	{
		var p = this.parent, aL = p.aListners, l = aL.length;
		for (var i=0; i<l; i++)
		    eval(aL[i] + '(this)');
	}
}

//-->
