#!/usr/local/bin/perl

open(STDERR,">>/tmp/check_query$$");

$path = $0;
$path =~ s#/[^/]*$##;

require "$path/config.ph";
printf("Content-type: text/html\n\n<TITLE>%s</TITLE>",URL_TITLE);

printf("<FORM ACTION=\"http:/cgi-bin/michael4/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};
print STDERR "formated_items (ANDING) $formatted_items\n";
       $formated_items =~ s#/# <i>and</i> #g;

   }
   elsif ( $field =~ /^s_/ && $search_items{$field} =~ "ORING" ) {

       $type = $ORING;
       $search_items{$field} =~ s#(/)?ORING(/)?##;
       $formated_items = $search_items{$field};
print STDERR "formated_items (ORING) $formatted_items\n";
       $formated_items =~ s#/# <i>or</i> #g;

   }
   elsif ( $field =~ /^[r|c]_/  ) {

       $type = $YEARS;
       $type = $CENTURIES if $field =~ /^c_/ ;
       $formated_items = $search_items{$field};
print STDERR "formated_items (rlc) $formatted_items\n";

   }
   elsif ( $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};
print STDERR "formated_items (e) $formatted_items\n";
      # $search_items{$field} =~ s/(\W)/\\$1/g;

   }
   else { # DJB how did we get here?
       $type = $ORING;
       $search_items{$field} =~ s#(/)?ORING(/)?##;
       $formated_items = $search_items{$field};
print STDERR "formated_items (none? using ORING) $formatted_items\n";
       $formated_items =~ s#/# <i>or</i> #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]);

       print("<dt><inPUT TYPE=\"checkbox\" NAME=\"");
       print(substr($field,3,2),"\\$type\\$search_items{$field}\" CHECKED>\n");
       print("The <b>",@field_names[substr($field,3,2)-1],"</b> must be:<br>\n");
       print("<dd><b>$formated_items</b><br>\n");
   }

}

  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__
