<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
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##
//	check if logged in
if(!@$_SESSION["UserID"] || !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
##endif##

//connect database
$conn = db_connect();

include('oldlibs/Smarty.class.php');
$smarty = new Smarty();

//	Before Process event
if(function_exists("BeforeProcessSearch"))
	BeforeProcessSearch($conn);


$includes=
"<STYLE>
	.vis1	{ visibility:\"visible\" }
	.vis2	{ visibility:\"hidden\" }
</STYLE>
<script language=\"JavaScript\" src=\"oldinclude/calendar.js\"></script>
<script language=\"JavaScript\" src=\"oldinclude/jsfunctions.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\" src=\"oldinclude/jquery.js\"></script>";
if ($useAJAX) {
$includes.="<script language=\"JavaScript\" src=\"oldinclude/ajaxsuggest.js\"></script>\r\n";
}
$includes.="<script language=\"JavaScript\" type=\"text/javascript\">\r\n".
"var locale_dateformat = ".$locale_info["LOCALE_IDATE"].";\r\n".
"var locale_datedelimiter = \"".$locale_info["LOCALE_SDATE"]."\";\r\n".
"var bLoading=false;\r\n".
"var TEXT_PLEASE_SELECT='".addslashes(##message PLEASE_SELECT##)."';\r\n";
if ($useAJAX) {
$includes.="var SUGGEST_TABLE = \"##@TABLE.strShortTableName##_searchsuggest.php\";\r\n";
}
$includes.="var detect = navigator.userAgent.toLowerCase();

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}


function ShowHideControls()
{
##foreach Fields as @f filter @f.bAdvancedSearch && !IsBinaryField(@f) order @f.nAdvSearchPageOrder##
	document.getElementById('second_##@f.strName g##').style.display =  
		document.forms.editform.elements['asearchopt_##@f.strName g##'].value==\"Between\" ? '' : 'none'; 
##endfor##	
	return false;
}
function ResetControls()
{
	var i;
	e = document.forms[0].elements; 
	for (i=0;i<e.length;i++) 
	{
		if (e[i].name!='type' && e[i].className!='button' && e[i].type!='hidden')
		{
			if(e[i].type=='select-one')
				e[i].selectedIndex=0;
			else if(e[i].type=='select-multiple')
			{
				var j;
				for(j=0;j<e[i].options.length;j++)
					e[i].options[j].selected=false;
			}
			else if(e[i].type=='checkbox' || e[i].type=='radio')
				e[i].checked=false;
			else 
				e[i].value = ''; 
		}
		else if(e[i].name.substr(0,6)=='value_' && e[i].type=='hidden')
			e[i].value = ''; 
	}
	ShowHideControls();	
	return false;
}";

##if @TABLE.bSearchSuggest##
$includes.="
$(document).ready(function() {
##foreach Fields as @f filter @f.bAdvancedSearch && @f.strEditFormat==EDIT_FORMAT_TEXT_FIELD##
	document.forms.editform.value_##@f.strName g##.onkeyup=function(event) {searchSuggest(event,document.forms.editform.value_##@f.strName g##,'advanced')};
	document.forms.editform.value1_##@f.strName g##.onkeyup=function(event) {searchSuggest(event,document.forms.editform.value1_##@f.strName g##,'advanced1')};
	document.forms.editform.value_##@f.strName g##.onkeydown=function(event) {return listenEvent(event,document.forms.editform.value_##@f.strName g##,'advanced')};
	document.forms.editform.value1_##@f.strName g##.onkeydown=function(event) {return listenEvent(event,document.forms.editform.value1_##@f.strName g##,'advanced1')};
##endfor##
});
</script>
<div id=\"search_suggest\"></div>
";
##else##
$includes.="
function OnKeyDown(e)
{ if(!e) e = window.event; 
if (e.keyCode == 13){ e.cancel = true; document.forms[0].submit();} }

</script>";
##endif##

$smarty->assign("includes",$includes);
$smarty->assign("noAJAX",!$useAJAX);

$onload="onLoad=\"javascript: ShowHideControls();\"";
$smarty->assign("onload",$onload);

if(@$_SESSION[$strTableName."_asearchtype"]=="or")
{
	$smarty->assign("any_checked"," checked");
	$smarty->assign("all_checked","");
}
else
{
	$smarty->assign("any_checked","");
	$smarty->assign("all_checked"," checked");
}

$editformats=array();

##foreach Fields as @f filter @f.bAdvancedSearch && !IsBinaryField(@f) order @f.nAdvSearchPageOrder##
// ##@f.strName## 
$opt="";
$not=false;
if(@$_SESSION[$strTableName."_search"]==2)
{
	$opt=@$_SESSION[$strTableName."_asearchopt"]["##@f.strName s##"];
	$not=@$_SESSION[$strTableName."_asearchnot"]["##@f.strName s##"];
	$smarty->assign("value_##@f.strName g##",@$_SESSION[$strTableName."_asearchfor"]["##@f.strName s##"]);
	$smarty->assign("value1_##@f.strName g##",@$_SESSION[$strTableName."_asearchfor2"]["##@f.strName s##"]);
}	
if($not)
	$smarty->assign("not_##@f.strName g##"," checked");
//	write search options
$options="";
##if @f.strEditFormat==EDIT_FORMAT_DATE##
$options.="<OPTION VALUE=\"Equals\" ".(($opt=="Equals")?"selected":"").">".##message EQUALS##."</option>";
$options.="<OPTION VALUE=\"More than ...\" ".(($opt=="More than ...")?"selected":"").">".##message MORE_THAN##."</option>";
$options.="<OPTION VALUE=\"Less than ...\" ".(($opt=="Less than ...")?"selected":"").">".##message LESS_THAN##."</option>";
$options.="<OPTION VALUE=\"Equal or more than ...\" ".(($opt=="Equal or more than ...")?"selected":"").">".##message EQUAL_OR_MORE##."</option>";
$options.="<OPTION VALUE=\"Equal or less than ...\" ".(($opt=="Equal or less than ...")?"selected":"").">".##message EQUAL_OR_LESS##."</option>";
$options.="<OPTION VALUE=\"Between\" ".(($opt=="Between")?"selected":"").">".##message BETWEEN##."</option>";
$options.="<OPTION VALUE=\"Empty\" ".(($opt=="Empty")?"selected":"").">".##message EMPTY##."</option>";
##elseif @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.nSelectSize==1##
$options.="<OPTION VALUE=\"Equals\" ".(($opt=="Equals")?"selected":"").">".##message EQUALS##."</option>";
##elseif @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.nSelectSize>1##
$options.="<OPTION VALUE=\"Contains\" ".(($opt=="Contains")?"selected":"").">".##message CONTAINS##."</option>";
##elseif @f.strEditFormat==EDIT_FORMAT_TEXT_FIELD 
|| @f.strEditFormat==EDIT_FORMAT_TEXT_AREA
|| @f.strEditFormat==EDIT_FORMAT_PASSWORD
|| @f.strEditFormat==EDIT_FORMAT_HIDDEN
|| @f.strEditFormat==EDIT_FORMAT_READONLY##
$options.="<OPTION VALUE=\"Contains\" ".(($opt=="Contains")?"selected":"").">".##message CONTAINS##."</option>";
$options.="<OPTION VALUE=\"Equals\" ".(($opt=="Equals")?"selected":"").">".##message EQUALS##."</option>";
$options.="<OPTION VALUE=\"Starts with ...\" ".(($opt=="Starts with ...")?"selected":"").">".##message STARTS_WITH##."</option>";
$options.="<OPTION VALUE=\"More than ...\" ".(($opt=="More than ...")?"selected":"").">".##message MORE_THAN##."</option>";
$options.="<OPTION VALUE=\"Less than ...\" ".(($opt=="Less than ...")?"selected":"").">".##message LESS_THAN##."</option>";
$options.="<OPTION VALUE=\"Equal or more than ...\" ".(($opt=="Equal or more than ...")?"selected":"").">".##message EQUAL_OR_MORE##."</option>";
$options.="<OPTION VALUE=\"Equal or less than ...\" ".(($opt=="Equal or less than ...")?"selected":"").">".##message EQUAL_OR_LESS##."</option>";
$options.="<OPTION VALUE=\"Between\" ".(($opt=="Between")?"selected":"").">".##message BETWEEN##."</option>";
$options.="<OPTION VALUE=\"Empty\" ".(($opt=="Empty")?"selected":"").">".##message EMPTY##."</option>";
##else##
$options.="<OPTION VALUE=\"Equals\" ".(($opt=="Equals")?"selected":"").">".##message EQUALS##."</option>";
##endif##
$searchtype = "<SELECT ID=\"SearchOption\" NAME=\"asearchopt_##@f.strName g##\" SIZE=1 onChange=\"return ShowHideControls();\">";
$searchtype .= $options;
$searchtype .= "</SELECT>";
$smarty->assign("searchtype_##@f.strName g##",$searchtype);
//	edit format
##if @f.strEditFormat==EDIT_FORMAT_TEXT_AREA
|| @f.strEditFormat==EDIT_FORMAT_PASSWORD
|| @f.strEditFormat==EDIT_FORMAT_HIDDEN
|| @f.strEditFormat==EDIT_FORMAT_READONLY
|| @f.strEditFormat==EDIT_FORMAT_FILE
##
$editformats["##@f.strName s##"]=EDIT_FORMAT_TEXT_FIELD;
##else##
$editformats["##@f.strName s##"]="##@f.strEditFormat##";
##endif##
##endfor##

$linkdata="";

$linkdata .= "<script type=\"text/javascript\">\r\n";

if ($useAJAX) {
##foreach @TABLE.arrFieldObj as @f filter @f.bAdvancedSearch && @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
		$output = loadSelectContent("##@f.strName s##",@$_SESSION[$strTableName."_asearchfor"]["##@f.strCategoryControlField s##"],@$_SESSION[$strTableName."_asearchfor"]["##@f.strName s##"]);
		$txt = ""; 
		foreach( $output as $value ) {
			$txt .= jsreplace($value)."\\n";
		}
		$linkdata .= "preloadSelectContent('".$txt."', 'value_##@f.strName g##', '".jsreplace(@$_SESSION[$strTableName."_asearchfor"]["##@f.strName s##"])."','');\r\n";
##endfor##
}
else
{
	##foreach Fields as @f filter @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory && @f.bAdvancedSearch##
	//	##@f.strName## - ##@f.strCategoryControlField##
	$linkdata.="bLoading = true;";
	$linkdata.="SetSelection('value_##@f.strCategoryControlField g##', 'value_##@f.strName g##', '".jsreplace(@@$_SESSION[$strTableName."_asearchfor"]["##@f.strCategoryControlField s##"])."', '".jsreplace(@$_SESSION[$strTableName."_asearchfor"]["##@f.strName s##"])."', arr_##@f.strName g##);";
	$linkdata.="bLoading = false;";
	##endfor##
}
$linkdata.="</script>\r\n";

$smarty->assign("linkdata",$linkdata);

$templatefile = "##@TABLE.strShortTableName##_search.htm";
if(function_exists("BeforeShowSearch"))
	BeforeShowSearch($smarty,$templatefile);

$smarty->display($templatefile);

?>