/*
	Contains functions for handling the Navigation Context of a page. 
	The context consists of data associated with the route by which 
	a page was opened. The navigation context may influence the way in
	which a page is displayed.
	
	For example if a content page is opened from a search screen then the
	page should be dispalyed with the search terms highlighted. Also if a page
	is opened from an index screen like a subscription listing then the page
	should be displayed with a link to that index page in the crumb trail.
	
	Some pages should pass their navigation on to pages opened from them.
	For example if a report home page is opened from a search screen then any
	documents opened should inherit the navigation context.
	
	This module provides facilities for storing and retrieving navigation context data
	 and for propagating it from page to page.
*/

var CTX_COOKIE_NAME_PREFIX = "FcmCtx";
var CTX_PARAM_NAME_PREFIX = "ctx";
var CTX_MAX_COOKIES = 6;


/*
 	Look through the cookies associated with the current page and
 	return an array of all the names of any navigation context cookies.
  
  Parameters:
  	None. 
 
  Returns:
  	An array of cookie names. 
*/
function _CtxGetCookieNames() 
{
	if (!document.cookie) return new Array();
	
	var aNames = new Array();
	
	var a = document.cookie.split(";");
	
	for (var n=0; n<a.length; n++) {
	
		var s = a[n].replace(/^[ ]*/, "");
		
		if (s.length<CTX_COOKIE_NAME_PREFIX.length) continue;
		if (s.substr(0, CTX_COOKIE_NAME_PREFIX.length)!=CTX_COOKIE_NAME_PREFIX) continue;
		
		var j = s.indexOf("=");
		aNames[aNames.length] = s.substr(0, j);		
		
	}

	return aNames;
}   
   
/*
 	Look through the cookies associated with the current page and
 	return the next useable context cookie name.
  
  Parameters:
  	None. 
 
  Returns:
  	A string which is the next context cookie name. 
*/
function _CtxGetNextName() 
{
	var a = _CtxGetCookieNames();
	
	var iMax = 0;
	
	for (var n=0; n<a.length; n++) {
	
		var s = a[n];
		
		var sNum = s.substr(CTX_COOKIE_NAME_PREFIX.length);
		var i = parseInt(sNum);
		
		if (i>iMax) iMax = i;
		
	}

	return CTX_COOKIE_NAME_PREFIX + (iMax+1);
}   
   
/*
	Remove the oldest context cookie if the maximum number of
	cookies has been exceeded.
  
  Parameters:
  	None. 
 
  Returns:
  	Nothing. 
*/
function _CtxPruneCookies() 
{
	var a = _CtxGetCookieNames();
	if (a.length<=CTX_MAX_COOKIES) return;
	
	var sName;
	var iMin = 999999;
	
	for (var n=0; n<a.length; n++) {
	
		var s = a[n];
		
		var sNum = s.substr(CTX_COOKIE_NAME_PREFIX.length);
		var i = parseInt(sNum);
		
		if (i<iMin) {
			iMin = i;
			sName = s;
		}
		
	}
	
	StdSetCookie(sName, "", -1);	
}   
   
/*
 	Store a context data item in a newly created cookie and
 	return the name of the cookie that was created.
  
  Parameters:
  	sValue	The data to be stored in the cookie. 
 
  Returns:
  	The name of the cookie into which the data was stored. 
*/
function _CtxCreateCookie(sValue) 
{
	var sName = _CtxGetNextName();

	StdSetCookie(sName, sValue, 0);

	_CtxPruneCookies();
		
	return sName;
}   
   
/*
	Set a context data item into a new cookie and add a reference to 
	the cookie into a passed url, returning the updated url.
	
  Parameters:
  	sUrl		The url to receive the data reference.
  	sName	The name of the context data item. 
  	sValue	The value of the context data item. 
 
  Returns:
  	The updated url. 
*/
function CtxPut(sUrl, sName, sValue) 
{ 
	var sCookieName = _CtxCreateCookie(sValue);
	
	var sParamName = CTX_PARAM_NAME_PREFIX+sName;
	
	var regexp = new RegExp("&"+sParamName+"=[^&]*", "gi");
	
	var sJoin = sUrl.indexOf("?")==-1 ? "?" : "&";
	
	return sUrl.replace(regexp, "")+sJoin+sParamName+"="+sCookieName;
}   
   
/*
  Get the value of a context data item.

  Parameters:
  	sName	The name of the item. 
 
  Returns:
  	The item value as a string or null if not found. 
*/
function CtxGet(sName) 
{ 
	var sParamName = CTX_PARAM_NAME_PREFIX+sName;

	var regexp = new RegExp("&"+sParamName+"=([^&]*)", "i");
  	var a = window.location.search.match(regexp);
  	if (a==null)	return null; 
	var sCookieName = a[1];
 
	return StdGetCookie(sCookieName);
}   
   
/*
  Propagate the context data of the current page into a passed url.

  Parameters:
  	sUrl	The url into which the context data of the current page is to be copied. 
 
  Returns:
  	The updated url. 
*/
function CtxPropagate(sUrl) 
{ 
	var regexp = new RegExp("&"+CTX_PARAM_NAME_PREFIX+"[^=]*", "gi");
  	var a = window.location.search.match(regexp);
  	if (a==null)	return sUrl;

	for (var n=0; n<a.length; n++) {
		sUrl = NavSetParameter(sUrl, a[n].substr(1), StdGetQueryStringParameter(a[n].substr(1))) ; 
	}

	return sUrl;
}   


/*
  Add a context link to the current page into a passed url.

  Parameters:
  	url		The url into which the context data of the current page is to be copied. 
  	name	The name of the context link. 
  	lablel	The trail label of the context link. 
 
  Returns:
  	The updated url. 
*/
function CtxAddContextLink(url, name, label)
{
	url = CtxPut(url, name+"Link", location.href);
	url = CtxPut(url, name+"Label", label);
	return url;
}

   
/*
  Set the Current Index link into a passed URL.

  Parameters:
  	url		The url into which the context data of the current page is to be copied. 
  	lablel	The trail label of the context link. 
 
  Returns:
  	The updated url. 
*/
function CtxSetIndexLink(url, label)
{
	return CtxAddContextLink(url, "ixp", label);
}

   
   