/*
 * ONE CMS: jslib/one&cookie.js
 * An js libray for the cookie class
 *
 * Joris Dormans (2007)
 *
 * requires:
 *
 *
 */
 
function Cookie(document, name, hours) {
  this._document=document;
  this._name=name;
  if (hours) {
    this._expiration = new Date((new Date()).getTime()+ hours*3600000);
  } else this._experiration = null;
}

Cookie.prototype.store = function() {
  var cookieval="";
  for (var prop in this) {
    if ((prop.charAt(0)=="_") || (typeof this[prop] == "function")) continue;
    if (cookieval != "") cookieval+="&";
    cookieval+=prop+":"+escape(this[prop]);
  }

  var cookie = this._name+"="+cookieval;
  if (this._expiration) cookie+="; expires="+this._expiration.toGMTString();
  cookie+="; path=/";
  this._document.cookie=cookie;
}

Cookie.prototype.load = function () {
  var allcookies = this._document.cookie;
  if (allcookies == "") return false;

  var start=allcookies.indexOf(this._name+"=");
  if (start==-1) return false;
  start+=this._name.length+1;
  var end = allcookies.indexOf(";", start);
  if (end==-1) end = allcookies.length;
  var cookieval = allcookies.substring(start, end);

  var a = cookieval.split("&");
  for (var i=0; i<a.length; i++) a[i]=a[i].split(":");
  for (var i=0; i<a.length; i++) {
    this[a[i][0]]=unescape(a[i][1]);
  }
  return true;
}

Cookie.prototype.remove = function () {
  var cookie = this._name+"=";
  cookie+="; expires=Fri, 02-Jan-1970 00:00:00 GMT; path=/";
  this._document.cookie = cookie;
}

