#!/usr/old/bin/perl5.004_01

unshift(@INC, ".");

# wrap it all up
require "safe.pl";
&safe(<<'THE_END');

require "support.pl";
require "pqs.pl";

#  since only reading don't need locks
# sub LOCK_EX {0x02;}
# sub LOCK_UN {0x08;}

#      db format:
# $category $linkurl $linktitle $Linkgif $linkdescrip $linkwords $linkemail
# define some global variables

   $fields = 9;                       # Number of fields in each record
   $filename = "/home/lorraine/db/retail.db";  # The database text file
   $results = 1000;               # maximum number of results to display

   # %searchstring = &parse_query_string();

   &parse_form;
   $searchstring = $FORM{'searchstring'};

   #  no adding of records allowed
   #    &addrecord if ($searchstring eq "**ADD RECORD**");


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


    &out("Content-type: text/html\n\
                <html>\n\
               <head>\n");
    if ($searchstring eq "lt") {
       &out("<TITLE>Jewelry Mall Retail Stores with Jewelry less than 50 dollars\!</TITLE>\n");
    }
    elsif ($searchstring eq "50-") {
       &out("<TITLE>Jewelry Mall Retail Stores with Jewelry between \$50 - \$125</TITLE>\n");
    }
    elsif ($searchstring eq "125-") {
       &out("<TITLE>Jewelry Mall Retail Stores with Jewelry between \$125 - \$600</TITLE>\n");
    }
    elsif ($searchstring eq "600-") {
       &out("<TITLE>Jewelry Mall Retail Stores with Jewelry between \$600 - \$1250</TITLE>\n");
    }
    elsif ($searchstring eq "gt") {
       &out("<TITLE>Jewelry Mall Retail Stores: Exquisite Fine Jewelry over \$1250</TITLE>\n");
    }
    elsif ($searchstring eq "tools") {
       &out("<TITLE>Jewelry Mall Retail Stores: Jewelry Making Tools Page</TITLE>\n");
    }
    elsif ($searchstring eq "accessories") {
       &out("<TITLE>Jewelry Mall Retail Stores: Storage/Cleaning/Safety Clasps</TITLE>\n");
    }
    &out("<BODY BGCOLOR=#FDFDFF TEXT=#00009C LINK=#9932CD VLINK=#23238E>\n");
    &out( "<center>\n");
    &out("<TABLE CELLSPACING=4 CELLPADDING=6 BORDER=0>\n");
    &out("<TR>\n");
    &out("<TD BGCOLOR=#00009C VALIGN=MIDDLE ALIGN=CENTER>\n");
    &out("<NOBR><FONT FACE=\"HELVETICA, TIMES\" SIZE=2><B>\n");
    &out("&nbsp <A HREF=/stores.html> <font color=#00FFFF>Stores</font> </A> &nbsp \n");
    &out(" <A HREF=/gemshows.html> <font color=#FF7E00>Shows</font> </A> &nbsp \n");
    &out(" <A HREF=/birthstones.html> <font color=#FFFFFF>Birthstones</font> </A> &nbsp \n");
    &out(" <A HREF=/malls.html> <font color=#EAADEA>Malls</font> </A> &nbsp \n");
    &out(" <A HREF=/wholesale.html> <font color=#00FF00>Wholesale</font> </A> &nbsp \n");
    &out(" <A HREF=/index.shtml> <font color=#DDDFDD>HOME</font> </A> &nbsp \n");
    &out(" <A HREF=/jewelryjokes.html> <font color=#FF00FF>Jokes</font> </A> &nbsp \n");
    &out(" <A HREF=/jewellinks.html> <font color=#FFFF00>Jewellinks</font> </A> &nbsp \n");
    &out(" <A HREF=/links.html> <font color=#FFFFFF>Links</font> </A> &nbsp \n");
    &out(" <A HREF=/therapy.html> <font color=#00FFFF>Therapy</font> </A> \n");
    &out("</B></FONT></NOBR></TD></TR> \n");
    &out("</TABLE> \n");
    &out( "</center>\n");
    &out("<p></p>\n");
    &out("<center>\n");
    if ($searchstring eq "125-") {
       &out("<a href=http://www.globaljewelry.com>\n");
       &out(" <IMG ALIGN=center width=400 height=60 SRC=/images/JM698.gif  ALT=\"Our wonderful sponsor: Global Jewelry\"></a>\n");
    } elsif ($searchstring eq "50-") {
       &out("<a href=http://www2.burstnet.com/ads/ad3318b-map.cgi><img src=http://www2.burstnet.com/cgi-bin/ads/ad3318b.cgi ismap border=0 height=60 width=468></a>\n");
    } elsif ($searchstring eq "600-") {
       &out("<IMG SRC=\"http://service.bfast.com/bfast/serve/proflowers?sourceid=260988&categoryid=banners\" BORDER=0 WIDTH=1 HEIGHT=1><A HREF=\"http://service.bfast.com/bfast/click/proflowers?sourceid=260988&categoryid=banners\" TARGET=\"_top\"><IMG SRC=http://www.proflowers.com/affiliates/images/bma21.gif BORDER=0 WIDTH=468 HEIGHT=60 ALT=\"Proflowers Ad Banners\"></A>\n"); 
    } elsif ($searchstring eq "lt") {
       &out("<IMG SRC=http://service.bfast.com/bfast/serve/proflowers?sourceid=260988&categoryid=banners BORDER=0 WIDTH=1 HEIGHT=1><A HREF=http://service.bfast.com/bfast/click/proflowers?sourceid=260988&categoryid=banners TARGET=_top><IMG SRC=http://www.proflowers.com/affiliates/images/Hollis2.gif BORDER=0 WIDTH=468 HEIGHT=60 ALT=\"Proflowers Ad Banners\"></A>\n");
    } elsif ($searchstring eq "gt") {
       &out("<a href=http://www2.burstnet.com/ads/ad3318b-map.cgi><img src=http://www2.burstnet.com/cgi-bin/ads/ad3318b.cgi ismap border=0 height=60 width=468></a>\n");
    } else {
       &out("<a href=http://www2.burstnet.com/ads/ad3318b-map.cgi><img src=http://www2.burstnet.com/cgi-bin/ads/ad3318b.cgi ismap border=0 height=60 width=468></a>\n");
    }
    &out( "</center>\n");
    &out("<p></p>\n");
    &out("<center>\n");
    &out("Categories show the average price of a store's items.");
    &out("<br>\n");
    &out("<A HREF=/cgibin/catsearch.pl?searchstring=lt>&lt; \$50</A> <IMG SRC=/images/sdi/bl_sdi.gif ALT=\"~~\"></A>  \n");
    &out("<A HREF=/cgibin/catsearch.pl?searchstring=50->\$50 - \$125</A> <IMG SRC=/images/sdi/cy_sdi.gif ALT=\"~~\"></A>  \n");
    &out("<A HREF=/cgibin/catsearch.pl?searchstring=125->\$125 - \$600</A> <IMG SRC=/images/sdi/yl_sdi.gif ALT=\"~~\"></A>  \n");
    &out("<A HREF=/cgibin/catsearch.pl?searchstring=600->\$600 - \$1250</A> <IMG SRC=/images/sdi/pi_sdi.gif ALT=\"~~\"></A>  \n");
    &out("<A HREF=/cgibin/catsearch.pl?searchstring=gt>&gt; \$1250</A>\n");
    &out("<br>\n");
    &out("Non-Jewelry: <A HREF=/cgibin/catsearch.pl?searchstring=accessories>Boxes/cleaning/safety clasps</A>\n");
    &out(" <IMG SRC=/images/sdi/cy_sdi.gif ALT=\"~~\">\n");
    &out("<A HREF=/cgibin/catsearch.pl?searchstring=tools>tools</A>\n");
    &out("</center>\n");
    &out("<p></p>\n");

    &out("<p>\n");
    &out("<center>\n");
    &out("<FORM METHOD=POST ACTION=\"/cgibin/search.pl\">\n");
    &out("<FONT SIZE=4 COLOR=000000 FACE=\"ARIAL,TIMES NEW ROMAN\">S e a r c h  &nbsp; O u r &nbsp; D a t a b a s e</FONT><br>\n");
    &out("<INPUT NAME=\"searchstring\" SIZE=\"30\" MAXLENGTH=\"30\">\n");
    &out("<INPUT TYPE=SUBMIT VALUE=\"Search\"><BR>\n");
    &out("<FONT SIZE=3>Use spaces to separate keywords</FONT>\n");
    &out("</FORM>\n");
    &out("</center>\n");
    &out("</p>\n");
    if ($searchstring eq "lt") {
      &out("<h3>Average price: &nbsp; Under \$50 </h3>");
    }
    elsif ($searchstring eq "50-") {
      &out("<h3>Average price: &nbsp; \$50 - \$125 </h3>");
    }
    elsif ($searchstring eq "125-") {
      &out("<h3>Average price: &nbsp; \$125 - \$600 </h3>");
    }
    elsif ($searchstring eq "600-") {
      &out("<h3>Average price: &nbsp; \$600 - \$1250 </h3>");
    }
    elsif ($searchstring eq "gt") {
      &out("<h3>Average price: &nbsp; Over \$1250 </h3>");
    }
    elsif ($searchstring eq "tools") {
      &out("<h3>Retail Tools for Crafters</h3>");
    }
    elsif ($searchstring eq "accessories") {
      &out("<h3>Jewelry Storage/Cleaning/Safety Clasps</h3>");
    }

    &out( "<UL>\n");
    $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);
    &out( "</UL>\n");
    if ($searchstring eq "tools") {
       &out("<p>More tools can be found for businesses on our <a href=/cgibin/wcatsearch.pl?searchstring=Tools>Jewelry Business Tools</a> page</p>");
    }

    if ($counter == 0) {
       &out( "<BR><center><B> Sorry, No Matches were found.</B></center><p></p>\n");
    }

    &out("<p></p>\n");
    &out("<br>\n");
    &out("<p></p>\n");
    &out("<h4>\n");
    &out("<center>\n");
    &out("<A HREF=/birthstones.html>Birthstones</A> ~ \n");
    &out("<A HREF=/gemshows.html>Gemstone and Jewelry Shows</A> ~ \n");
    &out("<A HREF=/therapy.html>Gemstone Therapy</A>\n");
    &out("</center>\n");
    &out("<p></p>\n");
    &out("<center>\n");
    &out(" ~ \n");
    &out("<A HREF=/stores.html>Stores</A> ~ \n");
    &out("<A HREF=/malls.html>Malls</A> ~ \n");
    &out("<A HREF=/wholesale.html>Wholesale</A> ~\n");
    &out("<A HREF=/join.html>Add URL</A> ~ \n");
    &out("<A HREF=/diamond-guide.html>Diamond Guide</A>\n");
    &out(" ~ \n");
    &out("</center>\n");
    &out("<p></p>\n");
    &out("<center>\n");
    &out("<A HREF=/jewelryjokes.html>Jewelry Jokes</A> ~ \n");
    &out("<A HREF=/jewellinks.html>Some Jewelry Related Links</A> ~ \n");
    &out("<A href=/about.html>About Us</A> ~ \n");
    &out("<A HREF=/links.html>Other Links</A>  \n");
    &out("</center>\n");
    &out("<p></p>\n");
    &out("</h4>\n");
    &out("<p></p>\n");
    &out("<center>\n");
    &out("<IMG ALIGN=center SRC=/images/line_col.gif ALT=\"thin line\"></center>\n");
    &out("<center>\n");
    &out("For questions or comments, please send them to \n");
    &out("<A href=\"mailto:davebraun\@jewelrymall.com\">\n");
    &out("<address>davebraun\@jewelrymall.com</address></A>\n");
    &out("<p></p>\n");
    &out("Pages are &copy Copyright 1995-1999 Jewelry Mall.\n");
    &out("All rights reserved.\n");
    &out("<br>\n");
    &out("http://www.jewelrymall.com\n");
    &out("</center>\n");
    &out("<br>\n");
    &out("</BODY></HTML>\n");


#########################################
#
#  Print the matched record
#
#########################################
sub print_record {
      &out( "<font color=teal>". $category . " </font><strong><A HREF=" . $linkurl . ">" . $linktitle . "</A></strong> ");
      # print secure.gif
      if ($linkgif =~ /secure.gif/) {
           &out( "<img src=/images/" . $linkgif . " ALT=\"Secure Ordering\" width=100 height=14> ");    
      } 
      # print giftwrap.gif
      if ($wrapgif =~ /giftwrap.gif/) {
           &out( "<img src=/images/" . $wrapgif . " ALT=\"Gift Wrap\" width=80 height=14> ");    
      } 
      if ($overnightgif =~ /overnight.gif/) {
           &out( "<img src=/images/" . $overnightgif . " ALT=\"Overnight Delivery\" width=100 height=14> ");
      }
       &out( "<br>" . $linkdescrip . "<p>\n");    
}



##########################################
#
#  Check to see if record matches search criteria
#
##########################################
sub check_record {
    # get the data from the record read from the file. $tabledata

   $category = $tabledata[0];
   $linkurl   = $tabledata[1];
   $linktitle = $tabledata[2];
   $linkgif =  $tabledata[3];
   $wrapgif =  $tabledata[4];
   $overnightgif =  $tabledata[5];
   $linkdescrip = $tabledata[6];
   $linkwords = $tabledata[7];
   $linemail   = $tabledata[8];
   chop($linemail);

    # skip cmment lines
    # otherwise map fields
    if ($category =~ /^#/){
         $searchline = "";
    } else {
       #build the search line with all fields we want to search in
       $searchline =  $category;
    }
   
   #search by keywords
   # only perform the keyword search if the length of the search string is greater than 2
   # don't think we want people to search for and  or or etc.
   $sfound = 0;
   $found = 0;
   $notfound = 1; 

   $stlen = length($searchstring);
   if ($stlen > 1) {
       @words = split(/ +/,$searchstring);
        foreach $aword (@words) {
           if ($searchline =~ /\b$aword/i) {
                  $sfound = 1;
           } 
           else {
                  $notfound = 0;
            }
         }
     }
    if ($sfound == 1 && $notfound == 1) {
        $found = 1;
     }

    # if search string is too small .. set found to 1
    if ($stlen <= 1) {
        $found = 1;
    }
    #if page doesn't have a title then return not found
    $tlen = length($linktitle);
    if ($tlen < 1) {
        $found = 0;
    }
}

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

THE_END

############################################
#
#  Add Record -- not used right now
#
############################################
sub addrecord {
  $category = $FORM{'category'};
  $linktitle  = $FORM{'linktitle'};
  $linkdescrip  = $FORM{'linkdescrip'};
  $linkwords    = $FORM{'linkwords'};
  $linkemail   = $FORM{'linkemail'};
  $linkurl = $FORM{'linkurl'};
  $linkgif = $FORM{'linkgif'};

  #  Convert < tags to &lt;
  $linktitle =~ s/</\&lt;/g;
  $linkdescrip =~ s/</\&lt;/g;
  $linkwords =~ s/</\&lt;/g;
  $linkemail =~ s/</\&lt;/g;
  $linkurl =~ s/</\&lt;/g;
  $category =~ s/</\&lt;/g;
  $category =~ s/>/\&gt;/g;
  &open_file("FILE1",">>",$filename);

  &write_file("FILE1",$category . "|" . $linkurl . "|" . $linktitle . "|". $linkgif . "|". $linkdescrip. "|" .$linkwords ."|" .$linkemail ."\n");
   close("FILE1");
   print "Content-type: text/html\n\n";
   print "<html><head><title>Thank You</title></head>\n";
   print "<body><BR><h3><CENTER>Thank You For Adding to this database</CENTER></h3>\n";
   print "</body></html>\n";
   exit;
}

