014effd6633b282f9af4fb361dcf6b056da408f3
[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',
14               'url'  => $p.'misc/states.cgi',
15               'subs' => [ $opt{'prefix'}. 'get_states' ],
16            )
17 %>
18
19 <SCRIPT TYPE="text/javascript">
20
21   function opt(what,value,text) {
22     var optionName = new Option(text, value, false, false);
23     var length = what.length;
24     what.options[length] = optionName;
25   }
26
27   function <%= $opt{'prefix'} %>country_changed(what, callback) {
28
29     country = what.options[what.selectedIndex].text;
30
31     function <%= $opt{'prefix'} %>update_states(states) {
32
33       // blank the current state list
34       for ( var i = what.form.<%= $opt{'prefix'} %>state.length; i >= 0; i-- )
35           what.form.<%= $opt{'prefix'} %>state.options[i] = null;
36
37       // add the new states
38       var statesArray = eval('(' + states + ')' );
39       for ( var s = 0; s < statesArray.length; s++ ) {
40           var stateLabel = statesArray[s];
41           if ( stateLabel == "" )
42               stateLabel = '(n/a)';
43           opt(what.form.<%= $opt{'prefix'} %>state, statesArray[s], stateLabel);
44       }
45
46       //run the callback
47       if ( callback != null ) 
48         callback();
49     }
50
51     // go get the new states
52     <%= $opt{'prefix'} %>get_states( country, <%= $opt{'prefix'} %>update_states );
53
54   }
55
56 </SCRIPT>
57
58 <SELECT NAME="<%= $opt{'prefix'} %>country" onChange="<%= $opt{'prefix'} %>country_changed(this); <%= $opt{'onchange'} %>" <%= $opt{'disabled'} %>>
59
60 <% foreach my $country (
61      sort { ($b eq $countrydefault) <=> ($a eq $countrydefault) or $a cmp $b }
62      map { $_->country }
63      qsearch( 'cust_main_county',{}, 'DISTINCT ON ( country ) *', )
64    ) {
65 %>
66
67   <OPTION VALUE="<%= $country %>"<%= $country eq $opt{'country'} ? ' SELECTED' : '' %>><%= $country %>
68
69 <% } %>
70
71 </SELECT>
72