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;
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';
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 %> ) {
81 if ( address_info['same'] ) {
82 cf.elements['bill_censustract'].value =
83 address_info['bill_censustract'];
85 cf.elements['ship_censustract'].value =
86 address_info['ship_censustract'];
90 % if ( $conf->config('address_standardize_method') ) {
92 status_message('Verifying address...');
93 address_standardize(JSON.stringify(address_info), confirm_standardize);
96 % foreach my $pre (@prefixes) {
97 cf.elements['<% $pre %>addr_clean'].value = 'Y';
99 post_standardization();
104 post_standardization();
106 % } # if address_standardize_method
111 function confirm_standardize(arg) {
112 // contains 'old', which was what we sent, and 'new', which is what came
113 // back, including any errors
114 returned = JSON.parse(arg);
116 if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
118 replace_address(); // with the contents of returned['new']
120 } else if ( returned['all_same'] ) {
122 // then all entered address fields are correct
123 // but we still need to set the lat/long fields and addr_clean
124 status_message('Verified');
129 var querystring = encodeURIComponent( JSON.stringify(returned) );
130 // confirmation popup: knows to call replace_address(),
131 // post_standardization(), or submit_abort() depending on the
134 '<%$p%>/misc/confirm-address_standardize.html',
137 overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY,
138 AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
139 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
146 function replace_address() {
148 var newaddr = returned['new'];
150 var cf = document.<% $formname %>;
151 % foreach my $pre (@prefixes) {
152 var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
153 var error = newaddr['<% $pre %>error'];
155 % foreach my $field (qw(address1 address2 city state zip addr_clean censustract)) {
156 cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
159 if ( cf.elements['<% $pre %>coord_auto'].value ) {
160 cf.elements['<% $pre %>latitude'].value = newaddr['<% $pre %>latitude'];
161 cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
163 % if ( $withcensus ) {
164 if ( clean && newaddr['<% $pre %>censustract'] ) {
165 cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
171 post_standardization();
175 function confirm_manual_address() {
176 %# not much to do in this case, just confirm the censustract
177 % if ( $withcensus ) {
178 var cf = document.<% $formname %>;
179 if ( cf.elements['same'] && cf.elements['same'].checked ) {
180 cf.elements['bill_censustract'].value =
181 cf.elements['enter_censustract'].value;
183 cf.elements['ship_censustract'].value =
184 cf.elements['enter_censustract'].value;
187 post_standardization();
190 function post_standardization() {
192 % if ( $conf->exists('enable_taxproducts') ) {
194 var cf = document.<% $formname %>;
196 if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
199 var country_el = cf.elements['<% $taxpre %>country'];
200 var country = country_el.options[ country_el.selectedIndex ].value;
201 var geocode = cf.elements['bill_geocode'].value;
203 if ( country == 'CA' || country == 'US' ) {
205 var state_el = cf.elements['<% $taxpre %>state'];
206 var state = state_el.options[ state_el.selectedIndex ].value;
208 var url = "<% $p %>/misc/choose_tax_location.html" +
209 "?data_vendor=cch-zip" +
210 ";city=" + cf.elements['<% $taxpre %>city'].value +
212 ";zip=" + cf.elements['<% $taxpre %>zip'].value +
213 ";country=" + country +
214 ";geocode=" + geocode +
215 ";formname=" + '<% $formname %>' +
219 OLgetAJAX( url, update_geocode, 300 );
223 cf.elements['bill_geocode'].value = 'DEFAULT';
230 cf.elements['bill_geocode'].value = '';
243 function update_geocode() {
246 set_geocode = function (what) {
248 var cf = document.<% $formname %>;
250 //alert(what.options[what.selectedIndex].value);
251 var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
252 cf.elements['<% $taxpre %>city'].value = argsHash['city'];
253 setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
254 cf.elements['<% $taxpre %>zip'].value = argsHash['zip'];
255 cf.elements['bill_geocode'].value = argsHash['geocode'];
262 overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
266 function setselect(el, value) {
268 for ( var s = 0; s < el.options.length; s++ ) {
269 if ( el.options[s].value == value ) {
270 el.selectedIndex = s;
278 my $conf = new FS::Conf;
280 my $withfirm = $opt{'with_firm'} ? 1 : 0;
281 my $withcensus = $opt{'with_census'} ? 1 : 0;
284 my $billship = $opt{'billship'} ? 1 : 0; # whether to have bill_ and ship_ prefixes
287 @prefixes = qw(bill_ ship_);
288 $taxpre = $conf->exists('tax-ship_address') ? 'ship_' : 'bill_';
291 my $formname = $opt{form} || 'CustomerForm';
292 my $post_geocode = $opt{callback} || 'post_geocode();';