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 = { billship: <% $billship %> };
12 returnobj['same'] = cf.elements['same'].checked;
14 % if ( $withcensus ) {
15 % # "entered" censustract always goes with the ship_ address if there is one
17 returnobj['ship_censustract'] = cf.elements['enter_censustract'].value;
18 % } else { # there's only a package address, so it's just "censustract"
19 returnobj['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';
62 // standardize if the old address wasn't clean
63 if ( cf.elements['<% $pre %>addr_clean'].value == '' ) {
68 // or if it was clean but has been changed
69 for (var key in address_info) {
70 var old_el = cf.elements['old_'+key];
71 if ( old_el && address_info[key] != old_el.value ) {
77 % # If address hasn't been changed, auto-confirm the existing value of
78 % # censustract so that we don't ask the user to confirm it again.
80 if ( !changed && <% $withcensus %> ) {
82 if ( address_info['same'] ) {
83 cf.elements['bill_censustract'].value =
84 address_info['bill_censustract'];
86 cf.elements['ship_censustract'].value =
87 address_info['ship_censustract'];
90 cf.elements['censustract'].value =
91 address_info['censustract'];
95 % if ( $conf->config('address_standardize_method') ) {
97 status_message('Verifying address...');
98 address_standardize(JSON.stringify(address_info), confirm_standardize);
101 % foreach my $pre (@prefixes) {
102 cf.elements['<% $pre %>addr_clean'].value = 'Y';
104 post_standardization();
109 post_standardization();
111 % } # if address_standardize_method
116 function confirm_standardize(arg) {
117 // contains 'old', which was what we sent, and 'new', which is what came
118 // back, including any errors
119 returned = JSON.parse(arg);
121 if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
123 replace_address(); // with the contents of returned['new']
125 } else if ( returned['all_same'] ) {
127 // then all entered address fields are correct
128 // but we still need to set the lat/long fields and addr_clean
129 status_message('Verified');
134 var querystring = encodeURIComponent( JSON.stringify(returned) );
135 // confirmation popup: knows to call replace_address(),
136 // post_standardization(), or submit_abort() depending on the
139 '<%$p%>/misc/confirm-address_standardize.html',
142 overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY,
143 AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
144 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
151 function replace_address() {
153 var newaddr = returned['new'];
155 var cf = document.<% $formname %>;
156 % foreach my $pre (@prefixes) {
157 var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
158 var error = newaddr['<% $pre %>error'];
160 % foreach my $field (qw(address1 address2 city state zip addr_clean censustract)) {
161 cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
164 if ( cf.elements['<% $pre %>coord_auto'].value ) {
165 cf.elements['<% $pre %>latitude'].value = newaddr['<% $pre %>latitude'];
166 cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
168 % if ( $withcensus ) {
169 if ( clean && newaddr['<% $pre %>censustract'] ) {
170 cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
176 post_standardization();
180 function confirm_manual_address() {
181 %# not much to do in this case, just confirm the censustract
182 % if ( $withcensus ) {
183 var cf = document.<% $formname %>;
185 if ( cf.elements['same'] && cf.elements['same'].checked ) {
186 cf.elements['bill_censustract'].value =
187 cf.elements['enter_censustract'].value;
189 cf.elements['ship_censustract'].value =
190 cf.elements['enter_censustract'].value;
193 cf.elements['censustract'].value = cf.elements['enter_censustract'].value;
196 post_standardization();
199 function post_standardization() {
201 % if ( $conf->exists('enable_taxproducts') ) {
203 var cf = document.<% $formname %>;
205 if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
208 var country_el = cf.elements['<% $taxpre %>country'];
209 var country = country_el.options[ country_el.selectedIndex ].value;
210 var geocode = cf.elements['<% $taxpre %>geocode'].value;
212 if ( country == 'CA' || country == 'US' ) {
214 var state_el = cf.elements['<% $taxpre %>state'];
215 var state = state_el.options[ state_el.selectedIndex ].value;
217 var url = "<% $p %>/misc/choose_tax_location.html" +
218 "?data_vendor=cch-zip" +
219 ";city=" + cf.elements['<% $taxpre %>city'].value +
221 ";zip=" + cf.elements['<% $taxpre %>zip'].value +
222 ";country=" + country +
223 ";geocode=" + geocode +
224 ";formname=" + '<% $formname %>' +
228 OLgetAJAX( url, update_geocode, 300 );
232 cf.elements['<% $taxpre %>geocode'].value = 'DEFAULT';
239 cf.elements['<% $taxpre %>geocode'].value = '';
252 function update_geocode() {
255 set_geocode = function (what) {
257 var cf = document.<% $formname %>;
259 //alert(what.options[what.selectedIndex].value);
260 var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
261 cf.elements['<% $taxpre %>city'].value = argsHash['city'];
262 setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
263 cf.elements['<% $taxpre %>zip'].value = argsHash['zip'];
264 cf.elements['<% $taxpre %>geocode'].value = argsHash['geocode'];
271 overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
275 function setselect(el, value) {
277 for ( var s = 0; s < el.options.length; s++ ) {
278 if ( el.options[s].value == value ) {
279 el.selectedIndex = s;
287 my $conf = new FS::Conf;
289 my $withcensus = $opt{'with_census'} ? 1 : 0;
292 my $billship = $opt{'billship'} ? 1 : 0; # whether to have bill_ and ship_ prefixes
294 # probably should just geocode both addresses, since either one could
295 # be a package address in the future
297 @prefixes = qw(bill_ ship_);
298 $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : 'bill_';
301 my $formname = $opt{form} || 'CustomerForm';
302 my $post_geocode = $opt{callback} || 'post_geocode();';