RT# 82137 - default payment amount now has processing fee in total if processing...
[freeside.git] / httemplate / elements / select-county.html
1 <%doc>
2
3 Example:
4
5  <& /elements/select-county.html,
6     #recommended
7     country    => $current_country,
8     state      => $current_state,
9     county     => $current_county,
10
11     #optional
12     prefix        => $optional_unique_prefix,
13     onchange      => $javascript,
14     disabled      => 0, #bool
15     disable_empty => 1, #defaults to 1, set to 0 to disable the empty option
16     empty_label   => 'all', #label for empty option
17     style         => [ 'attribute:value', 'another:value' ],
18   &>
19
20 </%doc>
21 % if ( $countyflag ) { 
22
23   <% include('/elements/xmlhttp.html',
24                 'url'  => $p.'misc/counties.cgi',
25                 'subs' => [ $pre. 'get_counties' ],
26              )
27   %>
28   
29   <SCRIPT TYPE="text/javascript">
30   
31     function opt(what,value,text) {
32       var optionName = new Option(text, value, false, false);
33       var length = what.length;
34       what.options[length] = optionName;
35     }
36   
37     function <% $pre %>state_changed(what, callback) {
38
39       what.form.<% $pre %>county.disabled = 'disabled';
40
41       state = what.options[what.selectedIndex].value;
42       country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
43   
44       function <% $pre %>update_counties(counties) {
45
46         // blank the current county list
47         for ( var i = what.form.<% $pre %>county.length; i >= 0; i-- )
48             what.form.<% $pre %>county.options[i] = null;
49
50 %       unless ( $opt{disable_empty} ) {
51           opt( what.form.<% $pre %>county, '', <% $opt{empty_label} |js_string %> );
52 %       }
53   
54         // add the new counties
55         var countiesArray = eval('(' + counties + ')' );
56         for ( var s = 0; s < countiesArray.length; s++ ) {
57             var countyLabel = countiesArray[s];
58             if ( countyLabel == "" )
59                 countyLabel = '(n/a)';
60             opt(what.form.<% $pre %>county, countiesArray[s], countyLabel);
61         }
62
63         var countyFormLabel = document.getElementById('<% $pre %>countylabel');
64
65         if ( countiesArray.length > 1 ) { 
66           what.form.<% $pre %>county.style.display = '';
67           if ( countyFormLabel )  {
68             //countyFormLabel.style.visibility = 'visible';
69             countyFormLabel.style.display = '';
70           }
71         } else {
72           what.form.<% $pre %>county.style.display = 'none';
73           if ( countyFormLabel ) {
74             //countyFormLabel.style.visibility = 'hidden';
75             countyFormLabel.style.display = 'none';
76           }
77         }
78
79         what.form.<% $pre %>county.disabled = '';
80
81         //run the callback
82         if ( callback != null )  {
83           callback();
84         } else {
85           <% $pre %>county_changed(what.form.<% $pre %>county);
86         }
87       }
88   
89       // go get the new counties
90       <% $pre %>get_counties( state, country, <% $pre %>update_counties );
91   
92     }
93   
94   </SCRIPT>
95
96   <SELECT NAME    = "<% $pre %>county"
97           ID      = "<% $pre %>county"
98           onChange= "<% $onchange %>"
99           <% $opt{'disabled'} %>
100           <% $style %>
101   >
102
103 % unless ( $opt{'disable_empty'} ) {
104   <OPTION VALUE="" <% $opt{county} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %>
105 % }
106
107 % foreach my $county ( @counties ) {
108
109     <OPTION VALUE="<% $county |h %>"
110             <% $county eq $opt{'county'} ? 'SELECTED' : '' %>
111     ><% $county eq $opt{'empty_data_value'} ? $opt{'empty_data_label'} : $county %>
112
113 % } 
114
115   </SELECT>
116
117 % } else { 
118
119   <SCRIPT TYPE="text/javascript">
120     function <% $pre %>state_changed(what) {
121     }
122   </SCRIPT>
123
124   <SELECT NAME  = "<% $pre %>county"
125            ID   = "<% $pre %>county"
126           STYLE = "display:none"
127   >
128     <OPTION SELECTED VALUE="<% $opt{'county'} |h %>">
129   </SELECT>
130
131 % } 
132
133 <%init>
134
135 my %opt = @_;
136 foreach my $opt (qw( county state country prefix onchange disabled
137                      empty_value )) {
138   $opt{$opt} = '' unless exists($opt{$opt}) && defined($opt{$opt});
139 }
140
141 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
142
143 my $pre = $opt{'prefix'};
144
145
146 # disable_cityupdate?
147 my $onchange =
148   ( $opt{'disable_cityupdate'} ? '' : $pre.'county_changed(this); ' ).
149   $opt{'onchange'};
150
151 my $county_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
152
153 my @counties = ();
154 if ( $countyflag ) {
155
156   @counties = map { length($_) ? $_ : $opt{'empty_data_value'} }
157                   counties( $opt{'state'}, $opt{'country'} );
158
159   push @$county_style, 'display:none'
160     unless scalar(@counties) > 1;
161
162 }
163
164 my $style =
165   scalar(@$county_style)
166     ? 'STYLE="'. join(';', @$county_style). '"'
167     : '';
168
169 </%init>
170 <%once>
171
172 my $sql = "SELECT COUNT(*) FROM cust_main_county".
173           " WHERE county IS NOT NULL AND county != ''";
174 my $sth = dbh->prepare($sql) or die dbh->errstr;
175 $sth->execute or die $sth->errstr;
176 my $countyflag = $sth->fetchrow_arrayref->[0];
177
178 </%once>