s1JS.Cookie = function (name, hours, path, delimiter)
{
   this.$name = name;
   if(hours) this.$expires = new Date((new Date()).getTime() + hours * 3600000);
   if(path) this.$path = path;
   if(delimiter) 
   {
      this.$delim = delimiter;
   }
   else
   {
      this.$delim = ':';
   }
}

s1JS.Cookie.prototype.store = function(parseForPerl)
{
   var cookieval = "";

   var seperator = '&';
   if(parseForPerl) seperator = escape(';');

   for(var prop in this)
   {
      if((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) continue;
     
      if( cookieval != "" ) cookieval += seperator;
      cookieval += prop + this.$delim + escape(this[prop]);
   }

   var cookie = this.$name + '=' + cookieval;
   if(this.$expires) cookie += '; expires=' + this.$expires.toGMTString();
   if(this.$path)    cookie += '; path=' + this.$path;

   document.cookie = cookie;
}

s1JS.Cookie.prototype.load = function(keepIntact)
{
   var cookies = document.cookie;
   if( cookies == "") return false;

   var start = cookies.indexOf(this.$name + '=');
   if(start == -1) return false;

   start += this.$name.length + 1;
   var end = cookies.indexOf(';', start);
   if(end == -1) end = cookies.length;

   var cookieval = cookies.substring(start, end);

   if(keepIntact)
   {
      this[this.$name] = unescape(cookieval);
   }
   else
   {
      var params = cookieval.split('&');

      for(var i = 0; i < params.length; i++)
      {
         params[i] = params[i].split(this.$delim);         
      }

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

   return true;
}

s1JS.Cookie.prototype.remove = function()
{
   var cookie = this.$name + '=';
   if(this.$path) cookie += '; path=' + this.$path;
   cookie += '; expires=Fri, 28-Sep-1984 00:00:00 GMT'; // happy birthday to me
   document.cookie = cookie;
}

s1JS.SimpleCookie = function(name, hours, path)
{
   this.s1Cookie = new s1JS.Cookie(name, hours, path);
}

s1JS.SimpleCookie.prototype.load = function()
{
   if(this.s1Cookie.load(true))
   {
      this[this.s1Cookie.$name] = this.s1Cookie[this.s1Cookie.$name];
      return true;
   }
   return false;
}

s1JS.SimpleCookie.prototype.store = function(value)
{
   var cookie = this.s1Cookie.$name + '=' + value;
   if(this.s1Cookie.$expires) cookie += '; expires =' + this.s1Cookie.$expires.toGMTString();
   if(this.s1Cookie.$path) cookie += '; path=' + this.s1Cookie.$path;

   document.cookie = cookie;
}

s1JS.SimpleCookie.prototype.remove = function()
{
   return this.s1Cookie.remove();
}
