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