function getCookieValue(cookiename) { 
	allcook = document.cookie;		
	
	thiscookie = document.cookie.split("; ")
	var myret = "";
	var tempcook = new String("");
	//alert("allcook=" + allcook);	
	
	for (var i = 0; i < thiscookie.length; i++) {
		tempcook = new String(thiscookie[i]);
		if (tempcook.substring(0, cookiename.length) == cookiename) {
			myret = tempcook.split("=")[1]	 
			break;
		}			
	}
	return unescape(myret);
}
function isCookieExists(Name) {
	var search = Name + "="
	var exists = false
	if (document.cookie.length > 0) { 
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			exists = true;
		}
	}
	return exists;
}
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}
var today = new Date();
var expired = new Date(today.getTime() - 28 * 24 * 60 * 60 * 1000); // less 28 days
function deleteCookie(name) { // use: deleteCookie("name");
    document.cookie=name + "=null; expires=" + expired.toGMTString(); // delete cookie
    document.cookie = document.cookie;
  }


function GetCookie (name)  {
	//alert("in GetCookie");
	//alert("c=" + document.cookie);
	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 SetCookie (name, value)  {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");

}
function DeleteCookie (name) { 
	var exp = new Date();
	var argv = DeleteCookie.arguments;
	var argc = DeleteCookie.arguments.length;
	var path = (argc > 1) ? argv[1] : null;
	exp.setTime (exp.getTime() - 1000000000);  // This cookie is history
	//alert('cookie b4=' + document.cookie);
	var cval = GetCookie (name);
	//alert('newval=' + name + "=" + cval + "; expires=" + exp.toGMTString());
	document.cookie =name + "=" + cval + "; expires=" + exp.toGMTString() + ((path == null) ? "" : ("; path=" + path)); 
	//alert("It should have deleted the cookie-cookie after=" + document.cookie);

}
function findCookieValueByKey(cname, cval, csep, valsep) {
	var CookieString = document.cookie;
	var CookieSet = CookieString.split (';');
	var SetSize = CookieSet.length;
	var CookiePieces
	var ReturnValue = '';
	var x = 0;
	for (x = 0; ((x < SetSize) && (ReturnValue == '')); x++) {
		//alert(CookieSet[x]);
		CookiePieces = CookieSet[x].split ('=');
		if (CookiePieces[0].substring (0,1) == ' ') {
			CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
		}
		if (CookiePieces[0] == cname) {
			if (CookiePieces.length > 2) {
				for (var j = 1; j < CookiePieces.length; j++) {
					if (ReturnValue == '') {
						ReturnValue = CookiePieces[j];
					} else {
						ReturnValue = ReturnValue + valsep + CookiePieces[j];	
					}
				}
			} else {
				ReturnValue = CookiePieces[1];
			}
		}
	}
	//alert('return value=' + ReturnValue);
	var vals = ReturnValue.split(csep);
	var tval = '';
	for (var i = 0; i < vals.length; i++) {
		tval = vals[i];
		tpartvals = tval.split(valsep);
		if (tpartvals[0] == cval) {
			return tpartvals[1];	
		}
	}
	
}
