<?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##

$conn = db_connect();

$field = postvalue('searchField');
$value = postvalue('searchFor');
$lookupValue = postvalue('lookupValue');
$LookupSQL = "";
$response = array();
$output = "";

##if @BUILDER.bCreateLoginPage##
##if @TABLE.strDataSourceTable!=@BUILDER.strLoginTable##
	if(!@$_SESSION["UserID"]) { return;	}
	if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Edit") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search")) { return;	}
##else##
	if ( 1==1
	##foreach @BUILDER.Registration.arrUserFields as @u##
		&& $field!="##@u g##" 
	##endfor##
	) {
		if(!@$_SESSION["UserID"]) { return;	}
		if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Edit") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search")) { return;	}
	}
##endif##
##endif##

##foreach @TABLE.arrFieldObj as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
	if($field=="##@f.strName g##") 
	{
	
##if @f.nLookupType==LT_LOOKUPTABLE##
		$LookupSQL = "SELECT ";
##if @f.bUnique##
		$LookupSQL .= "DISTINCT ";
##endif##
		$LookupSQL .= "##@f.strLinkField ws##";
##if @f.bCustomDisplay##
		$LookupSQL .= ",##@f.strDisplayField s##";
##else##
		$LookupSQL .= ",##@f.strDisplayField ws##";
##endif##
		$LookupSQL .= " FROM ##@f.pLookupObj.strTable ts## ";
##if @f.strWhere##
		$LookupSQL.="where (".##@f.strWhere##.")  AND ";
##else##
		$LookupSQL .= " WHERE ";
##endif##
##if @f.bCustomDisplay##
		$LookupSQL .= "##@f.strDisplayField s## LIKE '".db_addslashes($value)."%'";
##else##
		$LookupSQL .= "##@f.strDisplayField ws## LIKE '".db_addslashes($value)."%'";
##endif##
##if @f.bUseCategory##
		$cvalue = make_db_value("##@f.strCategoryControlField s##",postvalue("category"));
		$LookupSQL.=" AND ##@f.strCategoryFilterField ws##=".$cvalue;
##endif##
##if @f.strOrderBy##
		$LookupSQL.= " ORDER BY ##@f.strOrderBy ws##";
	##if @f.pLookupObj.bDesc##
		$LookupSQL.=" DESC";	
	##endif##
##endif##
##endif##
	}
##endfor##

$rs=db_query($LookupSQL,$conn);

$found=false;
while ($data = db_fetch_numarray($rs)) 
{
	if(!$found && $data[0]==$lookupValue)
		$found=true;
	$response[] = $data[0];
	$response[] = $data[1];
}

##if @f.strWhere##
if(!$found )
{
// try to make query without WHERE expression
##foreach Fields as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
	if($field=="##@f.strName g##") 
	{
		$origfield=GetFieldByGoodFieldName($field);
		$lookupValue = make_db_value($origfield,$lookupValue);
##if @f.nLookupType==LT_LOOKUPTABLE##
		$LookupSQL = "SELECT count(*)";
		$LookupSQL .= " FROM ##@f.pLookupObj.strTable ts## ";
##if @f.strWhere##
		$LookupSQL.="where (".##@f.strWhere##.")  AND ##@f.strLinkField ws##=".$lookupValue;
##else##
		$LookupSQL .= " WHERE ##@f.strLinkField ws##=".$lookupValue;
##endif##
##if @f.bUseCategory##
		$cvalue = make_db_value("##@f.strCategoryControlField s##",postvalue("category"));
		$LookupSQL.=" AND ##@f.strCategoryFilterField ws##=".$cvalue;
##endif##

##endif##
	}
##endfor##

$rs1=db_query($LookupSQL,$conn);
$datacount=db_fetch_numarray($rs1);

if ( !$datacount[0] ) {
##foreach Fields as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD##
	if($field=="##@f.strName g##") 
	{
	
##if @f.nLookupType==LT_LOOKUPTABLE##
		$LookupSQL = "SELECT ";
##if @f.bUnique##
		$LookupSQL .= "DISTINCT ";
##endif##
		$LookupSQL .= "##@f.strLinkField ws##";
##if @f.bCustomDisplay##
		$LookupSQL .= ",##@f.strDisplayField s##";
##else##
		$LookupSQL .= ",##@f.strDisplayField ws##";
##endif##
		$LookupSQL .= " FROM ##@f.pLookupObj.strTable ts## ";
		$LookupSQL .= " WHERE ##@f.strLinkField ws##=".$lookupValue." AND ";
##if @f.bCustomDisplay##
		$LookupSQL .= "##@f.strDisplayField s## LIKE '".db_addslashes($value)."%'";
##else##
		$LookupSQL .= "##@f.strDisplayField ws## LIKE '".db_addslashes($value)."%'";
##endif##
##if @f.bUseCategory##
		$cvalue = make_db_value("##@f.strCategoryControlField s##",postvalue("category"));
		$LookupSQL.=" AND ##@f.strCategoryFilterField ws##=".$cvalue;
##endif##
##endif##
	}
##endfor##

	$rs2=db_query($LookupSQL,$conn);
	
	if ( $data = db_fetch_numarray($rs2) ) {	
		
		$response[]=$data[0];
		$response[]=$data[1];
	}
}
}
##endif##

if ($output = array_chunk($response,40)) {
	foreach( $output[0] as $value ) {
		echo $value."\n";
		//echo str_replace("\n","\\n",$value)."\n";
	}
}

?>