<?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(isset($_SESSION[$strTableName.'_advsearch']))
{
	$whereObject = unserialize($_SESSION[$strTableName.'_advsearch']);
	$where = $whereObject->getTable();
}
else
{
	$where = &$_SESSION;
}

$pageName = "search.php";


//Basic includes js files
$includes="";
//javascript code
$jscode="";

##if @BUILDER.bCreateLoginPage##
$chrt_array=array();
$rpt_array=array();
##if @BUILDER.bWebReports##
include("include/reportfunctions.php");
if(isset( $_GET["rname"]))
{
	$rpt_array = getReportArray(postvalue("rname"));
}
else if(isset( $_GET["cname"]))
{
	$chrt_array = getChartArray(postvalue("cname"));
}
##endif##
//	check if logged in
if( (!@$_SESSION["UserID"] || !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search") && !@$chrt_array['status'] && !@$rpt_array['status'])
|| (@$rpt_array['status'] == "private" && @$rpt_array['owner'] != @$_SESSION["UserID"])
|| (@$chrt_array['status'] == "private" && @$chrt_array['owner'] != @$_SESSION["UserID"]) )
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
##endif##

include('include/xtempl.php');
$xt = new Xtempl();

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

$includes.="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\" src=\"include/customlabels.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";
AddJSFile("ajaxsuggest");


$jscode.="window.TEXT_MONTH_JAN='".jsreplace(##message MONTH_JAN##)."';\r\n";
$jscode.="window.TEXT_MONTH_FEB='".jsreplace(##message MONTH_FEB##)."';\r\n";
$jscode.="window.TEXT_MONTH_MAR='".jsreplace(##message MONTH_MAR##)."';\r\n";
$jscode.="window.TEXT_MONTH_APR='".jsreplace(##message MONTH_APR##)."';\r\n";
$jscode.="window.TEXT_MONTH_MAY='".jsreplace(##message MONTH_MAY##)."';\r\n";
$jscode.="window.TEXT_MONTH_JUN='".jsreplace(##message MONTH_JUN##)."';\r\n";
$jscode.="window.TEXT_MONTH_JUL='".jsreplace(##message MONTH_JUL##)."';\r\n";
$jscode.="window.TEXT_MONTH_AUG='".jsreplace(##message MONTH_AUG##)."';\r\n";
$jscode.="window.TEXT_MONTH_SEP='".jsreplace(##message MONTH_SEP##)."';\r\n";
$jscode.="window.TEXT_MONTH_OCT='".jsreplace(##message MONTH_OCT##)."';\r\n";
$jscode.="window.TEXT_MONTH_NOV='".jsreplace(##message MONTH_NOV##)."';\r\n";
$jscode.="window.TEXT_MONTH_DEC='".jsreplace(##message MONTH_DEC##)."';\r\n";

$jscode.="window.TEXT_DAY_SU='".jsreplace(##message DAY_SU##)."';\r\n";
$jscode.="window.TEXT_DAY_MO='".jsreplace(##message DAY_MO##)."';\r\n";
$jscode.="window.TEXT_DAY_TU='".jsreplace(##message DAY_TU##)."';\r\n";
$jscode.="window.TEXT_DAY_WE='".jsreplace(##message DAY_WE##)."';\r\n";
$jscode.="window.TEXT_DAY_TH='".jsreplace(##message DAY_TH##)."';\r\n";
$jscode.="window.TEXT_DAY_FR='".jsreplace(##message DAY_FR##)."';\r\n";
$jscode.="window.TEXT_DAY_SA='".jsreplace(##message DAY_SA##)."';\r\n";

$jscode.="window.TEXT_TODAY='".jsreplace(##message TODAY##)."';\r\n";
	
AddJSFile("calendar");
///////////////////////
##if Fields[bAdvancedSearch && strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && nControlType==LCT_LIST].len##
AddJSFile("ui");
AddJSFile("ui.core","ui");
AddJSFile('ui.resizable','ui.core');
AddJSFile("onthefly");
##endif##
////////////////////// time picker
##foreach Fields as @f filter @f.useTimePicker##
##if @first##
AddJSFile("ui");
AddJSFile("jquery.utils","ui");
AddJSFile("ui.dropslide","jquery.utils");
AddJSFile("ui.timepickr","ui.dropslide");
AddCSSFile("ui.dropslide");
##endif##
##endfor##
//////////////////////

$jscode.="locale_dateformat = ".$locale_info["LOCALE_IDATE"].";\r\n".
"locale_datedelimiter = \"".$locale_info["LOCALE_SDATE"]."\";\r\n".
"bLoading=false;\r\n".
"TEXT_PLEASE_SELECT='".jsreplace(##message PLEASE_SELECT##)."';\r\n".
"SUGGEST_TABLE = \"##@TABLE.strShortTableName##_searchsuggest.php\";\r\n".
"detect = navigator.userAgent.toLowerCase();
window.checkIt = function(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
window.ShowHideControls = function ()
{
##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;
}
window.ResetControls = function()
{
	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##
$jscode.="
##foreach Fields as @f filter @f.bAdvancedSearch && (@f.strEditFormat==EDIT_FORMAT_TEXT_FIELD||@f.strEditFormat==EDIT_FORMAT_READONLY||@f.strEditFormat==EDIT_FORMAT_FILE)##
	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##";
$includes.="<div id=\"search_suggest\"></div>";
##else##
$jscode.="
window.OnKeyDown = function (e)
{ if(!e) e = window.event; 
if (e.keyCode == 13){ e.cancel = true; document.forms[0].submit();}};";
##endif##

$all_checkbox="value=\"and\"";
$any_checkbox="value=\"or\"";

if(@$where[$strTableName."_asearchtype"]=="or")
	$any_checkbox.=" checked";
else
	$all_checkbox.=" checked";
$xt->assign("any_checkbox",$any_checkbox);
$xt->assign("all_checkbox",$all_checkbox);

$editformats=array();

	if(!is_array($where[$strTableName."_asearchopt"]))
		$where[$strTableName."_asearchopt"]=array();
	if(!is_array($where[$strTableName."_asearchnot"]))
		$where[$strTableName."_asearchnot"]=array();
	if(!is_array($where[$strTableName."_asearchfor"]))
		$where[$strTableName."_asearchfor"]=array();
	if(!is_array($where[$strTableName."_asearchfor2"]))
		$where[$strTableName."_asearchfor2"]=array();


##foreach Fields as @f filter @f.bAdvancedSearch && !IsBinaryField(@f) order @f.nAdvSearchPageOrder##
// ##@f.strName## 
$opt="";
$not=false;
$control_##@f.strName g##=array();
$control_##@f.strName g##["params"] = array();
if(@$where[$strTableName."_search"]==2)
{
	$opt=@$where[$strTableName."_asearchopt"]["##@f.strName s##"];
	$not=@$where[$strTableName."_asearchnot"]["##@f.strName s##"];
	$control_##@f.strName g##["params"]["value"]=@$where[$strTableName."_asearchfor"]["##@f.strName s##"];
}
$control_##@f.strName g##["func"]="xt_buildeditcontrol";
$control_##@f.strName g##["params"]["field"]="##@f.strName s##";
$control_##@f.strName g##["params"]["mode"]="search";
$xt->assignbyref("##@f.strName g##_editcontrol",$control_##@f.strName g##);
$control1_##@f.strName g##=$control_##@f.strName g##;
$control1_##@f.strName g##["params"]["second"]=true;
if(@$where[$strTableName."_search"]==2)
	$control1_##@f.strName g##["params"]["value"]=@$where[$strTableName."_asearchfor2"]["##@f.strName s##"];
$xt->assignbyref("##@f.strName g##_editcontrol1",$control1_##@f.strName g##);
	
$xt->assign_section("##@f.strName g##_fieldblock","<input type=\"Hidden\" name=\"asearchfield[]\" value=\"##@f.strName h##\">","");
$notbox_##@f.strName g##="name=\"not_##@f.strName g##\"";
if($not)
	$notbox_##@f.strName g##=" checked";
$xt->assign("##@f.strName g##_notbox",$notbox_##@f.strName g##);

//	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##
  ##if @f.nSelectSize>1 || @f.nControlType==LCT_CBLIST##
$options.="<OPTION VALUE=\"Contains\" ".(($opt=="Contains")?"selected":"").">".##message CONTAINS##."</option>";
  ##else##
$options.="<OPTION VALUE=\"Equals\" ".(($opt=="Equals")?"selected":"").">".##message EQUALS##."</option>";
  ##endif##
##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>";
$xt->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##

if ($useAJAX) {
##foreach @TABLE.arrFieldObj as @f filter @f.bAdvancedSearch && @f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD && @f.bUseCategory##
		$output = loadSelectContent("##@f.strName s##",@$where[$strTableName."_asearchfor"]["##@f.strCategoryControlField s##"],@$where[$strTableName."_asearchfor"]["##@f.strName s##"]);
		$txt = ""; 
		foreach( $output as $value ) {
			$txt .= jsreplace($value)."\\n";
		}
		$jscode .= "preloadSelectContent('".$txt."', 'value_##@f.strName g##', '".jsreplace(@$where[$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##
	$jscode.="bLoading = true;";
	$jscode.="SetSelection('value_##@f.strCategoryControlField g##', 'value_##@f.strName g##', '".jsreplace(@@$where[$strTableName."_asearchfor"]["##@f.strCategoryControlField s##"])."', '".jsreplace(@$where[$strTableName."_asearchfor"]["##@f.strName s##"])."', arr_##@f.strName g##);";
	$jscode.="bLoading = false;";
	##endfor##
}

$body=array();
$body["begin"]=$includes;
$jscode.="ShowHideControls();";
PrepareJSCode($jscode,'');
$body["end"]="<script>".$jscode."</script>";	
$xt->assignbyref("body",$body);

$contents_block=array();
$contents_block["begin"]="<form method=\"POST\" ";
if(isset( $_GET["rname"]))
{
	$contents_block["begin"].="action=\"dreport.##@ext##?rname=".htmlspecialchars(rawurlencode(postvalue("rname")))."\" ";
}	
else if(isset( $_GET["cname"]))
{
	$contents_block["begin"].="action=\"dchart.##@ext##?cname=".htmlspecialchars(rawurlencode(postvalue("cname")))."\" ";
}	
else
{
##if IsTable(@TABLE)##
$contents_block["begin"].="action=\"##@TABLE.strShortTableName##_list.##@ext##\" ";
##elseif IsReport(@TABLE)##
$contents_block["begin"].="action=\"##@TABLE.strShortTableName##_report.##@ext##\" ";
##elseif IsChart(@TABLE)##
$contents_block["begin"].="action=\"##@TABLE.strShortTableName##_chart.##@ext##\" ";
##endif##
}
$contents_block["begin"].="name=\"editform\"><input type=\"hidden\" id=\"a\" name=\"a\" value=\"advsearch\">";
$contents_block["end"]="</form>";
$xt->assignbyref("contents_block",$contents_block);

$xt->assign("searchbutton_attrs","name=\"SearchButton\" onclick=\"javascript:document.forms.editform.submit();\"");
$xt->assign("resetbutton_attrs","onclick=\"return ResetControls();\"");

$xt->assign("backbutton_attrs","onclick=\"javascript: document.forms.editform.a.value='return'; document.forms.editform.submit();\"");

$xt->assign("conditions_block",true);
$xt->assign("search_button",true);
$xt->assign("reset_button",true);
$xt->assign("back_button",true);

##if @BUILDER.bWebReports##
if ( isset( $_GET["rname"] ) ) {
	$xt->assign("dynamic", "true");
	$xt->assign("rname", @$_GET["rname"]);
}
if ( isset( $_GET["cname"] ) ) {
	$xt->assign("dynamic", "true");
	$xt->assign("cname", @$_GET["cname"]);
}
##endif##
	
$templatefile = "##@TABLE.strShortTableName##_search.htm";
if(function_exists("BeforeShowSearch"))
	BeforeShowSearch($xt,$templatefile);

$xt->display($templatefile);

?>