1 function status_message(text, caption) {
2 text = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">' + text + '</FONT></B></P>';
3 caption = caption || 'Please wait...';
4 overlib(text, WIDTH, 444, HEIGHT, 168, CAPTION, caption, STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
7 function form_address_info() {
8 var cf = document.<% $formname %>;
10 var returnobj = { onlyship: <% $onlyship ? 1 : 0 %> };
12 returnobj['same'] = cf.elements['same'].checked;
15 % # not part of either address, really
16 returnobj['company'] = cf.elements['company'].value;
18 % if ( $withcensus ) {
19 % # "entered" censustract always goes with the ship_ address if there is one
20 returnobj['ship_censustract'] = cf.elements['enter_censustract'].value;
22 % for my $pre (@prefixes) {
23 if ( <% $pre eq 'ship_' ? 1 : 0 %> && returnobj['same'] ) {
24 % # special case: don't include any ship_ fields, and move the entered
25 % # censustract over to bill_.
26 returnobj['bill_censustract'] = returnobj['ship_censustract'];
27 delete returnobj['ship_censustract'];
30 % for my $field (qw(address1 address2 city state zip country)) {
31 returnobj['<% $pre %><% $field %>'] = cf.elements['<% $pre %><% $field %>'].value;
33 } // if returnobj['same']
39 function standardize_locations() {
41 var cf = document.<% $formname %>;
42 var address_info = form_address_info();
44 var changed = false; // have any of the address fields been changed?
46 // clear coord_auto fields if the user has changed the coordinates
47 % for my $pre (@prefixes) {
48 % for my $field ($pre.'latitude', $pre.'longitude') {
50 if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
51 cf.elements['<% $pre %>coord_auto'].value = '';
55 // but if the coordinates have been set to null, turn coord_auto on
57 if ( cf.elements['<% $pre %>latitude'].value == '' &&
58 cf.elements['<% $pre %>longitude'].value == '' ) {
59 cf.elements['<% $pre %>coord_auto'].value = 'Y';
65 // standardize if the old address wasn't clean
66 if ( cf.elements['old_ship_addr_clean'].value == '' ||
67 cf.elements['old_bill_addr_clean'].value == '' ) {
72 // or if it was clean but has been changed
73 for (var key in address_info) {
74 var old_el = cf.elements['old_'+key];
75 if ( old_el && address_info[key] != old_el.value ) {
81 % # If address hasn't been changed, auto-confirm the existing value of
82 % # censustract so that we don't ask the user to confirm it again.
85 if ( address_info['same'] ) {
86 cf.elements['bill_censustract'].value =
87 address_info['bill_censustract'];
89 cf.elements['ship_censustract'].value =
90 address_info['ship_censustract'];
94 % if ( $conf->config('address_standardize_method') ) {
96 status_message('Verifying address...');
97 address_standardize(JSON.stringify(address_info), confirm_standardize);
100 % foreach my $pre (@prefixes) {
101 cf.elements['<% $pre %>addr_clean'].value = 'Y';
103 post_standardization();
108 post_standardization();
110 % } # if address_standardize_method
115 function confirm_standardize(arg) {
116 // contains 'old', which was what we sent, and 'new', which is what came
117 // back, including any errors
118 returned = JSON.parse(arg);
120 if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
122 replace_address(); // with the contents of returned['new']
124 } else if ( returned['all_same'] ) {
126 // then all entered address fields are correct
127 // but we still need to set the lat/long fields and addr_clean
128 status_message('Verified');
133 var querystring = encodeURIComponent( JSON.stringify(returned) );
134 // confirmation popup: knows to call replace_address(),
135 // post_standardization(), or submit_abort() depending on the
138 '<%$p%>/misc/confirm-address_standardize.html',
141 overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY,
142 AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
143 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
150 function replace_address() {
152 var newaddr = returned['new'];
154 var cf = document.<% $formname %>;
155 % foreach my $pre (@prefixes) {
156 var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
157 var error = newaddr['<% $pre %>error'];
159 % foreach my $field (qw(address1 address2 city state zip addr_clean censustract)) {
160 cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
163 if ( cf.elements['<% $pre %>coord_auto'].value ) {
164 cf.elements['<% $pre %>latitude'].value = newaddr['<% $pre %>latitude'];
165 cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
167 % if ( $withcensus ) {
168 if ( clean && newaddr['<% $pre %>censustract'] ) {
169 cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
175 post_standardization();
179 function confirm_manual_address() {
180 %# not much to do in this case, just confirm the censustract
181 % if ( $withcensus ) {
182 var cf = document.<% $formname %>;
183 if ( cf.elements['same'] && cf.elements['same'].checked ) {
184 cf.elements['bill_censustract'].value =
185 cf.elements['enter_censustract'].value;
187 cf.elements['ship_censustract'].value =
188 cf.elements['enter_censustract'].value;
191 post_standardization();
194 function post_standardization() {
196 % if ( $conf->exists('enable_taxproducts') ) {
198 if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
201 var country_el = cf.elements['<% $taxpre %>country'];
202 var country = country_el.options[ country_el.selectedIndex ].value;
203 var geocode = cf.elements['geocode'].value;
205 if ( country == 'CA' || country == 'US' ) {
207 var state_el = cf.elements['<% $taxpre %>state'];
208 var state = state_el.options[ state_el.selectedIndex ].value;
210 var url = "<% $p %>/misc/choose_tax_location.html" +
211 "?data_vendor=cch-zip" +
212 ";city=" + cf.elements['<% $taxpre %>city'].value +
214 ";zip=" + cf.elements['<% $taxpre %>zip'].value +
215 ";country=" + country +
216 ";geocode=" + geocode +
217 ";formname=" + '<% $formname %>' +
221 OLgetAJAX( url, update_geocode, 300 );
225 cf.elements['geocode'].value = 'DEFAULT';
232 cf.elements['geocode'].value = '';
245 function update_geocode() {
248 set_geocode = function (what) {
250 var cf = document.<% $formname %>;
252 //alert(what.options[what.selectedIndex].value);
253 var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
254 cf.elements['<% $taxpre %>city'].value = argsHash['city'];
255 setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
256 cf.elements['<% $taxpre %>zip'].value = argsHash['zip'];
257 cf.elements['geocode'].value = argsHash['geocode'];
264 overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
268 function setselect(el, value) {
270 for ( var s = 0; s < el.options.length; s++ ) {
271 if ( el.options[s].value == value ) {
272 el.selectedIndex = s;
280 my $conf = new FS::Conf;
285 my $formname = $opt{form} || 'CustomerForm';
286 my $onlyship = $opt{onlyship} || '';
287 #my $main_prefix = $opt{main_prefix} || '';
288 #my $ship_prefix = $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
289 # The prefixes are now 'ship_' and 'bill_'.
290 my $taxpre = 'bill_';
291 $taxpre = 'ship_' if ( $conf->exists('tax-ship_address') || $onlyship );
292 my $post_geocode = $opt{callback} || 'post_geocode();';
293 $withfirm = 0 if $opt{no_company};
294 $withcensus = 0 if $opt{no_census};
296 my @prefixes = ('ship_');
297 unshift @prefixes, 'bill_' unless $onlyship;