/**********************************************************************************************

		Copyright (C) 2004 Cognos Incorporated. All Rights Reserved.
		Cognos (R) is a trademark of Cognos Incorporated.
		
 **********************************************************************************************/
 // Define the limit for max No. of characters that we want to store in a browser cookie. Cognos customers can "potentially" modify this in this file.
 // Use ~ 80% of actual browser limit of 4094 bytes.  This limit should be checked when setting cookies that could potentially exceed the limit.
var g_iMaxAllowableCookieSize = 3275; 
var g_cc_state = "cc_state";
 
function setCookie(name, value, expires, path, domain, secure)
{
	// We could be very strict and prevent the cookie size from going over the limit right here but that would penalize small cookies too.
	//if ( (document.cookie.length + value.length) > g_iMaxAllowableCookieSize )
		//return;
	
	document.cookie = name + "=" + value +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function delCookie(name, path, domain)
{
	if (path == null)
		path = getPath();
	if (getCookie(name))
		document.cookie = name + "=" + 
		"; expires=0" +
		((path) ? "; path=" + path : "") + 
		((domain) ? "; domain=" + domain : "");
}

function getCookie(name)
{
	var arg = name + "=";
	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) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break; 
	}
	return null;
}

function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return document.cookie.substring(offset, endstr);
}

function setExpireDate(nOffset)
{
	if (!nOffset)
		nOffset = 0;	
	var expdate = new Date ();
	expdate.setTime(expdate.getTime() + nOffset);
	return expdate;
}

function getPath()
{
	if (sCookiePath == "")
		return location.pathname.substring(0, location.pathname.lastIndexOf("/"));
	else
		return sCookiePath;
}

function setStateValue(sName, sValue)
{	
	var sSession = getCookie(g_cc_state);
	
	//-- If we have no session or it is empty or the name is invalid then return.
	if (!(sName == "" || sName == null))
	{
		var sNewSession = "";	
		if (sSession == "" || sSession == "null" || sSession == null)
		{
			sNewSession = sName + sNameValueDelimiter + sValue;
		}
		else		
		{
			var aSession = new Array();
			var bParameterExists = false;
			// unpack the session
			aSession = sSession.split(sParamDelimiter);
			
			var n = aSession.length;
			// update the specified parameter
			for (var i = 0; i < n; i++)
			{	
				if (aSession[i].indexOf(sName) == 0)
				{
					aSession[i] = sName + sNameValueDelimiter + sValue;
					bParameterExists = true;
					break;
				}
			}			
			// if a value was updated in the existing session then repack the session, otherwise just append the new param at the end.
			if (!bParameterExists)
				sNewSession = sSession + sParamDelimiter + sName + sNameValueDelimiter + sValue;
			else // pack the updated session
				sNewSession = aSession.join(sParamDelimiter);
		}		
		// Save the new session ...
		setCookie(g_cc_state, sNewSession, 0, getPath());
	}
	return;
}

function getSessionValue(sName)
{
	// Get the current session 
	var sSession = getCookie(g_cc_state);
	var sRetValue = null;
	// If we have no session or it is empty then return null.
	if (sSession != "" && sSession != "null" && sSession != null)
	{
		var aSession = new Array();
		aSession = sSession.split(sParamDelimiter);	
		
		var n = aSession.length;
		// unpack the session and update the specified parameter 
		for (var i = 0; i < n; i++)
		{	
			if (aSession[i].indexOf(sName) == 0)
			{
				sRetValue = aSession[i].substring(aSession[i].indexOf(sNameValueDelimiter) + sNameValueDelimiter.length, aSession[i].length);
				break;
			}
		}
	}
	return sRetValue;
}

function resetStateParam(sName)
{
	// If we have no session or it is empty then return null.
	var sCurrentValue = getSessionValue(sName);
	if (sCurrentValue)
		setStateValue(sName, "");
}

function updateSession()
{
	// Updates the browser session to control caching.
	var currentValue = parseInt(getSessionValue("us"));
	var newValue = isNaN(currentValue) ? 0 : (currentValue + 1);
	setStateValue("us", newValue);
}


