var inputedTextBox;
var selectionBox;
var form;

function init() {
  inputedTextBox = document.getElementById("inputText");
  selectionBox = document.getElementById("userid");
  form = document.getElementById("lookupForm");
  populateSelBox();
}

function submitIfReady() {
  var selectedIndex = selectionBox.selectedIndex;
  if (selectedIndex == -1) {
    alert("You must select an alumni from the selection box!");
  } else {
    if (selectionBox.options[selectedIndex].value == "noneSelected") {
      alert("You must select an alumni from the selection box!  Click reset if there are none listed."); 
    } else {
      form.submit()
    }
  }
}

function populateSelBox() {
  var nameRE = new RegExp("^" + inputedTextBox.value, "i");
  var optionIndex = 0;
  clearSelBox();
  for(var i = 0; i < names.length; i++) {
    if (names[i].search(nameRE) != -1) {
      selectionBox.options[optionIndex++] = 
        new Option(names[i], alumniID[i], false, false);
    }
  }
  if (optionIndex == 0) {
    selectionBox.options[optionIndex] = 
      new Option("Sorry, no matches.", "noneSelected", false, false);
  }
}

function resetForm() {
  inputedTextBox.value = "";
  populateSelBox();
}

function clearSelBox() {
  selectionBox.options.length = 0;
}

