Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / httemplate / elements / standardize_locations.js
1 function standardize_locations() {
2
3   var cf = document.<% $formname %>;
4
5   var state_el      = cf.elements['<% $main_prefix %>state'];
6   var ship_state_el = cf.elements['<% $ship_prefix %>state'];
7
8   var address_info = new Array(
9 % if ( $onlyship ) {
10     'onlyship', 1,
11 % } else {
12 %   if ( $withfirm ) {
13     'company',  cf.elements['company'].value,
14 %   }
15     'address1', cf.elements['<% $main_prefix %>address1'].value,
16     'address2', cf.elements['<% $main_prefix %>address2'].value,
17     'city',     cf.elements['<% $main_prefix %>city'].value,
18     'state',    state_el.options[ state_el.selectedIndex ].value,
19     'zip',      cf.elements['<% $main_prefix %>zip'].value,
20 % }
21     'ship_address1', cf.elements['<% $ship_prefix %>address1'].value,
22     'ship_address2', cf.elements['<% $ship_prefix %>address2'].value,
23     'ship_city',     cf.elements['<% $ship_prefix %>city'].value,
24     'ship_state',    ship_state_el.options[ ship_state_el.selectedIndex ].value,
25     'ship_zip',      cf.elements['<% $ship_prefix %>zip'].value
26   );
27
28   address_standardize( address_info, update_address );
29
30 }
31
32 var standardize_address;
33
34 function update_address(arg) {
35
36   var argsHash = eval('(' + arg + ')');
37
38   var changed  = argsHash['address_standardized'];
39   var ship_changed = argsHash['ship_address_standardized'];
40   var error = argsHash['error'];
41   var ship_error = argsHash['ship_error'];
42   
43
44   //yay closures
45   standardize_address = function () {
46
47     var cf = document.<% $formname %>;
48     var state_el      = cf.elements['<% $main_prefix %>state'];
49     var ship_state_el = cf.elements['<% $ship_prefix %>state'];
50
51 % if ( !$onlyship ) {
52     if ( changed ) {
53 %   if ( $withfirm ) {
54       cf.elements['<% $main_prefix %>company'].value  = argsHash['new_company'];
55 %   }
56       cf.elements['<% $main_prefix %>address1'].value = argsHash['new_address1'];
57       cf.elements['<% $main_prefix %>address2'].value = argsHash['new_address2'];
58       cf.elements['<% $main_prefix %>city'].value     = argsHash['new_city'];
59       setselect(cf.elements['<% $main_prefix %>state'], argsHash['new_state']);
60       cf.elements['<% $main_prefix %>zip'].value      = argsHash['new_zip'];
61     }
62 % }
63
64     if ( ship_changed ) {
65 % if ( $withfirm ) {
66       cf.elements['<% $ship_prefix %>company'].value  = argsHash['new_ship_company'];
67 % }
68       cf.elements['<% $ship_prefix %>address1'].value = argsHash['new_ship_address1'];
69       cf.elements['<% $ship_prefix %>address2'].value = argsHash['new_ship_address2'];
70       cf.elements['<% $ship_prefix %>city'].value     = argsHash['new_ship_city'];
71       setselect(cf.elements['<% $ship_prefix %>state'], argsHash['new_ship_state']);
72       cf.elements['<% $ship_prefix %>zip'].value      = argsHash['new_ship_zip'];
73     }
74
75     post_standardization();
76
77   }
78
79
80
81   if ( changed || ship_changed ) {
82
83 %   if ( $conf->exists('cust_main-auto_standardize_address') ) {
84
85     standardize_address();
86
87 %   } else {
88
89     // popup a confirmation popup
90
91     var confirm_change =
92       '<CENTER><BR><B>Confirm address standardization</B><BR><BR>' +
93       '<TABLE>';
94     
95     if ( changed ) {
96
97       confirm_change = confirm_change + 
98         '<TR><TH>Entered billing address</TH>' +
99           '<TH>Standardized billing address</TH></TR>';
100         // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
101       
102       if ( argsHash['company'] || argsHash['new_company'] ) {
103         confirm_change = confirm_change +
104         '<TR><TD>' + argsHash['company'] +
105           '</TD><TD>' + argsHash['new_company'] + '</TD></TR>';
106       }
107       
108       confirm_change = confirm_change +
109         '<TR><TD>' + argsHash['address1'] +
110           '</TD><TD>' + argsHash['new_address1'] + '</TD></TR>' +
111         '<TR><TD>' + argsHash['address2'] +
112           '</TD><TD>' + argsHash['new_address2'] + '</TD></TR>' +
113         '<TR><TD>' + argsHash['city'] + ', ' + argsHash['state'] + '  ' + argsHash['zip'] +
114           '</TD><TD>' + argsHash['new_city'] + ', ' + argsHash['new_state'] + '  ' + argsHash['new_zip'] + '</TD></TR>' +
115           '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
116
117     }
118
119     if ( ship_changed ) {
120
121       confirm_change = confirm_change + 
122         '<TR><TH>Entered service address</TH>' +
123           '<TH>Standardized service address</TH></TR>';
124         // + '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
125       
126       if ( argsHash['ship_company'] || argsHash['new_ship_company'] ) {
127         confirm_change = confirm_change +
128         '<TR><TD>' + argsHash['ship_company'] +
129           '</TD><TD>' + argsHash['new_ship_company'] + '</TD></TR>';
130       }
131       
132       confirm_change = confirm_change +
133         '<TR><TD>' + argsHash['ship_address1'] +
134           '</TD><TD>' + argsHash['new_ship_address1'] + '</TD></TR>' +
135         '<TR><TD>' + argsHash['ship_address2'] +
136           '</TD><TD>' + argsHash['new_ship_address2'] + '</TD></TR>' +
137         '<TR><TD>' + argsHash['ship_city'] + ', ' + argsHash['ship_state'] + '  ' + argsHash['ship_zip'] +
138           '</TD><TD>' + argsHash['new_ship_city'] + ', ' + argsHash['new_ship_state'] + '  ' + argsHash['new_ship_zip'] + '</TD></TR>' +
139         '<TR><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>';
140
141     }
142
143     var addresses = 'address';
144     var height = 268;
145     if ( changed && ship_changed ) {
146       addresses = 'addresses';
147       height = 396; // #what
148     }
149
150     confirm_change = confirm_change +
151       '<TR><TD>' +
152         '<BUTTON TYPE="button" onClick="post_standardization();"><IMG SRC="<%$p%>images/error.png" ALT=""> Use entered ' + addresses + '</BUTTON>' + 
153       '</TD><TD>' +
154         '<BUTTON TYPE="button" onClick="standardize_address();"><IMG SRC="<%$p%>images/tick.png" ALT=""> Use standardized ' + addresses + '</BUTTON>' + 
155       '</TD></TR>' +
156       '<TR><TD COLSPAN=2 ALIGN="center">' +
157         '<BUTTON TYPE="button" onClick="document.<% $formname %>.submitButton.disabled=false; parent.cClick();"><IMG SRC="<%$p%>images/cross.png" ALT=""> Cancel submission</BUTTON></TD></TR>' +
158         
159       '</TABLE></CENTER>';
160
161     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 );
162
163 %   }
164
165   } else {
166
167     post_standardization();
168
169   }
170
171
172 }
173
174 function post_standardization() {
175
176   var cf = document.<% $formname %>;
177
178 % if ( $conf->exists('enable_taxproducts') ) {
179
180   if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
181   {
182
183     var country_el = cf.elements['<% $taxpre %>country'];
184     var country = country_el.options[ country_el.selectedIndex ].value;
185     var geocode = cf.elements['geocode'].value;
186
187     if ( country == 'CA' || country == 'US' ) {
188
189       var state_el = cf.elements['<% $taxpre %>state'];
190       var state = state_el.options[ state_el.selectedIndex ].value;
191
192       var url = "<% $p %>/misc/choose_tax_location.html" +
193                   "?data_vendor=cch-zip" + 
194                   ";city="     + cf.elements['<% $taxpre %>city'].value +
195                   ";state="    + state + 
196                   ";zip="      + cf.elements['<% $taxpre %>zip'].value +
197                   ";country="  + country +
198                   ";geocode="  + geocode +
199                   ";formname=" + '<% $formname %>' +
200                   ";";
201
202       // popup a chooser
203       OLgetAJAX( url, update_geocode, 300 );
204
205     } else {
206
207       cf.elements['geocode'].value = 'DEFAULT';
208       <% $post_geocode %>;
209
210     }
211
212   } else {
213
214     cf.elements['geocode'].value = '';
215     <% $post_geocode %>;
216
217   }
218
219 % } else {
220
221   <% $post_geocode %>;
222
223 % }
224
225 }
226
227 function update_geocode() {
228
229   //yay closures
230   set_geocode = function (what) {
231
232     var cf = document.<% $formname %>;
233
234     //alert(what.options[what.selectedIndex].value);
235     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
236     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
237     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
238     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
239     cf.elements['geocode'].value  = argsHash['geocode'];
240     <% $post_geocode %>;
241
242   }
243
244   // popup a chooser
245
246   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
247
248 }
249
250 function setselect(el, value) {
251
252   for ( var s = 0; s < el.options.length; s++ ) {
253      if ( el.options[s].value == value ) {
254        el.selectedIndex = s;
255      }
256   }
257
258 }
259 <%init>
260
261 my %opt = @_;
262 my $conf = new FS::Conf;
263
264 my $withfirm = 1;
265
266 my $formname =  $opt{form} || 'CustomerForm';
267 my $onlyship =  $opt{onlyship} || '';
268 my $main_prefix =  $opt{main_prefix} || '';
269 my $ship_prefix =  $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
270 my $taxpre = $main_prefix;
271 $taxpre = $ship_prefix if ( $conf->exists('tax-ship_address') || $onlyship );
272 my $post_geocode = $opt{callback} || 'post_geocode();';
273 $withfirm = 0 if $opt{no_company};
274
275 </%init>