<?php

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


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

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

$fromdate   =$_POST["fromdate"];
$todate     =$_POST["todate"];

if ($fromdate == '')
	$fromdate = date('01/m/Y');

if ($todate == '')
	$todate =  date('d/m/Y');

$querylog=" select ifNull(FI_SalesDel,0) As FI_SalesDel, ifNull(FI_SalesMod,0) As FI_SalesMod, ".
          " ifNull(login_belongstogrp,0) As FI_BelongsToGrp ,ifNull(FI_SalesWithPrice,0) As FI_SalesWithPrice, ifNull(ShowTransaction,0) As ShowTransaction ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ".
			 " ifNull(Module_ProjectParent,0) As Module_ProjectParent, ifNull(Module_CostCenterParent,0) As Module_CostCenterParent, ".	
             " ifNull(Module_PrintWaybill,0) As Module_PrintWaybill, ifNull(Module_PostToJournal,0) As Module_PostToJournal, Module_BatchNo, Module_ExpireDate 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
$SaDelMode  = 1;
$SaEditMode = 1;
$SaPrevMode = 1;

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



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

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

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays,FI_login_ViewDocument as FI_login_ViewDocument ".
              " From ".$HTTP_SESSION_VARS['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$HTTP_SESSION_VARS['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;	

$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on (($Module_ProjectParent=1 and project.project_code like concat(ifnull(loginstk.project_code,project.project_code),'%')) or ($Module_ProjectParent=0 and project.project_code = loginstk.project_code )) ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " and (loginstk.project_code is not null and loginstk.project_code != '') ".
			  " order by project.project_code";
$projectresult = mysql_query($queryproject,$connection) or die(mysql_error());
$projectexist  = mysql_num_rows($projectresult);		  

$querycostcent=" select distinct costcent.costcent_code, costcent_name ".
               " from costcent left join ".$HTTP_SESSION_VARS['dbLabel'].".loginstk on (($Module_CostCenterParent=1 and costcent.costcent_code like concat(ifnull(loginstk.costcent_code,costcent.costcent_code),'%')) or ($Module_CostCenterParent=0 and costcent.costcent_code = loginstk.costcent_code )) ".
			   " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$HTTP_SESSION_VARS['dbLabel'].".ledgerstk  ".
			  " where user_account='".$HTTP_SESSION_VARS['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];
if (empty($ledgerinclude)) $ledgerinclude = 0;

//Get Default Project
$querydflproject="select project_code as defaultproject from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_project=1 ";
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
	
//Get Default Coscenter
$querydflcostcent="select costcent_code as defaultcostcent from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((!isset($PProjectCode)) && $Module_Project == 1)
	$PProjectCode = $defaultproject;
if ((!isset($PCostCentCode)) && $Module_CostCenter == 1)	
	$PCostCentCode = $defaultcostcent;	

if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 2;

if ($startrange=='')  $startrange = 0;
if ($totalrows=='')   $totalrows = 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" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Search Sales</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.search.keyword.value='';
  document.search.PProjectCode.value='';
  document.search.PCostCentCode.value='';
  document.search.fromdate.value = '';
  document.search.todate.value = '';

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

}

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

function SetNextPage(startrange,perpage){ 

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

function SetPrvPage(startrange,perpage){

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


function UploadDocuments(vform,txtPorId){



	win = window.open('UploadDocuments.php?vform='+vform+'&prqstid='+txtPorId +'&readonly=readonly', 'Upload', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=990,height=600,top=60,left=100');		
	  win.creator = self;
	  win.focus();	 
  
  }


function DeleteRecord(SaId, SaRef){

	if (SaId != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+SaRef+" ?") )
		{
			document.search.action="Form_Sales_Actions.php?mainmode=delete&SSaId="+SaId;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(SaId){
		
	document.search.action="Form_Sales_Actions.php?mainmode=search&SSaId="+SaId;
	document.search.submit();
	
	document.search.PMode.value=1;
	
	opener.document.sales.txtSaId.value=SaId;
	opener.document.sales.mainaction.value='edit';
	opener.document.sales.actionmode.value='';
	
	//opener.document.sales.action='Form_Sales.php';
	//opener.document.sales.submit();
	
	
	//self.close();
	

}

function ShowHideDetails(SaId, Option, startrange) {
	/*if (Option == 'S') {
		document.search.SelectedID.value = SaId;
		document.search.action='Form_SalesSearch.php?startrange='+startrange+'&SelectedID='+SaId;
	}
	else if (Option == 'H') {
		document.search.SelectedID.value = '';
		document.search.action='Form_SalesSearch.php?startrange='+startrange;
	}	
	
	document.search.submit();*/
	
	vSlideDivId = '#displaydetails'+SaId;
	$(vSlideDivId).slideToggle();
}


function PrintPreview(SaId,MultiLangPrint,exp)
{
	var str = '';
	
	var vLetterHead = 0;
	var option_to_print = document.search.option_to_print.value;
	var option_to_print2 = document.search.option_to_print2.value;
	if (MultiLangPrint != 1) {
		if (confirm ( "Do you want to print with Letter Head?") )
		{
			vLetterHead = 1;
		}
		
		str = 'Form_SalesPreview.php?SalesId='+SaId+'&LetterHead='+vLetterHead+'&option_to_print='+option_to_print+'&option_to_print2='+option_to_print2+'&exp='+exp;
		window.open(str);
	} else {
		document.search.SalesId.value = SaId;
		loadPopupBox();
	}
}
function SendEmail(PId, PRef){
	

	document.search.action='sendMailAttached.php?vform=Sales&SId='+PId;
	document.search.submit();

}
function PrintPreviewWaybill(SaId)
{
	var str = '';
	
	str = 'Form_SalesWaybillPreview.php?SalesId='+SaId;
	window.open(str);
	
	//self.close();

}

function unloadPopupBox() {    // TO Unload the Popupbox
	$("#PrintOptions").fadeOut("fast");
	$("#backgroundPopup").fadeOut("fast");
    $("#backgroundPopup").css({ // this is just for style        
        "opacity": "1"  
    }); 
}    
        
function loadPopupBox() {    // To Load the Popupbox
    XPos = (screen.availHeight - 350) / 2;
	YPos = (screen.availWidth - 400) / 2;
	
    $("#PrintOptions").fadeIn("fast");
	$("#backgroundPopup").fadeIn("slow");
    $("#backgroundPopup").css({ // this is just for style
        "-ms-filter": "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)",
        "filter":"alpha(opacity=50)",
		"opacity": "0.5"
    });
	$("#PrintOptions").css({ // this is just for style
		"top": XPos,
		"left": YPos
    });	
} 

function PrintPreview1()
{
	var str = '';
	
	var vLetterHead = 0;
	var SaId = document.search.SalesId.value;
	
	for (var i=0; i < document.search.lang.length; i++)
	{
		if (document.search.lang[i].checked)
		{
			var vLang = document.search.lang[i].value;
		}
	}
	
	if (confirm ( "Do you want to print with Letter Head?") )
	{
		vLetterHead = 1;
	}
		
	str = 'Form_SalesPreview.php?SalesId='+SaId+'&LetterHead='+vLetterHead+'&PLang='+vLang;
	window.open(str);
	
}

function ChangeLocation()
{
    if (document.search.PMode.value == 1) {
		opener.document.sales.message.value = document.search.message.value;
		opener.document.sales.action='Form_Sales.php';
		opener.document.sales.submit();
		self.close();
	}	
}

function showlistProject(str){

  window.open('List_SorProject.php?frm=6&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistCostcenter(str){

  window.open('List_SorCostcenter.php?frm=6&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

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

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

}

function SelectSearchBy()
{	

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

}

function SubmitForm()
{	

  //document.search.keyword.value='';
  document.search.action='Form_SalesSearch.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 RefreshPage()
{
   document.search.action='Form_SalesSearch.php?startrange=0';
  document.search.submit();

}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_SalesSearch.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_SalesSearch.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	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;}

</style>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action="">
<input type='hidden' name='SalesId' id='SalesId' value=''>
HERE;

echo "<input type='hidden' name='message' value='".htmlspecialchars($message, ENT_QUOTES)."'>";


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

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
$querysearch=" Select SQL_CALC_FOUND_ROWS Sales.Sale_ID, Sales.Sale_Reference, Date_Format(Sales.Sale_Date,'%d-%m-%Y') As Sale_Date, ".
             " Sales.Client_Code As ClientCode, Clients.Ledger_name As ClientName, Sales.Currency_code, ".
			 " Project.Project_Name As ProjectName, Costcent.Costcent_name As CostcentName, Salesmen.Ledger_Name As SalesmanName, ".
			 " Round((ifNull(Sales.Sale_Total,0) + ifNull(Sales.Sale_VAT,0) - ifNull(Sales.Sale_DiscountAmount,0) + ifNull(Sales.Sale_TransportAmount,0) + ".
			 "        ifNull(Sales.Sale_BonusAmount,0) + (ifNull(Sales.Sale_Tax1,0) + ifNull(Sales.Sale_Tax2,0) + ifNull(Sales.Sale_Tax3,0) + ifNull(Sales.Sale_Tax4,0) + ifNull(Sales.Sale_Tax5,0))), ".$DecimalBase.") As NetTotal,  ".
			 " ifNull(Temp_Sales.Sale_Id, -1) As TempSalesId, Journal.Journal_folio, Journal.Journal_refgovernment, ".
			 " Sales.Sorder_Reference, Sales.Sale_RelatedID,Sales.Sale_Remark  ".
			 " From Sales Left Join Clients on Sales.Client_Code = Clients.Ledger_Number ".
			 " Left Join Project on Sales.Project_Code = Project.Project_Code ".
			 " Left Join Costcent on Sales.Costcenter_code = Costcent.Costcent_code ".
			 " Left Join Salesmen on Sales.Salesman_Code = Salesmen.Ledger_Number ".
			 " Left Join Journal on Sales.Journal_ID = Journal.Journal_ID ".
			 " Left Join Temp_Sales on Temp_Sales.Sale_ID = Sales.Sale_ID ";
			 
if ($projectexist >	0)
  $querysearch= $querysearch." Inner join ( select distinct project_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "			    where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "              and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and Sales.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and Sales.project_code = Q1.project_code )) ";
							 
if ($costcentexist > 0)
  $querysearch= $querysearch." Inner join ( select distinct costcent_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk ".
							 "		   	    where loginstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
							 "              and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and Sales.costcenter_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and Sales.costcenter_code = Q2.costcent_code )) ";

							 
if($ledgerinclude== 1 && $ledgerexist > 0) {
	$querysearch = $querysearch." And Exists ( select 1 from ".$HTTP_SESSION_VARS['dbLabel'].".ledgerstk ".
								"              where ledgerstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
								"              and (ledger_number is not null and ledger_number != '') ".
								"              and Sales.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"            ) ";
}
if($ledgerinclude== 0 && $ledgerexist > 0){
	$querysearch = $querysearch." And Not Exists ( select 1 from ".$HTTP_SESSION_VARS['dbLabel'].".ledgerstk ".
								"                  where ledgerstk.user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
								"          	       and (ledger_number is not null and ledger_number != '') ".
								"                  and Sales.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"                ) ";	
}
							 
$querysearch= $querysearch.											 
			 " Where (($searchby = 2 and Sales.Sale_Reference Like Concat(ifNull($PRef, Sales.Sale_Reference),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(Sales.Sale_Date,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(Sales.Sale_Date,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 3 and (Sales.Client_Code Like Concat(ifNull($PRef, Sales.Client_Code),'%') or Clients.Ledger_name Like Concat('%', ifNull($PRef, Clients.Ledger_name),'%')) ) Or  ".
			 "        ($searchby = 4 and (Sales.Salesman_Code Like Concat(ifNull($PRef, Sales.Salesman_Code),'%') or Salesmen.Ledger_Name Like Concat('%', ifNull($PRef, Salesmen.Ledger_Name),'%')) ) Or  ".
			 "        ($searchby = 5 and Journal.Journal_folio Like Concat(ifNull($PRef, Journal.Journal_folio),'%')) Or ".
			 "        ($searchby = 6 and Journal.Journal_refgovernment Like Concat(ifNull($PRef, Journal.Journal_refgovernment),'%')) Or  ".
			 "        ($searchby = 7 and Sales.Sorder_Reference Like Concat( ifNull($PRef, Sales.Sorder_Reference),'%') Or Sales.Sale_SorderReferences Like Concat( '%', ifNull($PRef, Sales.Sale_SorderReferences),'%')) Or  ".
			 "        ($searchby = 8 and Sales.Sale_RelatedID Like Concat( ifNull($PRef, Sales.Sale_RelatedID),'%') Or Sales.Sale_DeliveryReferences Like Concat( '%', ifNull($PRef, Sales.Sale_DeliveryReferences),'%')) Or  ".
			 "        ($searchby = 9 and Sales.Sale_Remark Like Concat( '%',ifNull($PRef, Sales.Sale_Remark),'%')) Or ".
			 "        ($searchby = 10 and Round(ifNull(Sales.Sale_Total,0) + ifNull(Sales.Sale_VAT,0) - ifNull(Sales.Sale_DiscountAmount,0) + ifNull(Sales.Sale_TransportAmount,0) + ".
			 "     								ifNull(Sales.Sale_BonusAmount,0) + (ifNull(Sales.Sale_Tax1,0) + ifNull(Sales.Sale_Tax2,0) + ifNull(Sales.Sale_Tax3,0) + ifNull(Sales.Sale_Tax4,0) + ifNull(Sales.Sale_Tax5,0)), 2) ".
			 " 				Like Concat('%', ifNull($PRef, Round(ifNull(Sales.Sale_Total,0) + ifNull(Sales.Sale_VAT,0) - ifNull(Sales.Sale_DiscountAmount,0) + ifNull(Sales.Sale_TransportAmount,0) + ".
			 "        										 ifNull(Sales.Sale_BonusAmount,0) + (ifNull(Sales.Sale_Tax1,0) + ifNull(Sales.Sale_Tax2,0) + ifNull(Sales.Sale_Tax3,0) + ifNull(Sales.Sale_Tax4,0) + ifNull(Sales.Sale_Tax5,0)), 2)),'%')) ) ".
			 " And Sales.Journal_Type = 'SI' ".
			 " And (($rowsperiod = 0 and 1 = 1) Or ($rowsperiod > 0 and (($LockViewPeriod = 1 and  Date_Format(Sales.Sale_Date,'%Y-%m-%d') >= Date_Format(ifNull('".$ViewPeriodDate."',Sales.Sale_Date),'%Y-%m-%d')) Or ($LockViewPeriod = 0 and 1=1 ))   ".
             "                                                      and (($ViewLockDaily = 1 and DateDiff(Date_Format(Now(),'%Y-%m-%d'),Date_Format(Sales.Sale_Date,'%Y-%m-%d')) <= $ViewNumberOfDays) Or ($ViewLockDaily = 0 and  1 = 1 ))) ) ".
			 " And ifNull(Sales.Sales_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
			 
if ($ShowTransaction != 1)
	$querysearch= $querysearch." And Upper(Sales.User_Account) = Upper('".$HTTP_SESSION_VARS['useraccount']."') ";
			 
if ($PProjectCode != '')
	$querysearch= $querysearch." And Sales.project_code like concat('".$PProjectCode."','%') ";
if ($PCostCentCode != '')
	$querysearch= $querysearch." And Sales.Costcenter_code like concat('".$PCostCentCode."','%') ";	

if ($fromdate != '' && $todate !='' )
	$querysearch = $querysearch." And Date_Format(Sales.Sale_Date,'%Y-%m-%d') between '".explodeword($fromdate)."' and '".explodeword($todate)."' ";
			 
			 
if ($orderby == 1)
	$querysearch= $querysearch." Order By Sales.Sale_Date Desc, Sales.Sale_Reference Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By Sales.Sale_Reference Desc ";	
			 
//echo $querysearch;exit;

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

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
//$totalrows = mysql_num_rows($resultsearch);
$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:1200px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Sales
			</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'>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=65>
	  <col width=70>
	  <col width=60>
	  <col width=60>
	  <col width=50>
	  <col width=50>
	  <col width=75>
	  <col width=170>
	  <col width=110>
	  <col width=100>
	  <col width=100>
	  <col width=55>
	  <col width=90>
	   <col width=250>
		     

	  <tr class='Search-header-bg'>";
	  
if ($Module_Project == 1) {
	echo" <td align='left' colspan='4'><span class='style4'>Project</span></td>
	      <td align='left' colspan='3'><span class='style4'>
		    <input type='text' name='PProjectCode' id='PProjectCode' value='$PProjectCode' size='15' maxlength='15' /></span>
		    <label><span class='style4'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(1); '  /></span></label></td>";
} else {
	echo" <td align='left' colspan='7'><input type='hidden' name='PProjectCode' id='PProjectCode' value='$PProjectCode' /></td>";

}			

if ($Module_CostCenter == 1) {		  
	echo" <td align='left' colspan='2'><span class='style4'>Costcenter</span></td>		
		  <td align='left' colspan='3'><span class='style4'>
		    <input type='text' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' size='15' maxlength='15' /></span>
		    <label><span class='style4'><img src='img/btnopen.jpg' title=' Select Costcenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\"  onClick='showlistCostcenter(1); ' /></span></label></td>";
} else {
	echo" <td align='left' colspan='5'><input type='hidden' name='PCostCentCode' id='PCostCentCode' value='$PCostCentCode' /></td>";

}	  

echo" <td align='right' colspan=2 ><span class='style4'>Order By</span></td>		
		  <td align='left' ><span class='style4'><select name='orderby' id='orderby' value='$orderby' onchange='SelectOrderBy();'>";
		
		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 colspan='3'>&nbsp;</td>";			 
		  
echo" </tr>";
echo" <tr bgcolor='#DDDDDD'>	  
	  
	    <td align='left' colspan='4'><span class='style4'>Search By</span></td>
		<td align='left' colspan='15'><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';
			else if ($searchby==6)	$s26 = 'selected';	
			else if ($searchby==7)	$s27 = 'selected';
			else if ($searchby==8)	$s28 = 'selected';	
			else if ($searchby==9)	$s29 = 'selected';	
			else if ($searchby==10)	$s31 = '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'>Salesman</option>";
				 
				 if ($Module_PostToJournal == 1) {
					echo" <option ".$s25." value='5'>Folio</option>
						  <option ".$s26." value='6'>Jv Ref.</option>";
				}		  
				
			echo"<option ".$s27." value='7'>Sorder Ref.</option>
				 <option ".$s28." value='8'>Delivery Ref.</option>
				 <option ".$s29." value='9'>Remark.</option>
				 <option ".$s31." value='10'>Amount</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>";
			if($Module_BatchNo == 1)
			{
				echo "<input type='checkbox' name='option_to_print' onclick=\"if(document.search.option_to_print.checked) { document.search.option_to_print.value='batch' } else { document.search.option_to_print.value='' }\" value=''> <b>Print Batch No.</b>";
			}
			else
			{
				echo "<input type='hidden' name='option_to_print' value=''>";
			}
			if($Module_ExpireDate == 1)
			{
				echo "<input type='checkbox' name='option_to_print2' onclick=\"if(document.search.option_to_print2.checked) { document.search.option_to_print2.value='expirydate' } else { document.search.option_to_print2.value='' }\" value=''> <b>Print Expiry Date</b>";
			}
			else
			{
				echo "<input type='hidden' name='option_to_print2' value=''>";
			}
			echo"</td>
		";
		
echo" </tr>
<tr class='Search-header-bg'>
<td colspan = '19'><span class='style4'>From&nbsp;</span><input type='text' name='fromdate' id='fromdate' value='$fromdate' maxlength='10' size='9'>
		<A HREF=\"#\" onClick=\"displayCalendar(document.search.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
&nbsp; &nbsp;<span class='style4'>To&nbsp;</span><input type='text' name='todate' id='todate' value='$todate' maxlength='10' size='9'>
		<A HREF=\"#\" onClick=\"displayCalendar(document.search.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span></td>
</tr>
</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:1025px;'>
		<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();\">";
              
            $perpages = $config['trowlimit'];
			foreach ($perpages as $val){
			echo" <option value = '$val' "; if ($perpage ==$val) echo "selected"; 
			echo" >$val</option> ";
			}
	echo'	
				 </select></span></td>
			<td  style="width:74px;border-right:1px 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"&nbsp;&nbsp;<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"&nbsp;&nbsp;<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"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";			

	if($message!=''){
		echo"	  <tr class='Search-header-bg'>
		<td height='15' align='center' colspan='8' class='searchmessage'>".$message."</td>
		</tr>";
	}
	
	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:1200px;'>
			<table border='0'  cellpadding='0' cellspacing='1' width='1465px'>
			<col width=20>
			<col width=20>
			<col width=20>
			<col width=20>
			<col width=20>
			  <col width=20>
			  <col width=20>
			  <col width=20>
			  <col width=65>
			  <col width=90>
			  <col width=60>
			  <col width=60>
			  <col width=50>
			  <col width=50>
			  <col width=75>
			  <col width=170>
			  <col width=110>
			  <col width=100>
			  <col width=100>
			  <col width=55>
			  <col width=90>
			  <col width=250>
			
			
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='19'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr class='Search-Label-bg'>
	  <td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
	  <td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
	  <td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
	  <td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
	  <td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<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'>Sorder Ref. </span></td>
		<td  height='25' align='center'><span class='style4'>Delivery Ref. </span></td>";
		
if ($Module_PostToJournal == 1) {
	echo" <td  height='25' align='center'><span class='style4'>Folio </span></td>
		  <td  height='25' align='center'><span class='style4'>JV Ref. </span></td>";
} else {
	echo" <td  height='25' colspan='2'>&nbsp;</td>";
}
	  
echo"	<td  height='25' align='center'><span class='style4'>Client</span></td>
		<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style4'>Project</span></td>
		<td  height='25' align='center'><span class='style4'>CostCenter</span></td>
		<td  height='25' align='center'><span class='style4'>Salesman</span></td>
		<td  height='25' align='center'><span class='style4'>Currency</span></td>
		<td  height='25' align='center'><span class='style4'>Total</span></td>
		<td  height='25' align='center'><span class='style4'>Remark</span></td>
	  </tr>";
		

	

$i = 1;

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."'";	
		
		

	$PrevHint   = 'Preview Invoice';
	$PrevHint1  = 'Preview Waybill';
	$EditHint   = 'Edit Record';
	$DelHint    = 'Delete Record';
	
	if ($TempSalesId != -1) {$PrevHint=$PrevHint1=$EditHint=$DelHint = 'Exists in Pending';}
	
    if ($FI_SalesMod == 0)	$SaEditMode = 0; 
	else if ($FI_SalesMod == 1 && $TempSalesId != -1) $SaEditMode =0;
	else if ($FI_SalesMod == 1 && $TempSalesId == -1) $SaEditMode =1;
	
	if ($FI_SalesDel == 0)	$SaDelMode =0; 
	else if ($FI_SalesDel == 1 && $TempSalesId != -1) $SaDelMode = 0;
	else if ($FI_SalesDel == 1 && $TempSalesId == -1) $SaDelMode = 1;
	
	if ($TempSalesId != -1) $SaPrevMode = 0; else $SaPrevMode = 1;		
			
		   
	echo'<tr '.$Color.'>';
		
	echo' <td align="center"><span class="style5"><img src="img/email.png" onMouseOver="style.cursor=\'hand\'" onclick="SendEmail('.$Sale_ID.', \''.$Sale_Reference.'\');" title="Send email" style="width:17px; height:15px; "></span></td>';

	if($SaEditMode==1)
	    echo'<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord('.$Sale_ID.');" title="'.$EditHint.'" style="width: 20px; height: 20px; "></span></td>';
	else 
		echo'<td align="center">&nbsp;</td>';		
		
	if($SaDelMode==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.'"  style="width: 20px; height: 20px; "></span></td>';
	else 
		echo'<td align="center">&nbsp;</td>';		
		
	if($SaPrevMode==1 )	{
		echo'<td align="center"><span class="style5"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$Sale_ID.','.$Setup_MultiLangPrint.',0);"  title="'.$PrevHint.'" style="width: 15px; height: 15px; "></span></td>';
		echo'<td align="center"><span class="style5"><img src="img/excelbtn.png" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreview('.$Sale_ID.','.$Setup_MultiLangPrint.',1);"  title="Exp Excel" style="width: 15px; height: 15px; " ></span></td>';
	}else{ 
		echo'<td align="center">&nbsp;</td>';	
		echo'<td align="center">&nbsp;</td>';	
	}	
		
	if ($Module_PrintWaybill == 1 && $SaPrevMode==1)	
		echo'<td align="center"><span class="style5"><img src="img/mag.jpg" onMouseOver="style.cursor=\'hand\'"  onclick="PrintPreviewWaybill('.$Sale_ID.');"  title="'.$PrevHint1.'" style="width: 15px; height: 15px; " ></span></td>';
	else
		echo'<td align="center">&nbsp;</td>';
			
	echo'<td align="center"><span class="style5"><img src="img/details.gif" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$Sale_ID.', \''.'S'.'\','.$startrange.');" title="Show Details" style="width:15px; height:15px; "></span></td>';
	if($FI_login_ViewDocument==1){
		echo'   <td align="center"><span class="style5"><img src="img/viewdoc.png" onMouseOver="style.cursor=\'hand\'"  onclick="UploadDocuments(\''.'Sales'.'\','.$Sale_ID.');"  title="Show Document" style="width: 18px; height: 15px; " ></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="left"><span class="style5">'.$Sorder_Reference.'</span></td>
			<td align="left"><span class="style5">'.$Sale_RelatedID.'</span></td>';
			
	if ($Module_PostToJournal == 1) {		
		echo' <td align="center"><span class="style5">'.$Journal_folio.'</span></td>
			  <td align="center"><span class="style5">'.$Journal_refgovernment.'</span></td>';
	} else {
		echo' <td align="center" colspan="2">&nbsp;</td>';
	}
	
	echo'	<td align="center"><span class="style5">'.$ClientCode.'</span></td>
			<td align="left"><span class="style5">'.$ClientName.'</span></td>
			<td align="left"><span class="style5">'.$ProjectName.'</span></td>
			<td align="left"><span class="style5">'.$CostcentName.'</span></td>
			<td align="left"><span class="style5">'.$SalesmanName.'</span></td>
			<td align="center"><span class="style5">'.$Currency_code.'</span></td>
			<td align="right"><span class="style5">'.number_format($NetTotal, $DecimalBase).'</span></td>
			<td align="left"><span class="style5">'.$Sale_Remark.'</span></td>
		</tr>';
		
					 
	$sub_query = " Select SalesDt.SaleDt_ID, SalesDt.Item_CodePacked, SalesDt.Item_code, Item_Description, SalesDt.Warehouse_Code, ".
				 " Item_UnitSalesDesc, SaleDt_Quantity, Round(SaleDt_Price, ".$DecimalBase."), Round(SaleDt_DiscountPer, ".$DecimalBase."),  ".
				 " Round(((SaleDt_Quantity*SaleDt_Price)-((SaleDt_Quantity*SaleDt_Price*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
				 " Round(SaleDt_PriceTTC, ".$DecimalBase."), ".
				 " Round(((SaleDt_Quantity*SaleDt_PriceTTC)-((SaleDt_Quantity*SaleDt_PriceTTC*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC ".
				 " From SalesDt Left Join Items on SalesDt.Item_code = Items.Item_code ".
				 " Where SalesDt.Sale_ID = ".$Sale_ID;

	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error($connection));	
	$sub_totalrows = mysql_num_rows($sub_result);
		
	if ($sub_totalrows > 0) {
		$SlideDivId = 'displaydetails'.$Sale_ID;
			
		echo "<tr ".$Color.">";
		echo "<td colspan='19' valign='top' align='left'><div id='$SlideDivId' class='div-search-details' style='display:none; margin-left:100px;'>";
		echo "<table border='0' style='table-layout:fixed; width:920px;' cellpadding='0' cellspacing='1'>";
		echo "<tr><td width='140px'><span class='style4'>Item Code</span></td>
				  <td width='240px'><span class='style4'>Description</span></td> 
				  <td width='100px'><span class='style4'>Warehouse</span></td> 
				  <td width='60px' align='center'><span class='style4'>Unit1</span></td>
				  <td width='80px' align='center'><span class='style4'>Qty</span></td>
				  <td width='100px' align='center'><span class='style4'>Price</span></td>
				  <td width='80px' align='center'><span class='style4'>Disc(%)</span></td>
				  <td width='120px' align='center'><span class='style4'>Total</span></td>
			 </tr>";
		while($sub_rows=mysql_fetch_array($sub_result)){
			echo "<tr><td align='left' valign='top' class='style9'>".$sub_rows[2]."</td>
					  <td align='left' valign='top' class='style9'>".$sub_rows[3]."</td>
					  <td align='left' valign='top' class='style9'>".$sub_rows[4]."</td>
					  <td align='center' valign='top' class='style9'>".$sub_rows[5]."</td>
					  <td align='right' valign='top' class='style9'>".$sub_rows[6]."</td>";
			
			if ($SalesTTC == 1) {
				echo" <td align='right' valign='top' class='style9'>".number_format($sub_rows[10],$DecimalBase)."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[8],$DecimalBase1)."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[11],$DecimalBase2)."</td>";
			} else {
				echo" <td align='right' valign='top' class='style9'>".number_format($sub_rows[7],$DecimalBase)."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[8],$DecimalBase1)."</td>
					  <td align='right' valign='top' class='style9'>".number_format($sub_rows[9],$DecimalBase2)."</td>";
			}
			
  			echo "</tr>";
				
		}
			
		echo "</table></div></td>";
		echo "</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:1025px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:280px;'>&nbsp;</td><td   style='border-right:1px 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"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='left'>";
		
		
		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"&nbsp;&nbsp;<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;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

echo"<div id='PrintOptions' style='position:absolute; display:none; z-index:9999999; width:200px; height:150px; border:2px solid #E6E6E6; background-color:#F7F7F7;'>
		<div style='position:relative; float:left; clear:both; margin:10px 0 0 10px;'>
			<input type='radio' name='lang' value='EN' checked>English<br>
			<input type='radio' name='lang' value='FR'>French<br>
			<input type='radio' name='lang' value='AR'>Arabic
		</div>	
		<div style='position:relative; float:left; clear:both; margin:50px 0 0 30px;'>
			<button type='button' id='btnPreview' name='btnPreview' onclick='PrintPreview1(); return false;' title='Preview'><span><span>Preview</span></span></button>
			<button type='button' id='btnExit' name='btnExit' onclick='unloadPopupBox();return false;' title='Close'><span><span>Close</span></span></button></td>
		</div>
	</div>
	<div id='backgroundPopup' style='position:absolute; display:none; z-index:1; width:100%; height:100%; top:0; left:0;'></div>";
          

		  

print <<<HERE

</form>
</body>

HERE;
function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}

?>