rename geocode_cache; minor coord_auto bugfix
[freeside.git] / httemplate / elements / standardize_locations.js
1 function standardize_locations() {
2
3   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>';
4   overlib(startup_msg, WIDTH, 444, HEIGHT, 168, CAPTION, 'Please wait...', STICKY, AUTOSTATUSCAP, CLOSECLICK, MIDX, 0, MIDY, 0);
5   var cf = document.<% $formname %>;
6
7   var state_el      = cf.elements['<% $main_prefix %>state'];
8   var ship_state_el = cf.elements['<% $ship_prefix %>state'];
9
10   var changed = false; // have any of the address fields been changed?
11   var address_info = {
12 % if ( $onlyship ) {
13     'onlyship': 1,
14 % } else {
15 %   if ( $withfirm ) {
16     'company':  cf.elements['<% $main_prefix %>company'].value,
17 %   }
18     'address1': cf.elements['<% $main_prefix %>address1'].value,
19     'address2': cf.elements['<% $main_prefix %>address2'].value,
20     'city':     cf.elements['<% $main_prefix %>city'].value,
21     'state':    state_el.options[ state_el.selectedIndex ].value,
22     'zip':      cf.elements['<% $main_prefix %>zip'].value,
23     'country':  cf.elements['<% $main_prefix %>country'].value,
24 % }
25 % if ( $withfirm ) {
26     'ship_company':  cf.elements['<% $ship_prefix %>company'].value,
27 % }
28 % if ( $withcensus ) {
29     'ship_censustract': cf.elements['censustract'].value,
30 % }
31     'ship_address1': cf.elements['<% $ship_prefix %>address1'].value,
32     'ship_address2': cf.elements['<% $ship_prefix %>address2'].value,
33     'ship_city':     cf.elements['<% $ship_prefix %>city'].value,
34     'ship_state':    ship_state_el.options[ ship_state_el.selectedIndex ].value,
35     'ship_zip':      cf.elements['<% $ship_prefix %>zip'].value,
36     'ship_country':  cf.elements['<% $ship_prefix %>country'].value,
37   };
38
39 // clear coord_auto fields if the user has changed the coordinates
40 % for my $pre ($ship_prefix, $onlyship ? () : $main_prefix) {
41 %   for my $field ($pre.'latitude', $pre.'longitude') {
42
43   if ( cf.elements['<% $field %>'].value != cf.elements['old_<% $field %>'].value ) {
44     cf.elements['<% $pre %>coord_auto'].value = '';
45   }
46
47 %   }
48   // but if the coordinates have been set to null, turn coord_auto on 
49   // and standardize
50   if ( cf.elements['<% $pre %>latitude'].value == '' &&
51        cf.elements['<% $pre %>longitude'].value == '' ) {
52     cf.elements['<% $pre %>coord_auto'].value = 'Y';
53     changed = true;
54   }
55
56 % }
57
58   // standardize if the old address wasn't clean
59   if ( cf.elements['old_<% $ship_prefix %>addr_clean'].value == '' ||
60       ( <% !$onlyship || 0 %> && 
61         cf.elements['old_<% $main_prefix %>addr_clean'].value == '' ) ) {
62
63     changed = true;
64
65   }
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 ) {
70       changed = true;
71       break;
72     }
73   }
74
75   if ( changed ) {
76     address_standardize(JSON.stringify(address_info), confirm_standardize);
77   }
78   else {
79     cf.elements['ship_addr_clean'].value = 'Y';
80 % if ( !$onlyship ) {
81     cf.elements['addr_clean'].value = 'Y';
82 % }
83     post_standardization();
84   }
85 }
86
87 var returned;
88
89 function confirm_standardize(arg) {
90   // contains 'old', which was what we sent, and 'new', which is what came
91   // back, including any errors
92   returned = JSON.parse(arg);
93
94   if ( <% $conf->exists('cust_main-auto_standardize_address') || 0 %> ) {
95
96     replace_address(); // with the contents of returned['new']
97   
98   }
99   else {
100
101     var querystring = encodeURIComponent( JSON.stringify(returned) );
102     // confirmation popup: knows to call replace_address(), 
103     // post_standardization(), or submit_abort() depending on the 
104     // user's choice.
105     OLpostAJAX(
106         '<%$p%>/misc/confirm-address_standardize.html', 
107         'q='+querystring,
108         function() {
109           overlib( OLresponseAJAX, CAPTION, 'Address standardization', STICKY, 
110             AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 
111             576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', 
112             TEXTSIZE, 3 );
113         }, 0);
114
115   }
116 }
117
118 function replace_address() {
119
120   var newaddr = returned['new'];
121
122   var clean = newaddr['addr_clean'] == 'Y';
123   var ship_clean = newaddr['ship_addr_clean'] == 'Y';
124   var error = newaddr['error'];
125   var ship_error = newaddr['ship_error'];
126
127   var cf = document.<% $formname %>;
128   var state_el      = cf.elements['<% $main_prefix %>state'];
129   var ship_state_el = cf.elements['<% $ship_prefix %>state'];
130
131 % if ( !$onlyship ) {
132   if ( clean ) {
133 %   if ( $withfirm ) {
134         cf.elements['<% $main_prefix %>company'].value  = newaddr['company'];
135 %   }
136         cf.elements['<% $main_prefix %>address1'].value = newaddr['address1'];
137         cf.elements['<% $main_prefix %>address2'].value = newaddr['address2'];
138         cf.elements['<% $main_prefix %>city'].value     = newaddr['city'];
139         setselect(cf.elements['<% $main_prefix %>state'], newaddr['state']);
140         cf.elements['<% $main_prefix %>zip'].value      = newaddr['zip'];
141         cf.elements['<% $main_prefix %>addr_clean'].value = 'Y';
142
143         if ( cf.elements['<% $main_prefix %>coord_auto'].value ) {
144           cf.elements['<% $main_prefix %>latitude'].value = newaddr['latitude'];
145           cf.elements['<% $main_prefix %>longitude'].value = newaddr['longitude'];
146         }
147   }
148 % }
149
150   if ( ship_clean ) {
151 % if ( $withfirm ) {
152       cf.elements['<% $ship_prefix %>company'].value  = newaddr['ship_company'];
153 % }
154       cf.elements['<% $ship_prefix %>address1'].value = newaddr['ship_address1'];
155       cf.elements['<% $ship_prefix %>address2'].value = newaddr['ship_address2'];
156       cf.elements['<% $ship_prefix %>city'].value     = newaddr['ship_city'];
157       setselect(cf.elements['<% $ship_prefix %>state'], newaddr['ship_state']);
158       cf.elements['<% $ship_prefix %>zip'].value      = newaddr['ship_zip'];
159       cf.elements['<% $ship_prefix %>addr_clean'].value = 'Y';
160 % if ( $withcensus ) {
161       cf.elements['<% $main_prefix %>censustract'].value = newaddr['ship_censustract']
162 %    }
163       if ( cf.elements['<% $ship_prefix %>coord_auto'].value ) {
164         cf.elements['<% $ship_prefix %>latitude'].value = newaddr['latitude'];
165         cf.elements['<% $ship_prefix %>longitude'].value = newaddr['longitude'];
166       }
167   }
168
169   post_standardization();
170
171 }
172
173 function post_standardization() {
174
175   var cf = document.<% $formname %>;
176
177 % if ( $conf->exists('enable_taxproducts') ) {
178
179   if ( new String(cf.elements['<% $taxpre %>zip'].value).length < 10 )
180   {
181
182     var country_el = cf.elements['<% $taxpre %>country'];
183     var country = country_el.options[ country_el.selectedIndex ].value;
184     var geocode = cf.elements['geocode'].value;
185
186     if ( country == 'CA' || country == 'US' ) {
187
188       var state_el = cf.elements['<% $taxpre %>state'];
189       var state = state_el.options[ state_el.selectedIndex ].value;
190
191       var url = "<% $p %>/misc/choose_tax_location.html" +
192                   "?data_vendor=cch-zip" + 
193                   ";city="     + cf.elements['<% $taxpre %>city'].value +
194                   ";state="    + state + 
195                   ";zip="      + cf.elements['<% $taxpre %>zip'].value +
196                   ";country="  + country +
197                   ";geocode="  + geocode +
198                   ";formname=" + '<% $formname %>' +
199                   ";";
200
201       // popup a chooser
202       OLgetAJAX( url, update_geocode, 300 );
203
204     } else {
205
206       cf.elements['geocode'].value = 'DEFAULT';
207       <% $post_geocode %>;
208
209     }
210
211   } else {
212
213     cf.elements['geocode'].value = '';
214     <% $post_geocode %>;
215
216   }
217
218 % } else {
219
220   <% $post_geocode %>;
221
222 % }
223
224 }
225
226 function update_geocode() {
227
228   //yay closures
229   set_geocode = function (what) {
230
231     var cf = document.<% $formname %>;
232
233     //alert(what.options[what.selectedIndex].value);
234     var argsHash = eval('(' + what.options[what.selectedIndex].value + ')');
235     cf.elements['<% $taxpre %>city'].value     = argsHash['city'];
236     setselect(cf.elements['<% $taxpre %>state'], argsHash['state']);
237     cf.elements['<% $taxpre %>zip'].value      = argsHash['zip'];
238     cf.elements['geocode'].value  = argsHash['geocode'];
239     <% $post_geocode %>;
240
241   }
242
243   // popup a chooser
244
245   overlib( OLresponseAJAX, CAPTION, 'Select tax location', STICKY, AUTOSTATUSCAP, CLOSETEXT, '', MIDX, 0, MIDY, 0, DRAGGABLE, WIDTH, 576, HEIGHT, 268, BGCOLOR, '#333399', CGCOLOR, '#333399', TEXTSIZE, 3 );
246
247 }
248
249 function setselect(el, value) {
250
251   for ( var s = 0; s < el.options.length; s++ ) {
252      if ( el.options[s].value == value ) {
253        el.selectedIndex = s;
254      }
255   }
256
257 }
258 <%init>
259
260 my %opt = @_;
261 my $conf = new FS::Conf;
262
263 my $withfirm = 1;
264 my $withcensus = 1;
265
266 my $formname =  $opt{form} || 'CustomerForm';
267 my $onlyship =  $opt{onlyship} || '';
268 my $main_prefix =  $opt{main_prefix} || '';
269 my $ship_prefix =  $opt{ship_prefix} || ($onlyship ? '' : 'ship_');
270 my $taxpre = $main_prefix;
271 $taxpre = $ship_prefix if ( $conf->exists('tax-ship_address') || $onlyship );
272 my $post_geocode = $opt{callback} || 'post_geocode();';
273 $withfirm = 0 if $opt{no_company};
274 $withcensus = 0 if $opt{no_census};
275
276 </%init>