fix "texas tax" in 1.9
[freeside.git] / httemplate / edit / cust_main / select-county.html
index 70939fc..0dc8268 100644 (file)
@@ -1,25 +1,10 @@
-%
-%
-%  my %opt = @_;
-%  foreach my $opt (qw( county state country prefix onchange disabled )) {
-%    $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
-%  }
-%
-%  my $sql = "SELECT COUNT(*) FROM cust_main_county".
-%            " WHERE county IS NOT NULL AND county != ''";
-%  my $sth = dbh->prepare($sql) or die dbh->errstr;
-%  $sth->execute or die $sth->errstr;
-%  my $countyflag = $sth->fetchrow_arrayref->[0];
-%
-%
 % if ( $countyflag ) { 
 
-
   <% include('/elements/xmlhttp.html',
                 'url'  => $p.'misc/counties.cgi',
                 'subs' => [ $opt{'prefix'}. 'get_counties' ],
              )
-%>
+  %>
   
   <SCRIPT TYPE="text/javascript">
   
@@ -31,8 +16,8 @@
   
     function <% $opt{'prefix'} %>state_changed(what, callback) {
 
-      state = what.options[what.selectedIndex].text;
-      country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].text;
+      state = what.options[what.selectedIndex].value;
+      country = what.form.<% $opt{'prefix'} %>country.options[what.form.<% $opt{'prefix'} %>country.selectedIndex].value;
   
       function <% $opt{'prefix'} %>update_counties(counties) {
 
             opt(what.form.<% $opt{'prefix'} %>county, countiesArray[s], countyLabel);
         }
 
+        var countyFormLabel = document.getElementById('<% $opt{'prefix'} %>countylabel');
+
+        if ( countiesArray.length > 1 ) { 
+          what.form.<% $opt{'prefix'} %>county.style.display = '';
+          countyFormLabel.style.visibility = 'visible';
+        } else {
+          what.form.<% $opt{'prefix'} %>county.style.display = 'none';
+          countyFormLabel.style.visibility = 'hidden';
+        }
+
         //run the callback
         if ( callback != null ) 
           callback();
   </SCRIPT>
 
   <SELECT NAME="<% $opt{'prefix'} %>county" onChange="<% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
-% foreach my $county (
-%       sort
-%       map { $_->county }
-%       qsearch('cust_main_county', { 'state'   => $opt{'state'},
-%                                     'country' => $opt{'country'},
-%                                   }
-%              )
-%     ) {
-%  
 
+% foreach my $county ( @counties ) {
 
     <OPTION VALUE="<% $county %>"<% $county eq $opt{'county'} ? ' SELECTED' : '' %>><% $county %>
-% } 
 
+% } 
 
   </SELECT>
+
 % } else { 
 
 
   </SCRIPT>
 
   <INPUT TYPE="hidden" NAME="<% $opt{'prefix'} %>county" VALUE="<% $opt{'county'} %>">
+
 % } 
 
+<%init>
+
+my %opt = @_;
+foreach my $opt (qw( county state country prefix onchange disabled )) {
+  $opt{$_} = '' unless exists($opt{$_}) && defined($opt{$_});
+}
+
+my @counties = ();
+if ( $countyflag ) {
+
+  @counties = counties( $opt{'state'}, $opt{'country'} );
+
+  # this is very hacky
+  unless ( scalar(@counties) > 1 ) {
+    if ( $opt{'disabled'} =~ /STYLE=/i ) {
+      $opt{'disabled'} =~ s/STYLE="([^"]+)"/STYLE="$1; display:none"/i;
+    } else {
+      $opt{'disabled'} .= ' STYLE="display:none"';
+    }
+  }
+
+}
+
+</%init>
+<%once>
+
+my $sql = "SELECT COUNT(*) FROM cust_main_county".
+          " WHERE county IS NOT NULL AND county != ''";
+my $sth = dbh->prepare($sql) or die dbh->errstr;
+$sth->execute or die $sth->errstr;
+my $countyflag = $sth->fetchrow_arrayref->[0];
+
+</%once>