address standardization/census tract fixes
[freeside.git] / httemplate / elements / standardize_locations.js
1 function form_address_info() {
2   var cf = document.<% $formname %>;
3
4   var returnobj = { onlyship: <% $onlyship ? 1 : 0 %> };
5 % if ( !$onlyship ) {
6   returnobj['same'] = cf.elements['same'].checked;
7 % }
8 % if ( $withfirm ) {
9 % # not part of either address, really
10   returnobj['company'] = cf.elements['company'].value;
11 % }
12 % if ( $withcensus ) {
13 % # "entered" censustract always goes with the ship_ address if there is one
14   returnobj['ship_censustract'] = cf.elements['enter_censustract'].value;
15 % }
16 % for my $pre (@prefixes) {
17   if ( <% $pre eq 'ship_' ? 1 : 0 %> && returnobj['same'] ) {
18 %   # special case: don't include any ship_ fields, and move the entered
19 %   # censustract over to bill_.
20     returnobj['bill_censustract'] = returnobj['ship_censustract'];
21     delete returnobj['ship_censustract'];
22   } else {
23 %   # normal case
24 %   for my $field (qw(address1 address2 city state zip country)) {
25     returnobj['<% $pre %><% $field %>'] = cf.elements['<% $pre %><% $field %>'].value;
26 %   } #for $field
27   } // if returnobj['same']
28 % } #foreach $pre
29
30   return returnobj;
31 }
32
33 function standardize_locations() {
34
35   var cf = document.<% $formname %>;
36   var address_info = form_address_info();
37
38   var changed = false; // have any of the address fields been changed?
39
40 // clear coord_auto fields if the user has changed the coordinates
41 % for my $pre (@prefixes) {
42 %   for my $field ($pre.'latitude', $pre.'longitude') {
43
44   if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
45     cf.elements['<% $pre %>coord_auto'].value = '';
46   }
47
48 %   } #foreach $field
49   // but if the coordinates have been set to null, turn coord_auto on 
50   // and standardize
51   if ( cf.elements['<% $pre %>latitude'].value == '' &&
52        cf.elements['<% $pre %>longitude'].value == '' ) {
53     cf.elements['<% $pre %>coord_auto'].value = 'Y';
54     changed = true;
55   }
56
57 % } #foreach $pre
58
59   // standardize if the old address wasn't clean
60   if ( cf.elements['old_ship_addr_clean'].value == '' ||
61        cf.elements['old_bill_addr_clean'].value == '' ) {
62
63     changed = true;
64
65   }
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 ) {
70       changed = true;
71       break;
72     }
73   }
74
75 % # If address hasn't been changed, auto-confirm the existing value of 
76 % # censustract so that we don't ask the user to confirm it again.
77
78   if ( !changed ) {
79     if ( address_info['same'] ) {
80       cf.elements['bill_censustract'].value =
81         address_info['bill_censustract'];
82     } else {
83       cf.elements['ship_censustract'].value =
84         address_info['ship_censustract'];
85     }
86   }
87
88 % if ( $conf->config('address_standardize_method') ) {
89   if ( changed ) {
90     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>';
91     overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
92     address_standardize(JSON.stringify(address_info), confirm_standardize);
93   }
94   else {
95 %   foreach my $pre (@prefixes) {
96     cf.elements['<% $pre %>addr_clean'].value = 'Y';
97 %   }
98     post_standardization();
99   }
100
101 % } else {
102
103   post_standardization();
104
105 % } # if address_standardize_method
106 }
107
108 var returned;
109
110 function confirm_standardize(arg) {
111   // contains 'old', which was what we sent, and 'new', which is what came
112   // back, including any errors
113   returned = JSON.parse(arg);
114
115   if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
116
117     replace_address(); // with the contents of returned['new']
118   
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 censustract)) {
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   if ( cf.elements['same'] && cf.elements['same'].checked ) {
173     cf.elements['bill_censustract'].value =
174       cf.elements['enter_censustract'].value;
175   } else {
176     cf.elements['ship_censustract'].value =
177       cf.elements['enter_censustract'].value;
178   }
179 % }
180   post_standardization();
181 }
182
183 function post_standardization() {
184
185 % if ( $conf->exists('enable_taxproducts') ) {
186
187   if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
188   {
189
190     var country_el = cf.elements['<% $taxpre %>country'];
191     var country = country_el.options[ country_el.selectedIndex ].value;
192     var geocode = cf.elements['geocode'].value;
193
194     if ( country == 'CA' || country == 'US' ) {
195
196       var state_el = cf.elements['<% $taxpre %>state'];
197       var state = state_el.options[ state_el.selectedIndex ].value;
198
199       var url = "<% $p %>/misc/choose_tax_location.html" +
200                   "?data_vendor=cch-zip" + 
201                   ";city="     + cf.elements['<% $taxpre %>city'].value +
202                   ";state="    + state + 
203                   ";zip="      + cf.elements['<% $taxpre %>zip'].value +
204                   ";country="  + country +
205                   ";geocode="  + geocode +
206                   ";formname=" + '<% $formname %>' +
207                   ";";
208
209       // popup a chooser
210       OLgetAJAX( url, update_geocode, 300 );
211
212     } else {
213
214       cf.elements['geocode'].value = 'DEFAULT';
215       <% $post_geocode %>;
216
217     }
218
219   } else {
220
221     cf.elements['geocode'].value = '';
222     <% $post_geocode %>;
223
224   }
225
226 % } else {
227
228   <% $post_geocode %>;
229
230 % }
231
232 }
233
234 function update_geocode() {
235
236   //yay closures
237   set_geocode = function (what) {
238
239     var cf = document.<% $formname %>;
240
241     //alert(what.options[what.selectedIndex].value);
242     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
243     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
244     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
245     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
246     cf.elements['geocode'].value  = argsHash['geocode'];
247     <% $post_geocode %>;
248
249   }
250
251   // popup a chooser
252
253   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
254
255 }
256
257 function setselect(el, value) {
258
259   for ( var s = 0; s < el.options.length; s++ ) {
260      if ( el.options[s].value == value ) {
261        el.selectedIndex = s;
262      }
263   }
264
265 }
266 <%init>
267
268 my %opt = @_;
269 my $conf = new FS::Conf;
270
271 my $withfirm = 1;
272 my $withcensus = 1;
273
274 my $formname =  $opt{form} || 'CustomerForm';
275 my $onlyship =  $opt{onlyship} || '';
276 #my $main_prefix =  $opt{main_prefix} || '';
277 #my $ship_prefix =  $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
278 # The prefixes are now 'ship_' and 'bill_'.
279 my $taxpre = 'bill_';
280 $taxpre = 'ship_' if ( $conf->exists('tax-ship_address') || $onlyship );
281 my $post_geocode = $opt{callback} || 'post_geocode();';
282 $withfirm = 0 if $opt{no_company};
283 $withcensus = 0 if $opt{no_census};
284
285 my @prefixes = ('ship_');
286 unshift @prefixes, 'bill_' unless $onlyship;
287
288 </%init>