
////////////////////////////////////////////////////////////////////////////
// Cookie class for reading and writing cookies in JS...
function Cookie(doc, name, hours, path, domain, secure)
{
  // All the properties of this object begin with $ to distinguish them from
  // other properties.
  this.$document= doc;
  this.$name= name;
  // If it is not a session cookie, set it to expire in 30 days...
  if (typeof(hours) != 'undefined' && hours)
  {
    var expDate= new Date((new Date()).getTime() + hours * 3600000);
    this.$expiration= expDate.toUTCString();
  }
  else
  {
    this.$expiration= null;
  }
  if (path) this.$path= path; else this.$path= null;
  if (domain) this.$domain= domain; else this.$domain= null;
  if (secure) this.$secure= secure; else this.$secure= 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;
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  if (this.$secure) cookie += '; secure';
  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(':');
    this[a[i][0]]= unescape(a[i][1]);
  }
  
  return true;
}

Cookie.prototype.remove = function()
{
  var cookie= this.$name + '=';
  if (this.$path) cookie += '; path=' + this.$path;
  if (this.$domain) cookie += '; domain=' + this.$domain;
  cookie += '; expires=Fri, 01-Jan-1970 00:00:00 GMT';
  this.$document.cookie= cookie;
}

// End Cookie class
////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////
// Saves select form variables from the specified form in a cookie
function saveSearchValues(frmID, fldList)
{
  if (typeof(frmID) == 'undefined' || !frmID ||
    typeof(fldList) == 'undefined' || !fldList)
  {
    return false;
  }
  var frm= document.getElementById(frmID);
  if (typeof(frm) == 'undefined' || !frm) return false;
  
  // If you would rather use a session cookie instead of a persistant cookie,
  // set the third parameter to zero (0).  Otherwise, the cookie will persist
  // for n hours which will get updated each time they post the form. 240 is 10 days on next line
  var searchCriteria= new Cookie(document, 'searchCriteria', 240);
  
  // Traverse each element in the form...
  for(var i= 0; i < frm.elements.length; i++)
  {
    // Traverse each listed field...
    for(var fld in fldList)
    {
      if (frm.elements[i].name == fldList[fld])
      {
        if (((frm.elements[i].type == 'radio' || frm.elements[i].type == 'checkbox') &&
          frm.elements[i].checked) ||
          (frm.elements[i].type != 'radio' && frm.elements[i].type != 'checkbox'))
        {
          searchCriteria[fldList[fld]]= frm.elements[i].value;
        }
        break;
      }
    }
  }
  
  // Finally, store the cookie...
  searchCriteria.store();
  
  return true;
}

////////////////////////////////////////////////////////////////////////////
// Restore the forms search criteria from the cookie if it exists...
function restoreSearchValues(frmID, fldList)
{
  if (typeof(frmID) == 'undefined' || !frmID ||
    typeof(fldList) == 'undefined' || !fldList)
  {
    return false;
  }
  var frm= document.getElementById(frmID);
  if (typeof(frm) == 'undefined' || !frm) return false;
  
  var searchCriteria= new Cookie(document, 'searchCriteria', 240);
  if (!searchCriteria.load()) return false; // No cookie...
  
  // Traverse each listed field...
  for(var fld in fldList)
  {
    // Traverse each element in the form...
    for(var i= 0; i < frm.elements.length; i++)
    {
      if (frm.elements[i].name == fldList[fld])
      {
        if (frm.elements[i].type == 'radio')
        {
          var k= -1;
          for(var n in radioButtons[frm.elements[i].name])
          {
            if (radioButtons[frm.elements[i].name][n] == searchCriteria[fldList[fld]])
            {
              k= n;
              break;
            }
          }
          if (k != -1)
          {
            if (frm.elements[i].name == 'optTripType')
            {
              frm.optTripType[n].checked= true;
            }
            else if (frm.elements[i].name == 'simpleclass')
            {
              frm.simpleclass[n].checked= true;
            }
          }
        }
        else if (frm.elements[i].type == 'checkbox')
        {
          if (typeof(searchCriteria[fldList[fld]]) != 'undefined')
            frm.elements[i].checked= true;
          else
            frm.elements[i].checked= false;
        }
        else
          frm.elements[i].value= searchCriteria[fldList[fld]];
        break;
      }
    }
  }
  
  return true;
}
