70a8f947783a8b0da99a55f09705d882ec71f39c
[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
16 <% if ( $countyflag ) { %>
17
18   <%= include('/elements/xmlhttp.html', $p.'misc/counties.cgi', $opt{'prefix'}. 'get_counties' ) %>
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'} %>state_changed(what, callback) {
29
30       state = what.options[what.selectedIndex].text;
31       country = what.form.<%= $opt{'prefix'} %>country.options[what.form.<%= $opt{'prefix'} %>country.selectedIndex].text;
32   
33       function <%= $opt{'prefix'} %>update_counties(counties) {
34
35         // blank the current county list
36         for ( var i = what.form.<%= $opt{'prefix'} %>county.length; i >= 0; i-- )
37             what.form.<%= $opt{'prefix'} %>county.options[i] = null;
38   
39         // add the new counties
40         var countiesArray = eval('(' + counties + ')' );
41         for ( var s = 0; s < countiesArray.length; s++ ) {
42             var countyLabel = countiesArray[s];
43             if ( countyLabel == "" )
44                 countyLabel = '(n/a)';
45             opt(what.form.<%= $opt{'prefix'} %>county, countiesArray[s], countyLabel);
46         }
47
48         //run the callback
49         if ( callback != null ) 
50           callback();
51       }
52   
53       // go get the new counties
54       <%= $opt{'prefix'} %>get_counties( state, country, <%= $opt{'prefix'} %>update_counties );
55   
56     }
57   
58   </SCRIPT>
59
60   <SELECT NAME="<%= $opt{'prefix'} %>county" onChange="<%= $opt{'onchange'} %>" <%= $opt{'disabled'} %>>
61
62   <% foreach my $county (
63        sort
64        map { $_->county }
65        qsearch('cust_main_county', { 'state'   => $opt{'state'},
66                                      'country' => $opt{'country'},
67                                    }
68               )
69      ) {
70   %>
71
72     <OPTION VALUE="<%= $county %>"<%= $county eq $opt{'county'} ? ' SELECTED' : '' %>><%= $county %>
73
74   <% } %>
75
76   </SELECT>
77
78 <% } else { %>
79
80   <SCRIPT TYPE="text/javascript">
81     function <%= $opt{'prefix'} %>state_changed(what) {
82     }
83   </SCRIPT>
84
85   <INPUT TYPE="hidden" NAME="<%= $opt{'prefix'} %>county" VALUE="<%= $opt{'county'} %>">
86
87 <% } %>