<!--
/*
trims spaces from both ends of string using methods consistent across browsers
*/
function trimSpaces(x) {
  // leading spaces
  while (x.charAt(0) == ' ') {
    x = x.substring(1);
  }

  // trailing spaces
  while (x.charAt(x.length - 1) == ' ') {
    x = x.substring(0, x.length - 1);
  }

  return x;
}

var isCleared = false;

function validate_search(f) {
  f.action = "/maps/SiteAdvantage/advantage.asp";
  zipValue = f.postalCode.value;
  if (zipValue.length==0) {
    // no ZIP and no state - either only city is entered or all fields blank
    if (f.state.options[f.state.selectedIndex].value.length==0) {
      alert("You must enter a city and state, state, or ZIP code.");
      if (trimSpaces(f.city.value).length==0) {
        f.state.focus();
      } else {
        f.city.focus();
        f.city.select();
      }

      return;
    } else {
      // state selected, but not city - get list of cities
      if (trimSpaces(f.city.value).length==0) {
        //f.stateName.value = f.state.options[f.state.selectedIndex].text;
        f.stateProvince.value = f.state.options[f.state.selectedIndex].text;
        f.action = "/maps/SiteAdvantage/state_locations_by_city.asp";
      } else {
        // city/state entered - find best match
        f.action = "/maps/SiteAdvantage/check_city_state.asp";
      }
    }
  } else {
    for (var i=0; i<zipValue.length; i++) {
      var digit = zipValue.charAt(i);
      if (digit<"0" || digit>"9") {
        alert("\"" + zipValue + "\" is not a valid ZIP code.  Please re-enter.")
        f.postalCode.focus();
        f.postalCode.select();
        return;
      }
    }

    if (zipValue.length!=5) {
      alert("Please enter 5-digit ZIP code.");
      f.postalCode.focus();
      f.postalCode.select();
      return;
    }

    // blank out other field entries (they will not be used)
    f.city.value = "";
    f.state.selectedIndex = 0;
  }

  f.submit();
}

//-->
