///////////////////////////////////////////
// JS functions for the database search. //
///////////////////////////////////////////


// fillProv() fills in the options for the "Province" field of the search form and
// selects the previously selected option as default.

function fillProv() {
  document.searchForm.provField.length = 0;

  for (i = 0; i < Provinces.length; i++) {
    var sel = 0;
    if ((Provinces[i] == formProv) || (formProv == "" && i == 0)) {
      sel = 1;
    }
    document.searchForm.provField.options[i] = new Option(ProvLookup[Provinces[i]], Provinces[i], sel, sel);
  }
  document.searchForm.provField.options[Provinces.length] = new Option(blankFieldProv, '');
  fillCity();
}


function fillProvF() {
  document.searchForm.provField.length = 0;

  for (i = 0; i < Provinces.length; i++) {
    var sel = 0;
    if ((Provinces[i] == formProv) || (formProv == "" && i == 0)) {
      sel = 1;
    }
    document.searchForm.provField.options[i] = new Option(ProvLookupF[Provinces[i]], Provinces[i], sel, sel);
  }
  document.searchForm.provField.options[Provinces.length] = new Option(blankFieldProv, '');
  fillCity();
}


// fillCity() fills in the options for the "City" field of the search form and
// selects the previously selected option as default.

function fillCity() {
  var chosenProv, chosenText;

  if (document.searchForm.provField.options[document.searchForm.provField.selectedIndex].value == '') {
    document.searchForm.provField.selectedIndex--;
  }

  document.searchForm.cityField.length = 0;
  chosenProv = new String(document.searchForm.provField.options[document.searchForm.provField.selectedIndex].value);
  chosenText = new String(document.searchForm.provField.options[document.searchForm.provField.selectedIndex].text);
  if (chosenProv == "xx") {
    clearCity();
  } else {
    if (formCity == "") {
      document.searchForm.cityField.options[0] = new Option(vbl + ' ' + chosenText, '', 1, 1);
    } else {
      document.searchForm.cityField.options[0] = new Option(vbl + ' ' + chosenText, '');
    }
    document.searchForm.cityField.options[1] = new Option(blankFieldCity, '');
    for (i = 0; i < Cities[chosenProv].length; i++) {
      var sel = 0;
      if (Cities[chosenProv][i] == formCity) {
        sel = 1;
      }
      document.searchForm.cityField.options[i + 2] = new Option(Cities[chosenProv][i], Cities[chosenProv][i], sel, sel);
    }
  }
}


// launchProv() launches a search from the Offices database from the clickable maps

function launchProv(map_city) {
  document.altSearch.cityField.value = map_city;
  document.altSearch.submit();
}


// clearCity() fills the "City" field of the search form with empty options.

function clearCity() {
  document.searchForm.cityField.length = 0;
  document.searchForm.cityField.options[0] = new Option(blankFieldCity, '', 1, 1);
  for (i = 1; i <= 7; i++) {
    document.searchForm.cityField.options[i] = new Option(blankFieldCity, '');
  }
}


// checkCity() checks to see if the divider line has been selected and if so
// changes the selection to the previous option.

function checkCity() {
  if (document.searchForm.cityField.options[document.searchForm.cityField.selectedIndex].text == blankFieldCity) {
    document.searchForm.cityField.selectedIndex--;
  }
}


// checkData() validates the "Province" field to ensure that something was
// selected.  It also validates the "Dealer" field for illegal or problematic
// entries by the user.

function checkData() {
  var prov, dlr;
    
  prov = new String(document.searchForm.provField.options[document.searchForm.provField.selectedIndex].value);

  if (prov == 'xx') {
    alert("You must select a province.  Please try again.");
    return false;
  }
  return true;
}


function checkDealer(flag) {
  var dlr = new String(document.dealerForm.dealerField.value);
  var dlr_out = new String("");

  if (flag == 'f') {
    var msg = "Vous devez entrer un mot de recherche pour le concessionnaire.  Veuillez en entrer un.";
  } else {
    var msg = "You must enter a search term for the dealer.  Please enter one.";
  }

  if (dlr == "") {
    alert(msg);
    document.dealerForm.dealerField.focus();
    return false;
  }

  var x = dlr.split("");
  for (i = 0; i < x.length; i++) {
    if (x[i] == "'") {
      x[i] = '%';
    }
    dlr_out = dlr_out + x[i];
  }
  document.dealerForm.dealerField.value = dlr_out;

  return true;
}
