<?php
error_reporting(E_PARSE);
if($_GET['_SESSION'] != '' || $_POST['_SESSION'] != '' || $_COOKIE['_SESSION'] != '') { exit; }


###############################################################################
## Soholaunch(R) Site Management Tool
## Version 4.5
##      
## Author: 			Mike Johnston [mike.johnston@soholaunch.com]                 
## Homepage:	 	http://www.soholaunch.com
## Bug Reports: 	http://bugzilla.soholaunch.com
## Release Notes:	sohoadmin/build.dat.php
###############################################################################

##############################################################################
## COPYRIGHT NOTICE                                                     
## Copyright 1999-2003 Soholaunch.com, Inc. and Mike Johnston 
## Copyright 2003-2007 Soholaunch.com, Inc.
## All Rights Reserved.       
##                                                                        
## This script may be used and modified in accordance to the license      
## agreement attached (license.txt) except where expressly noted within      
## commented areas of the code body. This copyright notice and the comments  
## comments above and below must remain intact at all times.  By using this 
## code you agree to indemnify Soholaunch.com, Inc, its coporate agents   
## and affiliates from any liability that might arise from its use.                                                        
##                                                                           
## Selling the code for this program without prior written consent is       
## expressly forbidden and in violation of Domestic and International 
## copyright laws.  		                                           
###############################################################################


error_reporting(0);
session_cache_limiter('none');
session_start();
track_vars;

reset($HTTP_POST_VARS);
while (list($name, $value) = each($HTTP_POST_VARS)) {
		$value = htmlspecialchars($value);	// Bugzilla #13
		${$name} = $value;
}	

$THIS_DISPLAY = "";
$instant_browse_flag = 0;	// Used if Keyword Search returns 0 matches

##########################################################################
### LET'S MAKE SURE TO RETURN END-USER BACK TO PREVIOUS SEARCH REQUEST
### WHEN THEY ADD SOMETHING TO THEIR CART AND DECIDE TO "CONTINUE
### SHOPPING" AGAIN... THIS HAS BEEN AN OVERWHELMING REQUEST FROM SHOPPERS
##########################################################################

if (!session_is_registered("cont_shopping_string")) { session_register("cont_shopping_string"); }

	$cont_shopping_string = "?";

	reset($HTTP_POST_VARS);
	while (list($name, $value) = each($HTTP_POST_VARS)) {
		$cont_shopping_string .= "$name=$value&";
	}

	reset($HTTP_GET_VARS);
	while (list($name, $value) = each($HTTP_GET_VARS)) {
		$cont_shopping_string .= "$name=$value&";
	}

##########################################################################
### STEP 1:
### WE WILL NEED TO KNOW THE DATABASE NAME; UN; PW; ETC TO OPERATE THE  
### REAL-TIME EXECUTION.  THIS IS CONFIGURED IN THE isp.conf FILE      
##########################################################################

include("pgm-cart_config.php");
$dot_com = $this_ip;	// Assign dot_com variable to configured ip address

$test_lang = $lang["Search Results For"];

if ( strlen($test_lang) < 4 ) { // $lang array is either empty or about to display arifacts, so reload it
   
   // Register language settings from site_specs table
   // ----------------------------------------------------
   $selSpecs = mysql_query("SELECT * FROM site_specs");
   $getSpec = mysql_fetch_array($selSpecs);
   
   if ( $getSpec[df_lang] == "" ) {
      $language = "english.php";
      echo "getSpec[df_lang] = ($getSpec[df_lang])\n";
      exit;
   
   } else {
      $language = $getSpec[df_lang];
   }
   
   if ( $lang_dir != "" ) {
      $lang_include = $lang_dir."/".$language;
   } else {
      $lang_include = "../sohoadmin/language/$language";
   }
   
   include ("$lang_include");
   
   session_register("lang");
   session_register("language");
   session_register("getSpec");
}


##########################################################################
### STEP 2:
### READ DATABASED OPTIONS INTO MEMORY NOW
##########################################################################

$result = mysql_query("SELECT * FROM cart_options");
$OPTIONS = mysql_fetch_array($result);

//040406 - Pull Currency Info
$dSign = $OPTIONS[PAYMENT_CURRENCY_SIGN];
$dType = $OPTIONS[PAYMENT_CURRENCY_TYPE];

##########################################################################
### STEP 3: 
### IS THIS A KEYWORD SEARCH?  IF SO, PROCESS REQUEST NOW
##########################################################################

if ($find == 1) {

	// ------------------------------------------------------------------
	// This is a keyword search request ($searchfor holds keyword data)
	// First, lets parse any extra bullshit that the user may have
	// submitted along with his/her search -- they will do wierd shit
	// ------------------------------------------------------------------

	$searchfor = ltrim($searchfor);				// trim extra spaces from left of words
	$searchfor = rtrim($searchfor);				// trim extra spaces from end of words

	$searchfor = str_replace(",", " ", $searchfor);		// replace commas with a space
	$searchfor = str_replace(";", " ", $searchfor);		// replace semi-colons with a space
	$searchfor = str_replace("  ", " ", $searchfor);	// replace multiple spaces with a single space

	$SEARCH_KEYWORDS = split(" ", $searchfor);		// split keywords into an array
	$NUM_SEARCH_KEYS = count($SEARCH_KEYWORDS);		// get a count of the number of words entered

	$tmp_sql_string = "SELECT PRIKEY, PROD_SKU, PROD_CATNO, PROD_NAME, PROD_DESC, PROD_UNITPRICE, PROD_CATEGORY1, ";
	$tmp_sql_string .= "VARIANT_PRICE1, VARIANT_PRICE2, VARIANT_PRICE3, VARIANT_PRICE4, VARIANT_PRICE5, ";
	$tmp_sql_string .= "VARIANT_PRICE6, OPTION_DETAILPAGE, PROD_THUMBNAIL, PROD_FULLIMAGENAME, OPTION_KEYWORDS, ";
	$tmp_sql_string .= "OPTION_SECURITYCODE, OPTION_RECOMMENDSKU FROM cart_products WHERE ";

	$keyflag = 0;	// Set keyword flag to zero by default

	for ($x=0;$x<=$NUM_SEARCH_KEYS;$x++) {

		if ($SEARCH_KEYWORDS[$x] != "") {

			$SEARCH_KEYWORDS[$x] = strtoupper($SEARCH_KEYWORDS[$x]);	// Force search to be case-insensitive

			$tmp_sql_string .= "(UPPER(PROD_SKU) LIKE '%$SEARCH_KEYWORDS[$x]%' OR UPPER(PROD_CATNO) LIKE '%$SEARCH_KEYWORDS[$x]%' OR ";
			$tmp_sql_string .= "UPPER(PROD_NAME) LIKE '%$SEARCH_KEYWORDS[$x]%' OR UPPER(PROD_DESC) LIKE '%$SEARCH_KEYWORDS[$x]%' OR ";
			$tmp_sql_string .= "UPPER(OPTION_KEYWORDS) LIKE '%$SEARCH_KEYWORDS[$x]%') OR ";

			$keyflag = 1; 	// Let next routine know that we actually found keywords; in case of all search;
		}

	}

	// Kill extra "OR" or "WHERE" tag in sql string that is left over from loop or flag
	// ----------------------------------------------------------------------------------

	$tmp = strlen($tmp_sql_string);

	if ($keyflag == 1) {

		$parse = $tmp - 3;	// Parse last OR in keyword loop
		$KEYFLAGTXT = "AND";	// Adding extra control to NOT call products with no category or display off

	} else {

		$parse = $tmp - 6;	// Parse WHERE
		$KEYFLAGTXT = "WHERE"; // Adding extra control to NOT call products with no category or display off

	}

	$tmp_sql_string = substr($tmp_sql_string, 0, $parse);

	$tmp_sql_string .= " $KEYFLAGTXT (PROD_CATEGORY1 <> ' ' AND OPTION_DISPLAY <> 'N') ";

	// ----------------------------------------------------------------------------------
	// ADD SECURITY CODE [GROUPS] CONTROL OVER SEARCH RESULTS
	// ----------------------------------------------------------------------------------

	if (isset($GROUPS)) {

		$grp_check = "HAVING OPTION_SECURITYCODE IN (";

		$grp_tmp = split(";", $GROUPS);	// Split this user's sec code groups into individual array
		$grp_cnt = count($grp_tmp);		// How Many sec groups does this user have access to?

		for ($gl=0;$gl<=$grp_cnt;$gl++) {	// Start to build SQL "IN" cluster
			if ($grp_tmp[$gl] != "") {
				$grp_check .= "'$grp_tmp[$gl]', ";
			}
		}

		$grp_check .= "'Public')";

	} else {

		$grp_check = "HAVING OPTION_SECURITYCODE IN('Public')";

	}
			

	$tmp_sql_string .= "$grp_check";

	// ----------------------------------------------------------------------------------

	$result = mysql_query("$tmp_sql_string ORDER BY $OPTIONS[DISPLAY_RESULTSORT]");

	$TOTAL_FOUND = mysql_num_rows($result);	// Denote how many results where returned on query

	$searchfor = ucwords($searchfor);
	$display_searchfor = $searchfor;
	if ($searchfor == "") { $display_searchfor = "All Products"; }

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Determine the Prev/Next Function
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	if ($start == "") { $start = 1; }
	if ($end == "") { $end = $OPTIONS[DISPLAY_RESULTS]; }
	if ($TOTAL_FOUND < $end) { $end = $TOTAL_FOUND; }

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	$THIS_DISPLAY = "<CENTER><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0 class=text>\n";
	$THIS_DISPLAY .= "<TR><TD ALIGN=LEFT VALIGN=MIDDLE>".$lang["Search Results For"].": <U>$display_searchfor</U></TD>\n";
	$THIS_DISPLAY .= "<TD ALIGN=RIGHT VALIGN=MIDDLE>\n";

	if ($TOTAL_FOUND != 0) {
		$THIS_DISPLAY .= $lang["Displaying"]." $start-$end of ";
	}

	$THIS_DISPLAY .= "$TOTAL_FOUND ".$lang["Matches Found"].".\n";
	$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

	// -----------------------------------------------------------------
	// Set up button click Javascript action for linking to the 
	// "More Info" page. (Used with the User defined Button and the
	// Buy Now! button. (Buy Now button is changed to "Add to Cart"
	// if that option is on.
	// -----------------------------------------------------------------

	$THIS_DISPLAY .= "<SCRIPT LANGUAGE=Javascript>\n<!--\n\n";
	$THIS_DISPLAY .= "     function userbutton(prikey) {\n";
	$THIS_DISPLAY .= "          var strlink = \"pgm-more_information.php?id=\"+prikey+\"&=SID#MOREINFO\";\n";
	$THIS_DISPLAY .= "          window.location = strlink;\n";
	$THIS_DISPLAY .= "     }\n\n";
	$THIS_DISPLAY .= "// -->\n</SCRIPT>\n\n";

	// -----------------------------------------------------------------
	// Loop Through Query Results and place inside of the product
	// search template.
	// -----------------------------------------------------------------

	$count = 1;

	while ($PROD = mysql_fetch_array($result)) {

				if ($count >= $start && $count <= $end) {
					ob_start(); 
					include("prod_search_template.inc");
					$THIS_DISPLAY .= ob_get_contents(); 
					ob_end_clean(); 			
				}

				$count++;
	}

	if ($TOTAL_FOUND == 0) {

		$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=10 class=text>\n";
		$THIS_DISPLAY .= "<TR><TD ALIGN=CENTER VALIGN=MIDDLE><FONT COLOR=RED>\n";
		$THIS_DISPLAY .= "<B>".$lang["Sorry, no products were found that match your search criteria."]."<BR>".$lang["Please try again or browse the suggested selections below."]."\n";
		$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

		$instant_browse_flag = 1;	// Tell Browse Function we're coming...
		$browse = 1;			// Since Nothing was found; might as well browse

	} else {

		$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=text>\n";
		$THIS_DISPLAY .= "<TR><TD ALIGN=CENTER VALIGN=MIDDLE>\n";
	
		if ($TOTAL_FOUND != 0) {
			$THIS_DISPLAY .= $lang["Displaying"]." $start-$end of ";
		}

		$THIS_DISPLAY .= "$TOTAL_FOUND ".$lang["Matches Found"].".\n";
		$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

		$tmp = $start;

		$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=text>\n";
		$THIS_DISPLAY .= "<TR>\n";

			$tend = $start - 1;
			$tstart = $tend - $OPTIONS[DISPLAY_RESULTS];

			if ($tmp != 1) {

				if ($tstart == 0) { $tstart = 1; }

				$THIS_DISPLAY .= "<TD ALIGN=CENTER VALIGN=MIDDLE>\n";

				$THIS_DISPLAY .= "<FORM METHOD=POST ACTION=start.php>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=searchfor VALUE=\"$searchfor\">\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=find VALUE=1>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=start VALUE=$tstart>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=end VALUE=$tend>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=SUBMIT VALUE=\"<< PREV\" CLASS=FormLt1>\n";
				$THIS_DISPLAY .= "</FORM>\n";

				$THIS_DISPLAY .= "</TD>\n";

			}

			$tstart = $end + 1;
			$tend = $tstart + $OPTIONS[DISPLAY_RESULTS] - 1;

			if ($TOTAL_FOUND > $end) {

				if ($TOTAL_FOUND <= $tend) { $tend = $TOTAL_FOUND; }

				$THIS_DISPLAY .= "<TD ALIGN=CENTER VALIGN=MIDDLE>\n";

				$THIS_DISPLAY .= "<FORM METHOD=POST ACTION=start.php>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=searchfor VALUE=\"$searchfor\">\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=find VALUE=1>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=start VALUE=$tstart>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=end VALUE=$tend>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=SUBMIT VALUE=\"NEXT >>\" CLASS=FormLt1>\n";
				$THIS_DISPLAY .= "</FORM>\n";

				$THIS_DISPLAY .= "</TD>\n";

			}

		$THIS_DISPLAY .= "</TR></TABLE>\n\n";

	}

} // End $find == 1 Statement

##########################################################################
### STEP 4: 
### IS THIS A "BROWSE" REQUEST? IF SO, PROCESS NOW.
### 
### DEVNOTE: A BROWSE REQUEST MUST BE DENOTED BY A browse=1 GET OR POST
### VARIABLE WHEN CALLING THIS FUNCTION. OPTIONALLY, THE cat=2 VARIABLE
### CAN BE INCLUDED TO BROWSE THROUGH A SPECIFIC CATEGORY NUMBER. IF NO
### CATEGORY NUMBER IS PASSED, WE ASSUME BROWSING OF "ALL" SKUS.
##########################################################################

if ($browse == 1) { 

	$tmp_sql_string = "SELECT PRIKEY, PROD_SKU, PROD_CATNO, PROD_NAME, PROD_DESC, PROD_UNITPRICE, PROD_CATEGORY1, ";
	$tmp_sql_string .= "VARIANT_PRICE1, VARIANT_PRICE2, VARIANT_PRICE3, VARIANT_PRICE4, VARIANT_PRICE5, ";
	$tmp_sql_string .= "VARIANT_PRICE6, OPTION_DETAILPAGE, PROD_THUMBNAIL, PROD_FULLIMAGENAME, OPTION_KEYWORDS, ";
	$tmp_sql_string .= "OPTION_SECURITYCODE, OPTION_RECOMMENDSKU FROM cart_products WHERE ";

	if ($cat != "") {

         
          /*
         $aCatSet = explode(";", $cat);
         $aWhere = array();
         foreach($aCatSet as $sCatGroup)
           {
             $aCatGroup = explode(",", $sCatGroup);
             switch (count($aCatGroup))
               {
               case 1:
               $sCat1 = $aCatGroup[0];
               $sLoop = "(PROD_CATEGORY1='$sCat1' OR PROD_CATEGORY2='$sCat1' OR PROD_CATEGORY3='$sCat1')";
               $aWhere[] = $sLoop;
               break;
               
               case 2:
               $sCat1 = $aCatGroup[0];
               $sCat2 = $aCatGroup[1];
               $sLoop = "(PROD_CATEGORY1='$sCat1' AND (PROD_CATEGORY2='$sCat2' OR PROD_CATEGORY3='$sCat2')) OR ";
               $sLoop .= "(PROD_CATEGORY1='$sCat2' AND (PROD_CATEGORY2='$sCat1' OR PROD_CATEGORY3='$sCat1')) OR ";
               $sLoop .= "((PROD_CATEGORY2='$sCat2' AND PROD_CATEGORY3='$sCat1')) OR ";
               $sLoop .= "((PROD_CATEGORY2='$sCat1' AND PROD_CATEGORY3='$sCat2'))";
               $aWhere[] = $sLoop;
               break;
               
               case 3:
               $sCat1 = $aCatGroup[0];
               $sCat2 = $aCatGroup[1];
               $sCat3 = $aCatGroup[2];
               $sLoop = "(PROD_CATEGORY1='$sCat1' AND ";
                     "((PROD_CATEGORY2='$sCat2' AND PROD_CATEGORY3='$sCat3') OR
                       (PROD_CATEGORY2='$sCat3' AND PROD_CATEGORY3='$sCat2'))) OR ";
               $sLoop .= "(PROD_CATEGORY1='$sCat2' AND ";
                     "((PROD_CATEGORY2='$sCat1' AND PROD_CATEGORY3='$sCat3') OR
                       (PROD_CATEGORY2='$sCat3' AND PROD_CATEGORY3='$sCat1'))) OR ";
               $sLoop .= "(PROD_CATEGORY1='$sCat3' AND ";
                     "((PROD_CATEGORY2='$sCat2' AND PROD_CATEGORY3='$sCat1') OR
                       (PROD_CATEGORY2='$sCat1' AND PROD_CATEGORY3='$sCat2')))";
               $aWhere[] = $sLoop;
               break;
               
               
               default:
               }
 
           }
 
         if (0 == count($aWhere))
             $tmp_sql_string .= "OPTION_DISPLAY = 'Y' ";
         else
             $tmp_sql_string .= "(".implode(" OR ", $aWhere).") AND OPTION_DISPLAY = 'Y' ";
         //$tmp_sql_string .= "(PROD_CATEGORY1 = '$cat' OR PROD_CATEGORY2 = '$cat' OR PROD_CATEGORY3 = '$cat') AND OPTION_DISPLAY = 'Y' ";
 
         */                            

		$pass_cat = $cat; // For use with Next/Prev Buttons

		$tmp_sql_string .= "(PROD_CATEGORY1 = '$cat' OR PROD_CATEGORY2 = '$cat' OR PROD_CATEGORY3 = '$cat') AND OPTION_DISPLAY = 'Y' ";

		// --------------------------------------------------------------
		// Also, get the category REAL NAME for display in search results
		// --------------------------------------------------------------

			$catname = mysql_query("SELECT category FROM cart_category WHERE keyfield = '$cat'");
			$this_cat = mysql_fetch_array($catname);
			$BROWSECAT = $this_cat[category];

		// --------------------------------------------------------------

	} else {

		$pass_cat = ""; // For use with Next/Prev Buttons

		$tmp_sql_string .= "PROD_CATEGORY1 <> ' ' AND OPTION_DISPLAY <> 'N' ";
		$BROWSECAT = "All";	// Because no category is defined, we are searching all

	}

	// ----------------------------------------------------------------------------------
	// ADD SECURITY CODE [GROUPS] CONTROL OVER SEARCH RESULTS
	// ----------------------------------------------------------------------------------

	if (isset($GROUPS)) {

		$grp_check = " HAVING OPTION_SECURITYCODE IN (";

		$grp_tmp = split(";", $GROUPS);	// Split this user's sec code groups into individual array
		$grp_cnt = count($grp_tmp);		// How Many sec groups does this user have access to?

		for ($gl=0;$gl<=$grp_cnt;$gl++) {	// Start to build SQL "IN" cluster
			if ($grp_tmp[$gl] != "") {
				$grp_check .= "'$grp_tmp[$gl]', ";
			}
		}

		$grp_check .= "'Public')";

	} else {

		$grp_check = " HAVING OPTION_SECURITYCODE IN ('Public')";

	}
			

	$tmp_sql_string .= "$grp_check";

	// --------------------------------------------------------------------------
	// Make SQL Query Now and Sort by the Field Name defined in display options
	// --------------------------------------------------------------------------

	// echo "\n\n<!-- $tmp_sql_string \n\n-->\n\n";
	
	$result = mysql_query("$tmp_sql_string ORDER BY $OPTIONS[DISPLAY_RESULTSORT]");

	$TOTAL_FOUND = mysql_num_rows($result);	// Denote how many results where returned on query

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Determine the Prev/Next Function
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	if ($start == "") { $start = 1; }
	if ($end == "") { $end = $OPTIONS[DISPLAY_RESULTS]; }
	if ($TOTAL_FOUND < $end) { $end = $TOTAL_FOUND; }

	// --------------------------------------------------------------
	// Build Display Variable
	// --------------------------------------------------------------
	
	if ($instant_browse_flag != 1) {	// If keyword search returns 0 matches, this flag is turned on

		$THIS_DISPLAY .= "<CENTER><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=0 class=text>\n";
		$THIS_DISPLAY .= "<TR><TD ALIGN=LEFT VALIGN=MIDDLE>Browsing Category: <U>$BROWSECAT</U></TD>\n";
		$THIS_DISPLAY .= "<TD ALIGN=RIGHT VALIGN=MIDDLE>\n";

		if ($TOTAL_FOUND != 0) {
			$THIS_DISPLAY .= "Displaying $start-$end of ";
		}

		$THIS_DISPLAY .= "$TOTAL_FOUND Found.\n";
		$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

	}

	// -----------------------------------------------------------------
	// Set up button click Javascript action for linking to the 
	// "More Info" page. (Used with the User defined Button and the
	// Buy Now! button. (Buy Now button is changed to "Add to Cart"
	// if that option is on.
	// -----------------------------------------------------------------

	$THIS_DISPLAY .= "<SCRIPT LANGUAGE=Javascript>\n<!--\n\n";
	$THIS_DISPLAY .= "     function userbutton(prikey) {\n";
	$THIS_DISPLAY .= "          var strlink = \"pgm-more_information.php?id=\"+prikey+\"&=SID#MOREINFO\";\n";
	$THIS_DISPLAY .= "          window.location = strlink;\n";
	$THIS_DISPLAY .= "     }\n\n";
	$THIS_DISPLAY .= "// -->\n</SCRIPT>\n\n";

	// -----------------------------------------------------------------
	// Loop Through Query Results and place inside of the product
	// search template. 
	//
	// DEVNOTE: WE TURN THE "OUTPUT BUFFER" OFF WHILE WE LET THE INCLUDE
	// DO ITS WORK AND PLACE THE OUTPUT INTO THE $THIS_DISPLAY VAR.
	// THIS WAY WE CAN MODIFY OUR INCLUDE TO PERFORM JUST LIKE NORMAL
	// HTML AS MUCH AS POSSIBLE.
	// -----------------------------------------------------------------

	$count = 1;	// Keep Number to Display on Search in Count Var

	while ($PROD = mysql_fetch_array($result)) {

				if ($count >= $start && $count <= $end) {

					ob_start(); 
					include("prod_search_template.inc");
					$THIS_DISPLAY .= ob_get_contents(); 
					ob_end_clean(); 			

				}

				$count++;		

	}

	if ($TOTAL_FOUND == 0) {

		$THIS_DISPLAY .= "<BR><BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=10 class=text>\n";
		$THIS_DISPLAY .= "<TR><TD ALIGN=CENTER VALIGN=MIDDLE><FONT COLOR=RED>\n";
		$THIS_DISPLAY .= "<B>Sorry, there are currently no products in this category.<BR><BR>Please check back soon.\n";
		$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

	} else {

	// ------------------------------------------------------------------
	// Display Previous And Next Buttons
	// ------------------------------------------------------------------

		$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=text>\n";
		$THIS_DISPLAY .= "<TR><TD ALIGN=CENTER VALIGN=MIDDLE>\n";
	
		if ($TOTAL_FOUND != 0) {
			$THIS_DISPLAY .= "Displaying $start-$end of ";
		}

		$THIS_DISPLAY .= "$TOTAL_FOUND Matches Found.\n";
		$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

		$tmp = $start;

		$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=text>\n";
		$THIS_DISPLAY .= "<TR>\n";

			$tend = $start - 1;
			$tstart = $tend - $OPTIONS[DISPLAY_RESULTS];

			if ($tmp != 1) {

				if ($tstart == 0) { $tstart = 1; }

				$THIS_DISPLAY .= "<TD ALIGN=CENTER VALIGN=MIDDLE>\n";

				$THIS_DISPLAY .= "<FORM METHOD=POST ACTION=start.php>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=cat VALUE=\"$pass_cat\">\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=browse VALUE=1>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=start VALUE=$tstart>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=end VALUE=$tend>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=SUBMIT VALUE=\"<< ".$lang["PREV"]."\" CLASS=FormLt1>\n";
				$THIS_DISPLAY .= "</FORM>\n";

				$THIS_DISPLAY .= "</TD>\n";

			}

			$tstart = $end + 1;
			$tend = $tstart + $OPTIONS[DISPLAY_RESULTS];
			
			if ($TOTAL_FOUND > $end) {

				if ($TOTAL_FOUND <= $tend) { $tend = $TOTAL_FOUND; }
				
				$THIS_DISPLAY .= "<TD ALIGN=CENTER VALIGN=MIDDLE>\n";

				$THIS_DISPLAY .= "<FORM METHOD=POST ACTION=start.php>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=cat VALUE=\"$pass_cat\">\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=browse VALUE=1>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=start VALUE=$tstart>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=HIDDEN NAME=end VALUE=$tend>\n";
				$THIS_DISPLAY .= "<INPUT TYPE=SUBMIT VALUE=\"".$lang["NEXT"]." >>\" CLASS=FormLt1>\n";
				$THIS_DISPLAY .= "</FORM>\n";

				$THIS_DISPLAY .= "</TD>\n";

			}

		$THIS_DISPLAY .= "</TR></TABLE>\n\n";

	} // End TOTAL_NUMBER = 0 IF


} // End $browse == 1 Statement

##########################################################################
### STEP 5: 
### SETUP SEARCH COLUMN HTML FOR DISPLAY (REGARDLESS OF FUNCTION CALL)
##########################################################################

$SEARCH_COLUMN = "";

ob_start(); 
	include("prod_search_column.inc");
	$SEARCH_COLUMN .= ob_get_contents(); 
ob_end_clean(); 

##########################################################################
### STEP 6: 
### BUILD PRIVACY POLICY DISPLAY IF REQUESTED
##########################################################################

if ($policy == "privacy") {

	$THIS_DISPLAY = "<BR><BR>";
	$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=smtext STYLE='border: inset BLACK 1px;'>\n";
	$THIS_DISPLAY .= "<TR> \n";
	$THIS_DISPLAY .= "<TD  BGCOLOR='$OPTIONS[DISPLAY_HEADERBG]'><FONT COLOR=$OPTIONS[DISPLAY_HEADERTXT]><B><FONT FACE=\"Verdana, Arial, Helvetica, sans-serif\">".$lang["Privacy Policy"]."</FONT></B></FONT></TD>\n";
	$THIS_DISPLAY .= "</TR>\n";
	$THIS_DISPLAY .= "<TR> \n";
	$THIS_DISPLAY .= "<TD ALIGN=LEFT VALIGN=TOP>\n";
 
	// ------------------------------------------
	// Read Privacy Policy Statement into Memory
	// ------------------------------------------

	$filename = "$cgi_bin/privacy.txt";
	if (file_exists($filename)) {
		$file = fopen("$filename", "r") or DIE("Error: Could not open privacy policy ($filename)");
			$PRIVACY_POLICY = fread($file,filesize($filename));
		fclose($file);
	}

	// ------------------------------------------
	// Format textarea data to display as HTML
	// ------------------------------------------

	$PRIVACY_POLICY = chop($PRIVACY_POLICY);
	$PRIVACY_POLICY = str_replace("\n", "<BR>", $PRIVACY_POLICY);

	// ------------------------------------------
	// Add Policy Text to $THIS_DISPLAY
	// ------------------------------------------

	$THIS_DISPLAY .= $PRIVACY_POLICY;


	$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

} // End Privacy Policy Build

##########################################################################
### STEP 7: 
### BUILD ALL OTHER POLICIES
##########################################################################

if ($policy == "shipping" || $policy == "returns" || $policy == "other") {

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// DISPLAY SHIPPING POLICY #1
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	$THIS_DISPLAY = "<BR><BR>";
	$THIS_DISPLAY .= "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=smtext STYLE='border: inset BLACK 1px;'>\n";
	$THIS_DISPLAY .= "<TR> \n";
	$THIS_DISPLAY .= "<TD  BGCOLOR='$OPTIONS[DISPLAY_HEADERBG]'><FONT COLOR=$OPTIONS[DISPLAY_HEADERTXT]><B><FONT FACE=\"Verdana, Arial, Helvetica, sans-serif\">".$lang["Shipping Information"]."</FONT></B></FONT></TD>\n";
	$THIS_DISPLAY .= "</TR>\n";
	$THIS_DISPLAY .= "<TR> \n";
	$THIS_DISPLAY .= "<TD ALIGN=LEFT VALIGN=TOP>\n";
 
	// ------------------------------------------
	// Read Shipping Policy Statement into Memory
	// ------------------------------------------

	$filename = "$cgi_bin/cart_delivery.txt";
	if (file_exists($filename)) {
		$file = fopen("$filename", "r") or DIE("Error: Could not open privacy policy ($filename)");
			$SHIPPING_POLICY = fread($file,filesize($filename));
		fclose($file);
	}

	// ------------------------------------------
	// Format textarea data to display as HTML
	// ------------------------------------------

	$SHIPPING_POLICY = chop($SHIPPING_POLICY);
	$SHIPPING_POLICY = str_replace("\n", "<BR>", $SHIPPING_POLICY);

	// ------------------------------------------
	// Add Policy Text to $THIS_DISPLAY
	// ------------------------------------------

	$THIS_DISPLAY .= $SHIPPING_POLICY;


	$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// DISPLAY RETURNS/EXCHANGES POLICY #2
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	$THIS_DISPLAY .= "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=smtext STYLE='border: inset BLACK 1px;'>\n";
	$THIS_DISPLAY .= "<TR> \n";
	$THIS_DISPLAY .= "<TD  BGCOLOR='$OPTIONS[DISPLAY_HEADERBG]'><FONT COLOR=$OPTIONS[DISPLAY_HEADERTXT]><B><FONT FACE=\"Verdana, Arial, Helvetica, sans-serif\">".$lang["Returns & Exchanges"]."</FONT></B></FONT></TD>\n";
	$THIS_DISPLAY .= "</TR>\n";
	$THIS_DISPLAY .= "<TR> \n";
	$THIS_DISPLAY .= "<TD ALIGN=LEFT VALIGN=TOP>\n";
 
	// ------------------------------------------
	// Read Policy Statement into Memory
	// ------------------------------------------

	$filename = "$cgi_bin/cart_returns.txt";
	if (file_exists($filename)) {
		$file = fopen("$filename", "r") or DIE("Error: Could not open privacy policy ($filename)");
			$RETURNS_POLICY = fread($file,filesize($filename));
		fclose($file);
	}

	// ------------------------------------------
	// Format textarea data to display as HTML
	// ------------------------------------------

	$RETURNS_POLICY = chop($RETURNS_POLICY);
	$RETURNS_POLICY = str_replace("\n", "<BR>", $RETURNS_POLICY);

	// ------------------------------------------
	// Add Policy Text to $THIS_DISPLAY
	// ------------------------------------------

	$THIS_DISPLAY .= $RETURNS_POLICY;


	$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// DISPLAY "OTHER POLICIES" IF EXISTS #3
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	$filename = "$cgi_bin/other_policies.txt";
	if (file_exists($filename)) {

		// ------------------------------------------
		// Read Policy Statement into Memory
		// ------------------------------------------

		$file = fopen("$filename", "r") or DIE("Error: Could not open privacy policy ($filename)");
			$OTHER_POLICY = fread($file,filesize($filename));
		fclose($file);

		if (strlen($OTHER_POLICY) > 10) {	// Other Policy Statement should contain more than 10 chars

			$THIS_DISPLAY .= "<BR><TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5 class=smtext STYLE='border: inset BLACK 1px;'>\n";
			$THIS_DISPLAY .= "<TR> \n";
			$THIS_DISPLAY .= "<TD  BGCOLOR='$OPTIONS[DISPLAY_HEADERBG]'><FONT COLOR=$OPTIONS[DISPLAY_HEADERTXT]><B><FONT FACE=\"Verdana, Arial, Helvetica, sans-serif\">".$lang["Other Policies"]."</FONT></B></FONT></TD>\n";
			$THIS_DISPLAY .= "</TR>\n";
			$THIS_DISPLAY .= "<TR> \n";
			$THIS_DISPLAY .= "<TD ALIGN=LEFT VALIGN=TOP>\n";
 
			// ------------------------------------------
			// Format textarea data to display as HTML
			// ------------------------------------------

			$OTHER_POLICY = chop($OTHER_POLICY);
			$OTHER_POLICY = str_replace("\n", "<BR>", $OTHER_POLICY);

			// ------------------------------------------
			// Add Policy Text to $THIS_DISPLAY
			// ------------------------------------------
		
			$THIS_DISPLAY .= $OTHER_POLICY;

			$THIS_DISPLAY .= "</TD></TR></TABLE>\n\n";

		} // End if More than 10 Chars

	} // End if File Exists

} // End Other Policy Build

##########################################################################
### STEP 8: 
### BUILD OVERALL TABLE TO PLACE SEARCH COLUMN TO THE LEFT OR RIGHT OF
### SEARCH RESULT DISPLAY AS DEFINED IN DISPLAY OPTIONS
##########################################################################

$FINAL_DISPLAY = "<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH=612 ALIGN=CENTER>\n";

// ----------------------------------------------------------------------------------
// If a welcome header text is present, then display it now.
//
// DEVNOTE: I have had requests to place header images at the top of each search,
// like banner ads or relevant images based on current category.  I havn't done it
// but my recommendation is that code be inserted here looking for an include file
// based on category or a banner ad rotation that display's in this section.
// The value of the DISPLAY_WELCOME field could be a filename... Hmmm.
// ----------------------------------------------------------------------------------

if (strlen($OPTIONS[DISPLAY_WELCOME]) > 3) {	// Welcome text should be longer than 3 char

	$FINAL_DISPLAY .= "<TR><TD COLSPAN=2 ALIGN=LEFT VALIGN=MIDDLE>\n";
	$FINAL_DISPLAY .= "<HR WIDTH=100% STYLE='height: 1px; color: $OPTIONS[DISPLAY_HEADERBG];'>\n";

	$FINAL_DISPLAY .= "<FONT SIZE=3 FACE=VERDANA><B><I>".$lang["Welcome to"]."...</B></I></FONT><BR>";
	$FINAL_DISPLAY .= "<FONT SIZE=6 FACE=VERDANA><B>$OPTIONS[DISPLAY_WELCOME]</B></FONT>";

	$FINAL_DISPLAY .= "<HR WIDTH=100% STYLE='height: 1px; color: $OPTIONS[DISPLAY_HEADERBG];'>";
	$FINAL_DISPLAY .= "</TD></TR>\n\n";

}

$FINAL_DISPLAY .= "<TR>\n";

	if (eregi("L", $OPTIONS[DISPLAY_COLPLACEMENT] )) {

		$FINAL_DISPLAY .= "<TD WIDTH=150 ALIGN=CENTER VALIGN=TOP>\n\n$SEARCH_COLUMN\n\n</TD>\n<TD ALIGN=CENTER VALIGN=TOP>\n\n$THIS_DISPLAY\n\n</TD>\n";

	} else {

		$FINAL_DISPLAY .= "<TD ALIGN=CENTER VALIGN=TOP>\n\n$THIS_DISPLAY\n\n</TD>\n<TD WIDTH=150 ALIGN=CENTER VALIGN=TOP>\n\n$SEARCH_COLUMN\n\n</TD>\n";

	}

$FINAL_DISPLAY .= "</TR>\n\n";

// ----------------------------------------------------------------------------------
// If a business address has been supplied, display at the footer of each shopping
// cart page.  This can be removed if you wish, but studies have shown it instills
// trust among consumers that wish to buy from this web site
// ----------------------------------------------------------------------------------

if ($OPTIONS[BIZ_ADDRESS_1] != "" && $OPTIONS[BIZ_POSTALCODE] != "") {

	$FINAL_DISPLAY .= "<TR><TD COLSPAN=2 ALIGN=CENTER VALIGN=MIDDLE CLASS=smtext>\n";
	$FINAL_DISPLAY .= "<HR WIDTH=100% STYLE='height: 1px; color: $OPTIONS[DISPLAY_HEADERBG];'>\n".$lang["Mailing Address"].": $OPTIONS[BIZ_ADDRESS_1], ";

		if ($OPTIONS[BIZ_ADDRESS_2] != "") {
			$FINAL_DISPLAY .= "$OPTIONS[BIZ_ADDRESS_2], ";
		}

	$FINAL_DISPLAY .= "$OPTIONS[BIZ_CITY], $OPTIONS[BIZ_STATE], $OPTIONS[BIZ_POSTALCODE]\n<HR WIDTH=100% STYLE='height: 1px; color: $OPTIONS[DISPLAY_HEADERBG];'>";
	$FINAL_DISPLAY .= "</TD></TR>\n\n";

}

// ----------------------------------------------------------------------------------

$FINAL_DISPLAY .= "</TABLE>";


###########################################################################
### THE pgm-realtime_builder.php FILE COMPILES THE TEMPLATE DATA AND PAGE 
### CONTENT DATA TOGETHER AND PUTS IT OUT AS THE $template_header AND 
### $template_footer VARS RESPECTIVELY.  ANY MODIFICATION TO CHANGE THE
### WAY PAGES ARE OUTPUT TO THE SITE VISITOR SHOULD BE MADE WITHIN THE
### realtime_builder.php FILE
###########################################################################

$module_active = "yes";
include ("pgm-template_builder.php");

#######################################################

echo ("$template_header\n");
	$template_footer = eregi_replace("#CONTENT#", $FINAL_DISPLAY, $template_footer);
echo ("$template_footer\n\n");
echo ("\n\n<SCRIPT language=Javascript>\n     window.focus();\n</SCRIPT>\n\n");
exit;

?>