<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
set_magic_quotes_runtime(0);

include("oldinclude/dbcommon.php");
include("oldinclude/##@TABLE.strShortTableName s##_variables.php");
##if @BUILDER.arrLanguages.len>1##
include("oldinclude/languages.php");
##endif##

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

$conn=db_connect();	

$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 ( $searchField == '' || $searchField=="##@f.strName g##")
	{
		$field="##@f.strName s##";
		if(CheckFieldPermissions($field))
		{
		$whereCondition = ($suggestAllContent) ? " like '%".str_replace("'","''",$searchFor)."%'" : " like '".str_replace("'","''",$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($gsqlWhere,$whereCondition);
##if @BUILDER.nDatabaseType==DATABASE_MySQL##
		$strSQL = "SELECT DISTINCT ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1 LIMIT 10";
		$rs=db_query($strSQL,$conn);
##elseif @BUILDER.nDatabaseType==DATABASE_MSSQLServer##
		$strSQL = "SELECT DISTINCT TOP 10 ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1";		
		$rs=db_query($strSQL,$conn);
##elseif @BUILDER.nDatabaseType==DATABASE_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==DATABASE_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==DATABASE_PostgreSQL##
		$strSQL = "SELECT distinct ".GetFullFieldName($field)." ".$gsqlFrom." WHERE ".$whereCondition.$gsqlTail." ORDER BY 1 LIMIT 10";		
		$rs=db_query($strSQL,$conn);
##endif##		

			while ($row = db_fetch_numarray($rs)) {
				$pos = strpos($row[0],"\n");
				if ($pos!==FALSE) {
					$response[] = substr($row[0],0,$pos);
				} else {
					$response[] = $row[0];
				}
			}
		}
		}
	##endif##
	##endif##
	##endfor##
	db_close($conn);
}

sort($response);

if ($output = array_chunk(array_unique($response),10)) {
	foreach( $output[0] as $value ) {
		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";
	}
}
?>