// *******************************************
// COOKIE READING/SETTING AND REDIRECTION
// *******************************************

function readCookie(cookieName) {

	if (document.cookie) {

		var whichChip = new RegExp("\\b" + cookieName + "=", "i");
		var index = document.cookie.search(whichChip);

		if (index != -1) {

			// Find the start 
			cookieStart = index + cookieName.length + 1;

			// Find the end 
			cookieStop = document.cookie.indexOf(";", index);
			if (cookieStop == -1) { cookieStop = document.cookie.length }

			// with the start and the end... now you've got the whole thing 
			return document.cookie.substring(cookieStart, cookieStop)
		}
		
		else return false;
		
	}
	return false;
}


function readChip(cookieValue, chipName) {

	if (!cookieValue) return false;
	
	cookieValue = unescape(cookieValue);
	var chips = cookieValue.split("|");
	
	for (c=0; c < chips.length; c++) {
	
		if (chips[c].indexOf(chipName) == 0) {
		
			var chipValue = chips[c].substring(chipName.length + 1);
			return chipValue;
		}
	}
	
	return false;
}

function setCookie (name,value,expires,path,domain,secure) 
{
   document.cookie = name + "=" + value +
   ((expires)	? ";expires=" + expires.toUTCString() : "") +
   ((path)		? ";path=" + path : "") +
   ((domain)	? ";domain=" + domain : "") +
   ((secure)	? ";secure" : "");
   //alert("setting cookie \nName:" + name + "\nValue:" + value + "\n" );
}

function deleteCookie(name, path, domain) {
	if (readCookie(name)) {
		document.cookie = name + '=' + 
		(( path ) ? ';path=' + path : '') + 
		(( domain ) ? ';domain=' + domain : '') + 
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
}