1 function standardize_locations() {
3 var startup_msg = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">Verifying address...</FONT></B></P>';
4 overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
5 var cf = document.<% $formname %>;
7 var state_el = cf.elements['<% $main_prefix %>state'];
8 var ship_state_el = cf.elements['<% $ship_prefix %>state'];
10 var changed = false; // have any of the address fields been changed?
16 'company': cf.elements['<% $main_prefix %>company'].value,
18 'address1': cf.elements['<% $main_prefix %>address1'].value,
19 'address2': cf.elements['<% $main_prefix %>address2'].value,
20 'city': cf.elements['<% $main_prefix %>city'].value,
21 'state': state_el.options[ state_el.selectedIndex ].value,
22 'zip': cf.elements['<% $main_prefix %>zip'].value,
23 'country': cf.elements['<% $main_prefix %>country'].value,
26 'ship_company': cf.elements['<% $ship_prefix %>company'].value,
28 % if ( $withcensus ) {
29 'ship_censustract': cf.elements['censustract'].value,
31 'ship_address1': cf.elements['<% $ship_prefix %>address1'].value,
32 'ship_address2': cf.elements['<% $ship_prefix %>address2'].value,
33 'ship_city': cf.elements['<% $ship_prefix %>city'].value,
34 'ship_state': ship_state_el.options[ ship_state_el.selectedIndex ].value,
35 'ship_zip': cf.elements['<% $ship_prefix %>zip'].value,
36 'ship_country': cf.elements['<% $ship_prefix %>country'].value,
39 // clear coord_auto fields if the user has changed the coordinates
40 % for my $pre ($ship_prefix, $onlyship ? () : $main_prefix) {
41 % for my $field ($pre.'latitude', $pre.'longitude') {
43 if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
44 cf.elements['<% $pre %>coord_auto'].value = '';
48 // but if the coordinates have been set to null, turn coord_auto on
50 if ( cf.elements['<% $pre %>latitude'].value == '' &&
51 cf.elements['<% $pre %>longitude'].value == '' ) {
52 cf.elements['<% $pre %>coord_auto'].value = 'Y';
58 // standardize if the old address wasn't clean
59 if ( cf.elements['old_<% $ship_prefix %>addr_clean'].value == '' ||
60 ( <% !$onlyship || 0 %> &&
61 cf.elements['old_<% $main_prefix %>addr_clean'].value == '' ) ) {
66 // or if it was clean but has been changed
67 for (var key in address_info) {
68 var old_el = cf.elements['old_'+key];
69 if ( old_el && address_info[key] != old_el.value ) {
76 address_standardize(JSON.stringify(address_info), confirm_standardize);
79 cf.elements['ship_addr_clean'].value = 'Y';
81 cf.elements['addr_clean'].value = 'Y';
83 post_standardization();
89 function confirm_standardize(arg) {
90 // contains 'old', which was what we sent, and 'new', which is what came
91 // back, including any errors
92 returned = JSON.parse(arg);
94 if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
96 replace_address(); // with the contents of returned['new']
101 var querystring = encodeURIComponent( JSON.stringify(returned) );
102 // confirmation popup: knows to call replace_address(),
103 // post_standardization(), or submit_abort() depending on the
106 '<%$p%>/misc/confirm-address_standardize.html',
109 overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY,
110 AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
111 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
118 function replace_address() {
120 var newaddr = returned['new'];
122 var clean = newaddr['addr_clean'] == 'Y';
123 var ship_clean = newaddr['ship_addr_clean'] == 'Y';
124 var error = newaddr['error'];
125 var ship_error = newaddr['ship_error'];
127 var cf = document.<% $formname %>;
128 var state_el = cf.elements['<% $main_prefix %>state'];
129 var ship_state_el = cf.elements['<% $ship_prefix %>state'];
131 % if ( !$onlyship ) {
134 cf.elements['<% $main_prefix %>company'].value = newaddr['company'];
136 cf.elements['<% $main_prefix %>address1'].value = newaddr['address1'];
137 cf.elements['<% $main_prefix %>address2'].value = newaddr['address2'];
138 cf.elements['<% $main_prefix %>city'].value = newaddr['city'];
139 setselect(cf.elements['<% $main_prefix %>state'], newaddr['state']);
140 cf.elements['<% $main_prefix %>zip'].value = newaddr['zip'];
141 cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
143 if ( cf.elements['<% $main_prefix %>coord_auto'].value ) {
144 cf.elements['<% $main_prefix %>latitude'].value = newaddr['latitude'];
145 cf.elements['<% $main_prefix %>longitude'].value = newaddr['longitude'];
152 cf.elements['<% $ship_prefix %>company'].value = newaddr['ship_company'];
154 cf.elements['<% $ship_prefix %>address1'].value = newaddr['ship_address1'];
155 cf.elements['<% $ship_prefix %>address2'].value = newaddr['ship_address2'];
156 cf.elements['<% $ship_prefix %>city'].value = newaddr['ship_city'];
157 setselect(cf.elements['<% $ship_prefix %>state'], newaddr['ship_state']);
158 cf.elements['<% $ship_prefix %>zip'].value = newaddr['ship_zip'];
159 cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
160 % if ( $withcensus ) {
161 cf.elements['<% $main_prefix %>censustract'].value = newaddr['ship_censustract']
163 if ( cf.elements['<% $ship_prefix %>coord_auto'].value ) {
164 cf.elements['<% $ship_prefix %>latitude'].value = newaddr['latitude'];
165 cf.elements['<% $ship_prefix %>longitude'].value = newaddr['longitude'];
169 post_standardization();
173 function post_standardization() {
175 var cf = document.<% $formname %>;
177 % if ( $conf->exists('enable_taxproducts') ) {
179 if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
182 var country_el = cf.elements['<% $taxpre %>country'];
183 var country = country_el.options[ country_el.selectedIndex ].value;
184 var geocode = cf.elements['geocode'].value;
186 if ( country == 'CA' || country == 'US' ) {
188 var state_el = cf.elements['<% $taxpre %>state'];
189 var state = state_el.options[ state_el.selectedIndex ].value;
191 var url = "<% $p %>/misc/choose_tax_location.html" +
192 "?data_vendor=cch-zip" +
193 ";city=" + cf.elements['<% $taxpre %>city'].value +
195 ";zip=" + cf.elements['<% $taxpre %>zip'].value +
196 ";country=" + country +
197 ";geocode=" + geocode +
198 ";formname=" + '<% $formname %>' +
202 OLgetAJAX( url, update_geocode, 300 );
206 cf.elements['geocode'].value = 'DEFAULT';
213 cf.elements['geocode'].value = '';
226 function update_geocode() {
229 set_geocode = function (what) {
231 var cf = document.<% $formname %>;
233 //alert(what.options[what.selectedIndex].value);
234 var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
235 cf.elements['<% $taxpre %>city'].value = argsHash['city'];
236 setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
237 cf.elements['<% $taxpre %>zip'].value = argsHash['zip'];
238 cf.elements['geocode'].value = argsHash['geocode'];
245 overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
249 function setselect(el, value) {
251 for ( var s = 0; s < el.options.length; s++ ) {
252 if ( el.options[s].value == value ) {
253 el.selectedIndex = s;
261 my $conf = new FS::Conf;
266 my $formname = $opt{form} || 'CustomerForm';
267 my $onlyship = $opt{onlyship} || '';
268 my $main_prefix = $opt{main_prefix} || '';
269 my $ship_prefix = $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
270 my $taxpre = $main_prefix;
271 $taxpre = $ship_prefix if ( $conf->exists('tax-ship_address') || $onlyship );
272 my $post_geocode = $opt{callback} || 'post_geocode();';
273 $withfirm = 0 if $opt{no_company};
274 $withcensus = 0 if $opt{no_census};