LISTINGS_CHECKED_COOKIE = 'listing_checked';

// Make hash object
CheckHash = new Object();

function id(str) {
// Compatibly get element by id
  if (document.getElementById)
    return document.getElementById(str);
  else
    return document.all[str];
}

function bodyLoad() {
// Fill hash from cookie, set checkmarks while we do it
  CheckList = getCookie(LISTINGS_CHECKED_COOKIE).split(',');
  for (i in CheckList) {
    k = CheckList[i];
    if (k.length > 0) {
      CheckHash[k] = 1;
      if (id('chk'+k)) id('chk'+k).checked = 1;
    }
  }
}

function setCookie(name, value) {
// Store cookie
  if (!name.length) return;

  // today  = new Date();
  // expire = new Date();
  // expire.setTime(today.getTime() + 3600000*24*days);
  // document.cookie = name + '=' + escape(value) + ';expires=' + expire.toGMTString();
  document.cookie = name + '=' + escape(value);
}

function getCookie(name) {
// Return cookie value
  if (!name.length) return '';

  index = document.cookie.indexOf(name + '=');
  if (index == -1) return '';

  start = index + name.length + 1;
  end   = document.cookie.indexOf(';', start);

  if (end == -1) end = document.cookie.length;

  return unescape(document.cookie.substring(start, end));
}

function setFromHash() {
// Set cookie from current state of hash

  // Make comma-string representation of checked values
  str = '';
  for (k in CheckHash) {
    if (CheckHash[k] == 1) {
      if (str.length > 0) str += ',';
      str += k;
    }
  }

  // Remember it as cookie
  setCookie(LISTINGS_CHECKED_COOKIE, str);
}

function toggle(k) {
// Toggle checkbox value, remember in hash, set cookie
  if (!id('chk'+k)) return;

  // Set value on/off from checkbox
  if (id('chk'+k).checked)
    CheckHash[k] = 1;
  else
    CheckHash[k] = 0;

  setFromHash();
}

function clearAll() {
// Clear all checkboxes (even ones not on this page)

  for (k in CheckHash) {
    if (CheckHash[k] == 1) {
      CheckHash[k] = 0;
      if (id('chk'+k)) id('chk'+k).checked = 0;
    }
  }

  // Store in cookie
  setFromHash();
}

