/////////////////////////////////////////////////////////////
//
// cookielib.js
//
// Antonio Cisternino 2008
//
// http://www.toscana4u.net
//
////////////////////////////////////////////////////////////

RegisterScript('CookieLib', 1, 'cookie library');

function Cookies() {
  var data = (document.cookie + "").split("; "), i;
  this.length = data.length;
  for (i = 0; i < data.length; i++) {
    var idx = data[i].indexOf("=");
    this[data[i].substring(0, idx)] = unescape(data[i].substring(idx + 1));
  }
  
  this.Set = function (name, value, expiration, path, domain, secure) {
    var s = name + '=' + escape(value) + ';';
    if (expiration)
      s += 'expires=' + expiration.toGMTString() + ';';
    if (path)
      s += 'path=' + path + ';';
    if (domain)
      s += 'domain=' + path + ';';
    if (secure)
      s += 'secure;'

    if (!expiration || (expiration && expiration > (new Date()))) {
      document.cookie = s;
      this[name] = value;
    } else throw "Invalid expiration!";
  }
  
  this.Remove = function (name) {
    if (this[name]) {
      document.cookie = name + '=; expires=' + (new Date((new Date()).getTime() - 1)).toGMTString()+';';
      this[name] = null;
      this.length--;
    }
  }  
}
