RT# 82137 - default payment amount now has processing fee in total if processing...
[freeside.git] / httemplate / elements / city.html
1 <%doc>
2
3 Example:
4
5   <& /elements/city.html,
6        #recommended
7        country        => $current_country,
8        state          => $current_state,
9        county         => $current_county,
10        city           => $current_city,
11
12        #optional
13        prefix         => $optional_unique_prefix,
14        onchange       => $javascript,
15        disabled       => 0, #bool
16        style          => [ 'attribute:value', 'another:value' ],
17        disable_empty  => 1, #defaults to 1, set to 0 to disable the empty option
18        empty_label    => 'all', #label for empty option
19        disable_text   => 1, # disable the text input (just show the select)
20        #doesn't work as an option yet, set when conf tax_district_method
21        #disable_select => 1, # disable the selector (just show a text input)
22   &>
23
24 </%doc>
25
26 % if ( $disable_select ) {
27 <SCRIPT TYPE="text/javascript">
28 function <% $pre %>county_changed(what, callback) {}
29 </SCRIPT>
30 % }
31 % else {
32
33
34 <% include('/elements/xmlhttp.html',
35               'url'  => $p.'misc/cities.cgi',
36               'subs' => [ $pre. 'get_cities' ],
37            )
38 %>
39
40 <SCRIPT TYPE="text/javascript">
41
42   function opt(what,value,text) {
43     var optionName = new Option(text, value, false, false);
44     var length = what.length;
45     what.options[length] = optionName;
46   }
47
48   var saved_<%$pre%>city= '<% $saved_city |h %>';
49
50   function <% $pre %>county_changed(what, callback) {
51
52     what.form.<% $pre %>city_select.disabled = 'disabled';
53
54     county  = what.options[what.selectedIndex].value;
55     state   = what.form.<% $pre %>state.options[what.form.<% $pre %>state.selectedIndex].value;
56     country = what.form.<% $pre %>country.options[what.form.<% $pre %>country.selectedIndex].value;
57
58     function <% $pre %>update_cities(cities) {
59
60       // blank the current city list
61       for ( var i = what.form.<% $pre %>city_select.length; i >= 0; i-- )
62           what.form.<% $pre %>city_select.options[i] = null;
63
64 %     unless ( $opt{disable_empty} ) {
65         opt( what.form.<% $pre %>city_select, '', <% $opt{empty_label} |js_string %> );
66 %     }
67
68       // add the new cities
69       var citiesArray = eval('(' + cities + ')' );
70
71       for ( var s = 0; s < citiesArray.length; s++ ) {
72           var cityLabel = citiesArray[s];
73           if ( cityLabel == "" )
74               cityLabel = '(n/a)';
75           opt(what.form.<% $pre %>city_select, citiesArray[s], cityLabel);
76       }
77
78      if ( citiesArray.length > 1 || (citiesArray[0] && citiesArray[0].length) ){
79         // turn off the text city, turn on the select
80         saved_<%$pre%>city = what.form.<%$ pre %>city.value;
81         <%$pre%>city_select_changed(what.form.<% $pre %>city_select);
82         what.form.<% $pre %>city.style.display = 'none';
83         what.form.<% $pre %>city_select.style.display = '';
84       } else if ( what.form.<% $pre %>city.style.display == 'none' ) {
85         // turn on the text city, turn off the select
86         what.form.<%$ pre %>city.value = saved_<%$pre%>city;
87         what.form.<% $pre %>city.style.display = '';
88         what.form.<% $pre %>city_select.style.display = 'none';
89       }
90
91       what.form.<% $pre %>city_select.disabled = '';
92
93       //run the callback
94       if ( callback != null )
95         callback();
96     }
97
98     // go get the new cities
99     <% $pre %>get_cities( county, state, country, <% $pre %>update_cities );
100
101   }
102
103   function <%$pre%>city_select_changed(what) {
104     what.form.<%$pre%>city.value = what.options[what.selectedIndex].value;
105   }
106
107 </SCRIPT>
108
109 % } #!disable_select
110
111 <INPUT TYPE     = "<% $opt{disable_text} ? 'hidden' : 'text' %>"
112        NAME     = "<%$pre%>city"
113        ID       = "<%$pre%>city"
114        VALUE    = "<% $opt{'city'} |h %>"
115 % unless ( $opt{disable_text} ) {
116        onChange = "<% $opt{'onchange'} %>"
117        <% $opt{'disabled'} %>
118        <% $text_style %>
119 % }
120 >
121
122 % if ( $disable_select ) {
123 %# avoid JS errors
124     <INPUT TYPE="hidden" ID="city_select">
125 % } else {
126
127     <SELECT NAME     = "<%$pre%>city_select"
128             ID       = "<%$pre%>city_select"
129             onChange = "<%$pre%>city_select_changed(this); <% $opt{onchange} %>"
130             <% $opt{disabled} %>
131             <% $select_style %>
132     >
133
134 %   if ( $opt{city} ) {
135       <OPTION VALUE="<% $opt{city} %>" SELECTED><% $opt{city} %></OPTION>
136 %   }
137
138 %   unless ( $opt{'disable_empty'} ) {
139       <OPTION VALUE="" <% $opt{city} eq '' ? 'SELECTED' : '' %>><% $opt{empty_label} %></OPTION>
140 %   }
141
142 %   foreach my $city ( @cities ) {
143
144       <OPTION VALUE="<% $city |h %>"
145               <% $city eq $opt{city} ? 'SELECTED' : '' %>
146       ><% $city eq $opt{empty_data_value} ? $opt{empty_data_label} : $city %></OPTION>
147
148 %   }
149
150     </SELECT>
151 % }
152
153 <%init>
154
155 my %opt = @_;
156
157 my $pre = $opt{'prefix'};
158
159 my $conf = new FS::Conf;
160 my $disable_select = 0;
161
162 $opt{'disable_empty'} = 1 unless exists($opt{'disable_empty'});
163
164 if ($conf->exists('cust_main-no_city_in_address')) {
165   $opt{'disable_text'} = 1;
166   $disable_select = 1;
167 }
168
169 my $text_style   = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
170 my $select_style = $opt{'style'} ? [ @{ $opt{'style'} } ] : [];
171
172 push @$text_style,   @{ $opt{'text_style'} }   if $opt{'text_style'};
173 push @$select_style, @{ $opt{'select_style'} } if $opt{'select_style'};
174
175 my @cities = cities( $opt{'county'}, $opt{'state'}, $opt{'country'} );
176 my $saved_city = '';
177 if ( scalar(@cities) > 1 || $cities[0] and !$disable_select ) {
178   push @$text_style, 'display:none';
179 } else {
180   push @$select_style, 'display:none';
181   $saved_city = $opt{'city'};
182 }
183
184 $text_style =
185   scalar(@$text_style)
186     ? 'STYLE="'. join(';', @$text_style). '"'
187     : '';
188
189 $select_style =
190   scalar(@$select_style)
191     ? 'STYLE="'. join(';', @$select_style). '"'
192     : '';
193
194 </%init>