ajax-style xmlhttprequest state/county/country selector!
[freeside.git] / httemplate / edit / cust_main / select-country.html
1 <%
2
3   my %opt = @_;
4   foreach my $opt (qw( county state country prefix onchange disabled )) {
5     $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
6   }
7
8   my $conf = new FS::Conf;
9   my $countrydefault = $conf->config('countrydefault') || 'US';
10
11 %>
12
13 <%= include('/elements/xmlhttp.html', $p.'misc/states.cgi', $opt{'prefix'}. 'get_states') %>
14
15 <SCRIPT TYPE="text/javascript">
16
17   function opt(what,value,text) {
18     var optionName = new Option(text, value, false, false);
19     var length = what.length;
20     what.options[length] = optionName;
21   }
22
23   function <%= $opt{'prefix'} %>country_changed(what, callback) {
24
25     country = what.options[what.selectedIndex].text;
26
27     function <%= $opt{'prefix'} %>update_states(states) {
28
29       // blank the current state list
30       for ( var i = what.form.<%= $opt{'prefix'} %>state.length; i >= 0; i-- )
31           what.form.<%= $opt{'prefix'} %>state.options[i] = null;
32
33       // add the new states
34       var statesArray = eval('(' + states + ')' );
35       for ( var s = 0; s < statesArray.length; s++ ) {
36           var stateLabel = statesArray[s];
37           if ( stateLabel == "" )
38               stateLabel = '(n/a)';
39           opt(what.form.<%= $opt{'prefix'} %>state, statesArray[s], stateLabel);
40       }
41
42       //run the callback
43       if ( callback != null ) 
44         callback();
45     }
46
47     // go get the new states
48     <%= $opt{'prefix'} %>get_states( country, <%= $opt{'prefix'} %>update_states );
49
50   }
51
52 </SCRIPT>
53
54 <SELECT NAME="<%= $opt{'prefix'} %>country" onChange="<%= $opt{'prefix'} %>country_changed(this); <%= $opt{'onchange'} %>" <%= $opt{'disabled'} %>>
55
56 <% foreach my $country (
57      sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
58      map { $_->country }
59      qsearch( 'cust_main_county',{}, 'DISTINCT ON ( country ) *', )
60    ) {
61 %>
62
63   <OPTION VALUE="<%= $country %>"<%= $country eq $opt{'country'} ? ' SELECTED' : '' %>><%= $country %>
64
65 <% } %>
66
67 </SELECT>
68