2d1fc742334c52812198ba3992b6d3e2fb30456a
[freeside.git] / httemplate / elements / standardize_locations.js
1 function status_message(text, caption) {
2   text = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">' + text + '</FONT></B></P>';
3   caption = caption || 'Please wait...';
4   overlib(text, WIDTH, 444, HEIGHT, 168, CAPTION, caption, STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
5 }
6
7 function form_address_info() {
8   var cf = document.<% $formname %>;
9
10   var returnobj = { billship: <% $billship %> };
11 % if ( $billship ) {
12   returnobj['same'] = cf.elements['same'].checked;
13 % }
14 % for my $pre (@prefixes) {
15 %   # normal case
16 %   for my $field (qw(address1 address2 city state zip country)) {
17     returnobj['<% $pre %><% $field %>'] = cf.elements['<% $pre %><% $field %>'].value;
18 %   } #for $field
19 %   if ( $withcensus ) {
20     returnobj['<% $pre %>censustract'] = cf.elements['<% $pre %>enter_censustract'].value;
21 %   }
22 % } #foreach $pre
23
24   return returnobj;
25 }
26
27 function standardize_locations() {
28
29   var cf = document.<% $formname %>;
30   var address_info = form_address_info();
31
32   var changed = false; // have any of the address fields been changed?
33
34 // clear coord_auto fields if the user has changed the coordinates
35 % for my $pre (@prefixes) {
36 %   for my $field ($pre.'latitude', $pre.'longitude') {
37
38   if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
39     cf.elements['<% $pre %>coord_auto'].value = '';
40   }
41
42 %   } #foreach $field
43   // but if the coordinates have been set to null, turn coord_auto on 
44   // and standardize
45   if ( cf.elements['<% $pre %>latitude'].value == '' &&
46        cf.elements['<% $pre %>longitude'].value == '' ) {
47     cf.elements['<% $pre %>coord_auto'].value = 'Y';
48     changed = true;
49   }
50   // standardize if the old address wasn't clean
51   if ( cf.elements['<% $pre %>addr_clean'].value == '' ) {
52     changed = true;
53   }
54 % } #foreach $pre
55
56   // or if it was clean but has been changed
57   for (var key in address_info) {
58     var old_el = cf.elements['old_'+key];
59     if ( old_el && address_info[key] != old_el.value ) {
60       changed = true;
61       break;
62     }
63   }
64
65 % # If address hasn't been changed, auto-confirm the existing value of 
66 % # censustract so that we don't ask the user to confirm it again.
67
68   if ( !changed && <% $withcensus %> ) {
69 %   if ( $billship ) {
70     if ( address_info['same'] ) {
71       cf.elements['bill_censustract'].value =
72         address_info['bill_censustract'];
73     } else {
74       cf.elements['ship_censustract'].value =
75         address_info['ship_censustract'];
76     }
77 %   } else {
78       cf.elements['censustract'].value =
79         address_info['censustract'];
80 %   }
81   }
82
83 % if ( $conf->config('address_standardize_method') ) {
84   if ( changed ) {
85     status_message('Verifying address...');
86     address_standardize(JSON.stringify(address_info), confirm_standardize);
87   }
88   else {
89 %   foreach my $pre (@prefixes) {
90     cf.elements['<% $pre %>addr_clean'].value = 'Y';
91 %   }
92     post_standardization();
93   }
94
95 % } else {
96
97   post_standardization();
98
99 % } # if address_standardize_method
100 }
101
102 var returned;
103
104 function confirm_standardize(arg) {
105   // contains 'old', which was what we sent, and 'new', which is what came
106   // back, including any errors
107   returned = JSON.parse(arg);
108
109   if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
110
111     replace_address(); // with the contents of returned['new']
112   
113   } else if ( returned['all_same'] ) {
114
115     // then all entered address fields are correct
116     // but we still need to set the lat/long fields and addr_clean
117     status_message('Verified');
118     replace_address();
119
120   } else {
121
122     var querystring = encodeURIComponent( JSON.stringify(returned) );
123     // confirmation popup: knows to call replace_address(), 
124     // post_standardization(), or submit_abort() depending on the 
125     // user's choice.
126     OLpostAJAX(
127         '<%$p%>/misc/confirm-address_standardize.html', 
128         'q='+querystring,
129         function() {
130           overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY, 
131             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 
132             576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', 
133             TEXTSIZE, 3 );
134         }, 0);
135
136   }
137 }
138
139 function replace_address() {
140
141   var newaddr = returned['new'];
142
143   var cf = document.<% $formname %>;
144 %  foreach my $pre (@prefixes) {
145   var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
146   var error = newaddr['<% $pre %>error'];
147   if ( clean ) {
148 %   foreach my $field (qw(address1 address2 city state zip addr_clean )) {
149     cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
150 %   } #foreach $field
151
152     if ( cf.elements['<% $pre %>coord_auto'].value ) {
153       cf.elements['<% $pre %>latitude'].value  = newaddr['<% $pre %>latitude'];
154       cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
155     }
156 %   if ( $withcensus ) {
157     if ( clean && newaddr['<% $pre %>censustract'] ) {
158       cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
159     }
160 %   } #if $withcensus
161   } // if clean
162 % } #foreach $pre
163
164   post_standardization();
165
166 }
167
168 function confirm_manual_address() {
169 %# not much to do in this case, just confirm the censustract
170 % if ( $withcensus ) {
171   var cf = document.<% $formname %>;
172 %   foreach my $pre (@prefixes) {
173   cf.elements['<% $pre %>censustract'].value =
174     cf.elements['<% $pre %>enter_censustract'].value;
175 %   }
176 % } # $withcensus
177   post_standardization();
178 }
179
180 function post_standardization() {
181
182 % if ( $conf->exists('enable_taxproducts') ) {
183
184   var cf = document.<% $formname %>;
185
186   var prefix = '<% $taxpre %>';
187   // fix edge case with cust_main
188   if ( cf.elements['same']
189     && cf.elements['same'].checked
190     && prefix == 'ship_' ) {
191
192     prefix = 'bill_';
193   }
194
195   if ( new String(cf.elements[prefix + 'zip'].value).length < 10 )
196   {
197
198     var country_el = cf.elements[prefix + 'country'];
199     var country = country_el.options[ country_el.selectedIndex ].value;
200     var geocode = cf.elements[prefix + 'geocode'].value;
201
202     if ( country == 'CA' || country == 'US' ) {
203
204       var state_el = cf.elements[prefix + 'state'];
205       var state = state_el.options[ state_el.selectedIndex ].value;
206
207       var url = "<% $p %>/misc/choose_tax_location.html" +
208                   "?data_vendor=cch-zip" + 
209                   ";city="     + cf.elements[prefix + 'city'].value +
210                   ";state="    + state + 
211                   ";zip="      + cf.elements[prefix + 'zip'].value +
212                   ";country="  + country +
213                   ";geocode="  + geocode +
214                   ";formname=" + '<% $formname %>' +
215                   ";";
216
217       // popup a chooser
218       OLgetAJAX( url, update_geocode, 300 );
219
220     } else {
221
222       cf.elements[prefix + 'geocode'].value = 'DEFAULT';
223       <% $post_geocode %>;
224
225     }
226
227   } else {
228
229     cf.elements[prefix + 'geocode'].value = '';
230     <% $post_geocode %>;
231
232   }
233
234 % } else {
235
236   <% $post_geocode %>;
237
238 % }
239
240 }
241
242 function update_geocode() {
243
244   //yay closures
245   set_geocode = function (what) {
246
247     var cf = document.<% $formname %>;
248     var prefix = '<% $taxpre %>';
249     if ( cf.elements['same']
250       && cf.elements['same'].checked
251       && prefix == 'ship_' ) {
252       prefix = 'bill_';
253     }
254
255     //alert(what.options[what.selectedIndex].value);
256     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
257     cf.elements[prefix + 'city'].value     = argsHash['city'];
258     setselect(cf.elements[prefix + 'state'], argsHash['state']);
259     cf.elements[prefix + 'zip'].value      = argsHash['zip'];
260     cf.elements[prefix + 'geocode'].value  = argsHash['geocode'];
261     <% $post_geocode %>;
262
263   }
264
265   // popup a chooser
266
267   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
268
269 }
270
271 function setselect(el, value) {
272
273   for ( var s = 0; s < el.options.length; s++ ) {
274      if ( el.options[s].value == value ) {
275        el.selectedIndex = s;
276      }
277   }
278
279 }
280
281 % if ($census_functions) { # do not use this in cust_main
282 function confirm_censustract() {
283 %   if ( FS::Conf->new->exists('cust_main-require_censustract') ) {
284   var form = document.<% $formname %>;
285   // this is the existing/confirmed censustract, not the manually entered one
286   if ( form.elements['censustract'].value == '' ||
287        form.elements['censustract'].value != 
288           form.elements['enter_censustract'].value ) {
289     var address_info = form_address_info();
290     address_info['latitude']  = form.elements['latitude'].value;
291     address_info['longitude'] = form.elements['longitude'].value;
292     OLpostAJAX(
293         '<%$p%>/misc/confirm-censustract.html',
294         'q=' + encodeURIComponent(JSON.stringify(address_info)),
295         function() {
296           overlib( OLresponseAJAX, CAPTION, 'Confirm censustract', STICKY,
297             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
298             576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
299             TEXTSIZE, 3 );
300         },
301         0);
302   } else {
303     <% $post_censustract %>;
304   }
305 %   } else { # skip this step
306   <% $post_censustract %>;
307 %   }
308 }
309
310 function set_censustract(tract, year) {
311   var form = document.<% $formname %>;
312   form.elements['censustract'].value = tract;
313   form.elements['censusyear'].value = year;
314   <% $post_censustract %>;
315 }
316
317 % } # $census_functions
318
319 <%init>
320
321 my %opt = @_;
322 my $conf = new FS::Conf;
323
324 my $withcensus = $opt{'with_census'} ? 1 : 0;
325
326 my @prefixes = '';
327 my $billship = $opt{'billship'} ? 1 : 0; # whether to have bill_ and ship_ prefixes
328 my $taxpre = '';
329 # probably should just geocode both addresses, since either one could
330 # be a package address in the future
331 if ($billship) {
332   @prefixes = qw(bill_ ship_);
333   $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : 'bill_';
334 }
335
336 my $formname =  $opt{form} || 'CustomerForm';
337 my $post_geocode = $opt{callback} || 'post_geocode();';
338 my $post_censustract;
339
340 my $census_functions = $opt{'with_census_functions'} ? 1 : 0;
341 if ( $census_functions ) {
342   $post_censustract = $post_geocode;
343   $post_geocode = 'confirm_censustract()';
344 }
345
346 </%init>