// Get the value of a variable passed in the URL, or supply a default
function getValue(varname, defvalue) {
  // Attempt to extract from URL
  var value = "";
  var url = window.location.href;
  var qparts = url.split("?");
  if (qparts.length >= 2) {
    var query = qparts[1];
    var vars = query.split("&");
    for (i = 0; i < vars.length; i++) {
      var parts = vars[i].split("=");
      if (parts[0] == varname) {
        value = parts[1];
        break;
      }
    }
  }
  // Return value from URL, or default
  if (value == "")
    value = defvalue;
  value = unescape(value);
  return value.replace(/\+/g, " ");
}

// Write a value
function writeValue(varname, defvalue) {
  document.write(getValue(varname, defvalue));
}

// Write a link, incorporating our variables in the URL
function writeChoice(choicetext, desturl) {
  var url = window.location.href;
  var qparts = url.split("?");
  document.write("<li><a href=\"", desturl);
  if (qparts.length >= 2)
    document.write("?", qparts[1]);
  document.write("\">", choicetext, "</a></li>");
}

// Write a form option, putting "selected" next to current choice from URL
function writeOption(varname, value, optiontext) {
  document.write("<option value = \"", value, "\"");
  if (getValue(varname) == value)
    document.write(" selected=\"selected\"");
  document.write(">", optiontext, "</option>");
}

// Write a form text input, putting value from URL in the field 
function writeTextInput(varname, defvalue) {
  document.write("<input type=\"text\" name=\"", varname, "\" value=\"", getValue(varname, defvalue), "\">");
}

