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