function findDealers() {
  var userZip = document.getElementById("zip").value;
  var userCity = document.getElementById("city").value;
  var userState = document.getElementById("state");
  userState = userState.options[userState.selectedIndex].text;

  // trim the strings
  userZip = userZip.replace(/^\s+/g, "");
  userZip = userZip.replace(/\s+$/g, "");
  userCity = userCity.replace(/^\s+/g, "");
  userCity = userCity.replace(/\s+$/g, "");
  
  // figure out which one to use -- use the zip code if
  // it's provided since that is the most specific

  var valid = (userZip.length == 5) || ((userCity != "") && (userState != "--"));
  
  if (valid)
  {
    if (userZip != "") {
      getUserCoordinates(userZip);
    } else {
      // e.g. turn "Seattle" and "WA" into "Seattle, WA"
      getUserCoordinates(userCity + ", " + userState);
    }
  }
  else
  {
    alert("Please provide either the city and state, or the 5-digit zip code.");
  }
}

var geocoder = new GClientGeocoder();

function getUserCoordinates(addr) {
  geocoder.getLocations(
    addr,
    function(result) {
      if (result.Status.code != G_GEO_SUCCESS)
      {
        var message = "";

	switch (result.Status.code)
	{
	  case G_GEO_MISSING_ADDRESS:
	    message = "Address is missing.";
	    break;
	  
	  case G_GEO_UNKNOWN_ADDRESS:
	    message = "Unable to find location.";
	    break;

	  case G_GEO_SERVER_ERROR:
	    message = "Unable to fulfill request.  Please try again later.";
	    break;

	  default:
	    message = "Location service is currently unavailable.";
	}

        alert(message);

      } else {
        var placemark = result.Placemark[0];
        var p = placemark.Point.coordinates;
        document.userloc.latitude.value = p[1];  // p[1] is lat
        document.userloc.longitude.value = p[0]; // p[0] is lon

	if (placemark.AddressDetails.Country.AdministrativeArea == null)
	{
	  alert("Unable to use the provided location.  Try another nearby location.");
	}
	else
	{
	  document.userloc.userstate.value = placemark.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
          document.userloc.submit();
	}
      }
    }
  );
}



