//*********************************************************************************
// Title: 				getCookieValue
// Desc: 				Get cookie values for a given cookie
// Created : 			July 31, 2002
// Last Modified: 		Sept 23, 2002
// Accepts:				cookieName - Name of the cookie.
// Returns:				The cookie value(s).
//*********************************************************************************
function getCookieValue(cookieName){
	var cookieValue = document.cookie;
	var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
	
	// check if cookie exists.  If cookie does not exist, do a second check at the beginning of the string
	if (cookieStartsAt == -1){
		cookieStartsAt = cookieValue.indexOf(cookieName + "=");
	}
	
	if (cookieStartsAt == -1){
		// coookie really does not exist
		cookieValue = null;
	}else{
		cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
		var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
		if (cookieEndsAt == -1){
			// cookie is the last one in the string
			cookieEndsAt = cookieValue.length;
		}
		cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
	}
	return cookieValue;
}

//*********************************************************************************
// Title: 				setSessionCookie
// Desc: 				Get cookie values for a given cookie
// Created : 			August 12, 2005
// Last Modified: 		August 31, 2005
// Accepts:				cookieName - Name of the cookie.
// 						cookieValue - Cookie value(s).
// 						cookiePath - Path cookie is valid for.
// 						cookieDomain - Expriy date/time of the cookie.
// Returns:				
//*********************************************************************************
function setSessionCookie(cookieName, cookieValue, cookiePath, cookieDomain){
	cookieValue = escape(cookieValue);
	
	if (cookiePath == "" || cookiePath == null){
		cookiePath = "; path=/";
	}else{
		cookiePath = "; path=" + cookiePath;
	}
	
	if (cookieDomain == "" || cookieDomain == null){
		cookieDomain = "";
	}else{
		cookieDomain = "; domain=" + cookieDomain;
	}
	//alert(cookieName + "=" + cookieValue + "; expires=" + cookieExpires + cookiePath + cookieDomain);
	document.cookie = cookieName + "=" + cookieValue + ";" + cookiePath + cookieDomain;
}


//*********************************************************************************
// Title: 				setCookie
// Desc: 				Get cookie values for a given cookie
// Created : 			July 31, 2002
// Last Modified: 		August 31, 2005
// Accepts:				cookieName - Name of the cookie.
// 						cookieValue - Cookie value(s).
// 						cookiePath - Path cookie is valid for.
// 						cookieExpires - Expriy date/time of the cookie.
//						cookieDomain - domain of the cookie
// Returns:				
//*********************************************************************************
function setCookie(cookieName, cookieValue, cookieExpires, cookiePath, cookieDomain){
	cookieValue = escape(cookieValue);
	
	if (cookieExpires == "" || cookieExpires == null){
		var nowDate = new Date();
		nowDate.setMonth(nowDate.getMonth() + 1);
		cookieExpires = nowDate.toGMTString();
	}
	
	if (cookiePath == "" || cookiePath == null){
		cookiePath = "; path=/";
	}else{
		cookiePath = "; path=" + cookiePath;
	}
	
	if (cookieDomain == "" || cookieDomain == null){
		cookieDomain = "";
	}else{
		cookieDomain = "; domain=" + cookieDomain;
	}
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires + cookiePath + cookieDomain;
}