var CityStateMap = new Object();
var CampusMap = new Object();

function itemList() {
  var list = new Array();

  this.addItem = function(c, u) {
    list[c] = u;
  };
  this.getList = function() { return list; };
}

function addCampus(c,s,u) {
  if ('undefined' == typeof(CampusMap[s]))
    CampusMap[s] = new itemList();
  CampusMap[s].addItem("F:" + c, u);
}

function addCity(c,s,u) {
  if ('undefined' == typeof(CityStateMap[s]))
    CityStateMap[s] = new itemList();
  CityStateMap[s].addItem("F:" + c, u);
}

function stateSelectCity(selector, sec) {
  if (selector[selector.selectedIndex].value <= 0) {
    selectStatePrompt(sec);
    return;
  }

  var list = CityStateMap[selector[selector.selectedIndex].value].getList();
  stateSelect(selector, sec, list, "city");
}

function stateSelectCampus(selector, sec) {
  if (selector[selector.selectedIndex].value <= 0) {
    selectStatePrompt(sec);
    return;
  }

  var list = CampusMap[selector[selector.selectedIndex].value].getList();
  stateSelect(selector, sec, list, "campus");
}

function selectStatePrompt(secondary) {
  secondary.options.length = 0;
  secondary.options[0] = new Option("--", -1);
  secondary.options[1] = new Option("Please select a state", -1);
}

function stateSelect(selector, secondary, list, what) {
  var i = 1;
  secondary.options.length = 0;
  secondary.options[0] = new Option("Please select a " + what, -1);

  for (var city in list)
    if (city.substring(0, 2) == "F:")
      secondary.options[i++] = new Option(city.substring(2), list[city]);
}

function selectThis(selector, reset) {
  var idx = selector.selectedIndex;
  if (selector[idx].value <= 0)
    return;
  if (reset)
    selector.selectedIndex = 0;
  document.location.href = encodeSession(selector[idx].value);
}

function selectCampus(name, state, id, stateSel, campusSel) {
  for (var i=1; i<stateSel.options.length; i++) {
    if (stateSel.options[i].value == state) {
      stateSel.selectedIndex = i;
      stateSelectCampus(stateSel, campusSel);
      break;
    }
  }

  for (var i=1; i<campusSel.options.length; i++) {
    if (campusSel.options[i].value == id) {
      campusSel.selectedIndex = i;
      break;
    }
  }
}
