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(c,u);
}

function addCity(c,s,u) {
  if ('undefined' == typeof(CityStateMap[s]))
    CityStateMap[s] = new itemList();
  CityStateMap[s].addItem(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("Select State First", -1);
}

function stateSelect(selector, secondary, list, what) {
  var i = 1;
  secondary.options.length = 0;
  secondary.options[0] = new Option("Select A " + what, -1);
  for (var city in list)
    secondary.options[i++] = new Option(city, list[city]);
}

function selectThis(selector) {
  var idx = selector.selectedIndex;
  if (selector[idx].value <= 0)
    return;

  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;
    }
  }
}
