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