sub FILTER {

  my $fname = $in{'fname'};
  my $style = $in{'style'};
  if ($in{'efname'}) {
    ($fname,$style) = $in{'efname'} =~ /^(.*)_(.*)$/;
    $in{'fname'} = $fname;
    $in{'style'} = $style;
  }
  &Error("Invalid filter name: $fname") if $fname =~ /[^A-Za-z0-9_]/;
  &Error("You cannot modify or delete the default filter.") if $fname eq 'default';

  if ($in{'delete'}) {
    &Error("You must specify a filter name.") unless $fname;
    &Error("You must specify a filter style.") unless $style;
    &DeleteFilter($fname,$style);
    $in{'a'} = "display";
    $in{'f'} = 'default_basic' if $in{'f'} eq $fname.'_'.$style;
    &DISPLAY();
  
  } elsif ($in{'save'}) {
    &Error("You must specify a filter name.") unless $fname;
    &Error("You must specify a filter style.") unless $style;
    &Error("You must specify at least one address.") unless $in{'filter'};
    &SaveFilter($fname,$style,$in{'filter'});
    $in{'a'} = "display";
    $in{'f'} = $fname.'_'.$style;
    &DISPLAY();
  
  } elsif ($in{'edit'}) {
    &Error("You must specify a filter name.") unless $fname;
    &Error("Unable to locate filter: $fname ($style)") unless defined $Filters{$fname}{$style};
    &FilterPage($fname,$style);
  
  } else {
    &FilterPage();
  }
  exit;
}

sub DeleteFilter {
  my $fname = shift || &Error("Invalid blank filter name");
  my $style = shift || &Error("Invalid blank filter style");

  &Error("Cannot delete the default filter.") if $fname eq 'default';
  &Error("Filter $fname ($style) not found") unless defined $Filters{$fname}{$style};

  delete $Filters{$fname}{$style};
  &WriteKookie();
}

sub SaveFilter {
  my($fname,$style,$filter) = @_;
  $filter =~ s/ +//gs;
  $filter =~ s/\n+/\t/gs;
  $Filters{$fname}{$style} = $filter;
  &WriteKookie();
}


sub FilterPage {
  my $fname  = shift;
  my $style  = shift;
  my $filter = $Filters{$fname}{$style}; $filter =~ s/\t/\n/gs;
  my $cstyle = ($style eq 'compact')?'checked':'';
  my $bstyle = $cstyle?'':'checked';

  my $select = '';
  foreach $fn (sort keys %Filters) {
    next if $fn eq 'default';
    foreach $fs (sort keys %{$Filters{$fn}}) {
      $select .= '<option value="'.$fn.'_'.$fs.'">'.$fn.':'.$fs.'</option>';
    }
  }
  if ($select) {
    $select = '<select name=efname style="'.$Fxs.'"><option selected></option>'.$select.'</select><br>';
  }

  print &Header($Myname,"Address Filters",0);
  print <<EOG;
 <font style="$Fsn">Enter your Filter Name, Style, and List of IP addresses for this filter.
 To edit an existing filter enter the filter name and click on the Edit button below.
 To delete an existing filter enter the filter name and click on the Delete button below.<br></font>
 <table border=0 cellspacing=8 cellpadding=0>
  <tr valign="top">
   <td nowrap>
    <form method=get action="$nPulseurl">
    <input type=hidden name=a value=filter>
    <table border=0 cellspacing=0><tr><td nowrap><font style="$Fss" color="#ff66ff">Filter Name:</font></td><td><input type=text name=fname size=20 style="$Fxs" value="$fname"></td></tr><tr><td></td><td>$select</td></tr></table>
    <font style="$Fss"><font color="#ff66ff">Filter Style:</font><input type=radio name=style value=basic $bstyle>Basic, <input type=radio name=style value=compact $cstyle>Compact, or <input type=radio name=style value=list $cstyle>List<br>
    <font color="#ff66ff">Addresses:</font><br><textarea style="$Fxs" rows=10 cols=40 name=filter>$filter</textarea><br>
    <input type=submit name=save value="Save"><input type=submit name=edit value="Edit"><input type=submit name=delete value="Delete"></font></form>
   </td>
   <td width=1 bgcolor="#cccccc"><img src="images/dot.gif" width=1 height=50></td>
   <td>
    <font style="$Fss">Group Names can contain only alphanumeric characters (a-zA-Z0-9_).<p>
    Addresses are in the same form as the 'configfile.' For example to group/show all devices
    for ip addresses 192.168.10.10 through 192.168.10.59 except 
    *.23 and *.24, you would enter<br>
    <nobr><b>&nbsp;&nbsp;&nbsp;192.168.10.10-22,25-59</b></nobr><br>
    You could also enter each address separately.</font>
   </td>
  </tr>
 </table>
EOG
  print &Footer();
}

sub GetKookie {
  
  $Filters{'default'}{'list'} = $Filters{'default'}{'basic'} = $Filters{'default'}{'compact'} = "";

  local(*K);
  local($/) = undef;
  if (open(K, "<$UserDir/kookie")) {
    my $kookie = <K>;
    close(K);
    chomp $kookie;
    my($k,$v,$l);
    foreach $l (split(/\n/,$kookie)) {
      ($k,$v) = $l =~ /^(.*?)=(.*)$/;
      if ($k =~ /^filter_/) {
        ($fname,$style) = $k =~ /^filter_(.*)_(.*)$/;
        $Filters{$fname}{$style} = $v;
      } else {
        $in{$k} ||= $v;
      }
    }
  }
  
  $in{'sort'}    ||= $DefaultSort;
}

sub WriteKookie {
  local(*K);
  if (open(K, ">$UserDir/kookie")) {
    print K 'sort=',$in{'sort'};
    foreach $fname (sort keys %Filters) {
      foreach $style (sort keys %{$Filters{$fname}}) {
        print K "\nfilter_",$fname,"_",$style,"=",$Filters{$fname}{$style};
      }
    }
    print K "\n";
    close(K);
  }
  &GetKookie();
}

1;
