var geocoder = new google.maps.Geocoder();

function geocode(address, action) {
  geocoder.geocode( { 'address': address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      // TODO handle ambigiuos locations
      var location = results[0].geometry.location;
      window.location = action+'?lat='+location.lat()+'&lng='+location.lng();
    } else {
      // TODO show error in form
      alert('can\'t determine your address');
    }
  });
};

$(document).ready(function() {
  $('#dealer_search_by_location').submit(function() {
    geocode($('#address').val(), $(this).attr('action'));
    return false;
  });
});