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