1 function form_address_info() {
2 var cf = document.<% $formname %>;
3 var state_el = cf.elements['<% $main_prefix %>state'];
4 var ship_state_el = cf.elements['<% $ship_prefix %>state'];
10 'company', cf.elements['company'].value,
12 'address1': cf.elements['<% $main_prefix %>address1'].value,
13 'address2': cf.elements['<% $main_prefix %>address2'].value,
14 'city': cf.elements['<% $main_prefix %>city'].value,
15 'state': state_el.options[ state_el.selectedIndex ].value,
16 'zip': cf.elements['<% $main_prefix %>zip'].value,
17 'country': cf.elements['<% $main_prefix %>country'].value,
19 % if ( $withcensus ) {
20 'ship_censustract': cf.elements['enter_censustract'].value,
22 'ship_address1': cf.elements['<% $ship_prefix %>address1'].value,
23 'ship_address2': cf.elements['<% $ship_prefix %>address2'].value,
24 'ship_city': cf.elements['<% $ship_prefix %>city'].value,
25 'ship_state': ship_state_el.options[ ship_state_el.selectedIndex ].value,
26 'ship_zip': cf.elements['<% $ship_prefix %>zip'].value,
27 'ship_country': cf.elements['<% $ship_prefix %>country'].value,
29 'same': cf.elements['same'].checked
34 function standardize_locations() {
36 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>';
37 overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
38 var cf = document.<% $formname %>;
39 var address_info = form_address_info();
41 var changed = false; // have any of the address fields been changed?
43 // clear coord_auto fields if the user has changed the coordinates
44 % for my $pre ($ship_prefix, $onlyship ? () : $main_prefix) {
45 % for my $field ($pre.'latitude', $pre.'longitude') {
47 if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
48 cf.elements['<% $pre %>coord_auto'].value = '';
52 // but if the coordinates have been set to null, turn coord_auto on
54 if ( cf.elements['<% $pre %>latitude'].value == '' &&
55 cf.elements['<% $pre %>longitude'].value == '' ) {
56 cf.elements['<% $pre %>coord_auto'].value = 'Y';
62 // standardize if the old address wasn't clean
63 if ( cf.elements['old_<% $ship_prefix %>addr_clean'].value == '' ||
64 ( <% !$onlyship || 0 %> &&
65 cf.elements['old_<% $main_prefix %>addr_clean'].value == '' ) ) {
70 // or if it was clean but has been changed
71 for (var key in address_info) {
72 var old_el = cf.elements['old_'+key];
73 if ( old_el && address_info[key] != old_el.value ) {
79 % # If address hasn't been changed, auto-confirm the existing value of
80 % # censustract so that we don't ask the user to confirm it again.
83 cf.elements['<% $main_prefix %>censustract'].value =
84 address_info['ship_censustract'];
87 % if ( $conf->config('address_standardize_method') ) {
89 address_standardize(JSON.stringify(address_info), confirm_standardize);
92 cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
94 cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
96 post_standardization();
101 post_standardization();
103 % } # if address_standardize_method
108 function confirm_standardize(arg) {
109 // contains 'old', which was what we sent, and 'new', which is what came
110 // back, including any errors
111 returned = JSON.parse(arg);
113 if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
115 replace_address(); // with the contents of returned['new']
120 var querystring = encodeURIComponent( JSON.stringify(returned) );
121 // confirmation popup: knows to call replace_address(),
122 // post_standardization(), or submit_abort() depending on the
125 '<%$p%>/misc/confirm-address_standardize.html',
128 overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY,
129 AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH,
130 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399',
137 function replace_address() {
139 var newaddr = returned['new'];
141 var clean = newaddr['addr_clean'] == 'Y';
142 var ship_clean = newaddr['ship_addr_clean'] == 'Y';
143 var error = newaddr['error'];
144 var ship_error = newaddr['ship_error'];
146 var cf = document.<% $formname %>;
147 var state_el = cf.elements['<% $main_prefix %>state'];
148 var ship_state_el = cf.elements['<% $ship_prefix %>state'];
150 % if ( !$onlyship ) {
153 cf.elements['<% $main_prefix %>company'].value = newaddr['company'];
155 cf.elements['<% $main_prefix %>address1'].value = newaddr['address1'];
156 cf.elements['<% $main_prefix %>address2'].value = newaddr['address2'];
157 cf.elements['<% $main_prefix %>city'].value = newaddr['city'];
158 setselect(cf.elements['<% $main_prefix %>state'], newaddr['state']);
159 cf.elements['<% $main_prefix %>zip'].value = newaddr['zip'];
160 cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
162 if ( cf.elements['<% $main_prefix %>coord_auto'].value ) {
163 cf.elements['<% $main_prefix %>latitude'].value = newaddr['latitude'];
164 cf.elements['<% $main_prefix %>longitude'].value = newaddr['longitude'];
171 cf.elements['<% $ship_prefix %>company'].value = newaddr['ship_company'];
173 cf.elements['<% $ship_prefix %>address1'].value = newaddr['ship_address1'];
174 cf.elements['<% $ship_prefix %>address2'].value = newaddr['ship_address2'];
175 cf.elements['<% $ship_prefix %>city'].value = newaddr['ship_city'];
176 setselect(cf.elements['<% $ship_prefix %>state'], newaddr['ship_state']);
177 cf.elements['<% $ship_prefix %>zip'].value = newaddr['ship_zip'];
178 cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
179 if ( cf.elements['<% $ship_prefix %>coord_auto'].value ) {
180 cf.elements['<% $ship_prefix %>latitude'].value = newaddr['latitude'];
181 cf.elements['<% $ship_prefix %>longitude'].value = newaddr['longitude'];
184 % if ( $withcensus ) {
185 % # then set the censustract if address_standardize provided one.
186 if ( ship_clean && newaddr['ship_censustract'] ) {
187 cf.elements['<% $main_prefix %>censustract'].value = newaddr['ship_censustract'];
191 post_standardization();
195 function confirm_manual_address() {
196 %# not much to do in this case, just confirm the censustract
197 % if ( $withcensus ) {
198 var cf = document.<% $formname %>;
199 cf.elements['<% $main_prefix %>censustract'].value =
200 cf.elements['enter_censustract'].value;
202 post_standardization();
205 function post_standardization() {
207 % if ( $conf->exists('enable_taxproducts') ) {
209 if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
212 var country_el = cf.elements['<% $taxpre %>country'];
213 var country = country_el.options[ country_el.selectedIndex ].value;
214 var geocode = cf.elements['geocode'].value;
216 if ( country == 'CA' || country == 'US' ) {
218 var state_el = cf.elements['<% $taxpre %>state'];
219 var state = state_el.options[ state_el.selectedIndex ].value;
221 var url = "<% $p %>/misc/choose_tax_location.html" +
222 "?data_vendor=cch-zip" +
223 ";city=" + cf.elements['<% $taxpre %>city'].value +
225 ";zip=" + cf.elements['<% $taxpre %>zip'].value +
226 ";country=" + country +
227 ";geocode=" + geocode +
228 ";formname=" + '<% $formname %>' +
232 OLgetAJAX( url, update_geocode, 300 );
236 cf.elements['geocode'].value = 'DEFAULT';
243 cf.elements['geocode'].value = '';
256 function update_geocode() {
259 set_geocode = function (what) {
261 var cf = document.<% $formname %>;
263 //alert(what.options[what.selectedIndex].value);
264 var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
265 cf.elements['<% $taxpre %>city'].value = argsHash['city'];
266 setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
267 cf.elements['<% $taxpre %>zip'].value = argsHash['zip'];
268 cf.elements['geocode'].value = argsHash['geocode'];
275 overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
279 function setselect(el, value) {
281 for ( var s = 0; s < el.options.length; s++ ) {
282 if ( el.options[s].value == value ) {
283 el.selectedIndex = s;
291 my $conf = new FS::Conf;
296 my $formname = $opt{form} || 'CustomerForm';
297 my $onlyship = $opt{onlyship} || '';
298 my $main_prefix = $opt{main_prefix} || '';
299 my $ship_prefix = $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
300 my $taxpre = $main_prefix;
301 $taxpre = $ship_prefix if ( $conf->exists('tax-ship_address') || $onlyship );
302 my $post_geocode = $opt{callback} || 'post_geocode();';
303 $withfirm = 0 if $opt{no_company};
304 $withcensus = 0 if $opt{no_census};