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};

  &Error("Cannot delete a global filter.") if ($GFilters{$fname}{$style} and !$AdminUser);
  delete $Filters{$fname}{$style};
  delete $GFilters{$fname}{$style};
  &WriteKookie();
}

sub SaveFilter {
  my($fname,$style,$filter) = @_;
  $filter =~ s/ +//gs;
  $filter =~ s/\n+/\t/gs;
  $Filters{$fname}{$style} = $filter;
  $GFilters{$fname}{$style} = ($AdminUser and $in{'global'})?1:0;
  &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}}) {
      next if (!$AdminUser and ($GFilters{$fn}{$fs}));
      $select .= '<option value="'.$fn.'_'.$fs.'">'.$fn.':'.$fs.'</option>';
    }
  }
  if ($select) {
    $select = '<select name=efname style="'.$Fxs.'"><option selected></option>'.$select.'</select>';
  }

  my $global = "";
  if ($fname and $AdminUser) {
    my $isglobal = $GFilter{$fname}{$style}?'checked':'';
    $global = '<font style="'.$Fxs.'"><input style="'.$Fxs.'" type=checkbox name=global value="1" $isglobal>Global</font>';
  } elsif ($AdminUser) {
    $global = '<font style="'.$Fxs.'"><input style="'.$Fxs.'" type=checkbox name=global value="1">Global</font>';
  }

  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.
 Only the Admin user can edit/delete Global filters.<br></font>
 <table border="0" cellspacing="8" cellpadding="0">
  <tr valign="top">
   <td nowrap>
    <form method="post" 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 $global</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();
}

1;
