//*********************************************//
// Generic cookie functions
//    getCookie(name)
//    setCookie(name,value,expires,path,domain,secure)

// sets cookie value, 
// path defaults to "/", expires to today + 365 days
function setCookie(name,value,expires,path,domain,secure) {
  if (!expires) expires = getCookieExpiry(365); // expire in 365 days
  document.cookie = escape(name) + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "" ) +
	((path) ? ("; path=" + path) : ("; path=/")) +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	//alert(">>>>> setCookie(): Updated <<<<<<\n\n" + unescape(document.cookie.replace(/;/g,";\n"))); //).replace(/;/g,";\n")
}

function getCookieExpiry(daysToExpire){
  // compute an expiry date given daysToExpire
  var expDate = new Date();
  expDate.setTime( expDate.getTime() + (daysToExpire * 86400000) );
  // one day = 24 * 60 * 60 * 1000 ms = 86400000 ms
  //alert(" expDate="+expDate+"\n expDate.toGMTString()="+expDate.toGMTString())
  return expDate
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime(); // skew = timezone offset?
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

// returns single cookie value from cookie string.
function getCookie(cookieName){
	return getValue(cookieName,document.cookie,";"," ");
	}

//
// gets value from a storage string of multiple "name=value; " pairs
function getValue(nameSpec,storage,endChar,startChar) {
  //if (!storage) return ""; 
  // for MS ASP compatability dealing with "_" in names
  var name = escape(nameSpec);
  var prefix = name + "=";
  var begin = storage.indexOf(endChar + startChar + prefix); //look for "; name="
  if (begin == -1) {
    begin = storage.indexOf(prefix);
    if (begin != 0) return "";  //was null;
  } else
    begin += 2;
  var end = storage.indexOf(endChar, begin);
  if (end == -1)
    end = storage.length;
  return unescape(storage.substring(begin + prefix.length, end));
}

// OLD VERSION retrieves value from a name=value; pair stored in a string
function getValueOLD(name,stringStore,endChar,startChar) {
	var arg = escape(name) + "=";
	var alen = arg.length;
	var clen = stringStore.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (stringStore.substring(i, j) == arg){
			var endstr = stringStore.indexOf (endChar, j);
			if (endstr == -1)
				endstr = stringStore.length;
			return unescape(stringStore.substring(j, endstr));
			}
		i = stringStore.indexOf(startChar, i) + 1;
		if (i == 0) 
			 break; 
		}
	return "";
	}

// returns a value from cookie containing multiple name=value; elements
function getSubCookie(superCookie, subName){
	return getValue(subName,getCookie(superCookie),";"," ");
	}

// modifies or adds a value stored in a cookie as multiple name=value; elements
function setSubCookie(superCookie, subName, subValue) {
  var storage = getCookie(superCookie) // get the superCookie
  var name = escape(subName)
  var value = escape(subValue)
  var sep = "; "
  if (!storage) { // new superCookie
    setCookie(superCookie, name +"="+ value);
    return;
  }
  var nameLen = name.length
  var store = storage.split(sep)
  for (var i = 0; i < store.length; i++){
    if(name == store[i].substring(0,nameLen)) break; //found it
  }
  store[i] = name + "=" + value
  setCookie(superCookie, store.join(sep))
}

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
  if (getCookie(name)) document.cookie = name + "=" +
    (( path) ? ";path=" + path : ";path=/") +
    (( domain) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
