ask for help assignting geocode more often
[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   }
90
91   var cf = document.CustomerForm;
92
93 % if ( $conf->exists('enable_taxproducts') ) {
94
95   if ( <% $taxpre %>error ||
96        new String(argsHash['new_<% $taxpre %>zip']).length < 10 )
97   {
98
99     var country_el = cf.elements['<% $taxpre %>country'];
100     var country = country_el.options[ country_el.selectedIndex ].value;
101
102     if ( country == 'CA' || country == 'US' ) {
103
104       var state_el = cf.elements['<% $taxpre %>state'];
105       var state = state_el.options[ state_el.selectedIndex ].value;
106
107       var url = "cust_main/choose_tax_location.html" +
108                   "?data_vendor=cch-zip" + 
109                   ";city="    + cf.elements['<% $taxpre %>city'].value +
110                   ";state="   + state + 
111                   ";zip="     + cf.elements['<% $taxpre %>zip'].value +
112                   ";country=" + country +
113                   ";";
114
115       // popup a chooser
116       OLgetAJAX( url, update_geocode, 300 );
117
118     } else {
119
120       cf.elements['geocode'].value = 'DEFAULT';
121       cf.submit();
122
123     }
124
125   } else
126
127 % }
128
129   if ( changed || ship_changed ) {
130
131 %   if ( $conf->exists('cust_main-auto_standardize_address') ) {
132
133     standardize_address();
134     cf.submit();
135
136 %   } else {
137
138     // popup a confirmation popup
139
140     var confirm_change =
141       '<CENTER><BR><B>Confirm address standardization</B><BR><BR>' +
142       '<TABLE>';
143     
144     if ( changed ) {
145
146       confirm_change = confirm_change + 
147         '<TR><TH>Entered billing address</TH>' +
148           '<TH>Standardized billing address</TH></TR>';
149         // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
150       
151       if ( argsHash['company'] || argsHash['new_company'] ) {
152         confirm_change = confirm_change +
153         '<TR><TD>' + argsHash['company'] +
154           '</TD><TD>' + argsHash['new_company'] + '</TD></TR>';
155       }
156       
157       confirm_change = confirm_change +
158         '<TR><TD>' + argsHash['address1'] +
159           '</TD><TD>' + argsHash['new_address1'] + '</TD></TR>' +
160         '<TR><TD>' + argsHash['address2'] +
161           '</TD><TD>' + argsHash['new_address2'] + '</TD></TR>' +
162         '<TR><TD>' + argsHash['city'] + ', ' + argsHash['state'] + '  ' + argsHash['zip'] +
163           '</TD><TD>' + argsHash['new_city'] + ', ' + argsHash['new_state'] + '  ' + argsHash['new_zip'] + '</TD></TR>' +
164           '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
165
166     }
167
168     if ( ship_changed ) {
169
170       confirm_change = confirm_change + 
171         '<TR><TH>Entered service address</TH>' +
172           '<TH>Standardized service address</TH></TR>';
173         // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
174       
175       if ( argsHash['ship_company'] || argsHash['new_ship_company'] ) {
176         confirm_change = confirm_change +
177         '<TR><TD>' + argsHash['ship_company'] +
178           '</TD><TD>' + argsHash['new_ship_company'] + '</TD></TR>';
179       }
180       
181       confirm_change = confirm_change +
182         '<TR><TD>' + argsHash['ship_address1'] +
183           '</TD><TD>' + argsHash['new_ship_address1'] + '</TD></TR>' +
184         '<TR><TD>' + argsHash['ship_address2'] +
185           '</TD><TD>' + argsHash['new_ship_address2'] + '</TD></TR>' +
186         '<TR><TD>' + argsHash['ship_city'] + ', ' + argsHash['ship_state'] + '  ' + argsHash['ship_zip'] +
187           '</TD><TD>' + argsHash['new_ship_city'] + ', ' + argsHash['new_ship_state'] + '  ' + argsHash['new_ship_zip'] + '</TD></TR>' +
188         '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
189
190     }
191
192     var addresses = 'address';
193     var height = 268;
194     if ( changed && ship_changed ) {
195       addresses = 'addresses';
196       height = 396; // #what
197     }
198
199     confirm_change = confirm_change +
200       '<TR><TD>' +
201         '<BUTTON TYPE="button" onClick="document.CustomerForm.submit();"><IMG SRC="<%$p%>images/error.png" ALT=""> Use entered ' + addresses + '</BUTTON>' + 
202       '</TD><TD>' +
203         '<BUTTON TYPE="button" onClick="standardize_address(); document.CustomerForm.submit();"><IMG SRC="<%$p%>images/tick.png" ALT=""> Use standardized ' + addresses + '</BUTTON>' + 
204       '</TD></TR>' +
205       '<TR><TD COLSPAN=2 ALIGN="center">' +
206         '<BUTTON TYPE="button" onClick="document.CustomerForm.submitButton.disabled=false; parent.cClick();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
207         
208       '</TABLE></CENTER>';
209
210     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 );
211
212 %   }
213
214   } else {
215
216     cf.submit();
217
218   }
219
220 }
221
222 function update_geocode() {
223
224   //yay closures
225   set_geocode = function (what) {
226
227     var cf = document.CustomerForm;
228
229     //alert(what.options[what.selectedIndex].value);
230     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
231     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
232     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
233     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
234     cf.elements['geocode'].value  = argsHash['geocode'];
235
236   }
237
238   // popup a chooser
239
240   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
241
242 }
243
244 function copyelement(from, to) {
245   if ( from == undefined ) {
246     to.value = '';
247   } else if ( from.type == 'select-one' ) {
248     to.value = from.options[from.selectedIndex].value;
249     //alert(from + " (" + from.type + "): " + to.name + " => (" + from.selectedIndex + ") " + to.value);
250   } else if ( from.type == 'checkbox' ) {
251     if ( from.checked ) {
252       to.value = from.value;
253     } else {
254       to.value = '';
255     }
256   } else {
257     if ( from.value == undefined ) {
258       to.value = '';
259     } else {
260       to.value = from.value;
261     }
262   }
263   //alert(from + " (" + from.type + "): " + to.name + " => " + to.value);
264 }
265
266 function setselect(el, value) {
267
268   for ( var s = 0; s < el.options.length; s++ ) {
269      if ( el.options[s].value == value ) {
270        el.selectedIndex = s;
271      }
272   }
273
274 }
275 <%init>
276
277 my $conf = new FS::Conf;
278
279 my $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : '';
280
281 </%init>