<?
ob_start();

 include("header.php3");

// to go back to ALL LOTS comment out line 5 below
// $andLotnumber  = " and lotnumber < 1000 ";


?> <TITLE>Millennium Philatelic Auctions Search</title> 
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen> 
      <div align="left"></div>
      <div align="left"><span class="legalgrey"> </span></div>
      <div align="left"> 
        <table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr> 
            <td>

<? if ($lot_dbname=="lots"){  // ie the current auction ?>      
   <? if ($auction_finished) { ?>
      <? if ($nobidsyet) { ?>
         <? fileprint("search_postauction_nobids.txt");?>
      <? } else { ?>
         <? fileprint("search_postauction.txt");?>
      <? } ?>
   <? } else { ?>
      <? fileprint("search.txt");?>
   <? } ?> 

<? } else { ?>

   <? 
      $tmp="select name,date_held from past_auctions where id=$past_auction";
      $r=mysql_query($tmp);
      if (mysql_error()){
       //  echo mysql_error();
       //  echo "<P>$tmp ";
      }
      @$auction_title=mysql_result($r,0,"name");
      @$auction_date=mysql_result($r,0,"date_held");
      if ($auction_title) {
         echo "<p><strong><font size=+1>$auction_title</font>";
         echo "</strong>";
      }
   ?>

<? } ?>


</td>
          </tr>
        </table>
        <br>
      </div>
      <?
$numrows=0; //default

if ($showcategory){
   list ($cat,$subcat,$subcat2,$subcat3)=split(",",$showcategory);
   if ($past_auction=="pending"){
      echo "<center><h2>Pending Lots</h2></center>";
   }
   if ($auction_finished){
      $qry="select * from $lot_dbname where category='$cat' ";   
      if ($subcat) $qry .= "and subcategory='$subcat' ";
      if ($subcat2) $qry .= "and subcategory2='$subcat2' ";
      if ($subcat3) $qry .= "and subcategory3='$subcat3' ";
      if ($unsold_only) $qry .= "and (realised_price='' or realised_price='0') ";
   } else {
      $qry="select * from $lot_dbname where category='$cat' ";   
      if ($subcat) $qry .= "and subcategory='$subcat' ";
      if ($subcat2) $qry .= "and subcategory2='$subcat2' ";
      if ($subcat3) $qry .= "and subcategory3='$subcat3' ";
   }
   $qry .= $andLotnumber ;
   $qry .= " order by lotnumber";
    //echo "<P>qry=$qry";

   $result=mysql_query($qry);
   $numrows=mysql_num_rows($result);
   
   // brad april
   
   mysql_data_seek($result, 0);// reset SQL poiner to the START

   $showcategory=trim($showcategory);
   
//   echo "<P>Category: <B>" . ereg_replace("," , ", ",$showcategory) . "</b>";
   while (substr($showcategory,0,1)==',') { // trim leading comma
      $showcategory=substr($showcategory,1);
   }
   while (substr($showcategory,strlen($showcategory)-1,1)==',') { // trim trailing
      $showcategory=substr($showcategory,0,strlen($showcategory)-1);
   }


   // show this in showresults under the status message and codes link
   $displayCategory="<P><B>" . ereg_replace("," , ", ",$showcategory) . "</b>";
   // echo "<BR>$numrows Lots found";
   if ($numrows){
      // find next cat - cant restriect by sold/unsold dummy
      $q1="select lotnumber from $lot_dbname where category='$cat' ";   
      if ($subcat) $q1 .= "and subcategory='$subcat' ";
      if ($subcat2) $q1 .= "and subcategory2='$subcat2' ";
      if ($subcat3) $q1 .= "and subcategory3='$subcat3' ";
      // $qry .= $andLotnumber ;
      $q1 .= " order by lotnumber";
      //echo $q1;
      $r1=mysql_query($q1);
      $n1=mysql_num_rows($r1);
      if ($n1){
         $prevcat_lot=mysql_result($r1,0,'lotnumber') -1;
         $nextcat_lot=mysql_result($r1,$n1-1,'lotnumber') +1 ;
      }
   } else { 
   	// we might be at the END or an EMPTY CATEGORY in post auction mode
   	echo "<p>&nbsp;<p>&nbsp;<p align=center><font size=3><strong>There are no Lots in this Category.</strong></font><br><br><font size=2>Please <a href=catalog.php3?past_auction=0&unsold_only=1><strong>click here</strong></a> to go back to the Unsold Lots page.</font></p><p>&nbsp;<p>&nbsp;<p>&nbsp;\n";
      $prevcat_lot=0;
      $nextcat_lot=0;
      $LASTLOT+=0;
      $q1="select lotnumber from $lot_dbname where lotnumber>$MAXLOT   ";   
      if ($unsold_only) $q1 .= "and (realised_price='' or realised_price='0') ";
      $q1 .=" order by lotnumber ASC limit 1 ";
      
      // echo $q1;
      $r1=mysql_query($q1);
      $n1=mysql_num_rows($r1);
      if ($n1){
  //       $prevcat_lot=mysql_result($r1,0,'lotnumber') -1;
         $nextcat_lot=mysql_result($r1,$n1-1,'lotnumber') +1 ;
      }
      
      if ($nextcat_lot<$LASTLOT) { // if there are NO LOTS set this to jump ahead next time
      	$nextcat_lot=$LASTLOT+1;

      }
   }
    //echo "<P>LASTLOT=$LASTLOT, prevcat_lot=$prevcat_lot, nextcat_lot=$nextcat_lot";
   // $result=mysql_query($qry);
}

if ($searchtext){
   // allow prev / next page links in showresults for search only ?
   $start=intval($start);
   $max=intval($max);
   if ($max==0) {
      $max=15; // default
   }
   $tosearchtext=strtolower($searchtext);

/**************
now show all lots but with sOLD on soled ones 28/10/2002

   if ($auction_finished){
      if ($totalresults==0) {
         //$qry="select * from $lot_dbname where lcase(description) rlike '$tosearchtext'  and (realised_price='' or realised_price='0')";
         $result=mysql_query($qry);
         echo mysql_error();
         if ($result)  $totalresults=mysql_num_rows($result);
      }
   } else {
   }
*****************/
      if ($totalresults==0){ // only need to do this once for every search then pass it along next time

         $qry="select * from $lot_dbname where lcase(description) rlike '$tosearchtext' $andLotnumber";


         $result=mysql_query($qry);
         echo mysql_error();
         if ($result) $totalresults=mysql_num_rows($result);
      }

      $toshowstart=$start+1;
   
      if  ($totalresults < ($start+$max)) {
         $toshowtotalresults=$totalresults;
      } else {
         $toshowtotalresults= $start+$max;
      }   

      echo "Found <b>$totalresults</b> lots matching your search word '<b>$tosearchtext</b>'. Showing results $toshowstart to $toshowtotalresults." ;


      $qry="select * from $lot_dbname where lcase(description) rlike '$tosearchtext' $andLotnumber order by lotnumber ASC limit $start,$max";


//   echo "<P>qry=$qry";
   $result=mysql_query($qry);
   echo mysql_error();
   $numrows=mysql_num_rows($result) ;
//   echo " found $numrows results for search phrase \"<b>$searchtext</b>\"";
}   

if ($searchlot) {

   $qry="select * from $lot_dbname where lotnumber=$searchlot $andLotnumber";
   
   if ($auction_finished) {
//      $qry.="  and (realised_price='' or realised_price='0') ";
   }
//   echo "<P>qry=$qry";
   $result=mysql_query($qry);
   echo mysql_error();
   $numrows=mysql_num_rows($result);
   if ($numrows==0) {
      echo "<P>&nbsp;<P>&nbsp;<P>&nbsp;Lot Number <b>$searchlot</b> is invalid.<P>&nbsp;<P>&nbsp;<P>&nbsp;";
   }
}
   
   $inshortlist=0; // not
   include ("showresults.php3");

if ($showcategory){

   echo "<center>\n ";
   if ($prevcat_lot>0){
      $qry="select category,subcategory,subcategory2,subcategory3 from $lot_dbname where lotnumber=$prevcat_lot";      
      $result=mysql_query($qry);
      if (mysql_num_rows($result)){
      list ($category,$subcategory,$subcategory2,$subcategory3)=mysql_fetch_row($result);
      $linktext=urlencode("$category,$subcategory,$subcategory2,$subcategory3");
      echo "[ <a href=search.php3?showcategory=$linktext&unsold_only=$unsold_only&past_auction=$past_auction&LASTLOT=$prevcat_lot><span title=\"BACK TO: $category,$subcategory,$subcategory2,$subcategory3\">Previous Category</span></a> ]  ";
      }
   }
   
   $qry="select category,subcategory,subcategory2,subcategory3 from $lot_dbname where lotnumber=$nextcat_lot";
   $result=mysql_query($qry);
   if (mysql_num_rows($result)>0) {
      list ($category,$subcategory,$subcategory2,$subcategory3)=mysql_fetch_row($result);
      $linktext=urlencode("$category,$subcategory,$subcategory2,$subcategory3");
	  echo "[ <a href=search.php3?showcategory=$linktext&unsold_only=$unsold_only&past_auction=$past_auction&LASTLOT=$nextcat_lot><span title=\"FORWARD TO: $category,$subcategory,$subcategory2,$subcategory3\">Next Category</span></a> ]  ";
   } 
   echo "</center>";
}


if ($searchtext){ // can only page through searches
   echo "<center>";
   if ($start>0){
      $nextstart=$start-$max;
      echo "[ <a href=\"search.php3?searchtext=" . urlencode($searchtext) . "&start=" . $nextstart . "&totalresults=$totalresults&unsold_only=$unsold_only&past_auction=$past_auction\"><font size=1 face=arial>Previous $max Results</font></a> ]";
   } 
   $nextstart=$start+$max;
   if ($nextstart<$totalresults){
      echo "&nbsp;&nbsp;&nbsp;[ <a href=\"search.php3?searchtext=" . urlencode($searchtext) . "&start=" . $nextstart . "&totalresults=$totalresults&unsold_only=$unsold_only&past_auction=$past_auction\"><font size=1 face=arial>Next $max Results</font></a> ]";
   }
   echo "</center>";
}
?>
      <form method=post action="search.php3">
     <? if ($past_auction){ ?>
        <input type=hidden name=past_auction value="<? echo $past_auction; ?>">
      <? } ?>
        <table border=0 cellpadding=4>
          <TR> 
            <TD align=right>Keyword Search of Lot Descriptions</td>
            <TD> <input type=text size=20 name=searchtext value=""></td>
            <TD><input type=submit name="cmd_submit" value=" Search  "></td>
          </TR>
        </table>
      </form>
   <BR><BR><BR><BR><BR><BR><BR><BR>
      <? include("footer.php3");?>

