Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created December 27, 2010 19:19
Show Gist options
  • Save ashaw/756439 to your computer and use it in GitHub Desktop.
Save ashaw/756439 to your computer and use it in GitHub Desktop.
validateAddress : function(addr) {
that = this;
this.geocoder.geocode({'address': addr, 'region' : 'us'}, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
that.location_type = results[0]['geometry']['location_type'];
if (that.isValidLocationType()) {
that.followValidAddress();
that.cur_google_suggestion = results[0].formatted_address;
} else {
// if it's not a valid address or zip, we'll *assume* it's a facility search
// though this means no more inline sad msgs unless we can't geocode at all.
that.followFacilitySearch();
}
} else {
that.notifyInvalid("We couldn't do your search. Please try again.");
}
});
},
isValidZip : function() {
return !_.isNull(this.submittedAddr.match(/^[0-9]{5}$/));
},
isValidLocationType : function() {
if (this.isValidZip()) {
return true;
} else if (this.location_type === "RANGE_INTERPOLATED" || this.location_type === "ROOFTOP" ) {
return true;
} else {
return false;
}
},
submitAddress : function(e) {
var that = this;
e.preventDefault();
that.submittedAddr = $("#search_address input").val();
that.validateAddress(that.submittedAddr);
},
followValidAddress : function() {
$("#address_search_form").submit();
},
followFacilitySearch : function() {
$("#address_search_form").attr("action", RAILS_ROOT + 'search');
$("#address_search_form").submit();
},
notifyInvalid : function(msg) {
$("#search_notice").show().addClass("sad_msg").html("<p>" + msg + "<\/p>").delay(4000)
.slideUp(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment