FCC form 477 reporting #4912
[freeside.git] / httemplate / edit / cust_main / bottomfixup.js
1 function bottomfixup(what) {
2
3   var layervars = new Array(
4     'payauto',
5     'payinfo', 'payinfo1', 'payinfo2', 'paytype',
6     'payname', 'paystate', 'exp_month', 'exp_year', 'paycvv',
7     'paystart_month', 'paystart_year', 'payissue',
8     'payip',
9     'paid'
10   );
11
12   var cf = document.CustomerForm;
13   var payby = cf.payby.options[cf.payby.selectedIndex].value;
14   for ( f=0; f < layervars.length; f++ ) {
15     var field = layervars[f];
16     copyelement( cf.elements[payby + '_' + field],
17                  cf.elements[field]
18                );
19   }
20
21   //this part does USPS address correction
22
23   // XXX should this be first and should we update the form fields that are
24   // displayed???
25
26   var cf = document.CustomerForm;
27
28   var state_el      = cf.elements['state'];
29   var ship_state_el = cf.elements['ship_state'];
30
31   //address_standardize(
32   var cust_main = new Array(
33     'company',  cf.elements['company'].value,
34     'address1', cf.elements['address1'].value,
35     'address2', cf.elements['address2'].value,
36     'city',     cf.elements['city'].value,
37     'state',    state_el.options[ state_el.selectedIndex ].value,
38     'zip',      cf.elements['zip'].value,
39
40     'ship_company',  cf.elements['ship_company'].value,
41     'ship_address1', cf.elements['ship_address1'].value,
42     'ship_address2', cf.elements['ship_address2'].value,
43     'ship_city',     cf.elements['ship_city'].value,
44     'ship_state',    ship_state_el.options[ ship_state_el.selectedIndex ].value,
45     'ship_zip',      cf.elements['ship_zip'].value
46   );
47
48   address_standardize( cust_main, update_address );
49
50 }
51
52 var standardize_address;
53
54 function update_address(arg) {
55
56   var argsHash = eval('(' + arg + ')');
57
58   var changed  = argsHash['address_standardized'];
59   var ship_changed = argsHash['ship_address_standardized'];
60   var error = argsHash['error'];
61   var ship_error = argsHash['ship_error'];
62   
63
64   //yay closures
65   standardize_address = function () {
66
67     var cf = document.CustomerForm;
68     var state_el      = cf.elements['state'];
69     var ship_state_el = cf.elements['ship_state'];
70
71     if ( changed ) {
72       cf.elements['company'].value  = argsHash['new_company'];
73       cf.elements['address1'].value = argsHash['new_address1'];
74       cf.elements['address2'].value = argsHash['new_address2'];
75       cf.elements['city'].value     = argsHash['new_city'];
76       setselect(cf.elements['state'], argsHash['new_state']);
77       cf.elements['zip'].value      = argsHash['new_zip'];
78     }
79
80     if ( ship_changed ) {
81       cf.elements['ship_company'].value  = argsHash['new_ship_company'];
82       cf.elements['ship_address1'].value = argsHash['new_ship_address1'];
83       cf.elements['ship_address2'].value = argsHash['new_ship_address2'];
84       cf.elements['ship_city'].value     = argsHash['new_ship_city'];
85       setselect(cf.elements['ship_state'], argsHash['new_ship_state']);
86       cf.elements['ship_zip'].value      = argsHash['new_ship_zip'];
87     }
88
89     post_standardization();
90
91   }
92
93
94
95   if ( changed || ship_changed ) {
96
97 %   if ( $conf->exists('cust_main-auto_standardize_address') ) {
98
99     standardize_address();
100
101 %   } else {
102
103     // popup a confirmation popup
104
105     var confirm_change =
106       '<CENTER><BR><B>Confirm address standardization</B><BR><BR>' +
107       '<TABLE>';
108     
109     if ( changed ) {
110
111       confirm_change = confirm_change + 
112         '<TR><TH>Entered billing address</TH>' +
113           '<TH>Standardized billing address</TH></TR>';
114         // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
115       
116       if ( argsHash['company'] || argsHash['new_company'] ) {
117         confirm_change = confirm_change +
118         '<TR><TD>' + argsHash['company'] +
119           '</TD><TD>' + argsHash['new_company'] + '</TD></TR>';
120       }
121       
122       confirm_change = confirm_change +
123         '<TR><TD>' + argsHash['address1'] +
124           '</TD><TD>' + argsHash['new_address1'] + '</TD></TR>' +
125         '<TR><TD>' + argsHash['address2'] +
126           '</TD><TD>' + argsHash['new_address2'] + '</TD></TR>' +
127         '<TR><TD>' + argsHash['city'] + ', ' + argsHash['state'] + '  ' + argsHash['zip'] +
128           '</TD><TD>' + argsHash['new_city'] + ', ' + argsHash['new_state'] + '  ' + argsHash['new_zip'] + '</TD></TR>' +
129           '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
130
131     }
132
133     if ( ship_changed ) {
134
135       confirm_change = confirm_change + 
136         '<TR><TH>Entered service address</TH>' +
137           '<TH>Standardized service address</TH></TR>';
138         // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
139       
140       if ( argsHash['ship_company'] || argsHash['new_ship_company'] ) {
141         confirm_change = confirm_change +
142         '<TR><TD>' + argsHash['ship_company'] +
143           '</TD><TD>' + argsHash['new_ship_company'] + '</TD></TR>';
144       }
145       
146       confirm_change = confirm_change +
147         '<TR><TD>' + argsHash['ship_address1'] +
148           '</TD><TD>' + argsHash['new_ship_address1'] + '</TD></TR>' +
149         '<TR><TD>' + argsHash['ship_address2'] +
150           '</TD><TD>' + argsHash['new_ship_address2'] + '</TD></TR>' +
151         '<TR><TD>' + argsHash['ship_city'] + ', ' + argsHash['ship_state'] + '  ' + argsHash['ship_zip'] +
152           '</TD><TD>' + argsHash['new_ship_city'] + ', ' + argsHash['new_ship_state'] + '  ' + argsHash['new_ship_zip'] + '</TD></TR>' +
153         '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
154
155     }
156
157     var addresses = 'address';
158     var height = 268;
159     if ( changed && ship_changed ) {
160       addresses = 'addresses';
161       height = 396; // #what
162     }
163
164     confirm_change = confirm_change +
165       '<TR><TD>' +
166         '<BUTTON TYPE="button" onClick="post_standardization();"><IMG SRC="<%$p%>images/error.png" ALT=""> Use entered ' + addresses + '</BUTTON>' + 
167       '</TD><TD>' +
168         '<BUTTON TYPE="button" onClick="standardize_address();"><IMG SRC="<%$p%>images/tick.png" ALT=""> Use standardized ' + addresses + '</BUTTON>' + 
169       '</TD></TR>' +
170       '<TR><TD COLSPAN=2 ALIGN="center">' +
171         '<BUTTON TYPE="button" onClick="document.CustomerForm.submitButton.disabled=false; parent.cClick();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
172         
173       '</TABLE></CENTER>';
174
175     overlib( confirm_change, CAPTION, 'Confirm address standardization', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, height, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
176
177 %   }
178
179   } else {
180
181     post_standardization();
182
183   }
184
185
186 }
187
188 function post_standardization() {
189
190   var cf = document.CustomerForm;
191
192 % if ( $conf->exists('enable_taxproducts') ) {
193
194   if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
195   {
196
197     var country_el = cf.elements['<% $taxpre %>country'];
198     var country = country_el.options[ country_el.selectedIndex ].value;
199
200     if ( country == 'CA' || country == 'US' ) {
201
202       var state_el = cf.elements['<% $taxpre %>state'];
203       var state = state_el.options[ state_el.selectedIndex ].value;
204
205       var url = "cust_main/choose_tax_location.html" +
206                   "?data_vendor=cch-zip" + 
207                   ";city="    + cf.elements['<% $taxpre %>city'].value +
208                   ";state="   + state + 
209                   ";zip="     + cf.elements['<% $taxpre %>zip'].value +
210                   ";country=" + country +
211                   ";";
212
213       // popup a chooser
214       OLgetAJAX( url, update_geocode, 300 );
215
216     } else {
217
218       cf.elements['geocode'].value = 'DEFAULT';
219       post_geocode();
220
221     }
222
223   } else {
224
225     post_geocode();
226
227   }
228
229 % } else {
230
231   post_geocode();
232
233 % }
234
235 }
236
237 function post_geocode() {
238
239 % if ( $conf->exists('cust_main-require_censustract') ) {
240
241   //alert('fetch census tract data');
242   var cf = document.CustomerForm;
243   var state_el = cf.elements['ship_state'];
244   var census_data = new Array(
245     'year',    '2008', // from config value?
246     'address', cf.elements['ship_address1'].value,
247     'city',    cf.elements['ship_city'].value,
248     'state',   state_el.options[ state_el.selectedIndex ].value,
249     'zip',     cf.elements['ship_zip'].value
250   );
251
252   censustract( census_data, update_censustract );
253
254 % }else{
255
256   document.CustomerForm.submit();
257
258 % }
259
260 }
261
262 function update_geocode() {
263
264   //yay closures
265   set_geocode = function (what) {
266
267     var cf = document.CustomerForm;
268
269     //alert(what.options[what.selectedIndex].value);
270     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
271     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
272     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
273     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
274     cf.elements['geocode'].value  = argsHash['geocode'];
275     post_geocode();
276
277   }
278
279   // popup a chooser
280
281   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
282
283 }
284
285 var set_censustract;
286
287 function update_censustract(arg) {
288
289   var argsHash = eval('(' + arg + ')');
290
291   var cf = document.CustomerForm;
292
293   var msacode    = argsHash['msacode'];
294   var statecode  = argsHash['statecode'];
295   var countycode = argsHash['countycode'];
296   var tractcode  = argsHash['tractcode'];
297   var error      = argsHash['error'];
298   
299   set_censustract = function () {
300
301     cf.elements['censustract'].value =
302       document.forms.popupcensustract.elements.censustract.value;
303     cf.submit();
304
305   }
306
307   if (error) {
308     // popup an entry dialog
309
310     var choose_censustract =
311       '<CENTER><BR><B>Enter census tract</B><BR><BR>' + 
312       '<FORM name="popupcensustract">' +
313       '<TABLE>';
314     
315     choose_censustract = choose_censustract + 
316       '<TR><TH>Census Tract: </TH>' +
317         '<TD><INPUT NAME="censustract" ID="censustract"></TD>' +
318       '</TR><TR>' +
319         '<TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
320       
321     choose_censustract = choose_censustract + 
322       '<TR><TD>' +
323         '<BUTTON TYPE="button" onClick="set_censustract();"><IMG SRC="<%$p%>images/tick.png" ALT="">Submit census tract</BUTTON>' + 
324       '</TD><TD>' +
325         '<BUTTON TYPE="button" onClick="document.CustomerForm.submitButton.disabled=false; parent.cClick();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
326       '</TABLE></FORM></CENTER>';
327
328       overlib( choose_censustract, CAPTION, 'Choose a census tract', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
329
330       setTimeout("document.forms.popupcensustract.elements.censustract.focus()",1);
331   } else {
332
333     cf.elements['censustract'].value =
334       new String(statecode)  +
335       new String(countycode) +
336       new String(tractcode);
337     cf.submit();
338
339   }
340
341 }
342
343 function copyelement(from, to) {
344   if ( from == undefined ) {
345     to.value = '';
346   } else if ( from.type == 'select-one' ) {
347     to.value = from.options[from.selectedIndex].value;
348     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
349   } else if ( from.type == 'checkbox' ) {
350     if ( from.checked ) {
351       to.value = from.value;
352     } else {
353       to.value = '';
354     }
355   } else {
356     if ( from.value == undefined ) {
357       to.value = '';
358     } else {
359       to.value = from.value;
360     }
361   }
362   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
363 }
364
365 function setselect(el, value) {
366
367   for ( var s = 0; s < el.options.length; s++ ) {
368      if ( el.options[s].value == value ) {
369        el.selectedIndex = s;
370      }
371   }
372
373 }
374 <%init>
375
376 my $conf = new FS::Conf;
377
378 my $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : '';
379
380 </%init>