//This is all javascript code for the cookies 
function getName(cookiestring) 
{ 
	//The position of the 1st "=" 
	var end = cookiestring.indexOf('='); 
	return cookiestring.substring(0,end); 
} 

function getValue(cookiestring) 
{ 
	var start = cookiestring.indexOf('=') + 1; 
	return cookiestring.substring(start,cookiestring.length); 
} 

function splitValue(valuestring, valuekey) 
{ 
	//split the value string, The delimiter is "&" 
	var a = valuestring.split('&'); 
	for(var i=0; i<a.length; i++) 
	{ 
		if (a[i].indexOf(valuekey) >= 0) //check if array element is the value part requested 
		{ 
			return getValue(a[i]); 
		} 
	} 
	return ''; 
} 

function replaceValue(valuestring, valuekey, valuekeystring) 
{ 
	//split the value string, The delimiter is "&" 
	var isnewvalue=false; 
	var newvalue='' 
	var a = valuestring.split('&'); 
	for(var i=0; i<a.length; i++) 
	{ 
		if (a[i].indexOf(valuekey) >= 0) //check if array element is the value part requested 
		{ 
			a[i] = valuekeystring; 
			isnewvalue=true; 
		} 
		//build new value string 
		delim = (((i+1)==a.length) ? '' : '&'); 
		if (a[i].length > 0){
			newvalue += a[i] + delim; 
		}

	} 
	//Check that the new value exists 
	if (isnewvalue!=true) 
	newvalue += '&' + valuekeystring; 
	
	return newvalue; 
}

//Function to get the cookie and it value 
function getCookie(cname) 
{ 
	var allcookies = document.cookie; 
	//alert("allcookies are " + allcookies)
	//Check that cookie has a value 
	if (allcookies!='') 
	{ 
		//split the cookies 
		var cookies = allcookies.split(';'); 
		for (var i=0; i < cookies.length; i++) 
		{ 
			name = trim(getName(cookies[i])); 
			//alert("cookie = " + cname+ ", cname length = " + cname.length +", name found is " + name+ ", name length = " + name.length);
			if (name.indexOf(cname)>=0 && cname.length==name.length) //See if the cookie matches the one requested 
				return cookies[i]; 
		} 
	}	 
	return ''; 
}  

//Function to update the cookie. The string is provided 
function updateCookie(cookieToUpdate, updateString, domain) 
{ 
	//updates the existing cookie 
	var expiration = new Date(new Date().getTime() + 240*3600000); 
	//expires in 10 Days 
	var updatecookie = cookieToUpdate+ '=' + updateString; 
	updatecookie += '; expires=' + expiration.toGMTString(); 
	updatecookie += '; path=/'; 
	updatecookie += ';domain=' + domain; 
	//alert("updating = " + updatecookie); 
	document.cookie = updatecookie; 
} 

//Function to read the cookie 
function readCookie(cookieToRead) 
{ 
	currentcookie = getCookie(cookieToRead); 
	//alert("reading = " + currentcookie);
	return currentcookie; 
} 

function removeNavCookieSetting(spanName) 
{
	//replace the value with nothing
	updateNavCookie(spanName,"");
}

function addNavCookieSetting(spanName) 
{
	//add the value if it doesn't already exist
	updateNavCookie(spanName,spanName);
}

function updateNavCookie(keyValue,replacementValue)
{
	//Get the nav gookie
	hwbnav = getCookie("hwbnav");

	//get the new value to add for this cookie
	newValue = replaceValue(getValue(hwbnav), keyValue, replacementValue); 

	//update the cookie. function will exist in the main page
	updateCookieValue("hwbnav",newValue);
}


