// cookie.js
// Copyright 2004 Ben Measures
//
// function existCookie(name)
// Checks if a cookie exists.
// Returns true if cookie is found, false otherwise.
//
// function setCookie(name, content, expiry, path, domain, secure)
// Sets a cookie. 
// Returns true if successful, false otherwise.
//
// function getCookie(name)
// Retrives a cookie.
// Returns the value of the cookie if successful, false otherwise.
//
// function deleteCookie(name, path, domain, secure)
// Expires a cookie.
// Returns false if the cookie is still accessible, true otherwise.

function existCookie(name)
{
  if (typeof(document.cookie) == 'undefined') {
    return false;
  } else {
    return (document.cookie.indexOf(escape(name)) != -1);
  }
}

function setCookie(name, content, expiry, path, domain, secure)
{
  if ((typeof(document.cookie) == 'undefined') || (typeof(name) == 'undefined')) {
    return false;
  } else {
    document.cookie = (escape(name) + '=' + escape(content))
                      + ((expiry) ? "; expires=" + expiry.toGMTString() : "")
		      + ((path) ? "; path=" + path : "")
		      + ((domain) ? "; domain=" + domain : "")
		      + ((secure) ? "; secure" : "");

    var cookie_success = (getCookie(name) == content);
    return cookie_success;
  }
} 

function getCookie(name)
{
  if ((typeof(document.cookie) == 'undefined') || !existCookie(name)) {
    return false;
  } else {
    var cookiestring = document.cookie;

    var startpos = cookiestring.indexOf(escape(name));
    var next_semicolon = cookiestring.indexOf(";", startpos);
    var endpos = ((next_semicolon == -1) ? cookiestring.length : next_semicolon);

    return unescape(cookiestring.substring(startpos + escape(name).length + 1, endpos));
  }
}

function deleteCookie(name, path, domain, secure)
{
  if ((typeof(document.cookie) == 'undefined') || !existCookie(name)) {
    return true;
  } else {
    var current_time = new Date();
    document.cookie = (escape(name) + '=' + escape(getCookie(name)))
                      + ('; expires=' + current_time.toGMTString())
		      + ((path) ? "; path=" + path : "")
		      + ((domain) ? "; domain=" + domain : "")
		      + ((secure) ? "; secure" : "");

    return !existCookie(name);
  }
}


