PageSourceSearch

https://sbschool.org/js/google-address-lookup-crm.js

js sbschool.org collected 2026-10-03 21:39:09 UTC 5,053 bytes, 136 lines download raw bytes

1/* ----------------------------------------------------------------------------------------- */
2/* Addresses ------------------------------------------------------------------------------- */
3/* ----------------------------------------------------------------------------------------- */
4var google_addr_is_init = 0;
5function initialize() {
6	google_addr_is_init = 1;
7
8	$('.address-fields-wrap').each(function() {
9		var address_fields_wrap = $(this);
10
11		var split_address_fields = $(address_fields_wrap).find('.split-address-field');
12		var use_google_smart_search_field = $(address_fields_wrap).find('.use-google-smart-search-field');
13
14		var address1_field = $(address_fields_wrap).find('.address1-field');
15		var city_field = $(address_fields_wrap).find('.city-field');
16		var country_field = $(address_fields_wrap).find('.country-field');
17		var state_field = $(address_fields_wrap).find('input.state-province-field');
18		var state_select = $(address_fields_wrap).find('select.state-province-field');
19		var zip_field = $(address_fields_wrap).find('.zip-field');
20		var latitude_field = $(address_fields_wrap).find('.latitude-field');
21		var longitude_field = $(address_fields_wrap).find('.longitude-field');
22
23		var google_address_field = $(address_fields_wrap).find('.google-address-field');
24
25		var input = $(google_address_field)[0];
26
27		$(use_google_smart_search_field).change(function() {
28			if($(use_google_smart_search_field).prop('checked') == true) {
29				$(google_address_field).show();
30				$(split_address_fields).hide();
31			} else {
32				$(google_address_field).hide();
33				$(split_address_fields).show();
34			}
35		}).change();
36
37		$(country_field).change(function() {
38			if($(this).val() == 'US') {
39				$(state_field).hide().attr('name','');
40				$(state_select).show().attr('name',$(state_select).attr('data-orig-name'));
41			} else {
42				$(state_field).show().attr('name',$(state_select).attr('data-orig-name'));
43				$(state_select).hide().attr('name','');
44			}
45		}).change();
46
47		var options = {};
48		var autocomplete = new google.maps.places.Autocomplete(input,options);
49		google.maps.event.addListener(autocomplete, 'place_changed', function() {
50			newInfo = $(input).val();
51			json_str = '';
52
53			error = '';
54			if(newInfo=='') error += 'You must enter a valid address.';
55
56			if(error) {
57				showAlert(error,"bad");
58
59			} else {
60				var geocoder = new google.maps.Geocoder();
61				geocoder.geocode({ 'address': newInfo }, function (results, status) {
62					if(results[0]&&results[0].address_components) {
63						var address_arr = {};
64
65						$(results[0].address_components).each(function() {
66							switch(this.types[0]) {
67								case 'street_number':
68									address_arr.address1=this.short_name;
69									break;
70								case 'route':
71									address_arr.address1=(address_arr.address1?address_arr.address1+' ':'')+this.short_name;
72									break;
73								default:
74								case 'locality':
75									if(!address_arr.city || this.types[0]=='locality') address_arr.city=this.long_name;
76									break;
77								case 'administrative_area_level_1':
78									address_arr.state=(this.short_name?this.short_name:this.long_name);
79									address_arr.longstate=this.long_name;
80									break;
81								case 'country':
82									address_arr.country=(this.short_name?this.short_name:this.long_name);
83									break;
84								case 'postal_code':
85									address_arr.zip=(this.short_name?this.short_name:this.long_name);
86									break;
87							}
88						});
89
90						// set fields
91						if(address_arr.address1) $(address1_field).val(address_arr.address1);
92						if(address_arr.city) $(city_field).val(address_arr.city);
93						if(address_arr.state) {
94							$(state_field).val(address_arr.state);
95							$(state_select).val(address_arr.state);
96						}
97						if(address_arr.country) $(country_field).val(address_arr.country).change();
98						if(address_arr.zip) $(zip_field).val(address_arr.zip);
99
100						// lat/lng
101						$(latitude_field).val(new String(results[0].geometry.location.lat()));
102						$(longitude_field).val(new String(results[0].geometry.location.lng()));
103
104						// get postal code manually if not obtained above
105						if(!address_arr.zip) {
106							geocoder.geocode({ 'location': results[0].geometry.location }, function (results, status) {
107								if (status == google.maps.GeocoderStatus.OK) {
108									var result=results[0].address_components;
109
110									for(var i=0;i<result.length;++i){
111										if(result[i].types[0]=="postal_code"){
112											address_arr.zip = (result[i].short_name?result[i].short_name:result[i].long_name);
113											$(zip_field).val(address_arr.zip);
114										}
115									}
116								}
117							});
118						}
119					}
120				});
121			}
122		});
123	});
124}
125
126google.maps.event.addDomListener(window, 'load', initialize);
127setTimeout(function() {
128	if(!google_addr_is_init) initialize();
129},500);
130
131$(document).on('keydown',".address-field",function(e) {
132	if(e.keyCode=='13') {
133		return false;
134		e.preventDefault();
135	}
136});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.