#!/usr/bin/perl



# define some global variables



    $fields = 10;      # Number of fields in each record
    $filename = "bookworm.txt";      # The database file
    $results = 900;      # maximum number of results to display
    $totalfound = 0;
    $counter=0;

    &parse_form;

    &open_file("FILE1","",$filename);

    
    $findrange = $FORM{'item'};
   
    
    
    $looping = 0;
    $thistable = 0;

	


print "Content-type: text/html\n\n";
print "<HTML><HEAD>\n";

print <<tohere1a;

<title>Search Results</title>

</head>

tohere1a


print <<tohere1a;

<body BGCOLOR="#FFFFFF" TEXT="#007100" LINK="#0000FF" VLINK="#800080" ALINK="#0000FF"
BACKGROUND="../images/back.gif" leftmargin="25"
topmargin="25" marginwidth="25" marginheight="25">
<form  NAME="itemsform">
<div align="center"><center><table border="0" cellpadding="0" cellspacing="0" width="90%">
    <tr>
      <td valign="top" align="center" width="600"><img src="../images/logosm.gif"
      alt="Miltary Book Worm" width="46" height="60"><br> <font face="Arial" color="#007100" size="3"><b>Military Bookworm</b></font>

  <p align = "left"><font face="Arial" color="#007100" size="3"> <b>$findrange</b> </p>

tohere1a



    $counter = 0;

    while (($line = &read_file("FILE1")) && ($counter < $results)) {
         # split the fields at the ,$ character     
         @tabledata = split(/\s*\|\s*/,$line ,$fields);
          &check_record;
          if ($found == 1) {
            $counter++;
            &print_record;
          }

    }

    close(FILE1);
   
    if ($counter == 0) {
       print "<div align=\"center\"><table WIDTH=\"85%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    <tr>
      
<td width=\"85%\" align=\"center\"><font size=\"2\" face=\"Arial\"><b>Sorry no books are available under this category
at present we update our list<br>every 7 days.</b>
      </font></td></tr>
  </table>
  </div>\n";

       
		} 

print <<tohere1c;
 

</form>


tohere1c

print "</body></html>";

#########################################
#
#  Print the matched record
#
#########################################
sub print_record {
$looping++;
$thistable++;

if ($looping >= 1) {
		
		$thistable=1;
	}


if ($thistable == 1) {


print " 
	</tr>
  </table>
  </center></div><b>
  	<div align=\"left\"><table WIDTH=\"85%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    <tr>
      
<td width=\"85%\"><font size=\"2\" face=\"Arial\"><b>$FIELD1 <font color=\"red\">$FIELD2</b></font><br> <font color=\"#000000\"><b>$FIELD4 $FIELD3</b></font> <b>$FIELD5 $FIELD6 $FIELD7 </b><br> <b><font
      COLOR=\"#FF0000\" face=\"Arial\">£$FIELD8</font></b>
      <input TYPE=\"hidden\" NAME=\"$FIELD9\quant\" VALUE=\"1\" > <input TYPE=\"button\"
      NAME=\"$FIELD9\add\" VALUE=\"Buy\"
      onclick=\"top.menu.buyItem('$FIELD1 $FIELD2 ','$FIELD8', document.itemsform.$FIELD9\quant.value)\"><br><hr width=\"75%\">
      </font></td></tr>
  </table>
  </div>\n";



}
}
##########################################
#
#  Check to see if record matches search criteria
#
##########################################



sub check_record {
    # get the data from the record read from the file. $tabledata

$FIELD1 = $tabledata[0];
$FIELD2 = $tabledata[1];
$FIELD3 = $tabledata[2];
$FIELD4 = $tabledata[3];
$FIELD5 = $tabledata[4];
$FIELD6 = $tabledata[5];
$FIELD7 = $tabledata[6];
$FIELD8 = $tabledata[7];
$FIELD9 = $tabledata[8];
$FIELD10 = $tabledata[9];



   #build the search line with all fields we want to search in
   $searchline = $FIELD10;




   #search by keywords
   $sfound = 0;
   $found = 0;
   $notfound = 1; 

   $stlen = length($findrange);
   if ($stlen >= 1) {

       @words = split(/&/,$findrange);
        foreach $aword (@words) {
           if ($searchline =~ /\b$aword/i) {

		$totalfound++;
                $sfound = 1;
           } 
           else {

                  $notfound = 0;
            }
         }
     }
    if ($sfound == 1 && $notfound == 1) {
        $found = 1;
     }
}


sub parse_form {

   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   if (length($buffer) < 5) {
         $buffer = $ENV{QUERY_STRING};
    }
 
  @pairs = split(/&/, $buffer);
   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);

      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

      $FORM{$name} = $value;
   }
}



sub open_file {

  local ($filevar, $filemode, $filename) = @_;
  
  open ($filevar,$filemode . $filename) ||
     die ("Can't open $filename");
}

sub read_file {

  local ($filevar) = @_;

  <$filevar>;  
}

sub write_file {

  local ($filevar, $line) = @_;

  print $filevar ($line);
}


