// Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
// Cognos (R) is a trademark of Cognos ULC, (formerly 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 clearSessionEntry(sNames)
{
	var sSession = getCookie(g_cc_state);
	if (sSession != "" && sSession != "null" && sSession != null)
	{
		var newSession = sSession; 
		var aName = new Array();
		// Unpack the elements passed in to remove from the cc_state.
		// The format of sNames is the same as what is used for the cc_state cookie <name> [[separator <name>]...]) 
		aName = sNames.split(sParamDelimiter);	
		var n = aName.length;
		for (var i = 0; i < n; i++)
		{
			var sName = aName[i];
			// index in the session of the named entry.	
			var iStart = newSession.indexOf(sName);
			if( iStart != -1)
			{
				var frontEnd = null;
				var backEnd = null;
				// index in the session after the named entry.
				var iEnd = iStart + sName.length + sNameValueDelimiter.length;
				// Get the info from the session before the named entry.
				if (--iStart > 0)
		 			frontEnd = newSession.substring(0,iStart);
				if (iEnd < newSession.length)
				{
					// Get the info from the session after the named entry - including the parameter delimiter.
					backEnd = newSession.substring(iEnd);
					iEnd = backEnd.indexOf(sParamDelimiter);
					if(iEnd != -1)
						backEnd = backEnd.substring(++iEnd);
					else
						backEnd = null;
				}
				// Put the info together again - without the named entry
				if(backEnd!=null && frontEnd != null)
				{
					newSession = frontEnd + sParamDelimiter + backEnd;
				} else
				{
					if (backEnd == null)
					{
						newSession = frontEnd;
					}
					else
					{
						if (frontEnd == null)
						{
							newSession = backEnd;
						}
					}
				}
			}
		}
		setCookie(g_cc_state, newSession, 0, getPath());
	}

}
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);
}
