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