<?php
//This code is copyright (c) ViciSoft SL, all rights reserved.
//The contents of this file are protected under law as the intellectual property 
//of ViciSoft SL. Any use, reproduction, disclosure or copying of any kind 
//without the express and written permission of ViciSoft SL is forbidden.
//Author: Vince Reid, vincereid@gmail.com
if(!@$GLOBALS['incfunctionsdefined']){print 'No incfunctions.php file';exit;}
global $topsectionids,$catid,$isrootsection,$success,$sortoptions,$adminProdsPerPage,$nosearchrelevance,$nosearchdescription,$nosearchlongdescription,$magictoolboxproducts,$prodoptions,$Count,$countryTaxRate,$cs,$WSP,$OWSP,$TWSP;
if((@$_SERVER['CONTENT_LENGTH']!='' && $_SERVER['CONTENT_LENGTH'] > 10000) || strlen(getget('pg'))>8){
	header('HTTP/1.1 400 Bad Request');
	exit;
}
$nosearchrelevance=$showcategories=$gotcriteria=$isrootsection=FALSE;
$iNumOfPages=0;
$numcats=0;
$catid=0;
$nobox='';
$topsectionids='0';
if(! is_numeric(getget('pg')) || strlen(getget('pg'))>8)
	$CurPage=1;
else
	$CurPage=max(1, (int)getget('pg'));
if(getget('nobox')=='true' || getpost('nobox')=='true')
	$nobox='true';
$cs=$WSP=$OWSP='';
$TWSP='pPrice';
get_wholesaleprice_sql();
$scat=preg_replace('/[^,\d]/','',getrequest('scat'));
$scat=preg_replace('/,+/',',',$scat);
$scat=preg_replace('/^,|,$/','',$scat);
$sman=preg_replace('/[^,\d]/','',getrequest('sman'));
$sman=preg_replace('/,+/',',',$sman);
$sman=preg_replace('/^,|,$/','',$sman);
$stext=getrequest('stext');
$stype=getrequest('stype');
if($stype!='any' && $stype!='exact')$stype='';
$sprice=strip_tags(getrequest('sprice'));
if(!is_numeric($sprice))$sprice='';
$minprice=strip_tags(getrequest('sminprice'));
if(!is_numeric($minprice))$minprice='';
if(substr($scat,0,2)=='ms') $thecat=substr($scat,2); else $thecat=$scat;
$thecat=str_replace("'",'',$thecat);
$catarr=explode(',', $thecat);
$manarr=explode(',', $sman);
if(! @isset($Count)) $Count=0; else $Count=($Count+$adminProdsPerPage)-($Count % $adminProdsPerPage);
$noautocorrect='autocapitalize="off" autocomplete="off" spellcheck="false" autocorrect="off"';
if(strtolower($GLOBALS['adminencoding'])=='iso-8859-1') $raquo='»'; else $raquo='&raquo;';
if(@$magictoolboxproducts!=''){
	print '<script src="' . ($magictoolboxproducts=='MagicTouch' ? 'http://www.magictoolbox.com/mt/' . $magictouchid . '/magictouch.js' : strtolower($magictoolboxproducts) . '/' . strtolower($magictoolboxproducts) . '.js') . '"></script>' . @$magictooloptionsjsproducts;
	$magictoolboxproducts=str_replace('MagicZoomPlus','MagicZoom',$magictoolboxproducts);
	$magictool=$magictoolboxproducts;
}
function writemenulevel($id,$itlevel){
	global $allcatsa,$numcats,$thecat,$catarr,$raquo;
	if($itlevel<10){
		for($wmlindex=0; $wmlindex < $numcats; $wmlindex++){
			if($allcatsa[$wmlindex]['topSection']==$id){
				print "<option value='" . $allcatsa[$wmlindex]['sectionID'] . "'";
				if($catarr[0]==$allcatsa[$wmlindex]['sectionID']) print ' selected="selected">'; else print '>';
				for($index=0; $index < $itlevel-1; $index++)
					print $raquo . ' ';
				print $allcatsa[$wmlindex][getlangid('sectionName',256)] . "</option>\n";
				if($allcatsa[$wmlindex]['rootSection']==0) writemenulevel($allcatsa[$wmlindex]['sectionID'],$itlevel+1);
			}
		}
	}
}
function sortlinesearch($soid, $sotext){
	global $sortoptions;
	if(($sortoptions & pow(2,($soid-1)))!=0) print '<option value="'.$soid.'"'.($GLOBALS['dosortby']==$soid?' selected="selected"':'').'>'.$sotext.'</option>';
}
$pblink='<a class="ectlink" href="search' . $GLOBALS['extension'] . '?nobox=' . $nobox . '&amp;scat=' . urlencode($scat) . ($stext!=''?'&amp;stext='.urlencode($stext):'') . ($stype!=''?'&amp;stype='.$stype:'') . ($sprice!=''?'&amp;sprice='.urlencode($sprice):'') . ($minprice!=''?"&amp;sminprice=".$minprice:'') . ($sman!=''?'&amp;sman='.$sman:'');
$nofirstpg=FALSE;
function segetlike($fie,$t,$tjn){
	global $sNOTSQL;
	if(substr($t, 0, 1)=='-'&&@$GLOBALS['usenotsearch']){ // pSKU excluded to work around NULL problems
		if($fie!='pSKU' && $fie!='pSearchParams') $sNOTSQL.=$fie." LIKE '%".substr($t, 1)."%' OR ";
	}else
		return $fie . " LIKE '%".$t."%' ".$tjn;
}
$GLOBALS['alreadygotadmin']=getadminsettings();
if(@$orprodsperpage!='') $adminProdsPerPage=$orprodsperpage;
checkCurrencyRates($GLOBALS['currConvUser'],$GLOBALS['currConvPw'],$GLOBALS['currLastUpdate'],$GLOBALS['currRate1'],$GLOBALS['currSymbol1'],$GLOBALS['currRate2'],$GLOBALS['currSymbol2'],$GLOBALS['currRate3'],$GLOBALS['currSymbol3']);
if(@$_SESSION['clientLoginLevel']!='') $minloglevel=$_SESSION['clientLoginLevel']; else $minloglevel=0;
if(@$nosearchdescription && @$nosearchlongdescription) $nosearchrelevance=TRUE;
$scrid=''; $numscrid=0;
$relsql[0]=$relsql[1]='';
if(getpost('posted')=='1' || getget('pg')!=''){
	if(is_numeric(getrequest('sortby'))) $_SESSION['sortby']=(int)getrequest('sortby');
	if(@$_SESSION['sortby']!='') $GLOBALS['dosortby']=(int)($_SESSION['sortby']);
	if(@$orsortby!='') $GLOBALS['dosortby']=$orsortby;
	if($GLOBALS['dosortby']==5) $nosearchrelevance=TRUE;
	if(@$GLOBALS['nosearchbyfilters']!=TRUE){
		foreach(@$_GET as $key => $val){
			if(substr($key,0,4)=='sfil'){
				if(is_numeric(substr($key,4)) && is_numeric($val)){
					$scrid.=$val.',';
					$pblink.='&amp;'.$key.'='.$val;
				}
			}
		}
		if($scrid!=''){
			$scrid=trim($scrid,',');
			$scridarr=explode(',',$scrid);
			$numscrid=count($scridarr);
			$gotcriteria=TRUE;
		}
	}
	$pblink.='&amp;pg=';
	if($thecat!=''){
		$sSQL='SELECT DISTINCT '.(! @$nosearchrelevance?'0 AS relevanceorder,':'').'products.pId,'.getlangid('pName',1).','.$WSP.'pPrice,pOrder,pDateAdded,pManufacturer,pSKU,pPopularity,pNumSales FROM ';
		if($numscrid>1) $sSQL.=str_repeat('(',$numscrid-1);
		$sSQL.='(multisections RIGHT JOIN products ON products.pId=multisections.pId INNER JOIN sections on products.pSection=sections.sectionID)' . ($scrid!=''?' INNER JOIN multisearchcriteria ON multisearchcriteria.mSCpID=products.pID':'');
		for($scrindex=1; $scrindex<=$numscrid-1; $scrindex++){
			$sSQL.=') INNER JOIN multisearchcriteria msc'.$scrindex.' ON products.pID=msc'.$scrindex.'.mSCpID';
		}
		$sSQL.=' WHERE sectionDisabled<='.$minloglevel.' AND pDisplay<>0 ' . (@$GLOBALS['ectsiteid']!='' ? 'AND pSiteID='.$GLOBALS['ectsiteid'].' ' : '') . (@$GLOBALS['nosearcharticles'] ? 'AND pSchemaType=0 ' : '');
		$gotcriteria=TRUE;
		$sectionids=getsectionids($thecat, FALSE);
		if($sectionids!='') $sSQL.="AND (products.pSection IN (" . $sectionids . ") OR multisections.pSection IN (" . $sectionids . ")) ";
	}else{
		$sSQL='SELECT DISTINCT '.(! @$nosearchrelevance?'0 AS relevanceorder,':'').'products.pId,'.getlangid('pName',1).','.$WSP.'pPrice,pOrder,pDateAdded,pManufacturer,pSKU,pPopularity,pNumSales FROM ';
		if($numscrid>1) $sSQL.=str_repeat('(',$numscrid-1);
		$sSQL.='(products INNER JOIN sections on products.pSection=sections.sectionID)' . ($scrid!=''?' INNER JOIN multisearchcriteria ON multisearchcriteria.mSCpID=products.pID':'');
		for($scrindex=1; $scrindex<=$numscrid-1; $scrindex++){
			$sSQL.=') INNER JOIN multisearchcriteria msc'.$scrindex.' ON products.pID=msc'.$scrindex.'.mSCpID';
		}
		$sSQL.=' WHERE sectionDisabled<='.$minloglevel.' AND pDisplay<>0 ' . (@$GLOBALS['ectsiteid']!='' ? 'AND pSiteID='.$GLOBALS['ectsiteid'].' ' : '') . (@$GLOBALS['nosearcharticles'] ? 'AND pSchemaType=0 ' : '');
	}
	if(is_numeric($sprice)){
		$gotcriteria=TRUE;
		$sSQL.="AND ".$TWSP."<='" . escape_string($sprice) . "' ";
	}
	if(is_numeric($minprice)){
		$gotcriteria=TRUE;
		$sSQL.="AND ".$TWSP.">='" . escape_string($minprice) . "' ";
	}
	if($sman!=''){
		$gotcriteria=TRUE;
		$sSQL.="AND pManufacturer IN (" . escape_string($sman) . ") ";
	}
	if($scrid!=''){
		$sSQL.=' AND (multisearchcriteria.mSCscID=' . $scridarr[0];
		for($scrindex=1; $scrindex<=$numscrid-1; $scrindex++){
			$sSQL.=' AND msc'.$scrindex.'.mSCscID='.$scridarr[$scrindex];
		}
		$sSQL.=')';
	}
	if(trim($stext)!=''){
		$gotcriteria=TRUE;
		$Xstext=escape_string(substr($stext, 0, 1024));
		$aText=explode(' ',$Xstext);
		$aFields[0]='products.pId';
		$aFields[1]=getlangid('pName',1);
		$aFields[2]=getlangid('pDescription',2);
		$aFields[3]=getlangid('pLongDescription',4);
		$aFields[4]='pSKU';
		$aFields[5]='pSearchParams';
		//if(! @$nosearchrelevance) $sSQL.="''||SPBLOCKMARKER||''";
		$sSQL.="''||SPBLOCKMARKER||''";
		for($relindex=0;$relindex<=(@$nosearchrelevance?0:1);$relindex++){
			if($stype=='exact'){
				$relsql[$relindex]='AND ';
				if(substr($Xstext, 0, 1)=='-'&&@$GLOBALS['usenotsearch']){ $relsql[$relindex].='NOT '; $Xstext=substr($Xstext, 1); $isnot=TRUE; }else $isnot=FALSE;
				if($relindex==0 || @$nosearchrelevance)
					$relsql[$relindex].="(" . (@$GLOBALS['nosearchprodid']?'':"products.pId LIKE '%".$Xstext."%' OR ") . getlangid('pName',1)." LIKE '%".$Xstext."%'".(@$nosearchparams?'':" OR pSearchParams LIKE '%".$Xstext."%'").($isnot||@$nosearchsku? '' : " OR pSKU LIKE '%".$Xstext."%'") . (!@$nosearchrelevance?') ':'');
				if($relindex==1 || @$nosearchrelevance)
					$relsql[$relindex].=(@$nosearchdescription && @$nosearchlongdescription ? '' : ((@$nosearchrelevance?' OR ':'(').(@$nosearchdescription?'':getlangid('pDescription',2)." LIKE '%".$Xstext."%'").(@$nosearchlongdescription?'':(@$nosearchdescription?'':' OR ').getlangid('pLongDescription',2)." LIKE '%".$Xstext."%'"))) . ') ';
			}elseif(count($aText) < 24){
				$sNOTSQL=''; $sYESSQL='';
				if($stype=='any'){
					for($index=(@$GLOBALS['nosearchprodid']?1:0);$index<=5;$index++){
						$tmpSQL='';
						$arrelms=count($aText);
						foreach($aText as $theopt){
							if(is_array($theopt))$theopt=$theopt[0];
							if(! ((@$nosearchdescription==TRUE && $index==2) || (@$nosearchlongdescription==TRUE && $index==3) || (@$nosearchsku==TRUE && $index==4) || (@$nosearchparams==TRUE && $index==5)))
								if((($index==0 || $index==1 || $index==4 || $index==5) && $relindex==0) || (($index==2 || $index==3) && $relindex==1) || @$nosearchrelevance) $tmpSQL.=segetlike($aFields[$index], $theopt, 'OR ');
						}
						if($tmpSQL!='') $sYESSQL.= '(' . substr($tmpSQL, 0, strlen($tmpSQL)-3) . ') ';
						if($tmpSQL!='') $sYESSQL.='OR ';
					}
					$sYESSQL=substr($sYESSQL, 0, -3);
				}else{
					foreach($aText as $theopt){
						$tmpSQL='';
						$arrelms=count($aText);
						for($index=(@$GLOBALS['nosearchprodid']?1:0);$index<=5;$index++){
							if(is_array($theopt))$theopt=$theopt[0];
							if(! ((@$nosearchdescription==TRUE && $index==2) || (@$nosearchlongdescription==TRUE && $index==3) || (@$nosearchsku==TRUE && $index==4) || (@$nosearchparams==TRUE && $index==5)))
								if((($index==0 || $index==1 || $index==4 || $index==5) && $relindex==0) || (($index==2 || $index==3) && $relindex==1) || @$nosearchrelevance) $tmpSQL.=segetlike($aFields[$index], $theopt, 'OR ');
						}
						if($tmpSQL!='') $sYESSQL.= '(' . substr($tmpSQL, 0, strlen($tmpSQL)-3) . ') ';
						if($tmpSQL!='') $sYESSQL.='AND ';
					}
					$sYESSQL=substr($sYESSQL, 0, -4);
				}
				$relsql[$relindex]='';
				if($sYESSQL!='') $relsql[$relindex].='AND (' . $sYESSQL . ') ';
				if($sNOTSQL!='') $relsql[$relindex].='AND NOT (' . substr($sNOTSQL, 0, strlen($sNOTSQL)-4) . ')';
			}
		}
		//if(@$nosearchrelevance) $sSQL.=$relsql[0];
	}else
		$nosearchrelevance=TRUE;
	if(! $gotcriteria) $nosearchrelevance=TRUE;
	if(@$GLOBALS['dosortby']==2 || @$GLOBALS['dosortby']==12)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pId'.(@$GLOBALS['dosortby']==12?' DESC':'');
	elseif(@$GLOBALS['dosortby']==14||@$GLOBALS['dosortby']==15)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pSKU'.(@$GLOBALS['dosortby']==15?' DESC,pId':',pId');
	elseif(@$GLOBALS['dosortby']==3||@$GLOBALS['dosortby']==4)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pPrice'.(@$GLOBALS['dosortby']==4?' DESC,pId':',pId');
	elseif(@$GLOBALS['dosortby']==5)
		$sSortBy=(! @$nosearchrelevance?'1,':'');
	elseif(@$GLOBALS['dosortby']==6||@$GLOBALS['dosortby']==7)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pOrder'.(@$GLOBALS['dosortby']==7?' DESC,pId':',pId');
	elseif(@$GLOBALS['dosortby']==8||@$GLOBALS['dosortby']==9)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pDateAdded'.(@$GLOBALS['dosortby']==9?' DESC,pId':',pId');
	elseif(@$GLOBALS['dosortby']==10)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pManufacturer';
	elseif(@$GLOBALS['dosortby']==16)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pNumRatings DESC,pId';
	elseif(@$GLOBALS['dosortby']==17)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'IF(pNumRatings=0,0,pTotRating/pNumRatings) DESC,pNumRatings DESC,pId';
	elseif(@$GLOBALS['dosortby']==18)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pNumSales DESC,pId';
	elseif(@$GLOBALS['dosortby']==19)
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').'pPopularity DESC,pId';
	else
		$sSortBy=' ORDER BY '.(! @$nosearchrelevance?'1,':'').getlangid('pName',1);
	if($GLOBALS['useStockManagement'] && @$GLOBALS['noshowoutofstock']) $sSQL.=' AND pInStock>pMinQuant';
	$origSQL=$sSQL;
	$relevantmatches='';
	$userelevantmatches=TRUE;
	$numrelevantmatches=0;
	if(!$nosearchrelevance){
		$allprods=ect_query(str_replace("''||SPBLOCKMARKER||''",$relsql[0],$sSQL)) or ect_error();
		while($rs=ect_fetch_assoc($allprods)){
			$relevantmatches.="'".escape_string($rs['pId'])."',";
			$numrelevantmatches++;
			if($numrelevantmatches>100){ $userelevantmatches=FALSE; break; }
		}
		ect_free_result($allprods);
	}
	if($relevantmatches!='') $relevantmatches=substr($relevantmatches,0,-1); else $userelevantmatches=FALSE;
	if($gotcriteria){
		if($numrelevantmatches>=100||$nosearchrelevance)
			$sSQL=str_replace("''||SPBLOCKMARKER||''",$relsql[0],$sSQL);
		else
			$sSQL=($relevantmatches!=''?str_replace("''||SPBLOCKMARKER||''",$relsql[0],$sSQL) . ' UNION ALL ':'') . str_replace("''||SPBLOCKMARKER||''",$relsql[1].($userelevantmatches?" AND NOT products.pId IN (".$relevantmatches.')':''),str_replace('0 AS relevanceorder,','1 AS relevanceorder,',$sSQL));
	}
	$sSQL=preg_replace('/SELECT/','SELECT SQL_CALC_FOUND_ROWS',$sSQL,1) . $sSortBy . ' LIMIT ' . ($adminProdsPerPage*($CurPage-1)) . ', ' . $adminProdsPerPage;
	$allprods=ect_query($sSQL) or ect_error();
	if(ect_num_rows($allprods)==0){
		$relsql0=preg_replace('/LIKE \'%([^\']{3,}?)s%\'/i','LIKE \'%\\1%\'', $relsql[0]); // Try replacing plurals with singular
		if($relsql0!=$relsql[0]){
			$relsql[0]=$relsql0;
			$relsql[1]=preg_replace('/LIKE \'%([^\']{3,}?)s%\'/i','LIKE \'%\\1%\'', $relsql[1]);
			$sSQL=$origSQL;
			$relevantmatches='';
			$userelevantmatches=TRUE;
			$numrelevantmatches=0;
			if(!$nosearchrelevance){
				$allprods=ect_query(str_replace("''||SPBLOCKMARKER||''",$relsql[0],$sSQL)) or ect_error();
				while($rs=ect_fetch_assoc($allprods)){
					$relevantmatches.="'".escape_string($rs['pId'])."',";
					$numrelevantmatches++;
					if($numrelevantmatches>100){ $userelevantmatches=FALSE; break; }
				}
				ect_free_result($allprods);
			}
			if($relevantmatches!='') $relevantmatches=substr($relevantmatches,0,-1); else $userelevantmatches=FALSE;
			if($gotcriteria){
				if($numrelevantmatches>=100||$nosearchrelevance)
					$sSQL=str_replace("''||SPBLOCKMARKER||''",$relsql[0],$sSQL);
				else
					$sSQL=($relevantmatches!=''?str_replace("''||SPBLOCKMARKER||''",$relsql[0],$sSQL) . ' UNION ALL ':'') . str_replace("''||SPBLOCKMARKER||''",$relsql[1].($userelevantmatches?" AND NOT products.pId IN (".$relevantmatches.')':''),str_replace('0 AS relevanceorder,','1 AS relevanceorder,',$sSQL));
			}
			$sSQL=preg_replace('/SELECT/','SELECT SQL_CALC_FOUND_ROWS',$sSQL,1) . $sSortBy . ' LIMIT ' . ($adminProdsPerPage*($CurPage-1)) . ', ' . $adminProdsPerPage;
			$allprods=ect_query($sSQL) or ect_error();
		}
	}
	if(ect_num_rows($allprods)==0)
		$success=FALSE;
	else{
		$success=TRUE;
		$prodlist='';
		$addcomma='';
		while($rs=ect_fetch_assoc($allprods)){
			//print "RO: " . $rs['relevanceorder'] . " : " . $rs['pId'] . "<br>";
			$prodlist.=$addcomma . "'" . $rs['pId'] . "'";
			$addcomma=',';
		}
		ect_free_result($allprods);
		$allprods=ect_query('SELECT FOUND_ROWS() AS bar') or ect_error();
		$rs=ect_fetch_assoc($allprods);
		$iNumOfPages=ceil($rs['bar']/$adminProdsPerPage);
		ect_free_result($allprods);
		$GLOBALS['wantmanufacturer']=(strpos(@$GLOBALS['productpagelayout'].@$GLOBALS['quickbuylayout'],'manufacturer')!==FALSE || (@$useproductbodyformat==3 && strpos(@$GLOBALS['cpdcolumns'], 'manufacturer')!==FALSE) || (! @$GLOBALS['usecsslayout'] && @$GLOBALS['xxManLab']!=''));
		$sSQL='SELECT '.(! @$nosearchrelevance?'0 AS relevanceorder,':'').'pId,pSKU,'.getlangid('pName',1).','.$WSP.'pPrice,pListPrice,pSection,pSell,pStockByOpts,pStaticPage,'.getaltlang('pStaticURL',8388608).',pInStock,pExemptions,pTax,pTotRating,pNumRatings,pBackOrder,pDateAdded,pMinQuant,pSchemaType,pCustomCSS,';
		$templayoutarray=explode(',',strtolower(@$GLOBALS['productpagelayout'].','.@$GLOBALS['quickbuylayout']));
		foreach($templayoutarray as $layoutoption){
			if(substr(strtolower(trim($layoutoption)),0,6)=='custom'){
				if(strpos($sSQL,str_replace('custom','pCustom',$layoutoption).',')===FALSE)$sSQL.=str_replace('custom','pCustom',$layoutoption).',';
			}elseif(substr(trim($layoutoption),0,1)=='<'&&preg_match('/%custom(\d+)%/i',$layoutoption,$matchnum)){
				if(strpos($sSQL,'pCustom'.$matchnum[1].',')===FALSE)$sSQL.='pCustom'.$matchnum[1].',';
			}
		}
		$sSQL.=($GLOBALS['wantmanufacturer']?getlangid('scName',131072).',':'').(@$shortdescriptionlimit===0?"'' AS ":'').getlangid('pDescription',2).','.getlangid('pLongDescription',4).' FROM products '.($GLOBALS['wantmanufacturer']?'LEFT OUTER JOIN searchcriteria on products.pManufacturer=searchcriteria.scID ':'').'WHERE pId IN (' . $prodlist . ')' . $sSortBy;
		//print $sSQL . "<br><br>";
		$allprods=ect_query($sSQL) or ect_error();
	}
}
if($nobox==''){ ?>
	  <form method="get" id="stextform" action="search<?php print $GLOBALS['extension']?>">
		  <input type="hidden" name="pg" value="1" />
          <div class="searchform">
			<div class="searchheader"><?php print $GLOBALS['xxSrchPr']?></div>
			<div class="searchcntnr searchfor_cntnr">
				<div class="searchtext searchfortext"><label class="ectlabel" for="stext"><?php print $GLOBALS['xxSrchFr']?></label></div>
				<div class="searchcontrol searchfor">
					<div style="position:relative">
						<input type="search" name="stext" id="stext" class="ecttextinput" size="20" maxlength="1024" value="<?php print htmlspecials($stext)?>" <?php if(!@$GLOBALS['noautosearch']) print 'onkeydown="return ectAutoSrchKeydown(this,event,\'php\')" onblur="ectAutoHideCombo(this)" ';?><?php print $noautocorrect ?>>
					</div>
<?php	if(@$GLOBALS['noautosearch']!=TRUE){ ?>
					<div class="autosearch" style="position:absolute;display:none;" id="selectstext"></div>
<?php	} ?>
				</div>
			</div>
			<div class="searchcntnr searchprice_cntnr">
				<div class="searchtext searchpricetext"><label class="ectlabel" for="sprice"><?php print $GLOBALS['xxSrchMx']?></label></div>
				<div class="searchcontrol searchprice"><input type="number" name="sprice" id="sprice" class="ecttextinput" size="10" maxlength="64" value="<?php print htmlspecials($sprice)?>" <?php print $noautocorrect ?> /></div>
			</div>
			<div class="searchcntnr searchtype_cntnr">
				<div class="searchtext searchtypetext"><label class="ectlabel" for="stype"><?php print $GLOBALS['xxSrchTp']?></label></div>
				<div class="searchcontrol searchtype"><select class="ectselectinput searchtype" name="stype" id="stype" size="1">
					<option value=""><?php print $GLOBALS['xxSrchAl']?></option>
					<option value="any" <?php if($stype=="any") print 'selected="selected"'?>><?php print $GLOBALS['xxSrchAn']?></option>
					<option value="exact" <?php if($stype=="exact") print 'selected="selected"'?>><?php print $GLOBALS['xxSrchEx']?></option>
					</select></div>
			</div>
<?php	if(@$GLOBALS['nocategorysearch']!=TRUE){ ?>
			<div class="searchcntnr searchcategory_cntnr">
				<div class="searchtext searchcategorytext"><label class="ectlabel" for="scat"><?php print $GLOBALS['xxSrchCt']?></label></div>
				<div class="searchcontrol searchcategory"><select class="ectselectinput searchcategory" name="scat" id="scat" size="1">
					<option value=""><?php print $GLOBALS['xxSrchAC']?></option>
<?php		$lasttsid=-1;
			$sSQL='SELECT sectionID,'.getlangid('sectionName',256).',topSection,rootSection FROM sections WHERE sectionDisabled<=' . $minloglevel . ' ';
			if(@$onlysubcats==TRUE) $sSQL.='AND rootSection=1 ORDER BY '.getlangid('sectionName',256); else $sSQL.='ORDER BY '.(@$GLOBALS['sortcategoriesalphabetically']?getlangid('sectionName',256):'sectionOrder');
			$allcats=ect_query($sSQL) or ect_error();
			while($row=ect_fetch_assoc($allcats)){
				$allcatsa[$numcats++]=$row;
			}
			ect_free_result($allcats);
			if($numcats > 0) writemenulevel($GLOBALS['catalogroot'],1);
?>					</select></div>
			</div>
<?php	}
		if(($GLOBALS['prodfilter'] & 8)==8 && $sortoptions!=0 && ! @$GLOBALS['nosortsearch']){ ?>
			<div class="searchcntnr searchsort_cntnr">
				<div class="searchtext searchsorttext"><label class="ectlabel" for="sortby"><?php print $GLOBALS['xxSortBy']?></label></div>
				<div class="searchcontrol searchsort"><select name="sortby" id="sortby" class="ectselectinput searchsort" size="1">
				<option value="0"><?php print $GLOBALS['xxPlsSel']?></option>
<?php			sortlinesearch(1, @$GLOBALS['sortoption1']!=''?$GLOBALS['sortoption1']:'Sort Alphabetically');
				sortlinesearch(11, @$GLOBALS['sortoption11']!=''?$GLOBALS['sortoption11']:'Alphabetically (Desc.)');
				sortlinesearch(2, @$GLOBALS['sortoption2']!=''?$GLOBALS['sortoption2']:'Sort by Product ID');
				sortlinesearch(12, @$GLOBALS['sortoption12']!=''?$GLOBALS['sortoption12']:'Product ID (Desc.)');
				sortlinesearch(14, @$GLOBALS['sortoption14']!=''?$GLOBALS['sortoption14']:'Sort By SKU');
				sortlinesearch(15, @$GLOBALS['sortoption15']!=''?$GLOBALS['sortoption15']:'Sort By SKU (Desc.)');
				sortlinesearch(3, @$GLOBALS['sortoption3']!=''?$GLOBALS['sortoption3']:'Sort Price (Asc.)');
				sortlinesearch(4, @$GLOBALS['sortoption4']!=''?$GLOBALS['sortoption4']:'Sort Price (Desc.)');
				sortlinesearch(5, @$GLOBALS['sortoption5']!=''?$GLOBALS['sortoption5']:'Database Order');
				sortlinesearch(6, @$GLOBALS['sortoption6']!=''?$GLOBALS['sortoption6']:'Product Order');
				sortlinesearch(7, @$GLOBALS['sortoption7']!=''?$GLOBALS['sortoption7']:'Product Order (Desc.)');
				sortlinesearch(8, @$GLOBALS['sortoption8']!=''?$GLOBALS['sortoption8']:'Date Added (Asc.)');
				sortlinesearch(9, @$GLOBALS['sortoption9']!=''?$GLOBALS['sortoption9']:'Date Added (Desc.)');
				sortlinesearch(10, @$GLOBALS['sortoption10']!=''?$GLOBALS['sortoption10']:'Sort by Manufacturer');
				sortlinesearch(16, @$GLOBALS['sortoption16']!=''?$GLOBALS['sortoption16']:'Number of Ratings');
				sortlinesearch(17, @$GLOBALS['sortoption17']!=''?$GLOBALS['sortoption17']:'Average Rating');
				sortlinesearch(18, @$GLOBALS['sortoption18']!=''?$GLOBALS['sortoption18']:'Sales Rank');
				sortlinesearch(19, @$GLOBALS['sortoption19']!=''?$GLOBALS['sortoption19']:'Popularity');
?>			  </select></div>
			</div>
<?php	}
		if(@$GLOBALS['nosearchbyfilters']!=TRUE){
			if(@$GLOBALS['searchfiltergroups']!=''){ @$GLOBALS['searchfiltergroups']='WHERE scGroup IN ('.$GLOBALS['searchfiltergroups'].')'; $GLOBALS['onlysearchfiltermanufacturer']=TRUE;} else $GLOBALS['searchfiltergroups']='WHERE scGroup=0 ';
			$sSQL='SELECT '.(@$GLOBALS['nocountsearchfilter']?'':'COUNT(*) as tcount,').'scID,'.getlangid('scName',131072).',scGroup,scgTitle FROM (searchcriteria INNER JOIN searchcriteriagroup ON searchcriteria.scGroup=searchcriteriagroup.scgID) '.(@$GLOBALS['nocountsearchfilter']?(@$GLOBALS['onlysearchfiltermanufacturer']?$GLOBALS['searchfiltergroups']:''):'INNER JOIN multisearchcriteria ON multisearchcriteria.mSCscID=searchcriteria.scID ' . (@$GLOBALS['onlysearchfiltermanufacturer']?$GLOBALS['searchfiltergroups']:'') . 'GROUP BY scID,'.getlangid('scName',131072).',scGroup,scOrder,scgOrder,scgTitle ') . 'ORDER BY scGroup,scOrder,'.getlangid('scName',131072);
			$result2=ect_query($sSQL) or ect_error();
			if(ect_num_rows($result2)>0){
?>			<div class="searchcntnr searchfilters_cntnr">
				<div class="searchtext searchfilterstext"><?php print $GLOBALS['xxSeaFil']?></div>
				<div class="searchcontrol searchfilters"><?php
				$currgroup=-1;
				while($rs2=ect_fetch_assoc($result2)){
					if($currgroup!=$rs2['scGroup']){
						if($currgroup!=-1) print "</select></div>\r\n";
						print '<div class="searchfiltergroup'.$rs2['scGroup'].' searchfiltergroup"><select class="ectselectinput searchfiltergroup" name="sfil'.$rs2['scGroup'].'" size="1"><option style="font-weight:bold" value="">' . sprintf($GLOBALS['xxSFiAll'],htmlspecials($rs2['scgTitle'])) . '</option>';
						$currgroup=$rs2['scGroup'];
					}
					print '<option value="' . $rs2['scID'] . '"';
					if((int)getget('sfil'.$rs2['scGroup'])==$rs2['scID']) print ' selected="selected"';
					print '>' . $rs2[getlangid('scName',131072)] . (@$GLOBALS['nocountsearchfilter']?'':' (' . $rs2['tcount'] . ')') . "</option>\r\n";
				}
				if($currgroup!=-1) print '</select></div>'; ?></div>
			</div>
<?php		}
			ect_free_result($result2);
		} ?>
			<div class="searchcntnr ectsearchsubmit"><div class="searchtext"></div><div class="searchcontrol"><?php print imageorsubmit(@$imgsearch,$GLOBALS['xxSearch'],'search')?></div></div>
		  </div>
		</form>
<?php
}
if(getpost('posted')=='1' || getget('pg')!=''){
	print '<div class="searchresults">';
	if(! $success)print '<div class="nosearchresults">' . $GLOBALS['xxSrchNM'] . '</div>';
	if($success){
		if(@$GLOBALS['usesearchbodyformat']==3)
			include './vsadmin/inc/incproductbody3.php';
		elseif(@$GLOBALS['usesearchbodyformat']==2)
			include './vsadmin/inc/incproductbody2.php';
		else
			include './vsadmin/inc/incproductbody.php';
		if(! @$usecsslayout) print '</td></tr>';
		ect_free_result($allprods);
	}
	print '</div>';
}
?>
