escape = encodeURIComponent;
unescape = decodeURIComponent;

var kMainFrameName = "worksite_mainframe"

var reAmp = new RegExp();
reAmp.compile("&", "g");

var reApos = new RegExp();
reApos.compile('\'', 'g');

var reQuot = new RegExp();
reQuot.compile('\"', 'g');

var reLt = new RegExp();
reLt.compile('<', 'g');

var reGt = new RegExp();
reGt.compile('>', 'g');

var reSlash = new RegExp();
reSlash.compile('\\\\', 'g');

var reSpace = new RegExp();
reSpace.compile('\\s', 'g');

function javaString(str) {
	// This add's backslashes to any single || double quotes
	if ( str ) 
	{
		str = str.replace(reSpace, " ")
		str = str.replace(reSlash, "\\\\")
		str = str.replace(reApos, "\\\'")
		str = str.replace(reQuot, "\\\"")

		return str
	} 
	else 
	{
		return ''
	}
}
	
function htmlString(str) 
{
	// This HTML escapes the string
	if ( str ) 
	{
		str = str.replace(reAmp, "&amp;")
		str = str.replace(reApos, "&#39;")
		str = str.replace(reQuot, "&quot;")
		str = str.replace(reLt, "&lt;")
		str = str.replace(reGt, "&gt;")

		return str
	} 
	else 
	{
		return ''
	}
}

function htmlDeString(str) {
	// This HTML unescapes the string

	if ( str ) 
	{
		str = str.replace(/&#39;/gi, "\'")
		str = str.replace(/&quot;/gi, "\"")
		str = str.replace(/&lt;/gi, "<")
		str = str.replace(/&gt;/gi, ">")
		str = str.replace(/&amp;/gi, "&")
		return str
	} 
	else 
	{
		return ''
	}
}

function rTrim(str) 
{ 
	if ( str )
	{
		return str.replace(/\s+$/, "");
	}
	else
	{
		return '';
	}
} 

function lTrim(str) 
{
	if ( str )
	{
		return str.replace(/^\s+/, "") 
	}
	else
	{
		return '';
	}
} 

function trim(str) 
{
	return rTrim(lTrim(str));
} 

function getURLParam(url, param) 
{
	// This extracts a named param
	
	var index = url.indexOf(param + "=")
	if (index >= 0) 
	{
		var start
		if (index == 0) 
		{
			start = param.length + 1
		} 
		else 
		{
			var priorChar = url.charAt(index-1)
			if (priorChar == '&' || priorChar == '?') 
			{
				start = index + param.length + 1
			}
		}
		
		if (start) 
		{
			var end = url.indexOf("&", start)
			if (end < 0) 
			{
				end = url.length
			}
			return unescape(url.slice(start, end));
		}
	}
	
	return '';
}

function setURLParam(url, param, value)
{
	// This sets a named param
	
	var index = url.indexOf("?" + param + "=")
	if (index < 0) 
	{
		index = url.indexOf("&" + param + "=")
	}

	if (index >= 0) 
	{
		var start = index + 1
		var end = url.indexOf("&", start)
		if (end < 0) 
		{
			if ( value )
			{
				url = url.slice(0, start) + param + "=" + value
			}
			else
			{
				url = url.slice(0, start - 1)
			}
		}
		else
		{
			if ( value )
			{
				url = url.slice(0, start) + param + "=" + value + url.slice(end)
			}
			else
			{
				url = url.slice(0, start) + url.slice(end - 1)
			}
		}
	} 
	else if ( value )
	{
		url = appendURLParam(url, param, value)
	}
	
	return url
}

function appendURLParam(url, param, value) {
	if (url.indexOf("?") < 0) {
		url = url + "?"
		} else {
		url =  url + "&"
	}
	url += param + "=" + value
	
	return url
}
	
function getParam(name) {
	var query = window.location.search
	var arg = name + "="
	var alen = arg.length
	var qlen = query.length
	var i = query.indexOf("?") + 1
	while (i < qlen) {
		var j = i + alen
		if (query.substring(i, j) == arg) {
			var cEnd = query.indexOf("&",j)
			if (cEnd == -1) {
				cEnd = query.length
			}
			return unescape(query.substring(j, cEnd))
		} else {
			i = query.indexOf("&", i) + 1
			if (i == 0) break;
		}
	}
	return ""
}


function getCookie(name, item) {
	var arg = name.toUpperCase() + "="
	var alen = arg.length
	var clen = document.cookie.length
	var i = 0
	while (i < clen) {
		var j = i + alen
		if (document.cookie.substring(i, j).toUpperCase() == arg) {
			var cEnd = document.cookie.indexOf(";",j)
			if (cEnd == -1) {
				cEnd = document.cookie.length
			}
			var cookie = document.cookie.substring(j, cEnd)
			if (item != null) { // Find item
				arg = item.toUpperCase() + "="
				alen = arg.length
				clen = cookie.length
				i = 0
				while (i < clen) {
					j = i + alen
					if (cookie.substring(i, j).toUpperCase() == arg) {
						cEnd = cookie.indexOf("&",j)
						if (cEnd == -1) {
							cEnd = cookie.length
						}
						cookie = cookie.substring(j, cEnd)
						return unescape(cookie)
					} else {
						i = cookie.indexOf("&", i) + 1
						if (i == 0) return ""
					}
				}	
			}
			return unescape(cookie)
		} else {
			i = document.cookie.indexOf(" ", i) + 1
			if (i == 0) break;
		}
	}
	return ""
}

function setCookie(name, value, expireDays,path) {
	var pair = name + "=" + value
	if (expireDays) {
		var expires = new Date()
		var expTime = expires.getTime() + (expireDays * 86400000)
		expires.setTime(expTime)
		pair += "; expires=" + expires.toGMTString()
	}
	if (path) {
		pair += "; path=" + path
	} else {
		pair += "; path=" + "/"
	}
	document.cookie = pair
}

function deleteCookie(cookieName) {
		var now = new Date()
		now.setTime (now.getTime() - 1)
		var cookieValue = getCookie (cookieName)
		document.cookie = cookieName + "=" + cookieValue + "; expires=" + now.toGMTString()
}

function restoreOffset() {
	var name = window.name + "Offset"
	var offset = getCookie(name)
	if ( offset && window.scrollTo ) {
		var offsets = offset.split(",")
		window.scrollTo(offsets[0], offsets[1])
		setCookie(name, "", 0)
	}	
}

function validateTransferControl()
{
    if(page.transferControl != TransferControl.FileControl )
    {
        try
        {
            var control = getTransferCtrl();
            
            var isValid 
            if(typeof(control) == "undefined")
            {
                control = window.opener.document.WebTransferCtrl;
            }   
            
            if( page.transferControl ==  TransferControl.Applet)
            {
                isValid = typeof(control.getFormName()) != "undefined"
            }
            else
            {
                isValid = typeof(control.FormName) != "undefined"
            }

            if(!isValid)
	        {	
	            page.transferControl = TransferControl.FileControl	    	
	        }
	    }
	    catch(e)
	    {
	        page.transferControl = TransferControl.FileControl	    	
	    }
	}
}

function appendRedirectToURL(url) {
	//This function will append the redirect from the page object (complete with tabid).
	var isMainWindow = isHomeLink(url)
	url = virtualPath() + setURLParam(url, "redirect", escape(window.page.getRedirect()))
	if(isMainWindow) {
		window.location = url
	} else	{
		openDialogWindow(url)
	}
}

//Check the length of "str" with "size".If greater, then trim "str" to "size".
function checkLength(str,size) {
	if(str.length > size)	
		str = str.substring(0,size) + "..."	
	return str
}

function mailtoEncode(input) {
	return escape(input).replace(/%/g,  "%25")
}

function mailtoURL(nrtid, title, script) { 
	/*	NS, 8/6/02:
		This function now supports multiple links sent simultaneously.
		nrtid parameter can contain multiple id's separated by kMultiOpSeparator
		NOTE:  nrtid will come in escaped; must unescape
	*/
	var body = ""
	var subject = ""
	var link = ""
	
	nrtid = unescape(nrtid)
	//Trim off last kMultiOpSeparator so array length represents the actual # of nrtid's passed
	if (nrtid.charAt(nrtid.length-1) == kMultiOpSeparator)
	{
		nrtid = nrtid.slice(0, nrtid.length-1)
	}
	
	var nrtidAry = nrtid.split(kMultiOpSeparator)
	
	if (nrtidAry.length == 1) {
		if (!title) {
			title = getLocStrByLID(1115, "WorkSite Link")
		}
	} else {
		title = getLocStrByLID(1114, "WorkSite Links")
	}

	var tmpNrtId = new String("");
	for (var j = 0; j < nrtidAry.length; j++)
	{
		tmpNrtId = tmpNrtId + nrtidAry[j]+";"
	}
	var MailtoObj = new ActiveXObject ("iManFile.MailTo.8.1")
	if (MailtoObj != null )
	{
		MailtoObj.Version = getURLParam(script, "latest") != "undefined" && getURLParam(script, "latest") != "" ? getURLParam(script, "latest") : "0"
		MailtoObj.SendNrlLink (tmpNrtId,title,script)
	}
}

function centralize(Sr, Pl, Pw, Dw) {
	return Math.max(0, Math.min(Sr - Dw, (Math.max(0, Pl) + Math.min(Sr, Pl + Pw) - Dw) / 2))
}

// BrowserInfo object
function Browser() {
/*
	Properties:
		isIE:	is Internet Explorer;
		isNS6:	is Netscape 6 and above;
		isNS4:  is Netscape 4.x;
		isNav:	is Netscape browser;
		isMac:	is Macintosh machine;
		isWin:  is Windows machine;
		isIE5:  is Internet Explorer 5.0*;
		isIE55: is Internet Explorer 5.5;
*/	
	 this.isIE	= (document.all)? true : false
	 this.isNS6 = (document.implementation && document.implementation.createDocument) ? true : false
	 this.isNS4 = (navigator.userAgent.toUpperCase().indexOf("MOZILLA/4.79") > -1) || (navigator.userAgent.toUpperCase().indexOf("MOZILLA/4.8") > -1)
	 this.isNav = this.isNS6 || this.isNS4
	 this.isMac = (navigator.userAgent.indexOf("Macintosh") > -1)
	 this.isWin = !this.isMac	 
	 this.isIE5 = (navigator.userAgent.indexOf("MSIE 5.0") > -1)
	 this.isIE55 = (navigator.userAgent.indexOf("MSIE 5.5") > -1)
}

window.disposeAdvise = window_disposeAdvise

function window_disposeAdvise(funcObj) {
	if ( window.disposeListeners ) {
		window.disposeListeners[window.disposeListeners.length] = funcObj
	} else {
		window.disposeListeners = new Array(funcObj)
	}
}

window.onunload = window_onunload

function window_onunload() {
	if ( window.disposeListeners ) {
		for(var i = 0; i < 	window.disposeListeners.length; ++i ) {
			var listener = window.disposeListeners[i]
			if ( typeof(listener) == "function" ) {
				listener()
			} else if ( typeof(listener) == "string" ) {
				eval(listener)
			}
		}
	}
}

window.onloadAdvise = window_onloadAdvise
function window_onloadAdvise(funcObj) {
	if ( window.onloadListeners ) {
		window.onloadListeners[window.onloadListeners.length] = funcObj
	} else {
		window.onloadListeners = new Array(funcObj)
	}
}

window.onload = window_onload
function window_onload() {
	if ( window.onloadListeners ) {
		for(var i = 0; i < 	window.onloadListeners.length; ++i ) {
			var listener = window.onloadListeners[i]
			if ( typeof(listener) == "function" ) {
				listener()
			} else if ( typeof(listener) == "string" ) {
				eval(listener)
			}
		}
	}
}
g_browser = new Browser()

function virtualRoot() 
{
	if ( ! virtualRootCache ) 
	{
		virtualRootCache = prependIPSURL(getCookie("virtualRoot"))
	}
	return virtualRootCache
}
var virtualRootCache = ""

function virtualPath() 
{
	if ( ! virtualPathCache ) 
	{
		virtualPathCache = prependIPSURL(getCookie("virtualPath"))
	}
	return virtualPathCache
}
var virtualPathCache = ""

function getThemeRoot()
{
	if ( ! themeRootCache ) 
	{
		themeRootCache = virtualPath() + "/themes/" + getCookie("themeRoot");
	}
	return themeRootCache
}
var themeRootCache = ""

//This function is only useful for iPlanet gateway.
function prependIPSURL(str) 
{
	var add_ipsURL_to_iman = str;
	return add_ipsURL_to_iman;
}

function worksiteDialogCallBack(returnObject)
{
	if ( window.document.body )
	{
		window.document.body.className += ' worksite-portal-reload';
	}
	window.location = returnObject && returnObject.url ? returnObject.url : page.getRedirect();
}

function closeDialogWindow(returnObject)
{		
	if ( window.opener )
	{		
		var windowClosed = false;
		if(typeof(returnObject) != "undefined")
		{
		    if(returnObject.fullrefresh && returnObject.fullrefresh == true)
	        {
    	        window.opener.name == kMainFrameName ? window.opener.parent.location.reload(true):window.opener.location.reload(true);		        
    	        windowClosed = true;
		        window.close();
	        }		
		    else if(returnObject.fromError && returnObject.fromError == true )	
		    // check for flag from ReportError in the return object
		    {
			    windowClosed = true;
			    window.close();
		    }		    
		 }
		 
		 if(!windowClosed && typeof(window.opener.worksiteDialogCallBack) != "undefined")
		 {
		    window.opener.worksiteDialogCallBack(returnObject);
			window.close();
		 }
	}
	else if ( typeof(window.worksiteDialogCallBack) != "undefined")
	{
		window.worksiteDialogCallBack(returnObject);
		return true;
	}
}

function objArrayToMultiOpStr(objArray)
{
	//This function will create a multi-op string from an array of objects that can be sent to the server for multi-op processing.
	var nrtidStr = '';
	
	for (i=0; i < objArray.length; i++)
	{
		nrtidStr += objArray[i].nrtid + kMultiOpSeparator;
	}
	
	return nrtidStr ? nrtidStr.substring(0,(nrtidStr.length-1)) : "";
}

