// ***** Cookies ***************************************************************

// ***** setCookie *****

/*

PARAMETERS:

name, value  - cookie name & value
expires      - cookie expiration date (defaults to end of current session)
path, domain - cookie path & domain (defaults to document's path & domain)
secure       - cookie sent only over secure connection

example CALLS:

setCookie("counter", "1");
setCookie("counter", "1", Date("January 01, 2010 00:00:01"));
setCookie("counter", "1", undefined, '/');

*/


function setCookie(name, value, expires, path, domain, secure)
{
  document.cookie =
    name+"="+escape(value)+
    (expires ? "; expires="+expires.toGMTString() : "")+
    (path    ? "; path="   +path   : "")+
    (domain  ? "; domain=" +domain : "")+
    (secure  ? "; secure" : "");
}

// ***** setCookieLT *****

// PARAMETERS: lifetime - cookie lifetime in seconds

function setCookieLT(name, value, lifetime, path, domain, secure)
{
  if (lifetime) lifetime = new Date(Date.parse(new Date())+lifetime*1000);
  setCookie(name, value, lifetime, path, domain, secure);
}

// ***** getCookie *****

function getCookie(name)
{
  var cookie, offset, end;
  cookie  = " "+document.cookie;
  offset  = cookie.indexOf(" "+name+"=");
  if (offset == -1) return undefined;
  offset += name.length+2;
  end     = cookie.indexOf(";", offset)
  if (end    == -1) end = cookie.length;
  return unescape(cookie.substring(offset, end));
}

// ***** delCookie *****

// PARAMETERS:
//
// name         - cookie name
// path, domain - cookie path & domain (the same as those used to create cookie)

function delCookie(name, path, domain)
{
  if (getCookie(name))
    setCookie(name, "", new Date("January 01, 2000 00:00:01"), path, domain);
}

// ***** Examples **************************************************************

// ***** Set & Delete *****

// The cookie is set and then deleted.

function example1()
{
  setCookie("example", "Delete cookie example!");
  alert(getCookie("example"));
  delCookie("example");
  alert(getCookie("example"));
}

// ***** Lifetime *****

// The cookie should expire between the first and the second alerts.

function example2()
{
  setCookieLT("example", "Cookie lifetime example!", 1);
  alert(getCookie("example"));
  alert(getCookie("example"));
}

// ***** Delete & Visibility *****

/*

For this example the document should not be in the root directory.
The first cookie is set in the root directory.
The second cookie is set in the current directory and it hides the first one.
However, when the second cookie is deleted the first one becomes visible again.

*/

function example3()
{
  setCookie("example", "Cookie visibility example - 1!", undefined, '/');
  alert(getCookie("example"));
  setCookie("example", "Cookie visibility example - 2!");
  alert(getCookie("example"));
  delCookie("example");
  alert(getCookie("example"));
  delCookie("example", '/');
}


//________________________________________________________________________________________________