function cleanUp() {
  document.getElementById('person').checked = true;
  document.getElementById('region').options[0].selected = true;
  document.getElementById('degree').options[0].selected = true;
  document.getElementById('prodegree').options[0].selected = true;
  document.getElementById('foreign_lang').options[0].selected = true;
  document.getElementById('search_for').value = "";
  updateSpec(0, '');
}

function initValues(type, sp, sc, pr, pd) {
  if (type == 'office') {
    document.getElementById('office').checked = true;
    updateSpec(1, sp);
  } else {
    document.getElementById('person').checked = true;
    updateSpec(0, sp);
  }
  spec = document.getElementById('spec').value;
  updateScope(spec, sc);
  updatePref(spec, pr);
  updateProDegree(spec, pd);
}

function updateSpec(type, sp) {
  specSelect = document.getElementById('spec');
  specSelect.options.length = 0;
  x = 0;
  specValue = 0;
  for (var i in speciality) {
    if ((speciality[i][2] == 1) || ((speciality[i][2] == 0) && (type == 0))) {
      specSelect.options[x++] = new Option(speciality[i][1], speciality[i][0], false, false);
      if (sp == speciality[i][0]) {
        specSelect.options[x - 1].selected = true;
        specValue = speciality[i][0];
      }
    }
  }
  if (sp == "") {
    specSelect.options[0].selected = true;
    specValue = specSelect.options[0].value;
  }
  if (type == 1) {
    document.getElementById('degree').disabled = true;
    document.getElementById('prodegree').disabled = true;
  } else {
    document.getElementById('degree').disabled = false;
    document.getElementById('prodegree').disabled = false;
  }
  updatePrefsAndScope(specValue);
}

function updatePrefsAndScope(spec) {
  updatePref(spec, '');
  updateScope(spec, '');
  updateProDegree(spec, '');
}

function updatePref(spec, pr) {
  prefSelect = document.getElementById('pref');
  prefSelect.options.length = 1;
  x = 1;
  for (var i in parentPrefs) {
    if (parentPrefs[i][2] == spec) {
      prefSelect.options[x++] = new Option(parentPrefs[i][1], parentPrefs[i][0], false, false);
      a = parentPrefs[i][0];
      if (pr == parentPrefs[i][0]) {
        prefSelect.options[x - 1].selected = true;
      }
      for (var j in c[a]) {
        if (c[a][j][2] == spec) {
          prefSelect.options[x++] = new Option('-- ' + c[a][j][1], c[a][j][0], false, false);
          if (pr == c[a][j][0]) {
            prefSelect.options[x - 1].selected = true;
          }
        }
      }
    }
  }
  for (var k in e) {
    if (e[k][0] == spec) {
      spec2 = e[k][1];
      for (var i in parentPrefs) {
        if (parentPrefs[i][2] == spec2) {
          prefSelect.options[x++] = new Option(parentPrefs[i][1], parentPrefs[i][0], false, false);
          a = parentPrefs[i][0];
          if (pr == parentPrefs[i][0]) {
            prefSelect.options[x - 1].selected = true;
          }
          for (var j in c[a]) {
            if (c[a][j][2] == spec2) {
              prefSelect.options[x++] = new Option('-- ' + c[a][j][1], c[a][j][0], false, false);
              if (pr == c[a][j][0]) {
                prefSelect.options[x - 1].selected = true;
              }
            }
          }
        }
      }
    }
  }
  //    for (var i in pref.options) {
  //        if (pr == pref.options[i].value) {
  //            pref.options[i].selected = true;
  //        }
  //    }
}

function updateScope(spec, sc) {
  scope_help = document.getElementById('scope');
  scope_help.options.length = 1;
  x = 1;
  for (var i in scopeHelp) {
    if (scopeHelp[i][2] == spec) {
      scope_help.options[x++] = new Option(scopeHelp[i][1], scopeHelp[i][0], false, false);
      if (sc == scopeHelp[i][0]) {
        scope_help.options[x - 1].selected = true;
      }
    }
  }
}

function updateProDegree(spec, sc) {
  pro_degree = document.getElementById('prodegree');
  pro_degree.options.length = 1;
  x = 1;
  for (var i in proDegree) {
    if (proDegree[i][2] == spec) {
      pro_degree.options[x++] = new Option(proDegree[i][1], proDegree[i][0], false, false);
      if (sc == proDegree[i][0]) {
        pro_degree.options[x - 1].selected = true;
      }
    }
  }
}

