#!/usr/bin/perl

$path = $0;
$path =~ s#/[^/]*$##;

require "$path/config.ph";
printf("Content-type: text/html\n\n<TITLE>%s</TITLE>",URL_TITLE);

printf("<FORM ACTION=\"http:perform_query.pl\" METHOD=\"POST\">
 <h2> $START_CHECK_MESSAGE</h2><hr>\n");


if ($ENV{'REQUEST_METHOD'} eq 'POST') {
      # Read in the desired number of bytes
      read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
} else {
       $request = $ENV{'QUERY_STRING'};
}

print "The request to generate this page was<br>$request<br>" if $VERBOSE;
print "Set VERBOSE to 0 in config.ph to remove this message.<br><br>\n" if $VERBOSE;

@field = split(/&/, $request);

foreach $next_field (@field) {
    ($name, $value) = split(/=/, $next_field);
    $value =~ tr/+/ /;
    $value =~ s/%2F/\//g;
    $value =~ s/~!/ ~!/g;   # Stops people using subshells apparently

    $search_items{$name} .= "/"."$value";

#print "$name and $value<br>";
}
    

foreach $field ( keys %search_items ) {
    
#print "*****$search_items{$field}<br>";


   $search_items{$field} =~ s#^/*##;
   $search_items{$field} =~ s#/*$##;

#print "-----$search_items{$field}<br>";


   if ( $field =~ /^s_/ && $search_items{$field} =~ "ANDING" ) {

       $type = $ANDING;
       $search_items{$field} =~ s#(/)?ANDING(/)?##;
       $formated_items = $search_items{$field};
       $formated_items =~ s#/# <i>and</i> #g;

   }
   if ( $field =~ /^s_/ && $search_items{$field} =~ "ORING" ) {

       $type = $ORING;
       $search_items{$field} =~ s#(/)?ORING(/)?##;
       $formated_items = $search_items{$field};
       $formated_items =~ s#/# <i>or</i> #g;

   }
   if ( $field =~ /^[r|c]_/  ) {

       $type = $YEARS;
       $type = $CENTURIES if $field =~ /^c_/ ;
       $formated_items = $search_items{$field};

   }

   if ( $field =~ /^e_/  ) {

       $type = $REG_EXP;
       $search_items{$field}=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
       $formated_items = $search_items{$field};
      # $search_items{$field} =~ s/(\W)/\\$1/g;

   }


   $formated_items =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;


   if ($search_items{$field} ne "" ) {
 
       printf("<dt><inPUT TYPE=\"checkbox\" NAME=\"%s\\$type\\$search_items{$field}\" CHECKED>The <b>%s</b> must be:\n<br><dd><b>$formated_items</b><br>\n",substr($field,3,2),@field_names[substr($field,3,2)-1]);

   }

}

  printf("</dl><hr>
    <inPUT TYPE=\"radio\" NAME=\"ANDING\" VALUE=\"on\" CHECKED> See the records in which <b>all</b> the selected conditions apply.<br>or<br>
    <inPUT TYPE=\"radio\" NAME=\"ANDING\" VALUE=\"off\"> See the records in which <b>any</b> of selected conditions apply.<br>
  <br><inPUT TYPE=\"submit\" VALUE=\"Submit this query.\"></FORM>\n");

  printf("$END_CHECK_MESSAGE");





__END__
