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