function Cookie(name) {
  this.$name = name;  // Remember the name of this cookie

  var allcookies = document.cookie;
  if (allcookies == "") return;

  var cookies = allcookies.split(';');
  var cookie = null;
  for(var i = 0; i < cookies.length; i++) {
    // Does this cookie string begin with the name we want?
    if (cookies[i].substring(0, name.length+1) == (name + "=")) {
      cookie = cookies[i];
      break;
    }
  }

  // If we didn't find a matching cookie, quit now
  if (cookie == null) return;

  // The cookie value is the part after the equals sign
  var cookieval = cookie.substring(name.length+1);

  var a = cookieval.split('&'); // Break it into an array of name/value pairs
  for(var i=0; i < a.length; i++)  // Break each pair into an array
    a[i] = a[i].split(':');

  for(var i = 0; i < a.length; i++) {
    this[a[i][0]] = decodeURIComponent(a[i][1]);
  }
}

Cookie.prototype.store = function(daysToLive, path, domain, secure) {
  var cookieval = "";
  for(var prop in this) {
    // Ignore properties with names that begin with '$' and also methods
    if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
      continue;
    if (cookieval != "") cookieval += '&';
    cookieval += prop + ':' + encodeURIComponent(this[prop]);
  }

  var cookie = this.$name + '=' + cookieval;
  if (daysToLive || daysToLive == 0) { 
    cookie += "; max-age=" + (daysToLive*24*60*60);
  }

  if (path) cookie += "; path=" + path;
  if (domain) cookie += "; domain=" + domain;
  if (secure) cookie += "; secure";

  // Now store the cookie by setting the magic Document.cookie property
  document.cookie = cookie;
}

Cookie.prototype.remove = function(path, domain, secure) {
  // Delete the properties of the cookie
  for(var prop in this) {
    if (prop.charAt(0) != '$' && typeof this[prop] != 'function') 
      delete this[prop];
  }

  // Then, store the cookie with a lifetime of 0
  this.store(0, path, domain, secure);
}

Cookie.enabled = function() {
  // Use navigator.cookieEnabled if this browser defines it
  if (navigator.cookieEnabled != undefined) return navigator.cookieEnabled;

  // If we've already cached a value, use that value
  if (Cookie.enabled.cache != undefined) return Cookie.enabled.cache;

  // Otherwise, create a test cookie with a lifetime
  document.cookie = "testcookie=test; max-age=10000";  // Set cookie

  // Now see if that cookie was saved
  var cookies = document.cookie;
  if (cookies.indexOf("testcookie=test") == -1) {
    // The cookie was not saved
    return Cookie.enabled.cache = false;
  }
  else {
    // Cookie was saved, so we've got to delete it before returning
    document.cookie = "testcookie=test; max-age=0";  // Delete cookie
    return Cookie.enabled.cache = true;
  }
}
