<?php


session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$querylog=" select ifNull(FC_CashRegisterDel,0) As FC_CashRegisterDel, ifNull(FC_CashRegisterMod,0) As FC_CashRegisterMod, ".
          " ifNull(FC_CashRegScrollPrint,0) As FC_CashRegScrollPrint, ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ".
		  " ifNull(FC_CashRegisterCredit,0) As FC_CashRegisterCredit, ifNull(FC_SearchByCashRegister,0) As FC_SearchByCashRegister, ".
		  " ifNull(FC_CashRegisterVoid,0) As FC_CashRegisterVoid, ifNull(FC_CashRegisterWaste,0) As FC_CashRegisterWaste ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


//Read From CashRegister Table
$queryCashRegister=" Select ifNull(CashRegister_SlipPrinter,0) As CashRegister_SlipPrinter, ifNull(Module_RestaurantDelivery,0) As Module_RestaurantDelivery  ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_PrintWaybill,0) As Module_PrintWaybill From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


//Access Mode On Insert/Edit/Delete
$DelMode = '';
$EditMode = '';
$PrevMode = '';

$PrevHint   = '';
$EditHint   = '';
$DelHint    = '';


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2,ifnull(cashreg_colortheme,'default') as cashreg_colortheme from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}

$MainCompanyName=explode('_',$_SESSION['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$_SESSION['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$_SESSION['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	
			  
//echo $queryperiod;	


$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$_SESSION['dbLabel'].".ledgerstk  ".
			  " where user_account='".$_SESSION['useraccount']."' ".
			  " and (ledger_number is not null and ledger_number != '') ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];


$keyword    = $_POST['keyword']; if ($keyword == ""){$keyword = $_GET["keyword"];}
$searchby   = $_POST['searchby']; if ($searchby == ""){$searchby = $_GET["searchby"];}
$orderby    = $_POST['orderby']; if ($orderby == ""){$orderby = $_GET["orderby"];}
$perpage    = $_POST['perpage']; if ($perpage == ""){$perpage = $_GET["perpage"];}
$startrange = $_POST['startrange']; if ($startrange == ""){$startrange = $_GET["startrange"];}

$PMode = $_POST['PMode']; if ($PMode == ""){$PMode = $_GET["PMode"];}
$SelectedRec = $_POST['SelectedRec']; if ($SelectedRec == ""){$SelectedRec = $_GET["SelectedRec"];}
$SelectedRecColor = $_POST['SelectedRecColor']; if ($SelectedRecColor == ""){$SelectedRecColor = $_GET["SelectedRecColor"];}
$Sale_Id = $_POST['Sale_Id']; if ($Sale_Id == ""){$Sale_Id = $_GET["Sale_Id"];}
$index = $_POST['index']; if ($index == ""){$index = $_GET["index"];}
$ReportName = $_POST['ReportName']; if ($ReportName == ""){$ReportName = $_GET["ReportName"];}
$Sale_Reference = $_POST['Sale_Reference']; if ($Sale_Reference == ""){$Sale_Reference = $_GET["Sale_Reference"];}
$PrevWaybillHint = $_POST['PrevWaybillHint']; if ($PrevWaybillHint == ""){$PrevWaybillHint = $_GET["PrevWaybillHint"];}
$PrevHint = $_POST['PrevHint']; if ($PrevHint == ""){$PrevHint = $_GET["PrevHint"];}
$EditHint = $_POST['EditHint']; if ($EditHint == ""){$EditHint = $_GET["EditHint"];}
$DelHint = $_POST['DelHint']; if ($DelHint == ""){$DelHint = $_GET["DelHint"];}


if (empty($orderby))  $orderby = 2;
if (empty($searchby)) $searchby = 2;
if ($startrange=='')  $startrange = 0;
if ($perpage=='')	  $perpage = 20;

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
HERE;

if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}

print <<<HERE
<title>Search Invoices</title>
<script type="text/javascript" language="javascript">

function keypressed(e){

var intKey = (window.Event) ? e.which : e.keyCode;

if(intKey==40 && document.search.index.value!='') {
	var id = parseInt(document.search.index.value)
	id = id + 1;
	id='row' + id;
	if(document.getElementById(id))
		document.getElementById(id).click();

}

if(intKey==38 && document.search.index.value!='') {
	var id = parseInt(document.search.index.value)
	id = id - 1;
	id='row' + id;
		if(document.getElementById(id))
	document.getElementById(id).click();

}

}

function changecolor(vobj,vtextname,vcolorname,Sale_ID,Sale_Reference,ReportName,ct,FC_CashRegScrollPrint,Module_PrintWaybill,FC_CashRegisterMod,FC_CashRegisterDel,PrevWaybillHint,PrevHint,EditHint,DelHint,SaleStatus ){


	//if(FC_CashRegScrollPrint==1 && Module_PrintWaybill==1) 
		//document.search.btnWaybill.style.visibility = 'visible';
	//else	document.search.btnWaybill.style.visibility = 'hidden';

	if(FC_CashRegScrollPrint==1) {
		document.search.btnPreview.style.visibility = 'visible';
		document.search.btnPreview2.style.visibility = 'visible';
	}
	else{	document.search.btnPreview.style.visibility = 'hidden';
	document.search.btnPreview2.style.visibility = 'hidden';
	}

	if (SaleStatus != 2 && SaleStatus != 3) {
	
		if(FC_CashRegisterMod==1)
			document.search.btnEdit.style.visibility = 'visible';
		else	document.search.btnEdit.style.visibility = 'hidden';

		if(FC_CashRegisterDel==1)
			document.search.btnDelete.style.visibility = 'visible';
		else	document.search.btnDelete.style.visibility = 'hidden';

		if (document.getElementById('btnVoid'))  document.search.btnVoid.style.visibility = 'visible';
		if (document.getElementById('btnWaste')) document.search.btnWaste.style.visibility = 'visible';

		document.search.index.value=ct;



		document.getElementById('Sale_Id').value = Sale_ID;
		document.getElementById('ReportName').value = ReportName;
		document.getElementById('Sale_Reference').value = Sale_Reference;


		document.getElementById('btnWaybill').title = PrevWaybillHint;
		document.getElementById('btnPreview').value = PrevHint;
		document.getElementById('btnEdit').value = EditHint;
		document.getElementById('btnDelete').value = DelHint;
	} else{	
		document.search.btnEdit.style.visibility = 'hidden';
		document.search.btnDelete.style.visibility = 'hidden';
		if (document.getElementById('btnVoid'))  document.search.btnVoid.style.visibility = 'hidden';
		if (document.getElementById('btnWaste')) document.search.btnWaste.style.visibility = 'hidden';
		
		document.getElementById('Sale_Id').value = Sale_ID;
		document.getElementById('ReportName').value = ReportName;
		document.getElementById('Sale_Reference').value = Sale_Reference;
	}
	
	if (document.getElementById(vtextname).value=='')
	{
		document.getElementById(vtextname).value = vobj.id;
		document.getElementById(vcolorname).value = vobj.style.backgroundColor;
	}	
	else {

		var prevobj = document.getElementById(vtextname).value;

		document.getElementById(prevobj).style.backgroundColor = document.getElementById(vcolorname).value;
		document.getElementById(vtextname).value = vobj.id;
		document.getElementById(vcolorname).value = vobj.style.backgroundColor;
	}	

	document.getElementById(vobj.id).style.backgroundColor ='#B8EDFF';

}



function clearform()
{
  document.search.keyword.value='';

  document.search.action='Form_RetailSearch.php';
  document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.keyword.focus();
	
	ChangeLocation();
}

function SetNextPage(startrange,perpage){ 
	
	document.search.action='Form_RetailSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){

	document.search.action='Form_RetailSearch.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


function DeleteRecord(RetId, RetRef){


	RetId = document.search.Sale_Id.value;
	RetRef = document.search.Sale_Reference.value;
	if (RetId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+RetRef+" ?") )
		{
			document.search.action="Form_Retail_Actions.php?mainmode=delete&SRetId="+RetId;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(RetId){
	
	
	
	RetId = document.search.Sale_Id.value;	
	
	if(RetId!=''){
	document.search.action="Form_Retail_Actions.php?mainmode=search&SRetId="+RetId;
	document.search.submit();
	
	//document.search.PMode.value=1;
	
	opener.document.retail.txtId.value=RetId;
	opener.document.retail.mainaction.value='edit';
	opener.document.retail.actionmode.value='';
	opener.document.retail.copiedVers.value='';
	}

}

function ControlInvoice(Option){
	//Option: 1-Void,  2-Waste
	
	if (Option == 1) {vMode = 'voidinvoice'; vAction = 'Void'; }
	else if (Option == 2) {vMode = 'wasteinvoice'; vAction = 'Waste'; }

	RetId = document.search.Sale_Id.value;
	RetRef = document.search.Sale_Reference.value;
	
	if(RetId!=''){
		if (confirm ( "Are you sure you want to "+vAction+" this record with reference = "+RetRef+" ?") )
		{
			document.search.action="Form_Retail_Actions.php?mainmode="+vMode+"&SRetId="+RetId;
			document.search.submit();
		}
	}	
}

function ChangeLocation()
{
    if (document.search.PMode.value == 1) {
		opener.document.retail.action='Form_Retail.php';
		opener.document.retail.submit();
		self.close();
	} else {
		opener.document.retail.txtId.value='';
		opener.document.retail.mainaction.value='insert';
		opener.document.retail.actionmode.value='';
	}
}


function PrintPreview(RetId, ReportName)
{
	var str = '';
	RetId = document.search.Sale_Id.value;
	ReportName = document.search.ReportName.value;
	
	
	str = ReportName+'?RetailId='+RetId+'&countPrints=OK';
	window.open(str, 'Form_RetailSlip', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500,top=100');	
								  
	//self.close();

}

function PrintPreview2(RetId, ReportName)
{
	var str = '';
	RetId = document.search.Sale_Id.value;
	//ReportName = document.search.ReportName.value;
	
	
	str = ReportName+'?RetailId='+RetId;
	window.open(str, 'Form_RetailSlip', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500,top=100');	
								  
	//self.close();

}

function PrintPreviewWaybill(RetId, ReportName)
{
	var str = '';
	
	RetId = document.search.Sale_Id.value;
	ReportName = document.search.ReportName.value;
	
	str = ReportName+'?RetailId='+RetId+'&Waybill=1';
	window.open(str, 'Form_RetailSlip', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=350,height=500,top=100');	
								  
	//self.close();

}



function SelectSearchBy()
{	

  //document.search.keyword.value='';
  document.search.action='Form_RetailSearch.php?startrange=0';
  document.search.submit();

}

function SubmitForm()
{	

  //document.search.keyword.value='';
  document.search.action='Form_RetailSearch.php?startrange=0';
  document.search.submit();

}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_RetailSearch.php?startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_RetailSearch.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function RefreshPage()
{
 document.search.action='Form_RetailSearch.php?startrange=0';	
  document.search.submit();

}

function kd(e)
{
	x=document.search;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
    return true;
}

function OpenKeyboard(field){
	VirtualKeyboard.attachInput(field);
	VirtualKeyboard.open(field,'td');
}

</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

 button {
  padding: 0 0 0 0;
  margin: 0 3px 0 3px;
  border: none;
  cursor: pointer;
  text-decoration: none;
  outline: none;
  white-space: nowrap;
  overflow: visible;
  height: 22px; 
  font-family: 11px Arial, sans-serif;
  font-size: 13px;
  color: #000000;
  background: none;
  font-weight: bold;

}

</style>

</head>
<body onLoad='LoadForm();'  onkeydown='return keypressed(event);'  class='bodybackground'>
<form name='search' method='post' action="">
<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
<input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
<input type='hidden' name='Sale_Id' id='Sale_Id' value='$Sale_Id'>
<input type='hidden' name='index' id='index' value='$index'>
<input type='hidden' name='ReportName' id='ReportName' value='$ReportName'>
<input type='hidden' name='Sale_Reference' id='Sale_Reference' value='$Sale_Reference'>

<input type='hidden' name='PrevWaybillHint' id='PrevWaybillHint' value='$PrevWaybillHint'>
<input type='hidden' name='PrevHint' id='PrevHint' value='$PrevHint'>
<input type='hidden' name='EditHint' id='EditHint' value='$EditHint'>
<input type='hidden' name='DelHint ' id='DelHint' value='$DelHint'>
HERE;


$PRef = GetSQLValueString($keyword, 'text');

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
$querysearch=" Select SQL_CALC_FOUND_ROWS Distinct Retail.Sale_ID, Retail.Sale_Reference, Date_Format(Retail.Sale_Date,'%d/%m/%Y %h:%i %p') As Sale_Date, ".
             " Retail.Currency_code, Retail.Client_Code As ClientCode, Clients.Ledger_name As ClientName, Retail.CashRegister_Name As CashRegisterName, ".
			 " Round((ifNull(Retail.Sale_Taxable,0) + ifNull(Retail.Sale_NoneTaxable,0) + ifNull(Retail.Sale_VAT,0) - ifNull(Retail.Sale_DiscountAmount,0) + ifnull(Retail.sale_TaxConsumption,0) + ifnull(Retail.sale_service,0) + ".
			 " (ifNull(Retail.Sale_Tax1,0) + ifNull(Retail.Sale_Tax2,0) + ifNull(Retail.Sale_Tax3,0) + ifNull(Retail.Sale_Tax4,0) + ifNull(Retail.Sale_Tax5,0))), ".$DecimalBase.") As NetTotal,  ".
			 " Retail.Journal_Type As RetailType, ifNull(Temp_Retail.Sale_ID, -1) As TempSaleId, Retail.Sales_CreditAcc, Ledger.Ledger_Name As Sales_CreditName, Customer.Customer_Name As CustomerName,  ".
			 " ifNull(Retail.Sale_Status,0) as SaleStatus, if(ifNull(Retail.Sale_Status,0)=2, 'Void', if(ifNull(Retail.Sale_Status,0)=3, 'Waste', '')) as SaleStatusRemark, Projectdt_Name ".
			 " From Retail Left Join Clients on Retail.Client_Code = Clients.Ledger_Number ".
			 " Left Join Ledger on Retail.Sales_CreditAcc =  Ledger.Ledger_Number ".
			 " Left Join Customer on Retail.Customer_Code =  Customer.Customer_Code ".
			 " Left Join Temp_Retail on Temp_Retail.Sale_ID = Retail.Sale_ID ".
			 " Left Join Projectdt on Projectdt.Projectdt_ID = Retail.Projectdt_ID ";
			 
			  
/*if ($ledgerexist > 0)
  $querysearch= $querysearch." Inner join ( select distinct ledger_number from ".$_SESSION['dbLabel'].".ledgerstk ".
							 "			    where user_account='".$_SESSION['useraccount']."' ".
							 "              and (ledger_number is not null and ledger_number != '') ".
							 "			 )  Q3 on (($ledgerinclude = 1 and Retail.Client_Code like concat(Q3.ledger_number,'%') ) or ($ledgerinclude = 0 and Retail.Client_Code Not like concat(Q3.ledger_number,'%') ) or Retail.Client_Code is null) ";							 
*/
//  $querysearch= $querysearch." And (($ledgerinclude = 1 and Retail.Client_Code In (select distinct ledger_number from ".$_SESSION['dbLabel'].".ledgerstk where user_account='".$_SESSION['useraccount']."') ) Or ".
  //                           "      ($ledgerinclude = 0 and Retail.Client_Code Not In (select distinct ledger_number from ".$_SESSION['dbLabel'].".ledgerstk where user_account='".$_SESSION['useraccount']."') ) ) ";      


							 
$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and Retail.Sale_Reference Like Concat(ifNull($PRef, Retail.Sale_Reference),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(Retail.Sale_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(Retail.Sale_Date,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 3 and (Retail.Client_Code Like Concat(ifNull($PRef, Retail.Client_Code),'%') or Clients.Ledger_name Like Concat('%', ifNull($PRef, Clients.Ledger_name),'%'))) Or ".
			 "        ($searchby = 4 and Retail.Sales_CreditAcc Like Concat('%',ifNull($PRef, Retail.Sales_CreditAcc),'%')) Or ".
			 "        ($searchby = 5 and (ifNull(Retail.Customer_Code,-1) Like Concat('%',ifNull($PRef, ifNull(Retail.Customer_Code,-1)),'%') or (Customer.Customer_Name Like Concat('%', ifNull($PRef, Customer.Customer_Name),'%')))) ) ".
			 " And (($FC_SearchByCashRegister = 1 and Retail.CashRegister_Name = '".$_SESSION['cashregister']."') or ".
			 "      ($FC_SearchByCashRegister = 0 and ((Retail.CashRegister_Name = '".$_SESSION['cashregister']."' and 1 = 1) or (Retail.CashRegister_Name != '".$_SESSION['cashregister']."' and ifNull(Retail.Sale_Posted,0) = 1) ))) ".
			 " And ifNull(Retail.Sale_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
			 " And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(Retail.Sale_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',Retail.Sale_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(Retail.Sale_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ";
		 

if ($orderby == 1)
	$querysearch= $querysearch." Order By Retail.Sale_Date Desc, Retail.Sale_Reference Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By Retail.Sale_Reference Desc ";	
	
			 
//	echo $querysearch;			 
// $resulrowcount = "SELECT FOUND_ROWS()";
// $resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
// $totalrows = mysql_num_rows($resultsearch);
	

		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	

$row = mysql_fetch_row(mysql_query("SELECT FOUND_ROWS()"));
	$totalrows = $row[0];
	
echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:1075px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Invoices
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
echo"<table border='0' cellspacing='1' style='table-layout:fixed; width:1040px;'>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=80>
	  <col width=100>
	  <col width=50>
	  <col width=100>";
	  
	  if ($FC_CashRegisterCredit == 1)
		echo"<col width=150>";
	  else echo"<col width=380>";
	  
if ($FC_CashRegisterCredit == 1) {	  
	echo"<col width=80>
		 <col width=150>";
}
		 
echo" <col width=95>
	  <col width=65>
	  <col width=120>";
		     
	 /* <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='12'><span class='style16'>List of Invoices</span></td>";
		
if ($FC_CashRegisterCredit == 1) 
	echo"<td colspan='2'>&nbsp;</td>";
	
echo" </tr>";*/

echo" <tr class='Search-header-bg'>";	  
echo"   <td align='left' colspan='5'><span class='style4'>Search By</span></td>
		<td align='left' colspan='5'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30' onkeydown='return kd(event);'  /><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		  
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
			if ($searchby==1)		$s21 = 'selected';
			else if ($searchby==2)	$s22 = 'selected';
			else if ($searchby==3)	$s23 = 'selected';
			else if ($searchby==4)	$s24 = 'selected';
			else if ($searchby==5)	$s25 = 'selected';
				
			echo"<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>Reference</option>
				 <option ".$s23." value='3'>Client</option>
				 <option ".$s24." value='4'>Credit Acc</option>
				 <option ".$s25." value='5'>Customer</option>
				 </select>";		
				 
echo"	    
		<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='clearform();return false;' title='Cancel'><span><span>Cancel</span></span></button>";
			
		
		echo"</td>
		<td align='right' valign='center' ><span class='style4'>Order By</span></td>
		
		  <td align='left' ><span class='style4'><select name='orderby' id='orderby' value='$orderby' onchange='SelectSearchBy();'>";
		
		if ($orderby==1)
			$s11 = 'selected';
		else if ($orderby==2)
			$s12 = 'selected';
			
		echo"<option ".$s11." value='1'>Date</option>
			 <option ".$s12." value='2'>Reference</option>
			 </select></span></td>";
	//	echo"<td>&nbsp;</td>";	 
		
if ($FC_CashRegisterCredit == 1) 
	echo"<td colspan='2'>&nbsp;</td>";
	
echo" </tr>";
echo" <tr class='Search-header-bg'>
		<td align='left' valign='center' colspan='8'>&nbsp;</td>";
		echo"<td>&nbsp;</td>";
		echo"<td>&nbsp;</td>";
		echo"<td>&nbsp;</td>";
		echo"<td>&nbsp;</td>";

if ($FC_CashRegisterCredit == 1) 
	echo"<td colspan='2'>&nbsp;</td>";
	
echo" </tr>
	  <tr class='Search-header-bg'>
		<td height='15' align='center' colspan='12'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>";

if ($FC_CashRegisterCredit == 1) 
	echo"<td colspan='2'>&nbsp;</td>";
	
echo" 	</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	

  echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:1040px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage();\">";
              
			if ($perpage==20)
				$s20 = 'selected';
			else if ($perpage==30)
				$s30 = 'selected';
			else if ($perpage==40)
				$s40 = 'selected';
			else if ($perpage==50)
				$s50 = 'selected';

						
							
			echo'<option '.$s20.' value="20">20</option>
				 <option '.$s30.' value="30">30</option>
				 <option '.$s40.' value="40">40</option>
				 <option '.$s50.' value="50">50</option>
				 </select></span></td>
			<td  style="width:74px;border-right:0px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
		
echo"<tr>";		

echo"	<td colspan=10><table border=0   cellpadding='0' cellspacing='1'><tr>    ";
echo" 	<td><button type='button' style='visibility:hidden' id='btnWaybill' name='btnWaybill'  onclick=\"PrintPreviewWaybill('".$Sale_ID."', '".$ReportName."');\" title='Waybill'><span><span>Waybill</span></span></button></td>";
echo"	<td><button type='button' style='visibility:hidden'  id='btnPreview' name='btnPreview'   onclick=\"PrintPreview('".$Sale_ID."', '".$ReportName."');\"  title='Preview'><span><span>Preview</span></span></button></td>";
echo"	<td><button type='button' style='visibility:hidden'  id='btnPreview2' name='btnPreview2'   onclick=\"PrintPreview2('".$Sale_ID."', 'Form_RetailSlip_NoPrice.php');\"  title='Preview'><span><span>Preview without prices</span></span></button></td>";
echo"	<td><button type='button' style='visibility:hidden' id='btnEdit' name='btnEdit' onclick=\"UpdateRecord('".$Sale_ID."');\"  title='Edit'><span><span>Edit Record</span></span></button></td>";
echo"	<td><button type='button' style='visibility:hidden' id='btnDelete' name='btnDelete'  onclick=\"DeleteRecord('".$Sale_ID."', '".$Sale_Reference."');\"  title='Delete'><span><span>Delete Record</span></span></button></td>";
 
if ($FC_CashRegisterVoid == 1)
	echo"<td><button type='button' style='visibility:hidden' id='btnVoid' name='btnVoid'  onclick=\"ControlInvoice(1);\"  title='Void Invoice'><span><span>Refund (Void)</span></span></button></td>";
if ($FC_CashRegisterWaste == 1)
	echo"<td><button type='button' style='visibility:hidden' id='btnWaste' name='btnWaste'  onclick=\"ControlInvoice(2);\"  title='Waste Invoice'><span><span>Refund (Waste)</span></span></button></td>";
	
echo"	</tr>
		</table></td>";

echo"</tr>";		
		
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>";
	echo"<div style='overflow:scroll;height:400px;width:1070px;'>";
	echo"<table border='0'  cellpadding='0' cellspacing='1' >
		  <col width=80>
		  <col width=150>
		  <col width=40>
		  <col width=40>
		  <col width=300>";

if ($Module_RestaurantDelivery == 1) {
	echo"<col width=60>";
}

if ($FC_CashRegisterCredit == 1) {	  
	echo"<col width=80>
		 <col width=150>";
}
echo" <col width=50>
	  <col width=50>
	  <col width=120>
	  
	  <tr class='Search-Label-bg'>
		
		<td  height='25' align='left'><span class='style4'>Reference </span></td>
		<td  height='25' align='center'><span class='style4'>Date </span></td>
		<td  height='25' align='center'><span class='style4'>Type </span></td>
		<td  height='25' align='center'><span class='style4'>Table </span></td>
		<td  height='25' align='center'><span class='style4'>Customer</span></td>";
		
if ($Module_RestaurantDelivery == 1) {
	echo"<td  height='25' align='center'><span class='style4'>Status</span></td>";
}

if ($FC_CashRegisterCredit == 1) {
	echo"<td  height='25' align='center'><span class='style4'>Credit Acc</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>";
}
		
echo"	<td  height='25' align='center'><span class='style4'>POS</span></td>
		<td  height='25' align='center'><span class='style4'>Curr</span></td>
		<td  height='25' align='center'><span class='style4'>Total</span></td>
	  </tr>";
		
	

$i = 0;
while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
	
	$PrevWaybillHint='Waybill';
	$PrevHint   = 'Preview';
	$EditHint   = 'Edit Record';
	$DelHint    = 'Delete Record';
	
	if ($TempSaleId != -1) {$PrevHint=$EditHint=$DelHint =$PrevWaybillHint= 'Exists in Pending';}
	
    if ($FC_CashRegisterMod == 0)	$EditMode = ' disabled '; 
	else if ($FC_CashRegisterMod == 1 && $TempSaleId != -1) $EditMode = ' disabled ';
	else if ($FC_CashRegisterMod == 1 && $TempSaleId == -1) $EditMode = '';
	
	if ($FC_CashRegisterDel == 0)	$DelMode = ' disabled '; 
	else if ($FC_CashRegisterDel == 1 && $TempSaleId != -1) $DelMode = ' disabled ';
	else if ($FC_CashRegisterDel == 1 && $TempSaleId == -1) $DelMode = '';
	
	
	if ($TempSaleId != -1) $PrevMode = ' disabled '; else $PrevMode = '';
	
	if ($CashRegister_SlipPrinter == 1) $ReportName = 'Form_RetailSlip.php';
	else $ReportName = 'Form_RetailPreview.php';
	
	echo"<tr  onMouseOver=\"style.cursor='pointer'\"  id='row$ct' onclick=\"changecolor(this,'SelectedRec','SelectedRecColor','".$Sale_ID."','".$Sale_Reference."','".$ReportName."','".$ct."','".$FC_CashRegScrollPrint."','".$Module_PrintWaybill."','".$FC_CashRegisterMod."','".$FC_CashRegisterDel."','".$PrevWaybillHint."','".$PrevHint."','".$EditHint."','".$DelHint."','".$SaleStatus."')\"  ".$Color.">";
	
	//Gilbert 20-11-2013
	//Adding column for waybill
	/*
	if ($FC_CashRegScrollPrint == 1 && $Module_PrintWaybill == 1)
		echo'<td align="center"><span class="style5"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreviewWaybill('.$Sale_ID.', \''.$ReportName.'\');"  title="'.$PrevWaybillHint.'"  style="width: 15px; height: 15px; " '.$PrevMode.'></span></td>';
	else
		echo'<td align="center">&nbsp;</td>';
			
	if ($FC_CashRegScrollPrint == 1)
	    echo'<td align="center"><span class="style5"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$Sale_ID.', \''.$ReportName.'\');"  title="'.$PrevHint.'"  style="width: 15px; height: 15px; " '.$PrevMode.'></span></td>';
	else
		echo'<td align="center">&nbsp;</td>';
		
	if ($FC_CashRegisterMod == 1)	
		echo'<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord('.$Sale_ID.');" title="'.$EditHint.'" '.$EditMode.' ></span></td>';
	else
		echo'<td align="center">&nbsp;</td>';
		
	if ($FC_CashRegisterDel == 1)
		echo'<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$Sale_ID.', \''.$Sale_Reference.'\');" title="'.$DelHint.'" '.$DelMode.' ></span></td>';
	else
		echo'<td align="center">&nbsp;</td>';
	*/	
	echo'	<td align="left"><span class="style5">'.$Sale_Reference.'</span></td>
			<td align="center"><span class="style5">'.$Sale_Date.'</span></td>
			<td align="center"><span class="style5">'.$RetailType.'</span></td>
			<td align="center"><span class="style5">'.$Projectdt_Name.'</span></td>
			<td align="left"><span class="style5">'.$CustomerName.'</span></td>';
			
	if ($Module_RestaurantDelivery == 1) {
		echo'<td align="center"><span class="style5">'.$SaleStatusRemark.'</span></td>';
	}


	if ($FC_CashRegisterCredit == 1) {
		echo'<td align="center"><span class="style5">'.$Sales_CreditAcc.'</span></td>
			 <td align="left"><span class="style5">'.$Sales_CreditName.'</span></td>';
	}
	
	echo'	<td align="center"><span class="style5">'.$CashRegisterName.'</span></td>
			<td align="center"><span class="style5">'.$Currency_code.'</span></td>
			<td align="right"><span class="style5">'.number_format($NetTotal, $DecimalBase).'</span></td>
		</tr>';
}
							

echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";					
		
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:1040px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&nbsp;</td><td align='right' style='width:74px;border-right:0px solid #000000; ' valign='top'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='74px' align='left' valign='top'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='380px'>&nbsp;";
		
		//include("toolbar.php");	 
		
		echo"</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr>";

echo"</td></tr></table>";
	


          

		  

print <<<HERE

</form>
</body>

HERE;

?>