var req;

function change_dependent_menu(name_of_menu_to_change, 
  name_of_menu_specifying_data) {

  window.status = " "

  var menu_to_change = document.getElementById(name_of_menu_to_change)

  var menu_specifying_data = 
    document.getElementById(name_of_menu_specifying_data)

  if(menu_specifying_data.length > 0) {
    var category_ID = 
      menu_specifying_data.options[menu_specifying_data.selectedIndex].value

    if(category_ID != "" && category_ID != "Not a category ID") {
      url = "/cgi-bin/send-categories/send?category_ID=" + 
	escape(category_ID)
      window.status = "Now downloading data from server..."

      var download_start_time = new Date().getTime()
      more_specific_categories = get_doc(url);

      var download_time = (new Date().getTime() - download_start_time) / 1000

      if(download_time > .9 && download_time < 1.1) {
	window.status = "Data downloaded from server in 1 second"
      } else {
	window.status = "Data downloaded from server in " +
	  (Math.floor(download_time * 10) / 10) + " seconds"
      }

      var lines = more_specific_categories.split("\n")

      // note that the last element of this array will be an empty string.

      if(lines.length > 2) {
	/* 
	  it should never be 1 or 2, because /cgi-bin/send-categories/send
	  should never return data that would produce such an array.
	*/
	clear_menu(name_of_menu_to_change)
      }

      for(var j = 0; j + 1 <= lines.length - 2; j += 2) {
	append_to_menu(name_of_menu_to_change, lines[j], lines[j + 1])
      }
    }
  }
}

function clear_menu(menu_name) {
  eval("document.forms[1]." + menu_name + ".length = 0")
}

function empty_menu(menu_name) {
  clear_menu(menu_name)

  append_to_menu(menu_name, "Not a category ID", "[Menu empty]")
}

function append_to_menu(menu_name, value, presentation_text) {
  var menu = eval("document.forms[1]." + menu_name)
  menu.options[menu.options.length] = new Option(presentation_text, value)
}

function get_doc(url) {
if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    req.open("GET", url, false);
    req.send(null);
  } else if (window.ActiveXObject) {
    isIE = true;
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      req.open("GET", url, false);
      req.send();
    }
  }

  if (req.status == 200) {
    return req.responseText
  } else {
    alert("There was a problem retrieving data from the server:\n" +
      req.statusText);

    return "";
  }
}

