// The following four functions are taken from "JavaScript, The Definitive Guide" by David Flanagan, published by O'Reilly & Associates, with minor changes.
function Cookie(theDoc, nm, hours, path, domain, secure)
{
this.QQdocument = theDoc;
this.QQname = nm;
if (hours)
   this.QQexpiration = new Date((new Date()).getTime() + hours * 3600000);
else
   this.QQexpiration = null;
if (path)
   this.QQpath = path;
else
   this.QQpath = null;
if (domain)
   this.QQdomain = domain;
else
   this.QQdomain = null;
if (secure)
   this.QQsecure = true;
else
   this.QQsecure = false;
}

function _Cookie_store()
{
   var cookieval = "";
   for (var prop in this)
   {
      if ((typeof this[prop] == 'function') || ((prop.charAt(0) == 'Q') && (prop.charAt(1) == 'Q')))
         continue;
      if (cookieval != "")
         cookieval += '&';
      cookieval += prop + ':' + escape(this[prop]);
   }
   var cookie = this.QQname + '=' + cookieval;
   if (this.QQexpiration)
      cookie += '; expires=' + this.QQexpiration.toGMTString();
   if (this.QQpath)
      cookie += '; path=' + this.QQpath;
   if (this.QQdomain)
      cookie += '; domain=' + this.QQdomain;
   if (this.QQsecure)
      cookie += '; secure';
   this.QQdocument.cookie = cookie;
   return;
}

function _Cookie_load()
{
   var allcookies = this.QQdocument.cookie;
   if (allcookies == "")
      return false;
   var start = allcookies.indexOf(this.QQname + '=');
   if (start == -1)
      return false;
   start += this.QQname.length + 1;
   var end = allcookies.indexOf(';', start);
   if (end == -1)
      end = allcookies.length;
   var cookieval = allcookies.substring(start, end);
   var a = mysplit(cookieval, '&');
   for (var i=0; i<a.length; i++)
      a[i] = mysplit(a[i], ':');
   for (var i=0; i<a.length; i++)
      this[a[i][0]] = unescape(a[i][1]);
   return true;
}

function _Cookie_remove()
{
   var cookie;
   cookie = this.QQname + '=';
   if (this.QQpath)
      cookie += '; path=' + this.QQpath;
   if (this.QQdomain)
      cookie += '; domain=' + this.QQdomain;
   cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
   this.QQdocument.cookie = cookie;
   return;
}

// The following code is custom JavaScript written by Michael Eidson

function mysplit(instr, delim)
{
   var thestr = "" + instr;
   var cnt = 0;
   for (var i=0; i<thestr.length; i++)
   {
      if (thestr.charAt(i) == delim)
         cnt++;
   }
   var retArray = new Array(cnt);
   var whichStr = 0;
   var start = 0;
   for (var i=0; i<thestr.length; i++)
   {
      if (thestr.charAt(i) == delim)
      {
         var astring = "";
         for (var j=start; j<i; j++)
            astring = astring + thestr.charAt(j);
         start = i+1;
         retArray[whichStr] = "" + astring;
         whichStr++;
      }
   }
   var astring = "";
   for (var j=start; j<thestr.length; j++)
      astring = astring + thestr.charAt(j);
   retArray[whichStr] = "" + astring;
   return retArray;
}

var protoCookie = new Cookie(document, "proto", null, null, null, false);
Cookie.prototype.store  = _Cookie_store;
Cookie.prototype.load   = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;
protoCookie.remove();

