1 function form_address_info() {
2 var cf = document.<% $formname %>;
4 var returnobj = { onlyship: <% $onlyship ? 1 : 0 %> };
6 returnobj['same'] = cf.elements['same'].checked;
9 % # not part of either address, really
10 returnobj['company'] = cf.elements['company'].value;
12 % if ( $withcensus ) {
13 % # "entered" censustract always goes with the ship_ address if there is one
14 returnobj['ship_censustract'] = cf.elements['enter_censustract'].value;
16 % for my $pre (@prefixes) {
17 if ( <% $pre eq 'ship_' ? 1 : 0 %> && returnobj['same'] ) {
18 % # special case: don't include any ship_ fields, and move the entered
19 % # censustract over to bill_.
20 returnobj['bill_censustract'] = returnobj['ship_censustract'];
21 delete returnobj['ship_censustract'];
24 % for my $field (qw(address1 address2 city state zip country)) {
25 returnobj['<% $pre %><% $field %>'] = cf.elements['<% $pre %><% $field %>'].value;
27 } // if returnobj['same']
33 function standardize_locations() {
35 var cf = document.<% $formname %>;
36 var address_info = form_address_info();
38 var changed = false; // have any of the address fields been changed?
40 // clear coord_auto fields if the user has changed the coordinates
41 % for my $pre (@prefixes) {
42 % for my $field ($pre.'latitude', $pre.'longitude') {
44 if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
45 cf.elements['<% $pre %>coord_auto'].value = '';
49 // but if the coordinates have been set to null, turn coord_auto on
51 if ( cf.elements['<% $pre %>latitude'].value == '' &&
52 cf.elements['<% $pre %>longitude'].value == '' ) {
53 cf.elements['<% $pre %>coord_auto'].value = 'Y';
59 // standardize if the old address wasn't clean
60 if ( cf.elements['old_ship_addr_clean'].value == '' ||
61 cf.elements['old_bill_addr_clean'].value == '' ) {
66 // or if it was clean but has been changed
67 for (var key in address_info) {
68 var old_el = cf.elements['old_'+key];
69 if ( old_el && address_info[key] != old_el.value ) {
75 % # If address hasn't been changed, auto-confirm the existing value of
76 % # censustract so that we don't ask the user to confirm it again.
79 if ( address_info['same'] ) {
80 cf.elements['bill_censustract'].value =
81 address_info['bill_censustract'];
83 cf.elements['ship_censustract'].value =
84 address_info['ship_censustract'];
88 % if ( $conf->config('address_standardize_method') ) {
90 var startup_msg = '<P STYLE="position:absolute; top:50%; margin-top:-1em; width:100%; text-align:center"><B><FONT SIZE="+1">Verifying address...</FONT></B></P>';
91 overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
92 address_standardize(JSON.stringify(address_info), confirm_standardize);
95 % foreach my $pre (@prefixes) {
96 cf.elements['<% $pre %>addr_clean'].value = 'Y';
98 post_standardization();
103 post_standardization();
105 % } # if address_standardize_method
110 function confirm_standardize(arg) {
111 // contains 'old', which was what we sent, and 'new', which is what came
112 // back, including any errors
113 returned = JSON.parse(arg);
115 if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
117 replace_address(); // with the contents of returned['new']
122 var querystring = encodeURIComponent( JSON.stringify(returned) );
123 // confirmation popup: knows to call replace_address(),
124 // post_standardization(), or submit_abort() depending on the
127 '<%$p%>/misc/confirm-address_standardize.html',
130 overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY,
131 AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
132 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
139 function replace_address() {
141 var newaddr = returned['new'];
143 var cf = document.<% $formname %>;
144 % foreach my $pre (@prefixes) {
145 var clean = newaddr['<% $pre %>addr_clean'] == 'Y';
146 var error = newaddr['<% $pre %>error'];
148 % foreach my $field (qw(address1 address2 city state zip addr_clean censustract)) {
149 cf.elements['<% $pre %><% $field %>'].value = newaddr['<% $pre %><% $field %>'];
152 if ( cf.elements['<% $pre %>coord_auto'].value ) {
153 cf.elements['<% $pre %>latitude'].value = newaddr['<% $pre %>latitude'];
154 cf.elements['<% $pre %>longitude'].value = newaddr['<% $pre %>longitude'];
156 % if ( $withcensus ) {
157 if ( clean && newaddr['<% $pre %>censustract'] ) {
158 cf.elements['<% $pre %>censustract'].value = newaddr['<% $pre %>censustract'];
164 post_standardization();
168 function confirm_manual_address() {
169 %# not much to do in this case, just confirm the censustract
170 % if ( $withcensus ) {
171 var cf = document.<% $formname %>;
172 if ( cf.elements['same'] && cf.elements['same'].checked ) {
173 cf.elements['bill_censustract'].value =
174 cf.elements['enter_censustract'].value;
176 cf.elements['ship_censustract'].value =
177 cf.elements['enter_censustract'].value;
180 post_standardization();
183 function post_standardization() {
185 % if ( $conf->exists('enable_taxproducts') ) {
187 if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
190 var country_el = cf.elements['<% $taxpre %>country'];
191 var country = country_el.options[ country_el.selectedIndex ].value;
192 var geocode = cf.elements['geocode'].value;
194 if ( country == 'CA' || country == 'US' ) {
196 var state_el = cf.elements['<% $taxpre %>state'];
197 var state = state_el.options[ state_el.selectedIndex ].value;
199 var url = "<% $p %>/misc/choose_tax_location.html" +
200 "?data_vendor=cch-zip" +
201 ";city=" + cf.elements['<% $taxpre %>city'].value +
203 ";zip=" + cf.elements['<% $taxpre %>zip'].value +
204 ";country=" + country +
205 ";geocode=" + geocode +
206 ";formname=" + '<% $formname %>' +
210 OLgetAJAX( url, update_geocode, 300 );
214 cf.elements['geocode'].value = 'DEFAULT';
221 cf.elements['geocode'].value = '';
234 function update_geocode() {
237 set_geocode = function (what) {
239 var cf = document.<% $formname %>;
241 //alert(what.options[what.selectedIndex].value);
242 var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
243 cf.elements['<% $taxpre %>city'].value = argsHash['city'];
244 setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
245 cf.elements['<% $taxpre %>zip'].value = argsHash['zip'];
246 cf.elements['geocode'].value = argsHash['geocode'];
253 overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
257 function setselect(el, value) {
259 for ( var s = 0; s < el.options.length; s++ ) {
260 if ( el.options[s].value == value ) {
261 el.selectedIndex = s;
269 my $conf = new FS::Conf;
274 my $formname = $opt{form} || 'CustomerForm';
275 my $onlyship = $opt{onlyship} || '';
276 #my $main_prefix = $opt{main_prefix} || '';
277 #my $ship_prefix = $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
278 # The prefixes are now 'ship_' and 'bill_'.
279 my $taxpre = 'bill_';
280 $taxpre = 'ship_' if ( $conf->exists('tax-ship_address') || $onlyship );
281 my $post_geocode = $opt{callback} || 'post_geocode();';
282 $withfirm = 0 if $opt{no_company};
283 $withcensus = 0 if $opt{no_census};
285 my @prefixes = ('ship_');
286 unshift @prefixes, 'bill_' unless $onlyship;