<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
set_magic_quotes_runtime(0);
include("include/dbcommon.php");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
header("Pragma: no-cache");
header("Cache-Control: no-cache");

include("include/##@TABLE.strShortTableName s##_variables.php");

##if @BUILDER.bCreateLoginPage##
if(!@$_SESSION["UserID"])
{ 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	return;
}
##endif##



$response = array();

$suggestAllContent=true;
if(postvalue("start"))
	$suggestAllContent=false;

if (isset($_GET['searchFor']) && postvalue('searchFor') != '') {

	$searchFor = postvalue('searchFor');
	$searchField = GoodFieldName( postvalue('searchField') );
	
	##foreach Fields as @f filter (@f.bSearch || @f.bAdvancedSearch)##
	##if IsCharField(@f) || IsNumberField(@f)##
	##if !IsTextField(@f)##
	
##if !@f.bSearch##	
	$searchByField =  (($searchField == '' || $searchField=="##@f.strName g##")&&(postvalue("searchType")!="ordinary"));
##else## 		
	$searchByField = ($searchField == '' || $searchField=="##@f.strName g##");
##endif##
	if($searchByField)
	{
	
		$field="##@f.strName s##";
		if(CheckFieldPermissions($field))
		{
		$whereCondition = ($suggestAllContent) ? " like '%".db_addslashes($searchFor)."%'" : " like '".db_addslashes($searchFor)."%'";
		$whereCondition = " ".GetFullFieldName($field).$whereCondition;
##if @TABLE.nSecOptions==ADVSECURITY_VIEW_OWN  && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
		$whereCondition = whereAdd($whereCondition,SecuritySQL("Search"));
##endif##
		$whereCondition = whereAdd($gsqlWhereExpr,$whereCondition);
##if @BUILDER.nDatabaseType==nDATABASE_MySQL##
		$strSQL = "SELECT DISTINCT ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1 LIMIT 10";
		$rs=db_query($strSQL,$conn);
##elseif @BUILDER.nDatabaseType==nDATABASE_MSSQLServer##
		$strSQL = "SELECT DISTINCT TOP 10 ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1";		
		$rs=db_query($strSQL,$conn);
##elseif @BUILDER.nDatabaseType==nDATABASE_Access##
		$strSQL = "SELECT DISTINCT TOP 10 ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1";		
		$rs=db_query_direct($strSQL,$conn,10);
##elseif @BUILDER.nDatabaseType==nDATABASE_Oracle##
		$whereCondition = whereAdd($whereCondition,"rownum<11");
		$strSQL = "SELECT DISTINCT ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1";
		$rs=db_query_direct($strSQL,$conn,10);
##elseif @BUILDER.nDatabaseType==nDATABASE_PostgreSQL##
		$strSQL = "SELECT distinct ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1 LIMIT 10";		
		$rs=db_query($strSQL,$conn);
##else##
		$strSQL = "SELECT distinct ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail;
		$rs=db_query($strSQL,$conn);	
##endif##		
			$i=0;
			while ($row = db_fetch_numarray($rs)) 
			{
				$i++;
				$pos = strpos($row[0],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[0],0,$pos);
				} else {
					$response[] = $row[0];
				}
				if ($i>10)
					break;
			}
		}
		}
	##endif##
	##endif##
	##endfor##
	db_close($conn);
}

sort($response);

for( $i=0;$i<10 && $i<count($response);$i++) 
{
	$value=$response[$i];
	if($suggestAllContent)
	{
		$str=substr($value,0,50);
		$pos=my_stripos($str,$searchFor,0);
		if($pos===false)
			echo $str;
		else
			echo substr($str,0,$pos)."<b>".substr($str,$pos,strlen($searchFor))."</b>".substr($str,$pos+strlen($searchFor));
		echo "\n";
	}
	else
		echo  "<b>".substr($value,0,strlen($searchFor))."</b>".substr($value,strlen($searchFor),50-strlen($searchFor))."\n";
}
?>