var reWhitespace = /^\s+$/;

function go(c, a1, a2) {
  if (c == "ds") {
    if (!confirm("This will remove your saved search and cannot be undone.  Continue?")) {
      return;
    }
  }
  if (c == "f" && a2 == "_") {
    return;
  }
  if (c == "fa" && a1 == "") {
    return;
  }
  if (c == "s" && (a1 == "" || reWhitespace.test(a1))) {
    alert("Please enter a non-empty name for this search first.");
    return;
  }

  if (c == "ce") {
    // Commit bulk edit -- must copy down the values:
    var f = document.forms.k;
    var i;
    var s = "";
    for(i=0;i<f.elements.length;i++) {
      var elem = f.elements[i]; 
      if (elem.name.substring(0, 2) == "be") {
        s += "_;x_" + elem.name+"="+elem.value;
      }
    }
    a1 = s;
  }

  var f = document.forms.f;
  if (typeof a1 == "undefined") a1="";
  if (typeof a2 == "undefined") a2="";
  f.u.value = c;
  f.v.value = a1;
  f.w.value = a2;
  f.submit();
}

function hint() {
  var s = "";
  hintUpto += 1;
  if (hintUpto == hints.length) hintUpto=0;
  var t = hints[hintUpto];
  s = "<font name=verdana size=+1><b>Hint:</b>&nbsp;<a title=\"Click this to see another hint.\" href=javascript:hint()>" + t + "</a></font>";
  document.getElementById("hint").innerHTML = s;
}

function hidediv(id) {
  // safe function to hide an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById(id).style.display = 'none';
  } else {
    if (document.layers) { // Netscape 4
      document.id.display = 'none';
    } else { // IE 4
      document.all.id.style.display = 'none';
    }
  }
}

function showdiv(id) {
  // safe function to show an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById(id).style.display = 'block';
  } else {
    if (document.layers) { // Netscape 4
      document.id.display = 'block';
    } else { // IE 4
      document.all.id.style.display = 'block';
    }
  }
}

function divishidden(id) {
  // safe function to show an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    return document.getElementById(id).style.display == 'none';
  } else {
    if (document.layers) { // Netscape 4
      return document.id.display == 'none';
    } else { // IE 4
      return document.all.id.style.display == 'none';
    }
  }
}

function toggleAccount() {
  if (divishidden("p2a")) {
    showdiv("p2a");
    showdiv("p2b");
    document.getElementById("ac").innerHTML = "Use existing Account";
    document.getElementById("ss").value = "Create";
  } else {
    hidediv("p2a");
    hidediv("p2b");
    document.getElementById("ac").innerHTML = "Create new Account";
    document.getElementById("ss").value = "Sign In";
  }
}

function doAccount() {
  var p1 = document.forms.a.p1.value;
  
  if (!divishidden("p2a")) {
    var p2 = document.forms.a.p2.value;
    if (p1 != p2) {
      alert("Passwords are different.  Please enter the same password twice.");
      return;
    }
  }

  document.forms.a.u.value = "ac";

  if (divishidden("p2a")) {
    document.forms.a.v.value = "ss";
  } else {
    document.forms.a.v.value = "cc";
  }

  document.forms.a.submit();
}

function tf(vid, isIdent) {
  var v = document.forms.f.g.value;
  var vs = vid.substring(1);
  var s1 = "" + vs;
  var s2 = "." + vs;
  var s3 = vs + ".";
  var s4 = "." + vs + ".";
  if (divishidden(vid)) {
    // Add this to the list:
    showdiv(vid);
    if (v != s1 &&
	v.indexOf(s3) != 0 &&
        v.substring(v.length-s2.length) != s2 &&
	v.indexOf(s4) == -1) {
      if (v == "") {
	v = s1;
      } else {
	v = v + s2;
      }
    }
  } else {
    // Remove from the list:
    hidediv(vid);
    if (v == s1) {
      v = "";
    } else if (v.substring(0, s3.length) == s3) {
      v = v.substring(s3.length);
    } else if (v.substring(v.length-s2.length) == s2) {
      v = v.substring(0, v.length-s2.length);
    } else {
      v = v.replace(s4, ".");
    }
  }
  document.forms.f.g.value = v;
}

function xf(vid, isIdent, lhs) {
  if (isIdent) {
    if (lhs) {
      alert("Please clear the values, then click Go, before hiding this filter.");
    } else {
      alert("Please clear this filter first, then click on the pencil.");
    }
  } else {
    if (lhs) {
      alert("Please remove this filter (click red X) before hiding this filter.");
    } else {
      alert("Please first select 'Clear Filter' to hide this filter, then click on the pencil.");
    }
  }
}

function shf(vid) {
  var i;
  if (divishidden("m"+vid+"x0")) {
    document.getElementById("x"+vid).innerHTML = "less...";
    for(i=0;;i++) {
      var id = "m"+vid+"x"+i;
      var obj = document.getElementById(id);
      if (obj == undefined) break;
      showdiv(id);
      obj.className = "odd";
    }
  } else {
    document.getElementById("x"+vid).innerHTML = "more...";
    for(i=0;;i++) {
      var id = "m"+vid+"x"+i;
      var obj = document.getElementById(id);
      if (obj == undefined) break;
      hidediv(id);
      obj.className = "odd";
    }
  }
}


