Will things ever be the same again?
[freeside.git] / httemplate / edit / cust_main / select-county.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 $sql = "SELECT COUNT(*) FROM cust_main_county".
9 %            " WHERE county IS NOT NULL AND county != ''";
10 %  my $sth = dbh->prepare($sql) or die dbh->errstr;
11 %  $sth->execute or die $sth->errstr;
12 %  my $countyflag = $sth->fetchrow_arrayref->[0];
13 %
14 %
15 % if ( $countyflag ) { 
16
17
18   <% include('/elements/xmlhttp.html',
19                 'url'  => $p.'misc/counties.cgi',
20                 'subs' => [ $opt{'prefix'}. 'get_counties' ],
21              )
22 %>
23   
24   <SCRIPT TYPE="text/javascript">
25   
26     function opt(what,value,text) {
27       var optionName = new Option(text, value, false, false);
28       var length = what.length;
29       what.options[length] = optionName;
30     }
31   
32     function <% $opt{'prefix'} %>state_changed(what, callback) {
33
34       state = what.options[what.selectedIndex].text;
35       country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].text;
36   
37       function <% $opt{'prefix'} %>update_counties(counties) {
38
39         // blank the current county list
40         for ( var i = what.form.<% $opt{'prefix'} %>county.length; i >= 0; i-- )
41             what.form.<% $opt{'prefix'} %>county.options[i] = null;
42   
43         // add the new counties
44         var countiesArray = eval('(' + counties + ')' );
45         for ( var s = 0; s < countiesArray.length; s++ ) {
46             var countyLabel = countiesArray[s];
47             if ( countyLabel == "" )
48                 countyLabel = '(n/a)';
49             opt(what.form.<% $opt{'prefix'} %>county, countiesArray[s], countyLabel);
50         }
51
52         //run the callback
53         if ( callback != null ) 
54           callback();
55       }
56   
57       // go get the new counties
58       <% $opt{'prefix'} %>get_counties( state, country, <% $opt{'prefix'} %>update_counties );
59   
60     }
61   
62   </SCRIPT>
63
64   <SELECT NAME="<% $opt{'prefix'} %>county" onChange="<% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
65 % foreach my $county (
66 %       sort
67 %       map { $_->county }
68 %       qsearch('cust_main_county', { 'state'   => $opt{'state'},
69 %                                     'country' => $opt{'country'},
70 %                                   }
71 %              )
72 %     ) {
73 %  
74
75
76     <OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
77 % } 
78
79
80   </SELECT>
81 % } else { 
82
83
84   <SCRIPT TYPE="text/javascript">
85     function <% $opt{'prefix'} %>state_changed(what) {
86     }
87   </SCRIPT>
88
89   <INPUT TYPE="hidden" NAME="<% $opt{'prefix'} %>county" VALUE="<% $opt{'county'} %>">
90 % } 
91