5b068214e3df9efb7bd447d85bee51e69eac3cb1
[freeside.git] / httemplate / edit / cust_main / select-county.html
1 % if ( $countyflag ) { 
2
3   <% include('/elements/xmlhttp.html',
4                 'url'  => $p.'misc/counties.cgi',
5                 'subs' => [ $opt{'prefix'}. 'get_counties' ],
6              )
7   %>
8   
9   <SCRIPT TYPE="text/javascript">
10   
11     function opt(what,value,text) {
12       var optionName = new Option(text, value, false, false);
13       var length = what.length;
14       what.options[length] = optionName;
15     }
16   
17     function <% $opt{'prefix'} %>state_changed(what, callback) {
18
19       state = what.options[what.selectedIndex].value;
20       country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].value;
21   
22       function <% $opt{'prefix'} %>update_counties(counties) {
23
24         // blank the current county list
25         for ( var i = what.form.<% $opt{'prefix'} %>county.length; i >= 0; i-- )
26             what.form.<% $opt{'prefix'} %>county.options[i] = null;
27   
28         // add the new counties
29         var countiesArray = eval('(' + counties + ')' );
30         for ( var s = 0; s < countiesArray.length; s++ ) {
31             var countyLabel = countiesArray[s];
32             if ( countyLabel == "" )
33                 countyLabel = '(n/a)';
34             opt(what.form.<% $opt{'prefix'} %>county, countiesArray[s], countyLabel);
35         }
36
37         if ( countiesArray.length > 1 ) { 
38           what.form.<% $opt{'prefix'} %>county.style.display = '';
39         } else {
40           what.form.<% $opt{'prefix'} %>county.style.display = 'none';
41         }
42
43         //run the callback
44         if ( callback != null ) 
45           callback();
46       }
47   
48       // go get the new counties
49       <% $opt{'prefix'} %>get_counties( state, country, <% $opt{'prefix'} %>update_counties );
50   
51     }
52   
53   </SCRIPT>
54
55   <SELECT NAME="<% $opt{'prefix'} %>county" onChange="<% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
56
57 % foreach my $county ( @counties ) {
58
59     <OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
60
61 % } 
62
63   </SELECT>
64
65 % } else { 
66
67
68   <SCRIPT TYPE="text/javascript">
69     function <% $opt{'prefix'} %>state_changed(what) {
70     }
71   </SCRIPT>
72
73   <INPUT TYPE="hidden" NAME="<% $opt{'prefix'} %>county" VALUE="<% $opt{'county'} %>">
74
75 % } 
76
77 <%init>
78
79 my %opt = @_;
80 foreach my $opt (qw( county state country prefix onchange disabled )) {
81   $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
82 }
83
84 my @counties = ();
85 if ( $countyflag ) {
86
87   @counties =
88       sort
89       map { $_->county }
90       qsearch( {
91         'select'  => 'DISTINCT county',
92         'table'   => 'cust_main_county',
93         'hashref' => { 'state'   => $opt{'state'},
94                        'country' => $opt{'country'},
95                      },
96       } );
97
98   # this is very hacky
99   unless ( scalar(@counties) > 1 ) {
100     if ( $opt{'disabled'} =~ /STYLE=/i ) {
101       $opt{'disabled'} =~ s/STYLE="([^"]+)"/STYLE="$1; display:none"/i;
102     } else {
103       $opt{'disabled'} .= ' STYLE="display:none"';
104     }
105   }
106
107 }
108
109 </%init>
110 <%once>
111
112 my $sql = "SELECT COUNT(*) FROM cust_main_county".
113           " WHERE county IS NOT NULL AND county != ''";
114 my $sth = dbh->prepare($sql) or die dbh->errstr;
115 $sth->execute or die $sth->errstr;
116 my $countyflag = $sth->fetchrow_arrayref->[0];
117
118 </%once>