// Common operations on cookies


function eraseCookie (name, domain, path)
// Erase the cookie of the given name by giving it an expiry date of yesterday
{
	var date = new Date();
	date.setTime (date.getTime() - (24 * 60 * 60 * 1000));
	var expires = date.toGMTString();
	document.cookie = name + "=; expires=" + expires +
        				((path) ? "; path=" + path : "; path=/") +
        				((domain) ? "; domain=" + domain : "");	
}

function setCookie (name, value, domain, expires, path, secure) 
// Stores a cookie of the given name, value, domain where the cookie is valid (default 
// is the domain of the calling document), expiration date (default is the end of the
// current session), path where the cookie is valid (default is the calling document)
// and a boolean called 'secure' indicating if cookie transmission should be secure.
{
	document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "; path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie (name) 
// Finds a cookie of the given name and returns its contents 
{
   var cookie = document.cookie;
   var prefix = name + "=";
   var begin = cookie.indexOf ("; " + prefix);

   if (begin == -1) 
	{
      begin = cookie.indexOf (prefix);
      if (begin != 0) 
			return null;
	}
    	else 
        	begin += 2;
  
    var end = document.cookie.indexOf(";", begin);

    if (end == -1) 
       end = cookie.length;

    return unescape (cookie.substring (begin + prefix.length, end));
}
