var oFavorites = {
 cookieName: "favoriteproducts",
 buttonName: "savedhouses",

 // generic functions
 getCookieItemValue: function(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf(prefix);
  var end;

  if (begin == -1)
   return null;
  begin += prefix.length;
  end = document.cookie.indexOf(";", begin);
  if (end == -1)
   end = dc.length;
  return unescape(dc.substring(begin, end));
 },

 setCookieItem: function(name, value, expires, path, domain, secure) {
  var CookieItem = name + "=" + escape(value);
  CookieItem += ((expires) ? "; expires=" + expires.toGMTString() : "");
  //CookieItem += ((path) ? "; path=" + path : "");
  CookieItem += "; path=/";
  CookieItem += ((domain) ? "; domain=" + domain : "");
  CookieItem += ((secure) ? "; secure" : "");
  document.cookie = CookieItem;
 },

 // custom functions
 find: function(p_strCode) {
  var cCookie = this.getCookieItemValue(this.cookieName);
  var aCookie;

  if (cCookie == null)
   return false;
  aCookie = cCookie.split("|");
  for (i = 0; i < aCookie.length; i++)
   if (p_strCode == aCookie[i].split("@")[0])
   return true;
  return false;
 },

 updateButtonText: function() {
  var oButton = document.getElementById(this.buttonName);
  var cCookie = this.getCookieItemValue(this.cookieName);
  var nCookieLength = 0;

  if (!oButton)
   return;
  if (cCookie != null)
   if (cCookie != '')
    nCookieLength = cCookie.split("|").length;
  oButton.innerHTML = oButton.innerHTML.substr(0, oButton.innerHTML.indexOf('(') + 1) + nCookieLength + ")";
 },

 remove: function(p_strCode) {
  var cCookie = this.getCookieItemValue(this.cookieName);
  var cLine = '';
  var today = new Date();
  var expires = new Date();
  var aCookie;
  var nCookieLength;

  expires.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 365);
  if (cCookie != null) {
   aCookie = cCookie.split("|");
   nCookieLength = aCookie.length;
   for (i = 0; i < nCookieLength; i++)
    if (p_strCode != aCookie[i].split("@")[0])
    cLine += aCookie[i] + '|';
   if (cLine.length != 0)
    cLine = cLine.substr(0, cLine.length - 1);

   //setCookieItem: function(name, value, expires, path, domain, secure)			 
   this.setCookieItem(this.cookieName, cLine, expires);
  }
 },

 add: function(p_strCode, p_dFrom, p_intDuration) {
  var cCookie = this.getCookieItemValue(this.cookieName);
  var cLine = '';
  var today = new Date();
  var expires = new Date();
  var aCookie;
  var nCookieLength;

  expires.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 365);
  if (cCookie != null)
   if (cCookie == '')
   this.setCookieItem(this.cookieName, p_strCode + '@' + p_dFrom + '@' + p_intDuration, expires);
  else {
   aCookie = cCookie.split("|");
   nCookieLength = aCookie.length;
   for (i = 0; i < nCookieLength; i++)
    if (p_strCode == aCookie[i].split("@")[0])
     return;
   this.setCookieItem(this.cookieName, cCookie + '|' + p_strCode + '@' + p_dFrom + '@' + p_intDuration, expires);
  }
  else
   this.setCookieItem(this.cookieName, p_strCode + '@' + p_dFrom + '@' + p_intDuration, expires);
 },

 toggle: function(p_blnNewValue, p_strCode, p_From, p_intDuration) {
  if (p_blnNewValue) {
   if (this.find(p_strCode))
    this.remove(p_strCode);
   this.add(p_strCode, p_From, p_intDuration);
  }
  else
   if (this.find(p_strCode))
    this.remove(p_strCode);
  this.updateButtonText();
 },

 reSet: function(oSelect) {
  var aValues, cFrom, cDuration;
  var dToday = new Date();
  var dExpires = new Date();
  var cFavorites = this.getCookieItemValue(this.cookieName);
  var aFavorites = cFavorites.split('|');
  if (cFavorites != '') {
   cFavorites = '';
   for (i = 0; i < aFavorites.length; i++) {
    aValues = aFavorites[i].split('@');
    cFrom = oSelect.name == 'From' ? oSelect[oSelect.selectedIndex].value : aValues[1];
    if (cFrom == '') cFrom = aValues[1];
    cDuration = oSelect.name == 'Duration' ? oSelect[oSelect.selectedIndex].value : aValues[2];
    if (cDuration == '') cDuration = aValues[2];
    if (cFavorites != '') cFavorites += '|';
    cFavorites += aValues[0] + '@' + cFrom + "@" + cDuration;
   }
   if (cFavorites != '') {
    dExpires.setTime(dToday.getTime() + 1000 * 60 * 60 * 24 * 365);
    this.setCookieItem(this.cookieName, cFavorites, dExpires);
   }
  }
 }
}
