<?php
header("Content-type: text/html; charset=windows-1256");
session_start();

if (!function_exists('ConvertCurrency')) {
	function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
	{
		$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
		$result = selectData($connection, $query);
		//echo $query;
		Return	$result[0];

	}
}

if (!empty($_GET["action"])) {
	$dftorderby   = $_GET["dftorderby"];
	$action 	  = $_GET["action"];
	$orderby 	  = $_GET["orderby"];
	$morelines	  = $_GET["morelines"];
	$startrange   = $_GET["startrange"];
	$perpage 	  = $_GET["perpage"];
	$totalrows    = $_GET["totalrows"];
	$direction    = $_GET["direction"];
	$filepath     = $_GET["filepath"];
	$transid      = $_GET["transid"];
	$transdtid    = $_GET["transdtid"];
	$form      	  = $_GET["form"];
	
	
	
	include("config.inc.php");
	include("Functions_Sales.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");
	
	//echo $_SESSION['dbName']."<br>";
	if ($form == 'SO') {
		ShowSorderDtGrid($connection,$form,$transid,$_SESSION['gso_parameters'],$_SESSION['gso_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'SO-TOT') {
		ShowSorderTotals($connection,$transid,$_SESSION['gso_parameters'],$_SESSION['gso_arrayaccess']);
	}
	else if ($form == 'DI') {
		ShowDeliveryDtGrid($connection,$form,$transid,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'DI-TOT') {
		ShowDeliveryTotals($connection,$transid,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess']);
	}
	else if ($form == 'DR') {
		ShowReturnDeliveryDtGrid($connection,$form,$transid,$_SESSION['grdl_parameters'],$_SESSION['grdl_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'DR-TOT') {
		ShowReturnDeliveryTotals($connection,$transid,$_SESSION['grdl_parameters'],$_SESSION['grdl_arrayaccess']);
	}
	else if ($form == 'SI') {
		ShowSalesDtGrid($connection,$form,$transid,$_SESSION['gsa_parameters'],$_SESSION['gsa_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'SI-TOT') {
		ShowSalesTotals($connection,$transid,$_SESSION['gsa_parameters'],$_SESSION['gsa_arrayaccess']);
	}
	else if ($form == 'SR') {
		ShowReturnSalesDtGrid($connection,$form,$transid,$_SESSION['grsa_parameters'],$_SESSION['grsa_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'SR-TOT') {
		ShowReturnSalesTotals($connection,$transid,$_SESSION['grsa_parameters'],$_SESSION['grsa_arrayaccess']);
	}
	else if ($form == 'PO') {
		ShowPurchaseOrderDtGrid($connection,$form,$transid,$_SESSION['gpor_parameters'],$_SESSION['gpor_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'PO-TOT') {
		ShowPurchaseOrderTotals($connection,$transid,$_SESSION['gpor_parameters'],$_SESSION['gpor_arrayaccess']);
	}
	else if ($form == 'PI') {
		ShowPurchaseDtGrid($connection,$form,$transid,$_SESSION['gpur_parameters'],$_SESSION['gpur_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'PI-TOT') {
		ShowPurchaseTotals($connection,$transid,$_SESSION['gpur_parameters'],$_SESSION['gpur_arrayaccess']);
	}
	else if ($form == 'PR') {
		ShowReturnPurchaseDtGrid($connection,$form,$transid,$_SESSION['gpr_parameters'],$_SESSION['gpr_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}	
	else if ($form == 'PR-TOT') {
		ShowReturnPurchaseTotals($connection,$transid,$_SESSION['gpr_parameters'],$_SESSION['gpr_arrayaccess']);
	}
	else if ($form == 'GRN') {
		ShowGoodRecNoteDtGrid($connection,$form,$transid,$_SESSION['ggrn_parameters'],$_SESSION['ggrn_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'GRN-R') {
		ShowReturnGoodRecNoteDtGrid($connection,$form,$transid,$_SESSION['ggrnr_parameters'],$_SESSION['ggrnr_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'ASS') {
		ShowAssembleDtGrid($connection,$form,$transid,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'PreASS') {
		ShowPreAssembleDtGrid($connection,$form,$transid,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'ASS-TOT') {
		ShowAssembleTotals($connection,$transid,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess']);
	}
	else if ($form == 'PreASS-TOT') {
		ShowPreAssembleTotals($connection,$transid,$_SESSION['gass_parameters'],$_SESSION['gass_arrayaccess']);
	}
	else if ($form == 'PRD-TPL-1') {
		ShowProdTplDtGridFG($connection,$form,$transid,$_SESSION['gprdtpl_parameters'],$_SESSION['gprdtpl_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'PRD-TPL-2') {
		ShowProdTplDtGridRM($connection,$form,$transid,$_SESSION['gprdtpl_parameters'],$_SESSION['gprdtpl_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'PRD-1') {
		ShowProdDtGridFG($connection,$form,$transid,$_SESSION['gprd_parameters'],$_SESSION['gprd_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'PRD-2') {
		ShowProdDtGridRM($connection,$form,$transid,$_SESSION['gprd_parameters'],$_SESSION['gprd_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'PRD-SUMM') {
		ShowProdGridSummary($connection,$transid,$_SESSION['gprd_parameters'],$_SESSION['gprd_arrayaccess']);
	}	
	else if ($form == 'PRD-TOT') {
		ShowProductionTotals($connection,$transid,$_SESSION['gprd_parameters'],$_SESSION['gprd_arrayaccess']);
	}
	else if ($form == 'ADJ') {
		ShowAdjustmentDtGrid($connection,$form,$transid,$_SESSION['gadj_parameters'],$_SESSION['gadj_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	}
	else if ($form == 'ADJ-TOT') {
		ShowAdjustmentTotals($connection,$transid,$_SESSION['gadj_parameters'],$_SESSION['gadj_arrayaccess']);
	}
	else if ($form == 'JV') {
		ShowJournalDtGrid($connection,$form,$transid,$_SESSION['gjv_parameters'],$_SESSION['gjv_arrayaccess'],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);	
	}	
	else if ($form == 'JV-C') {
		ShowJournalDtGridChecks($connection,$form,$transid,$transdtid,$_SESSION['gjv_parameters'],$_SESSION['gjv_arrayaccess']);
	}	
	else if ($form == 'JV-B') {
		ShowJournalDtGridBills($connection,$form,$transid,$transdtid,$_SESSION['gjv_parameters'],$_SESSION['gjv_arrayaccess']);
	}
	else if (strpos($form,'JV-D') !== false) {
		$divtype = strtoupper(trim($form,'JV-D-')); 
		ShowJournalDtGridDivisions($connection,$form,$transid,$transdtid,$divtype,$_SESSION['gjv_parameters'],$_SESSION['gjv_arrayaccess']);	
	}	
	else if ($form == 'JV-TOT') {
		ShowJournalTotals($connection,$transid,$_SESSION['gjv_parameters'],$_SESSION['gjv_arrayaccess']);
	}
	else if ($form == 'TRS-1') {
		$actions = explode("-",$action);
		$type = $actions[1];
		
		$ParamSessionName  = 'gtrs_parameters'.$type;
		$AccessSessionName = 'gtrs_arrayaccess'.$type;
		ShowTransDtGrid($connection,$form,$transid,$_SESSION[$ParamSessionName],$_SESSION[$AccessSessionName],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);	
	}
	else if ($form == 'TRS-2') {
		$actions = explode("-",$action);
		$type = $actions[1];
		
		$ParamSessionName  = 'gtrs_parameters'.$type;
		$AccessSessionName = 'gtrs_arrayaccess'.$type;
		ShowTransDtGrid2($connection,$form,$transid,$_SESSION[$ParamSessionName],$_SESSION[$AccessSessionName],$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);	
	}
	else if ($form == 'TRS-TOT') {
		ShowTransTotals($connection,$transid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}
	else if (strpos($form,'TRS-D') !== false) {
		$divtype = strtoupper(trim($form,'TRS-D-')); 
		ShowTransDtGridDivisions($connection,$form,$transid,$transdtid,$divtype,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);	
	}
	else if (strpos($form,'TRS-2-D') !== false) {
		$divtype = strtoupper(trim($form,'TRS-2-D-')); 
		ShowTransDtGridDivisions2($connection,$form,$transid,$transdtid,$divtype,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);	
	}
	else if ($form == 'TRS-C') {
		ShowTransDtGridChecks($connection,$form,$transid,$transdtid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}
	else if ($form == 'TRS-2-C') {
		ShowTransDtGridChecks2($connection,$form,$transid,$transdtid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}	
	else if ($form == 'TRS-B') {
		ShowTransDtGridBills($connection,$form,$transid,$transdtid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}
	else if ($form == 'TRS-2-B') {
		ShowTransDtGridBills2($connection,$form,$transid,$transdtid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}
	else if ($form == 'TRS-I') {
		ShowTransDtGridInvoices($connection,$form,$transid,$transdtid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}
	else if ($form == 'TRS-2-I') {
		ShowTransDtGridInvoices2($connection,$form,$transid,$transdtid,$_SESSION['gtrs_parameters'],$_SESSION['gtrs_arrayaccess']);
	}
	
}


function ShowSorderDtGrid($connection,$form,$PSorId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$FI_SorderCanceledQty = $arrayparams['FI_SorderCanceledQty'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_AllowPrice = $arrayparams['Login_AllowPrice'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Allow_MinimumPrice = $arrayparams['Allow_MinimumPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Module_NegativeSorder = $arrayparams['Module_NegativeSorder'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_SorderCheckQtyOnPost = $arrayparams['Module_SorderCheckQtyOnPost'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$Module_ShowWarehouseSalesorder  = $arrayparams['Module_ShowWarehouseSalesorder'];
	
	
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         	= $arrayparams['txtDate'];
	$txtClientCode   	= $arrayparams['txtClientCode'];
	$txtClientDetail 	= $arrayparams['txtClientDetail'];
	$txtCurrCode     	= $arrayparams['txtCurrCode'];
	$txtClientPromotion = $arrayparams['txtClientPromotion'];
	
	if (empty($txtClientPromotion)) $txtClientPromotion = 0;
	
	if ($txtClientCode == '' && $PSorId != '') {
		$sub_query = "SELECT Client_Code FROM Temp_Sorder WHERE Sorder_ID = '".$PSorId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		$txtClientCode   = $sub_result[0];
	}
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode", "itemorderedqty", "cancelledqty");
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	array_push($columnsarray, 'price');
	array_push($columnsarray, 'sordtdiscount');
	array_push($columnsarray, 'sordttotal');
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "itemorderedqty", "sordtquantity", "cancelledqty", "unit2qty", "price", "sordtdiscount", "sordttotal", "projectcodedt", "costcentcodedt", "remarkdt", "sordtid", "existingqty", "bookingqty", "availableqty", "lastprice", "itemunit1", "itemunit2", "itemvat", "itemminprice", "itemnonstock", "itemzeroprice", "itemunitcoef");
	$orderbyarray = array("SorderDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "SorderDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:1400px;'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		  
		if ($Module_Pack == 1)
			echo"  <th width='150px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSorId."', 'datagrid', 1, '".$dftorderby."', 'COR','SorderDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='170px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='200px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		
		if($Module_ShowWarehouseSalesorder==1)
		echo"  <th width='120px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSorId."', 'datagrid', 1, '".$dftorderby."', 'COR','SorderDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		
		
		echo"  <th width='60px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='70px;' align='center'><span>Ordered Qty</span></th> ";	
		echo"  <th width='70px;' align='center'><span>Cancelled Qty</span></th>";	
		echo"  <th width='70px;' align='center'><span>Qty</span></th>";
			
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='60px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='75px;' align='center'><span>Unit2 Qty</span></th>";
		}
		  
		echo"  <th width='100px;' align='center'><span>".$PriceLabel."</span></th>";
		echo"  <th width='80px;' align='center'><span>Last Sell. Price</span></th> ";
		echo"  <th width='65px;' align='center'><span>Disc(%)</span></th>";
		echo"  <th width='100px;' align='center'><span>Total</span></th>";
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='170px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='170px;' align='center'><span>Costcenter</span></th>";	
		}
	
		echo"  <th width='250px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:1400px;'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSorId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			
			$query = " Select SorderDt.SorderDt_id, SorderDt.Item_CodePacked, SorderDt.Item_code, Item_Description, SorderDt.Warehouse_Code, ".
					 " SorderDt_Remark, SorderDt_Quantity, Round(SorderDt_Price, ".$DecimalBase."), SorderDt_PriceBase1, SroderDt_PriceBase2, ".
					 " Round(SorderDt_Discount, ".$DecimalBase."),  ".
					 " Round(((SorderDt_Quantity*SorderDt_Price)-((SorderDt_Quantity*SorderDt_Price*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(SorderDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((SorderDt_Quantity*SorderDt_PriceTTC)-((SorderDt_Quantity*SorderDt_PriceTTC*ifNull(SorderDt_Discount,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " SorderDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, SorderDt.Project_code, SorderDt.CostCent_Code, ifNull(SorderDt_oqty,0), ifNull(SorderDt_cqty,0) ".
					 " From Temp_SorderDt As SorderDt Left Join Items on SorderDt.Item_code = Items.Item_code ".
					 " Where SorderDt.Sorder_ID = ".$PSorId.
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'SO', '', '') QtyExists From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack      = $sub_result[0];
				$i_zeroprice = $sub_result[1];
				$i_unit1     = $sub_result[2];
				$i_unit2 	 = $sub_result[3]; 
				$i_unitcoef	 = $sub_result[4];
				$i_nonstock  = $sub_result[5];
				$i_minprice  = $sub_result[6];
				
				$sub_query = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$c_sp    = $sub_result[0];
				
				$txtItemMinPrice[$i]  = $i_minprice;
				$txtItemZeroPrice[$i] = $i_zeroprice;
				$txtItemNonStock[$i]  = $i_nonstock;
				$txtItemUnitCoef[$i]  = $i_unitcoef;
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SO', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SO', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SO', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;

				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(SorderDt_Quantity,0)),0) FROM Temp_Sorderdt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Sorder_ID = $PSorId And SorderDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_SO = $result[0];
				
				$Qry_QtyTransferInTemp = " Select ifNull(Sum(ifNull(TransferDt_Quantity,0)),0) FROM Temp_TransferDt ".
										 " WHERE Item_code = '".$rowsdt[2]."'  And  TransferDt_WarehouseFrom = '".$rowsdt[4]."' ";
				$result = selectData($connection, $Qry_QtyTransferInTemp);						 
				$QtyTemp_TR = $result[0];
					
				$min_available_qty = $min_available_qty - $QtyTemp_SO - $QtyTemp_TR;
				$min_exists_qty = $min_exists_qty;
				$min_book_qty = $min_book_qty + $QtyTemp_SO + $QtyTemp_TR;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;'>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSorId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				if ($Module_Pack == 1)
					echo "	<td width='150px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='170px;' align='left'>
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemMinPrice[$i]' id='itemminprice[$i]' value='$txtItemMinPrice[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemZeroPrice[$i]' id='itemzeroprice[$i]' value='$txtItemZeroPrice[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtSorDtId[$i]' id='sordtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv($i);\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."'); \" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv($i);\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='200px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				if($Module_ShowWarehouseSalesorder==1)		
				echo"  	<td width='120px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				else echo" <input type='hidden' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";		


				echo "	<td width='60px;' align='left'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				echo"	<td width='70px;' align='center'><input type='text' name='txtSorDtOQty[$i]' id='itemorderedqty[$i]' value='".$rowsdt[19]."' maxlength='20' onKeyup='CalcSorDtQty($i); CalcSorDtTotal($DecimalBase,$i);' onchange=\"CalcQty2($Module_Unit1Method,1,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:60px; height:20px;' ".$KeyDown." $ModeModifyQty></td>";
				echo"   <td width='70px;' align='center'><input type='text' name='txtSorDtCQty[$i]' id='cancelledqty[$i]' value='".$rowsdt[20]."' maxlength='20' onKeyup='CalcSorDtQty($i); CalcSorDtTotal($DecimalBase,$i);'  onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:60px; height:20px;' ".$KeyDown."  ></td>";
				echo"	<td width='70px;' align='center'><input type='text' name='txtSorDtQty[$i]' id='sordtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:60px; height:20px;'  readonly></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='60px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='75px;' align='center'><input type='text' name='txtSorDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:60px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSorDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($SalesTTC == 1) 
					echo"	<td width='100px;' align='center'><input type='text' name='txtSorDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSorDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
				else
					echo"	<td width='100px;' align='center'><input type='text' name='txtSorDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSorDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
					
				echo "	<td width='80px;' align='right'><span class='".$TextColor."'>&nbsp;</span></td>";
				echo"	<td width='65px;' align='center'><input type='text' name='txtSorDtDiscount[$i]' id='sordtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSorDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:60px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
				
						
				if ($SalesTTC == 1) 
					echo"   <td width='100px;' align='center'><input type='text' name='txtSorDtTotal[$i]' id='sordttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='100px;' align='center'><input type='text' name='txtSorDtTotal[$i]' id='sordttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' ".$KeyDown." ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='170px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='170px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				echo"	<td width='250px;'><input type='text' name='txtSorDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeSorder,$Login_NegativeQty,$Login_AllowPrice,$Module_SorderCheckQtyOnPost,$Allow_MinimumPrice,$Module_RestrictProjCost,$Module_RestrictCost,$txtClientDetail,$FI_SalesCheckSalesman); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeSorder,$Login_NegativeQty,$Login_AllowPrice,$Module_SorderCheckQtyOnPost,$Allow_MinimumPrice,$Module_RestrictProjCost,$Module_RestrictCost,$txtClientDetail,$FI_SalesCheckSalesman,$txtClientPromotion,$Module_ShowWarehouseSalesorder); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_Pack == 1)
				echo"  <td width='150px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='170px;' align='left'>
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemMinPrice[$i]' id='itemminprice[$i]' value='$txtItemMinPrice[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemZeroPrice[$i]' id='itemzeroprice[$i]' value='$txtItemZeroPrice[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtSorDtId[$i]' id='sordtid[$i]' value='$txtSorDtId[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv($i);\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";

						if ($Module_DoubleUnit == 1) {
							echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						}		
								
			echo"			</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv($i);\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='200px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			if($Module_ShowWarehouseSalesorder==1)		
				echo"  	<td width='120px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			else 
				echo" <input type='hidden' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";					
			
			echo"  <td width='60px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' readonly></td>";
			echo"	<td width='70px;' align='center'><input type='text' name='txtSorDtOQty[$i]' id='itemorderedqty[$i]' value='$txtSorDtOQty[$i]' maxlength='20' onKeyup='CalcSorDtQty($i); CalcSorDtTotal($DecimalBase,$i);' onchange=\"CalcQty2($Module_Unit1Method,1,$i); SetModRecord('ModRec_G1','$i');\"  class='".$ClassI."' style='font-size:12px; width:60px; height:20px;' ".$KeyDown." $ModeModifyQty></td>";
			echo"   <td width='70px;' align='center'><input type='text' name='txtSorDtCQty[$i]' id='cancelledqty[$i]' value='$txtSorDtCQty[$i]' maxlength='20' onKeyup='CalcSorDtQty($i); CalcSorDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\"  class='".$ClassI."' style='font-size:12px; width:60px; height:20px;' ".$KeyDown."  ></td>";
			echo"	<td width='70px;' align='center'><input type='text' name='txtSorDtQty[$i]' id='sordtquantity[$i]' value='$txtSorDtQty[$i]' maxlength='20' class='".$ClassI."'   style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:60px; height:20px;'  readonly></td>";
						
			  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='60px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:50px; height:20px;' readonly></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtSorDtQty2[$i]' id='unit2qty[$i]' value='$txtSorDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:60px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSorDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			  
			echo"	<td width='100px;' align='center'><input type='text' name='txtSorDtPrice[$i]' id='price[$i]' value='$txtSorDtPrice[$i]' maxlength='20' onKeyup='CalcSorDtTotal($DecimalBase,$i);' class='".$ClassI."'  style='text-align:right; font-size:12px; width:90px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
			echo"   <td width='80px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
					  
			echo"	<td width='65px;' align='center'><input type='text' name='txtSorDtDiscount[$i]' id='sordtdiscount[$i]' value='$txtSorDtDiscount[$i]' maxlength='20' onKeyup='CalcSorDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'   style='text-align:right; font-size:12px; width:60px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
			echo"   <td width='100px;' align='center'><input type='text' name='txtSorDtTotal[$i]' id='sordttotal[$i]' value='$txtSorDtTotal[$i]' maxlength='20' class='".$ClassI."'   style='text-align:right; font-size:12px; width:90px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
	
			if ($Module_ProjectDt == 1) {
				echo"<td width='170px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='170px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			echo"	<td width='250px;'><input type='text' name='txtSorDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSorDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeSorder,$Login_NegativeQty,$Login_AllowPrice,$Module_SorderCheckQtyOnPost,$Allow_MinimumPrice,$Module_RestrictProjCost,$Module_RestrictCost,$txtClientDetail,$FI_SalesCheckSalesman); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeSorder,$Login_NegativeQty,$Login_AllowPrice,$Module_SorderCheckQtyOnPost,$Allow_MinimumPrice,$Module_RestrictProjCost,$Module_RestrictCost,$txtClientDetail,$FI_SalesCheckSalesman,$txtClientPromotion,$Module_ShowWarehouseSalesorder); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSorId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowSorderTotals($connection,$PSorId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	
	$txtClientCode   = $arrayparams['txtClientCode'];
	
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
	
	if ($PSorId != "") {		 
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)), ".$DecimalBase."), Round(ifNull(Sorder_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)) + ifNull(Sorder_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Sorder_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Sorder_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Sorder_VatChckbx,0) As Sorder_VatChckbx, Round(ifNull(Sorder_VAT,0), ".$DecimalBase.") As Sorder_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Sorder_Taxable,0)*(1+$Setup_VatPercentage/100),ifNull(Sorder_Taxable,0)) + ifNull(Sorder_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Sorder_VAT,0)) + ifNull(Sorder_TransportAmount,0) - IfNull(Sorder_DiscountAmount,0) + (ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0))), ".$DecimalBase.") As Sorder_NetTotal,  ".
					 " Round((ifNull(Sorder_Tax1,0) + ifNull(Sorder_Tax2,0) + ifNull(Sorder_Tax3,0) + ifNull(Sorder_Tax4,0) + ifNull(Sorder_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Sorder_TransportTTC,0),ifNull(Sorder_TransportAmount,0)), ".$DecimalBase.") As TransportAmt ".
					 " From Temp_Sorder As Sorder ".
					 " Where Sorder_ID = ".$PSorId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtSorTaxable      = $sub_result[0];
		$txtSorNoneTaxable  = $sub_result[1];
		$txtSorGrossTotal   = $sub_result[2];
		$txtSorDiscount     = $sub_result[3];
		$txtSorDiscountAmt  = $sub_result[4];
		$txtSorVatChk       = $sub_result[5];
		$txtSorVat          = $sub_result[6];
		$txtSorNetTotal     = $sub_result[7];
		$txtSorTotalTaxes   = $sub_result[8];
		$txtSorTransportAmt = $sub_result[9];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtSorVatChk = $sub_result[0];
	}
	
	echo"<table border='0' style='table-layout:fixed;' cellspacing=0>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;' valign='top'>
		<table border='0' style='table-layout:fixed;' cellspacing=0 >
			<col width=100>
			<col width=140>";
			
	echo"	
				<tr><td valign='center' align='right' width='35%'><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' style='padding-top:1px;padding-bottom:1px;'><input type='text' id='taxable'  name='txtSorTaxable' value='$txtSorTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='35%'><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' style='padding-top:1px;padding-bottom:1px;'><input type='text' id='nontaxable' name='txtSorNoneTaxable' value='$txtSorNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='35%'><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' style='padding-top:1px;padding-bottom:1px;'><input type='text' id='grosstotal' name='txtSorGrossTotal' value='$txtSorGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  width='35%'><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' style='padding-top:1px;padding-bottom:1px;'><input type='text' id='sordiscount' name='txtSorDiscount' value='$txtSorDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcSorDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span>
						<input type='text' id='sordiscountamt' name='txtSorDiscountAmt' value='$txtSorDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcSorDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right'  ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sortransportamt' name='txtSorTransportAmt' value='$txtSorTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sortransportamt' name='txtSorTransportAmt' value='$txtSorTransportAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right' width='35%'  style='padding-top:1px;padding-bottom:1px;'><span class='style10'>
						<input type='hidden' name='txtSorVatChk' id='txtSorVatChk' value='$txtSorVatChk' size='1'/>
						<input name='chkSorVat' type='checkbox' id='chkSorVat' value='1' onClick='SetSorVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'"; 
						if($txtSorVatChk==1) echo " checked $AllowModifyVAT />";  else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left'  style='padding-top:1px;padding-bottom:1px;'>
						<input type='text' id='sorvat' name='txtSorVat' value='$txtSorVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' width='35%'><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' style='padding-top:1px;padding-bottom:1px;'><input type='text' id='tax' name='txtSorTotalTaxes' value='$txtSorTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtSorTotalTaxes' value='$txtSorTotalTaxes'>";
	}
					
	echo "		<tr><td valign='middle' align='right' width='35%'><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left'  style='padding-top:1px;padding-bottom:1px;'><input type='text' id='nettotal' name='txtSorNetTotal' value='$txtSorNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
					if($txtSorNetTotal<0) echo " color:#FF0000;";
					
					
					echo" ' readonly></td>
				</tr> 
				  </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}

function ShowDeliveryDtGrid($connection,$form,$PSaId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$FI_DeliverySalesPrice = $arrayparams['FI_DeliverySalesPrice'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Login_MinAllowCostPer = $arrayparams['Login_MinAllowCostPer'];
	$Login_MaxAllowCostPer = $arrayparams['Login_MaxAllowCostPer'];
	$Module_NegativeQuantity = $arrayparams['Module_NegativeQuantity'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$Module_DeliveryClosingQty = $arrayparams['Module_DeliveryClosingQty'];
	$Module_DeliveryWithSorder = $arrayparams['Module_DeliveryWithSorder'];
	$Sale_MaxExceedQty = $arrayparams['Sale_MaxExceedQty'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$txtCurrCode  = $arrayparams['txtCurrCode'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtClientCode   = $arrayparams['txtClientCode'];
	$txtClientDetail = $arrayparams['txtClientDetail'];
	
	/*
$sub_query = "SELECT currency_code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		if($txtCurrCode=='')
			$txtCurrCode   = $sub_result[0];
		//echo "sad".$txtCurrCode;
		*/
	
$sub_query = "SELECT currency_code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		//echo "Currency".$txtCurrCode;
		if($txtCurrCode=='')
			$txtCurrCode   = $sub_result[0];
	//echo "Currency".$txtCurrCode;
	if ($txtClientCode == '' && $PSaId != '') {
		$sub_query = "SELECT Client_Code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		//echo "<br><br>".$sub_query;
		$sub_result = selectData($connection, $sub_query);
		$txtClientCode   = $sub_result[0];
	}

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
	
	if ($Module_DeliveryClosingQty == 1) array_push($columnsarray, 'sadtcloseqty');
	array_push($columnsarray, 'sadtquantity');
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
		array_push($columnsarray, 'price');
		array_push($columnsarray, 'sadtdiscount');
		array_push($columnsarray, 'sadttotal');
	}	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "sorderref", "sorderqty", "sadtcloseqty", "sadtquantity", "unit2qty", "price", "sadtdiscount", "lastprice", "sadttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "sadtid", "existingqty", "bookingqty", "availableqty", "returnqty", "soldqty", "itemunit1", "itemunit2", "itemvat", "itemnonstock", "itemexpire", "itemunitcoef", "costprice", "sellprice");
	$orderbyarray = array("DeliveryDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "DeliveryDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1145;
	if ($Module_SerialNo == 1) $TableWidth += 25;
	if ($Module_Pack == 1)     $TableWidth += 125;
	if ($Module_DeliveryClosingQty == 1)     $TableWidth += 75;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1)     $TableWidth += 345;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.DeliveryDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
		echo"  <th width='75px;' align='center'><span>Order Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Order Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		
		if ($Module_DeliveryClosingQty == 1) {
			echo"  <th width='75px;' align='center'><span>Closing Qty</span></th> ";	
		}	
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
			echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
			echo"  <th width='85px;' align='center'><span>Last Sell. Price-Disc</span></th> ";
			echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
			echo"  <th width='105px;' align='center'><span>Total</span></th>";
		}	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='50px;' align='center'><span>Promo</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSaId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
					   
			$query = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, DeliveryDt.Item_code, Item_Description, DeliveryDt.Warehouse_Code, ".
					 " DeliveryDt_Remark, DeliveryDt_Quantity, Round(DeliveryDt_Price, ".$DecimalBase."), DeliveryDt_PriceBase1, DeliveryDt_PriceBase2, ".
					 " Round(DeliveryDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(DeliveryDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " DeliveryDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, DeliveryDt.Project_code, DeliveryDt.CostCent_Code,  ".
					 " SorderDt.SorderDt_Quantity, ifNull(item_withserialno,0) As ItemWithSerialNo, Sorder.Sorder_reference, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(DeliveryDt_ExpireDate,'%d/%m/%Y') As DeliveryDt_ExpireDate, DeliveryDt_BatchNo, DeliveryDt.DeliveryDt_ClosingQuantity, ifNull(DeliveryDt_Promotion,0), ".
					 " Round(ifNull(MeanCostCur(DeliveryDt.Item_code, DeliveryDt.Delivery_Date, 1, '".$Base1."', DeliveryDt.Currency_Code),0), ".$DecimalBase.") as ItemCost,DeliveryDt_heatNo ".
					 " From Temp_DeliveryDt As DeliveryDt ".
					 " Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on DeliveryDt.DeliveryDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'DI' ".
					 " And Temp_Purchasesserialno.DeliveryDt_ID Is Not Null ".
					 " Left Join SorderDt on SorderDt.SorderDt_ID = DeliveryDt.RelatedDt_Id  ".
					 " Left Join Sorder on Sorder.Sorder_ID = SorderDt.Sorder_ID  ".
					 " Where DeliveryDt.Delivery_ID = ".$PSaId.
					 " Group By  DeliveryDt.DeliveryDt_ID ".
					 " Order By $orderby $direction ";
					 
		//	echo "sad".$query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'DI', '', '') QtyExists From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				$deliverydt_heatno[$i] = $rowsdt[28];
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ".
							 " ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0), Currency_Code, ifNull(Item_CostMethod,0), ".
							 " Item_SellingPrice1, Item_SellingPrice2, Item_SellingPrice3, Item_SellingPrice4, Item_SellingPrice5, Item_SellingPrice6 ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				$i_curr  	  = $sub_result[9];
				$i_costmethod = $sub_result[10];
				
				$sub_query1 = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query;
				$sub_result1 = selectData($connection, $sub_query1);
				$c_sp   = $sub_result1[0];
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemNonStock[$i]   = $i_nonstock;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
				
				/*switch ($c_sp){
					case "I":
						$i_sp = $sub_result[11]; break;
					case "II":
						$i_sp = $sub_result[12]; break;
					case "III":
						$i_sp = $sub_result[13]; break;
					case "IV":
						$i_sp = $sub_result[14]; break;
					case "V":
						$i_sp = $sub_result[15]; break;
					case "VI":
						$i_sp = $sub_result[16]; break;
				}
				
				//Convert SellPrice To Invoice Currency
				if ($txtCurrCode != $i_curr){
					$sub_query1 = "SELECT ifNull(convertcurfunc('".$i_curr."', '".$txtCurrCode."', '".insertMySQLDate($txtDate)."', 0, 0, '', '', ifNull(".$i_sp.",0)),0) From Dual ";
					$sub_result1 = selectData($connection, $sub_query1);
					$i_sp	= $sub_result1[0];
					if (empty($i_sp)) $i_sp = 0;
				}
				
				$txtItemSellPrice[$i] = $i_sp;*/
				
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(DeliveryDt_Quantity,0)),0) FROM Temp_Deliverydt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Delivery_ID = $PSaId And DeliveryDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_DI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_DI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_DI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				$sub_query = " Select ifNull(Sum(ifNull(DeliveryDt1.DeliveryDt_Quantity,0)),0) ".
							 " From Temp_DeliveryDt As DeliveryDt, DeliveryDt As DeliveryDt1 ".
							 " Where DeliveryDt.DeliveryDt_id = '".$rowsdt[0]."' ".
							 " And DeliveryDt.Journal_Type = 'DI' ".
							 " And (ifNull(DeliveryDt.RelatedDt_Id,0) != 0 and DeliveryDt.RelatedDt_Id != '') ".
							 " And DeliveryDt1.Journal_Type = 'DI' ".
							 " And DeliveryDt1.Delivery_ID != '".$PSaId."' ".
							 " And DeliveryDt1.RelatedDt_Id = DeliveryDt.RelatedDt_Id ";

				$sub_result = selectData($connection, $sub_query);
				$DeliveredQty = $sub_result[0];
				if ($DeliveredQty == '') $DeliveredQty = 0;
				$OrderedQty = $rowsdt[19];
				$RemainedQty = $OrderedQty - $DeliveredQty;
				
				
				$sub_query = " Select ifNull(Sum(ifNull(DeliveryDt1.DeliveryDt_Quantity,0)),0) ".
							 " From Temp_DeliveryDt As DeliveryDt, DeliveryDt As DeliveryDt1 ".
							 " Where DeliveryDt.DeliveryDt_id = ".$rowsdt[0].
							 " And DeliveryDt1.Journal_Type = 'DR' ".
							 " And DeliveryDt1.RelatedDt_Id = DeliveryDt.DeliveryDt_id ";

				$sub_result = selectData($connection, $sub_query);
				$txtSaDtReturnQty = $sub_result[0];
						
				
				$sub_query = " Select ifNull(Sum(ifNull(SalesDt.SaleDt_Quantity,0)),0) ".
							 " From Temp_DeliveryDt As DeliveryDt, SalesDt ".
							 " Where DeliveryDt.DeliveryDt_id = ".$rowsdt[0].
							 " And SalesDt.Journal_Type = 'SI' ".
							 " And SalesDt.RelatedDt_Id = DeliveryDt.DeliveryDt_id ";

				$sub_result = selectData($connection, $sub_query);
				$txtSaDtSoldQty = $sub_result[0];
					
				
				$sub_query = " Select Count(*) ".
							 " From SalesDt ".
							 " Where SalesDt.RelatedDt_Id = '".$rowsdt[0]."' ".
							 " And SalesDt.Journal_Type = 'SI' ";

				$sub_result = selectData($connection, $sub_query);
				$vSalesExists = $sub_result[0];
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[22] && $rowsdt[20] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[20] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
				
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSaId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_Delivery);return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[20] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$RemainedQty."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[20] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='165px;' align='left'>
							<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemCostPrice[$i]' id='costprice[$i]' value='$rowsdt[27]' >
							<input type='hidden' name='txtItemSellPrice[$i]' id='sellprice[$i]' value='$txtItemSellPrice[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<input type='hidden' name='txtSaDtReturnQty[$i]' id='returnqty[$i]' value='$txtSaDtReturnQty[$i]' >
							<input type='hidden' name='txtSaDtSoldQty[$i]' id='soldqty[$i]' value='$txtSaDtSoldQty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}		
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else 	echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtSorderRef[$i]' id='sorderref[$i]' value='".$rowsdt[21]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtSorderQty[$i]' id='sorderqty[$i]' value='".$RemainedQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				
				if ($Module_DeliveryClosingQty == 1) {
					echo " <td width='75px;' align='center'><input type='text' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='".$rowsdt[25]."' maxlength='20'  onchange=\"CalcFinalQty($i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"<input type='hidden' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='' >";
				}
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[22]."</span></td>";
				}
				
				if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
					if ($SalesTTC == 1) 
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
					else
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
					echo "	<td width='85px;' align='right'><span class='".$TextColor."'>&nbsp;</span></td>";
					echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
					if ($SalesTTC == 1) 
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					else
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
							<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
							<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
							<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
				}
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[24], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[23]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				echo"	<td width='50px;' align='center'><input type='checkbox' name='chkSaDtPromo[$i]' id='chkpromo[$i]' value='1'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown;  if ($rowsdt[26] == 1) echo " checked ";    echo" ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery,$Login_MinAllowCostPer,$Login_MaxAllowCostPer); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$totdtqty = $totdtqty + $rowsdt[6];
				$LastItemWarehouse = $rowsdt[4];
			}
			
			if ($totdtqty!=''){
			echo '<script type="text/javascript" language="javascript">';
						echo ' x = document.sales; ';
						echo ' x.txtRealQty.value='.$totdtqty.'; ';
						echo " if(x.txtRealQty.value=='') x.txtRealQty.value = 0; ";
						echo " if(x.txtVehicleWeight.value=='') x.txtVehicleWeight.value = 0; ";
						echo ' if(x.txtVehicleLoadWeight)';
						echo ' x.txtVehicleLoadWeight.value= parseInt(x.txtVehicleWeight.value) + parseInt(x.txtRealQty.value)';
						echo '</script>';
						}
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='$txtSaDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemCostPrice[$i]' id='costprice[$i]' value='$txtItemCostPrice[$i]' >
						<input type='hidden' name='txtItemSellPrice[$i]' id='sellprice[$i]' value='$txtItemSellPrice[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtSaDtReturnQty[$i]' id='returnqty[$i]' value='$txtSaDtReturnQty[$i]' >
						<input type='hidden' name='txtSaDtSoldQty[$i]' id='soldqty[$i]' value='$txtSaDtSoldQty[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						
						if ($Module_DoubleUnit == 1) {
							echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						}	
								
			echo"			</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)
			echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			else echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtSorderRef[$i]' id='sorderref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtSorderQty[$i]' id='sorderqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				
			if ($Module_DeliveryClosingQty == 1) {
				echo " <td width='75px;' align='center'><input type='text' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='$txtSaDtCloseQty[$i]' maxlength='20'  onchange=\"CalcFinalQty($i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"<input type='hidden' name='txtSaDtCloseQty[$i]' id='sadtcloseqty[$i]' value='' >";
			}
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='$txtSaDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='$txtSaDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
				
			if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
				if ($SalesTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
				echo"   <td width='85px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='$txtSaDtDiscount[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
				if ($SalesTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
						<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
						<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
						<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
			}
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtSaDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='$txtSaDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSaDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			echo"	<td width='50px;' align='center'><input type='checkbox' name='chkSaDtPromo[$i]' id='chkpromo[$i]' value='1'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_NegativeQuantity,$Login_NegativeQty,$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$Module_DeliveryWithSorder,$Sale_MaxExceedQty,$FI_SalesCheckSalesman,$Module_Delivery,$Login_MinAllowCostPer,$Login_MaxAllowCostPer); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowDeliveryTotals($connection,$PSaId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$ExportBonusToJV = $arrayparams['ExportBonusToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	
	$txtClientCode   = $arrayparams['txtClientCode'];
	
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
				 
	if ($PSaId != "") {	
	
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)), ".$DecimalBase."), Round(ifNull(Delivery_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Delivery_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Delivery_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Delivery_VatChkbx,0) As Delivery_VatChkbx, Round(ifNull(Delivery_VAT,0), ".$DecimalBase.") As Delivery_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Delivery_VAT,0)) + ifNull(Delivery_TransportAmount,0) + IfNull(Delivery_BonusAmount,0) - IfNull(Delivery_DiscountAmount,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotal,  ".
					 " Round((ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TransportTTC,0),ifNull(Delivery_TransportAmount,0)), ".$DecimalBase.") As TransportAmt, ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_BonusTTC,0),ifNull(Delivery_BonusAmount,0)), ".$DecimalBase.") As BonusAmt ".
					 " From Temp_Deliveries As Deliveries ".
					 " Where Delivery_ID = ".$PSaId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtSaTaxable      = $sub_result[0];
		$txtSaNoneTaxable  = $sub_result[1];
		$txtSaGrossTotal   = $sub_result[2];
		$txtSaDiscount     = $sub_result[3];
		$txtSaDiscountAmt  = $sub_result[4];
		$txtSaVatChk       = $sub_result[5];
		$txtSaVat          = $sub_result[6];
		$txtSaNetTotal     = $sub_result[7];
		$txtSaTotalTaxes   = $sub_result[8];
		$txtSaTransportAmt = $sub_result[9];
		$txtSaBonusAmt 	   = $sub_result[10];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtSaVatChk = $sub_result[0];
	}
	
	echo"<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
			<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>";
		
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'>
			<col width=90>
			<col width=140>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtSaTaxable' value='$txtSaTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtSaNoneTaxable' value='$txtSaNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtSaGrossTotal' value='$txtSaGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  width='40%'><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sadiscount' name='txtSaDiscount' value='$txtSaDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span>
						<input type='text' id='sadiscountamt' name='txtSaDiscountAmt' value='$txtSaDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcSaDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right' width='40%' ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt'>";
	}	

	if ($ExportBonusToJV == 1) {
		echo"	<tr><td valign='center' align='right' width='40%'><span class='style10'>Bonus</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right' width='40%' ><span class='style10'>
						<input type='hidden' name='txtSaVatChk' id='txtSaVatChk' value='$txtSaVatChk' size='1'/>
						<input name='chkSaVat' type='checkbox' id='chkSaVat' value='1' onClick='SetSaVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$Module_Delivery,$SalesTTC);'"; 
						if($txtSaVatChk==1) echo " checked $AllowModifyVAT />";  else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >
						<input type='text' id='savat' name='txtSaVat' value='$txtSaVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' width='40%'><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtSaTotalTaxes' value='$txtSaTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtSaTotalTaxes' value='$txtSaTotalTaxes'>";
	}
						
	echo "		<tr><td valign='middle' align='right' width='40%'><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtSaNetTotal' value='$txtSaNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
			if($txtSaNetTotal<0) echo " color:#FF0000;";		
			
				echo" ' readonly></td>
				</tr> 
			</table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
}

function ShowReturnDeliveryDtGrid($connection,$form,$PSaId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$FI_DeliverySalesPrice = $arrayparams['FI_DeliverySalesPrice'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Module_NegativeQuantity = $arrayparams['Module_NegativeQuantity'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtClientCode   = $arrayparams['txtClientCode'];
	$txtClientDetail = $arrayparams['txtClientDetail'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription");
	
		
	array_push($columnsarray,"warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
	
	
	array_push($columnsarray, 'sadtquantity');
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
		array_push($columnsarray, 'price');
		array_push($columnsarray, 'sadtdiscount');
		array_push($columnsarray, 'sadttotal');
	}	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "deliveryref", "deliveryqty", "sadtquantity", "unit2qty", "price", "sadtdiscount", "lastprice", "sadttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "sadtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemnonstock", "itemexpire", "itemunitcoef");
	$orderbyarray = array("DeliveryDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "DeliveryDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	$TableWidth = 1095;
	if ($Module_SerialNo == 1) $TableWidth += 25;
	if ($Module_Pack == 1)     $TableWidth += 125;
	if ($Module_DeliveryClosingQty == 1)     $TableWidth += 75;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1)     $TableWidth += 345;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','DeliveryDt.DeliveryDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
	
		echo"  <th width='75px;' align='center'><span>Invoice Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Invoice Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
			echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
			echo"  <th width='85px;' align='center'><span>Last Sell. Price</span></th> ";
			echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
			echo"  <th width='105px;' align='center'><span>Total</span></th>";
		}	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSaId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock   = 0;
			$i_zeroprice  = 0;
			$i_minprice   = 0;
			$i_unitcoef   = 1;
			$i_expire     = 0;
			$i_withserial = 0;
						   
			$query = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, DeliveryDt.Item_code, Item_Description, DeliveryDt.Warehouse_Code, ".
					 " DeliveryDt_Remark, DeliveryDt_Quantity, Round(DeliveryDt_Price, ".$DecimalBase."), DeliveryDt_PriceBase1, DeliveryDt_PriceBase2, ".
					 " Round(DeliveryDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(DeliveryDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " DeliveryDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, DeliveryDt.Project_code, DeliveryDt.CostCent_Code,  ".
					 " DeliveryDt.Related_Ref, DeliveryDt.RelatedDt_Id, ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(DeliveryDt_ExpireDate,'%d/%m/%Y') As DeliveryDt_ExpireDate, DeliveryDt_BatchNo, DeliveryDt_HeatNo ".
					 " From Temp_DeliveryDt As DeliveryDt ".
					 " Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on DeliveryDt.DeliveryDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'DR' ".
					 " And Temp_Purchasesserialno.DeliveryDt_ID Is Not Null ".
					 " Where DeliveryDt.Delivery_ID = ".$PSaId.
					 " Group By  DeliveryDt.DeliveryDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'DI', '', '') QtyExists From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				$deliverydt_heatno[$i] = $rowsdt[25];
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				$sub_query = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$c_sp   = $sub_result[0];
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemNonStock[$i]   = $i_nonstock;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DR', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DR', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DR', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(DeliveryDt_Quantity,0)),0) FROM Temp_Deliverydt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Delivery_ID = $PSaId And DeliveryDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_DI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_DI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_DI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				
				$sub_query = " Select ifNull(DeliveryDt.DeliveryDt_Quantity,0) - ifNull(Sum(ifNull(ReturnDeliveryDt.DeliveryDt_Quantity,0)),0) ".
							 " From DeliveryDt As DeliveryDt, DeliveryDt As ReturnDeliveryDt ".
							 " Where DeliveryDt.DeliveryDt_Id = '".$rowsdt[20]."' ".
							 " And DeliveryDt.Journal_Type = 'DI' ".
							 " And ReturnDeliveryDt.Journal_Type = 'DR' ".
							 " And ReturnDeliveryDt.DeliveryDt_Id != '".$rowsdt[0]."' ".
							 " And ReturnDeliveryDt.RelatedDt_Id = DeliveryDt.DeliveryDt_Id ";

				$sub_result = selectData($connection, $sub_query);
				$txtRemainedQty = $sub_result[0];
				if ($txtRemainedQty == '') $txtRemainedQty = 0;
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[22] && $rowsdt[21] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[21] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
								
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSaId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_Delivery);return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[21] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$RemainedQty."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[21] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='165px;' align='left'>
							<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
					
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryRef[$i]' id='deliveryref[$i]' value='".$rowsdt[19]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryQty[$i]' id='deliveryqty[$i]' value='".$txtRemainedQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[22]."</span></td>";
				}
				
				if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
					if ($SalesTTC == 1) 
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
					else
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
					echo "	<td width='85px;' align='right'><span class='".$TextColor."'>&nbsp;</span></td>";
					echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
					if ($SalesTTC == 1) 
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					else
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
							<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
							<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
							<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
				}
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[24], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[23]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='$txtSaDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
								
						if ($Module_DoubleUnit == 1) {
							echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						}	
						
		echo"				</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";				
				else echo"<input type='hidden' name='deliverydt_heatno[$i]' id='deliverydt_heatno[$i]' value='".$deliverydt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";		
						
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryRef[$i]' id='deliveryref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryQty[$i]' id='deliveryqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='$txtSaDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='$txtSaDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
				
			if ($Module_Delivery == 0 && $FI_DeliverySalesPrice == 1) {
				if ($SalesTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
				echo"   <td width='85px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='$txtSaDtDiscount[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
				if ($SalesTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"	<input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value=''  >
						<input type='hidden' name='txtItemLastSellPrice[$i]' id='lastprice[$i]' value=''  >
						<input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='' >
						<input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='' >";
			}
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtSaDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='$txtSaDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSaDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_SalesCheckSalesman,$Module_Delivery); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowReturnDeliveryTotals($connection,$PSaId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$ExportBonusToJV = $arrayparams['ExportBonusToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	
	$txtClientCode   = $arrayparams['txtClientCode'];
	
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
				 
	if ($PSaId != "") {	
	
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)), ".$DecimalBase."), Round(ifNull(Delivery_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Delivery_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Delivery_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Delivery_VatChkbx,0) As Delivery_VatChkbx, Round(ifNull(Delivery_VAT,0), ".$DecimalBase.") As Delivery_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Delivery_TaxableTTC,0),ifNull(Delivery_Taxable,0)) + ifNull(Delivery_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Delivery_VAT,0)) + ifNull(Delivery_TransportAmount,0) + IfNull(Delivery_BonusAmount,0) - IfNull(Delivery_DiscountAmount,0) + (ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0))), ".$DecimalBase.") As Delivery_NetTotal,  ".
					 " Round((ifNull(Delivery_Tax1,0) + ifNull(Delivery_Tax2,0) + ifNull(Delivery_Tax3,0) + ifNull(Delivery_Tax4,0) + ifNull(Delivery_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_TransportTTC,0),ifNull(Delivery_TransportAmount,0)), ".$DecimalBase.") As TransportAmt, ".
					 " Round(if($SalesTTC=1,ifNull(Delivery_BonusTTC,0),ifNull(Delivery_BonusAmount,0)), ".$DecimalBase.") As BonusAmt ".
					 " From Temp_Deliveries As Deliveries ".
					 " Where Delivery_ID = ".$PSaId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtRSaTaxable      = $sub_result[0];
		$txtRSaNoneTaxable  = $sub_result[1];
		$txtRSaGrossTotal   = $sub_result[2];
		$txtRSaDiscount     = $sub_result[3];
		$txtRSaDiscountAmt  = $sub_result[4];
		$txtRSaVatChk       = $sub_result[5];
		$txtRSaVat          = $sub_result[6];
		$txtRSaNetTotal     = $sub_result[7];
		$txtRSaTotalTaxes   = $sub_result[8];
		$txtRSaTransportAmt = $sub_result[9];
		$txtRSaBonusAmt 	= $sub_result[10];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtRSaVatChk = $sub_result[0];
	}
	
	echo"<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>";
	
			
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'><col width=90>
			<col width=140>
				<tr><td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtRSaTaxable' value='$txtRSaTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtRSaNoneTaxable' value='$txtRSaNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtRSaGrossTotal' value='$txtRSaGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  ><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sadiscount' name='txtRSaDiscount' value='$txtRSaDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcRSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span> 
						<input type='text' id='sadiscountamt' name='txtRSaDiscountAmt' value='$txtRSaDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcRSaDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right'  ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='satransportamt' name='txtRSaTransportAmt' value='$txtRSaTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='satransportamt' name='txtRSaTransportAmt' value='$txtRSaTransportAmt'>";
	}	

	if ($ExportBonusToJV == 1) {
		echo"	<tr><td valign='center' align='right' ><span class='style10'>Bonus</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sabonusamt' name='txtRSaBonusAmt' value='$txtRSaBonusAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sabonusamt' name='txtRSaBonusAmt' value='$txtRSaBonusAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right'  ><span class='style10'>
						<input type='hidden' name='txtRSaVatChk' id='txtRSaVatChk' value='$txtRSaVatChk' size='1'/>
						<input name='chkRSaVat' type='checkbox' id='chkRSaVat' value='1' onClick='SetRSaVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$Module_Delivery,$SalesTTC);'"; 
						if($txtRSaVatChk==1) echo " checked $AllowModifyVAT />";  else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >
						<input type='text' id='savat' name='txtRSaVat' value='$txtRSaVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' ><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtRSaTotalTaxes' value='$txtRSaTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtRSaTotalTaxes' value='$txtRSaTotalTaxes'>";
	}
			
	echo "		<tr><td valign='middle' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtRSaNetTotal' value='$txtRSaNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
				
					if($txtRSaNetTotal<0) echo " color:#FF0000;";
					
					echo" ' readonly>";
				 echo"</td>
		  </tr>
		  
		  </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";



}

function ShowSalesDtGrid($connection,$form,$PSaId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$FI_SalesWithPrice = $arrayparams['FI_SalesWithPrice'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Login_MinAllowCostPer = $arrayparams['Login_MinAllowCostPer'];
	$Login_MaxAllowCostPer = $arrayparams['Login_MaxAllowCostPer'];
	$Module_NegativeQuantity = $arrayparams['Module_NegativeQuantity'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$Module_DeliveryClosingQty = $arrayparams['Module_DeliveryClosingQty'];
	$Module_DeliveryWithSorder = $arrayparams['Module_DeliveryWithSorder'];
	$Allow_MinimumPrice = $arrayparams['Allow_MinimumPrice'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	$PriceDecimalBase = $arrayparams['PriceDecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtClientCode   = $arrayparams['txtClientCode'];
	$txtClientDetail = $arrayparams['txtClientDetail'];


	$txtCurrCode     = $arrayparams['txtCurrCode'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	array_push($columnsarray, 'sadtquantity');
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	
	if ($FI_SalesWithPrice == 1) {
		array_push($columnsarray, 'price');
		array_push($columnsarray, 'sadtdiscount');
		array_push($columnsarray, 'sadttotal');
	}
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "deliveryref", "deliveryqty", "sadtquantity", "unit2qty", "price", "sadtdiscount", "lastprice", "sadttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "sadtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemnonstock", "itemexpire", "itemunitcoef", "itemwithserial", "itemminprice", "costprice", "sellprice");
	$orderbyarray = array("SalesDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "SalesDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1145;
	if ($Module_SerialNo == 1)     $TableWidth += 25;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($FI_SalesWithPrice == 1)   $TableWidth += 345;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','SalesDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','SalesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		echo"  <th width='75px;' align='center'><span>Delivery Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Del. Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Invoice Qty</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		 
		if ($FI_SalesWithPrice == 1) { 
			echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
			echo"  <th width='85px;' align='center'><span>Last Sell. Price</span></th> ";
			echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
			echo"  <th width='105px;' align='center'><span>Total</span></th>";
		}
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='50px;' align='center'><span>Promo</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSaId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
						   
					   
			$query = " Select SalesDt.SaleDt_ID, SalesDt.Item_CodePacked, SalesDt.Item_code, Item_Description, SalesDt.Warehouse_Code, ".
					 " SalesDt_Remark, SaleDt_Quantity, Round(SalesDt.SaleDt_Price, ".$PriceDecimalBase."), SalesDt.SaleDt_PriceBase1, SalesDt.SaleDt_PriceBase2, ".
					 " Round(SaleDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((SaleDt_Quantity*SalesDt.SaleDt_Price)-((SaleDt_Quantity*SalesDt.SaleDt_Price*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(SaleDt_PriceTTC, ".$PriceDecimalBase."), ".
					 " Round(((SaleDt_Quantity*SaleDt_PriceTTC)-((SaleDt_Quantity*SaleDt_PriceTTC*ifNull(SaleDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " SalesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, SalesDt.Project_code, SalesDt.CostCenter_Code,  ".
					 " Deliveries.Delivery_reference, DeliveryDt.DeliveryDt_ID, DeliveryDt.DeliveryDt_Quantity, ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(SaleDt_ExpireDate,'%d/%m/%Y') As SaleDt_ExpireDate, SaleDt_BatchNo, ifNull(SaleDt_Promotion,0), ".
					 " Round(ifNull(MeanCostCur(SalesDt.Item_code, SalesDt.Sale_Date, 1, '".$Base1."', SalesDt.Currency_Code),0), ".$DecimalBase.") as ItemCost ".
					 " From Temp_SalesDt As SalesDt Left Join Items on SalesDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on SalesDt.SaleDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'SI' ".
					 " And Temp_Purchasesserialno.SaleDt_ID Is Not Null ".
					 " Left Join DeliveryDt on DeliveryDt.DeliveryDt_ID = SalesDt.RelatedDt_Id  ".
					 " Left Join Deliveries on Deliveries.Delivery_ID = DeliveryDt.Delivery_ID  ".
					 " Where SalesDt.Sale_ID = ".$PSaId.
					 " Group By  SalesDt.SaleDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'SI', '', '') QtyExists From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ".
							 " ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0), Currency_Code, ifNull(Item_CostMethod,0), ".
							 " Item_SellingPrice1, Item_SellingPrice2, Item_SellingPrice3, Item_SellingPrice4, Item_SellingPrice5, Item_SellingPrice6 ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				$i_curr  	  = $sub_result[9];
				$i_costmethod = $sub_result[10];
				
				
				$sub_query1 = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query1;
				$sub_result1 = selectData($connection, $sub_query1);
				$c_sp   = $sub_result1[0];
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemNonStock[$i]   = $i_nonstock;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				$txtItemMinPrice[$i]   = $i_minprice;

				
				/*switch ($c_sp){
					case "I":
						$i_sp = $sub_result[11]; break;
					case "II":
						$i_sp = $sub_result[12]; break;
					case "III":
						$i_sp = $sub_result[13]; break;
					case "IV":
						$i_sp = $sub_result[14]; break;
					case "V":
						$i_sp = $sub_result[15]; break;
					case "VI":
						$i_sp = $sub_result[16]; break;
				}
				
				//Convert SellPrice To Invoice Currency
				if ($txtCurrCode != $i_curr){
					$sub_query1 = "SELECT ifNull(convertcurfunc('".$i_curr."', '".$txtCurrCode."', '".insertMySQLDate($txtDate)."', 0, 0, '', '', ifNull(".$i_sp.",0)),0) From Dual ";
					$sub_result1 = selectData($connection, $sub_query1);
					$i_sp	= $sub_result1[0];
					if (empty($i_sp)) $i_sp = 0;
				}
				
				$txtItemSellPrice[$i] = $i_sp;*/
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SI', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SI', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SI', '', '', $0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(SalesDt_Quantity,0)),0) FROM Temp_Salesdt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Sale_ID = $PSaId And SaleDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_SI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_SI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_SI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
		//Get Last Purchase Price For an Item
				$i_lpurp = 0;
				$sub_query = " SELECT if($SalesTTC = 1, SaleDt_PriceTTC, SaleDt_Price),date_format(Sale_Date,'%d/%m/%Y'),currency_code FROM SalesDt WHERE Client_Code = '".$txtClientCode."' And Item_code = '".str_replace('\\','\\\\',$rowsdt[2])."' And ifNull(SaleDt_Price,0) > 0 ".
					 //" And (('".$lstWarehouse."' = '' And 1 = 1) Or ('".$lstWarehouse."' != '' And Warehouse_Code = '".$lstWarehouse."' )) ".
					 "   And SalesDt.SaleDt_id != '".$rowsdt[0]."' ".
					 " Order By Sale_Date Desc Limit 1 ";							 
				//echo$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_lpurp = $sub_result[0];
				$lastselldate = $sub_result[1];
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$i_lpurp = ConvertCurrency($sub_result[2],$txtCurrCode, insertMySQLDate($txtDate), $i_lpurp, $connection);
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$txtLastSellPrice[$i] = $i_lpurp;
				//echo $sub_query ;
				
				$sub_query = " Select Sum(Q1.QtySold) As QtySold From ".
							 " ( Select ifNull(Sum(ifNull(SaleDt_Quantity,0)), 0) As QtySold ".
							 "   From SalesDt ".
							 "   Where SalesDt.RelatedDt_Id = '".$rowsdt[20]."' ".
							 "   And SalesDt.SaleDt_id != '".$rowsdt[0]."' ".
							 "   Union ".
							 "   Select ifNull(Sum(ifNull(SaleDt_Quantity,0)), 0) As QtySold ".
							 "   From Temp_SalesDt ".
							 "   Where Temp_SalesDt.RelatedDt_Id = '".$rowsdt[20]."' ".
							 "   And Temp_SalesDt.SaleDt_id != '".$rowsdt[0]."' ".
							 " ) As Q1 ";			 

				$sub_result = selectData($connection, $sub_query);
				$SoldQty = $sub_result[0];
				if ($SoldQty == '') $SoldQty = 0;
				$DeliveredQty = $rowsdt[21];
				
				$sub_query = " Select Sum(ifNull(DR.DeliveryDt_Quantity,0)) As QuantityReturned ".
							 " From DeliveryDt DR ".
							 " Where DR.RelatedDt_ID =  '".$rowsdt[20]."' ".
							 " And DR.Journal_Type = 'DR' ";
				$sub_result = selectData($connection, $sub_query);
				if ($sub_result[0] == '') $sub_result[0] = 0;		  
				$ReturnQty = $sub_result[0];
			
				$RemainedQty = $DeliveredQty - $ReturnQty - $SoldQty;
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[23] && $rowsdt[22] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[22] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSaId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[22] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$RemainedQty."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[22] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='165px;' align='left'>
							<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemCostPrice[$i]' id='costprice[$i]' value='$rowsdt[27]' >
							<input type='hidden' name='txtItemSellPrice[$i]' id='sellprice[$i]' value='$txtItemSellPrice[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemMinPrice[$i]' id='itemminprice[$i]' value='$txtItemMinPrice[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtLastSellPrice[$i]' id='lastsellprice[$i]' value='$txtLastSellPrice[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Last Sell. Price</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtLastSellPrice[$i],$PriceDecimalBase)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Last Sell. Date</span></td><td width='100px;' align='right'><span class='style1'>".$lastselldate."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}		
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryRef[$i]' id='deliveryref[$i]' value='".$rowsdt[19]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryQty[$i]' id='deliveryqty[$i]' value='".$RemainedQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[23]."</span></td>";
				}
				
				if ($FI_SalesWithPrice == 1) {
					if ($SalesTTC == 1) 
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
					else
						echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
							
					echo "	<td width='85px;' align='right'><span class='".$TextColor."'>&nbsp;</span></td>";
					echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
						
								
					if ($SalesTTC == 1)
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					else
						echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					
				} else {
					if ($SalesTTC == 1) 
						echo"  <input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' >";
						else
						echo"  <input type='hidden' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' >";
						
					echo"  <input type='hidden' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' >";
					if ($SalesTTC == 1) 
						echo"  <input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' >";
					else
						echo"  <input type='hidden' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' >";
				}
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[25], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[24]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				echo"	<td width='50px;' align='center'><input type='checkbox' name='chkSaDtPromo[$i]' id='chkpromo[$i]' value='1'  onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown;  if ($rowsdt[26] == 1) echo " checked ";    echo" ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Allow_MinimumPrice); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Allow_MinimumPrice,$Login_MinAllowCostPer,$Login_MaxAllowCostPer); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
			
				$totdtqty = $totdtqty + $rowsdt[6];			
				$LastItemWarehouse = $rowsdt[4];
			}
			
			if ($totdtqty!=''){
			echo '<script type="text/javascript" language="javascript">';
						echo ' x = document.sales; ';
						echo ' x.txtRealQty.value='.$totdtqty.'; ';
						echo " if(x.txtRealQty.value=='') x.txtRealQty.value = 0; ";
						echo " if(x.txtVehicleWeight.value=='') x.txtVehicleWeight.value = 0; ";
						echo ' if(x.txtVehicleLoadWeight)';
						echo ' x.txtVehicleLoadWeight.value= parseInt(x.txtVehicleWeight.value) + parseInt(x.txtRealQty.value)';
						echo '</script>';
						}
			
		}
		/*
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='$txtSaDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemMinPrice[$i]' id='itemminprice[$i]' value='$txtItemMinPrice[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'>Existing Qty</td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'>Booking Qty</td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'>Available Qty</td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
							</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryRef[$i]' id='deliveryref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtSaDtDeliveryQty[$i]' id='deliveryqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='$txtSaDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
								
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='$txtSaDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
				
			if ($SalesTTC == 1) 
				echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
			else
				echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
			echo"   <td width='85px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='$txtSaDtDiscount[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
			if ($SalesTTC == 1) 
				echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			else
				echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtSaDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='$txtSaDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSaDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Allow_MinimumPrice); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Allow_MinimumPrice); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}*/
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowSalesTotals($connection,$PSaId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$ExportBonusToJV = $arrayparams['ExportBonusToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$FI_SalesWithPrice = $arrayparams['FI_SalesWithPrice'];
	
	$txtClientCode   = $arrayparams['txtClientCode'];	
	
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
				 
	if ($PSaId != "") {	
	
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Sale_TaxableTTC,0),ifNull(Sale_Taxable,0)), ".$DecimalBase."), Round(ifNull(Sale_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Sale_TaxableTTC,0),ifNull(Sale_Taxable,0)) + ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Sale_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Sale_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(sale_chkvat,0) As Sale_VatChckbx, Round(ifNull(Sale_VAT,0), ".$DecimalBase.") As Sale_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Sale_TaxableTTC,0),ifNull(Sale_Taxable,0)) + ifNull(Sale_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Sale_VAT,0)) + ifNull(Sale_TransportAmount,0) + IfNull(Sale_BonusAmount,0) - IfNull(Sale_DiscountAmount,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))), ".$DecimalBase.") As Sale_NetTotal,  ".
					 " Round((ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Sale_TransportTTC,0),ifNull(Sale_TransportAmount,0)), ".$DecimalBase.") As TransportAmt, ".
					 " Round(if($SalesTTC=1,ifNull(Sale_BonusTTC,0),ifNull(Sale_BonusAmount,0)), ".$DecimalBase.") As BonusAmt ".
					 " From Temp_Sales As Sales ".
					 " Where Sale_ID = ".$PSaId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtSaTaxable      = $sub_result[0];
		$txtSaNoneTaxable  = $sub_result[1];
		$txtSaGrossTotal   = $sub_result[2];
		$txtSaDiscount     = $sub_result[3];
		$txtSaDiscountAmt  = $sub_result[4];
		$txtSaVatChk       = $sub_result[5];
		$txtSaVat          = $sub_result[6];
		$txtSaNetTotal     = $sub_result[7];
		$txtSaTotalTaxes   = $sub_result[8];
		$txtSaTransportAmt = $sub_result[9];
		$txtSaBonusAmt 	   = $sub_result[10];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtSaVatChk = $sub_result[0];
	}
	
	echo"	<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
			<tr>
					<td id='total-top' style='width:240px;height:12px;'></td>
					</tr>
					<tr><td id='total-content' style='width:240px;height:12px;'>
			
				
			<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'>
				<col width=90>
				<col width=140>
				<tr><td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtSaTaxable' value='$txtSaTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtSaNoneTaxable' value='$txtSaNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtSaGrossTotal' value='$txtSaGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  ><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sadiscount' name='txtSaDiscount' value='$txtSaDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span>
						<input type='text' id='sadiscountamt' name='txtSaDiscountAmt' value='$txtSaDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcSaDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right'  ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt'>";
	}	

	if ($ExportBonusToJV == 1) {
		echo"	<tr><td valign='center' align='right' ><span class='style10'>Bonus</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right' ><span class='style10'>
						<input type='hidden' name='txtSaVatChk' id='txtSaVatChk' value='$txtSaVatChk' size='1'/>
						<input name='chkSaVat' type='checkbox' id='chkSaVat' value='1' onClick='SetSaVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$Module_Delivery,$SalesTTC);'"; 
						if($txtSaVatChk==1) echo " checked $AllowModifyVAT />";  else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >
						<input type='text' id='savat' name='txtSaVat' value='$txtSaVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' ><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtSaTotalTaxes' value='$txtSaTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtSaTotalTaxes' value='$txtSaTotalTaxes'>";
	}
						
	echo "		<tr><td valign='middle' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtSaNetTotal' value='$txtSaNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
					if($txtSaNetTotal<0) echo " color:#FF0000;";
					
					echo " ' readonly></td>
		  </tr>
		  
		  </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}


function ShowReturnSalesDtGrid($connection,$form,$PSaId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$FI_DeliverySalesPrice = $arrayparams['FI_DeliverySalesPrice'];
	$FI_SalesCheckSalesman = $arrayparams['FI_SalesCheckSalesman'];
	$Login_NegativeQty = $arrayparams['Login_NegativeQty'];
	$Login_ModifySellingPrice = $arrayparams['Login_ModifySellingPrice'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	$Module_NegativeQuantity = $arrayparams['Module_NegativeQuantity'];
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$Module_DeliveryClosingQty = $arrayparams['Module_DeliveryClosingQty'];
	$Module_DeliveryWithSorder = $arrayparams['Module_DeliveryWithSorder'];
	$Allow_MinimumPrice = $arrayparams['Allow_MinimumPrice'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	
	
	$SalesTTC = $arrayparams['SalesTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtClientCode   = $arrayparams['txtClientCode'];
	$txtClientDetail = $arrayparams['txtClientDetail'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($SalesTTC == 1)  $PriceLabel = 'Price TTC';	
	
	$ModeModifySellPrice = '';
	if ($Login_ModifySellingPrice != 1) $ModeModifySellPrice = 'readonly';
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	array_push($columnsarray, 'sadtquantity');
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	array_push($columnsarray, 'price');
	array_push($columnsarray, 'sadtdiscount');
	array_push($columnsarray, 'sadttotal');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "retdeliveryref", "retdeliveryqty", "sadtquantity", "unit2qty", "price", "sadtdiscount", "sadttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "sadtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemnonstock", "itemexpire", "itemunitcoef");
	$orderbyarray = array("SalesDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "SalesDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1355;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','SalesDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','SalesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		echo"  <th width='75px;' align='center'><span>Return Delivery Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem.Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
		echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
		echo"  <th width='105px;' align='center'><span>Total</span></th>";
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PSaId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;

					   
			$query = " Select SalesDt.SaleDt_ID, SalesDt.Item_CodePacked, SalesDt.Item_code, Item_Description, SalesDt.Warehouse_Code, ".
					 " SalesDt_Remark, SaleDt_Quantity, Round(SalesDt.SaleDt_Price, ".$DecimalBase."), SalesDt.SaleDt_PriceBase1, SalesDt.SaleDt_PriceBase2, ".
					 " Round(SaleDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((SaleDt_Quantity*SalesDt.SaleDt_Price)-((SaleDt_Quantity*SalesDt.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, ".
					 " SalesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, SalesDt.Project_code, SalesDt.CostCenter_Code,  ".
					 " Deliveries.Delivery_reference, DeliveryDt.DeliveryDt_ID, DeliveryDt.DeliveryDt_Quantity, ifNull(item_withserialno,0) As ItemWithSerialNo, ".
					 " Date_Format(SaleDt_ExpireDate,'%d/%m/%Y') As SaleDt_ExpireDate, SaleDt_BatchNo ".
					 " From Temp_SalesDt As SalesDt Left Join Items on SalesDt.Item_code = Items.Item_code ".
					 " Left Join DeliveryDt on DeliveryDt.DeliveryDt_ID = SalesDt.RelatedDt_Id and DeliveryDt.Journal_Type = 'DR'  ".
					 " Left Join Deliveries on Deliveries.Delivery_ID = DeliveryDt.Delivery_ID  ".
					 " Where SalesDt.Sale_ID = ".$PSaId.
					 " Group By  SalesDt.SaleDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

								
				$sub_query  = " Select QtyExistsWSorder('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', 0, 'SI', '', '') QtyExists From Dual ";
				//echo $sub_query;
				$sub_result = selectData($connection, $sub_query);
				$vItemQtyExists = 0;
				$vItemQtyExists = $sub_result[0];		
						
				$TextColor = 'style7';
				if ($vItemQtyExists < 0) {
					$Color="style='background-color:#FF0000;'";
					$TextColor = 'style8';
				}	
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				$sub_query = "SELECT ifNull(Client_SellingPrice,1) FROM Clients WHERE Ledger_Number = '".$txtClientCode."'";
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$c_sp   = $sub_result[0];
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemNonStock[$i]   = $i_nonstock;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				$txtItemMinPrice[$i]   = $i_minprice;
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SI', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SI', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'SI', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(SalesDt_Quantity,0)),0) FROM Temp_Salesdt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Sale_ID = $PSaId And SaleDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_SI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_SI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_SI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				$sub_query = " Select Sum(Q1.QtyReturned) As QtyReturned From ".
							 " ( Select ifNull(Sum(ifNull(SaleDt_Quantity,0)), 0) As QtyReturned ".
							 "   From SalesDt ".
							 "   Where SalesDt.RelatedDt_Id = '".$rowsdt[20]."' ".
							 "   And SalesDt.SaleDt_id != '".$rowsdt[0]."' ".
							 "   And SalesDt.Journal_Type = 'SR' ".
							 "   Union ".
							 "   Select ifNull(Sum(ifNull(SaleDt_Quantity,0)), 0) As QtyReturned ".
							 "   From Temp_SalesDt ".
							 "   Where Temp_SalesDt.RelatedDt_Id = '".$rowsdt[20]."' ".
							 "   And Temp_SalesDt.SaleDt_id != '".$rowsdt[0]."' ".
							 "   And Temp_SalesDt.Journal_Type = 'SR' ".
							 " ) As Q1 ";
							 
				
				$sub_result = selectData($connection, $sub_query);
				$ReturnQty = $sub_result[0];
				if ($ReturnQty == '') $ReturnQty = 0;
				$ReturnDeliveryQty = $rowsdt[21];
				
				$RemainedQty = $ReturnDeliveryQty - $ReturnQty;
				
				
			 
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;'>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PSaId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='165px;' align='left'>
							<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				echo"	<td width='75px;' align='center'><input type='text' name='txtRetDeliveryRef[$i]' id='retdeliveryref[$i]' value='".$rowsdt[19]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtRetDeliveryQty[$i]' id='retdeliveryqty[$i]' value='".$RemainedQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($SalesTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
				echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
							
				if ($SalesTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='".$rowsdt[11]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[24], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[23]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		/*
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtSaDtId[$i]' id='sadtid[$i]' value='$txtSaDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemNonStock[$i]' id='itemnonstock[$i]' value='$txtItemNonStock[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'>Existing Qty</td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'>Booking Qty</td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'>Available Qty</td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
							</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
			echo"	<td width='75px;' align='center'><input type='text' name='txtRetDeliveryRef[$i]' id='retdeliveryref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtRetDeliveryQty[$i]' id='retdeliveryqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtSaDtQty[$i]' id='sadtquantity[$i]' value='$txtSaDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcSaDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
								
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='$txtSaDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtSaDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($SalesTTC == 1) 
				echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
			else
				echo"	<td width='85px;' align='center'><input type='text' name='txtSaDtPrice[$i]' id='price[$i]' value='$txtSaDtPrice[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown." $ModeModifySellPrice ></td>";
						
			echo"   <td width='85px;' align='center'><input type='text' name='txtItemLastSellPrice' id='lastprice[$i]' value='$txtItemLastSellPrice' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"	<td width='70px;' align='center'><input type='text' name='txtSaDtDiscount[$i]' id='sadtdiscount[$i]' value='$txtSaDtDiscount[$i]' maxlength='20' onKeyup='CalcSaDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." $ModeDiscount ></td>";
					
							
			if ($SalesTTC == 1) 
				echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			else
				echo"   <td width='105px;' align='center'><input type='text' name='txtSaDtTotal[$i]' id='sadttotal[$i]' value='$txtSaDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtSaDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value='$txtSaDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtSaDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtSaDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtSaDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtSaDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PSaId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}*/
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowReturnSalesTotals($connection,$PSaId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$SalesTTC = $arrayparams['SalesTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$Module_Delivery = $arrayparams['Module_Delivery'];
	$ExportTransportToJV = $arrayparams['ExportTransportToJV'];
	$ExportBonusToJV = $arrayparams['ExportBonusToJV'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['AllowModifyVAT'];
	$Login_ModifyDiscount = $arrayparams['Login_ModifyDiscount'];
	
	$txtClientCode   = $arrayparams['txtClientCode'];
	
	if ($Login_ModifyDiscount == 0)	$DiscountMode = ' readonly ';
	
	if ($PSaId != "") {	
	
		$sub_query = " Select Round(if($SalesTTC=1,ifNull(Sale_TaxableTTC,0),ifNull(Sale_Taxable,0)), ".$DecimalBase."), Round(ifNull(Sale_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($SalesTTC=1,ifNull(Sale_TaxableTTC,0),ifNull(Sale_Taxable,0)) + ifNull(Sale_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Sale_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Sale_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(sale_chkvat,0) As Sale_VatChckbx, Round(ifNull(Sale_VAT,0), ".$DecimalBase.") As Sale_VAT, ".
					 " Round((if($SalesTTC=1,ifNull(Sale_TaxableTTC,0),ifNull(Sale_Taxable,0)) + ifNull(Sale_NoneTaxable,0) + if($SalesTTC=1,0,IfNull(Sale_VAT,0)) + ifNull(Sale_TransportAmount,0) + IfNull(Sale_BonusAmount,0) - IfNull(Sale_DiscountAmount,0) + (ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0))), ".$DecimalBase.") As Sale_NetTotal,  ".
					 " Round((ifNull(Sale_Tax1,0) + ifNull(Sale_Tax2,0) + ifNull(Sale_Tax3,0) + ifNull(Sale_Tax4,0) + ifNull(Sale_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(if($SalesTTC=1,ifNull(Sale_TransportTTC,0),ifNull(Sale_TransportAmount,0)), ".$DecimalBase.") As TransportAmt, ".
					 " Round(if($SalesTTC=1,ifNull(Sale_BonusTTC,0),ifNull(Sale_BonusAmount,0)), ".$DecimalBase.") As BonusAmt ".
					 " From Temp_Sales As Sales ".
					 " Where Sale_ID = ".$PSaId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtSaTaxable      = $sub_result[0];
		$txtSaNoneTaxable  = $sub_result[1];
		$txtSaGrossTotal   = $sub_result[2];
		$txtSaDiscount     = $sub_result[3];
		$txtSaDiscountAmt  = $sub_result[4];
		$txtSaVatChk       = $sub_result[5];
		$txtSaVat          = $sub_result[6];
		$txtSaNetTotal     = $sub_result[7];
		$txtSaTotalTaxes   = $sub_result[8];
		$txtSaTransportAmt = $sub_result[9];
		$txtSaBonusAmt 	   = $sub_result[10];
	} else {
		$sub_query = " Select ifNull(Client_VAT,0) From Clients Where Ledger_Number = '".$txtClientCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtSaVatChk = $sub_result[0];
	}	
	
	echo"<table border='0' style='table-layout:fixed;' cellspacing=0>
		<tr>
				<td id='total-top' style='width:240px;height:12px;'></td>
				</tr>
				<tr><td id='total-content' style='width:240px;height:12px;'>
		<table border='0' style='table-layout:fixed;' cellspacing=0 valign='top'>
					<col width=90>
					<col width=140>";
					
	echo"	
				<tr><td valign='center' align='right' width='40%'><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtSaTaxable' value='$txtSaTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtSaNoneTaxable' value='$txtSaNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='40%'><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtSaGrossTotal' value='$txtSaGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  width='40%'><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sadiscount' name='txtSaDiscount' value='$txtSaDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcSaDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ><span class='style10'> %</span>
						<input type='text' id='sadiscountamt' name='txtSaDiscountAmt' value='$txtSaDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcSaDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);' $DiscountMode ></td>
				</tr>";
				
	if ($ExportTransportToJV == 1) {
		echo"	<tr><td valign='center' align='right' width='40%' ><span class='style10'>Transport</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='satransportamt' name='txtSaTransportAmt' value='$txtSaTransportAmt'>";
	}	

	if ($ExportBonusToJV == 1) {
		echo"	<tr><td valign='center' align='right' width='40%'><span class='style10'>Bonus</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt' maxlength='20'  style='text-align:right; width:120px; height=20px;' onKeyup='CalcNetTotal($DecimalBase,$VatApplied,$Setup_VatPercentage,$SalesTTC);'></td>
				</tr>";
	} else {
		echo "	<input type='hidden' id='sabonusamt' name='txtSaBonusAmt' value='$txtSaBonusAmt'>";
	}
	
	echo"		<tr><td valign='center' align='right' width='40%' ><span class='style10'>
						<input type='hidden' name='txtSaVatChk' id='txtSaVatChk' value='$txtSaVatChk' size='1'/>
						<input name='chkSaVat' type='checkbox' id='chkSaVat' value='1' onClick='SetSaVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$Module_Delivery,$SalesTTC);'"; 
						if($txtSaVatChk==1) echo " checked $AllowModifyVAT />";  else echo " $AllowModifyVAT />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >
						<input type='text' id='savat' name='txtSaVat' value='$txtSaVat' maxlength='20' style='text-align:right; width:120px; height=20px;' >
					</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' width='40%'><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtSaTotalTaxes' value='$txtSaTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtSaTotalTaxes' value='$txtSaTotalTaxes'>";
	}
						
	echo "		<tr><td valign='middle' align='right' width='40%'><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtSaNetTotal' value='$txtSaNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
					if($txtSaNetTotal<0) echo " color:#FF0000;";
				echo" ' readonly></td>
				</tr> 
		 </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}

function ShowPurchaseOrderDtGrid($connection,$form,$PPorId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_PurchaseCategory = $arrayparams['FI_PurchaseCategory'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
		
	
	$PurchaseTTC = $arrayparams['PurchaseTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtSupplierCode = $arrayparams['txtSupplierCode'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($PurchaseTTC == 1)  $PriceLabel = 'Price TTC';	
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
	
	
	array_push($columnsarray, "pordtquantity");
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	array_push($columnsarray, 'price');
	array_push($columnsarray, 'pordtdiscount');
	array_push($columnsarray, 'pordttotal');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode","HeatNo", "prequestref", "prequestqty", "unit2qty", "pordtquantity", "price", "pordtdiscount", "pordttotal", "projectcodedt", "costcentcodedt", "remarkdt", "pordtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("PorderDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "PorderDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1350;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','PorderDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='180px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','PorderDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','PorderDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
		
		echo"  <th width='75px;' align='center'><span>Request Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Rqst Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		echo"  <th width='85px;' align='center'><span>Last Pur.Pr</span></th>";
		echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
		echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
		echo"  <th width='105px;' align='center'><span>Total</span></th>";
		
		if ($Module_ProjectDt == 1) { 
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPorId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
					   
			$query = " Select PorderDt.PorderDt_ID, PorderDt.Item_CodePacked, PorderDt.Item_code, Items.Item_Description, PorderDt.Warehouse_Code, ".
					 " PorderDt_Remark, PorderDt_Quantity, Round(PorderDt_Price, ".$DecimalBase."), PorderDt_PriceBase1, ProderDt_PriceBase2, ".
					 " Round(PorderDt_Discount, ".$DecimalBase."), ".
					 " Round(((PorderDt_Quantity*PorderDt_Price)-((PorderDt_Quantity*PorderDt_Price*ifNull(PorderDt_Discount,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(PorderDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((PorderDt_Quantity*PorderDt_PriceTTC)-((PorderDt_Quantity*PorderDt_PriceTTC*ifNull(PorderDt_Discount,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " PorderDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, PorderDt.Project_code, PorderDt.CostCent_Code,  ".
					 " PrequestDt.PRqstDt_Quantity, Prequest.PRqst_Reference, PorderDt.RelatedDt_Id,PorderDt_heatNo ".
					 " From Temp_PorderDt As PorderDt Left Join Items on PorderDt.Item_code = Items.Item_code ".
					 " Left Join PrequestDt on PrequestDt.PRqstDt_ID = PorderDt.RelatedDt_Id  ".
					 " Left Join Prequest on Prequest.PRqst_ID = PrequestDt.PRqst_ID  ".
					 " Where PorderDt.Porder_ID = ".$PPorId.
					 " Group By  PorderDt.PorderDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				$porderdt_heatno[$i] = $rowsdt[22];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
								
				$vRequestDtQty  = $rowsdt[19];
				$vRequestDtId   = $rowsdt[21];
				
				if (!empty($vRequestDtId) && $vRequestDtId != '') {
				
					$sub_query1 = " Select Sum(ifNull(PorderDt.PorderDt_Quantity,0)) As QuantityOrdered ".
								  " From PorderDt ".
								  " Where PorderDt.RelatedDt_ID =  '".$vRequestDtId."' ".
								  " And PorderDt.PorderDt_Id != '".$rowsdt[0]."' ";
					$sub_result1 = selectData($connection, $sub_query1);
					if ($sub_result1[0] == '') $sub_result1[0] = 0;
				
					$sub_query2 = " Select Sum(ifNull(Temp_PorderDt.PorderDt_Quantity,0)) As QuantityOrderedTemp ".
								  " From Temp_PorderDt ".
								  " Where Temp_PorderDt.RelatedDt_ID =  '".$vRequestDtId."' ".
								  " And Temp_PorderDt.PorderDt_Id != '".$rowsdt[0]."' ";
					$sub_result2 = selectData($connection, $sub_query2);
					if ($sub_result2[0] == '') $sub_result2[0] = 0;		  
				
				
					//Get The Remaining Qty
					$vRemainQty = ($vRequestDtQty - ($sub_result1[0] + $sub_result2[0]));
					
				}	
				
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPorId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
				
//Get Last Purchase Price For an Item
				$i_lpurp = 0;
				$sub_query = " SELECT if($PurchaseTTC = 1, PurchaseDt_PriceTTCBase1, PurchaseDt_PriceBase1), ifNull(PurchaseDt_DiscountPer,0), Purchase_Date FROM PurchasesDt ".
							 " WHERE Item_code = '".$rowsdt[2]."' And ifNull(PurchaseDt_Price,0) > 0 ".
							 " Order By Purchase_Date Desc Limit 1 ";
				$sub_result = selectData($connection, $sub_query);
				$i_lpurp = $sub_result[0];
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$i_lpurp = ConvertCurrency($Base1, $txtCurrCode, insertMySQLDate($txtDate), $i_lpurp, $connection);
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$txtLastPurPrice = $i_lpurp;
				$txtLastDiscount = $sub_result[1];
				$txtLastDate     = $sub_result[2];

				
				echo "	<td width='180px;' align='left'>
							<input type='hidden' name='txtPorDtId[$i]' id='pordtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							
							<label><span>
							<input type='button' name='$recno' value='H' style='width:15px;text-align:left;font-size:10px' onclick=openhistory('$rowsdt[2]')>
							<img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Last Pur. Price</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtLastPurPrice,$DecimalBase)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Discount</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtLastDiscount,$DecimalBase)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Date</span></td><td width='100px;' align='right'><span class='style1'>".$txtLastDate."</span></td></tr>
								</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>	
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='porderdt_heatno[$i]' id='porderdt_heatno[$i]' value='".$porderdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				else echo"<input type='hidden' name='porderdt_heatno[$i]' id='porderdt_heatno[$i]' value='".$porderdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				
				
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrequestRef[$i]' id='prequestref[$i]' value='".$rowsdt[20]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtPrequestQty[$i]' id='prequestqty[$i]' value='".$vRemainQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPorDtQty[$i]' id='pordtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcPorDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[15]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtPorDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[14]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPorDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				
				echo"<td width='85px;' align='center'><input type='text' name='txtLastPurPrice[$i]' id='txtLastPurPrice[$i]' value='".$txtLastPurPrice."' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				
				
				if ($PurchaseTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtPorDtPrice[$i]' id='price[$i]' value='".$rowsdt[12]."' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtPorDtPrice[$i]' id='price[$i]' value='".$rowsdt[7]."' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
						
				echo"	<td width='70px;' align='center'><input type='text' name='txtPorDtDiscount[$i]' id='pordtdiscount[$i]' value='".$rowsdt[10]."' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown."  ></td>";
											
				if ($PurchaseTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtPorDtTotal[$i]' id='pordttotal[$i]' value='".$rowsdt[13]."' maxlength='20' onchange='CalcPorDtTotal(2,$DecimalBase,$i);' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtPorDtTotal[$i]' id='pordttotal[$i]' value='".$rowsdt[11]."' maxlength='20' onchange='CalcPorDtTotal(2,$DecimalBase,$i);' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[18]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtPorDtId[$i]' id='pordtid[$i]' value='$txtPurDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
					
					<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedPurItem('getitemlastprice','".$i."','".$txtDate."','".$txtCurrCode."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
					
					<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-1'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
							
					<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						
						
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Last Pur. Price</span></td><td width='100px;' align='right'><input type='text' name='txtLastPurPrice[$i]' id='lastpurprice[$i]' value='$txtLastPurPrice[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Discount</span></td><td width='100px;' align='right'><input type='text' name='txtLastDiscount[$i]' id='lastdiscount[$i]' value='$txtLastDiscount[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Date</span></td><td width='100px;' align='right'><input type='text' name='txtLastDate[$i]' id='lastdate[$i]' value='$txtLastDate[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
							</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
							
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			
			if($Module_Heatno==1)
			echo"  	<td width='210px;' align='center'>
							<input type='text' name='porderdt_heatno[$i]' id='porderdt_heatno[$i]' value='".$porderdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			else echo"<input type='hidden' name='porderdt_heatno[$i]' id='porderdt_heatno[$i]' value='".$porderdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrequestRef[$i]' id='prequestref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtPrequestQty[$i]' id='prequestqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPorDtQty[$i]' id='pordtquantity[$i]' value='$txtPorDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPorDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='$txtPurDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			echo"<td width='85px;' align='center'><input type='text' name='txtLastPurPrice[$i]' id='txtLastPurPrice[$i]' value='".$txtLastPurPrice."' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			
			if ($PurchaseTTC == 1) 
				echo"	<td width='85px;' align='center'><input type='text' name='txtPorDtPrice[$i]' id='price[$i]' value='$txtPorDtPrice[$i]' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			else
				echo"	<td width='85px;' align='center'><input type='text' name='txtPorDtPrice[$i]' id='price[$i]' value='$txtPorDtPrice[$i]' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
						
			echo"	<td width='70px;' align='center'><input type='text' name='txtPorDtDiscount[$i]' id='pordtdiscount[$i]' value='$txtPorDtDiscount[$i]' maxlength='20' onKeyup='CalcPorDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." ></td>";
							
			if ($PurchaseTTC == 1) 
				echo"   <td width='105px;' align='center'><input type='text' name='txtPorDtTotal[$i]' id='pordttotal[$i]' value='$txtPorDtTotal[$i]' maxlength='20' onchange='CalcPorDtTotal(2,$DecimalBase,$i);' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			else
				echo"   <td width='105px;' align='center'><input type='text' name='txtPorDtTotal[$i]' id='pordttotal[$i]' value='$txtPorDtTotal[$i]' maxlength='20' onchange='CalcPorDtTotal(2,$DecimalBase,$i);' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPorId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPorId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}


function ShowPurchaseOrderTotals($connection,$PPorId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$PurchaseTTC = $arrayparams['PurchaseTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$VatApplied = $arrayparams['VatApplied'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	$txtSupplierCode   = $arrayparams['txtSupplierCode'];
	
	if ($PPorId != "") {
	
		$sub_query = " Select Round(if($PurchaseTTC=1,ifNull(Porder_Taxable,0),ifNull(Porder_Taxable,0)), ".$DecimalBase."), Round(ifNull(Porder_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($PurchaseTTC=1,ifNull(Porder_Taxable,0),ifNull(Porder_Taxable,0)) + ifNull(Porder_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Porder_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Porder_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Porder_VatChckbx,0) As Porder_VatChckbx, Round(ifNull(Porder_VAT,0), ".$DecimalBase.") As Porder_VAT, Round(Porder_VATBase1, ".$DecimalBase.") As Porder_VATBase1, ".
					 " Round((if($PurchaseTTC=1,ifNull(Porder_Taxable,0),ifNull(Porder_Taxable,0)) + ifNull(Porder_NoneTaxable,0) + if($PurchaseTTC=1,0,IfNull(Porder_VAT,0)) - IfNull(Porder_DiscountAmount,0) + (ifNull(Porder_Tax1,0) + ifNull(Porder_Tax2,0) + ifNull(Porder_Tax3,0) + ifNull(Porder_Tax4,0) + ifNull(Porder_Tax5,0))), ".$DecimalBase.") As Porder_NetTotal,  ".
					 " Round((ifNull(Porder_Tax1,0) + ifNull(Porder_Tax2,0) + ifNull(Porder_Tax3,0) + ifNull(Porder_Tax4,0) + ifNull(Porder_Tax5,0)), ".$DecimalBase.") As TotalTaxes  ".
					 " From Temp_Porder As Porder ".
					 " Where Porder_ID = ".$PPorId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPorTaxable      = $sub_result[0];
		$txtPorNoneTaxable  = $sub_result[1];
		$txtPorGrossTotal   = $sub_result[2];
		$txtPorDiscount     = $sub_result[3];
		$txtPorDiscountAmt  = $sub_result[4];
		$txtPorVatChk       = $sub_result[5];
		$txtPorVat          = $sub_result[6];
		$txtPorVatBase1     = $sub_result[7];
		$txtPorNetTotal     = $sub_result[8];
		$txtPorTotalTaxes   = $sub_result[9];
		
	} else {
		$sub_query = " Select ifNull(Supplier_VAT,0) From Suppliers Where Ledger_Number = '".$txtSupplierCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtPorVatChk = $sub_result[0];
	}
	
	
	echo"<table border='0' style='table-layout:fixed;' cellspacing=0 valign='top'>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;' valign='top'>";
		
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'>
			<col width=100>
			
				<tr><td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtPorTaxable' value='$txtPorTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtPorNoneTaxable' value='$txtPorNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtPorGrossTotal' value='$txtPorGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  ><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='pordiscount' name='txtPorDiscount' value='$txtPorDiscount' maxlength='5' style='text-align:right; width:32px; height=20px;' onKeyup='CalcPorDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage);'  >&nbsp;<span class='style10'>%</span>
						<input type='text' id='pordiscountamt' name='txtPorDiscountAmt' value='$txtPorDiscountAmt' maxlength='20' style='text-align:right; width:62px; height=20px;' onKeyup='CalcPorDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage);'  ></td>
				</tr>";
					
	echo"		<tr><td valign='center' align='right'  ><span class='style10'>
						<input type='hidden' name='txtPorVatChk' id='txtPorVatChk' value='$txtPorVatChk' size='1'/>
						<input name='ChkPorVat' type='checkbox' id='ChkPorVat' value='1' onClick='SetPorVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$PurchaseTTC);'"; 
						if($txtPorVatChk==1) echo " checked />";  else echo " />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >";
	echo"				<input type='text' id='porvat' name='txtPorVat' value='$txtPorVat' maxlength='20' style='text-align:right; width:120px; height=20px;'  >";
	echo"			</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' ><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtPorTotalTaxes' value='$txtPorTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtPorTotalTaxes' value='$txtPorTotalTaxes'>";
	}
		
	echo "		<tr><td valign='middle' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtPorNetTotal' value='$txtPorNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
					if($txtPorNetTotal<0) echo " color:#FF0000;";
					
				echo" ' readonly></td>
				</tr>";
		  
	echo"	</table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}


function ShowPurchaseDtGrid($connection,$form,$PPurId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_PurchaseCategory = $arrayparams['FI_PurchaseCategory'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
		
	
	$PurchaseTTC = $arrayparams['PurchaseTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	
	
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtSupplierCode = $arrayparams['txtSupplierCode'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	$ShowItemSellingPrice     = $arrayparams['ShowItemSellingPrice'];
	

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($PurchaseTTC == 1)  $PriceLabel = 'Price TTC';	
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");

	if($Module_Heatno==1)
		array_push($columnsarray, "purchasedt_heatno");
		
	array_push($columnsarray, "purdtquantity");	
	
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	array_push($columnsarray, 'purdtvolume');
	array_push($columnsarray, 'price');
	array_push($columnsarray, 'purdtdiscount');
	array_push($columnsarray, 'purdttotal');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "porderref", "porderqty", "purdtquantity", "unit2qty", "purdtquantity", "price", "purdtdiscount", "purdttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "purdtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("PurchasesDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "PurchasesDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1460;
	if ($Module_SerialNo == 1)     $TableWidth += 25;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='185px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		
		if($ShowItemSellingPrice)
		echo"  <th width='105px;' align='left' ><span>Selling Price</span></th>";
		
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
		
	
		echo"  <th width='75px;' align='center'><span>Order Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Order Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		echo"  <th width='105px;' align='center'><span>Volume</span></th>";  
		echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
		echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
		echo"  <th width='105px;' align='center'><span>Total</span></th>";
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPurId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
					   
			$query = " Select PurchasesDt.PurchaseDt_ID, PurchasesDt.Item_CodePacked, PurchasesDt.Item_code, Item_Description, PurchasesDt.Warehouse_Code, ".
					 " Purchasedt_Remark, PurchaseDt_Quantity, PurchaseDt_Volume, Round(PurchaseDt_Price, ".$DecimalBase."), PurchaseDt_PriceBase1, PurchaseDt_PriceBase2, ".
					 " Round(PurchaseDt_DiscountPer, ".$DecimalBase."), PurchaseDt_ChargePer, PurchaseDt_ChargesAmnt, PurchaseDt_ChargesAmntB1, PurchaseDt_ChargesAmntB2,  ".
					 " Round(((PurchaseDt_Quantity*PurchaseDt_Price)-((PurchaseDt_Quantity*PurchaseDt_Price*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(PurchaseDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((PurchaseDt_Quantity*PurchaseDt_PriceTTC)-((PurchaseDt_Quantity*PurchaseDt_PriceTTC*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " PurchasesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, PurchasesDt.Project_code, PurchasesDt.CostCent_Code,  ".
					 " PorderDt.PorderDt_Quantity, Porder.Porder_reference, PurchasesDt.RelatedDt_Id, ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(PurchaseDt_ExpireDate,'%d/%m/%Y') As PurchaseDt_ExpireDate, PurchaseDt_BatchNo, purchasedt_heatno ".
					 " From Temp_PurchasesDt As PurchasesDt Left Join Items on PurchasesDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on PurchasesDt.PurchaseDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PI' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Left Join PorderDt on PorderDt.PorderDt_ID = PurchasesDt.RelatedDt_Id  ".
					 " Left Join Porder on Porder.Porder_ID = PorderDt.Porder_ID  ".
					 " Where PurchasesDt.Purchase_ID = ".$PPurId.
					 " Group By  PurchasesDt.PurchaseDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection) or die(mysql_error());
			
			//echo $Base1;
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " ,Item_SellingPrice1Base1,Item_SellingPrice1Base2 FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				$Item_SellingPrice1Base1 = $sub_result[9];
				$Item_SellingPrice1Base2 = $sub_result[10];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				if($txtCurrCode==$Base1){
					$txtSellingPrice[$i] = $Item_SellingPrice1Base1;
					$vcurr = 1;
					}
				else	{
					$txtSellingPrice[$i] = $Item_SellingPrice1Base2;
					$vcurr = 2;
					}
					
				//Get Last Purchase Price For an Item
				$i_lpurp = 0;
				$sub_query = " SELECT if($PurchaseTTC = 1, PurchaseDt_PriceTTCBase1, PurchaseDt_PriceBase1), ifNull(PurchaseDt_DiscountPer,0), Purchase_Date FROM PurchasesDt ".
							 " WHERE Item_code = '".$rowsdt[2]."' And ifNull(PurchaseDt_Price,0) > 0 ".
							 " And PurchaseDt_ID != '".$rowsdt[0]."' ".
							 " Order By Purchase_Date Desc Limit 1 ";
				$sub_result = selectData($connection, $sub_query);
				$i_lpurp = $sub_result[0];
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$i_lpurp = ConvertCurrency($Base1, $txtCurrCode, insertMySQLDate($txtDate), $i_lpurp, $connection);
				if(!isset($i_lpurp)) $i_lpurp = 0;
				
				$txtLastPurPrice = $i_lpurp;
				$txtLastDiscount = $sub_result[1];
				$txtLastDate     = $sub_result[2];
				
				$vPorderDtQty  = $rowsdt[24];
				$vPorderDtId   = $rowsdt[26];
				$purchasedt_heatno[$i]   = $rowsdt[31];
				
				if (!empty($vPorderDtId) && $vPorderDtId != '') {
				
					$sub_query1 = " Select Sum(if(Journal_Type='PI', ifNull(PurchasesDt.PurchaseDt_Quantity,0), ifNull(PurchasesDt.PurchaseDt_Quantity,0)*(-1))) As QuantityPurchased ".
								  " From PurchasesDt ".
								  " Where PurchasesDt.RelatedDt_ID =  '".$vPorderDtId."' ".
								  " And PurchasesDt.Journal_Type In ('PI','PR') ".
								  " And PurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";
					$sub_result1 = selectData($connection, $sub_query1);
					if ($sub_result1[0] == '') $sub_result1[0] = 0;
				
					$sub_query2 = " Select Sum(ifNull(Temp_PurchasesDt.PurchaseDt_Quantity,0)) As QuantityPurchasedTemp ".
								  " From Temp_PurchasesDt ".
								  " Where Temp_PurchasesDt.RelatedDt_ID =  '".$vPorderDtId."' ".
								  " And Temp_PurchasesDt.Journal_Type = 'PI' ".
								  " And Temp_PurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";
					$sub_result2 = selectData($connection, $sub_query2);
					if ($sub_result2[0] == '') $sub_result2[0] = 0;		  
				
				
					//Get The Remaining Qty
					$vPorRemainQty = ($vPorderDtQty - ($sub_result1[0] + $sub_result2[0]));
					
				}	
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[28] && $rowsdt[27] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[27] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPurId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[27] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."','".$vPorRemainQty."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[27] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='185px;' align='left'>
							<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='button' name='$recno' value='H' style='width:15px;text-align:left;font-size:10px' onclick=openhistory('$rowsdt[2]')>
							
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistItems(1,$i,'$vcurr');\" style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Last Pur. Price</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtLastPurPrice,$DecimalBase)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Discount</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtLastDiscount,$DecimalBase)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Date</span></td><td width='100px;' align='right'><span class='style1'>".$txtLastDate."</span></td></tr>
								</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
						
			if($ShowItemSellingPrice)		
				echo"  	<td width='105px;' align='center'><input type='text' name='txtSellingPrice[$i]' id='txtSellingPrice[$i]' value='$txtSellingPrice[$i]' class='".$ClassE."' style='font-size:12px; width:95px; height:20px;' readonly/>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
			
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				else echo"<input type='hidden' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />";
						
				echo"	<td width='75px;' align='center'><input type='text' name='txtPorderRef[$i]' id='porderref[$i]' value='".$rowsdt[25]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtPorderQty[$i]' id='porderqty[$i]' value='".$vPorRemainQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[21]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcPurDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[20]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[19]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[28]."</span></td>";
				}
				
				echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='".$rowsdt[7]."' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
				if ($PurchaseTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='".$rowsdt[17]."' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
						
				echo"	<td width='70px;' align='center'><input type='text' name='txtPurDtDiscount[$i]' id='purdtdiscount[$i]' value='".$rowsdt[11]."' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown."  ></td>";
											
				if ($PurchaseTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='".$rowsdt[18]."' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='".$rowsdt[16]."' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[22]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[23]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[30], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[29]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		if($txtCurrCode==$Base1){
					$txtSellingPrice[$i] = $Item_SellingPrice1Base1;
					$vcurr = 1;
					}
				else	{
					$txtSellingPrice[$i] = $Item_SellingPrice1Base2;
					$vcurr = 2;
					}
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='185px;' align='left'>
						<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='$txtPurDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedPurItem('getitemlastprice','".$i."','".$txtDate."','".$txtCurrCode."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-1'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistItems(1,$i,'$vcurr');\" style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Last Pur. Price</span></td><td width='100px;' align='right'><input type='text' name='txtLastPurPrice[$i]' id='lastpurprice[$i]' value='$txtLastPurPrice[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Discount</span></td><td width='100px;' align='right'><input type='text' name='txtLastDiscount[$i]' id='lastdiscount[$i]' value='$txtLastDiscount[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Date</span></td><td width='100px;' align='right'><input type='text' name='txtLastDate[$i]' id='lastdate[$i]' value='$txtLastDate[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
							</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
					
			if($ShowItemSellingPrice)		
				echo"  	<td width='105px;' align='center'><input type='text' name='txtSellingPrice[$i]' id='txtSellingPrice[$i]' value='' class='".$ClassE."' style='font-size:12px; width:95px; height:20px;' readonly/>
				</td>";
			
			
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
			else echo"<input type='hidden' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />";
			
			echo"	<td width='75px;' align='center'><input type='text' name='txtPorderRef[$i]' id='porderref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtPorderQty[$i]' id='porderqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='$txtPurDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPurDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='$txtPurDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
			
			echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='$txtPurDtVolume[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
			if ($PurchaseTTC == 1) 
				echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='$txtPurDtPrice[$i]' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			else
				echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='$txtPurDtPrice[$i]' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
						
			echo"	<td width='70px;' align='center'><input type='text' name='txtPurDtDiscount[$i]' id='purdtdiscount[$i]' value='$txtPurDtDiscount[$i]' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." ></td>";
							
			if ($PurchaseTTC == 1) 
				echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='$txtPurDtTotal[$i]' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			else
				echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='$txtPurDtTotal[$i]' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPurId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowPurchaseTotals($connection,$PPurId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$PurchaseTTC = $arrayparams['PurchaseTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['FI_PurchasesVATMod'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	$txtSupplierCode   = $arrayparams['txtSupplierCode'];
	
	if ($PPurId != "") {
	
		$sub_query = " Select Round(if($PurchaseTTC=1,ifNull(Purchase_Taxable,0),ifNull(Purchase_Taxable,0)), ".$DecimalBase."), Round(ifNull(Purchase_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($PurchaseTTC=1,ifNull(Purchase_Taxable,0),ifNull(Purchase_Taxable,0)) + ifNull(Purchase_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Purchase_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Purchase_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Purchase_VatChkbx,0) As Purchase_VatChkbx, Round(ifNull(Purchase_VAT,0), ".$DecimalBase.") As Purchase_VAT, Round(Purchase_VATBase1, ".$DecimalBase.") As Purchase_VATBase1, ".
					 " Round((if($PurchaseTTC=1,ifNull(Purchase_Taxable,0),ifNull(Purchase_Taxable,0)) + ifNull(Purchase_NoneTaxable,0) + if($PurchaseTTC=1,0,IfNull(Purchase_VAT,0)) - IfNull(Purchase_DiscountAmount,0) + (ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0))), ".$DecimalBase.") As Purchase_NetTotal,  ".
					 " Round((ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(ifNull(Purchase_Charges,0), ".$DecimalBase."), Round(ifNull(Purchase_ChargesBase1,0), ".$DecimalBase."), Round(ifNull(Purchase_ChargesBase2,0), ".$DecimalBase.") ".
					 " From Temp_Purchases As Purchases ".
					 " Where Purchase_ID = ".$PPurId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPurTaxable      = $sub_result[0];
		$txtPurNoneTaxable  = $sub_result[1];
		$txtPurGrossTotal   = $sub_result[2];
		$txtPurDiscount     = $sub_result[3];
		$txtPurDiscountAmt  = $sub_result[4];
		$txtPurVatChk       = $sub_result[5];
		$txtPurVat          = $sub_result[6];
		$txtPurVatBase1     = $sub_result[7];
		$txtPurNetTotal     = $sub_result[8];
		$txtPurTotalTaxes   = $sub_result[9];
		
		$txtPurChgAmt   = $sub_result[10];
		$txtPurChgAmtB1 = $sub_result[11];
		$txtPurChgAmtB2 = $sub_result[12];
		
		$txtNetTotalWithCharges = number_format($txtPurNetTotal + $txtPurChgAmt, $DecimalBase,".","");
	} else {
		$sub_query = " Select ifNull(Supplier_VAT,0) From Suppliers Where Ledger_Number = '".$txtSupplierCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtPurVatChk = $sub_result[0];
	}
	
	
	echo"<table border='0' style='table-layout:fixed;' cellspacing=0>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>";
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'><col width=90>
			<col width=140>
				<tr><td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtPurTaxable' value='$txtPurTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtPurNoneTaxable' value='$txtPurNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtPurGrossTotal' value='$txtPurGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  ><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='purdiscount' name='txtPurDiscount' value='$txtPurDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcPurDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage);'  ><span class='style10'>%</span> 
						<input type='text' id='purdiscountamt' name='txtPurDiscountAmt' value='$txtPurDiscountAmt' maxlength='20' style='text-align:right; width:62px; height=20px;' onKeyup='CalcPurDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage);'  ></td>
				</tr>";
				
	
	//If User Has Access To Modify Vat Base1
	$VatModiMode = ' readonly ';
	if ($AllowModifyVAT == 1 && $Base1 == $txtCurr)
		$VatModiMode = '';
					
	echo"		<tr><td valign='center' align='right'  ><span class='style10'>
						<input type='hidden' name='txtPurVatChk' id='txtPurVatChk' value='$txtPurVatChk' size='1'/>
						<input name='chkPurVat' type='checkbox' id='chkPurVat' value='1' onClick='SetPurVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$PurchaseTTC);'"; 
						if($txtPurVatChk==1) echo " checked />";  else echo " />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >";
	echo"				<input type='text' id='purvat' name='txtPurVat' value='$txtPurVat' maxlength='20' style='text-align:right; width:120px; height=20px;' $VatModiMode >";
	
					//If User Has Access To Modify Vat Base1
					if ($AllowModifyVAT == 1){
						echo "<br><input type='text' id='purvatbase1' name='txtPurVatBase1' value='$txtPurVatBase1' maxlength='20' style='text-align:right; width:120px; height=20px;' >";
					} else {
						echo "<br><input type='hidden' id='purvatbase1' name='txtPurVatBase1' value='$txtPurVatBase1'  >";
					}
				
	echo"			</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' ><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtPurTotalTaxes' value='$txtPurTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtPurTotalTaxes' value='$txtPurTotalTaxes'>";
	}
						
	echo "		<tr><td valign='middle' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtPurNetTotal' value='$txtPurNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";

	echo" 	    <tr><td valign='middle' align='right' ><span class='style10'>NetTotal + Charges</span>&nbsp;</td> 
					<td align='left'><input type='hidden' id='chargeamt' name='txtPurChgAmt' value='$txtPurChgAmt'>
						<input type='text' id='totalcharges' name='txtNetTotalWithCharges' value='$txtNetTotalWithCharges' maxlength='20' style='text-align:right; width:120px; height=20px; height=20px;background:#C6D4E5;' readonly>
					</td>
				</tr>";
		  
	echo"	</table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";

}


function ShowReturnPurchaseDtGrid($connection,$form,$PPurId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_PurchaseCategory = $arrayparams['FI_PurchaseCategory'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
		
	
	$PurchaseTTC = $arrayparams['PurchaseTTC'];
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtSupplierCode = $arrayparams['txtSupplierCode'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	$PriceLabel = 'Price';	
	if ($PurchaseTTC == 1)  $PriceLabel = 'Price TTC';	
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
		
	array_push($columnsarray, "purdtquantity");
	
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	array_push($columnsarray, 'purdtvolume');
	array_push($columnsarray, 'price');
	array_push($columnsarray, 'purdtdiscount');
	array_push($columnsarray, 'purdttotal');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "purref", "purqty", "purdtquantity", "unit2qty", "purdtquantity", "price", "purdtdiscount", "purdttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "purdtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("PurchasesDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "PurchasesDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1440;
	if ($Module_SerialNo == 1)     $TableWidth += 25;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='145px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.ProductionDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
	
		echo"  <th width='75px;' align='center'><span>Pur. Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Pur. Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		echo"  <th width='105px;' align='center'><span>Volume</span></th>";  
		echo"  <th width='85px;' align='center'><span>".$PriceLabel."</span></th>";
		echo"  <th width='70px;' align='center'><span>Disc(%)</span></th>";
		echo"  <th width='105px;' align='center'><span>Total</span></th>";
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPurId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
					   
			$query = " Select PurchasesDt.PurchaseDt_ID, PurchasesDt.Item_CodePacked, PurchasesDt.Item_code, Item_Description, PurchasesDt.Warehouse_Code, ".
					 " Purchasedt_Remark, PurchaseDt_Quantity, PurchaseDt_Volume, Round(PurchaseDt_Price, ".$DecimalBase."), PurchaseDt_PriceBase1, PurchaseDt_PriceBase2, ".
					 " Round(PurchaseDt_DiscountPer, ".$DecimalBase."), PurchaseDt_ChargePer, PurchaseDt_ChargesAmnt, PurchaseDt_ChargesAmntB1, PurchaseDt_ChargesAmntB2,  ".
					 " Round(((PurchaseDt_Quantity*PurchaseDt_Price)-((PurchaseDt_Quantity*PurchaseDt_Price*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(PurchaseDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((PurchaseDt_Quantity*PurchaseDt_PriceTTC)-((PurchaseDt_Quantity*PurchaseDt_PriceTTC*ifNull(PurchaseDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " PurchasesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, PurchasesDt.Project_code, PurchasesDt.CostCent_Code,  ".
					 " PurchasesDt.Related_Ref, PurchasesDt.RelatedDt_Id, ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(PurchaseDt_ExpireDate,'%d/%m/%Y') As PurchaseDt_ExpireDate, PurchaseDt_BatchNo, PurchaseDt_heatNo ".
					 " From Temp_PurchasesDt As PurchasesDt Left Join Items on PurchasesDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on PurchasesDt.PurchaseDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PR' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Where PurchasesDt.Purchase_ID = ".$PPurId.
					 " Group By  PurchasesDt.PurchaseDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				$purchasedt_heatno[$i] = $rowsdt[30];
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
				$vRelatedDtId  = $rowsdt[25];
				
				if (!empty($vRelatedDtId) && $vRelatedDtId != '') {
					$sub_query = " Select ifNull(PurchasesDt.PurchaseDt_Quantity,0) - ifNull(Sum(ifNull(ReturnPurchasesDt.PurchaseDt_Quantity,0)),0) ".
								 " From PurchasesDt, PurchasesDt As ReturnPurchasesDt ".
								 " Where PurchasesDt.PurchaseDt_ID = '".$vRelatedDtId."' ".
								 " And PurchasesDt.Journal_Type = 'PI' ".
								 " And ReturnPurchasesDt.Journal_Type = 'PR' ".
								 " And ReturnPurchasesDt.RelatedDt_Id = PurchasesDt.PurchaseDt_ID ".
								 " And ReturnPurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";

					$sub_result = selectData($connection, $sub_query);
					if ($sub_result[0] == '') $sub_result[0] = 0;	
					
					$sub_query2 = " Select Sum(ifNull(Temp_PurchasesDt.PurchaseDt_Quantity,0)) As QuantityReturnedTemp ".
								  " From Temp_PurchasesDt ".
								  " Where Temp_PurchasesDt.RelatedDt_ID =  '".$vRelatedDtId."' ".
								  " And Temp_PurchasesDt.Journal_Type = 'PR' ".
								  " And Temp_PurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";
					$sub_result2 = selectData($connection, $sub_query2);
					if ($sub_result2[0] == '') $sub_result2[0] = 0;		

					$vRemainQty = $sub_result[0] - $sub_result2[0];
					if ($vRemainQty == '') $vRemainQty = 0;
				
				}	
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[27] && $rowsdt[26] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[26] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0) ) {
					$RecStyle = "style='background-color:#FF0000;'";
				}				
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPurId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[26] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."','".$vRemainQty."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[26] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='145px;' align='left'>
							<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";				
				echo "<input type='hidden' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				echo"	<td width='75px;' align='center'><input type='text' name='txtPurRef[$i]' id='purref[$i]' value='".$rowsdt[24]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtPurQty[$i]' id='purqty[$i]' value='".$vRemainQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[21]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcPurDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[20]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[19]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[27]."</span></td>";
				}
				
				echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='".$rowsdt[7]."' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
				if ($PurchaseTTC == 1) 
					echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='".$rowsdt[17]."' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				else
					echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
						
				echo"	<td width='70px;' align='center'><input type='text' name='txtPurDtDiscount[$i]' id='purdtdiscount[$i]' value='".$rowsdt[11]."' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown."  ></td>";
											
				if ($PurchaseTTC == 1) 
					echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='".$rowsdt[18]."' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				else
					echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='".$rowsdt[16]."' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[22]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[23]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[29], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[28]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='145px;' align='left'>
						<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='$txtPurDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-1'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";						
			else echo"<input type='hidden' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";			
						
			echo"	<td width='75px;' align='center'><input type='text' name='txtPurRef[$i]' id='purref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtPurQty[$i]' id='purqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='$txtPurDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPurDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='$txtPurDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
			
			echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='$txtPurDtVolume[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
			if ($PurchaseTTC == 1) 
				echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='$txtPurDtPrice[$i]' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			else
				echo"	<td width='85px;' align='center'><input type='text' name='txtPurDtPrice[$i]' id='price[$i]' value='$txtPurDtPrice[$i]' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
						
			echo"	<td width='70px;' align='center'><input type='text' name='txtPurDtDiscount[$i]' id='purdtdiscount[$i]' value='$txtPurDtDiscount[$i]' maxlength='20' onKeyup='CalcPurDtTotal(1,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:65px; height:20px;' ".$KeyDown." ></td>";
							
			if ($PurchaseTTC == 1) 
				echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='$txtPurDtTotal[$i]' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			else
				echo"   <td width='105px;' align='center'><input type='text' name='txtPurDtTotal[$i]' id='purdttotal[$i]' value='$txtPurDtTotal[$i]' maxlength='20' onchange='CalcPurDtTotal(2,$DecimalBase,$i);' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPurId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowReturnPurchaseTotals($connection,$PPurId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$PurchaseTTC = $arrayparams['PurchaseTTC'];
	$Setup_VatPercentage = $arrayparams['Setup_VatPercentage'];
	$Module_AdditionalTaxes = $arrayparams['Module_AdditionalTaxes'];
	$VatApplied = $arrayparams['VatApplied'];
	$AllowModifyVAT = $arrayparams['FI_PurchasesVATMod'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	$txtSupplierCode   = $arrayparams['txtSupplierCode'];
	
	if ($PPurId != "") {
	
		$sub_query = " Select Round(if($PurchaseTTC=1,ifNull(Purchase_Taxable,0),ifNull(Purchase_Taxable,0)), ".$DecimalBase."), Round(ifNull(Purchase_NoneTaxable,0), ".$DecimalBase."), ".
					 " Round(if($PurchaseTTC=1,ifNull(Purchase_Taxable,0),ifNull(Purchase_Taxable,0)) + ifNull(Purchase_NoneTaxable,0), ".$DecimalBase.") As GrossTotal, ".
					 " Round(ifNull(Purchase_DiscountPer,0), ".$DecimalBase."), Round(ifNull(Purchase_DiscountAmount,0), ".$DecimalBase."),  ".
					 " ifNull(Purchase_VatChkbx,0) As Purchase_VatChkbx, Round(ifNull(Purchase_VAT,0), ".$DecimalBase.") As Purchase_VAT, Round(Purchase_VATBase1, ".$DecimalBase.") As Purchase_VATBase1, ".
					 " Round((if($PurchaseTTC=1,ifNull(Purchase_Taxable,0),ifNull(Purchase_Taxable,0)) + ifNull(Purchase_NoneTaxable,0) + if($PurchaseTTC=1,0,IfNull(Purchase_VAT,0)) - IfNull(Purchase_DiscountAmount,0) + (ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0))), ".$DecimalBase.") As Purchase_NetTotal,  ".
					 " Round((ifNull(Purchase_Tax1,0) + ifNull(Purchase_Tax2,0) + ifNull(Purchase_Tax3,0) + ifNull(Purchase_Tax4,0) + ifNull(Purchase_Tax5,0)), ".$DecimalBase.") As TotalTaxes,  ".
					 " Round(ifNull(Purchase_Charges,0), ".$DecimalBase."), Round(ifNull(Purchase_ChargesBase1,0), ".$DecimalBase."), Round(ifNull(Purchase_ChargesBase2,0), ".$DecimalBase.") ".
					 " From Temp_Purchases As Purchases ".
					 " Where Purchase_ID = ".$PPurId;			 
				 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPurTaxable      = $sub_result[0];
		$txtPurNoneTaxable  = $sub_result[1];
		$txtPurGrossTotal   = $sub_result[2];
		$txtPurDiscount     = $sub_result[3];
		$txtPurDiscountAmt  = $sub_result[4];
		$txtPurVatChk       = $sub_result[5];
		$txtPurVat          = $sub_result[6];
		$txtPurVatBase1     = $sub_result[7];
		$txtPurNetTotal     = $sub_result[8];
		$txtPurTotalTaxes   = $sub_result[9];
		
		$txtPurChgAmt   = $sub_result[10];
		$txtPurChgAmtB1 = $sub_result[11];
		$txtPurChgAmtB2 = $sub_result[12];
		
		$txtNetTotalWithCharges = number_format($txtPurNetTotal + $txtPurChgAmt, $DecimalBase,".","");
	} else {
		$sub_query = " Select ifNull(Supplier_VAT,0) From Suppliers Where Ledger_Number = '".$txtSupplierCode."'";			 
		$sub_result = selectData($connection, $sub_query);
		$txtPurVatChk = $sub_result[0];
	}
	
	echo"<table border='0' style='table-layout:fixed;' cellspacing=0>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>
		";
	
	echo"	<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'>
			<col width=90>
			<col width=140>
				<tr><td valign='center' align='right' ><span class='style10'>Taxable</span>&nbsp;</td>
					<td align='left' ><input type='text' id='taxable'  name='txtPurTaxable' value='$txtPurTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>None Taxable</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nontaxable' name='txtPurNoneTaxable' value='$txtPurNoneTaxable' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' ><span class='style10'>Gross Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='grosstotal' name='txtPurGrossTotal' value='$txtPurGrossTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>	
				<tr><td valign='center' align='right'  ><span class='style10'>Discount</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='purdiscount' name='txtPurDiscount' value='$txtPurDiscount' maxlength='5' style='text-align:right; width:38px; height=20px;' onKeyup='CalcPurDiscount(1, $DecimalBase,$VatApplied,$Setup_VatPercentage);'  ><span class='style10'> %</span>
						<input type='text' id='purdiscountamt' name='txtPurDiscountAmt' value='$txtPurDiscountAmt' maxlength='20' style='text-align:right; width:58px; height=20px;' onKeyup='CalcPurDiscount(2, $DecimalBase,$VatApplied,$Setup_VatPercentage);'  ></td>
				</tr>";
				
	
	//If User Has Access To Modify Vat Base1
	$VatModiMode = ' readonly ';
	if ($AllowModifyVAT == 1 && $Base1 == $txtCurr)
		$VatModiMode = '';
					
	echo"		<tr><td valign='center' align='right'  ><span class='style10'>
						<input type='hidden' name='txtPurVatChk' id='txtPurVatChk' value='$txtPurVatChk' size='1'/>
						<input name='chkPurVat' type='checkbox' id='chkPurVat' value='1' onClick='SetPurVat($DecimalBase,$VatApplied,$Setup_VatPercentage,$PurchaseTTC);'"; 
						if($txtPurVatChk==1) echo " checked />";  else echo " />"; 
				  
	echo "				VAT $Setup_VatPercentage%&nbsp;</span>
					</td> 
					<td align='left' >";
	echo"				<input type='text' id='purvat' name='txtPurVat' value='$txtPurVat' maxlength='20' style='text-align:right; width:120px; height=20px;' $VatModiMode >";
	
					//If User Has Access To Modify Vat Base1
					if ($AllowModifyVAT == 1){
						echo "<br><input type='text' id='purvatbase1' name='txtPurVatBase1' value='$txtPurVatBase1' maxlength='20' style='text-align:right; width:120px; height=20px;' >";
					} else {
						echo "<br><input type='hidden' id='purvatbase1' name='txtPurVatBase1' value='$txtPurVatBase1'  >";
					}
				
	echo"			</td>
				</tr>";

	if ($Module_AdditionalTaxes == 1) {
		echo"	<tr>
					<td valign='center' align='right' ><span class='style10'>Taxes</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='tax' name='txtPurTotalTaxes' value='$txtPurTotalTaxes' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>";
	} else {
		echo "	<input type='hidden' name='txtPurTotalTaxes' value='$txtPurTotalTaxes'>";
	}
				
	echo "		<tr><td valign='middle' align='right' ><span class='style10'>Net Total</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='nettotal' name='txtPurNetTotal' value='$txtPurNetTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
					
					if($txtPurNetTotal<0) echo " color:#FF0000;";
					
					echo " ' readonly></td>";
				echo"</tr>";

	echo"	</table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}



function ShowGoodRecNoteDtGrid($connection,$form,$PPurId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_PurchaseCategory = $arrayparams['FI_PurchaseCategory'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
		
	
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtSupplierCode = $arrayparams['txtSupplierCode'];
	$PurType         = $arrayparams['PurType'];

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");

	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
	

	array_push($columnsarray, "purdtquantity");
	
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	//array_push($columnsarray, 'purdtvolume');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode","", "porderref", "porderqty", "purdtquantity", "unit2qty", "purdtquantity", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "purdtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemexpire", "itemunitcoef", "itemcodeFG");
	$orderbyarray = array("GrnPurchasesDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "GrnPurchasesDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1075;
	if ($Module_SerialNo == 1)     $TableWidth += 25;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($PurType == 'RM')          $TableWidth += 105;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='145px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		
		if ($PurType == 'RM')
			echo"  <th width='105px;' align='center'><span>Item Code Finished</span></th> ";
			
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
	
	if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPorId."', 'datagrid', 1, '".$dftorderby."', 'COR','PorderDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
		
		echo"  <th width='75px;' align='center'><span>Order Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. Order Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		//echo"  <th width='105px;' align='center'><span>Volume</span></th>";  
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPurId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
						   
			$query = " Select GrnPurchasesDt.PurchaseDt_ID, GrnPurchasesDt.Item_CodePacked, GrnPurchasesDt.Item_code, Item_Description, GrnPurchasesDt.Warehouse_Code, ".
					 " Purchasedt_Remark, PurchaseDt_Quantity, PurchaseDt_Volume, GrnPurchasesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, GrnPurchasesDt.Project_code, ".
					 " GrnPurchasesDt.CostCent_Code,  PorderDt.PorderDt_Quantity, Porder.Porder_reference, GrnPurchasesDt.RelatedDt_Id, GrnPurchasesDt.Item_CodeFinished, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  Date_Format(PurchaseDt_ExpireDate,'%d/%m/%Y') As PurchaseDt_ExpireDate, PurchaseDt_BatchNo,PurchaseDt_heatNo ".
					 " From Temp_GrnPurchasesDt As GrnPurchasesDt Left Join Items on GrnPurchasesDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on GrnPurchasesDt.PurchaseDt_ID = Temp_Purchasesserialno.GrnDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PI' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Left Join PorderDt on PorderDt.PorderDt_ID = GrnPurchasesDt.RelatedDt_Id  ".
					 " Left Join Porder on Porder.Porder_ID = PorderDt.Porder_ID  ".
					 " Where GrnPurchasesDt.Purchase_ID = ".$PPurId.
					 " Group By  GrnPurchasesDt.PurchaseDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
				$vPorderDtQty  = $rowsdt[13];
				$vPorderDtId   = $rowsdt[15];
				$purchasedt_heatno[$i]   = $rowsdt[21];
				
				if (!empty($vPorderDtId) && $vPorderDtId != '') {
				
					$sub_query1 = " Select Sum(if(Journal_Type='PI', ifNull(GrnPurchasesDt.PurchaseDt_Quantity,0), ifNull(GrnPurchasesDt.PurchaseDt_Quantity,0)*(-1))) As QuantityPurchased ".
								  " From GrnPurchasesDt ".
								  " Where GrnPurchasesDt.RelatedDt_ID =  '".$vPorderDtId."' ".
								  " And GrnPurchasesDt.Journal_Type In ('PI','PR') ".
								  " And GrnPurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";
					$sub_result1 = selectData($connection, $sub_query1);
					if ($sub_result1[0] == '') $sub_result1[0] = 0;
				
					$sub_query2 = " Select Sum(ifNull(Temp_GrnPurchasesDt.PurchaseDt_Quantity,0)) As QuantityPurchasedTemp ".
								  " From Temp_GrnPurchasesDt ".
								  " Where Temp_GrnPurchasesDt.RelatedDt_ID =  '".$vPorderDtId."' ".
								  " And Temp_GrnPurchasesDt.Journal_Type = 'PI' ".
								  " And Temp_GrnPurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";
					$sub_result2 = selectData($connection, $sub_query2);
					if ($sub_result2[0] == '') $sub_result2[0] = 0;		  
									
					//Get The Remaining Qty
					$vPorRemainQty = ($vPorderDtQty - ($sub_result1[0] + $sub_result2[0]));
					
				}	
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[18] && $rowsdt[17] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[17] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
				
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPurId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[17] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."','".$vPorRemainQty."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[17] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='20px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='145px;' align='left'>
							<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
						
				if ($PurType == 'RM')
					echo"	<td width='105px;' align='center'><input type='text' name='txtItemCodeFG[$i]' id='itemcodeFG[$i]' value='".$rowsdt[16]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' readonly></td>";
					
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";

				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				else echo"<input type='hidden' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
				
				echo"	<td width='75px;' align='center'><input type='text' name='txtPorderRef[$i]' id='porderref[$i]' value='".$rowsdt[14]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtPorderQty[$i]' id='porderqty[$i]' value='".$vPorRemainQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[10]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[9]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[8]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[18]."</span></td>";
				}
				
				//echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='".$rowsdt[7]."' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[11]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[12]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[20], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[19]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='145px;' align='left'>
						<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='$txtPurDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-1'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
					
			if ($PurType == 'RM')
				echo"	<td width='105px;' align='center'><input type='text' name='txtItemCodeFG[$i]' id='itemcodeFG[$i]' value='$txtItemCodeFG[$i]' maxlength='20' class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' readonly></td>";
					
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
			else echo"<input type='hidden' name='purchasedt_heatno[$i]' id='purchasedt_heatno[$i]' value='".$purchasedt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			echo"	<td width='75px;' align='center'><input type='text' name='txtPorderRef[$i]' id='porderref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtPorderQty[$i]' id='porderqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='$txtPurDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='$txtPurDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
			
			//echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='$txtPurDtVolume[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPurId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}


function ShowReturnGoodRecNoteDtGrid($connection,$form,$PPurId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_PurchaseCategory = $arrayparams['FI_PurchaseCategory'];
	
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
		
	
	$DecimalBase = $arrayparams['DecimalBase'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtSupplierCode = $arrayparams['txtSupplierCode'];
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode", "purdtquantity");
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	//array_push($columnsarray, 'purdtvolume');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "grnref", "grnqty", "purdtquantity", "unit2qty", "purdtquantity", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "purdtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("GrnPurchasesDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "GrnPurchasesDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1075;
	if ($Module_SerialNo == 1)     $TableWidth += 25;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='145px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','PurchasesDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		echo"  <th width='75px;' align='center'><span>GRN Ref</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Rem. GRN Qty</span></th>";	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPurId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		//echo"  <th width='105px;' align='center'><span>Volume</span></th>";  
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPurId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
						   
			$query = " Select GrnPurchasesDt.PurchaseDt_ID, GrnPurchasesDt.Item_CodePacked, GrnPurchasesDt.Item_code, Item_Description, GrnPurchasesDt.Warehouse_Code, ".
					 " Purchasedt_Remark, PurchaseDt_Quantity, PurchaseDt_Volume, GrnPurchasesDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, GrnPurchasesDt.Project_code, ".
					 " GrnPurchasesDt.CostCent_Code, GrnPurchasesDt.Related_Ref, GrnPurchasesDt.RelatedDt_Id, GrnPurchasesDt.Item_CodeFinished, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  Date_Format(PurchaseDt_ExpireDate,'%d/%m/%Y') As PurchaseDt_ExpireDate, PurchaseDt_BatchNo ".
					 " From Temp_GrnPurchasesDt As GrnPurchasesDt Left Join Items on GrnPurchasesDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on GrnPurchasesDt.PurchaseDt_ID = Temp_Purchasesserialno.GrnDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PR' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Where GrnPurchasesDt.Purchase_ID = ".$PPurId.
					 " Group By  GrnPurchasesDt.PurchaseDt_ID ".
					 " Order By $orderby $direction ";
				
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
				
				$vRelatedDtId  = $rowsdt[14];
				
				if (!empty($vRelatedDtId) && $vRelatedDtId != '') {
					$sub_query = " Select ifNull(GrnPurchasesDt.PurchaseDt_Quantity,0) - ifNull(Sum(ifNull(ReturnGrnPurchasesDt.PurchaseDt_Quantity,0)),0) ".
								 " From GrnPurchasesDt ".
								 " Left Join GrnPurchasesDt As ReturnGrnPurchasesDt on ReturnGrnPurchasesDt.RelatedDt_Id = GrnPurchasesDt.PurchaseDt_ID And ReturnGrnPurchasesDt.Journal_Type = 'PR' ".
								 " Where GrnPurchasesDt.PurchaseDt_ID = '".$vRelatedDtId."' ".
								 " And GrnPurchasesDt.Journal_Type = 'PI' ";
								 //" And ReturnGrnPurchasesDt.Purchase_Id != '".$PPurId."' ";
								 //" And ReturnGrnPurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";

					$sub_result = selectData($connection, $sub_query);
					if ($sub_result[0] == '') $sub_result[0] = 0;	
					
					$sub_query2 = " Select Sum(ifNull(Temp_GrnPurchasesDt.PurchaseDt_Quantity,0)) As QuantityPurchasedTemp ".
								  " From Temp_GrnPurchasesDt ".
								  " Where Temp_GrnPurchasesDt.RelatedDt_ID =  '".$vRelatedDtId."' ".
								  " And Temp_GrnPurchasesDt.Journal_Type = 'PR' ".
								  " And Temp_GrnPurchasesDt.PurchaseDt_Id != '".$rowsdt[0]."' ";
					$sub_result2 = selectData($connection, $sub_query2);
					if ($sub_result2[0] == '') $sub_result2[0] = 0;		

					$vRemainQty = $sub_result[0] - $sub_result2[0];
					if ($vRemainQty == '') $vRemainQty = 0;
				
				}	
			
								
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[17] && $rowsdt[16] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[16] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
				
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPurId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[16] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".$vRemainQty."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[16] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='145px;' align='left'>
							<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
						
					
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				echo"	<td width='75px;' align='center'><input type='text' name='txtGrnRef[$i]' id='grnref[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
				echo"   <td width='75px;' align='center'><input type='text' name='txtGrnQty[$i]' id='grnqty[$i]' value='".$vRemainQty."' maxlength='20' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[10]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[9]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[8]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[17]."</span></td>";
				}
				
				//echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='".$rowsdt[7]."' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[11]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[12]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[19], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[18]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		/*
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='145px;' align='left'>
						<input type='hidden' name='txtPurDtId[$i]' id='purdtid[$i]' value='$txtPurDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-1'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
					
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
			echo"	<td width='75px;' align='center'><input type='text' name='txtGrnRef[$i]' id='grnref[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly></td>";
			echo"   <td width='75px;' align='center'><input type='text' name='txtGrnQty[$i]' id='grnqty[$i]' value='' maxlength='20' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' readonly  ></td>";
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPurDtQty[$i]' id='purdtquantity[$i]' value='$txtPurDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='$txtPurDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtPurDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
			
			echo"	<td width='105px;' align='center'><input type='text' name='txtPurDtVolume[$i]' id='purdtvolume[$i]' value='$txtPurDtVolume[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown."  ></td>";
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,$FI_PurchaseCategory); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPurId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$FI_PurchaseCategory); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}*/
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPurId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}


function ShowAssembleDtGrid($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	$DisableIdeal    = $arrayparams['DisableIdeal'];
	
	
	$Module_Heatno = $arrayparams['Module_Heatno'];

	if ($PPrdId != '' ) {
		$sub_query = " Select Date_Format(Production_Date,'%d/%m/%Y') As Production_Date, Production.Currency_Code ".
					 " From Temp_Production As Production ".
					 " Where Production_ID = ".$PPrdId;
		$sub_result = selectData($connection, $sub_query);
		$txtDate 	 = $sub_result[0];
		$txtCurrCode = $sub_result[1];
	}
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 100;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
		
	array_push($columnsarray, "prddtquantity");
	
	
		
	if ($DisableIdeal == 0) array_push($columnsarray, 'prddtwaste');
	array_push($columnsarray, 'costprice');
	array_push($columnsarray, 'prddttotal');
	if ($DisableIdeal == 0) array_push($columnsarray, 'prddttotalideal');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "prddtquantity", "prddtidealqty", "prddtwastecalc", "prddtwaste", "costprice", "prddttotal", "prddttotalideal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "prddtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("ProductionDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProductionDt.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1160;
	//if ($Module_SerialNo == 1)     $TableWidth += 20;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($DisableIdeal == 0)	       $TableWidth += 330;
	//if ($Module_SerialNo == 1)     $TableWidth += 50;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 170;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		/*if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}*/	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.ProductionDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Actual Qty</span></th> ";	
		  
		if ($DisableIdeal == 0) {
			echo"  <th width='75px;' align='center'><span>Ideal Qty</span></th>";
			echo"  <th width='75px;' align='center'><span>Waste</span></th>";
			echo"  <th width='75px;' align='center'><span>Actual Waste</span></th>";
		}
		
		/*if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}*/
		
		echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
		echo"  <th width='105px;' align='center'><span>Actual Total</span></th>";
		
		if ($DisableIdeal == 0) {
			echo"  <th width='105px;' align='center'><span>Ideal Total</span></th>";
		}
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPrdId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
			$query = " Select ProductionDt.ProductionDt_ID, ProductionDt.Item_CodePacked, ProductionDt.Item_code, Item_Description, ProductionDt.Warehouse_Code, ".
					 " ProductionDt_Remark, ProductionDt_Quantity, ProductionDt_IdealQty, Round(ProductionDt_Cost, ".$DecimalBase."), ProductionDt_CostBase1, ProductionDt_CostBase2, ".
					 " ifNull(ProductionDt_IdealQty,0) - ifNull(ProductionDt_Quantity,0) As ProductionDtWasteCalc, ProductionDt_Waste, ".
					 " Round((ProductionDt_Quantity*ProductionDt_Cost), ".$DecimalBase.") As TotalActual, ".
					 " Round((ProductionDt_IdealQty*ProductionDt_Cost), ".$DecimalBase.") As TotalIdeal, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(Temp_Purchasesserialno.SerialNo_Code) As CountSerialNo, Item_UnitSalesDesc, ".
					 " Date_Format(ProductionDt_ExpireDate,'%d/%m/%Y') As ProductionDt_ExpireDate, ProductionDt_BatchNo, ProductionDt.Project_code, ".
					 " ProductionDt.CostCenter_Code, ifNull(ProductionDt_Checked,1) as ProductionDt_Checked, productionDt_heatNo ".
					 " From Temp_ProductionDt As ProductionDt Left Join Items on ProductionDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on ProductionDt.ProductionDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PD' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Where ProductionDt.Production_ID = ".$PPrdId.
					 " Group By  ProductionDt.ProductionDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$productiondt_heatno[$i] = $rowsdt[23];
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
							
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[16] && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				$SelRecord  = 'R'.$k;
				$$SelRecord = $rowsdt[0];
			   
				echo " 	<td width='25px;' align='center'><input type='checkbox' name='$SelRecord' id='$SelRecord' value='".$$SelRecord."' onclick='UnSelectRecord();' ";
				if ($rowsdt[22] == 1) echo "checked /></td>"; else echo "/></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				/*if ($rowsdt[15] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[15] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}*/
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(DeliveryDt_Quantity,0)),0) FROM Temp_Deliverydt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Delivery_ID = $PSaId And DeliveryDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_DI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_DI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_DI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='185px;' align='left'>
							<input type='hidden' name='txtPrdDtId[$i]' id='prddtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); GetItemCost(this.id,'costprice[$i]','getitemcost','".$i."','".$txtDate."','".$txtCurrCode."'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}		
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
							
							
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";

				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else echo"<input type='hidden' name='<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				
				
				
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[17]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQty[$i]' id='prddtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPrdDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
					
				if ($DisableIdeal == 0) {
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='".$rowsdt[11]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='".$rowsdt[12]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='' >";
				}
				
				
				
				/*if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				}*/
				
				
				echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPrice[$i]' id='costprice[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotal[$i]' id='prddttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($DisableIdeal == 0) {
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdeal[$i]' id='prddttotalideal[$i]' value='".$rowsdt[14]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				}	
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[20]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[21]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[19], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[18]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		echo"<input type='hidden' name='ChkCounter' id='ChkCounter' value='$k'>";
							
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor' table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			echo"   <td width='25px;' align='center'><span>&nbsp;</span></td>"; 
			
			/*if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			}*/ 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtPrdDtId[$i]' id='prddtid[$i]' value='$txtPrdDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-2'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"   ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); \" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)
			echo"  	<td width='210px;' align='center'>
							<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
					</td>";		
			else 
			echo"<input type='hidden' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQty[$i]' id='prddtquantity[$i]' value='$txtPrdDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPrdDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." ></td>";
					
			if ($DisableIdeal == 0) {
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='$txtPrdDtQtyIdeal[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='$txtPrdDtWasteCalc[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='$txtPrdDtWaste[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			} else {
				echo"  <input type='hidden' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='' >";
			}	
		  
			
			/*if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}*/
			
			echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPrice[$i]' id='costprice[$i]' value='$txtPrdDtCostPrice[$i]' maxlength='20' onKeyup='CalcPrdDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotal[$i]' id='prddttotal[$i]' value='$txtPrdDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
			if ($DisableIdeal == 0) {
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdeal[$i]' id='prddttotalideal[$i]' value='$txtPrdDtTotalIdeal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			}	
				
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPrdId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowPreAssembleDtGrid($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	$DisableIdeal    = $arrayparams['DisableIdeal'];
	
	
	$Module_Heatno = $arrayparams['Module_Heatno'];

	if ($PPrdId != '' ) {
		$sub_query = " Select Date_Format(Production_Date,'%d/%m/%Y') As Production_Date, Production.Currency_Code ".
					 " From Temp_PreProduction As Production ".
					 " Where Production_ID = ".$PPrdId;
		$sub_result = selectData($connection, $sub_query);
		$txtDate 	 = $sub_result[0];
		$txtCurrCode = $sub_result[1];
	}
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 100;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
		
	array_push($columnsarray, "prddtquantity");
	
	
		
	if ($DisableIdeal == 0) array_push($columnsarray, 'prddtwaste');
	array_push($columnsarray, 'costprice');
	array_push($columnsarray, 'prddttotal');
	if ($DisableIdeal == 0) array_push($columnsarray, 'prddttotalideal');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	array_push($columnsarray, 'remarkdt');
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "prddtquantity", "prddtidealqty", "prddtwastecalc", "prddtwaste", "costprice", "prddttotal", "prddttotalideal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "remarkdt", "prddtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("ProductionDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProductionDt.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1160;
	//if ($Module_SerialNo == 1)     $TableWidth += 20;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($DisableIdeal == 0)	       $TableWidth += 330;
	//if ($Module_SerialNo == 1)     $TableWidth += 50;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 170;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		/*if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}*/	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.ProductionDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Actual Qty</span></th> ";	
		  
		if ($DisableIdeal == 0) {
			echo"  <th width='75px;' align='center'><span>Ideal Qty</span></th>";
			echo"  <th width='75px;' align='center'><span>Waste</span></th>";
			echo"  <th width='75px;' align='center'><span>Actual Waste</span></th>";
		}
		
		/*if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}*/
		
		echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
		echo"  <th width='105px;' align='center'><span>Actual Total</span></th>";
		
		if ($DisableIdeal == 0) {
			echo"  <th width='105px;' align='center'><span>Ideal Total</span></th>";
		}
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPrdId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;
			
			$query = " Select ProductionDt.ProductionDt_ID, ProductionDt.Item_CodePacked, ProductionDt.Item_code, Item_Description, ProductionDt.Warehouse_Code, ".
					 " ProductionDt_Remark, ProductionDt_Quantity, ProductionDt_IdealQty, Round(ProductionDt_Cost, ".$DecimalBase."), ProductionDt_CostBase1, ProductionDt_CostBase2, ".
					 " ifNull(ProductionDt_IdealQty,0) - ifNull(ProductionDt_Quantity,0) As ProductionDtWasteCalc, ProductionDt_Waste, ".
					 " Round((ProductionDt_Quantity*ProductionDt_Cost), ".$DecimalBase.") As TotalActual, ".
					 " Round((ProductionDt_IdealQty*ProductionDt_Cost), ".$DecimalBase.") As TotalIdeal, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(Temp_Purchasesserialno.SerialNo_Code) As CountSerialNo, Item_UnitSalesDesc, ".
					 " Date_Format(ProductionDt_ExpireDate,'%d/%m/%Y') As ProductionDt_ExpireDate, ProductionDt_BatchNo, ProductionDt.Project_code, ".
					 " ProductionDt.CostCenter_Code, ifNull(ProductionDt_Checked,1) as ProductionDt_Checked, productionDt_heatNo ".
					 " From Temp_PreProductionDt As ProductionDt Left Join Items on ProductionDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on ProductionDt.ProductionDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PD' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Where ProductionDt.Production_ID = ".$PPrdId.
					 " Group By  ProductionDt.ProductionDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$productiondt_heatno[$i] = $rowsdt[23];
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
							
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[6] != $rowsdt[16] && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0 && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[6] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				$SelRecord  = 'R'.$k;
				$$SelRecord = $rowsdt[0];
			   
				echo " 	<td width='25px;' align='center'><input type='checkbox' name='$SelRecord' id='$SelRecord' value='".$$SelRecord."' onclick='UnSelectRecord();' ";
				if ($rowsdt[22] == 1) echo "checked /></td>"; else echo "/></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				/*if ($rowsdt[15] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[15] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}*/
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], 'DI', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(DeliveryDt_Quantity,0)),0) FROM Temp_Deliverydt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Delivery_ID = $PSaId And DeliveryDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_DI = $result[0];
				
					
				$min_available_qty = $min_available_qty - $QtyTemp_DI;
				$min_exists_qty = $min_exists_qty - $QtyTemp_DI;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;
				
				
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='185px;' align='left'>
							<input type='hidden' name='txtPrdDtId[$i]' id='prddtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); GetItemCost(this.id,'costprice[$i]','getitemcost','".$i."','".$txtDate."','".$txtCurrCode."'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}		
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
							
							
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";

				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else echo"<input type='hidden' name='<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				
				
				
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[17]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQty[$i]' id='prddtquantity[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPrdDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
					
				if ($DisableIdeal == 0) {
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='".$rowsdt[11]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='".$rowsdt[12]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='' >";
				}
				
				
				
				/*if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				}*/
				
				
				echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPrice[$i]' id='costprice[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotal[$i]' id='prddttotal[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
				if ($DisableIdeal == 0) {
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdeal[$i]' id='prddttotalideal[$i]' value='".$rowsdt[14]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				}	
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[20]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[21]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[19], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[18]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		echo"<input type='hidden' name='ChkCounter' id='ChkCounter' value='$k'>";
							
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor' table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			echo"   <td width='25px;' align='center'><span>&nbsp;</span></td>"; 
			
			/*if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			}*/ 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtPrdDtId[$i]' id='prddtid[$i]' value='$txtPrdDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-2'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"   ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); \" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			if($Module_Heatno==1)
			echo"  	<td width='210px;' align='center'>
							<input type='text' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
					</td>";		
			else 
			echo"<input type='hidden' name='productiondt_heatno[$i]' id='productiondt_heatno[$i]' value='".$productiondt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:180px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); ".$KeyDown."   />";
			
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQty[$i]' id='prddtquantity[$i]' value='$txtPrdDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcPrdDtTotal($DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." ></td>";
					
			if ($DisableIdeal == 0) {
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='$txtPrdDtQtyIdeal[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='$txtPrdDtWasteCalc[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='$txtPrdDtWaste[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			} else {
				echo"  <input type='hidden' name='txtPrdDtQtyIdeal[$i]' id='prddtidealqty[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWasteCalc[$i]' id='prddtwastecalc[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWaste[$i]' id='prddtwaste[$i]' value='' >";
			}	
		  
			
			/*if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}*/
			
			echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPrice[$i]' id='costprice[$i]' value='$txtPrdDtCostPrice[$i]' maxlength='20' onKeyup='CalcPrdDtTotal($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotal[$i]' id='prddttotal[$i]' value='$txtPrdDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				
			if ($DisableIdeal == 0) {
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdeal[$i]' id='prddttotalideal[$i]' value='$txtPrdDtTotalIdeal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			}	
				
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtPurDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value='$txtPurDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			echo"	<td width='255px;'><input type='text' name='txtPurDtRemark[$i]' id='remarkdt[$i]' value='".htmlspecialchars($txtPurDtRemark[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPrdId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowAssembleTotals($connection,$PPrdId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	
	if ($PPrdId != "") {
		$sub_query = " Select Round(Production_Total, ".$DecimalBase.") As ProductionTotal, ".
					 " Round(Production_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal, ".
					 " Round(Production_Total/ifNull(Production_Quantity,1), ".$DecimalBase.") As CostPricePerUnit, ".
					 " Round(Production_IdealTotal/ifNull(Production_IdealQty,1), ".$DecimalBase.") As CostPricePerUnitIdeal ".
					 " From Temp_Production As Production ".
					 " Where Production_ID = ".$PPrdId;
			 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPrdTotal      		= $sub_result[0];
		$txtPrdTotalIdeal		= $sub_result[1];
		$txtPrdCostPerUnit   	= $sub_result[2];
		$txtPrdCostPerUnitIdeal = $sub_result[3];
		
	} 
	
	
	
	echo"	<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
			<tr>
					<td id='total-top' style='width:240px;height:12px;'></td>
					</tr>
					<tr><td id='total-content' style='width:240px;height:12px;'>
			<table border='0' style='table-layout:fixed;' cellspacing=0 >
			<col width=90>
			<col width=140>
			
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Total Cost Actual:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='txtPrdTotal' value='$txtPrdTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Cost Price/Unit: <br>Actual</span>&nbsp;</td> 
					<td align='left' >
						<input type='text' id='unitcostactual' name='txtPrdCostPerUnit' value='$txtPrdCostPerUnit' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
						
						
			 
			 
			 echo"	' readonly>
						<input type='hidden' id='totalideal' name='txtPrdTotalIdeal' value='$txtPrdTotalIdeal'>
						<input type='hidden' id='unitcostideal' name='txtPrdCostPerUnitIdeal' value='$txtPrdCostPerUnitIdeal'>
					</td>
				</tr>";
		  
			
		  
		 echo" </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}

function ShowPreAssembleTotals($connection,$PPrdId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	
	if ($PPrdId != "") {
		$sub_query = " Select Round(Production_Total, ".$DecimalBase.") As ProductionTotal, ".
					 " Round(Production_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal, ".
					 " Round(Production_Total/ifNull(Production_Quantity,1), ".$DecimalBase.") As CostPricePerUnit, ".
					 " Round(Production_IdealTotal/ifNull(Production_IdealQty,1), ".$DecimalBase.") As CostPricePerUnitIdeal ".
					 " From Temp_PreProduction As Production ".
					 " Where Production_ID = ".$PPrdId;
			 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPrdTotal      		= $sub_result[0];
		$txtPrdTotalIdeal		= $sub_result[1];
		$txtPrdCostPerUnit   	= $sub_result[2];
		$txtPrdCostPerUnitIdeal = $sub_result[3];
		
	} 
	
	
	
	echo"	<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
			<tr>
					<td id='total-top' style='width:240px;height:12px;'></td>
					</tr>
					<tr><td id='total-content' style='width:240px;height:12px;'>
			<table border='0' style='table-layout:fixed;' cellspacing=0 >
			<col width=90>
			<col width=140>
			
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Total Cost Actual:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='txtPrdTotal' value='$txtPrdTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Cost Price/Unit: <br>Actual</span>&nbsp;</td> 
					<td align='left' >
						<input type='text' id='unitcostactual' name='txtPrdCostPerUnit' value='$txtPrdCostPerUnit' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
						
						
			 
			 
			 echo"	' readonly>
						<input type='hidden' id='totalideal' name='txtPrdTotalIdeal' value='$txtPrdTotalIdeal'>
						<input type='hidden' id='unitcostideal' name='txtPrdCostPerUnitIdeal' value='$txtPrdCostPerUnitIdeal'>
					</td>
				</tr>";
		  
			
		  
		 echo" </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			


}
function ShowProdTplDtGridFG($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Pack = $arrayparams['Module_Pack'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1       = $arrayparams['Base1'];
	$Base2       = $arrayparams['Base2'];
	$txtDate     = $arrayparams['txtDate'];
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcodefg", "itemdescriptionfg", "warehousecodefg", "prddtqtyfgf", "prddtqtyfg", "prdcostoptionfg", "remarkdtfg");
	$columnsarrayall = array("itemcodefg", "itemdescriptionfg", "warehousecodefg", "prddtqtyfg", "prddtqtyfgf", "remarkdtfg", "prdfgid");
	$orderbyarray = array("ProdRecipeFG.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProdRecipeFG.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1225;
	if ($Module_Pack == 1) $TableWidth += 125;
	
	
	//Initializations for Div Ids
	$ContainerDiv = 'container1';
	$LoaderDiv    = 'Loader1';
	$HeaderDiv    = 'grid_headerDiv1';
	$ContentDiv   = 'grid_contentDiv1';
	$TableFocus   = 'table-focus-g1';
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='$ContainerDiv' style='position: relative;z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:1073px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProdRecipeFG.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='140px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProdRecipeFG.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Qty From</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Qty To</span></th> ";	
		echo"  <th width='230px;' align='center'><span>Cost Option</span></th>";
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv');\" style='width:1090px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec_G1' id='SelectedRec_G1' value='$SelectedRec_G1'>
			 <input type='hidden' name='SelectedRecColor_G1' id='SelectedRecColor_G1' value='$SelectedRecColor_G1'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPrdId != '' ) {
		      
			$query = " Select ProdRecipeFG.ProdRecipeFG_ID, ProdRecipeFG.Item_CodePacked, ProdRecipeFG.Item_code, Item_Description, ProdRecipeFG.Warehouse_Code, ".
					 " ProdRecipeFG_Remark, ProdRecipeFG_QuantityFrom, ProdRecipeFG_Quantity, ProdRecipeFG_DistOption, Items.Item_UnitPurchaseDesc ".
					 " From ProdRecipeFG Left Join Items on ProdRecipeFG.Item_code = Items.Item_code ".
					 " Where ProdRecipeFG.ProdRecipe_ID = ".$PPrdId.
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				
					
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\')"';
				
				echo"<tr id='CurRec_G1$i' class='$bgcolor $TableFocus'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGridFG('".$filepath."','".$form."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='140px;' align='left'>
							<input type='hidden' name='txtPrdFGId[$i]' id='prdfgid[$i]' value='".$rowsdt[0]."' >
							<input type='text' name='txtItemCodeFG[$i]' id='itemcodefg[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeFG[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcodefg[$i]','itemdescriptionfg[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsFG(1,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescFG[$i]' id='itemdescriptionfg[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescFG[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDescFG(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCodeFG[$i]' id='warehousecodefg[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeFG[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecodefg[$i]','warehousenamefg[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseFG($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				echo "	<td width='65px;' align='center'><span class='".$TextColor."'>".$rowsdt[9]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyFGF[$i]' id='prddtqtyfgf[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyFG[$i]' id='prddtqtyfg[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				
				echo"   <td width='230px;' align='center'><span>";
				echo"  		<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='1' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[8]=='1' || $rowsdt[8]=='') echo "checked />"; else echo " />"; echo"<font size=2>";echo " Percentage";
				echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='2' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[8]=='2') echo "checked />"; else echo " />";  echo " Quantity";
				echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='3' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[8]=='3') echo "checked />"; else echo " />";  echo " Fixed";
				echo"	</span></td>";
				
				echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkFG[$i]' id='remarkdtfg[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' onclick=\"SubmitRecordGridFG('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		
							
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCodeFG[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCodeFG[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCodeFG[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCodeFG[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\')"';
			
				
			echo"<tr id='CurRec_G1$i' class='$bgcolor $TableFocus' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='140px;' align='left'>
						<input type='hidden' name='txtPrdFGId[$i]' id='prdfgid[$i]' value='$txtPrdFGId[$i]' >
						<input type='text' name='txtItemCodeFG[$i]' id='itemcodefg[$i]' value='$txtItemCodeFG[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeFG[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcodefg[$i]','itemdescriptionfg[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsFG(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescFG[$i]' id='itemdescriptionfg[$i]' value='$txtItemDescFG[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescFG[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDescFG(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCodeFG[$i]' id='warehousecodefg[$i]' value='$txtWarehouseCodeFG[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeFG[$i]'); \" onkeyup=\"AutoCompleteData(event,this.id,'warehousecodefg[$i]','warehousenamefg[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseFG($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1FG' id='itemunit1fg[$i]' value='$txtItemUnit1FG' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyFGF[$i]' id='prddtqtyfgf[$i]' value='$txtPrdDtQtyFGF[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyFG[$i]' id='prddtqtyfg[$i]' value='$txtPrdDtQtyFG[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." ></td>";
			
			echo"   <td width='230px;' align='center'><span>";
			echo"  		<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='1' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." checked />"; echo"<font size=2>";echo " Percentage ";
			echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='2' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." />";  echo " Quantity";
			echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='3' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." />";  echo " Fixed";
			echo"	</span></td>";
				
			echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkFG[$i]' id='remarkdtfg[$i]' value='".htmlspecialchars($txtPrdDtRemarkFG[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' onclick=\"SubmitRecordGridFG('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPrdId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}


function ShowProdTplDtGridRM($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Pack = $arrayparams['Module_Pack'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1       = $arrayparams['Base1'];
	$Base2       = $arrayparams['Base2'];
	$txtDate     = $arrayparams['txtDate'];
	$DefaultCur  = $arrayparams['DefaultCur'];
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcoderm", "itemdescriptionrm", "warehousecoderm", "prddtqtyrmf", "prddtqtyrm", "prdcostoptionrm", "currencycoderm", "costpricerm", "wasteraterm", "wasteperbilletrm", "remarkdtrm");
	$columnsarrayall = array("itemcoderm", "itemdescriptionrm", "warehousecoderm", "prddtqtyrm", "prddtqtyrmf", "currencycoderm", "costpricerm", "wasteraterm", "wasteperbilletrm", "remarkdtrm", "prdrmid");
	$orderbyarray = array("ProdRecipeRM.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProdRecipeRM.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1520;
	if ($Module_Pack == 1) $TableWidth += 125;
	
	
	//Initializations for Div Ids
	$ContainerDiv = 'container2';
	$LoaderDiv    = 'Loader2';
	$HeaderDiv    = 'grid_headerDiv2';
	$ContentDiv   = 'grid_contentDiv2';
	$TableFocus   = 'table-focus-g2';
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='$ContainerDiv' style='position: relative;z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:1073px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProdRecipeRM.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='140px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProdRecipeRM.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Qty From</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Qty To</span></th> ";	
		echo"  <th width='230px;' align='center'><span>Cost Option</span></th>";
		echo"  <th width='60px;' align='center'><span>Currency</span></th>";
		echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
		echo"  <th width='75px;' align='center'><span>Waste Rate(%)</span></th>";
		echo"  <th width='75px;' align='center'><span>Waste Per Billet(%)</span></th>";
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv');\" style='width:1090px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec_G2' id='SelectedRec_G2' value='$SelectedRec_G2'>
			 <input type='hidden' name='SelectedRecColor_G2' id='SelectedRecColor_G2' value='$SelectedRecColor_G2'>
			 <input type='hidden' name='ModRec_G2' id='ModRec_G2' value='$ModRec_G2'>";  
		
			 
		
		
		if ($PPrdId != '' ) {
	
			$query = " Select ProdRecipeRM.ProdRecipeRM_ID, ProdRecipeRM.Item_CodePacked, ProdRecipeRM.Item_code, Item_Description, ProdRecipeRM.Warehouse_Code, ".
					 " ProdRecipeRM_Remark, ProdRecipeRM_QuantityFrom, ProdRecipeRM_Quantity, ProdRecipeRM_DistOption, Items.Item_UnitPurchaseDesc, ".
					 " ProdRecipeRM.Currency_Code, ProdRecipeRM_Cost, ProdRecipeRM_WasteRate, ProdRecipeRM_WastePerBillet ".
					 " From ProdRecipeRM Left Join Items on ProdRecipeRM.Item_code = Items.Item_code ".
					 " Where ProdRecipeRM.ProdRecipe_ID = ".$PPrdId.
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$querycurrency="select currency_code from currency order by currency_code";
				$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));

					
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2['.$i.']\')"';
				
				echo"<tr id='CurRec_G2$i' class='$bgcolor $TableFocus'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGridRM('".$filepath."','".$form."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='140px;' align='left'>
							<input type='hidden' name='txtPrdRMId[$i]' id='prdrmid[$i]' value='".$rowsdt[0]."' >
							<input type='text' name='txtItemCodeRM[$i]' id='itemcoderm[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeRM[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcoderm[$i]','itemdescriptionrm[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsRM(1,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescRM[$i]' id='itemdescriptionrm[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescRM[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDescRM(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCodeRM[$i]' id='warehousecoderm[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeRM[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecoderm[$i]','warehousenamerm[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseRM($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
				echo "	<td width='65px;' align='center'><span class='".$TextColor."'>".$rowsdt[9]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRMF[$i]' id='prddtqtyrmf[$i]' value='".$rowsdt[6]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRM[$i]' id='prddtqtyrm[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
				
				echo"   <td width='230px;' align='center'><span>";
				echo"  		<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='1' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." "; if($rowsdt[8]=='1' || $rowsdt[8]=='') echo "checked />"; else echo " />";echo" <font size=2>"; echo " Percentage";
				echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='2' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." "; if($rowsdt[8]=='2') echo "checked />"; else echo " />";  echo " Quantity";
				echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='3' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." "; if($rowsdt[8]=='3') echo "checked />"; else echo " />";  echo " Fixed";
				echo"	</span></td>";
				
				echo"   <td width='60px;' align='center'><select name='txtCurrencyCodeRM[$i]' id='currencycoderm[$i]' class='".$ClassE."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." >";
				echo"	<option></option>";
				while($curf=mysql_fetch_row($resultcurrency)){
					echo"<option ";
					if ($rowsdt[10]==$curf[0])	echo " selected ";
					echo" value='$curf[0]'>$curf[0] </option>";
				}
				echo"	</select></td>";	
				
				echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostRM[$i]' id='costpricerm[$i]' value='".$rowsdt[11]."' maxlength='20' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtWasteRateRM[$i]' id='wasteraterm[$i]' value='".$rowsdt[12]."' maxlength='20' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtWastePerBilletRM[$i]' id='wasteperbilletrm[$i]' value='".$rowsdt[13]."' maxlength='20' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
				
				echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkRM[$i]' id='remarkdtrm[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";	
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_2[$i]' onclick=\"SubmitRecordGridRM('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		
							
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCodeRM[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCodeRM[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCodeRM[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCodeRM[$i] = $defaultwarehouse;
			}
			
			if (empty($DefaultCur))  $DefaultCur = $Base1;
			if (empty($txtCurrencyCodeRM[$i])) $txtCurrencyCodeRM[$i] = $DefaultCur;

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2['.$i.']\')"';
			
			$querycurrency="select currency_code from currency order by currency_code";
			$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
				
			echo"<tr id='CurRec_G2$i' class='$bgcolor $TableFocus' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G2');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='140px;' align='left'>
						<input type='hidden' name='txtPrdRMId[$i]' id='prdrmid[$i]' value='$txtPrdRMId[$i]' >
						<input type='text' name='txtItemCodeRM[$i]' id='itemcoderm[$i]' value='$txtItemCodeRM[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G2','$i'); GetItemCost(this.id,'costpricerm[$i]','getitemcost','".$i."','".$txtDate."','".$txtCurrencyCodeRM[$i]."'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeRM[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcoderm[$i]','itemdescriptionrm[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsRM(1,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescRM[$i]' id='itemdescriptionrm[$i]' value='$txtItemDescRM[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescRM[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDescRM(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCodeRM[$i]' id='warehousecoderm[$i]' value='$txtWarehouseCodeRM[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeRM[$i]'); \" onkeyup=\"AutoCompleteData(event,this.id,'warehousecoderm[$i]','warehousenamerm[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseRM($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1RM' id='itemunit1rm[$i]' value='$txtItemUnit1RM' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRMF[$i]' id='prddtqtyrmf[$i]' value='$txtPrdDtQtyRMF[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRM[$i]' id='prddtqtyrm[$i]' value='$txtPrdDtQtyRM[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." ></td>";
			
			echo"   <td width='230px;' align='center'><span>";
			echo"  		<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='1' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." checked />"; echo" <font size=2>";echo " Percentage";
			echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='2' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." />";  echo " Quantity";
			echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='3' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." />";  echo " Fixed";
			echo"	</span></td>";
				
			echo"   <td width='60px;' align='center'><select name='txtCurrencyCodeRM[$i]' id='currencycoderm[$i]' class='".$ClassI."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." >";
			echo"	<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
				echo"<option ";
				if ($txtCurrencyCodeRM[$i]==$curf[0])	echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"	</select></td>";	
				
			echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostRM[$i]' id='costpricerm[$i]' value='$txtPrdDtCostRM[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtWasteRateRM[$i]' id='wasteraterm[$i]' value='$txtWasteRateRM[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtWastePerBilletRM[$i]' id='wasteperbilletrm[$i]' value='$txtWastePerBilletRM[$i]' maxlength='20' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' ".$KeyDown." ></td>";
				
			echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkRM[$i]' id='remarkdtrm[$i]' value='".htmlspecialchars($txtPrdDtRemarkRM[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";	
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_2[$i]' onclick=\"SubmitRecordGridRM('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPrdId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowProdDtGridFG($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_ShowCostPrice = $arrayparams['FI_ShowCostPrice'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	$DisableIdeal    = $arrayparams['DisableIdeal'];

	
	/// grid Motion
		$Motion   = 1;
		$NextCell = 'itemcoderm[0]';
		$TabIndex = 0;		
	/////////////
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcodefg", "itemdescriptionfg", "warehousecodefg");


	if($Module_Heatno==1){
		array_push($columnsarray, "HeatNo");
		array_push($columnsarray, "Bundle");
		}
	array_push($columnsarray,"prddtqtyinitialfg", "prddtqtydamagefg", "prddtqtyremixfg", "prddtqtyfg");
	
	if ($DisableIdeal == 0) {
		array_push($columnsarray, 'prddtidealqtyfg');
		array_push($columnsarray, 'prddtwastecalcfg');
		array_push($columnsarray, 'prddtwastefg');
	}	
	
	if ($FI_ShowCostPrice == 1) {
	array_push($columnsarray, 'costpricefg');
	array_push($columnsarray, 'prddttotalfg');
	}
	
	if ($DisableIdeal == 0) array_push($columnsarray, 'prddttotalidealfg');

	
	
	if ($FI_ShowCostPrice == 1)
	array_push($columnsarray, 'prdcostoptionfg');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedtfg');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedtfg');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchnofg');
		array_push($columnsarray, 'expiredatefg');
	}	
	array_push($columnsarray, 'remarkdtfg');
	
	
	$columnsarrayall = array("itemcodefg", "itemdescriptionfg", "warehousecodefg", "prddtqtyinitialfg", "prddtqtydamagefg", "prddtqtyremixfg", "prddtqtyfg", "prddtidealqtyfg", "prddtwastecalcfg", "prddtwastefg", 
	                         "costpricefg", "prddttotalfg", "prddttotalidealfg", "prdcostoptionfg", "projectcodedtfg", "costcentcodedtfg", "batchnofg", "expiredatefg", "remarkdtfg", "prdfgid");
	$orderbyarray = array("ProductionFG.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProductionFG.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1335;
	if ($Module_SerialNo == 1)     $TableWidth += 20;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($DisableIdeal == 0)	       $TableWidth += 330;
	//if ($Module_SerialNo == 1)     $TableWidth += 50;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 170;
	
	//Initializations for Div Ids
	$ContainerDiv = 'container1';
	$LoaderDiv    = 'Loader1';
	$HeaderDiv    = 'grid_headerDiv1';
	$ContentDiv   = 'grid_contentDiv1';
	$TableFocus   = 'table-focus-g1';
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='$ContainerDiv' style='position: relative;z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:1073px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}
		
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-1', 1, '".$dftorderby."', 'COR','ProductionFG.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='140px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-1', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code (FG)".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-1', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-1', 1, '".$dftorderby."', 'COR','ProductionFG.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.ProductionDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.productionfg_bundle','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Bundle</span></th>";
	
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-1', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Initial Qty</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Damage Qty</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Remix Qty</span></th> ";	
		echo"  <th width='75px;' align='center'><span>Actual Qty</span></th> ";	
		
		if ($DisableIdeal == 0) {
			echo"  <th width='75px;' align='center'><span>Ideal Qty</span></th>";
			echo"  <th width='75px;' align='center'><span>Waste</span></th>";
			echo"  <th width='75px;' align='center'><span>Actual Waste</span></th>";
		}
		
		
		if ($FI_ShowCostPrice == 1) {  	
			echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
			echo"  <th width='105px;' align='center'><span>Actual Total</span></th>";
		}
		
		if ($DisableIdeal == 0) {
			echo"  <th width='105px;' align='center'><span>Ideal Total</span></th>";
		}
		
		if ($FI_ShowCostPrice == 1) 
		echo"  <th width='230px;' align='center'><span>Cost Option</span></th>";	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv');\" style='width:1090px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec_G1' id='SelectedRec_G1' value='$SelectedRec_G1'>
			 <input type='hidden' name='SelectedRecColor_G1' id='SelectedRecColor_G1' value='$SelectedRecColor_G1'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PPrdId != '' ) {
				  
			$query = " Select ProductionFG.ProductionFG_ID, ProductionFG.Item_CodePacked, ProductionFG.Item_code, Item_Description, ProductionFG.Warehouse_Code, ".
					 " ProductionFG_Remark, ProductionFG_Quantity, ProductionFG_IdealQty, Round(ProductionFG_Cost, ".$DecimalBase."), ProductionFG_CostBase1, ProductionFG_CostBase2, ".
					 " ifNull(ProductionFG_IdealQty,0) - ifNull(ProductionFG_Quantity,0) As ProductionFGWasteCalc, ProductionFG_Waste, ".
					 " Round((ProductionFG_Quantity*ProductionFG_Cost), ".$DecimalBase.") As TotalActual, ".
					 " Round((ProductionFG_IdealQty*ProductionFG_Cost), ".$DecimalBase.") As TotalIdeal, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(Temp_Purchasesserialno.SerialNo_Code) As CountSerialNo, Item_UnitSalesDesc, ".
					 " Date_Format(ProductionFG_ExpireDate,'%d/%m/%Y') As ProductionFG_ExpireDate, ProductionFG_BatchNo, ProductionFG.Project_code, ProductionFG.CostCenter_Code, ".
					 " ProductionFG_CostOption, ifNull(ProductionFG_InitialQty,0), ifNull(ProductionFG_DamageQty,0), ifNull(ProductionFG_RemixQty,0) , productionfg_heatno , productionfg_bundle ".
					 " From Temp_ProductionFG As ProductionFG Left Join Items on ProductionFG.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on ProductionFG.ProductionFG_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PD' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Where ProductionFG.ProductionHD_ID = ".$PPrdId.
					 " Group By  ProductionFG.ProductionFG_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				$productionfg_heatno[$i] = $rowsdt[26];
				$productionfg_bundle[$i] = $rowsdt[27];
				 
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				
					
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\')"';
				
				echo"<tr id='CurRec_G1$i' class='$bgcolor $TableFocus'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGridFG('".$filepath."','".$form."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[15] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[15] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='140px;' align='left'>
							<input type='hidden' name='txtPrdFGId[$i]' id='prdfgid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpirefg[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserialfg[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='text' name='txtItemCodeFG[$i]' id='itemcodefg[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeFG[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcodefg[$i]','itemdescriptionfg[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(8,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescFG[$i]' id='itemdescriptionfg[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescFG[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(8,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCodeFG[$i]' id='warehousecodefg[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeFG[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecodefg[$i]','warehousenamefg[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseFG($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionfg_heatno[$i]' id='productionfg_heatno[$i]' value='".$productionfg_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
			else echo"<input type='hidden' name='productionfg_heatno[$i]' id='productionfg_heatno[$i]' value='".$productionfg_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
			
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionfg_bundle[$i]' id='productionfg_bundle[$i]' value='".$productionfg_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;'   ".$KeyDown."   />
							
						</td>";		
				else 	echo"<input type='hidden' name='productionfg_bundle[$i]' id='productionfg_bundle[$i]' value='".$productionfg_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;'   ".$KeyDown."   />";

				
				echo "	<td width='65px;' align='center'><span class='".$TextColor."'>".$rowsdt[17]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyInitialFG[$i]' id='prddtqtyinitialfg[$i]' value='".$rowsdt[23]."' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyDamageFG[$i]' id='prddtqtydamagefg[$i]' value='".$rowsdt[24]."' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRemixFG[$i]' id='prddtqtyremixfg[$i]' value='".$rowsdt[25]."' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyFG[$i]' id='prddtqtyfg[$i]' value='".$rowsdt[6]."' maxlength='20'  onKeyup='CalcPrdDtTotalFG($DecimalBase,$i);' class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				
				if ($DisableIdeal == 0) {
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdealFG[$i]' id='prddtidealqtyfg[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalcFG[$i]' id='prddtwastecalcfg[$i]' value='".$rowsdt[11]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteFG[$i]' id='prddtwastefg[$i]' value='".$rowsdt[12]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPrdDtQtyIdealFG[$i]' id='prddtidealqtyfg[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteCalcFG[$i]' id='prddtwastecalcfg[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteFG[$i]' id='prddtwastefg[$i]' value='' >";
				}
				if ($FI_ShowCostPrice == 1) {  		
					echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPriceFG[$i]' id='costpricefg[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotalFG($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalFG[$i]' id='prddttotalfg[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"	<input type='hidden' name='txtPrdDtCostPriceFG[$i]' id='costpricefg[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotalFG($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  >";
					echo"   <input type='hidden' name='txtPrdDtTotalFG[$i]' id='prddttotalfg[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." >";
				}
				
				if ($DisableIdeal == 0) {
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdealFG[$i]' id='prddttotalidealfg[$i]' value='".$rowsdt[14]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				}	
				
				if ($FI_ShowCostPrice == 1) {  		
					echo"   <td width='230px;' align='center'><span>";
					echo"  		<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='1' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='1' || $rowsdt[22]=='') echo "checked />"; else echo " />";echo" <font size=2>";  echo " Percentage";
					echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='2' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='2') echo "checked />"; else echo " />";  echo " Quantity";
					echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='3' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='3') echo "checked />"; else echo " />";  echo " Fixed";
					echo"	</span></td>";
				} else {
					echo"  		<span><input type='radio' hidden name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='1' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='1' || $rowsdt[22]=='') echo "checked />"; else echo " />"; 
					echo"	 	<input type='radio' hidden  name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='2' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='2') echo "checked />"; else echo " />";  
					echo"	 	<input type='radio'  hidden name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='3' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='3') echo "checked />"; else echo " /></span>";  
				}

				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDtFG[$i]' id='projectcodedtfg[$i]' value='".$rowsdt[20]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCodeFG[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDtFG[$i]' id='projectcodedtfg[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDtFG[$i]' id='costcentcodedtfg[$i]' value='".$rowsdt[21]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCodeFG[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDtFG[$i]' id='costcentcodedtfg[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNoFG[$i]' id='batchnofg[$i]' value='".htmlspecialchars($rowsdt[19], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDateFG[$i]' id='expiredatefg[$i]' value='".$rowsdt[18]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDateFG[$i]' id='expiredatefg[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNoFG[$i]' id='batchnofg[$i]' value=''>";
				}
					
				
				echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkFG[$i]' id='remarkdtfg[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' 
				onclick=\"SubmitRecordGridFG('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,'$NextCell',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		
							
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCodeFG[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCodeFG[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCodeFG[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCodeFG[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\')"';
			
				
			echo"<tr id='CurRec_G1$i' class='$bgcolor $TableFocus' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			}
			
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='140px;' align='left'>
						<input type='hidden' name='txtPrdFGId[$i]' id='prdfgid[$i]' value='$txtPrdFGId[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpirefg[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserialfg[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCodeFG[$i]' id='itemcodefg[$i]' value='$txtItemCodeFG[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeFG[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-3'); SetRecordGridDefaults($i);\" onkeyup=\"AutoCompleteData(event,this.id,'itemcodefg[$i]','itemdescriptionfg[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(8,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescFG[$i]' id='itemdescriptionfg[$i]' value='$txtItemDescFG[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescFG[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(8,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCodeFG[$i]' id='warehousecodefg[$i]' value='$txtWarehouseCodeFG[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeFG[$i]'); \" onkeyup=\"AutoCompleteData(event,this.id,'warehousecodefg[$i]','warehousenamefg[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseFG($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
			
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionfg_heatno[$i]' id='productionfg_heatno[$i]' value='".$productionfg_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";		
				else echo"<input type='hidden' name='productionfg_heatno[$i]' id='productionfg_heatno[$i]' value='".$productionfg_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />		";
				
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionfg_bundle[$i]' id='productionfg_bundle[$i]' value='".$productionfg_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;'   ".$KeyDown."   />
							
						</td>";		
				else echo"<input type='hidden' name='productionfg_bundle[$i]' id='productionfg_bundle[$i]' value='".$productionfg_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;'   ".$KeyDown."   />";
				
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1FG' id='itemunit1fg[$i]' value='$txtItemUnit1FG' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyInitialFG[$i]' id='prddtqtyinitialfg[$i]' value='$txtPrdDtQtyInitialFG[$i]' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyDamageFG[$i]' id='prddtqtydamagefg[$i]' value='$txtPrdDtQtyDamageFG[$i]' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRemixFG[$i]' id='prddtqtyremixfg[$i]' value='$txtPrdDtQtyRemixFG[$i]' maxlength='20' onkeyup='CalcPrdFinalQtyFG($DecimalBase,$i);' class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyFG[$i]' id='prddtqtyfg[$i]' value='$txtPrdDtQtyFG[$i]' maxlength='20' onKeyup='CalcPrdDtTotalFG($DecimalBase,$i);' class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." ></td>";
			
			if ($DisableIdeal == 0) {
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdealFG[$i]' id='prddtidealqtyfg[$i]' value='$txtPrdDtQtyIdealFG[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalcFG[$i]' id='prddtwastecalcfg[$i]' value='$txtPrdDtWasteCalcFG[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteFG[$i]' id='prddtwastefg[$i]' value='$txtPrdDtWasteFG[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			} else {
				echo"  <input type='hidden' name='txtPrdDtQtyIdealFG[$i]' id='prddtidealqtyfg[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWasteCalcFG[$i]' id='prddtwastecalcfg[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWasteFG[$i]' id='prddtwastefg[$i]' value='' >";
			}
			if ($FI_ShowCostPrice == 1) {  
				echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPriceFG[$i]' id='costpricefg[$i]' value='$txtPrdDtCostPriceFG[$i]' maxlength='20' onKeyup='CalcPrdDtTotalFG($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalFG[$i]' id='prddttotalfg[$i]' value='$txtPrdDtTotalFG[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo"	<input type='hidden' name='txtPrdDtCostPriceFG[$i]' id='costpricefg[$i]' value='$txtPrdDtCostPriceFG[$i]' maxlength='20' onKeyup='CalcPrdDtTotalFG($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  >";
				echo"  <input type='hidden' name='txtPrdDtTotalFG[$i]' id='prddttotalfg[$i]' value='$txtPrdDtTotalFG[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." >";
			}
				
			if ($DisableIdeal == 0) {
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdealFG[$i]' id='prddttotalidealfg[$i]' value='$txtPrdDtTotalIdealFG[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			}	
			if ($FI_ShowCostPrice == 1) {  	
			echo"   <td width='230px;' align='center'><span>";
			echo"  		<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='1' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." checked />";echo" <font size=2>"; echo " Percentage";
			echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='2' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." />";  echo " Quantity";
			echo"	 	<input type='radio' name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='3' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." />";  echo " Fixed";
			echo"	</span></td>";
			} else {
			
			echo"  		<span><input type='radio' hidden  name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='1' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." checked />";
			echo"	 	<input type='radio'    hidden  name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='2' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." />";  
			echo"	 	<input type='radio'    hidden  name='txtCostOptionFG[$i]' id='prdcostoptionfg[$i]' value='3' onchange=\"SetModRecord('ModRec_G1','$i'); \" ".$KeyDown." /></span>";  
			
			}
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDtFG[$i]' id='projectcodedtfg[$i]' value='$txtProjectCodeDtFG[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCodeFG[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDtFG[$i]' id='projectcodedtfg[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDtFG[$i]' id='costcentcodedtfg[$i]' value='$txtCostCentCodeDtFG[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCodeFG[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDtFG[$i]' id='costcentcodedtfg[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNoFG[$i]' id='batchnofg[$i]' value='".htmlspecialchars($txtPurDtBatchNoFG[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDateFG[$i]' id='expiredatefg[$i]' value='$txtPurDtExpireDateFG[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDateFG[$i]' id='expiredatefg[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNoFG[$i]' id='batchnofg[$i]' value=''>";
			}
				
				
			echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkFG[$i]' id='remarkdtfg[$i]' value='".htmlspecialchars($txtPrdDtRemarkFG[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";	
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' onclick=\"SubmitRecordGridFG('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo,'$NextCell',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPrdId,'datagrid-1',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	$query = " Select Count(*) FROM Temp_ProductionFG ".
			 " WHERE ProductionHD_Id = ".$PPrdId;
			 
	$result = selectData($connection, $query);
	$txtPrdCountFG = $result[0];
	if($txtPrdCountFG == '') $txtPrdCountFG = 0;
	
	echo"<input type='hidden' name='txtPrdCountFG' value='$txtPrdCountFG' >";
	
}


function ShowProdDtGridRM($connection,$form,$PPrdId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_ShowCostPrice = $arrayparams['FI_ShowCostPrice'];
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	$DisableIdeal    = $arrayparams['DisableIdeal'];
	$Module_Heatno = $arrayparams['Module_Heatno'];
	/// grid Motion
					$Motion = 1;
					$NextCell = 'itemcodefg[0]';	
					$down = 1;	
			//////////
			
			
			
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	$k = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcoderm", "itemdescriptionrm", "warehousecoderm");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "HeatNo");
	
	if($Module_Heatno==1)
		array_push($columnsarray, "Bundle");
		
	
	$columnsarray = array_push($columnsarray, 'prddtqtyrm');
	
	if ($DisableIdeal == 0) {
		array_push($columnsarray, 'prddtidealqtyrm');
		array_push($columnsarray, 'prddtwastecalcrm');
		array_push($columnsarray, 'prddtwasterm');
	}	
	
	if ($FI_ShowCostPrice == 1) {
	array_push($columnsarray, 'costpricerm');
	array_push($columnsarray, 'prddttotalrm');
	}
	
	if ($DisableIdeal == 0) array_push($columnsarray, 'prddttotalidealrm');
	
	if ($FI_ShowCostPrice == 1) 
	array_push($columnsarray, 'prdcostoptionrm');
	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedtrm');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedtrm');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchnorm');
		array_push($columnsarray, 'expiredaterm');
	}	
	array_push($columnsarray, 'remarkdtrm');
	
	
	$columnsarrayall = array("itemcoderm", "itemdescriptionrm", "warehousecoderm", "prddtqtyrm", "prddtidealqtyrm", "prddtwastecalcrm", "prddtwasterm", 
	                         "costpricerm", "prddttotalrm", "prddttotalidealrm", "projectcodedtrm", "costcentcodedtrm", "batchnorm", "expiredaterm", "remarkdtrm", "prdrmid");
	$orderbyarray = array("ProductionRM.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "ProductionRM.Warehouse_Code", "Items.Item_UnitSalesDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 1110;
	if ($Module_SerialNo == 1)     $TableWidth += 20;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($DisableIdeal == 0)	       $TableWidth += 330;
	//if ($Module_SerialNo == 1)     $TableWidth += 50;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 170;
	
	//Initializations for Div Ids
	$ContainerDiv = 'container2';
	$LoaderDiv    = 'Loader2';
	$HeaderDiv    = 'grid_headerDiv2';
	$ContentDiv   = 'grid_contentDiv2';
	$TableFocus   = 'table-focus-g2';
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='$ContainerDiv' style='position: relative;z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:1073px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}
		
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-2', 1, '".$dftorderby."', 'COR','ProductionFG.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='140px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-2', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code (RM)".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-2', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-2', 1, '".$dftorderby."', 'COR','ProductionFG.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.ProductionDt_Heatno','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid', 1, '".$dftorderby."', 'COR','ProductionDt.productionRM_bundle','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Bundle</span></th>";
	
		
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PPrdId."', 'datagrid-2', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Actual Qty</span></th> ";	
		
		if ($DisableIdeal == 0) {
			echo"  <th width='75px;' align='center'><span>Ideal Qty</span></th>";
			echo"  <th width='75px;' align='center'><span>Waste</span></th>";
			echo"  <th width='75px;' align='center'><span>Actual Waste</span></th>";
		}
		
		if ($FI_ShowCostPrice == 1) {
		echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
		echo"  <th width='105px;' align='center'><span>Actual Total</span></th>";
		}
		
		if ($DisableIdeal == 0) {
			echo"  <th width='105px;' align='center'><span>Ideal Total</span></th>";
		}
		
		if ($FI_ShowCostPrice == 1) 
		echo"  <th width='230px;' align='center'><span>Cost Option</span></th>";	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='255px;' align='center'><span>Remark</span></th>";	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv');\" style='width:1090px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec_G2' id='SelectedRec_G2' value='$SelectedRec_G2'>
			 <input type='hidden' name='SelectedRecColor_G2' id='SelectedRecColor_G2' value='$SelectedRecColor_G2'>
			 <input type='hidden' name='ModRec_G2' id='ModRec_G2' value='$ModRec_G2'>";  
		
			 
		
		
		if ($PPrdId != '' ) {
				   
			$query = " Select ProductionRM.ProductionRM_ID, ProductionRM.Item_CodePacked, ProductionRM.Item_code, Item_Description, ProductionRM.Warehouse_Code, ".
					 " ProductionRM_Remark, ProductionRM_Quantity, ProductionRM_IdealQty, Round(ProductionRM_Cost, ".$DecimalBase."), ProductionRM_CostBase1, ProductionRM_CostBase2, ".
					 " ifNull(ProductionRM_IdealQty,0) - ifNull(ProductionRM_Quantity,0) As ProductionRMWasteCalc, ProductionRM_Waste, ".
					 " Round((ProductionRM_Quantity*ProductionRM_Cost), ".$DecimalBase.") As TotalActual, ".
					 " Round((ProductionRM_IdealQty*ProductionRM_Cost), ".$DecimalBase.") As TotalIdeal, ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(Temp_Purchasesserialno.SerialNo_Code) As CountSerialNo, Item_UnitSalesDesc, ".
					 " Date_Format(ProductionRM_ExpireDate,'%d/%m/%Y') As ProductionRM_ExpireDate, ProductionRM_BatchNo, ProductionRM.Project_code, ProductionRM.CostCenter_Code, ".
					 " ProductionRM_CostOption,productionrm_heatno,productionRM_bundle ".
					 " From Temp_ProductionRM As ProductionRM Left Join Items on ProductionRM.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on ProductionRM.ProductionRM_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = 'PD' ".
					 " And Temp_Purchasesserialno.PurchaseDt_ID Is Not Null ".
					 " Where ProductionRM.ProductionHD_ID = ".$PPrdId.
					 " Group By  ProductionRM.ProductionRM_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    				
				$recno = $i+1;
				$k = $k + 1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				$productionrm_heatno[$i] = $rowsdt[23];
				$productionrm_bundle[$i] = $rowsdt[24];
				
					
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2['.$i.']\')"';
				
				echo"<tr id='CurRec_G2$i' class='$bgcolor $TableFocus'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGridRM('".$filepath."','".$form."', '".$PPrdId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[15] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[6]."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[15] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='140px;' align='left'>
							<input type='hidden' name='txtPrdRMId[$i]' id='prdrmid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpirerm[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserialrm[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='text' name='txtItemCodeRM[$i]' id='itemcoderm[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeRM[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcoderm[$i]','itemdescriptionrm[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(9,$i); ' style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescRM[$i]' id='itemdescriptionrm[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescRM[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(9,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCodeRM[$i]' id='warehousecoderm[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeRM[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecoderm[$i]','warehousenamerm[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseRM($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				

				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionrm_heatno[$i]' id='productionrm_heatno[$i]' value='".$productionrm_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo1($i);' style='vertical-align:bottom;'/></span></label>
						</td>";	
				else echo"<input type='hidden' name='productionrm_heatno[$i]' id='productionrm_heatno[$i]' value='".$productionrm_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				if($Module_Heatno==1)		
					echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionrm_bundle[$i]' id='productionrm_bundle[$i]' value='".$productionrm_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' ".$KeyDown."   />
						</td>";	
				else echo"<input type='hidden' name='productionrm_bundle[$i]' id='productionrm_bundle[$i]' value='".$productionrm_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' ".$KeyDown."   />";
						
				echo "	<td width='65px;' align='center'><span class='".$TextColor."'>".$rowsdt[17]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRM[$i]' id='prddtqtyrm[$i]' value='".$rowsdt[6]."' maxlength='20' onKeyup='CalcPrdDtTotalRM($DecimalBase,$i);' class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
				
				if ($DisableIdeal == 0) {
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdealRM[$i]' id='prddtidealqtyrm[$i]' value='".$rowsdt[7]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalcRM[$i]' id='prddtwastecalcrm[$i]' value='".$rowsdt[11]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
					echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteRM[$i]' id='prddtwasterm[$i]' value='".$rowsdt[12]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtPrdDtQtyIdealRM[$i]' id='prddtidealqtyrm[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteCalcRM[$i]' id='prddtwastecalcrm[$i]' value='' >";
					echo"  <input type='hidden' name='txtPrdDtWasteRM[$i]' id='prddtwasterm[$i]' value='' >";
				}
				
				if ($FI_ShowCostPrice == 1) {
					echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPriceRM[$i]' id='costpricerm[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotalRM($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalRM[$i]' id='prddttotalrm[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"	<input type='hidden' name='txtPrdDtCostPriceRM[$i]' id='costpricerm[$i]' value='".$rowsdt[8]."' maxlength='20' onKeyup='CalcPrdDtTotalRM($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  >";
					echo"   <input type='hidden' name='txtPrdDtTotalRM[$i]' id='prddttotalrm[$i]' value='".$rowsdt[13]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." >";				
				
				}
				
				if ($DisableIdeal == 0) {
					echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdealRM[$i]' id='prddttotalidealrm[$i]' value='".$rowsdt[14]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				}	
				if ($FI_ShowCostPrice == 1) {
				echo"   <td width='230px;' align='center'><span>";
				echo"  		<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='1' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='1' || $rowsdt[22]=='') echo "checked />"; else echo " />"; echo" <font size=2>"; echo " Percentage";
				echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='2' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='2') echo "checked />"; else echo " />";  echo " Quantity";
				echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='3' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='3') echo "checked />"; else echo " />";  echo " Fixed";
				echo"	</span></td>";
				} else {
					echo"  		<span><input type='radio' hidden  name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='1' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='1' || $rowsdt[22]=='') echo "checked />"; else echo " />"; 
					echo"	 	<input type='radio' hidden name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='2' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='2') echo "checked />"; else echo " />";  
					echo"	 	<input type='radio'  hidden name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='3' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." "; if($rowsdt[22]=='3') echo "checked />"; else echo " /></span>";  
				}
				
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDtRM[$i]' id='projectcodedtrm[$i]' value='".$rowsdt[20]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCodeRM[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(2,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDtRM[$i]' id='projectcodedtrm[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDtRM[$i]' id='costcentcodedtrm[$i]' value='".$rowsdt[21]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCodeRM[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(2,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDtRM[$i]' id='costcentcodedtrm[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNoRM[$i]' id='batchnorm[$i]' value='".htmlspecialchars($rowsdt[19], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNoRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNoRM(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
							<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDateRM[$i]' id='expiredaterm[$i]' value='".$rowsdt[18]."' maxlength='10' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtPurDtExpireDateRM[$i]' id='expiredaterm[$i]' value=''>";
					echo" <input type='hidden' name='txtPurDtBatchNoRM[$i]' id='batchnorm[$i]' value=''>";
				}
					
				
				echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkRM[$i]' id='remarkdtrm[$i]' value='".htmlspecialchars($rowsdt[5], ENT_QUOTES)."'  maxlength='200'  class='".$ClassE."' style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";	
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_2[$i]' onclick=\"SubmitRecordGridRM('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		
							
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCodeRM[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCodeRM[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCodeRM[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCodeRM[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2['.$i.']\')"';
			$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_2[".$i."]','1');LeaveGrid('itemcoderm[$i]','itemcodefg[0]','','')\" ";		
		
				
			echo"<tr id='CurRec_G2$i' class='$bgcolor $TableFocus' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G2');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			}
			
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='140px;' align='left'>
						<input type='hidden' name='txtPrdRMId[$i]' id='prdrmid[$i]' value='$txtPrdRMId[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpirerm[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserialrm[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
						<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
						<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
						<input type='text' name='txtItemCodeRM[$i]' id='itemcoderm[$i]' value='$txtItemCodeRM[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'   onBlur=\"LeaveGrid('itemcoderm[$i]','itemcodefg[0]','','')\"  onchange=\"SetModRecord('ModRec_G2','$i'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-4'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCodeRM[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcoderm[$i]','itemdescriptionrm[$i]','items');\"  ".$KeyDown1.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCodeFG[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(9,$i); ' style='vertical-align:bottom;'/></span></label>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDescRM[$i]' id='itemdescriptionrm[$i]' value='$txtItemDescRM[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDescRM[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDescRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(9,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCodeRM[$i]' id='warehousecoderm[$i]' value='$txtWarehouseCodeRM[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCodeRM[$i]'); \" onkeyup=\"AutoCompleteData(event,this.id,'warehousecoderm[$i]','warehousenamerm[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouseRM($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
					
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionrm_heatno[$i]' id='productionrm_heatno[$i]' value='".$productionrm_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo1($i);' style='vertical-align:bottom;'/></span></label>
						</td>";	
				else echo"<input type='hidden' name='productionrm_heatno[$i]' id='productionrm_heatno[$i]' value='".$productionrm_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />";
				
				if($Module_Heatno==1)		
					echo"  	<td width='210px;' align='center'>
							<input type='text' name='productionrm_bundle[$i]' id='productionrm_bundle[$i]' value='".$productionrm_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' ".$KeyDown."   />
						</td>";	
				else echo"<input type='hidden' name='productionrm_bundle[$i]' id='productionrm_bundle[$i]' value='".$productionrm_bundle[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' ".$KeyDown."   />";
				
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1RM' id='itemunit1rm[$i]' value='$txtItemUnit1RM' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtPrdDtQtyRM[$i]' id='prddtqtyrm[$i]' value='$txtPrdDtQtyRM[$i]' maxlength='20' onKeyup='CalcPrdDtTotalRM($DecimalBase,$i);' class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." ></td>";
			
			if ($DisableIdeal == 0) {
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtQtyIdealRM[$i]' id='prddtidealqtyrm[$i]' value='$txtPrdDtQtyIdealRM[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteCalcRM[$i]' id='prddtwastecalcrm[$i]' value='$txtPrdDtWasteCalcRM[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' readonly ></td>";
				echo"  <td width='75px;' align='center'><input type='text' name='txtPrdDtWasteRM[$i]' id='prddtwasterm[$i]' value='$txtPrdDtWasteRM[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
			} else {
				echo"  <input type='hidden' name='txtPrdDtQtyIdealRM[$i]' id='prddtidealqtyrm[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWasteCalcRM[$i]' id='prddtwastecalcrm[$i]' value='' >";
				echo"  <input type='hidden' name='txtPrdDtWasteRM[$i]' id='prddtwasterm[$i]' value='' >";
			}
			if ($FI_ShowCostPrice == 1) {
				echo"	<td width='85px;' align='center'><input type='text' name='txtPrdDtCostPriceRM[$i]' id='costpricerm[$i]' value='$txtPrdDtCostPriceRM[$i]' maxlength='20' onKeyup='CalcPrdDtTotalRM($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalRM[$i]' id='prddttotalrm[$i]' value='$txtPrdDtTotalRM[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
				echo"	<input type='hidden' name='txtPrdDtCostPriceRM[$i]' id='costpricerm[$i]' value='$txtPrdDtCostPriceRM[$i]' maxlength='20' onKeyup='CalcPrdDtTotalRM($DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  >";
				echo"   <input type='hidden' name='txtPrdDtTotalRM[$i]' id='prddttotalrm[$i]' value='$txtPrdDtTotalRM[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." >";				
				}
			if ($DisableIdeal == 0) {
				echo"   <td width='105px;' align='center'><input type='text' name='txtPrdDtTotalIdealRM[$i]' id='prddttotalidealrm[$i]' value='$txtPrdDtTotalIdealRM[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
			}	
			if ($FI_ShowCostPrice == 1) {	
			echo"   <td width='230px;' align='center'><span>";
			echo"  		<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='1' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." checked />";echo" <font size=2>"; echo " Percentage";
			echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='2' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." />";  echo " Quantity";
			echo"	 	<input type='radio' name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='3' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." />";  echo " Fixed";
			echo"	</span></td>";
			} else {
			echo"  		<span><input type='radio'  hidden name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='1' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." checked />";
			echo"	 	<input type='radio' hidden  name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='2' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." />";  
			echo"	 	<input type='radio' hidden name='txtCostOptionRM[$i]' id='prdcostoptionrm[$i]' value='3' onchange=\"SetModRecord('ModRec_G2','$i'); \" ".$KeyDown." /></span>";  
			}
			
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDtRM[$i]' id='projectcodedtrm[$i]' value='$txtProjectCodeDtRM[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCodeRM[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(2,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDtRM[$i]' id='projectcodedtrm[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDtRM[$i]' id='costcentcodedtrm[$i]' value='$txtCostCentCodeDtRM[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCodeRM[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCodeRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(2,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDtRM[$i]' id='costcentcodedtrm[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				echo" 	<td width='105px;' align='center'><input type='text' name='txtPurDtBatchNoRM[$i]' id='batchnorm[$i]' value='".htmlspecialchars($txtPurDtBatchNoRM[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNoRM[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNoRM(1,$i); ' style='vertical-align:bottom;'/></span></label></td>
						<td width='85px;' align='center'><input type='text' name='txtPurDtExpireDateRM[$i]' id='expiredaterm[$i]' value='$txtPurDtExpireDateRM[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G2','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtPurDtExpireDateRM[$i]' id='expiredaterm[$i]' value=''>";
				echo" <input type='hidden' name='txtPurDtBatchNoRM[$i]' id='batchnorm[$i]' value=''>";
			}
				
				
			echo"	<td width='255px;'><input type='text' name='txtPrdDtRemarkRM[$i]' id='remarkdtrm[$i]' value='".htmlspecialchars($txtPrdDtRemarkRM[$i], ENT_QUOTES)."'  maxlength='200' class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";	
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_2[$i]' onclick=\"SubmitRecordGridRM('".$i."','".$filepath."', '".$form."', '".$PPrdId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PPrdId,'datagrid-1',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	 
	
	$query = " Select Count(*) FROM Temp_ProductionRM ".
			 " WHERE ProductionHD_Id = ".$PPrdId;
	$result = selectData($connection, $query);
	$txtPrdCountRM = $result[0];
	if($txtPrdCountRM == '') $txtPrdCountRM = 0;
	
	
	echo"<input type='hidden' name='txtPrdCountRM' value='$txtPrdCountRM' >";
	
}

function ShowProdGridSummary($connection,$PPrdId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	
	if ($PPrdId != "") {
		$sub_query = " Select ProductionSummaryRM.ProductionSummaryRM_Id, ProductionSummaryRM.Item_Code, Items.Item_Description, ".
					 " ifNull(ProductionSummaryRM_Quantity,0) As ActualQty, ifNull(ProductionSummaryRM_IdealQty,0) As IdealQty, ProductionSummaryRM_Multiplier, Items.Item_UnitSalesDesc ".
					 " From Temp_ProductionSummaryRM As ProductionSummaryRM Left Join Items on ProductionSummaryRM.Item_Code = Items.Item_code ".
					 " Where ProductionSummaryRM.ProductionHD_Id = '".$PPrdId."' ".
					 " Order By ProductionSummaryRM.Item_Code ";
		//echo $sub_query;
		
		echo"<table border='0' class='grid_data'>
			  <col width=20>
			  <col width=130>
			  <col width=350>
			  <col width=60>
			  <col width=100>
			  <col width=100>
			  <col width=100>";
					  
		echo"<tr height='20px;'>";
		echo"  <th align='left'><span>&nbsp;</span></th>";
		echo"  <th align='left'><span>Item Code</span></th>";
		echo"  <th align='left'><span>Description</span></th>";
		echo"  <th align='center'><span>Unit</span></th>";
		echo"  <th align='center'><span>Actual Qty</span></th>";
		echo"  <th align='center'><span>Ideal Qty</span></th>";
		echo"  <th align='center'><span>Multiplier</span></th>";
		echo"</tr>";		  
			
		$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());
		while($sub_rows=mysql_fetch_array($sub_result)){
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';
							
						
			echo "<tr class='$bgcolor' >";
			echo "	<td align='center' valign='center'><span><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateSummaryQty('".$sub_rows[0]."') title='Edit Record'  ></span></td>";
			echo "	<td align='left'><span>".$sub_rows[1]."</span></td>";
			echo "	<td align='left'><span>".$sub_rows[2]."</span></td>";
			echo "	<td align='center'><span>".$sub_rows[6]."</span></td>";
			echo "	<td align='right'><span>".number_format($sub_rows[3], $DecimalBase)."</span></td>";
			echo "	<td align='right'><span>".number_format($sub_rows[4], $DecimalBase)."</span></td>";
			echo "	<td align='right'><span>".number_format($sub_rows[5], $DecimalBase)."</span></td>";
			echo "</tr>";
				
			$i+=1;
		}
			
		echo "</table>";
		
	} 

}

function ShowProductionTotals($connection,$PPrdId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	
	if ($PPrdId != "") {
		$sub_query = " Select Round(ProductionHD_Total, ".$DecimalBase.") As ProductionTotal, ".
					 " Round(ProductionHD_IdealTotal, ".$DecimalBase.") As ProductionIdealTotal ".
					 " From Temp_ProductionHD As ProductionHD ".
					 " Where ProductionHD_ID = ".$PPrdId;
			 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtPrdTotal      		= $sub_result[0];
		$txtPrdTotalIdeal		= $sub_result[1];
		
		
		$sub_query = " Select sum(ProductionFG_Quantity*ProductionFG_Cost) from Temp_Productionfg where productionhd_id = ".$PPrdId;
		$sub_result = selectData($connection, $sub_query);
		$txtPrdTotalFG      		= $sub_result[0];
		
		$sub_query = " Select sum(ProductionRM_Quantity*ProductionRM_Cost) from Temp_ProductionRM where productionhd_id = ".$PPrdId;
		$sub_result = selectData($connection, $sub_query);
		$txtPrdTotalRM      		= $sub_result[0];
		
	} 
	
	$txtPrdTotalBal = $txtPrdTotalFG - $txtPrdTotalRM;
	
	echo"	<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
			<tr>
				<td id='total-top' style='width:240px;height:12px;'></td>
			</tr>
			<tr><td id='total-content' style='width:240px;height:12px;'>
				<table border='0' style='table-layout:fixed;' cellspacing=0 >
				<col width=90>
				<col width=140>
			
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Total Cost Actual:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='totalactual'  name='txtPrdTotal' value='$txtPrdTotal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style10'>Total Cost Ideal:</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='totalideal'  name='txtPrdTotalIdeal' value='$txtPrdTotalIdeal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				<tr><td valign='center' align='right' width='45%'><span class='style1'>Total Cost FG:</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='totalideal'  name='txtPrdTotalFG' value='$txtPrdTotalFG' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				
				<tr><td valign='center' align='right' width='45%'><span class='style1'>Total Cost RM:</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='totalideal'  name='txtPrdTotalRM' value='$txtPrdTotalRM' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				
					<tr><td valign='center' align='right' width='45%'><span class='style1'>Balance:</span>&nbsp;</td> 
					<td align='left' ><input type='text' id='totalideal'  name='txtPrdTotalBal' value='$txtPrdTotalBal' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
				</tr>
				
				
				</table></td>
			</tr><tr>
				<td id='total-bottom' style='width:240px;height:12px;'></td>
			</tr>
			
	 
			
			</table></td></tr>";
			


}


function ShowAdjustmentDtGrid($connection,$form,$PAdjId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	$Module_Unit1Method = $arrayparams['Module_Unit1Method'];
	$Module_Pack = $arrayparams['Module_Pack'];
	$Module_DoubleUnit = $arrayparams['Module_DoubleUnit'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$ShowCostCentLayout = $arrayparams['ShowCostCentLayout'];
	$Module_SerialNo = $arrayparams['Module_SerialNo'];
	$Module_ExpireDate = $arrayparams['Module_ExpireDate'];
	$FI_ShowCostPrice = $arrayparams['FI_ShowCostPrice'];
	
	$SetupDftWareHouse = $arrayparams['SetupDftWareHouse'];
	$defaultwarehouse  = $arrayparams['defaultwarehouse'];
	$Module_Heatno = $arrayparams['Module_Heatno'];	
	
	$DecimalBase = $arrayparams['DecimalBase'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate         = $arrayparams['txtDate'];
	$txtCurrCode     = $arrayparams['txtCurrCode'];
	$AdjustType      = $arrayparams['AdjustType'];

	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 50;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("itemcode", "itemdescription", "warehousecode");

	if($Module_Heatno==1)
		array_push($columnsarray, "adjustmentdt_heatno");

	 array_push($columnsarray, "adjdtquantity");
	 
	if ($Module_DoubleUnit == 1) array_push($columnsarray, 'unit2qty');
	if ($FI_ShowCostPrice == 1) array_push($columnsarray, 'costprice');	
	if ($Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) array_push($columnsarray, 'costcentcodedt');
	if ($Module_ExpireDate == 1) {
		array_push($columnsarray, 'batchno');
		array_push($columnsarray, 'expiredate');
	}	
	
	
	$columnsarrayall = array("itemcode", "itemdescription", "warehousecode", "adjdtquantity", "unit2qty", "costprice", "adjdttotal", "projectcodedt", "costcentcodedt", "expiredate", "batchno", "adjdtid", "existingqty", "bookingqty", "availableqty", "itemunit1", "itemunit2", "itemvat", "itemexpire", "itemunitcoef");
	$orderbyarray = array("AdjustmentDt.Item_CodePacked", "Items.Item_Code", "Items.Item_Description", "AdjustmentDt.Warehouse_Code", "Items.Item_UnitSalesDesc", "Items.Item_UnitPurchaseDesc");
	
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

		
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
	
	$TableWidth = 690;
	if ($Module_SerialNo == 1)     $TableWidth += 25;
	if ($Module_Pack == 1)         $TableWidth += 125;
	if ($Module_DoubleUnit == 1)   $TableWidth += 145;
	if ($Module_SerialNo == 1)     $TableWidth += 55;
	if ($FI_ShowCostPrice == 1)    $TableWidth += 190;
	if ($Module_ProjectDt == 1)    $TableWidth += 160;
	if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) $TableWidth += 160;
	if ($Module_ExpireDate == 1)   $TableWidth += 190;
	
	$TableWidthStyle = 'width:'.$TableWidth.'px;';
	
	echo"<div id='container' style='position: relative;z-index:1;' >";
	echo"	<div id='Loader' class=''></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	
		echo"<tr height='25px'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		}	
		  
		if ($Module_Pack == 1)
			echo"  <th width='125px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','AdjustmentDt.Item_CodePacked','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code Packed".$orderimg1."</span></th>";
			
		echo"  <th width='165px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Item Code".$orderimg2."</span></th>";
		echo"  <th width='205px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Description".$orderimg3."</span></th>";
		echo"  <th width='105px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','AdjustmentDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Warehouse".$orderimg4."</span></th>";
		
		if($Module_Heatno==1)
		echo"  <th width='210px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','AdjustmentDt.Warehouse_Code','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>HeatNo</span></th>";
	
		
		echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitSalesDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit1".$orderimg5."</span></th>";
		echo"  <th width='75px;' align='center'><span>Quantity</span></th> ";	
		  
		if ($Module_DoubleUnit == 1) {
			echo"  <th width='65px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PAdjId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_UnitPurchaseDesc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Unit2".$orderimg6."</span></th>";
			echo"  <th width='80px;' align='center'><span>Unit2 Qty</span></th>";
		}
		
		if ($Module_SerialNo == 1) {
			echo"  <th width='55px;' align='center'><span>S/N Bal</span></th>";
		}
		  
		if ($FI_ShowCostPrice == 1) {  
			echo"  <th width='85px;' align='center'><span>Cost Price</span></th>";
			echo"  <th width='105px;' align='center'><span>Total</span></th>";
		}	
		
		if ($Module_ProjectDt == 1) {
			echo"  <th width='160px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
			echo"  <th width='160px;' align='center'><span>Costcenter</span></th>";	
		}
		
		if ($Module_ExpireDate == 1) {
			echo"  <th width='105px;' align='center'><span>Batch No</span></th>";	
			echo"  <th width='85px;' align='center'><span>Expire Date</span></th>";	
		}
	
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>";  
		
			 
		
		
		if ($PAdjId != '' ) {
		      
			$TotalQty = 0;
			$i_nonstock  = 0;
			$i_zeroprice = 0;
			$i_minprice  = 0;
			$i_unitcoef  = 1;
			$i_expire    = 0;

					   
			$query = " Select AdjustmentDt.AdjustmentDt_ID, AdjustmentDt.Item_CodePacked, AdjustmentDt.Item_code, Item_Description, AdjustmentDt.Warehouse_Code, ".
					 " AdjustmentDt_Quantity, Round(AdjustmentDt_Price, ".$DecimalBase."), AdjustmentDt_PriceBase1, AdjustmentDt_PriceBase2, ".
					 " Round((ifNull(AdjustmentDt_Quantity,0)*ifNull(AdjustmentDt_Price,0)), ".$DecimalBase.") As TotalAmount, ".
					 " AdjustmentDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, AdjustmentDt.Project_code, AdjustmentDt.CostCent_Code,  ".
					 " ifNull(item_withserialno,0) As ItemWithSerialNo, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(AdjustmentDt_ExpireDate,'%d/%m/%Y') As AdjustmentDt_ExpireDate, AdjustmentDt_BatchNo, AdjustmentDt.Journal_Type,adjustmentdt_heatno ".
					 " From Temp_AdjustmentDt As AdjustmentDt Left Join Items on AdjustmentDt.Item_code = Items.Item_code ".
					 " Left Join Temp_Purchasesserialno on AdjustmentDt.AdjustmentDt_ID = Temp_Purchasesserialno.PurchaseDt_ID ".
					 " And Temp_Purchasesserialno.Journal_Type = AdjustmentDt.Journal_Type ".
					 " And Temp_Purchasesserialno.AdjustmentDt_ID Is Not Null ".
					 " Where AdjustmentDt.Adjustment_ID = ".$PAdjId.
					 " Group By  AdjustmentDt.AdjustmentDt_ID ".
					 " Order By $orderby $direction ";
					 
			//echo $query;
					
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);

			$query = $query." limit ".$startrange.",".$perpage;
			$resultdt=mysql_query($query,$connection);
			
			
			while($rowsdt=mysql_fetch_array($resultdt)){
			    $adjustmentdt_heatno[$i]=$rowsdt[20];			
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$TextColor = 'style7';
				
				$sub_query = " SELECT ifNull(Item_Pack, 0), ifNull(item_zeroPrice, 0), Item_UnitSalesDesc, Item_UnitPurchaseDesc, ifNull(Item_Unit,1), ifNull(Item_NonStock, 0), ifNull(Item_MinimumPrice, 0), ifNull(Item_Expire,0), ifNull(item_withserialno,0) ".
							 " FROM Items ".
							 " WHERE Items.Item_Code = '".$rowsdt[2]."'";
					
				//echo "<br><br>".$sub_query;
				$sub_result = selectData($connection, $sub_query);
				$i_pack       = $sub_result[0];
				$i_zeroprice  = $sub_result[1];
				$i_unit1      = $sub_result[2];
				$i_unit2 	  = $sub_result[3]; 
				$i_unitcoef	  = $sub_result[4];
				$i_nonstock   = $sub_result[5];
				$i_minprice   = $sub_result[6];
				$i_expire	  = $sub_result[7];
				$i_withserial = $sub_result[8];
				
				
				$txtItemWithSerial[$i] = $i_withserial;
				$txtItemUnitCoef[$i]   = $i_unitcoef;
				$txtItemExpire[$i]     = $i_expire;
				
				
				$Qry_QtyBookTemp = "SELECT ifnull(QtyBooking_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0]),0) ";
				$result = selectData($connection, $Qry_QtyBookTemp);
				if ($result[0] < 0){$result[0] = 0;}
				$min_book_qty = $result[0];
								 
				$Qry_QtyExists = " SELECT ifnull(QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], '".$rowsdt[19]."', '', ''),0) As QtyExists ";				 
				$result = selectData($connection, $Qry_QtyExists);
				$min_exists_qty = $result[0];
				//echo $Qry_QtyExists;
				
				$Qry_Unit2QtyExists = " SELECT ifnull(Unit2QtyExists('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], '".$rowsdt[19]."', '', ''),0) As QtyExists ";
				$result = selectData($connection, $Qry_Unit2QtyExists);
				$min_exists_unit2qty = $result[0];
				//echo $Qry_Unit2QtyExists;
				
				$Qry_QtyAvailable = " Select QtyExistsWSorder_Temp('".$rowsdt[2]."', '".$rowsdt[4]."', '".insertMySQLDate($txtDate)."', $rowsdt[0], '".$rowsdt[19]."', '', '', 0) QtyExists From Dual ";
				$result = selectData($connection, $Qry_QtyAvailable);
				$min_available_qty = $result[0];
				//echo $Qry_QtyAvailable;
		
				$Qry_QtyInTemp = " Select ifNull(Sum(ifNull(AdjustmentDt_Quantity,0)),0) FROM Temp_AdjustmentDt ".
								 " WHERE Item_code = '".$rowsdt[2]."'  And  Warehouse_code = '".$rowsdt[4]."' And Adjustment_ID = $PAdjId And AdjustmentDt_ID != ifNull('".$rowsdt[0]."',0) ";
				$result = selectData($connection, $Qry_QtyInTemp);
				$QtyTemp_Adj = $result[0];
				
					
				$min_available_qty = $min_available_qty + $QtyTemp_Adj;
				$min_exists_qty = $min_exists_qty + $QtyTemp_Adj;
				$min_book_qty = $min_book_qty;
		
				$txtBookingQty[$i]    = $min_book_qty;
				$txtAvailableQty[$i]  = $min_available_qty;
				$txtExistingQty[$i]   = $min_exists_qty;
				$txtExistUnit2Qty[$i] = $min_exists_unit2qty;


				
				
				//Checking For Difference Btw Qty and Count SerialNo
				if (($rowsdt[5] != $rowsdt[16] && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[5] == 0 && $rowsdt[15] != 0 && $Module_SerialNo == 1) || ($rowsdt[5] == 0)) {
					$RecStyle = "style='background-color:#FF0000;'";
				}
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
				echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;' $RecStyle>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PAdjId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				// If ItemWithSerialNo True And ModuleSerialNo True	
				if ($rowsdt[15] == 1 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'><img src='img/button_addserial.png' onMouseOver=\"style.cursor='hand'\" onclick=\"AddSerialNo('".$rowsdt[0]."','".htmlentities($rowsdt[2])."','".$rowsdt[5]."','".$rowsdt[4]."')\" title='Add Serial Nos'  ></td>";
				}
				else if ($rowsdt[15] == 0 && $Module_SerialNo == 1) {
					echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
				}
				
				if ($Module_Pack == 1)
					echo "	<td width='125px;' align='left'><span class='".$TextColor."'>".$rowsdt[1]."</span></td>";
					
				echo "	<td width='165px;' align='left'>
							<input type='hidden' name='txtAdjDtId[$i]' id='adjdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
							<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
							<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
							<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
							<input type='hidden' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' >
							<input type='hidden' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' >
							<input type='hidden' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' >
							<input type='hidden' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' >
							<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
							<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='".$rowsdt[2]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:110px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" ".$KeyDown.">
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
							<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtExistingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtBookingQty[$i],2)."</span></td></tr>
									<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><span class='style1'>".number_format($txtAvailableQty[$i],2)."</span></td></tr>";
									
							if ($Module_DoubleUnit == 1 && $rowsdt[15] != $rowsdt[16] && $rowsdt[15] != '') {
								echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><span class='style1'>".$rowsdt[15] ."  ".number_format($txtExistUnit2Qty[$i],2)."</span></td></tr>";
							}
									
				echo"			</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='105px;' align='center'>
							<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='".$rowsdt[4]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\" ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
						
						
				if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='adjustmentdt_heatno[$i]' id='adjustmentdt_heatno[$i]' value='".$adjustmentdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
				else echo"<input type='hidden' name='adjustmentdt_heatno[$i]' id='adjustmentdt_heatno[$i]' value='".$adjustmentdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />";
						
				echo "	<td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[12]."</span></td>";
				echo"	<td width='75px;' align='center'><input type='text' name='txtAdjDtQty[$i]' id='adjdtquantity[$i]' value='".$rowsdt[5]."' maxlength='20'  class='".$ClassE."' style='font-size:12px; font-weight:bold; text-align:center; border-width:0px; width:70px; height:20px;' onKeyup=\"CalcAdjDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); SetOutQty(1,$DecimalBase,$i); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
				if ($Module_DoubleUnit == 1) { 
					echo"  <td width='65px;' align='left'><span class='".$TextColor."'>".$rowsdt[11]."</span></td>";	
					echo"  <td width='80px;' align='center'><input type='text' name='txtAdjDtQty2[$i]' id='unit2qty[$i]' value='".$rowsdt[10]."' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo"  <input type='hidden' name='txtAdjDtQty2[$i]' id='unit2qty[$i]' value='' >";
				}
				
				if ($Module_SerialNo == 1) {
					echo"  <td width='55px;' align='center'><span class='".$TextColor."'>".$rowsdt[16]."</span></td>";
				}
				
				if ($FI_ShowCostPrice == 1) {
					echo"	<td width='85px;' align='center'><input type='text' name='txtAdjDtPrice[$i]' id='costprice[$i]' value='".$rowsdt[6]."' maxlength='20' onKeyup='CalcAdjDtTotal(2,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
					echo"   <td width='105px;' align='center'><input type='text' name='txtAdjDtTotal[$i]' id='adjdttotal[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." readonly ></td>";
				} else {
					echo" <input type='hidden' name='txtAdjDtPrice' id='costprice[$i]' value='' >
						  <input type='hidden' name='txtAdjDtTotal' id='adjdttotal[$i]' value='' >";
				}
				
				if ($Module_ProjectDt == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[13]."' class='".$ClassE."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
				}
				if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
					echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[14]."' class='".$ClassE."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
				}
				
				if ($Module_ExpireDate == 1) {
					if ($AdjustType == 'I') {
						echo" 	<td width='105px;' align='center'><input type='text' name='txtAdjDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[18], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
					} else {
						echo" 	<td width='105px;' align='center'><input type='text' name='txtAdjDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($rowsdt[18], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
								<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";
					}
								
					echo"	<td width='85px;' align='center'><input type='text' name='txtAdjDtExpireDate[$i]' id='expiredate[$i]' value='".$rowsdt[17]."' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
				} else {
					echo" <input type='hidden' name='txtAdjDtExpireDate[$i]' id='expiredate[$i]' value=''>";
					echo" <input type='hidden' name='txtAdjDtBatchNo[$i]' id='batchno[$i]' value=''>";
				}
				
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PAdjId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PAdjId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
				
				$i+=1;
				
				$LastItemWarehouse = $rowsdt[4];
			}
			
			
		}
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	
			
			if (empty($txtWarehouseCode[$i])) {
				if (!empty($LastItemWarehouse))
					$txtWarehouseCode[$i] = $LastItemWarehouse;
				else if (!empty($SetupDftWareHouse))
					$txtWarehouseCode[$i] = $SetupDftWareHouse;
				else	
					$txtWarehouseCode[$i] = $defaultwarehouse;
			}

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
			
				
			echo"<tr id='CurRec$i' class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			if ($Module_SerialNo == 1) {
				echo "	<td width='25px;' align='center' valign='center'>&nbsp;</td>";
			} 
			if ($Module_Pack == 1)
				echo"  <td width='125px;' align='center'><span>&nbsp;</span></td>";
				
			echo "	<td width='165px;' align='left'>
						<input type='hidden' name='txtAdjDtId[$i]' id='adjdtid[$i]' value='$txtAdjDtId[$i]' >
						<input type='hidden' name='txtItemVat[$i]' id='itemvat[$i]' value='$txtItemVat[$i]' >
						<input type='hidden' name='txtItemExpire[$i]' id='itemexpire[$i]' value='$txtItemExpire[$i]' >
						<input type='hidden' name='txtItemWithSerial[$i]' id='itemwithserial[$i]' value='$txtItemWithSerial[$i]' >
						<input type='hidden' name='txtItemUnitCoef[$i]' id='itemunitcoef[$i]' value='$txtItemUnitCoef[$i]' >
						<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv$i');\" style='vertical-align:bottom;'/></span></label>
						<input type='text' name='txtItemCode[$i]' id='itemcode[$i]' value='$txtItemCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:110px; height:20px;'  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','items','item_code',this.value,'lstItemCode[$i]'); GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','-2'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\"  ".$KeyDown.">
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItems(1,$i); ' style='vertical-align:bottom;'/></span></label>
						<div id='slidingDiv$i' class='slidingDiv slidingDivHide'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='100px;'><span class='style1'>Existing Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistingQty[$i]' id='existingqty[$i]' value='$txtExistingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Booking Qty</span></td><td width='100px;' align='right'><input type='text' name='txtBookingQty[$i]' id='bookingqty[$i]' value='$txtBookingQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>
								<tr height='25px;'><td width='100px;'><span class='style1'>Available Qty</span></td><td width='100px;' align='right'><input type='text' name='txtAvailableQty[$i]' id='availableqty[$i]' value='$txtAvailableQty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
								
						if ($Module_DoubleUnit == 1) {
							echo"<tr height='25px;'><td width='100px;'><span class='style1'>Existing Unit2Qty</span></td><td width='100px;' align='right'><input type='text' name='txtExistUnit2Qty[$i]' id='exitunit2qty[$i]' value='$txtExistUnit2Qty[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:100px; height:20px;' readonly></td></tr>";
						}	
						
			echo"			</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
									
			echo"  	<td width='205px;' align='center'><input type='text' name='txtItemDesc[$i]' id='itemdescription[$i]' value='$txtItemDesc[$i]'  class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"CheckLookup('chkcodeexists','items','item_description',this.value,'lstItemDesc[$i]');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Item' id='lstItemDesc[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistItemsDesc(1,$i); ' style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='105px;' align='center'>
						<input type='text' name='txtWarehouseCode[$i]' id='warehousecode[$i]' value='$txtWarehouseCode[$i]'  maxlength='20'  class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','warehouse','warehouse_code',this.value,'lstWarehouseCode[$i]'); GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'warehousecode[$i]','warehousename[$i]','warehouse');\"  ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Warehouse' id='lstWarehouseCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistWarehouse($i);' style='vertical-align:bottom;'/></span></label>
					</td>";
					
			if($Module_Heatno==1)		
				echo"  	<td width='210px;' align='center'>
							<input type='text' name='adjustmentdt_heatno[$i]' id='adjustmentdt_heatno[$i]' value='".$adjustmentdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />
							<label><span><img src='img/openlist.png' title='Select HeatNo' id='lstHeatNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistHeatNo($i);' style='vertical-align:bottom;'/></span></label>
						</td>";
			else echo"<input type='hidden' name='adjustmentdt_heatno[$i]' id='adjustmentdt_heatno[$i]' value='".$adjustmentdt_heatno[$i]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:185px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');  ".$KeyDown."   />";
						
			echo"   <td width='65px;' align='center'><input type='text' name='txtItemUnit1' id='itemunit1[$i]' value='$txtItemUnit1' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
			echo"	<td width='75px;' align='center'><input type='text' name='txtAdjDtQty[$i]' id='adjdtquantity[$i]' value='$txtAdjDtQty[$i]' maxlength='20'  class='".$ClassI."' style='font-size:12px; font-weight:bold; text-align:center; width:70px; height:20px;' onKeyup=\"CalcAdjDtTotal(1,$DecimalBase,$i);\" onchange=\"SetModRecord('ModRec_G1','$i'); SetOutQty(1,$DecimalBase,$i); CalcQty2($Module_Unit1Method,1,$i);\" ".$KeyDown." ></td>";
					
				
		  
			if ($Module_DoubleUnit == 1) {
				echo"  <td width='65px;' align='center'><input type='text' name='txtItemUnit2' id='itemunit2[$i]' value='$txtItemUnit2' maxlength='20' class='".$ClassI."'  style='text-align:right; font-size:12px; width:60px; height:20px;' readonly></td>";
				echo"  <td width='80px;' align='center'><input type='text' name='txtAdjDtQty2[$i]' id='unit2qty[$i]' value='$txtAdjDtQty2[$i]' maxlength='20' onchange=\"CalcQty2($Module_Unit1Method,2,$i); SetModRecord('ModRec_G1','$i');\" class='".$ClassI."'  style='font-size:12px; width:75px; height:20px;' ".$KeyDown." ></td>";
			} else {
				echo "<input type='hidden' name='txtAdjDtQty2[$i]' id='unit2qty[$i]' value='' >";
			}
			
			if ($Module_SerialNo == 1) {
				echo"  <td width='55px;' align='center'><span class='".$TextColor."'>&nbsp;</span></td>";
			}
			
				
			if ($FI_ShowCostPrice == 1) {
				echo"	<td width='85px;' align='center'><input type='text' name='txtAdjDtPrice[$i]' id='costprice[$i]' value='$txtAdjDtPrice[$i]' maxlength='20' onKeyup='CalcAdjDtTotal(2,$DecimalBase,$i);' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' ".$KeyDown."  ></td>";
				echo"   <td width='105px;' align='center'><input type='text' name='txtAdjDtTotal[$i]' id='adjdttotal[$i]' value='$txtAdjDtTotal[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:100px; height:20px;' ".$KeyDown." readonly ></td>";
			} else {
				echo" <input type='hidden' name='txtAdjDtPrice' id='costprice[$i]' value='' >
					  <input type='hidden' name='txtAdjDtTotal' id='adjdttotal[$i]' value='' >";
			}
				
			if ($Module_ProjectDt == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' >";
			}
			if ($Module_CostCenterDt == 1 && $ShowCostCentLayout == 1) {
				echo"<td width='160px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;' maxlength='15' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
						<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
			} else {
				echo"  <input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' >";
			}
			
			if ($Module_ExpireDate == 1) {
				if ($AdjustType == 'I') {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtAdjDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtAdjDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' ".$KeyDown." ></td>";
				} else {
					echo" 	<td width='105px;' align='center'><input type='text' name='txtAdjDtBatchNo[$i]' id='batchno[$i]' value='".htmlspecialchars($txtAdjDtBatchNo[$i], ENT_QUOTES)."'  maxlength='40' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' ".$KeyDown." >
							<label><span><img src='img/openlist.png' title='Select Batch No' id='lstBatchNo[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistBatchNo(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";
				}
				echo"	<td width='85px;' align='center'><input type='text' name='txtAdjDtExpireDate[$i]' id='expiredate[$i]' value='$txtAdjDtExpireDate[$i]' maxlength='10' onchange=\"SetModRecord('ModRec_G1','$i');\" class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' ".$KeyDown." ></td>"; 	 
			} else {
				echo" <input type='hidden' name='txtAdjDtExpireDate[$i]' id='expiredate[$i]' value=''>";
				echo" <input type='hidden' name='txtAdjDtBatchNo[$i]' id='batchno[$i]' value=''>";
			}
			
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PAdjId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PAdjId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$Module_ExpireDate,$Module_SerialNo); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
		

	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PAdjId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowAdjustmentTotals($connection,$PAdjId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	$txtCurr = $arrayparams['txtCurrCode']; 
	
	$FI_ShowCostPrice = $arrayparams['FI_ShowCostPrice']; 
	
	if ($PAdjId != "") {
		$sub_query = " Select Round(Adjustment_Amount, ".$DecimalBase.") As AdjustmentAmount, ".
					 " Round(Adjustment_AmountBase1, ".$DecimalBase1.") As AdjustmentAmountBase1, ".
					 " Round(Adjustment_AmountBase2, ".$DecimalBase2.") As AdjustmentAmountBase2 ".
					 " From Temp_Adjustment As Adjustment ".
					 " Where Adjustment_ID = ".$PAdjId;
			 
		//echo $sub_query;
		$sub_result = selectData($connection, $sub_query);
		
		$txtAdjAmount      		= $sub_result[0];
		$txtAdjAmountBase1		= $sub_result[1];
		$txtAdjAmountBase2   	= $sub_result[2];
		
	} 
	
	
	if ( $FI_ShowCostPrice==1) {
	echo"	<table border='0' style='table-layout:fixed;width:100px' cellspacing=0>
		<tr>
		<td id='total-top' style='width:240px;height:12px;'></td>
		</tr>
		<tr><td id='total-content' style='width:240px;height:12px;'>
		<table valign='top' border='0' cellspacing=0 cellpadding=0 style='width:240px;'>
		
			<col width=90>
			<col width=140>
				<tr height='25px'>
					<td valign='center' align='right' ><span class='style10'>Amount:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='adjamount'  name='txtAdjAmount' value='$txtAdjAmount' maxlength='20' style='text-align:right; width:120px; height:20px;background:#C6D4E5;";
					
					if($txtAdjAmount<0) echo " color:#FF0000;";
					
				echo" ' readonly></td>
				</tr>
				<tr height='25px'>
					<td valign='center' align='right' ><span class='style10'>Amount $Base1:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='adjamountb1'  name='txtAdjAmountBase1' value='$txtAdjAmountBase1' maxlength='20' style='text-align:right; width:120px; height:20px;background:#C6D4E5;";
					
				if($txtAdjAmountBase1<0) echo " color:#FF0000;";	
					
					echo" ' readonly></td>
				</tr>
				<tr height='25px'>
					<td valign='center' align='right' ><span class='style10'>Amount $Base2:</span>&nbsp;</td>
					<td align='left' ><input type='text' id='adjamountb2'  name='txtAdjAmountBase2' value='$txtAdjAmountBase2' maxlength='20' style='text-align:right; width:120px; height:20px;background:#C6D4E5;";
					
				if($txtAdjAmountBase2<0) echo " color:#FF0000;";		
					
					echo" ' readonly></td>
				</tr>";
		  
	 echo" </table></td>
				</tr><tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr></table></td></tr>";
			
	} else {
		echo"<input type='hidden' id='adjamount'  name='txtAdjAmount' value='$txtAdjAmount' maxlength='20' />";
		echo"<input type='hidden' id='adjamountb1'  name='txtAdjAmountBase1' value='$txtAdjAmountBase1' maxlength='20' />";
		echo"<input type='hidden' id='adjamountb2'  name='txtAdjAmountBase2' value='$txtAdjAmountBase2' maxlength='20' />";
	}

}

function ShowJournalDtGrid($connection,$form,$PJourId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	$ErrorRecId  = $arrayparams['ErrorRecId'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 1000;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	$columnsarray = array("ledgernumber", "ledgername", "journaldtdesc", "currencycode", "journaldtdbcd", "journaldtamount", "journaldtamount1", "journaldtamount2", "journaldtvaluedate", "journaldtmanualno", "journaldtdesc1");
	$columnsarrayall = array("ledgernumber", "ledgername", "journaldtdesc", "journaldtdesc1", "currencycode", "journaldtdbcd", "journaldtamount", "journaldtamount1", "journaldtamount2", "journaldtvaluedate", "journaldtmanualno", "jourdtid");
	$orderbyarray = array("Ledger.Ledger_Number", "Ledger.Ledger_Name", "JournalDt.Journaldt_desc", "JournalDt.Journaldt_desc1", "JournalDt.Journaldt_ValueDate");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
		
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

/// grid Motion
		$Motion = 1;
		if ($Module_Project == 1 || $Module_ProjectDt == 1){
			$NextCell = 'projectcodedt[0]';
			$TabIndex = 0 ;
			}
		else	
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1){
			$NextCell = 'costcentcodedt[0]';
			$TabIndex = 0 ;
			
			}
		else {
				$NextCell = 'chkjournumber[0]';	
				$TabIndex = 0 ;
			}	
		
	
	
	/////////////	
	
	
	$query = " Select JournalDt.Journaldt_id, JournalDt.journal_id, Ledger.Ledger_Number, Ledger.Ledger_Name, JournalDt.Journaldt_desc, JournalDt.Journaldt_desc1, ".
			 " Journaldt_dbcd, Round(ifNull(Journaldt_amount,0), ".$DecimalBase."), Round(ifNull(Journaldt_amount1,0), ".$DecimalBase1."),".
			 " Round(ifNull(Journaldt_amount2,0), ".$DecimalBase2."), date_format(Journaldt_ValueDate,'%d/%m/%Y'), JournalDt.currency_code, JournalDt.manual_no, ".
			 " ifNull(Journaldt_amount,0), ifNull(Journaldt_amount1,0), ifNull(Journaldt_amount2,0) ".
			 " From Temp_JournalDt As JournalDt ".
			 " Inner Join Ledger on JournalDt.Ledger_Number = Ledger.Ledger_Number ".
			 " Where JournalDt.Journal_ID = ".$PJourId.
			 " Order By $orderby $direction ";
	
	//echo $query;
			
	$result=mysql_query($query,$connection);
	$totalrows = mysql_num_rows($result);

	$query = $query." limit ".$startrange.",".$perpage;
	$result=mysql_query($query,$connection);
	
	
	echo"<div id='container' style='position:relative; z-index:1;' >";
	echo"	<div id='Loader' class=''><div></div></div>";
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:1210px;'>";
	
		echo"<tr height='30px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='110px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PJourId."', 'datagrid', 1, '".$dftorderby."', 'COR','Ledger.Ledger_Number','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Ledger No". $orderimg1."</span><span class='style2'>*</span></th>";
		echo"  <th width='150px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PJourId."', 'datagrid', 1, '".$dftorderby."', 'COR','Ledger.Ledger_Name','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Ledger Name". $orderimg2."</span></th>";
		echo"  <th width='220px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PJourId."', 'datagrid', 1, '".$dftorderby."', 'COR','JournalDt.Journaldt_desc','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Desc". $orderimg3."</span></th>";
		  
			
		echo"  <th width='60px;' align='center'><span>Curr.</span></th> ";	
		echo"  <th width='40px;' align='center'><span>D/C</span></th>";	
		echo"  <th width='90px;' align='center'><span>Amount</span></th>";
		echo"  <th width='90px;' align='center'><span>".$Base1."</span></th> ";
		echo"  <th width='90px;' align='center'><span>".$Base2."</span></th>";
		echo"  <th width='85px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PJourId."', 'datagrid', 1, '".$dftorderby."', 'COR','JournalDt.Journaldt_ValueDate','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Value Date". $orderimg5."</span><span class='style2'>*</span></th>";
		echo"  <th width='100px;' align='center'><span>Manual No</span></th> ";
		echo"  <th width='200px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PJourId."', 'datagrid', 1, '".$dftorderby."', 'COR','JournalDt.Journaldt_desc1','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Desc1". $orderimg4."</span></th>";
		  
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:1210px;'>";
		echo"<input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
			 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>"; 
		
			 
			 
		if ($PJourId != '' ) {
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				if ($ErrorRecId != "" && $ErrorRecId == $rowsdt[0]) $bgcolor='bgcolor3';
					
				$querycurrency="select currency_code from currency order by currency_code";
				$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
			
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
				
			
				echo"<tr id='CurRec$i' onclick=\"LoadDetails('".$filepath."','".$form."','LIST','datagrid','".$PJourId."','".$rowsdt[0]."','".$i."'); \" class='$bgcolor table-focus-g1'>";		
				echo"	<td width='25px;'>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."', '".$PJourId."','".$rowsdt[0]."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='110px;' align='left'>
							<input type='hidden' name='txtJourDtId[$i]' id='jourdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtJourDtAmount[$i]'  id='journaldtamount_h[$i]'  value='".$rowsdt[13]."' >
							<input type='hidden' name='txtJourDtAmount1[$i]' id='journaldtamount1_h[$i]' value='".$rowsdt[14]."' >
							<input type='hidden' name='txtJourDtAmount2[$i]' id='journaldtamount2_h[$i]' value='".$rowsdt[15]."' >
							<input type='text' name='txtLedgerNo[$i]' id='ledgernumber[$i]' value='".$rowsdt[2]."'  maxlength='10' class='".$ClassE."' style='font-size:12px; width:80px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','ledger','ledger_number',this.value,'lstLedgerCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'ledgernumber[$i]','ledgername[$i]','ledgers');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedger(17,1,'all',0,$i);\" style='vertical-align:bottom;'/></span></label>
						</td>";
										
				echo"  	<td width='150px;' align='center'><input type='text' name='txtLedgerName[$i]' id='ledgername[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:145px; height:20px;' onchange=\"CheckLookup('chkcodeexists','ledger','ledger_name',this.value,'lstLedgerName[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerName[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedgerName(17,1,'all',0,$i);\" style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
				echo"  	<td width='220px;' align='center'><input type='text' name='txtJourDtDesc[$i]' id='journaldtdesc[$i]' value='".$rowsdt[4]."'  maxlength='100' class='".$ClassE."' style='font-size:12px; width:215px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\"  ".$KeyDown."   /></td>";
				
				echo"   <td width='60px;' align='center'><select name='txtCurrencyCode[$i]' id='currencycode[$i]' class='".$ClassE."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(4,$DecimalBase1,$DecimalBase2,this.value,'".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." >";
				echo"	<option></option>";
				while($curf=mysql_fetch_row($resultcurrency)){
					echo"<option ";
					if ($rowsdt[11]==$curf[0])	echo " selected ";
					echo" value='$curf[0]'>$curf[0] </option>";
				}
				echo"	</select></td>";	
				
				if ($rowsdt[6] == 'D') { $selecteddb = 'selected'; $selectedcd = ''; }
				else if ($rowsdt[6] == 'C') { $selecteddb = ''; $selectedcd = 'selected'; }
				echo"   <td width='40px;' align='center'>
						<select name='txtJourDtdbcd[$i]' id='journaldtdbcd[$i]' class='".$ClassE."' style='font-size:12px; width:35px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." >
							<option value='D' $selecteddb>D</option>
							<option value='C' $selectedcd>C</option>
						</select>
						</td>";
				  
				echo"	<td width='90px;' align='center'><input type='text' name='txtJourDtAmount_D[$i]' id='journaldtamount[$i]' value='".$rowsdt[7]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:85px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(1,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='90px;' align='center'><input type='text' name='txtJourDtAmount1_D[$i]' id='journaldtamount1[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:85px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(2,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='90px;' align='center'><input type='text' name='txtJourDtAmount2_D[$i]' id='journaldtamount2[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:85px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(3,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"   <td width='85px;'><input type='text' name='txtValueDate[$i]' id='journaldtvaluedate[$i]' value='".$rowsdt[10]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"   <td width='100px;'><input type='text' name='txtJourDtManualNo[$i]' id='journaldtmanualno[$i]' value='".$rowsdt[12]."' maxlength='40' class='".$ClassE."' style='font-size:12px; width:95px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." /></td>";
				echo"  	<td width='200px;' align='center'><input type='text' name='txtJourDtDesc1[$i]' id='journaldtdesc1[$i]' value='".$rowsdt[5]."' maxlength='100' class='".$ClassE."' style='font-size:12px; width:190px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown."  /></td>";
				
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PJourId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PJourId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,'$NextCell',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo "</tr>";
						
	
				
				$i+=1;
				
				
			}
			
		}
		
		
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit['.$i.']\')"';
					
			//Initializations
			if (empty($Journal_DefaultCur))  $Journal_DefaultCur = $Base1;

			if (empty($txtCurrencyCode[$i])) $txtCurrencyCode[$i] = $Journal_DefaultCur;
			if (empty($txtValueDate[$i]))    $txtValueDate[$i] = displayToday();
			if (empty($txtJourDtdbcd[$i]))	 $txtJourDtdbcd[$i]='D';
			

			$querycurrency="select currency_code from currency order by currency_code";
			$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
			
			
			$sub_query2 = " Select JournalDt.Journal_ID, ".
						  " Round(Sum(if(Journaldt_dbcd='D', ifNull(JournalDt_Amount ,0), 0)) - Sum(if(Journaldt_dbcd='C', ifNull(JournalDt_Amount ,0), 0)), ".$DecimalBase.") As JournalDtAmount_D, ".
						  " Round(Sum(if(Journaldt_dbcd='D', ifNull(JournalDt_Amount1,0), 0)) - Sum(if(Journaldt_dbcd='C', ifNull(JournalDt_Amount1,0), 0)), ".$DecimalBase1.") As JournalDtAmount1_D, ".
						  " Round(Sum(if(Journaldt_dbcd='D', ifNull(JournalDt_Amount2,0), 0)) - Sum(if(Journaldt_dbcd='C', ifNull(JournalDt_Amount2,0), 0)), ".$DecimalBase2.") As JournalDtAmount2_D, ".
						  " Round(Sum(if(Journaldt_dbcd='D', ifNull(JournalDt_Amount ,0), 0)) - Sum(if(Journaldt_dbcd='C', ifNull(JournalDt_Amount ,0), 0)), 9) As JournalDtAmount, ".
						  " Round(Sum(if(Journaldt_dbcd='D', ifNull(JournalDt_Amount1,0), 0)) - Sum(if(Journaldt_dbcd='C', ifNull(JournalDt_Amount1,0), 0)), 9) As JournalDtAmount1, ".
						  " Round(Sum(if(Journaldt_dbcd='D', ifNull(JournalDt_Amount2,0), 0)) - Sum(if(Journaldt_dbcd='C', ifNull(JournalDt_Amount2,0), 0)), 9) As JournalDtAmount2 ".
						  " From Temp_JournalDt As JournalDt ".
						  " Where JournalDt.Journal_ID = '".$PJourId."' ".
						  " Group By JournalDt.Journal_ID ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			if ($sub_result2[2] < 0) {
		
				$txtJourDtdbcd[$i]='D';
				
				$txtJourDtAmount[$i]   = mabsolute($sub_result2[5]);
				$txtJourDtAmount[$i]   = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtJourDate), $txtJourDtAmount[$i], $connection);
				
				$txtJourDtAmount1[$i] = mabsolute($sub_result2[5]);
				$txtJourDtAmount2[$i] = mabsolute($sub_result2[6]);
				
				$txtJourDtAmount_D[$i]   = mabsolute($sub_result2[2]);	
				$txtJourDtAmount_D[$i]   = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtJourDate), $txtJourDtAmount_D[$i], $connection);
				//$txtJourDtAmount_D[$i]   = number_format($txtJourDtAmount_D[$i], $DecimalBase);
				
				$txtJourDtAmount1_D[$i] = mabsolute($sub_result2[2]);
				$txtJourDtAmount2_D[$i] = mabsolute($sub_result2[3]);
			}
			else if ($sub_result2[2] > 0) {
			
				$txtJourDtdbcd[$i]='C';
				
				$txtJourDtAmount[$i]   = mabsolute($sub_result2[5]);
				$txtJourDtAmount[$i]   = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtJourDate), $txtJourDtAmount[$i], $connection);
				
				$txtJourDtAmount1[$i] = mabsolute($sub_result2[5]);
				$txtJourDtAmount2[$i] = mabsolute($sub_result2[6]);
						
				$txtJourDtAmount_D[$i]   = mabsolute($sub_result2[2]);	
				$txtJourDtAmount_D[$i]   = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtJourDate), $txtJourDtAmount_D[$i], $connection);
				//$txtJourDtAmount_D[$i]   = number_format($txtJourDtAmount_D[$i], $DecimalBase);
				
				$txtJourDtAmount1_D[$i] = mabsolute($sub_result2[2]);
				$txtJourDtAmount2_D[$i] = mabsolute($sub_result2[3]);

			}
			
			$sub_query3 = " Select JournalDt.Journaldt_desc From Temp_JournalDt as JournalDt ".
						  " Where JournalDt.Journal_ID = '".$PJourId."' ".
						  " Order By JournalDt.JournalDt_ID desc Limit 1 ";
			$sub_result3 = selectData($connection, $sub_query3);
			if ($sub_result2[2] != 0) $txtJourDtDesc[$i] = $sub_result3[0];
				
			echo"<tr id='CurRec$i' onclick=\"LoadDetails('".$filepath."','".$form."','LIST','datagrid','".$PJourId."','-1',$i);\" class='$bgcolor table-focus-g1' >";
			echo"	<td width='25px;'>$recno</td>";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
			echo "	<td width='110px;' align='left'>
						<input type='hidden' name='txtJourDtId[$i]' id='jourdtid[$i]' value='$txtJourDtId[$i]' >
						<input type='hidden' name='txtJourDtAmount[$i]'  id='journaldtamount_h[$i]'  value='$txtJourDtAmount[$i]' >
						<input type='hidden' name='txtJourDtAmount1[$i]' id='journaldtamount1_h[$i]' value='$txtJourDtAmount1[$i]' >
						<input type='hidden' name='txtJourDtAmount2[$i]' id='journaldtamount2_h[$i]' value='$txtJourDtAmount2[$i]' >
						<input type='text' name='txtLedgerNo[$i]' id='ledgernumber[$i]' value='$txtLedgerNo[$i]'  maxlength='10' class='".$ClassI."' style='font-size:12px; width:80px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','ledger','ledger_number',this.value,'lstLedgerCode[$i]'); GetNameFromCode('getname','ledger','ledger_number','Ledger_name','currency_code',this.value,'ledgername[$i]','currencycode[$i]'); SetRecordGridDefaults($i);\" onkeyup=\"AutoCompleteData(event,this.id,'ledgernumber[$i]','ledgername[$i]','ledgers');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedger(17,1,'all',0,$i);\" style='vertical-align:bottom;'/></span></label>
					</td>";
										
			echo"  	<td width='150px;' align='center'><input type='text' name='txtLedgerName[$i]' id='ledgername[$i]' value='$txtLedgerName[$i]' class='".$ClassI."' style='font-size:12px; width:145px; height:20px;' onchange=\"CheckLookup('chkcodeexists','ledger','ledger_name',this.value,'lstLedgerName[$i]');\" ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerName[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedgerName(17,1,'all',0,$i);\" style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			echo"  	<td width='220px;' align='center'><input type='text' name='txtJourDtDesc[$i]' id='journaldtdesc[$i]' value='".htmlspecialchars($txtJourDtDesc[$i], ENT_QUOTES)."'  maxlength='100' class='".$ClassI."' style='font-size:12px; width:215px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown."   /></td>";
			
			echo"   <td width='60px;' align='center'><select name='txtCurrencyCode[$i]' id='currencycode[$i]' class='".$ClassI."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(4,$DecimalBase1,$DecimalBase2,this.value,'".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." >";
			echo"	<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
				echo"<option ";
				if ($txtCurrencyCode[$i]==$curf[0])	echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"	</select></td>";	
				  
			if ($txtJourDtdbcd[$i] == 'D') { $selecteddb = 'selected'; $selectedcd = ''; }
			else if ($txtJourDtdbcd[$i] == 'C') { $selecteddb = ''; $selectedcd = 'selected'; }
			echo"   <td width='40px;' align='center'>
					<select name='txtJourDtdbcd[$i]' id='journaldtdbcd[$i]' class='".$ClassI."' style='font-size:12px; width:35px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." >
						<option value='D' $selecteddb>D</option>
						<option value='C' $selectedcd>C</option>
					</select>
					</td>";
			
			echo"	<td width='90px;' align='center'><input type='text' name='txtJourDtAmount_D[$i]' id='journaldtamount[$i]' value='$txtJourDtAmount_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:85px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(1,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
			echo"	<td width='90px;' align='center'><input type='text' name='txtJourDtAmount1_D[$i]' id='journaldtamount1[$i]' value='$txtJourDtAmount1_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:85px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(2,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
			echo"	<td width='90px;' align='center'><input type='text' name='txtJourDtAmount2_D[$i]' id='journaldtamount2[$i]' value='$txtJourDtAmount2_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:85px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(3,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
			echo"   <td width='85px;'><input type='text' name='txtValueDate[$i]' id='journaldtvaluedate[$i]' value='$txtValueDate[$i]' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			echo"   <td width='100px;'><input type='text' name='txtJourDtManualNo[$i]' id='journaldtmanualno[$i]' value='".htmlspecialchars($txtJourDtManualNo[$i], ENT_QUOTES)."' maxlength='40' class='".$ClassI."' style='font-size:12px; width:95px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." /></td>";
			echo"  	<td width='200px;' align='center'><input type='text' name='txtJourDtDesc1[$i]' id='journaldtdesc1[$i]' value='".htmlspecialchars($txtJourDtDesc1[$i], ENT_QUOTES)."' maxlength='100' class='".$ClassI."' style='font-size:12px; width:190px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown."  /></td>";
				
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PJourId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$PJourId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,'$NextCell',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"</tr>";  

			$i+=1;	
		}
	
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PJourId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	echo"<input type='hidden' name='RecIndex_G1' id='RecIndex_G1' value='$RecIndex_G1'>"; 
	echo"</div>";
	
	
}

function ShowJournalDtGridDivisions($connection,$form,$PJourId,$PJourDtId,$PDivType,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	/// grid Motion
					$Motion = 1;
					$NextCell = 'chkjournumber[0]';	
						$TabIndex = 1;
					$down = 1;
					
		
			//////////
			
			
	
	
	$columnsarray = array();
	if ($Module_Project == 1 || $Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) array_push($columnsarray, 'costcentcodedt');
	array_push($columnsarray, 'cstjourperc');
	array_push($columnsarray, 'cstjouramount');
	array_push($columnsarray, 'cstjouramount1');
	array_push($columnsarray, 'cstjouramount2');
	
	$columnsarrayall = array("projectcodedt", "costcentcodedt", "cstjourperc", "cstjouramount", "cstjouramount1", "cstjouramount2", 
	                         "cstjouramount_h", "cstjouramount1_h", "cstjouramount2_h", "cstjourid", "cstjourbalperc", "cstjourbalamt", 
							 "cstjourbalamt1", "cstjourbalamt2", "cstjourtotperc", "cstjourtotamt", "cstjourtotamt1", "cstjourtotamt2");
							 
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From JournalDt For Selected Record
	$sub_query = " Select JournalDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " JournalDt.Currency_Code, Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As JournalDt_ValueDate, Journaldt_dbcd, ".
				 " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0) ".
				 " From Temp_JournalDt As JournalDt ".
				 " Inner Join Ledger on JournalDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber    = $sub_result[0];
	$LedgerName      = $sub_result[1];
	$LedgerActivity  = $sub_result[2];
	$JourDtCurrCode  = $sub_result[3];
	$JourDtValueDate = $sub_result[4];
	$JourDtdbcd      = $sub_result[5];
	$JourDtAmount    = $sub_result[6];
	$JourDtAmount1   = $sub_result[7];
	$JourDtAmount2   = $sub_result[8];
		
	//Initializations for Div Ids
	$ContainerDiv = 'container';
	$LoaderDiv    = 'Loader_G2';
	$HeaderDiv    = 'grid_headerDiv1';
	$ContentDiv   = 'grid_contentDiv1';
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv ' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:763px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:705px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='20px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_Project == 1 || $Module_ProjectDt == 1) {
			echo"  <th width='170px;' align='center'><span>Project</span></th>";	
		}
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
			echo"  <th width='170px;' align='center'><span>Costcenter</span></th>";	
		}
		
		echo"  <th width='50px;' align='center'><span>Perc</span><span class='style2'>*</span></th>";
		echo"  <th width='95px;' align='center'><span>Amount</span><span class='style2'>*</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:780px;height:120px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:705px;'>";
		echo"<input type='hidden' name='SelectedRec_G2' id='SelectedRec_G2' value='$SelectedRec_G2'>
			 <input type='hidden' name='SelectedRecColor_G2' id='SelectedRecColor_G2' value='$SelectedRecColor_G2'>
			 <input type='hidden' name='ModRec_G2' id='ModRec_G2' value='$ModRec_G2'>";  
			 
			 
		if ($PJourId != '' && $PJourDtId != '') {

			$sub_query2 = " Select JournalDt.JournalDt_ID, (100 - Round(Sum(ifNull(CstJour_Prcnt,0)),2)) As CstJourPer, ".
						  " (ifNull(JournalDt_Amount,0)  - Round(Sum(ifNull(CstJour_Amount,0)),9) ) As CstJourAmount, ".
						  " (ifNull(JournalDt_Amount1,0) - Round(Sum(ifNull(CstJour_Amount1,0)),9) ) As CstJourAmount1, ".
						  " (ifNull(JournalDt_Amount2,0) - Round(Sum(ifNull(CstJour_Amount2,0)),9) ) As CstJourAmount2, ".
						  " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0) ".
						  " From Temp_JournalDt As JournalDt ".
						  " Inner Join Temp_Cstjour As Cstjour on JournalDt.JournalDt_ID = Cstjour.JournalDt_ID ".
						  " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ".
						  " Group By JournalDt.JournalDt_ID ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtCstJourBalPerc_M  = $sub_result2[1];
			$txtCstJourBalAmt_M   = $sub_result2[2];
			$txtCstJourBalAmt1_M  = $sub_result2[3];
			$txtCstJourBalAmt2_M  = $sub_result2[4];
				
			$txtCstJourTotPerc  = 100;
			$txtCstJourTotAmt   = $sub_result2[5];
			$txtCstJourTotAmt1  = $sub_result2[6];
			$txtCstJourTotAmt2  = $sub_result2[7];
			
				
			$query = " SELECT Cst_increment, JournalDt_ID, Project_Code, Costcent_Code, ".
					 " Round(ifNull(CstJour_Amount,0), ".$DecimalBase."), Round(ifNull(CstJour_Amount1,0), ".$DecimalBase1."), ".
					 " Round(ifNull(CstJour_Amount2,0), ".$DecimalBase2."), Round(ifNull(CstJour_Prcnt,0),2), ".
					 " ifNull(CstJour_Amount,0), ifNull(CstJour_Amount1,0), ifNull(CstJour_Amount2,0) ".
					 " FROM Temp_Cstjour As Cstjour ".
					 " Where Cstjour.JournalDt_ID = '".$PJourDtId."' ";
				
			//echo $query;
			
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtCstJourBalPerc  = $txtCstJourBalPerc_M + $rowsdt[7];
				$txtCstJourBalAmt   = $txtCstJourBalAmt_M  + $rowsdt[8];
				$txtCstJourBalAmt1  = $txtCstJourBalAmt1_M + $rowsdt[9];
				$txtCstJourBalAmt2  = $txtCstJourBalAmt2_M + $rowsdt[10];
			
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_D['.$i.']\')"';
				
					
				echo"<tr id='CurRec_G2$i' class='$bgcolor table-focus-g2'>";		
				echo"	<td width='20px;' align='center'>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteDivisionDt('".$filepath."','".$form."','DEL','datagrid-d','".$PJourId."','".$PJourDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";

				echo"		<input type='hidden' name='txtCstJourId[$i]' id='cstjourid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtCstJourAmount[$i]'  id='cstjouramount_h[$i]'  value='".$rowsdt[8]."' >
							<input type='hidden' name='txtCstJourAmount1[$i]' id='cstjouramount1_h[$i]' value='".$rowsdt[9]."' >
							<input type='hidden' name='txtCstJourAmount2[$i]' id='cstjouramount2_h[$i]' value='".$rowsdt[10]."' >
							<input type='hidden' name='txtCstJourBalPerc[$i]'  id='cstjourbalperc[$i]'  value='$txtCstJourBalPerc' >
							<input type='hidden' name='txtCstJourBalAmt[$i]' id='cstjourbalamt[$i]'  value='$txtCstJourBalAmt' >
							<input type='hidden' name='txtCstJourBalAmt1[$i]' id='cstjourbalamt1[$i]'  value='$txtCstJourBalAmt1' >
							<input type='hidden' name='txtCstJourBalAmt2[$i]' id='cstjourbalamt2[$i]'  value='$txtCstJourBalAmt2' >
							<input type='hidden' name='txtCstJourTotPerc[$i]'  id='cstjourtotperc[$i]'  value='$txtCstJourTotPerc' >
							<input type='hidden' name='txtCstJourTotAmt[$i]' id='cstjourtotamt[$i]'  value='$txtCstJourTotAmt' >
							<input type='hidden' name='txtCstJourTotAmt1[$i]' id='cstjourtotamt1[$i]'  value='$txtCstJourTotAmt1' >
							<input type='hidden' name='txtCstJourTotAmt2[$i]' id='cstjourtotamt2[$i]'  value='$txtCstJourTotAmt2' >";
							
				if ($Module_Project == 1 || $Module_ProjectDt == 1) {
					echo"<td width='170px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[2]."' class='".$ClassE."' style='width:120px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"<input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' />";
				}
				if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
					echo"<td width='170px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='width:130px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"<input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' />";
				}			
							
				echo"   <td width='50px;' align='center'><input type='text' name='txtCstJourPerc[$i]' id='cstjourperc[$i]' value='".$rowsdt[7]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:45px; height:20px;' onBlur=\"CheckPercentage(this.id,'cstjourperc[$i]');\" onChange=\"SetModRecord('ModRec_G2','$i'); SetPercentageDivision(this.id,$DecimalBase1,$DecimalBase2,'',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtCstJourAmount_D[$i]' id='cstjouramount[$i]' value='".$rowsdt[4]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmountDivision(1,$DecimalBase1,$DecimalBase2,'".$JourDtCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."','',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtCstJourAmount1_D[$i]' id='cstjouramount1[$i]' value='".$rowsdt[5]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmountDivision(2,$DecimalBase1,$DecimalBase2,'".$JourDtCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."','',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtCstJourAmount2_D[$i]' id='cstjouramount2[$i]' value='".$rowsdt[6]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmountDivision(3,$DecimalBase1,$DecimalBase2,'".$JourDtCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."','',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_D[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitDivisionDt('".$i."','".$filepath."', '".$form."', 'ADD','datagrid-d','".$PJourId."', '".$PJourDtId."','$NextCell','$Motion','$TabIndex',$down); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_D['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_D[".$i."]','1');LeaveGrid('projectcodedt[$i]','chkjournumber[0]','tab1','".$TabIndex."')\" ";		
		
		$sub_query2 = " Select JournalDt.JournalDt_ID, (100 - Round(Sum(ifNull(CstJour_Prcnt,0)),2)) As CstJourPer,  ".
					  " Round((ifNull(JournalDt_Amount,0)  - Sum(ifNull(CstJour_Amount,0))), ".$DecimalBase.")  As CstJourAmount_D, ".
					  " Round((ifNull(JournalDt_Amount1,0) - Sum(ifNull(CstJour_Amount1,0))), ".$DecimalBase1.") As CstJourAmount1_D, ".
					  " Round((ifNull(JournalDt_Amount2,0) - Sum(ifNull(CstJour_Amount2,0))), ".$DecimalBase2.") As CstJourAmount2_D, ".
					  " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0), ".
					  " (ifNull(JournalDt_Amount,0) -  Round(Sum(ifNull(CstJour_Amount,0)),9) ) As CstJourAmount, ".
					  " (ifNull(JournalDt_Amount1,0) - Round(Sum(ifNull(CstJour_Amount1,0)),9) ) As CstJourAmount1, ".
					  " (ifNull(JournalDt_Amount2,0) - Round(Sum(ifNull(CstJour_Amount2,0)),9) ) As CstJourAmount2 ".
					  " From Temp_JournalDt As JournalDt ".
					  " Inner Join Temp_Cstjour As Cstjour on JournalDt.JournalDt_ID = Cstjour.JournalDt_ID ".
					  " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ".
					  " Group By JournalDt.JournalDt_ID ";

		
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		if ($sub_result2[0] != "") { 
			$txtCstJourBalPerc  = $sub_result2[1];
			$txtCstJourBalAmt   = $sub_result2[8];
			$txtCstJourBalAmt1  = $sub_result2[9];
			$txtCstJourBalAmt2  = $sub_result2[10];
			
			$txtCstJourTotPerc  = 100;
			$txtCstJourTotAmt   = $sub_result2[5];
			$txtCstJourTotAmt1  = $sub_result2[6];
			$txtCstJourTotAmt2  = $sub_result2[7];
			
			$txtCstJourPerc       = $sub_result2[1];
			$txtCstJourAmount_D   = $sub_result2[2];
			$txtCstJourAmount1_D  = $sub_result2[3];
			$txtCstJourAmount2_D  = $sub_result2[4];
			
			$txtCstJourAmount   = $sub_result2[8];
			$txtCstJourAmount1  = $sub_result2[9];
			$txtCstJourAmount2  = $sub_result2[10];	
		} else {
			$txtCstJourBalPerc  = 100;
			$txtCstJourBalAmt   = $JourDtAmount;
			$txtCstJourBalAmt1  = $JourDtAmount1;
			$txtCstJourBalAmt2  = $JourDtAmount2;
			
			$txtCstJourTotPerc  = 100;
			$txtCstJourTotAmt   = $JourDtAmount;
			$txtCstJourTotAmt1  = $JourDtAmount1;
			$txtCstJourTotAmt2  = $JourDtAmount2;
			
			$txtCstJourPerc       = 100;
			$txtCstJourAmount_D   = number_format($JourDtAmount,$DecimalBase,".","");
			$txtCstJourAmount1_D  = number_format($JourDtAmount1,$DecimalBase1,".","");
			$txtCstJourAmount2_D  = number_format($JourDtAmount2,$DecimalBase2,".","");
			
			$txtCstJourAmount   = $JourDtAmount;
			$txtCstJourAmount1  = $JourDtAmount1;
			$txtCstJourAmount2  = $JourDtAmount2;
		}
			

		echo"<tr id='CurRec_G2$i' class='$bgcolor table-focus-g2' >";
		echo"	<td width='20px;' align='center'>$recno</td>";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G2');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "		<input type='hidden' name='txtCstJourId[$i]' id='cstjourid[$i]' value='$txtCstJourId' >
					<input type='hidden' name='txtCstJourAmount[$i]'  id='cstjouramount_h[$i]'  value='$txtCstJourAmount' >
					<input type='hidden' name='txtCstJourAmount1[$i]' id='cstjouramount1_h[$i]' value='$txtCstJourAmount1' >
					<input type='hidden' name='txtCstJourAmount2[$i]' id='cstjouramount2_h[$i]' value='$txtCstJourAmount2' >
					<input type='hidden' name='txtCstJourBalPerc[$i]'  id='cstjourbalperc[$i]'  value='$txtCstJourBalPerc' >
					<input type='hidden' name='txtCstJourBalAmt[$i]' id='cstjourbalamt[$i]'  value='$txtCstJourBalAmt' >
					<input type='hidden' name='txtCstJourBalAmt1[$i]' id='cstjourbalamt1[$i]'  value='$txtCstJourBalAmt1' >
					<input type='hidden' name='txtCstJourBalAmt2[$i]' id='cstjourbalamt2[$i]'  value='$txtCstJourBalAmt2' >
					<input type='hidden' name='txtCstJourTotPerc[$i]'  id='cstjourtotperc[$i]'  value='$txtCstJourTotPerc' >
					<input type='hidden' name='txtCstJourTotAmt[$i]' id='cstjourtotamt[$i]'  value='$txtCstJourTotAmt' >
					<input type='hidden' name='txtCstJourTotAmt1[$i]' id='cstjourtotamt1[$i]'  value='$txtCstJourTotAmt1' >
					<input type='hidden' name='txtCstJourTotAmt2[$i]' id='cstjourtotamt2[$i]'  value='$txtCstJourTotAmt2' >";
				
		if ($Module_Project == 1 || $Module_ProjectDt == 1) {
			echo"<td width='170px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' style='width:120px;height:20px;' maxlength='15' onBlur=\"LeaveGrid('projectcodedt[$i]','chkjournumber[0]','tab1','".$TabIndex."')\" onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown1." /></span>
					<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
		} else {
			echo"<input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' />";
		}
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
			echo"<td width='170px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:130px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
					<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
		} else {
			echo"<input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' />";
		}			
										
		echo"   <td width='50px;' align='center'><input type='text' name='txtCstJourPerc[$i]' id='cstjourperc[$i]' value='$txtCstJourPerc' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:45px; height:20px;' onBlur=\"CheckPercentage(this.id,'cstjourbalperc[$i]');\" onChange=\"SetModRecord('ModRec_G2','$i'); SetPercentageDivision(this.id,$DecimalBase1,$DecimalBase2,'',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtCstJourAmount_D[$i]' id='cstjouramount[$i]' value='$txtCstJourAmount_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmountDivision(1,$DecimalBase1,$DecimalBase2,'".$JourDtCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."','',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtCstJourAmount1_D[$i]' id='cstjouramount1[$i]' value='$txtCstJourAmount1_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmountDivision(2,$DecimalBase1,$DecimalBase2,'".$JourDtCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."','',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtCstJourAmount2_D[$i]' id='cstjouramount2[$i]' value='$txtCstJourAmount2_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmountDivision(3,$DecimalBase1,$DecimalBase2,'".$JourDtCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."','',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_D[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitDivisionDt('".$i."','".$filepath."', '".$form."', 'ADD','datagrid-d','".$PJourId."', '".$PJourDtId."','$NextCell','$Motion','$TabIndex',$down); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
}

function ShowJournalDtGridChecks($connection,$form,$PJourId,$PJourDtId,$arrayparams,$arrayaccess){
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	$Module_Project = $arrayparams['Module_Project'];
		$Module_CostCenter = $arrayparams['Module_CostCenter'];
		$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
		$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
		$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
		$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
		$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
		$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
		$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
		$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
		
		
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
			
		/// grid Motion
		$Motion = 1;
		$NextCell = 'billjournumber[0]';
		$down = 0;
		
	
		if ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1) {
			$TabIndex = 2 ;		
			}
		else 	
		$TabIndex = 1 ;		
	/////////////
	
	
	$columnsarray = array("chkjournumber", "chkjourbank", "chkjourvaluedate", "chkjouramount", "chkjouramount1", "chkjouramount2");
	$columnsarrayall = array("chkjournumber", "chkjourbank", "chkjourvaluedate", "chkjouramount", "chkjouramount1", "chkjouramount2", "chkjouramount_h", "chkjouramount1_h", "chkjouramount2_h", 
							 "chkjourledgerno", "chkjourcurrcode", "chkjourid", "chkjourtype", "chkjourbalamt", "chkjourbalamt1", "chkjourbalamt2", "chkjourtotamt", "chkjourtotamt1", "chkjourtotamt2");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From JournalDt For Selected Record
	$sub_query = " Select JournalDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " JournalDt.Currency_Code, Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As JournalDt_ValueDate, Journaldt_dbcd ".
				 " From Temp_JournalDt As JournalDt ".
				 " Inner Join Ledger on JournalDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber    = $sub_result[0];
	$LedgerName      = $sub_result[1];
	$LedgerActivity  = $sub_result[2];
	$JourDtCurrCode  = $sub_result[3];
	$JourDtValueDate = $sub_result[4];
	$JourDtdbcd      = $sub_result[5];
		
		
	if(($LedgerActivity==1 && $JourDtdbcd=='D') or ($LedgerActivity==2 && $JourDtdbcd=='C')) {
		$showlist = 0;
		$status = '';	
	}	
	else {
		$showlist = 1;
		$status = 'readonly';	
	}	
		

	
	echo"<div id='container2' style='position:relative; z-index:1;' >";
	echo"	<div id='Loader_G3' class=''><div></div></div>";
	echo" 	<div id='grid_headerDiv2' class='grid_headerDiv' style='width:763px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:845px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='20px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='110px;' align='center'><span>Check No</span></th>";
		echo"  <th width='150px;' align='left'><span>Bank</span></th>";
		echo"  <th width='80px;' align='center'><span>Value Date</span></th>";
		echo"  <th width='50px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='110px;' align='center'><span>Amount</span></th>";
		echo"  <th width='110px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='110px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv2' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv2','grid_contentDiv2')\" style='width:780px;height:120px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:845px;'>";
		echo"<input type='hidden' name='SelectedRec_G3' id='SelectedRec_G3' value='$SelectedRec_G3'>
			 <input type='hidden' name='SelectedRecColor_G3' id='SelectedRecColor_G3' value='$SelectedRecColor_G3'>
			 <input type='hidden' name='ModRec_G3' id='ModRec_G3' value='$ModRec_G3'>";  
			 
			 
		if ($PJourId != '' && $PJourDtId != '') {

			$sub_query2 = " Select JournalDt.JournalDt_ID, ".
						  " (ifNull(JournalDt_Amount,0)  - Round(Sum(ifNull(Chk_Amount,0)),9) ) As ChkAmount, ".
						  " (ifNull(JournalDt_Amount1,0) - Round(Sum(ifNull(Chk_Amount1,0)),9) ) As ChkAmount1, ".
						  " (ifNull(JournalDt_Amount2,0) - Round(Sum(ifNull(Chk_Amount2,0)),9) ) As ChkAmount2, ".
						  " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0) ".
						  " From Temp_JournalDt As JournalDt Left Join Temp_ChkJour on JournalDt.JournalDt_ID = Temp_ChkJour.JournalDt_ID ".
						  " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ".
						  " Group By JournalDt.JournalDt_ID, JournalDt_Amount, JournalDt_Amount1, JournalDt_Amount2 ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtChkJourBalAmt_M   = $sub_result2[1];
			$txtChkJourBalAmt1_M  = $sub_result2[2];
			$txtChkJourBalAmt2_M  = $sub_result2[3];
			
			$txtChkJourTotAmt   = $sub_result2[4];
			$txtChkJourTotAmt1  = $sub_result2[5];
			$txtChkJourTotAmt2  = $sub_result2[6];
			
		 
			$query = " SELECT Chk_Increment, JournalDt_Id, Ledger_Number, Chk_Number, Chk_Bank, date_format(Chk_ValueDate,'%d/%m/%Y'), Chk_Type, ChkJour.Currency_Code, ".
					 " Round(ifNull(Chk_Amount,0), ".$DecimalBase."), Round(ifNull(Chk_Amount1,0), ".$DecimalBase1."), Round(ifNull(Chk_Amount2,0), ".$DecimalBase2."), ".
					 " ifNull(Chk_Amount,0), ifNull(Chk_Amount1,0), ifNull(Chk_Amount2,0) ".
					 " FROM Temp_ChkJour As ChkJour ".
					 " WHERE ChkJour.JournalDt_Id = '".$PJourDtId."'";
				
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtChkJourBalAmt   = $txtChkJourBalAmt_M  + $rowsdt[11];
				$txtChkJourBalAmt1  = $txtChkJourBalAmt1_M + $rowsdt[12];
				$txtChkJourBalAmt2  = $txtChkJourBalAmt2_M + $rowsdt[13];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_C['.$i.']\')"';
				
				echo"<tr id='CurRec_G3$i' class='$bgcolor table-focus-g3'>";		
				echo"	<td width='20px;' align='center'>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteChkJour('".$filepath."','".$form."','DEL','datagrid-c','".$PJourId."','".$PJourDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='110px;' align='left'>
							<input type='hidden' name='txtChkJourId[$i]' id='chkjourid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtChkJourType[$i]' id='chkjourtype[$i]' value='".$rowsdt[6]."' >
							<input type='hidden' name='txtChkJourLedgerNo[$i]' id='chkjourledgerno[$i]' value='".$rowsdt[2]."' >
							<input type='hidden' name='txtChkJourAmount[$i]'  id='chkjouramount_h[$i]'  value='".$rowsdt[11]."' >
							<input type='hidden' name='txtChkJourAmount1[$i]' id='chkjouramount1_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtChkJourAmount2[$i]' id='chkjouramount2_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtChkJourBalAmt[$i]' id='chkjourbalamt[$i]'  value='$txtChkJourBalAmt' >
							<input type='hidden' name='txtChkJourBalAmt1[$i]' id='chkjourbalamt1[$i]'  value='$txtChkJourBalAmt1' >
							<input type='hidden' name='txtChkJourBalAmt2[$i]' id='chkjourbalamt2[$i]'  value='$txtChkJourBalAmt2' >
							<input type='hidden' name='txtChkJourTotAmt[$i]' id='chkjourtotamt[$i]'  value='$txtChkJourTotAmt' >
							<input type='hidden' name='txtChkJourTotAmt1[$i]' id='chkjourtotamt1[$i]'  value='$txtChkJourTotAmt1' >
							<input type='hidden' name='txtChkJourTotAmt2[$i]' id='chkjourtotamt2[$i]'  value='$txtChkJourTotAmt2' >
							<input type='text' name='txtChkJourNumber[$i]' id='chkjournumber[$i]' value='".$rowsdt[3]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G3','$i');\" ".$KeyDown." $status />";
							
							if ($showlist == 1) 
								echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistCheck(1,".$i.",'".$PJourDtId."','".$rowsdt[0]."');\" style='vertical-align:bottom;' title='Select Check'  /></span></label>";
				echo"	</td>";
	
										
				echo"  	<td width='150px;' align='center'><input type='text' name='txtChkJourBank[$i]' id='chkjourbank[$i]' value='".$rowsdt[4]."' maxlength='30' class='".$ClassE."' style='font-size:12px; width:145px; height:20px;' onChange=\"SetModRecord('ModRec_G3','$i');\" ".$KeyDown."  /></td>";
				echo"   <td width='80px;' align='center'><input type='text' name='txtChkJourValueDate[$i]' id='chkjourvaluedate[$i]' value='".$rowsdt[5]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G3','$i');\" ".$KeyDown." $status ></td>";
				echo"   <td width='50px;' align='center'><input type='text' name='txtChkJourCurrCode[$i]' id='chkjourcurrcode[$i]' value='".$rowsdt[7]."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
				echo"	<td width='110px;' align='center'><input type='text' name='txtChkJourAmount_D[$i]' id='chkjouramount[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmount(5,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='110px;' align='center'><input type='text' name='txtChkJourAmount1_D[$i]' id='chkjouramount1[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmount(6,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='110px;' align='center'><input type='text' name='txtChkJourAmount2_D[$i]' id='chkjouramount2[$i]' value='".$rowsdt[10]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmount(7,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitChkJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-c', '".$PJourId."', '".$PJourDtId."','$NextCell','$Motion','$TabIndex',$down); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_C['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_C[".$i."]','1');LeaveGrid('chkjournumber[$i]','billjournumber[0]','','')\" ";		
					
		//Initializations
		
		if(($LedgerActivity==1 && $JourDtdbcd=='D') or ($LedgerActivity==2 && $JourDtdbcd=='C'))
			$txtChkJourType = 'A'; 
		else $txtChkJourType = 'C';
		 
		
		if (empty($txtChkJourCurrCode))	 $txtChkJourCurrCode  = $JourDtCurrCode;
		if (empty($txtChkJourValueDate)) $txtChkJourValueDate = $JourDtValueDate;
		if (empty($txtChkJourLedgerNo))  $txtChkJourLedgerNo  = $LedgerNumber;
			
		
		$sub_query2 = " Select JournalDt.JournalDt_ID, ".
					  " Round((ifNull(JournalDt_Amount,0)  - Sum(ifNull(Chk_Amount,0))), ".$DecimalBase.")  As ChkAmount_D, ".
					  " Round((ifNull(JournalDt_Amount1,0) - Sum(ifNull(Chk_Amount1,0))), ".$DecimalBase1.") As ChkAmount1_D, ".
					  " Round((ifNull(JournalDt_Amount2,0) - Sum(ifNull(Chk_Amount2,0))), ".$DecimalBase2.") As ChkAmount2_D, ".
					  " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0), ".
					  " (ifNull(JournalDt_Amount,0) -  Round(Sum(ifNull(Chk_Amount,0)),9) ) As ChkAmount, ".
					  " (ifNull(JournalDt_Amount1,0) - Round(Sum(ifNull(Chk_Amount1,0)),9) ) As ChkAmount1, ".
					  " (ifNull(JournalDt_Amount2,0) - Round(Sum(ifNull(Chk_Amount2,0)),9) ) As ChkAmount2 ".
					  " From Temp_JournalDt As JournalDt Left Join Temp_ChkJour on JournalDt.JournalDt_ID = Temp_ChkJour.JournalDt_ID ".
					  " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ".
					  " Group By JournalDt.JournalDt_ID, JournalDt_Amount, JournalDt_Amount1, JournalDt_Amount2 ";
					  
							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtChkJourBalAmt   = $sub_result2[7];
		$txtChkJourBalAmt1  = $sub_result2[8];
		$txtChkJourBalAmt2  = $sub_result2[9];
			
		$txtChkJourTotAmt   = $sub_result2[4];
		$txtChkJourTotAmt1  = $sub_result2[5];
		$txtChkJourTotAmt2  = $sub_result2[6];
			
		$txtChkJourAmount_D   = $sub_result2[1];
		$txtChkJourAmount1_D  = $sub_result2[2];
		$txtChkJourAmount2_D  = $sub_result2[3];
		
		$txtChkJourAmount   = $sub_result2[7];
		$txtChkJourAmount1  = $sub_result2[8];
		$txtChkJourAmount2  = $sub_result2[9];	
				
		echo"<tr id='CurRec_G3$i' class='$bgcolor table-focus-g3' >";
		echo"	<td width='20px;' align='center'>$recno</td>";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G3');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='110px;' align='left'>
					<input type='hidden' name='txtChkJourId[$i]' id='chkjourid[$i]' value='$txtChkJourId' >
					<input type='hidden' name='txtChkJourType[$i]' id='chkjourtype[$i]' value='$txtChkJourType' >
					<input type='hidden' name='txtChkJourLedgerNo[$i]' id='chkjourledgerno[$i]' value='$txtChkJourLedgerNo[$i]' >
					<input type='hidden' name='txtChkJourAmount[$i]'  id='chkjouramount_h[$i]'  value='$txtChkJourAmount' >
					<input type='hidden' name='txtChkJourAmount1[$i]' id='chkjouramount1_h[$i]' value='$txtChkJourAmount1' >
					<input type='hidden' name='txtChkJourAmount2[$i]' id='chkjouramount2_h[$i]' value='$txtChkJourAmount2' >
					<input type='hidden' name='txtChkJourBalAmt[$i]' id='chkjourbalamt[$i]'  value='$txtChkJourBalAmt' >
					<input type='hidden' name='txtChkJourBalAmt1[$i]' id='chkjourbalamt1[$i]'  value='$txtChkJourBalAmt1' >
					<input type='hidden' name='txtChkJourBalAmt2[$i]' id='chkjourbalamt2[$i]'  value='$txtChkJourBalAmt2' >
					<input type='hidden' name='txtChkJourTotAmt[$i]' id='chkjourtotamt[$i]'  value='$txtChkJourTotAmt' >
					<input type='hidden' name='txtChkJourTotAmt1[$i]' id='chkjourtotamt1[$i]'  value='$txtChkJourTotAmt1' >
					<input type='hidden' name='txtChkJourTotAmt2[$i]' id='chkjourtotamt2[$i]'  value='$txtChkJourTotAmt2' >
					<input type='text' name='txtChkJourNumber[$i]' id='chkjournumber[$i]' value='$txtChkJourNumber'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:80px; height:20px;'  onBlur=\"LeaveGrid('chkjournumber[$i]','billjournumber[0]','tab1','".$TabIndex."')\"  onChange=\"SetModRecord('ModRec_G3','$i');\" ".$KeyDown1." $status />";
					
					if ($showlist == 1) 
						echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistCheck(1,".$i.",'".$PJourDtId."','-1');\" style='vertical-align:bottom;' title='Select Check'  /></span></label>";
		echo"	</td>";
										
		echo"  	<td width='150px;' align='center'><input type='text' name='txtChkJourBank[$i]' id='chkjourbank[$i]' value='$txtChkJourBank' maxlength='30' class='".$ClassI."' style='font-size:12px; width:145px; height:20px;' onChange=\"SetModRecord('ModRec_G3','$i');\" ".$KeyDown."  /></td>";
		echo"   <td width='80px;' align='center'><input type='text' name='txtChkJourValueDate[$i]' id='chkjourvaluedate[$i]' value='$txtChkJourValueDate' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G3','$i');\" ".$KeyDown." $status /></td>";
		echo"   <td width='50px;' align='center'><input type='text' name='txtChkJourCurrCode[$i]' id='chkjourcurrcode[$i]' value='$txtChkJourCurrCode' maxlength='10' class='".$ClassI."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
		echo"	<td width='110px;' align='center'><input type='text' name='txtChkJourAmount_D[$i]' id='chkjouramount[$i]' value='$txtChkJourAmount_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmount(5,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='110px;' align='center'><input type='text' name='txtChkJourAmount1_D[$i]' id='chkjouramount1[$i]' value='$txtChkJourAmount1_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmount(6,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='110px;' align='center'><input type='text' name='txtChkJourAmount2_D[$i]' id='chkjouramount2[$i]' value='$txtChkJourAmount2_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmount(7,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitChkJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-c', '".$PJourId."', '".$PJourDtId."','$NextCell','$Motion','$TabIndex',$down); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}
function ShowJournalDtGridBills($connection,$form,$PJourId,$PJourDtId,$arrayparams,$arrayaccess){
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	$columnsarray = array("billjournumber", "billjourbank", "billjourvaluedate", "billjouramount", "billjouramount1", "billjouramount2");
	$columnsarrayall = array("billjournumber", "billjourbank", "billjourvaluedate", "billjouramount", "billjouramount1", "billjouramount2", "billjouramount_h", "billjouramount1_h", "billjouramount2_h", 
							 "billjourledgerno", "billjourcurrcode", "billjourid", "billjourtype", "billjourbalamt", "billjourbalamt1", "billjourbalamt2", "billjourtotamt", "billjourtotamt1", "billjourtotamt2");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From JournalDt For Selected Record
	$sub_query = " Select JournalDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " JournalDt.Currency_Code, Date_Format(JournalDt_ValueDate,'%d/%m/%Y') As JournalDt_ValueDate, Journaldt_dbcd ".
				 " From Temp_JournalDt As JournalDt ".
				 " Inner Join Ledger on JournalDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber    = $sub_result[0];
	$LedgerName      = $sub_result[1];
	$LedgerActivity  = $sub_result[2];
	$JourDtCurrCode  = $sub_result[3];
	$JourDtValueDate = $sub_result[4];
	$JourDtdbcd      = $sub_result[5];
		
		
	if(($LedgerActivity==3 && $JourDtdbcd=='D') or ($LedgerActivity==4 && $JourDtdbcd=='C')) {
		$showlist = 0;
		$status = '';	
	}	
	else {
		$showlist = 1;
		$status = 'readonly';	
	}	
		

		/// grid Motion
		$Motion = 1;
		$NextCell = 'ledgernumber[0]';
		$down = 0;
	/////////////
	
	
	echo"<div id='container3' style='position:relative; z-index:1;' >";
	echo"	<div id='Loader_G4' class=''><div></div></div>";
	echo" 	<div id='grid_headerDiv3' class='grid_headerDiv' style='width:763px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:845px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='20px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='110px;' align='center'><span>Bill No</span></th>";
		echo"  <th width='150px;' align='left'><span>Bank</span></th>";
		echo"  <th width='80px;' align='center'><span>Value Date</span></th>";
		echo"  <th width='50px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='110px;' align='center'><span>Amount</span></th>";
		echo"  <th width='110px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='110px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv3' class='grid_contentDiv' onscroll=\"ScrollHeader('grid_headerDiv3','grid_contentDiv3')\" style='width:780px;height:120px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:845px;'>";
		echo"<input type='hidden' name='SelectedRec_G4' id='SelectedRec_G4' value='$SelectedRec_G4'>
			 <input type='hidden' name='SelectedRecColor_G4' id='SelectedRecColor_G4' value='$SelectedRecColor_G4'>
			 <input type='hidden' name='ModRec_G4' id='ModRec_G4' value='$ModRec_G4'>";  
			 
			 
		if ($PJourId != '' && $PJourDtId != '') {

			$sub_query2 = " Select JournalDt.JournalDt_ID, ".
						  " (ifNull(JournalDt_Amount,0)  - Round(Sum(ifNull(Bill_Amount,0)),9) ) As BillAmount, ".
						  " (ifNull(JournalDt_Amount1,0) - Round(Sum(ifNull(Bill_Amount1,0)),9) ) As BillAmount1, ".
						  " (ifNull(JournalDt_Amount2,0) - Round(Sum(ifNull(Bill_Amount2,0)),9) ) As BillAmount2, ".
						  " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0) ".
						  " From Temp_JournalDt As JournalDt Left Join Temp_BillJour on JournalDt.JournalDt_ID = Temp_BillJour.JournalDt_ID ".
						  " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ".
						  " Group By JournalDt.JournalDt_ID, JournalDt_Amount, JournalDt_Amount1, JournalDt_Amount2 ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtBillJourBalAmt_M   = $sub_result2[1];
			$txtBillJourBalAmt1_M  = $sub_result2[2];
			$txtBillJourBalAmt2_M  = $sub_result2[3];
			
			$txtBillJourTotAmt   = $sub_result2[4];
			$txtBillJourTotAmt1  = $sub_result2[5];
			$txtBillJourTotAmt2  = $sub_result2[6];
			
		 
			$query = " SELECT Bill_Increment, JournalDt_Id, Ledger_Number, Bill_Number, Bill_Bank, date_format(Bill_ValueDate,'%d/%m/%Y'), Bill_Type, BillJour.Currency_Code, ".
					 " Round(ifNull(Bill_Amount,0), ".$DecimalBase."), Round(ifNull(Bill_Amount1,0), ".$DecimalBase1."), Round(ifNull(Bill_Amount2,0), ".$DecimalBase2."), ".
					 " ifNull(Bill_Amount,0), ifNull(Bill_Amount1,0), ifNull(Bill_Amount2,0) ".
					 " FROM Temp_BillJour As BillJour ".
					 " WHERE BillJour.JournalDt_Id = '".$PJourDtId."'";
				
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtBillJourBalAmt   = $txtBillJourBalAmt_M  + $rowsdt[11];
				$txtBillJourBalAmt1  = $txtBillJourBalAmt1_M + $rowsdt[12];
				$txtBillJourBalAmt2  = $txtBillJourBalAmt2_M + $rowsdt[13];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_B['.$i.']\')"';
				
				echo"<tr id='CurRec_G4$i' class='$bgcolor table-focus-g4'>";		
				echo"	<td width='20px;' align='center'>$recno</td>";
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteBillJour('".$filepath."','".$form."','DEL','datagrid-b','".$PJourId."','".$PJourDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='110px;' align='left'>
							<input type='hidden' name='txtBillJourId[$i]' id='billjourid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtBillJourType[$i]' id='billjourtype[$i]' value='".$rowsdt[6]."' >
							<input type='hidden' name='txtBillJourLedgerNo[$i]' id='billjourledgerno[$i]' value='".$rowsdt[2]."' >
							<input type='hidden' name='txtBillJourAmount[$i]'  id='billjouramount_h[$i]'  value='".$rowsdt[11]."' >
							<input type='hidden' name='txtBillJourAmount1[$i]' id='billjouramount1_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtBillJourAmount2[$i]' id='billjouramount2_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtBillJourBalAmt[$i]' id='billjourbalamt[$i]'  value='$txtBillJourBalAmt' >
							<input type='hidden' name='txtBillJourBalAmt1[$i]' id='billjourbalamt1[$i]'  value='$txtBillJourBalAmt1' >
							<input type='hidden' name='txtBillJourBalAmt2[$i]' id='billjourbalamt2[$i]'  value='$txtBillJourBalAmt2' >
							<input type='hidden' name='txtBillJourTotAmt[$i]' id='billjourtotamt[$i]'  value='$txtBillJourTotAmt' >
							<input type='hidden' name='txtBillJourTotAmt1[$i]' id='billjourtotamt1[$i]'  value='$txtBillJourTotAmt1' >
							<input type='hidden' name='txtBillJourTotAmt2[$i]' id='billjourtotamt2[$i]'  value='$txtBillJourTotAmt2' >
							<input type='text' name='txtBillJourNumber[$i]' id='billjournumber[$i]' value='".$rowsdt[3]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G4','$i');\" ".$KeyDown." $status />";
							
							if ($showlist == 1) 
								echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistBill(1,".$i.",'".$PJourDtId."','".$rowsdt[0]."');\" style='vertical-align:bottom;' title='Select Bill'  /></span></label>";
				echo"	</td>";
	
										
				echo"  	<td width='150px;' align='center'><input type='text' name='txtBillJourBank[$i]' id='billjourbank[$i]' value='".$rowsdt[4]."' maxlength='30' class='".$ClassE."' style='font-size:12px; width:145px; height:20px;' onChange=\"SetModRecord('ModRec_G4','$i');\" ".$KeyDown."  /></td>";
				echo"   <td width='80px;' align='center'><input type='text' name='txtBillJourValueDate[$i]' id='billjourvaluedate[$i]' value='".$rowsdt[5]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G4','$i');\" ".$KeyDown." $status ></td>";
				echo"   <td width='50px;' align='center'><input type='text' name='txtBillJourCurrCode[$i]' id='billjourcurrcode[$i]' value='".$rowsdt[7]."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
				echo"	<td width='110px;' align='center'><input type='text' name='txtBillJourAmount_D[$i]' id='billjouramount[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmount(8,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='110px;' align='center'><input type='text' name='txtBillJourAmount1_D[$i]' id='billjouramount1[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmount(9,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='110px;' align='center'><input type='text' name='txtBillJourAmount2_D[$i]' id='billjouramount2[$i]' value='".$rowsdt[10]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmount(10,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitBillJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-b', '".$PJourId."', '".$PJourDtId."','$NextCell','$Motion','$TabIndex',$down); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_B['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_B[".$i."]','1');LeaveToMainGrid('billjournumber[$i]','ledgernumber','RecIndex_G1','')\" ";						
		//Initializations
		
		if(($LedgerActivity==3 && $JourDtdbcd=='D') or ($LedgerActivity==4 && $JourDtdbcd=='C'))
			$txtBillJourType = 'A'; 
		else $txtBillJourType = 'C';
		 
		 
		if (empty($txtBillJourCurrCode))  $txtBillJourCurrCode  = $JourDtCurrCode;
		if (empty($txtBillJourValueDate)) $txtBillJourValueDate = $JourDtValueDate;
		if (empty($txtBillJourLedgerNo))  $txtBillJourLedgerNo  = $LedgerNumber;
		
		$sub_query2 = " Select JournalDt.JournalDt_ID, ".
					  " Round((ifNull(JournalDt_Amount,0)  - Sum(ifNull(Bill_Amount,0))), ".$DecimalBase.")  As BillAmount_D, ".
					  " Round((ifNull(JournalDt_Amount1,0) - Sum(ifNull(Bill_Amount1,0))), ".$DecimalBase1.") As BillAmount1_D, ".
					  " Round((ifNull(JournalDt_Amount2,0) - Sum(ifNull(Bill_Amount2,0))), ".$DecimalBase2.") As BillAmount2_D, ".
					  " ifNull(JournalDt_Amount,0), ifNull(JournalDt_Amount1,0), ifNull(JournalDt_Amount2,0), ".
					  " (ifNull(JournalDt_Amount,0) -  Round(Sum(ifNull(Bill_Amount,0)),9) ) As BillAmount, ".
					  " (ifNull(JournalDt_Amount1,0) - Round(Sum(ifNull(Bill_Amount1,0)),9) ) As BillAmount1, ".
					  " (ifNull(JournalDt_Amount2,0) - Round(Sum(ifNull(Bill_Amount2,0)),9) ) As BillAmount2 ".
					  " From Temp_JournalDt As JournalDt Left Join Temp_BillJour on JournalDt.JournalDt_ID = Temp_BillJour.JournalDt_ID ".
					  " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ".
					  " Group By JournalDt.JournalDt_ID, JournalDt_Amount, JournalDt_Amount1, JournalDt_Amount2 ";
					  
							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtBillJourBalAmt   = $sub_result2[7];
		$txtBillJourBalAmt1  = $sub_result2[8];
		$txtBillJourBalAmt2  = $sub_result2[9];
			
		$txtBillJourTotAmt   = $sub_result2[4];
		$txtBillJourTotAmt1  = $sub_result2[5];
		$txtBillJourTotAmt2  = $sub_result2[6];
			
		$txtBillJourAmount_D   = $sub_result2[1];
		$txtBillJourAmount1_D  = $sub_result2[2];
		$txtBillJourAmount2_D  = $sub_result2[3];
			
		$txtBillJourAmount   = $sub_result2[7];
		$txtBillJourAmount1  = $sub_result2[8];
		$txtBillJourAmount2  = $sub_result2[9];	
				
		echo"<tr id='CurRec_G4$i' class='$bgcolor table-focus-g4' >";
		echo"	<td width='20px;' align='center'>$recno</td>";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G4');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='110px;' align='left'>
					<input type='hidden' name='txtBillJourId[$i]' id='billjourid[$i]' value='$txtBillJourId' >
					<input type='hidden' name='txtBillJourType[$i]' id='billjourtype[$i]' value='$txtBillJourType' >
					<input type='hidden' name='txtBillJourLedgerNo[$i]' id='billjourledgerno[$i]' value='$txtBillJourLedgerNo' >
					<input type='hidden' name='txtBillJourAmount[$i]'  id='billjouramount_h[$i]'  value='$txtBillJourAmount' >
					<input type='hidden' name='txtBillJourAmount1[$i]' id='billjouramount1_h[$i]' value='$txtBillJourAmount1' >
					<input type='hidden' name='txtBillJourAmount2[$i]' id='billjouramount2_h[$i]' value='$txtBillJourAmount2' >
					<input type='hidden' name='txtBillJourBalAmt[$i]' id='billjourbalamt[$i]'  value='$txtBillJourBalAmt' >
					<input type='hidden' name='txtBillJourBalAmt1[$i]' id='billjourbalamt1[$i]'  value='$txtBillJourBalAmt1' >
					<input type='hidden' name='txtBillJourBalAmt2[$i]' id='billjourbalamt2[$i]'  value='$txtBillJourBalAmt2' >
					<input type='hidden' name='txtBillJourTotAmt[$i]' id='billjourtotamt[$i]'  value='$txtBillJourTotAmt' >
					<input type='hidden' name='txtBillJourTotAmt1[$i]' id='billjourtotamt1[$i]'  value='$txtBillJourTotAmt1' >
					<input type='hidden' name='txtBillJourTotAmt2[$i]' id='billjourtotamt2[$i]'  value='$txtBillJourTotAmt2' >
					<input type='text' name='txtBillJourNumber[$i]' id='billjournumber[$i]' value='$txtBillJourNumber'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:80px; height:20px;'  onBlur=\"LeaveToMainGrid('billjournumber[$i]','ledgernumber','RecIndex_G1','')\"    onChange=\"SetModRecord('ModRec_G4','$i');\" ".$KeyDown1." $status />";
					
					if ($showlist == 1) 
						echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistBill(1,".$i.",'".$PJourDtId."','-1');\" style='vertical-align:bottom;' title='Select Bill'  /></span></label>";
		echo"	</td>";
										
		echo"  	<td width='150px;' align='center'><input type='text' name='txtBillJourBank[$i]' id='billjourbank[$i]' value='$txtBillJourBank' maxlength='30' class='".$ClassI."' style='font-size:12px; width:145px; height:20px;' onChange=\"SetModRecord('ModRec_G4','$i');\" ".$KeyDown."  /></td>";
		echo"   <td width='80px;' align='center'><input type='text' name='txtBillJourValueDate[$i]' id='billjourvaluedate[$i]' value='$txtBillJourValueDate' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G4','$i');\" ".$KeyDown." $status /></td>";
		echo"   <td width='50px;' align='center'><input type='text' name='txtBillJourCurrCode[$i]' id='billjourcurrcode[$i]' value='$txtBillJourCurrCode' maxlength='10' class='".$ClassI."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
		echo"	<td width='110px;' align='center'><input type='text' name='txtBillJourAmount_D[$i]' id='billjouramount[$i]' value='$txtBillJourAmount_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmount(8,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrId."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='110px;' align='center'><input type='text' name='txtBillJourAmount1_D[$i]' id='billjouramount1[$i]' value='$txtBillJourAmount1_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmount(9,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrId."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='110px;' align='center'><input type='text' name='txtBillJourAmount2_D[$i]' id='billjouramount2[$i]' value='$txtBillJourAmount2_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmount(10,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrId."','".$Base1."','".$Base2."','".$txtJourDate."',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitBillJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-b', '".$PJourId."', '".$PJourDtId."','$NextCell','$Motion','$TabIndex',$down); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}

function ShowJournalTotals($connection,$PJourId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	

	$sub_query = " Select ifNull(Amnt1_db,0), ifNull(Amnt2_db,0), ifNull(Amnt1_cd,0), ifNull(Amnt2_cd,0)  ".
				 " From Temp_Journal As Journal ".
				 " Where Journal_ID = ".$PJourId;
			 
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);
	
	$txtJourAmountDb1   = $sub_result[0];
	$txtJourAmountDb2   = $sub_result[1];
	$txtJourAmountCd1   = $sub_result[2];
	$txtJourAmountCd2   = $sub_result[3];
	$txtJourBalance1  = $txtJourAmountDb1 - $txtJourAmountCd1;
	$txtJourBalance2  = $txtJourAmountDb2 - $txtJourAmountCd2;
	
	echo"	  <table valign='top' width='240' border='0' cellspacing=0 cellpadding=0>
				<tr>
					<td id='total-top' style='width:240px;height:12px;'></td>
				</tr>
				<tr>
					<td id='total-content' style='width:240px;height:12px;'>
					<table border='0' cellspacing=0 width=100%>
						<tr>
							<td valign='middle' align='center' width='15%'><span class='style10'>$Base1</span></td> 
							<td valign='middle' align='right' width='25%'><span class='style10'>Debit</span>&nbsp;</td>
							<td align='left'><input type='text' id='journalamnt1db'  name='txtJourAmountDb1' value='".number_format($txtJourAmountDb1,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style10'>Credit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='journalamnt1cd' name='txtJourAmountCd1' value='".number_format($txtJourAmountCd1,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style10'>Balance</span>&nbsp;</td> 
							<td align='left'><input type='text' id='journalbalance1' name='txtJourBalance1' value='".number_format($txtJourBalance1,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr>
							<td valign='middle' align='center' width='15%'><span class='style11'>$Base2</span></td> 
							<td valign='middle' align='right' width='25%'><span class='style11'>Debit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='journalamnt2db' name='txtJourAmountDb2' value='".number_format($txtJourAmountDb2,$DecimalBase2)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>	
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style11'>Credit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='journalamnt2cd' name='txtJourAmountCd2' value='".number_format($txtJourAmountCd2,$DecimalBase2)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style11'>Balance</span>&nbsp;</td> 
							<td align='left'><input type='text' id='journalbalance2' name='txtJourBalance2' value='".number_format($txtJourBalance2,$DecimalBase2)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						</table>
					</td>
				</tr>";
			
	echo "		<tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr>
			  </table>";
}

function ShowTransDtGrid($connection,$form,$PTransId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
	$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
	$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
	$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
	
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];

	
	
	
	$txtDate    = $arrayparams['txtDate'];
	$TransType  = $arrayparams['TransType'];
	$Type_dbcd  = $arrayparams['Type_dbcd1'];
	$TypeIndex  = $arrayparams['TypeIndex'];
	$ErrorRecId = $arrayparams['ErrorRecId'];
	$GridID     = $arrayparams['GridID1'];
	
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	
	$i = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 1000;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
		
	$columnsarray = array("ledgernumber", "ledgername", "transdtdesc", "currencycode", "transdtamount", "transdtamount1", "transdtamount2", "transdtvaluedate", "transdtmanualno");
	if ($TransType == 'RV') array_push($columnsarray, "clientdtcode");
	array_push($columnsarray, "transdtdesc1");
	
	$columnsarrayall = array("ledgernumber", "ledgername", "transdtdesc", "transdtdesc1", "transdtamount", "transdtamount1", "transdtamount2", "transdtvaluedate", "transdtmanualno", "transdtid");
	$orderbyarray = array("Ledger.Ledger_Number", "Ledger.Ledger_Name", "TransDt.TransDt_Description", "TransDt.TransDt_Description2", "TransDt.TransDt_ValueDate");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
		
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

	
	
	$query = " Select TransDt.TransDt_id, TransDt.Trans_id, Ledger.Ledger_Number, Ledger.Ledger_Name, TransDt.TransDt_Description, TransDt.TransDt_Description2, ".
			 " TransDt_dbcd, Round(ifNull(Transdt_Amount,0), ".$DecimalBase."), Round(ifNull(Transdt_Amount1,0), ".$DecimalBase1."),".
			 " Round(ifNull(Transdt_Amount2,0), ".$DecimalBase2."), date_format(TransDt_ValueDate,'%d/%m/%Y'), TransDt.currency_code, TransDt.TransDt_ManualNo, ".
			 " ifNull(Transdt_Amount,0), ifNull(Transdt_Amount1,0), ifNull(Transdt_Amount2,0), ClientsDetail.Detail_Code, ClientsDetail.Detail_Name ".
			 " From Temp_TransDt As TransDt ".
			 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
			 " Left Join ClientsDetail on TransDt.Detail_Code = ClientsDetail.Detail_Code ".
			 " Where TransDt.Trans_ID = ".$PTransId;
			 
	if ($Type_dbcd != -1) $query .= " And TransDt.TransDt_dbcd = '".$Type_dbcd."' ";
	
	$query .= " Order By $orderby $direction ";
	
	//echo $query;
			
	$result=mysql_query($query,$connection);
	$totalrows = mysql_num_rows($result);

	$query = $query." limit ".$startrange.",".$perpage;
	$result=mysql_query($query,$connection);
	
	/// grid Motion
		$Motion = 1;
		if ($Module_TransStandardView==1 && ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1)){
			$NextCell = 'projectcodedt[0]';
			$TabIndex = 0 ;
			
			}
		else {
				$NextCell = 'chkjournumber[0]';	
				$TabIndex = 0 ;
				
			}	
		
	
	
	/////////////
	
	
	
	//Initializations for Div Ids
	$ContainerDiv = 'container1';
	$LoaderDiv    = 'Loader1';
	$HeaderDiv    = 'grid_headerDiv1';
	$ContentDiv   = 'grid_contentDiv1';
	$TableFocus   = 'table-focus-g1';
	
	$TableWidth = '1200px;';
	if ($TransType == 'RV') $TableWidth = '1520px;';
	
		
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:783px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='0' style='width:$TableWidth'>";
	if ($Module_ShowBalance == 1) $wdth='125px'; else $wdth='100px';
		echo"<tr height='20px;'  >";
		echo"  <th width='25px;' align='center' ><span>&nbsp;</span></th>";
		echo"  <th width='$wdth;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','Ledger.Ledger_Number','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ><span>Ledger No". $orderimg1."</span><span class='style2'>*</span></th>";
		echo"  <th width='130px;' align='left' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','Ledger.Ledger_Name','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ><span >Ledger Name". $orderimg2."</span></th>";
		echo"  <th width='200px;' align='center' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','TransDt.TransDt_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ><span >Desc". $orderimg3."</span></th>";
		echo"  <th width='60px;' align='center' ><span >Curr.</span></th> ";	
		echo"  <th width='85px;' align='center' ><span >Amount</span></th>";
		echo"  <th width='85px;' align='center' ><span >".$Base1."</span></th> ";
		echo"  <th width='85px;' align='center' ><span >".$Base2."</span></th>";
		echo"  <th width='70px;' align='center'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','TransDt.TransDt_ValueDate','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ><span >ValueDate". $orderimg5."</span></th>";
		echo"  <th width='105px;' align='center' ><span >Manual No</span></th> ";
		
		if ($TransType == 'RV'){
			echo"  <th width='115px;' align='center' ><span >Detail Code</span></th> ";
			echo"  <th width='205px;' align='center' ><span >Name</span></th> ";	
		}
		
		echo"  <th width='205px;' align='center'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','TransDt.TransDt_Description2','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span>Desc1". $orderimg4."</span></th>";
		  
		echo"  <th width='25px;' align='center' ><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center' ><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:800px;height:135px;' >";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:$TableWidth;'>";
		echo"<input type='hidden' name='SelectedRec_G1' id='SelectedRec_G1' value='$SelectedRec_G1'>
			 <input type='hidden' name='SelectedRecColor_G1' id='SelectedRecColor_G1' value='$SelectedRecColor_G1'>
			 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>"; 
		
			 
			 
		if ($PTransId != '' ) {
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				if ($ErrorRecId != "" && $ErrorRecId == $rowsdt[0]) $bgcolor='bgcolor3';
					
				$querycurrency="select currency_code from currency order by currency_code";
				$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
			
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\')"';
				
				$ShowDetail = 0;
				if ($TransType == 'RV'){
					$sub_query = " SELECT ifNull(Clients.Client_Detail,0) ".
								 " FROM Clients WHERE Clients.Ledger_Number = '".$rowsdt[2]."'";
					$sub_result = selectData($connection, $sub_query);
					$ShowDetail = $sub_result[0];
					
					if (empty($ShowDetail)) $ShowDetail = 0;

				}
				
			
				echo"<tr id='CurRec_G1$i' onclick=\"LoadDetails('".$filepath."','".$form."','LIST','".$GridID."','".$PTransId."','".$rowsdt[0]."','".$i."'); \" class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."','".$GridID."','".$PTransId."','".$rowsdt[0]."','".$TransType."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='100px;' align='left'>
							<input type='hidden' name='txtTransDtId[$i]' id='transdtid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtTransDtAmount[$i]'  id='transdtamount_h[$i]'  value='".$rowsdt[13]."' >
							<input type='hidden' name='txtTransDtAmount1[$i]' id='transdtamount1_h[$i]' value='".$rowsdt[14]."' >
							<input type='hidden' name='txtTransDtAmount2[$i]' id='transdtamount2_h[$i]' value='".$rowsdt[15]."' >
							<input type='hidden' name='txtTransDtdbcd[$i]' id='transdtdbcd[$i]' value='".$rowsdt[6]."' >";
							
							if ($Module_ShowBalance == 1) {
								echo"<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetLedgerBalance('getbalance','ledgernumber','currbalance','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv-1$i');\" style='vertical-align:bottom;'/></span></label>";
							}
							
				echo "		<input type='text' name='txtLedgerNo[$i]' id='ledgernumber[$i]' value='".$rowsdt[2]."'  maxlength='10' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','ledger','ledger_number',this.value,'lstLedgerCode[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'ledgernumber[$i]','ledgername[$i]','ledgers');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedger(22,1,'all',0,$i);\" style='vertical-align:bottom;'/></span></label>
							
							<div id='slidingDiv-1$i' class='slidingDiv slidingDivHide' style='height:40px;'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='60px;'>Balance</td><td width='140px;' align='right'><input type='text' name='txtCurrBalance[$i]' id='currbalance[$i]' value='$txtCurrBalance[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:140px; height:20px;' readonly></td></tr>
								</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv-1$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='130px;' align='center'><input type='text' name='txtLedgerName[$i]' id='ledgername[$i]' value='".$rowsdt[3]."' title='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:125px; height:20px;' onchange=\"CheckLookup('chkcodeexists','ledger','ledger_name',this.value,'lstLedgerName[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerName[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedgerName(22,1,'all',0,$i);\" style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
						
				echo"  	<td width='200px;' align='center'><input type='text' name='txtTransDtDesc[$i]' id='transdtdesc[$i]' value='".$rowsdt[4]."' title='".$rowsdt[4]."' maxlength='150' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\"  ".$KeyDown."   /></td>";
				
				echo"   <td width='60px;' align='center'><select name='txtCurrencyCode[$i]' id='currencycode[$i]' class='".$ClassE."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(4,$DecimalBase1,$DecimalBase2,this.value,'".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." >";
				echo"	<option></option>";
				while($curf=mysql_fetch_row($resultcurrency)){
					echo"<option ";
					if ($rowsdt[11]==$curf[0])	echo " selected ";
					echo" value='$curf[0]'>$curf[0] </option>";
				}
				echo"	</select></td>";	
				
					  
				echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount_D[$i]' id='transdtamount[$i]' value='".$rowsdt[7]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(1,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount1_D[$i]' id='transdtamount1[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(2,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount2_D[$i]' id='transdtamount2[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(3,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"   <td width='70px;'><input type='text' name='txtValueDate[$i]' id='transdtvaluedate[$i]' value='".$rowsdt[10]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:65px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
				echo"   <td width='105px;'><input type='text' name='txtTransDtManualNo[$i]' id='transdtmanualno[$i]' value='".$rowsdt[12]."' maxlength='40' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Manual No' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistManualNo(4,$i); '  style='vertical-align:bottom;'/></span></label>
						</td>";
						
				if ($TransType == 'RV'){
					echo"   <td width='115px;' align='center'><input type='text'   name='txtClientDtCode[$i]' id='clientdtcode[$i]' value='".$rowsdt[16]."' class='".$ClassE."' style='font-size:12px; width:80px; height=18px;' maxlength='50' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." />
							   <label><span><img src='img/openlist.png' title='Select Client Detail' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistClientDetail(1,$i); '  style='vertical-align:bottom;'/></span></label>
							</td>
							<td width='205px;' align='center'><input type='text'   name='txtClientDtName[$i]' id='clientdtname[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='font-size:12px; width:200px; height=18px;' readonly /></td>";
					
				} else {
					echo"<input type='hidden'   name='txtClientDtCode[$i]' id='clientdtcode[$i]' value=''  />
						 <input type='hidden'   name='txtClientDtName[$i]' id='clientdtname[$i]' value='' />";
				}
		
				echo"  	<td width='205px;' align='center'><input type='text' name='txtTransDtDesc1[$i]' id='transdtdesc1[$i]' value='".$rowsdt[5]."' maxlength='150' class='".$ClassE."' style='font-size:12px; width:190px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown."  /></td>";
				
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_1[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$TransType."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail,'$NextCell',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo"	<td width='25px;'>$recno</td>";
				echo "</tr>";
						
	
				
				$i+=1;
				
				
			}
			
		}
		
		
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_1['.$i.']\');"';
			$KeyDown1   = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_1[".$i."]','1');LeaveGrid('ledgernumber[$i]','ledgernumber2[0]','','')\" ";
			//Initializations
			if (empty($DefaultCur))  $DefaultCur = $Base1;

			if (empty($txtCurrencyCode[$i])) $txtCurrencyCode[$i] = $DefaultCur;
			if (empty($txtValueDate[$i]))    $txtValueDate[$i]    = displayToday();
			if (empty($txtTransDtdbcd[$i]))	 $txtTransDtdbcd[$i]  = $Type_dbcd;
			
			$ShowDetail = 0;

			$querycurrency="select currency_code from currency order by currency_code";
			$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
			
			
			$sub_query2 = " Select TransDt.Trans_ID, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount ,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount ,0), 0)), ".$DecimalBase.") As TransDtAmount_D, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount1,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount1,0), 0)), ".$DecimalBase1.") As TransDtAmount1_D, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount2,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount2,0), 0)), ".$DecimalBase2.") As TransDtAmount2_D, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount ,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount ,0), 0)), 9) As TransDtAmount, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount1,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount1,0), 0)), 9) As TransDtAmount1, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount2,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount2,0), 0)), 9) As TransDtAmount2 ".
						  " From Temp_TransDt As TransDt ".
						  " Where TransDt.Trans_ID = '".$PTransId."' ".
						  " Group By TransDt.Trans_ID ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			if ($Type_dbcd == 'D' && $sub_result2[2] < 0) {
				if($txtCurrencyCode[$i] == $Base1) {
					$txtTransDtAmount[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount_D[$i] = mabsolute($sub_result2[2]);	
				}
				else if($txtCurrencyCode[$i] == $Base2) {
					$txtTransDtAmount[$i]   = mabsolute($sub_result2[6]);
					$txtTransDtAmount_D[$i] = mabsolute($sub_result2[3]);	
				}
				else {
					$txtTransDtAmount[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount[$i]   = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtDate), $txtTransDtAmount[$i], $connection);
					$txtTransDtAmount_D[$i] = mabsolute($sub_result2[2]);	
					$txtTransDtAmount_D[$i] = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtDate), $txtTransDtAmount_D[$i], $connection);
				}
				
				$txtTransDtAmount1[$i] = mabsolute($sub_result2[5]);
				$txtTransDtAmount2[$i] = mabsolute($sub_result2[6]);
				
				
				//$txtTransDtAmount_D[$i]   = number_format($txtTransDtAmount_D[$i], $DecimalBase);
				
				$txtTransDtAmount1_D[$i] = mabsolute($sub_result2[2]);
				$txtTransDtAmount2_D[$i] = mabsolute($sub_result2[3]);
			}
			else if ($Type_dbcd == 'C' && $sub_result2[2] > 0) {
				if($txtCurrencyCode[$i] == $Base1) {
					$txtTransDtAmount[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount_D[$i] = mabsolute($sub_result2[2]);	
				}
				else if($txtCurrencyCode[$i] == $Base2) {
					$txtTransDtAmount[$i]   = mabsolute($sub_result2[6]);
					$txtTransDtAmount_D[$i] = mabsolute($sub_result2[3]);	
				}
				else {
					$txtTransDtAmount[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount[$i]   = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtDate), $txtTransDtAmount[$i], $connection);
					$txtTransDtAmount_D[$i] = mabsolute($sub_result2[2]);	
					$txtTransDtAmount_D[$i] = ConvertCurrency($Base1, $txtCurrencyCode[$i], insertMySQLDate($txtDate), $txtTransDtAmount_D[$i], $connection);
				}
				
				$txtTransDtAmount1[$i] = mabsolute($sub_result2[5]);
				$txtTransDtAmount2[$i] = mabsolute($sub_result2[6]);
						
				//$txtTransDtAmount_D[$i]   = number_format($txtTransDtAmount_D[$i], $DecimalBase);
				
				$txtTransDtAmount1_D[$i] = mabsolute($sub_result2[2]);
				$txtTransDtAmount2_D[$i] = mabsolute($sub_result2[3]);

			}
			
			$sub_query3 = " Select TransDt.TransDt_Description From Temp_TransDt as TransDt ".
						  " Where TransDt.Trans_ID = '".$PTransId."' ".
						  " Order By TransDt.TransDt_ID desc Limit 1 ";
			$sub_result3 = selectData($connection, $sub_query3);
			
			if ($txtTransDtAmount[$i] != 0)	$txtTransDtDesc[$i] = $sub_result3[0];
				
			echo"<tr id='CurRec_G1$i' onclick=\"LoadDetails('".$filepath."','".$form."','LIST','".$GridID."','".$PTransId."','-1','".$i."');\" class='$bgcolor $TableFocus' >";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G1');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			 if ($Module_ShowBalance == 1) $wdth='125px'; else $wdth='100px'; 
			echo "	<td width='$wdth;' align='left'>
						<input type='hidden' name='txtTransDtId[$i]' id='transdtid[$i]' value='$txtTransDtId[$i]' >
						<input type='hidden' name='txtTransDtAmount[$i]'  id='transdtamount_h[$i]'  value='$txtTransDtAmount[$i]' >
						<input type='hidden' name='txtTransDtAmount1[$i]' id='transdtamount1_h[$i]' value='$txtTransDtAmount1[$i]' >
						<input type='hidden' name='txtTransDtAmount2[$i]' id='transdtamount2_h[$i]' value='$txtTransDtAmount2[$i]' >
						<input type='hidden' name='txtTransDtdbcd[$i]' id='transdtdbcd[$i]' value='$txtTransDtdbcd[$i]' >";
						
						if ($Module_ShowBalance == 1) {
							echo"<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetLedgerBalance('getbalance','ledgernumber','currbalance','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv-1$i');\" style='vertical-align:bottom;'/></span></label>";
						}
							
			echo "		<input type='text' name='txtLedgerNo[$i]' id='ledgernumber[$i]' value='$txtLedgerNo[$i]'  maxlength='10' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;'  onBlur=\"LeaveGrid('ledgernumber[$i]','ledgernumber2[0]','','')\"  onchange=\"SetModRecord('ModRec_G1','$i'); CheckLookup('chkcodeexists','ledger','ledger_number',this.value,'lstLedgerCode[$i]'); GetNameFromCode('getname','ledger','ledger_number','Ledger_name','currency_code',this.value,'ledgername[$i]','currencycode[$i]'); SetRecordGridDefaults($i); GetLedgerAmount('getledgeramount',1,'".$PTransId."','".$Type_dbcd."','".$i."','".$txtCurrencyCode[$i]."','".$txtDate."'); \" onkeyup=\"AutoCompleteData(event,this.id,'ledgernumber[$i]','ledgername[$i]','ledgers');\" ".$KeyDown1." />
						<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedger(22,1,'all',0,$i);\" style='vertical-align:bottom;'/></span></label>
						
						<div id='slidingDiv-1$i' class='slidingDiv slidingDivHide' style='height:40px;'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='60px;'>Balance</td><td width='140px;' align='right'><input type='text' name='txtCurrBalance[$i]' id='currbalance[$i]' value='$txtCurrBalance[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:140px; height:20px;' readonly></td></tr>
							</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv-1$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
										
			echo"  	<td width='130px;' align='center'><input type='text' name='txtLedgerName[$i]' id='ledgername[$i]' value='$txtLedgerName[$i]' class='".$ClassI."' style='font-size:12px; width:125px; height:20px;' onchange=\"CheckLookup('chkcodeexists','ledger','ledger_name',this.value,'lstLedgerName[$i]');\" ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerName[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedgerName(22,1,'all',0,$i);\" style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
				
			echo"  	<td width='200px;' align='center'><input type='text' name='txtTransDtDesc[$i]' id='transdtdesc[$i]' value='".htmlspecialchars($txtTransDtDesc[$i], ENT_QUOTES)."'  maxlength='150' class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown."   /></td>";
			
			echo"   <td width='60px;' align='center'><select name='txtCurrencyCode[$i]' id='currencycode[$i]' class='".$ClassI."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(4,$DecimalBase1,$DecimalBase2,this.value,'".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." >";
			echo"	<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
				echo"<option ";
				if ($txtCurrencyCode[$i]==$curf[0])	echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"	</select></td>";	
				  
			
			echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount_D[$i]' id='transdtamount[$i]' value='$txtTransDtAmount_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(1,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount1_D[$i]' id='transdtamount1[$i]' value='$txtTransDtAmount1_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(2,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount2_D[$i]' id='transdtamount2[$i]' value='$txtTransDtAmount2_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G1','$i'); SetAmount(3,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			echo"   <td width='70px;'><input type='text' name='txtValueDate[$i]' id='transdtvaluedate[$i]' value='$txtValueDate[$i]' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:65px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." ></td>";
			echo"   <td width='105px;'><input type='text' name='txtTransDtManualNo[$i]' id='transdtmanualno[$i]' value='".htmlspecialchars($txtTransDtManualNo[$i], ENT_QUOTES)."' maxlength='40' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Manual No' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistManualNo(4,$i); '  style='vertical-align:bottom;'/></span></label>
					</td>";
					
			if ($TransType == 'RV'){
				echo"   <td width='115px;' align='center'><input type='text'   name='txtClientDtCode[$i]' id='clientdtcode[$i]' value='$txtClientDtCode[$i]' class='".$ClassI."' style='font-size:12px; width:80px; height=18px;' maxlength='50' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown." />
						   <label><span><img src='img/openlist.png' title='Select Client Detail' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistClientDetail(1,$i); '  style='vertical-align:bottom;'/></span></label>
						</td>
						<td width='205px;' align='center'><input type='text'   name='txtClientDtName[$i]' id='clientdtname[$i]' value='$txtClientDtName[$i]' class='".$ClassI."' style='font-size:12px; width:200px; height=18px;' readonly /></td>";
				
			} else {
				echo"<input type='hidden'   name='txtClientDtCode[$i]' id='clientdtcode[$i]' value=''  />
					 <input type='hidden'   name='txtClientDtName[$i]' id='clientdtname[$i]' value='' />";
			}
			
			echo"  	<td width='205px;' align='center'><input type='text' name='txtTransDtDesc1[$i]' id='transdtdesc1[$i]' value='".htmlspecialchars($txtTransDtDesc1[$i], ENT_QUOTES)."' maxlength='150' class='".$ClassI."' style='font-size:12px; width:190px; height:20px;' onchange=\"SetModRecord('ModRec_G1','$i');\" ".$KeyDown."  /></td>";
				
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_1[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid('".$i."', '".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_1[$i]' onclick=\"SubmitRecordGrid('".$i."','".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$TransType."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail,'$NextCell',$Motion,$TabIndex); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"	<td width='25px;'>$recno</td>";
			echo"</tr>";  

			$i+=1;	
		}
	
	echo"		<input type='hidden' name='RecCount_G1' id='RecCount_G1' value='$i'>"; 
	echo"		<input type='hidden' name='RecIndex_G1' id='RecIndex_G1' value='$RecIndex_G1'>"; 
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PTransId,$GridID,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowTransDtGrid2($connection,$form,$PTransId,$arrayparams,$arrayaccess,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
	$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
	$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
	$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
	
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	
	$txtDate    = $arrayparams['txtDate'];
	$TransType  = $arrayparams['TransType'];
	$Type_dbcd  = $arrayparams['Type_dbcd2'];
	$TypeIndex  = $arrayparams['TypeIndex'];
	$ErrorRecId = $arrayparams['ErrorRecId'];
	$GridID     = $arrayparams['GridID2'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	
	$i = 0;
	
	if ($startrange=='') $startrange = 0;
	if ($perpage=='')	 $perpage = 1000;
	if ($orderby=='')	 $orderby = $dftorderby;
	if ($morelines=='')	 $morelines = 1;
	
	if ($action == 'COR' || $direction == '') {
		if ($direction == 'asc') $direction = 'desc';
		else if ($direction == 'desc') $direction = 'asc';
		else $direction = 'asc';
	}
	
	/// grid Motion
		$Motion = 1;
		if ($Module_TransStandardView==1 && ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1)){
			$NextCell = 'projectcodedt2[0]';
			$TabIndex = 0 ;
			
			}
		else {
				$NextCell = 'chkjournumber2[0]';	
				$TabIndex = 0 ;
				
			}	
		
	
	
	/////////////


	
	$columnsarray = array("ledgernumber2", "ledgername2", "transdtdesc2", "currencycode2", "transdtamount02", "transdtamount12", "transdtamount22", "transdtvaluedate2", "transdtmanualno2");
	if ($TransType == 'RV') array_push($columnsarray, "clientdtcode2");
	array_push($columnsarray, "transdtdesc12");
	
	$columnsarrayall = array("ledgernumber2", "ledgername2", "transdtdesc22", "transdtdesc12", "transdtamount02", "transdtamount12", "transdtamount22", "transdtvaluedate2", "transdtmanualno2", "transdtid2");
	$orderbyarray = array("Ledger.Ledger_Number", "Ledger.Ledger_Name", "TransDt.TransDt_Description", "TransDt.TransDt_Description2", "TransDt.TransDt_ValueDate");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
	
		
	for ($j=1; $j<=count($orderbyarray); $j++){
				
		$index = array_search($orderby, $orderbyarray);
		$index += 1;
		
		$orderimg = 'orderimg'.$index;
		
		if (in_array($orderby, $orderbyarray) && $direction=='asc')
			$$orderimg = "<img src='img/asc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";
		else if (in_array($orderby, $orderbyarray) && $direction=='desc')
			$$orderimg = "<img src='img/desc-2.png' name='w' width='10'  height='10' style='padding-left:5px;'>";	
		else
			$$orderimg = "";
			
	}	

	
	
	$query = " Select TransDt.TransDt_id, TransDt.Trans_id, Ledger.Ledger_Number, Ledger.Ledger_Name, TransDt.TransDt_Description, TransDt.TransDt_Description2, ".
			 " TransDt_dbcd, Round(ifNull(Transdt_Amount,0), ".$DecimalBase."), Round(ifNull(Transdt_Amount1,0), ".$DecimalBase1."),".
			 " Round(ifNull(Transdt_Amount2,0), ".$DecimalBase2."), date_format(TransDt_ValueDate,'%d/%m/%Y'), TransDt.currency_code, TransDt.TransDt_ManualNo, ".
			 " ifNull(Transdt_Amount,0), ifNull(Transdt_Amount1,0), ifNull(Transdt_Amount2,0), ClientsDetail.Detail_Code, ClientsDetail.Detail_Name ".
			 " From Temp_TransDt As TransDt ".
			 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
			 " Left Join ClientsDetail on TransDt.Detail_Code = ClientsDetail.Detail_Code ".
			 " Where TransDt.Trans_ID = ".$PTransId;
			 
	if ($Type_dbcd != -1) $query .= " And TransDt.TransDt_dbcd = '".$Type_dbcd."' ";
	
	$query .= " Order By $orderby $direction ";
	
	//echo $query;
			
	$result=mysql_query($query,$connection);
	$totalrows = mysql_num_rows($result);

	$query = $query." limit ".$startrange.",".$perpage;
	$result=mysql_query($query,$connection);
	
	
	
	//Initializations for Div Ids
	$ContainerDiv = 'container2';
	$LoaderDiv    = 'Loader2';
	$HeaderDiv    = 'grid_headerDiv2';
	$ContentDiv   = 'grid_contentDiv2';
	$TableFocus   = 'table-focus-g2';
	
	$TableWidth = '1200px;';
	if ($TransType == 'RV') $TableWidth = '1520px;';
		
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:783px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:$TableWidth'>";
	if ($Module_ShowBalance == 1) $wdth='125px'; else $wdth='100px';
		echo"<tr height='20px;'>";
		echo"  <th width='25px;' align='center' ><span>&nbsp;</span></th>";
		echo"  <th width='$wdth;' align='center'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','Ledger.Ledger_Number','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span class='style1'>Ledger No". $orderimg1."</span><span class='style2'>*</span></th>";
		echo"  <th width='130px;' align='left'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','Ledger.Ledger_Name','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span class='style1'>Ledger Name". $orderimg2."</span></th>";
		echo"  <th width='200px;' align='center'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','TransDt.TransDt_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span class='style1'>Desc". $orderimg3."</span></th>";
		echo"  <th width='60px;' align='center' ><span >Curr.</span></th> ";	
		echo"  <th width='85px;' align='center' ><span >Amount</span></th>";
		echo"  <th width='85px;' align='center' ><span >".$Base1."</span></th> ";
		echo"  <th width='85px;' align='center' ><span >".$Base2."</span></th>";
		echo"  <th width='70px;' align='center'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','TransDt.TransDt_ValueDate','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span class='style1'>ValueDate". $orderimg5."</span></th>";
		echo"  <th width='105px;' align='center' ><span >Manual No</span></th> ";
		
		if ($TransType == 'RV'){
			echo"  <th width='115px;' align='center' ><span >Detail Code</span></th> ";
			echo"  <th width='205px;' align='center' ><span >Name</span></th> ";	
		}
		
		echo"  <th width='205px;' align='center'  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PTransId."', '".$GridID."', 1, '".$dftorderby."', 'COR','TransDt.TransDt_Description2','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"><span class='style1'>Desc1". $orderimg4."</span></th>";
		  
		echo"  <th width='25px;' align='center' ><span >&nbsp;</span></th>";
		echo"  <th width='25px;' align='center' ><span >&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:800px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:$TableWidth'>";
		echo"<input type='hidden' name='SelectedRec_G2' id='SelectedRec_G2' value='$SelectedRec_G2'>
			 <input type='hidden' name='SelectedRecColor_G2' id='SelectedRecColor_G2' value='$SelectedRecColor_G2'>
			 <input type='hidden' name='ModRec_G2' id='ModRec_G2' value='$ModRec_G2'>"; 
		
			 
			 
		if ($PTransId != '' ) {
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				if ($ErrorRecId != "" && $ErrorRecId == $rowsdt[0]) $bgcolor='bgcolor3';
					
				$querycurrency="select currency_code from currency order by currency_code";
				$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
			
				$ShowDetail = 0;
				if ($TransType == 'RV'){
					$sub_query = " SELECT ifNull(Clients.Client_Detail,0) ".
								 " FROM Clients WHERE Clients.Ledger_Number = '".$rowsdt[2]."'";
					$sub_result = selectData($connection, $sub_query);
					$ShowDetail = $sub_result[0];
					
					if (empty($ShowDetail)) $ShowDetail = 0;

				}
			
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2['.$i.']\')"';
				
			
				echo"<tr id='CurRec_G2$i' onclick=\"LoadDetails2('".$filepath."','".$form."','LIST','".$GridID."','".$PTransId."','".$rowsdt[0]."','".$i."'); \" class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteRecordGrid('".$filepath."','".$form."','".$GridID."','".$PTransId."','".$rowsdt[0]."','".$TransType."','".$i."','".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='100px;' align='left'>
							<input type='hidden' name='txtTransDtId[$i]' id='transdtid2[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtTransDtAmount02[$i]'  id='transdtamount02_h[$i]'  value='".$rowsdt[13]."' >
							<input type='hidden' name='txtTransDtAmount12[$i]' id='transdtamount12_h[$i]' value='".$rowsdt[14]."' >
							<input type='hidden' name='txtTransDtAmount22[$i]' id='transdtamount22_h[$i]' value='".$rowsdt[15]."' >
							<input type='hidden' name='txtTransDtdbcd2[$i]' id='transdtdbcd2[$i]' value='".$rowsdt[6]."' >";
							
							if ($Module_ShowBalance == 1) {
								echo"<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetLedgerBalance('getbalance','ledgernumber2','currbalance2','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv-2$i');\" style='vertical-align:bottom;'/></span></label>";
							}
							
				echo"		<input type='text' name='txtLedgerNo2[$i]' id='ledgernumber2[$i]' value='".$rowsdt[2]."'  maxlength='10' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','ledger','ledger_number',this.value,'lstLedgerCode2[$i]');\" onkeyup=\"AutoCompleteData(event,this.id,'ledgernumber2[$i]','ledgername2[$i]','ledgers');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedger(23,1,'all',0,$i);\" style='vertical-align:bottom;'/></span></label>
							
							<div id='slidingDiv-2$i' class='slidingDiv slidingDivHide' style='height:40px;'>
								<table align='center' border='0' cellpadding='0' cellspacing='0'>
									<tr height='25px;'><td width='60px;'>Balance</td><td width='140px;' align='right'><input type='text' name='txtCurrBalance2[$i]' id='currbalance2[$i]' value='$txtCurrBalance2[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:140px; height:20px;' readonly></td></tr>
								</table>
								<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv-2$i');\"><span style='font-weight:normal;'>Close</span></a></div>
							</div>
						</td>";
										
				echo"  	<td width='130px;' align='center'><input type='text' name='txtLedgerName2[$i]' id='ledgername2[$i]' value='".$rowsdt[3]."' title='".$rowsdt[3]."' class='".$ClassE."' style='font-size:12px; width:125px; height:20px;' onchange=\"CheckLookup('chkcodeexists','ledger','ledger_name',this.value,'lstLedgerName2[$i]');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerName2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedgerName(23,1,'all',0,$i);\" style='vertical-align:bottom; display:none;'/></span></label>
						</td>";
		
				
				echo"  	<td width='200px;' align='center'><input type='text' name='txtTransDtDesc2[$i]' id='transdtdesc2[$i]' value='".$rowsdt[4]."' title='".$rowsdt[4]."' maxlength='150' class='".$ClassE."' style='font-size:12px; width:195px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\"  ".$KeyDown."   /></td>";
				
				echo"   <td width='60px;' align='center'><select name='txtCurrencyCode2[$i]' id='currencycode2[$i]' class='".$ClassE."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(14,$DecimalBase1,$DecimalBase2,this.value,'".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." >";
				echo"	<option></option>";
				while($curf=mysql_fetch_row($resultcurrency)){
					echo"<option ";
					if ($rowsdt[11]==$curf[0])	echo " selected ";
					echo" value='$curf[0]'>$curf[0] </option>";
				}
				echo"	</select></td>";	
				
					  
				echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount02_D[$i]' id='transdtamount02[$i]' value='".$rowsdt[7]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(11,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount12_D[$i]' id='transdtamount12[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(12,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount22_D[$i]' id='transdtamount22[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(13,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"   <td width='70px;'><input type='text' name='txtValueDate2[$i]' id='transdtvaluedate2[$i]' value='".$rowsdt[10]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:65px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
				echo"   <td width='105px;'><input type='text' name='txtTransDtManualNo2[$i]' id='transdtmanualno2[$i]' value='".$rowsdt[12]."' maxlength='40' class='".$ClassE."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." />
							<label><span><img src='img/openlist.png' title='Select Manual No' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistManualNo(5,$i); '  style='vertical-align:bottom;'/></span></label>
						</td>";
						
				if ($TransType == 'RV'){
					echo"   <td width='115px;' align='center'><input type='text'   name='txtClientDtCode2[$i]' id='clientdtcode2[$i]' value='".$rowsdt[16]."' class='".$ClassE."' style='font-size:12px; width:80px; height=18px;' maxlength='50' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." />
							   <label><span><img src='img/openlist.png' title='Select Client Detail' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistClientDetail(2,$i); '  style='vertical-align:bottom;'/></span></label>
							</td>
							<td width='205px;' align='center'><input type='text'   name='txtClientDtName2[$i]' id='clientdtname2[$i]' value='".$rowsdt[17]."' class='".$ClassE."' style='font-size:12px; width:200px; height=18px;' readonly /></td>";
					
				} else {
					echo"<input type='hidden'   name='txtClientDtCode2[$i]' id='clientdtcode2[$i]' value=''  />
						 <input type='hidden'   name='txtClientDtName2[$i]' id='clientdtname2[$i]' value='' />";
				}
				echo"  	<td width='205px;' align='center'><input type='text' name='txtTransDtDesc12[$i]' id='transdtdesc12[$i]' value='".$rowsdt[5]."' maxlength='150' class='".$ClassE."' style='font-size:12px; width:190px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown."  /></td>";
				
				//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid2('".$i."','".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_2[$i]' onclick=\"SubmitRecordGrid2('".$i."','".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$TransType."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail,'$NextCell','$Motion','$TabIndex','$down'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
				echo"	<td width='25px;'>$recno</td>";
				echo "</tr>";
						
	
				
				$i+=1;
				
				
			}
			
		}
		
		
		
		for ($k=1;$k <= 1;$k++) {
			$recno = $i+1;
			$mode = 'i';
			
			if ($i%2==0) $bgcolor='bgcolor1';
			else $bgcolor='bgcolor2';	

			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2['.$i.']\')"';
			$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_2[".$i."]','1');LeaveGrid('ledgernumber2[$i]','ledgernumber[0]','','')\" ";		
			//Initializations
			if (empty($DefaultCur))  $DefaultCur = $Base1;

			if (empty($txtCurrencyCode2[$i])) $txtCurrencyCode2[$i] = $DefaultCur;
			if (empty($txtValueDate2[$i]))    $txtValueDate2[$i]    = displayToday();
			if (empty($txtTransDtdbcd2[$i]))  $txtTransDtdbcd2[$i]  = $Type_dbcd;
			
			$ShowDetail = 0;
			
			$querycurrency="select currency_code from currency order by currency_code";
			$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error($connection));
			
			
			$sub_query2 = " Select TransDt.Trans_ID, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount ,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount ,0), 0)), ".$DecimalBase.") As TransDtAmount_D, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount1,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount1,0), 0)), ".$DecimalBase1.") As TransDtAmount1_D, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount2,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount2,0), 0)), ".$DecimalBase2.") As TransDtAmount2_D, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount ,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount ,0), 0)), 9) As TransDtAmount, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount1,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount1,0), 0)), 9) As TransDtAmount1, ".
						  " Round(Sum(if(Transdt_dbcd='D', ifNull(Transdt_Amount2,0), 0)) - Sum(if(Transdt_dbcd='C', ifNull(Transdt_Amount2,0), 0)), 9) As TransDtAmount2 ".
						  " From Temp_TransDt As TransDt ".
						  " Where TransDt.Trans_ID = '".$PTransId."' ".
						  " Group By TransDt.Trans_ID ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			if ($Type_dbcd == 'D' && $sub_result2[2] < 0) {
		
				if($txtCurrencyCode2[$i] == $Base1) {
					$txtTransDtAmount02[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount02_D[$i] = mabsolute($sub_result2[2]);	
				}
				else if($txtCurrencyCode2[$i] == $Base2) {
					$txtTransDtAmount02[$i]   = mabsolute($sub_result2[6]);
					$txtTransDtAmount02_D[$i] = mabsolute($sub_result2[3]);	
				}
				else {
					$txtTransDtAmount02[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount02[$i]   = ConvertCurrency($Base1, $txtCurrencyCode2[$i], insertMySQLDate($txtDate), $txtTransDtAmount02[$i], $connection);
					$txtTransDtAmount02_D[$i] = mabsolute($sub_result2[2]);	
					$txtTransDtAmount02_D[$i] = ConvertCurrency($Base1, $txtCurrencyCode2[$i], insertMySQLDate($txtDate), $txtTransDtAmount02_D[$i], $connection);
				}
				
				$txtTransDtAmount12[$i] = mabsolute($sub_result2[5]);
				$txtTransDtAmount22[$i] = mabsolute($sub_result2[6]);
				
				
				//$txtTransDtAmount02_D[$i]   = number_format($txtTransDtAmount02_D[$i], $DecimalBase);
				
				$txtTransDtAmount12_D[$i] = mabsolute($sub_result2[2]);
				$txtTransDtAmount22_D[$i] = mabsolute($sub_result2[3]);
			}
			else if ($Type_dbcd == 'C' && $sub_result2[2] > 0) {
			
				if($txtCurrencyCode2[$i] == $Base1) {
					$txtTransDtAmount02[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount02_D[$i] = mabsolute($sub_result2[2]);	
				}
				else if($txtCurrencyCode2[$i] == $Base2) {
					$txtTransDtAmount02[$i]   = mabsolute($sub_result2[6]);
					$txtTransDtAmount02_D[$i] = mabsolute($sub_result2[3]);	
				}
				else {
					$txtTransDtAmount02[$i]   = mabsolute($sub_result2[5]);
					$txtTransDtAmount02[$i]   = ConvertCurrency($Base1, $txtCurrencyCode2[$i], insertMySQLDate($txtDate), $txtTransDtAmount02[$i], $connection);
					$txtTransDtAmount02_D[$i] = mabsolute($sub_result2[2]);	
					$txtTransDtAmount02_D[$i] = ConvertCurrency($Base1, $txtCurrencyCode2[$i], insertMySQLDate($txtDate), $txtTransDtAmount02_D[$i], $connection);
				}
				
				$txtTransDtAmount12[$i] = mabsolute($sub_result2[5]);
				$txtTransDtAmount22[$i] = mabsolute($sub_result2[6]);
						
				
				//$txtTransDtAmount02_D[$i]   = number_format($txtTransDtAmount02_D[$i], $DecimalBase);
				
				$txtTransDtAmount12_D[$i] = mabsolute($sub_result2[2]);
				$txtTransDtAmount22_D[$i] = mabsolute($sub_result2[3]);

			}
			
			$sub_query3 = " Select TransDt.TransDt_Description From Temp_TransDt as TransDt ".
						  " Where TransDt.Trans_ID = '".$PTransId."' ".
						  " Order By TransDt.TransDt_ID desc Limit 1 ";
			$sub_result3 = selectData($connection, $sub_query3);
			if ($txtTransDtAmount02[$i] != 0)	$txtTransDtDesc2[$i] = $sub_result3[0];
			
				
			echo"<tr id='CurRec_G2$i' onclick=\"LoadDetails2('".$filepath."','".$form."','LIST','".$GridID."','".$PTransId."','-1','".$i."');\" class='$bgcolor $TableFocus' >";
			echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G2');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			if ($Module_ShowBalance == 1) $wdth='125px'; else $wdth='100px';  
			echo "	<td width='$wdth;' align='left'>
						<input type='hidden' name='txtTransDtId2[$i]' id='transdtid2[$i]' value='$txtTransDtId2[$i]' >
						<input type='hidden' name='txtTransDtAmount02[$i]'  id='transdtamount02_h[$i]'  value='$txtTransDtAmount02[$i]' >
						<input type='hidden' name='txtTransDtAmount12[$i]' id='transdtamount12_h[$i]' value='$txtTransDtAmount12[$i]' >
						<input type='hidden' name='txtTransDtAmount22[$i]' id='transdtamount22_h[$i]' value='$txtTransDtAmount22[$i]' >
						<input type='hidden' name='txtTransDtdbcd2[$i]' id='transdtdbcd2[$i]' value='$txtTransDtdbcd2[$i]' >";
						
						if ($Module_ShowBalance == 1) {
							echo"<label><span><img src='img/scroll-down-1.png' title='Show Details'  width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"GetLedgerBalance('getbalance','ledgernumber2','currbalance2','".$i."','".$txtDate."');ShowSlidingDiv('#slidingDiv-2$i');\" style='vertical-align:bottom;'/></span></label>";
						}
							
			echo"		<input type='text' name='txtLedgerNo2[$i]' id='ledgernumber2[$i]' value='$txtLedgerNo2[$i]'  maxlength='10' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onBlur=\"LeaveGrid('ledgernumber2[$i]','ledgernumber[0]','','')\"   onchange=\"SetModRecord('ModRec_G2','$i'); CheckLookup('chkcodeexists','ledger','ledger_number',this.value,'lstLedgerCode2[$i]'); GetNameFromCode('getname','ledger','ledger_number','Ledger_name','currency_code',this.value,'ledgername2[$i]','currencycode2[$i]'); SetRecordGridDefaults2($i); GetLedgerAmount('getledgeramount',2,'".$PTransId."','".$Type_dbcd."','".$i."','".$txtCurrencyCode2[$i]."','".$txtDate."'); \" onkeyup=\"AutoCompleteData(event,this.id,'ledgernumber2[$i]','ledgername2[$i]','ledgers');\" ".$KeyDown1." />
						<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerCode2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedger(23,1,'all',0,$i);\" style='vertical-align:bottom;'/></span></label>
						
						<div id='slidingDiv-2$i' class='slidingDiv slidingDivHide' style='height:40px;'>
							<table align='center' border='0' cellpadding='0' cellspacing='0'>
								<tr height='25px;'><td width='60px;'>Balance</td><td width='140px;' align='right'><input type='text' name='txtCurrBalance2[$i]' id='currbalance2[$i]' value='$txtCurrBalance2[$i]' style='font-size:12px; font-weight:bold; text-align:right; border-width:0px; background-color:transparent; width:140px; height:20px;' readonly></td></tr>
							</table>
							<div style='text-align:right;display:block;margin-top:5px;'><a href='javascript:void(0);' onclick=\"ShowSlidingDiv('#slidingDiv-2$i');\"><span style='font-weight:normal;'>Close</span></a></div>
						</div>
					</td>";
										
			echo"  	<td width='130px;' align='center'><input type='text' name='txtLedgerName2[$i]' id='ledgername2[$i]' value='$txtLedgerName2[$i]' class='".$ClassI."' style='font-size:12px; width:125px; height:20px;' onchange=\"CheckLookup('chkcodeexists','ledger','ledger_name',this.value,'lstLedgerName2[$i]');\" ".$KeyDown."  />
						<label><span><img src='img/openlist.png' title='Select Ledger' id='lstLedgerName2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick=\"showlistLedgerName(23,1,'all',0,$i);\" style='vertical-align:bottom; display:none;'/></span></label>
					</td>";
			
			
			echo"  	<td width='200px;' align='center'><input type='text' name='txtTransDtDesc2[$i]' id='transdtdesc2[$i]' value='".htmlspecialchars($txtTransDtDesc2[$i], ENT_QUOTES)."'  maxlength='150' class='".$ClassI."' style='font-size:12px; width:195px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown."   /></td>";
			
			echo"   <td width='60px;' align='center'><select name='txtCurrencyCode2[$i]' id='currencycode2[$i]' class='".$ClassI."' style='font-size:12px; width:55px; height:20px;' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(14,$DecimalBase1,$DecimalBase2,this.value,'".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." >";
			echo"	<option></option>";
			while($curf=mysql_fetch_row($resultcurrency)){
				echo"<option ";
				if ($txtCurrencyCode2[$i]==$curf[0])	echo " selected ";
				echo" value='$curf[0]'>$curf[0] </option>";
			}
			echo"	</select></td>";	
				  
			
			echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount02_D[$i]' id='transdtamount02[$i]' value='$txtTransDtAmount02_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(11,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount12_D[$i]' id='transdtamount12[$i]' value='$txtTransDtAmount12_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(12,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			echo"	<td width='85px;' align='center'><input type='text' name='txtTransDtAmount22_D[$i]' id='transdtamount22[$i]' value='$txtTransDtAmount22_D[$i]' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G2','$i'); SetAmount(13,$DecimalBase1,$DecimalBase2,'','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			echo"   <td width='70px;'><input type='text' name='txtValueDate2[$i]' id='transdtvaluedate2[$i]' value='$txtValueDate2[$i]' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:65px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." ></td>";
			echo"   <td width='105px;'><input type='text' name='txtTransDtManualNo2[$i]' id='transdtmanualno2[$i]' value='".htmlspecialchars($txtTransDtManualNo2[$i], ENT_QUOTES)."' maxlength='40' class='".$ClassI."' style='font-size:12px; width:70px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." />
						<label><span><img src='img/openlist.png' title='Select Manual No' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistManualNo(5,$i); '  style='vertical-align:bottom;'/></span></label>
					</td>";
					
			if ($TransType == 'RV'){
				echo"   <td width='115px;' align='center'><input type='text'   name='txtClientDtCode2[$i]' id='clientdtcode2[$i]' value='$txtClientDtCode2[$i]' class='".$ClassI."' style='font-size:12px; width:80px; height=18px;' maxlength='50' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown." />
						   <label><span><img src='img/openlist.png' title='Select Client Detail' width='20' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistClientDetail(2,$i); '  style='vertical-align:bottom;'/></span></label>
						</td>
						<td width='205px;' align='center'><input type='text'   name='txtClientDtName2[$i]' id='clientdtname2[$i]' value='$txtClientDtName2[$i]' class='".$ClassI."' style='font-size:12px; width:200px; height=18px;' readonly /></td>";
					
			} else {
				echo"<input type='hidden'   name='txtClientDtCode2[$i]' id='clientdtcode2[$i]' value=''  />
					 <input type='hidden'   name='txtClientDtName2[$i]' id='clientdtname2[$i]' value='' />";
			}
				
			echo"  	<td width='205px;' align='center'><input type='text' name='txtTransDtDesc12[$i]' id='transdtdesc12[$i]' value='".htmlspecialchars($txtTransDtDesc12[$i], ENT_QUOTES)."' maxlength='150' class='".$ClassI."' style='font-size:12px; width:190px; height:20px;' onchange=\"SetModRecord('ModRec_G2','$i');\" ".$KeyDown."  /></td>";
				
			//echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitRecordGrid2('".$i."', '".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
			echo "	<td width='25px;' align='center' valign='center'><input type='button' id='imgSubmit_2[$i]' onclick=\"SubmitRecordGrid2('".$i."','".$filepath."', '".$form."', '".$GridID."', '".$PTransId."', '".$TransType."', '".$dftorderby."','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$direction."',$Module_RestrictProjCost,$Module_RestrictCost,$Module_Project,$Module_CostCenter,$ModuleRestrictCashType,$Module_MultiCashType,$ShowDetail,'$NextCell','$Motion','$TabIndex','$down'); return false;\" title='Submit Record' style='width:20px; height:20px; vertical-align:bottom; cursor:pointer; outline:none; border:0px; background-image:url(img/confirm-1.png)' ></td>";
			echo"	<td width='25px;'>$recno</td>";
			echo"</tr>";  

			$i+=1;	
		}
	
	echo"		<input type='hidden' name='RecCount_G2' id='RecCount_G2' value='$i'>"; 
	echo"		<input type='hidden' name='RecIndex_G2' id='RecIndex_G2' value='$RecIndex_G2'>"; 
	echo"	</table>";  
	echo"	</div>";
	
	showDataGridFooter($filepath,$form,$PTransId,$GridID,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
	
}

function ShowTransDtGridDivisions($connection,$form,$PTransId,$PTransDtId,$PDivType,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	
	$txtDate = $arrayparams['txtDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
		
	
	$columnsarray = array();
	if ($Module_Project == 1 || $Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt');
	if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) array_push($columnsarray, 'costcentcodedt');
	array_push($columnsarray, 'cstjourperc');
	array_push($columnsarray, 'cstjouramount');
	array_push($columnsarray, 'cstjouramount1');
	array_push($columnsarray, 'cstjouramount2');
	
	$columnsarrayall = array("projectcodedt", "costcentcodedt", "cstjourperc", "cstjouramount", "cstjouramount1", "cstjouramount2", 
	                         "cstjouramount_h", "cstjouramount1_h", "cstjouramount2_h", "cstjourid", "cstjourbalperc", "cstjourbalamt", 
							 "cstjourbalamt1", "cstjourbalamt2", "cstjourtotperc", "cstjourtotamt", "cstjourtotamt1", "cstjourtotamt2");
							 
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd, ".
				 " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
	$TransDtAmount    = $sub_result[6];
	$TransDtAmount1   = $sub_result[7];
	$TransDtAmount2   = $sub_result[8];
		
	//Initializations for Div Ids
	$ContainerDiv = 'container3';
	$LoaderDiv    = 'Loader3';
	$HeaderDiv    = 'grid_headerDiv3';
	$ContentDiv   = 'grid_contentDiv3';
	$TableFocus   = 'table-focus-g3';
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv ' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:580px;'>";
	
		echo"<tr height='20px;'>";
		echo"  <th width='25px;' align='center' ><span>&nbsp;</span></th>";
		
		if ($Module_Project == 1 || $Module_ProjectDt == 1) {
			echo"  <th width='125px;' align='center' ><span class='style1'>Project</span></th>";	
		}
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
			echo"  <th width='125px;' align='center' ><span class='style1'>Costcenter</span></th>";	
		}
		
		echo"  <th width='45px;' align='center' ><span >Perc</span><span class='style2'>*</span></th>";
		echo"  <th width='85px;' align='center' ><span >Amount</span><span class='style2'>*</span></th>";
		echo"  <th width='85px;' align='center' ><span >$Base1</span></th>";
		echo"  <th width='85px;' align='center' ><span >$Base2</span></th>";
		echo"  <th width='25px;' align='center' ><span >&nbsp;</span></th>";
		echo"  <th width='25px;' align='center' ><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:580px;'>";
		echo"<input type='hidden' name='SelectedRec_G3' id='SelectedRec_G3' value='$SelectedRec_G3'>
			 <input type='hidden' name='SelectedRecColor_G3' id='SelectedRecColor_G3' value='$SelectedRecColor_G3'>
			 <input type='hidden' name='ModRec_G3' id='ModRec_G3' value='$ModRec_G3'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, (100 - Round(Sum(ifNull(CstJourDt_Per,0)),2)) As CstJourDtPer, ".
						  " (ifNull(TransDt_Amount,0)  - Round(Sum(ifNull(CstJourDt_Amount,0)),9) ) As CstJourDtAmount, ".
						  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(CstJourDt_Amount1,0)),9) ) As CstJourDtAmount1, ".
						  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(CstJourDt_Amount2,0)),9) ) As CstJourDtAmount2, ".
						  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
						  " From Temp_TransDt As TransDt ".
						  " Inner Join Temp_CstjourDt As CstjourDt on TransDt.TransDt_ID = CstjourDt.TransDt_ID ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtCstJourBalPerc_M  = $sub_result2[1];
			$txtCstJourBalAmt_M   = $sub_result2[2];
			$txtCstJourBalAmt1_M  = $sub_result2[3];
			$txtCstJourBalAmt2_M  = $sub_result2[4];
				
			$txtCstJourTotPerc  = 100;
			$txtCstJourTotAmt   = $sub_result2[5];
			$txtCstJourTotAmt1  = $sub_result2[6];
			$txtCstJourTotAmt2  = $sub_result2[7];
			
				
			$query = " SELECT CstJourDt_id, TransDt_ID, Project_Code, Costcenter_Code, ".
					 " Round(ifNull(CstJourDt_Amount,0), ".$DecimalBase."), Round(ifNull(CstJourDt_Amount1,0), ".$DecimalBase1."), ".
					 " Round(ifNull(CstJourDt_Amount2,0), ".$DecimalBase2."), Round(ifNull(CstJourDt_Per,0),2), ".
					 " ifNull(CstJourDt_Amount,0), ifNull(CstJourDt_Amount1,0), ifNull(CstJourDt_Amount2,0) ".
					 " FROM Temp_CstjourDt As CstjourDt ".
					 " Where CstjourDt.TransDt_ID = '".$PTransDtId."' ";
				
			//echo $query;
			
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      	
			/// grid Motion
					$Motion = 1;
					$NextCell = 'chkjournumber[0]';	
						$TabIndex = 1;
					$down = 1;
					
		
			//////////
			
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtCstJourBalPerc  = $txtCstJourBalPerc_M + $rowsdt[7];
				$txtCstJourBalAmt   = $txtCstJourBalAmt_M  + $rowsdt[8];
				$txtCstJourBalAmt1  = $txtCstJourBalAmt1_M + $rowsdt[9];
				$txtCstJourBalAmt2  = $txtCstJourBalAmt2_M + $rowsdt[10];
			
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_D['.$i.']\')"';
				
					
				echo"<tr id='CurRec_G3$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteDivisionDt('".$filepath."','".$form."','DEL','datagrid-1-d','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";

				echo"		<input type='hidden' name='txtCstJourId[$i]' id='cstjourid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtCstJourAmount[$i]'  id='cstjouramount_h[$i]'  value='".$rowsdt[8]."' >
							<input type='hidden' name='txtCstJourAmount1[$i]' id='cstjouramount1_h[$i]' value='".$rowsdt[9]."' >
							<input type='hidden' name='txtCstJourAmount2[$i]' id='cstjouramount2_h[$i]' value='".$rowsdt[10]."' >
							<input type='hidden' name='txtCstJourBalPerc[$i]'  id='cstjourbalperc[$i]'  value='$txtCstJourBalPerc' >
							<input type='hidden' name='txtCstJourBalAmt[$i]' id='cstjourbalamt[$i]'  value='$txtCstJourBalAmt' >
							<input type='hidden' name='txtCstJourBalAmt1[$i]' id='cstjourbalamt1[$i]'  value='$txtCstJourBalAmt1' >
							<input type='hidden' name='txtCstJourBalAmt2[$i]' id='cstjourbalamt2[$i]'  value='$txtCstJourBalAmt2' >
							<input type='hidden' name='txtCstJourTotPerc[$i]'  id='cstjourtotperc[$i]'  value='$txtCstJourTotPerc' >
							<input type='hidden' name='txtCstJourTotAmt[$i]' id='cstjourtotamt[$i]'  value='$txtCstJourTotAmt' >
							<input type='hidden' name='txtCstJourTotAmt1[$i]' id='cstjourtotamt1[$i]'  value='$txtCstJourTotAmt1' >
							<input type='hidden' name='txtCstJourTotAmt2[$i]' id='cstjourtotamt2[$i]'  value='$txtCstJourTotAmt2' >";
							
				if ($Module_Project == 1 || $Module_ProjectDt == 1) {
					echo"<td width='125px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='".$rowsdt[2]."' class='".$ClassE."' style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G3','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(25,1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"<input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' />";
				}
				if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
					echo"<td width='125px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G3','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(25,1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"<input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' />";
				}			
				//$TotalPer = $TotalPer + $rowsdt[7];
				
				echo"   <td width='45px;' align='center'><input type='text' name='txtCstJourPerc[$i]' id='cstjourperc[$i]' value='".$rowsdt[7]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:40px; height:20px;' onBlur=\"CheckPercentage(this.id,'cstjourperc[$i]');\" onChange=\"SetModRecord('ModRec_G3','$i'); SetPercentageDivision(1,this.id,$DecimalBase1,$DecimalBase2,'',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount_D[$i]' id='cstjouramount[$i]' value='".$rowsdt[4]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmountDivision(1,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount1_D[$i]' id='cstjouramount1[$i]' value='".$rowsdt[5]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmountDivision(2,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount2_D[$i]' id='cstjouramount2[$i]' value='".$rowsdt[6]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmountDivision(3,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_D[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitDivisionDt('".$i."','".$filepath."', '".$form."', 'ADD','datagrid-1-d','".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex',$down, $Module_RestrictProjCost, $Module_RestrictCost); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_D['.$i.']\')"';
			
			
		$sub_query2 = " Select TransDt.TransDt_ID, (100 - Round(Sum(ifNull(CstJourDt_Per,0)),2)) As CstJourDtPer,  ".
					  " Round((ifNull(TransDt_Amount,0)  - Sum(ifNull(CstJourDt_Amount,0))), ".$DecimalBase.")  As CstJourDtAmount_D, ".
					  " Round((ifNull(TransDt_Amount1,0) - Sum(ifNull(CstJourDt_Amount1,0))), ".$DecimalBase1.") As CstJourDtAmount1_D, ".
					  " Round((ifNull(TransDt_Amount2,0) - Sum(ifNull(CstJourDt_Amount2,0))), ".$DecimalBase2.") As CstJourDtAmount2_D, ".
					  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0), ".
					  " (ifNull(TransDt_Amount,0) -  Round(Sum(ifNull(CstJourDt_Amount,0)),9) ) As CstJourDtAmount, ".
					  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(CstJourDt_Amount1,0)),9) ) As CstJourDtAmount1, ".
					  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(CstJourDt_Amount2,0)),9) ) As CstJourDtAmount2 ".
					  " From Temp_TransDt As TransDt ".
					  " Inner Join Temp_CstjourDt As CstjourDt on TransDt.TransDt_ID = CstjourDt.TransDt_ID ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID ";

		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		if ($sub_result2[0] != "") { 
			$txtCstJourBalPerc  = $sub_result2[1];
			$txtCstJourBalAmt   = $sub_result2[8];
			$txtCstJourBalAmt1  = $sub_result2[9];
			$txtCstJourBalAmt2  = $sub_result2[10];
			
			$txtCstJourTotPerc  = 100;
			$txtCstJourTotAmt   = $sub_result2[5];
			$txtCstJourTotAmt1  = $sub_result2[6];
			$txtCstJourTotAmt2  = $sub_result2[7];
			
			$txtCstJourPerc       = $sub_result2[1];
			$txtCstJourAmount_D   = $sub_result2[2];
			$txtCstJourAmount1_D  = $sub_result2[3];
			$txtCstJourAmount2_D  = $sub_result2[4];
			
			$txtCstJourAmount   = $sub_result2[8];
			$txtCstJourAmount1  = $sub_result2[9];
			$txtCstJourAmount2  = $sub_result2[10];	
		} else {
			$txtCstJourBalPerc  = 100;
			$txtCstJourBalAmt   = $TransDtAmount;
			$txtCstJourBalAmt1  = $TransDtAmount1;
			$txtCstJourBalAmt2  = $TransDtAmount2;
			
			$txtCstJourTotPerc  = 100;
			$txtCstJourTotAmt   = $TransDtAmount;
			$txtCstJourTotAmt1  = $TransDtAmount1;
			$txtCstJourTotAmt2  = $TransDtAmount2;
			
			$txtCstJourPerc       = 100;
			$txtCstJourAmount_D   = number_format($TransDtAmount,$DecimalBase,".","");
			$txtCstJourAmount1_D  = number_format($TransDtAmount1,$DecimalBase1,".","");
			$txtCstJourAmount2_D  = number_format($TransDtAmount2,$DecimalBase2,".","");
			
			$txtCstJourAmount   = $TransDtAmount;
			$txtCstJourAmount1  = $TransDtAmount1;
			$txtCstJourAmount2  = $TransDtAmount2;
		}
			

		echo"<tr id='CurRec_G3$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G3');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "		<input type='hidden' name='txtCstJourId[$i]' id='cstjourid[$i]' value='$txtCstJourId' >
					<input type='hidden' name='txtCstJourAmount[$i]'  id='cstjouramount_h[$i]'  value='$txtCstJourAmount' >
					<input type='hidden' name='txtCstJourAmount1[$i]' id='cstjouramount1_h[$i]' value='$txtCstJourAmount1' >
					<input type='hidden' name='txtCstJourAmount2[$i]' id='cstjouramount2_h[$i]' value='$txtCstJourAmount2' >
					<input type='hidden' name='txtCstJourBalPerc[$i]'  id='cstjourbalperc[$i]'  value='$txtCstJourBalPerc' >
					<input type='hidden' name='txtCstJourBalAmt[$i]' id='cstjourbalamt[$i]'  value='$txtCstJourBalAmt' >
					<input type='hidden' name='txtCstJourBalAmt1[$i]' id='cstjourbalamt1[$i]'  value='$txtCstJourBalAmt1' >
					<input type='hidden' name='txtCstJourBalAmt2[$i]' id='cstjourbalamt2[$i]'  value='$txtCstJourBalAmt2' >
					<input type='hidden' name='txtCstJourTotPerc[$i]'  id='cstjourtotperc[$i]'  value='$txtCstJourTotPerc' >
					<input type='hidden' name='txtCstJourTotAmt[$i]' id='cstjourtotamt[$i]'  value='$txtCstJourTotAmt' >
					<input type='hidden' name='txtCstJourTotAmt1[$i]' id='cstjourtotamt1[$i]'  value='$txtCstJourTotAmt1' >
					<input type='hidden' name='txtCstJourTotAmt2[$i]' id='cstjourtotamt2[$i]'  value='$txtCstJourTotAmt2' >";
				
		if ($Module_Project == 1 || $Module_ProjectDt == 1) {
			echo"<td width='125px;' align='center'><input type='text' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='$txtProjectCodeDt[$i]' class='".$ClassI."' onBlur=\"checkperc('$txtCstJourPerc')\" style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G3','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode[$i]');\" ".$KeyDown." /></span>
					<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onBlur=\"checkperc('$txtCstJourPerc')\" onClick='showlistProjectDt(25,1,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
		} else {
			echo"<input type='hidden' name='txtProjectCodeDt[$i]' id='projectcodedt[$i]' value='' />";
		}
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
			echo"<td width='125px;' align='center'><input type='text' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='$txtCostCentCodeDt[$i]' class='".$ClassI."' style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G3','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode[$i]');\" ".$KeyDown." /></span>
					<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"   onBlur=\"checkperc('$txtCstJourPerc')\" onClick='showlistCostcenterDt(25,1,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
		} else {
			echo"<input type='hidden' name='txtCostCentCodeDt[$i]' id='costcentcodedt[$i]' value='' />";
		}			
										
		echo"   <td width='45px;' align='center'><input type='text' name='txtCstJourPerc[$i]' id='cstjourperc[$i]' value='$txtCstJourPerc' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:40px; height:20px;' onBlur=\"CheckPercentage(this.id,'cstjourbalperc[$i]');checkperc('$txtCstJourPerc')\" onChange=\"SetModRecord('ModRec_G3','$i'); SetPercentageDivision(1,this.id,$DecimalBase1,$DecimalBase2,'',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount_D[$i]' id='cstjouramount[$i]' value='$txtCstJourAmount_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);checkperc('$txtCstJourPerc')' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmountDivision(1,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount1_D[$i]' id='cstjouramount1[$i]' value='$txtCstJourAmount1_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);checkperc('$txtCstJourPerc')' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmountDivision(2,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount2_D[$i]' id='cstjouramount2[$i]' value='$txtCstJourAmount2_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);checkperc('$txtCstJourPerc')' onChange=\"SetModRecord('ModRec_G3','$i'); SetAmountDivision(3,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
			
		
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_D[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitDivisionDt('".$i."','".$filepath."', '".$form."', 'ADD','datagrid-1-d','".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex',$down, $Module_RestrictProjCost, $Module_RestrictCost); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
}

function ShowTransDtGridDivisions2($connection,$form,$PTransId,$PTransDtId,$PDivType,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	
	$txtDate = $arrayparams['txtDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
		
	/// grid Motion
					$Motion = 1;
					$NextCell = 'chkjournumber2[0]';	
						$TabIndex = 1;
					$down = 1;
					
		
			//////////
			
			
			
			
	$columnsarray = array();
	if ($Module_Project == 1 || $Module_ProjectDt == 1) array_push($columnsarray, 'projectcodedt2');
	if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) array_push($columnsarray, 'costcentcodedt2');
	array_push($columnsarray, 'cstjourperc2');
	array_push($columnsarray, 'cstjouramount20');
	array_push($columnsarray, 'cstjouramount21');
	array_push($columnsarray, 'cstjouramount22');
	
	$columnsarrayall = array("projectcodedt2", "costcentcodedt2", "cstjourperc2", "cstjouramount20", "cstjouramount21", "cstjouramount22", 
	                         "cstjouramount20_h", "cstjouramount21_h", "cstjouramount22_h", "cstjourid2", "cstjourbalperc2", "cstjourbalamt20", 
							 "cstjourbalamt21", "cstjourbalamt22", "cstjourtotperc2", "cstjourtotamt20", "cstjourtotamt21", "cstjourtotamt22");
							 
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd, ".
				 " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
	$TransDtAmount    = $sub_result[6];
	$TransDtAmount1   = $sub_result[7];
	$TransDtAmount2   = $sub_result[8];
		
	//Initializations for Div Ids
	$ContainerDiv = 'container4';
	$LoaderDiv    = 'Loader4';
	$HeaderDiv    = 'grid_headerDiv4';
	$ContentDiv   = 'grid_contentDiv4';
	$TableFocus   = 'table-focus-g4';
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv ' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:580px;'>";
	
		echo"<tr height='20px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		
		if ($Module_Project == 1 || $Module_ProjectDt == 1) {
			echo"  <th width='125px;' align='center' ><span class='style1'>Project</span></th>";	
		}
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
			echo"  <th width='125px;' align='center' ><span class='style1'>Costcenter</span></th>";	
		}
		
		echo"  <th width='45px;' align='center' ><span >Perc</span><span class='style2'>*</span></th>";
		echo"  <th width='85px;' align='center' ><span >Amount</span><span class='style2'>*</span></th>";
		echo"  <th width='85px;' align='center' ><span >$Base1</span> </th>";
		echo"  <th width='85px;' align='center' ><span >$Base2</span></th>";
		echo"  <th width='25px;' align='center' ><span >&nbsp;</span></th>";
		echo"  <th width='25px;' align='center' ><span >&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:580px;'>";
		echo"<input type='hidden' name='SelectedRec_G4' id='SelectedRec_G4' value='$SelectedRec_G4'>
			 <input type='hidden' name='SelectedRecColor_G4' id='SelectedRecColor_G4' value='$SelectedRecColor_G4'>
			 <input type='hidden' name='ModRec_G4' id='ModRec_G4' value='$ModRec_G4'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, (100 - Round(Sum(ifNull(CstJourDt_Per,0)),2)) As CstJourDtPer, ".
						  " (ifNull(TransDt_Amount,0)  - Round(Sum(ifNull(CstJourDt_Amount,0)),9) ) As CstJourDtAmount, ".
						  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(CstJourDt_Amount1,0)),9) ) As CstJourDtAmount1, ".
						  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(CstJourDt_Amount2,0)),9) ) As CstJourDtAmount2, ".
						  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
						  " From Temp_TransDt As TransDt ".
						  " Inner Join Temp_CstjourDt As CstjourDt on TransDt.TransDt_ID = CstjourDt.TransDt_ID ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtCstJourBalPerc2_M  = $sub_result2[1];
			$txtCstJourBalAmt20_M  = $sub_result2[2];
			$txtCstJourBalAmt21_M  = $sub_result2[3];
			$txtCstJourBalAmt22_M  = $sub_result2[4];
				
			$txtCstJourTotPerc2  = 100;
			$txtCstJourTotAmt20  = $sub_result2[5];
			$txtCstJourTotAmt21  = $sub_result2[6];
			$txtCstJourTotAmt22  = $sub_result2[7];
			
				
			$query = " SELECT CstJourDt_id, TransDt_ID, Project_Code, Costcenter_Code, ".
					 " Round(ifNull(CstJourDt_Amount,0), ".$DecimalBase."), Round(ifNull(CstJourDt_Amount1,0), ".$DecimalBase1."), ".
					 " Round(ifNull(CstJourDt_Amount2,0), ".$DecimalBase2."), Round(ifNull(CstJourDt_Per,0),2), ".
					 " ifNull(CstJourDt_Amount,0), ifNull(CstJourDt_Amount1,0), ifNull(CstJourDt_Amount2,0) ".
					 " FROM Temp_CstjourDt As CstjourDt ".
					 " Where CstjourDt.TransDt_ID = '".$PTransDtId."' ";
				
			//echo $query;
			
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtCstJourBalPerc2  = $txtCstJourBalPerc2_M + $rowsdt[7];
				$txtCstJourBalAmt20  = $txtCstJourBalAmt20_M + $rowsdt[8];
				$txtCstJourBalAmt21  = $txtCstJourBalAmt21_M + $rowsdt[9];
				$txtCstJourBalAmt22  = $txtCstJourBalAmt22_M + $rowsdt[10];
			
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_D['.$i.']\')"';
				
					
				echo"<tr id='CurRec_G4$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteDivisionDt('".$filepath."','".$form."','DEL','datagrid-1-d','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";

				echo"		<input type='hidden' name='txtCstJourId2[$i]' id='cstjourid2[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtCstJourAmount20[$i]'  id='cstjouramount20_h[$i]'  value='".$rowsdt[8]."' >
							<input type='hidden' name='txtCstJourAmount21[$i]' id='cstjouramount21_h[$i]' value='".$rowsdt[9]."' >
							<input type='hidden' name='txtCstJourAmount22[$i]' id='cstjouramount22_h[$i]' value='".$rowsdt[10]."' >
							<input type='hidden' name='txtCstJourBalPerc2[$i]'  id='cstjourbalperc2[$i]'  value='$txtCstJourBalPerc2' >
							<input type='hidden' name='txtCstJourBalAmt20[$i]' id='cstjourbalamt20[$i]'  value='$txtCstJourBalAmt20' >
							<input type='hidden' name='txtCstJourBalAmt21[$i]' id='cstjourbalamt21[$i]'  value='$txtCstJourBalAmt21' >
							<input type='hidden' name='txtCstJourBalAmt22[$i]' id='cstjourbalamt22[$i]'  value='$txtCstJourBalAmt22' >
							<input type='hidden' name='txtCstJourTotPerc2[$i]'  id='cstjourtotperc2[$i]'  value='$txtCstJourTotPerc2' >
							<input type='hidden' name='txtCstJourTotAmt20[$i]' id='cstjourtotamt20[$i]'  value='$txtCstJourTotAmt20' >
							<input type='hidden' name='txtCstJourTotAmt21[$i]' id='cstjourtotamt21[$i]'  value='$txtCstJourTotAmt21' >
							<input type='hidden' name='txtCstJourTotAmt22[$i]' id='cstjourtotamt22[$i]'  value='$txtCstJourTotAmt22' >";
							
				if ($Module_Project == 1 || $Module_ProjectDt == 1) {
					echo"<td width='125px;' align='center'><input type='text' name='txtProjectCodeDt2[$i]' id='projectcodedt2[$i]' value='".$rowsdt[2]."' class='".$ClassE."' style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G4','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode2[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(25,2,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
				} else {
					echo"<input type='hidden' name='txtProjectCodeDt2[$i]' id='projectcodedt2[$i]' value='' />";
				}
				if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
					echo"<td width='125px;' align='center'><input type='text' name='txtCostCentCodeDt2[$i]' id='costcentcodedt2[$i]' value='".$rowsdt[3]."' class='".$ClassE."' style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G4','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode2[$i]');\" ".$KeyDown." /></span>
							<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(25,2,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
				} else {
					echo"<input type='hidden' name='txtCostCentCodeDt2[$i]' id='costcentcodedt2[$i]' value='' />";
				}			
							
				echo"   <td width='45px;' align='center'><input type='text' name='txtCstJourPerc2[$i]' id='cstjourperc2[$i]' value='".$rowsdt[7]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:40px; height:20px;' onBlur=\"CheckPercentage(this.id,'cstjourperc2[$i]');\" onChange=\"SetModRecord('ModRec_G4','$i'); SetPercentageDivision(2,this.id,$DecimalBase1,$DecimalBase2,'',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount20_D[$i]' id='cstjouramount20[$i]' value='".$rowsdt[4]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmountDivision(4,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount21_D[$i]' id='cstjouramount21[$i]' value='".$rowsdt[5]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmountDivision(5,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount22_D[$i]' id='cstjouramount22[$i]' value='".$rowsdt[6]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmountDivision(6,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_D[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitDivisionDt2('".$i."','".$filepath."', '".$form."', 'ADD','datagrid-2-d','".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex',$down, $Module_RestrictProjCost, $Module_RestrictCost); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_D['.$i.']\')"';
			
		$sub_query2 = " Select TransDt.TransDt_ID, (100 - Round(Sum(ifNull(CstJourDt_Per,0)),2)) As CstJourDtPer,  ".
					  " Round((ifNull(TransDt_Amount,0)  - Sum(ifNull(CstJourDt_Amount,0))), ".$DecimalBase.")  As CstJourDtAmount_D, ".
					  " Round((ifNull(TransDt_Amount1,0) - Sum(ifNull(CstJourDt_Amount1,0))), ".$DecimalBase1.") As CstJourDtAmount1_D, ".
					  " Round((ifNull(TransDt_Amount2,0) - Sum(ifNull(CstJourDt_Amount2,0))), ".$DecimalBase2.") As CstJourDtAmount2_D, ".
					  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0), ".
					  " (ifNull(TransDt_Amount,0) -  Round(Sum(ifNull(CstJourDt_Amount,0)),9) ) As CstJourDtAmount, ".
					  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(CstJourDt_Amount1,0)),9) ) As CstJourDtAmount1, ".
					  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(CstJourDt_Amount2,0)),9) ) As CstJourDtAmount2 ".
					  " From Temp_TransDt As TransDt ".
					  " Inner Join Temp_CstjourDt As CstjourDt on TransDt.TransDt_ID = CstjourDt.TransDt_ID ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID ";

		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		if ($sub_result2[0] != "") { 
			$txtCstJourBalPerc2  = $sub_result2[1];
			$txtCstJourBalAmt20  = $sub_result2[8];
			$txtCstJourBalAmt21  = $sub_result2[9];
			$txtCstJourBalAmt22  = $sub_result2[10];
			
			$txtCstJourTotPerc2  = 100;
			$txtCstJourTotAmt20  = $sub_result2[5];
			$txtCstJourTotAmt21  = $sub_result2[6];
			$txtCstJourTotAmt22  = $sub_result2[7];
			
			$txtCstJourPerc2       = $sub_result2[1];
			$txtCstJourAmount20_D  = $sub_result2[2];
			$txtCstJourAmount21_D  = $sub_result2[3];
			$txtCstJourAmount22_D  = $sub_result2[4];
			
			$txtCstJourAmount20  = $sub_result2[8];
			$txtCstJourAmount21  = $sub_result2[9];
			$txtCstJourAmount22  = $sub_result2[10];	
		} else {
			$txtCstJourBalPerc2  = 100;
			$txtCstJourBalAmt20  = $TransDtAmount;
			$txtCstJourBalAmt21  = $TransDtAmount1;
			$txtCstJourBalAmt22  = $TransDtAmount2;
			
			$txtCstJourTotPerc2  = 100;
			$txtCstJourTotAmt20  = $TransDtAmount;
			$txtCstJourTotAmt21  = $TransDtAmount1;
			$txtCstJourTotAmt22  = $TransDtAmount2;
			
			$txtCstJourPerc2       = 100;
			$txtCstJourAmount20_D  = number_format($TransDtAmount,$DecimalBase,".","");
			$txtCstJourAmount21_D  = number_format($TransDtAmount1,$DecimalBase1,".","");
			$txtCstJourAmount22_D  = number_format($TransDtAmount2,$DecimalBase2,".","");
			
			$txtCstJourAmount20  = $TransDtAmount;
			$txtCstJourAmount21  = $TransDtAmount1;
			$txtCstJourAmount22  = $TransDtAmount2;
		}
			

		echo"<tr id='CurRec_G4$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G4');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "		<input type='hidden' name='txtCstJourId2[$i]' id='cstjourid2[$i]' value='$txtCstJourId2' >
					<input type='hidden' name='txtCstJourAmount20[$i]'  id='cstjouramount20_h[$i]'  value='$txtCstJourAmount20' >
					<input type='hidden' name='txtCstJourAmount21[$i]' id='cstjouramount21_h[$i]' value='$txtCstJourAmount21' >
					<input type='hidden' name='txtCstJourAmount22[$i]' id='cstjouramount22_h[$i]' value='$txtCstJourAmount22' >
					<input type='hidden' name='txtCstJourBalPerc2[$i]'  id='cstjourbalperc2[$i]'  value='$txtCstJourBalPerc2' >
					<input type='hidden' name='txtCstJourBalAmt20[$i]' id='cstjourbalamt20[$i]'  value='$txtCstJourBalAmt20' >
					<input type='hidden' name='txtCstJourBalAmt21[$i]' id='cstjourbalamt21[$i]'  value='$txtCstJourBalAmt21' >
					<input type='hidden' name='txtCstJourBalAmt22[$i]' id='cstjourbalamt22[$i]'  value='$txtCstJourBalAmt22' >
					<input type='hidden' name='txtCstJourTotPerc2[$i]'  id='cstjourtotperc2[$i]'  value='$txtCstJourTotPerc2' >
					<input type='hidden' name='txtCstJourTotAmt20[$i]' id='cstjourtotamt20[$i]'  value='$txtCstJourTotAmt20' >
					<input type='hidden' name='txtCstJourTotAmt21[$i]' id='cstjourtotamt21[$i]'  value='$txtCstJourTotAmt21' >
					<input type='hidden' name='txtCstJourTotAmt22[$i]' id='cstjourtotamt22[$i]'  value='$txtCstJourTotAmt22' >";
				
		if ($Module_Project == 1 || $Module_ProjectDt == 1) {
			echo"<td width='125px;' align='center'><input type='text' name='txtProjectCodeDt2[$i]' id='projectcodedt2[$i]' value='$txtProjectCodeDt2[$i]' class='".$ClassI."'  onBlur=\"checkperc2('$txtCstJourPerc2')\"  style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G4','$i'); CheckLookup('chkcodeexists','project','project_code',this.value,'lstProjectCode2[$i]');\" ".$KeyDown." /></span>
					<label><span><img src='img/openlist.png' title='Select Project' id='lstProjectCode2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onClick='showlistProjectDt(25,2,$i); ' style='vertical-align:bottom;'/></span></label></td>";	
		} else {
			echo"<input type='hidden' name='txtProjectCodeDt2[$i]' id='projectcodedt2[$i]' value='' />";
		}
		if ($Module_CostCenter == 1 || $Module_CostCenterDt == 1) {
			echo"<td width='125px;' align='center'><input type='text' name='txtCostCentCodeDt2[$i]' id='costcentcodedt2[$i]' value='$txtCostCentCodeDt2[$i]' class='".$ClassI."' onBlur=\"checkperc2('$txtCstJourPerc2')\"  style='width:95px;height:20px;' maxlength='15' onchange=\"SetModRecord('ModRec_G4','$i'); CheckLookup('chkcodeexists','costcent','costcent_code',this.value,'lstCostCentCode2[$i]');\" ".$KeyDown." /></span>
					<label><span><img src='img/openlist.png' title='Select Costcenter' id='lstCostCentCode2[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\"  onClick='showlistCostcenterDt(25,2,$i); ' style='vertical-align:bottom;' /></span></label></td>";	
		} else {
			echo"<input type='hidden' name='txtCostCentCodeDt2[$i]' id='costcentcodedt2[$i]' value='' />";
		}			
										
		echo"   <td width='45px;' align='center'><input type='text' name='txtCstJourPerc2[$i]' id='cstjourperc2[$i]' value='$txtCstJourPerc2' maxlength='20' class='".$ClassI."'  onBlur=\"checkperc2('$txtCstJourPerc2')\" style='text-align:right; font-size:12px; width:40px; height:20px;' onBlur=\"CheckPercentage(this.id,'cstjourbalperc2[$i]');\" onChange=\"SetModRecord('ModRec_G4','$i'); SetPercentageDivision(2,this.id,$DecimalBase1,$DecimalBase2,'',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount20_D[$i]' id='cstjouramount20[$i]' value='$txtCstJourAmount20_D' maxlength='20' class='".$ClassI."' onBlur=\"checkperc2('$txtCstJourPerc2')\"  style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmountDivision(4,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount21_D[$i]' id='cstjouramount21[$i]' value='$txtCstJourAmount21_D' maxlength='20' class='".$ClassI."'  onBlur=\"checkperc2('$txtCstJourPerc2')\" style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmountDivision(5,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='85px;' align='center'><input type='text' name='txtCstJourAmount22_D[$i]' id='cstjouramount22[$i]' value='$txtCstJourAmount22_D' maxlength='20' class='".$ClassI."'  onBlur=\"checkperc2('$txtCstJourPerc2')\" style='text-align:right; font-size:12px; width:80px; height:20px;' onBlur='CheckAmount(this.id);' onChange=\"SetModRecord('ModRec_G4','$i'); SetAmountDivision(6,$DecimalBase1,$DecimalBase2,'".$TransDtCurrCode."','".$Base1."','".$Base2."','".$txtDate."','',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_D[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitDivisionDt2('".$i."','".$filepath."', '".$form."', 'ADD','datagrid-2-d','".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex',$down, $Module_RestrictProjCost, $Module_RestrictCost); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
}

function ShowTransDtGridChecks($connection,$form,$PTransId,$PTransDtId,$arrayparams,$arrayaccess){
	
	
		$Module_Project = $arrayparams['Module_Project'];
		$Module_CostCenter = $arrayparams['Module_CostCenter'];
		$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
		$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
		$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
		$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
		$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
		$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
		$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
		$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
		
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate = $arrayparams['txtDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	$columnsarray = array("chkjournumber", "chkjourbank", "chkjourvaluedate", "chkjouramount", "chkjouramount1", "chkjouramount2");
	$columnsarrayall = array("chkjournumber", "chkjourbank", "chkjourvaluedate", "chkjouramount", "chkjouramount1", "chkjouramount2", "chkjouramount_h", "chkjouramount1_h", "chkjouramount2_h", 
							 "chkjourledgerno", "chkjourcurrcode", "chkjourid", "chkjourtype", "chkjourbalamt", "chkjourbalamt1", "chkjourbalamt2", "chkjourtotamt", "chkjourtotamt1", "chkjourtotamt2");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
		
		
	if(($LedgerActivity==1 && $TransDtdbcd=='D') or ($LedgerActivity==2 && $TransDtdbcd=='C')) {
		$showlist = 0;
		$status = '';	
	}	
	else {
		$showlist = 1;
		$status = 'readonly';	
	}	
		

		
		/// grid Motion
		$Motion = 1;
		$NextCell = 'billjournumber[0]';
		$down = 0;
		
	
		if ($Module_TransStandardView==1 && ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1)){
			$TabIndex = 2 ;		
			}
		else 	
		$TabIndex = 1 ;		
	/////////////
	
	
	
	
	//Initializations for Div Ids
	$ContainerDiv = 'container5';
	$LoaderDiv    = 'Loader5';
	$HeaderDiv    = 'grid_headerDiv5';
	$ContentDiv   = 'grid_contentDiv5';
	$TableFocus   = 'table-focus-g5';
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='105px;' align='center'>Check No</th>";
		echo"  <th width='105px;' align='left'><span>Bank</span></th>";
		echo"  <th width='70px;' align='center'><span>Value Date</span></th>";
		echo"  <th width='45px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='95px;' align='center'><span>Amount</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
		echo"<input type='hidden' name='SelectedRec_G5' id='SelectedRec_G5' value='$SelectedRec_G5'>
			 <input type='hidden' name='SelectedRecColor_G5' id='SelectedRecColor_G5' value='$SelectedRecColor_G5'>
			 <input type='hidden' name='ModRec_G5' id='ModRec_G5' value='$ModRec_G5'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, ".
						  " (ifNull(TransDt_Amount,0)  - Round(Sum(ifNull(Chk_Amount,0)),9) ) As ChkAmount, ".
						  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Chk_Amount1,0)),9) ) As ChkAmount1, ".
						  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Chk_Amount2,0)),9) ) As ChkAmount2, ".
						  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
						  " From Temp_TransDt As TransDt Left Join Temp_ChkJourDt on TransDt.TransDt_ID = Temp_ChkJourDt.TransDt_ID ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtChkJourBalAmt_M   = $sub_result2[1];
			$txtChkJourBalAmt1_M  = $sub_result2[2];
			$txtChkJourBalAmt2_M  = $sub_result2[3];
			
			$txtChkJourTotAmt   = $sub_result2[4];
			$txtChkJourTotAmt1  = $sub_result2[5];
			$txtChkJourTotAmt2  = $sub_result2[6];
			
		 
			$query = " SELECT ChkJourDt_Id, TransDt_ID, Ledger_Number, Chk_Number, Chk_Bank, date_format(Chk_ValueDate,'%d/%m/%Y'), Chk_Type, ChkJourDt.Currency_Code, ".
					 " Round(ifNull(Chk_Amount,0), ".$DecimalBase."), Round(ifNull(Chk_Amount1,0), ".$DecimalBase1."), Round(ifNull(Chk_Amount2,0), ".$DecimalBase2."), ".
					 " ifNull(Chk_Amount,0), ifNull(Chk_Amount1,0), ifNull(Chk_Amount2,0) ".
					 " FROM Temp_ChkJourDt As ChkJourDt ".
					 " WHERE ChkJourDt.TransDt_ID = '".$PTransDtId."'";
				
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtChkJourBalAmt   = $txtChkJourBalAmt_M  + $rowsdt[11];
				$txtChkJourBalAmt1  = $txtChkJourBalAmt1_M + $rowsdt[12];
				$txtChkJourBalAmt2  = $txtChkJourBalAmt2_M + $rowsdt[13];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_C['.$i.']\')"';
				
				echo"<tr id='CurRec_G5$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteChkJour('".$filepath."','".$form."','DEL','datagrid-1-c','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='105px;' align='left'>
							<input type='hidden' name='txtChkJourId[$i]' id='chkjourid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtChkJourType[$i]' id='chkjourtype[$i]' value='".$rowsdt[6]."' >
							<input type='hidden' name='txtChkJourLedgerNo[$i]' id='chkjourledgerno[$i]' value='".$rowsdt[2]."' >
							<input type='hidden' name='txtChkJourAmount[$i]'  id='chkjouramount_h[$i]'  value='".$rowsdt[11]."' >
							<input type='hidden' name='txtChkJourAmount1[$i]' id='chkjouramount1_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtChkJourAmount2[$i]' id='chkjouramount2_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtChkJourBalAmt[$i]' id='chkjourbalamt[$i]'  value='$txtChkJourBalAmt' >
							<input type='hidden' name='txtChkJourBalAmt1[$i]' id='chkjourbalamt1[$i]'  value='$txtChkJourBalAmt1' >
							<input type='hidden' name='txtChkJourBalAmt2[$i]' id='chkjourbalamt2[$i]'  value='$txtChkJourBalAmt2' >
							<input type='hidden' name='txtChkJourTotAmt[$i]' id='chkjourtotamt[$i]'  value='$txtChkJourTotAmt' >
							<input type='hidden' name='txtChkJourTotAmt1[$i]' id='chkjourtotamt1[$i]'  value='$txtChkJourTotAmt1' >
							<input type='hidden' name='txtChkJourTotAmt2[$i]' id='chkjourtotamt2[$i]'  value='$txtChkJourTotAmt2' >
							<input type='text' name='txtChkJourNumber[$i]' id='chkjournumber[$i]' value='".$rowsdt[3]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onChange=\"SetModRecord('ModRec_G5','$i');\" ".$KeyDown." $status />";
							
							if ($showlist == 1) 
								echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistCheck(1,".$i.",'".$PTransDtId."','".$rowsdt[0]."');\" style='vertical-align:bottom;' title='Select Check'  /></span></label>";
				echo"	</td>";
	
										
				echo"  	<td width='105px;' align='center'><input type='text' name='txtChkJourBank[$i]' id='chkjourbank[$i]' value='".$rowsdt[4]."' maxlength='30' class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G5','$i');\" ".$KeyDown."  /></td>";
				echo"   <td width='70px;' align='center'><input type='text' name='txtChkJourValueDate[$i]' id='chkjourvaluedate[$i]' value='".$rowsdt[5]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:65px; height:20px;' onChange=\"SetModRecord('ModRec_G5','$i');\" ".$KeyDown." $status ></td>";
				echo"   <td width='45px;' align='center'><input type='text' name='txtChkJourCurrCode[$i]' id='chkjourcurrcode[$i]' value='".$rowsdt[7]."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:40px; height:20px;' readonly ></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount_D[$i]' id='chkjouramount[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G5','$i'); SetAmount(5,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount1_D[$i]' id='chkjouramount1[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G5','$i'); SetAmount(6,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount2_D[$i]' id='chkjouramount2[$i]' value='".$rowsdt[10]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G5','$i'); SetAmount(7,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitChkJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-c', '".$PTransDtId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_C['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_C[".$i."]','1');LeaveGrid('chkjournumber[$i]','billjournumber[0]','','')\" ";		
					
		//Initializations
		
		if(($LedgerActivity==1 && $TransDtdbcd=='D') or ($LedgerActivity==2 && $TransDtdbcd=='C'))
			$txtChkJourType = 'A'; 
		else $txtChkJourType = 'C';
		 
		
		if (empty($txtChkJourCurrCode))	 $txtChkJourCurrCode  = $TransDtCurrCode;
		if (empty($txtChkJourValueDate)) $txtChkJourValueDate = $TransDtValueDate;
		if (empty($txtChkJourLedgerNo))  $txtChkJourLedgerNo  = $LedgerNumber;
			
		
		$sub_query2 = " Select TransDt.TransDt_ID, ".
					  " Round((ifNull(TransDt_Amount,0)  - Sum(ifNull(Chk_Amount,0))), ".$DecimalBase.")  As ChkAmount_D, ".
					  " Round((ifNull(TransDt_Amount1,0) - Sum(ifNull(Chk_Amount1,0))), ".$DecimalBase1.") As ChkAmount1_D, ".
					  " Round((ifNull(TransDt_Amount2,0) - Sum(ifNull(Chk_Amount2,0))), ".$DecimalBase2.") As ChkAmount2_D, ".
					  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0), ".
					  " (ifNull(TransDt_Amount,0) -  Round(Sum(ifNull(Chk_Amount,0)),9) ) As ChkAmount, ".
					  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Chk_Amount1,0)),9) ) As ChkAmount1, ".
					  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Chk_Amount2,0)),9) ) As ChkAmount2 ".
					  " From Temp_TransDt As TransDt Left Join Temp_ChkJourDt on TransDt.TransDt_ID = Temp_ChkJourDt.TransDt_ID ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
					  
							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtChkJourBalAmt   = $sub_result2[7];
		$txtChkJourBalAmt1  = $sub_result2[8];
		$txtChkJourBalAmt2  = $sub_result2[9];
			
		$txtChkJourTotAmt   = $sub_result2[4];
		$txtChkJourTotAmt1  = $sub_result2[5];
		$txtChkJourTotAmt2  = $sub_result2[6];
			
		$txtChkJourAmount_D   = $sub_result2[1];
		$txtChkJourAmount1_D  = $sub_result2[2];
		$txtChkJourAmount2_D  = $sub_result2[3];
		
		$txtChkJourAmount   = $sub_result2[7];
		$txtChkJourAmount1  = $sub_result2[8];
		$txtChkJourAmount2  = $sub_result2[9];	
				
		echo"<tr id='CurRec_G5$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G3');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='105px;' align='left'>
					<input type='hidden' name='txtChkJourId[$i]' id='chkjourid[$i]' value='$txtChkJourId' >
					<input type='hidden' name='txtChkJourType[$i]' id='chkjourtype[$i]' value='$txtChkJourType' >
					<input type='hidden' name='txtChkJourLedgerNo[$i]' id='chkjourledgerno[$i]' value='$txtChkJourLedgerNo' >
					<input type='hidden' name='txtChkJourAmount[$i]'  id='chkjouramount_h[$i]'  value='$txtChkJourAmount' >
					<input type='hidden' name='txtChkJourAmount1[$i]' id='chkjouramount1_h[$i]' value='$txtChkJourAmount1' >
					<input type='hidden' name='txtChkJourAmount2[$i]' id='chkjouramount2_h[$i]' value='$txtChkJourAmount2' >
					<input type='hidden' name='txtChkJourBalAmt[$i]' id='chkjourbalamt[$i]'  value='$txtChkJourBalAmt' >
					<input type='hidden' name='txtChkJourBalAmt1[$i]' id='chkjourbalamt1[$i]'  value='$txtChkJourBalAmt1' >
					<input type='hidden' name='txtChkJourBalAmt2[$i]' id='chkjourbalamt2[$i]'  value='$txtChkJourBalAmt2' >
					<input type='hidden' name='txtChkJourTotAmt[$i]' id='chkjourtotamt[$i]'  value='$txtChkJourTotAmt' >
					<input type='hidden' name='txtChkJourTotAmt1[$i]' id='chkjourtotamt1[$i]'  value='$txtChkJourTotAmt1' >
					<input type='hidden' name='txtChkJourTotAmt2[$i]' id='chkjourtotamt2[$i]'  value='$txtChkJourTotAmt2' >
					<input type='text' name='txtChkJourNumber[$i]' id='chkjournumber[$i]' value='$txtChkJourNumber'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;'  onBlur=\"LeaveGrid('chkjournumber[$i]','billjournumber[0]','tab1','".$TabIndex."')\"   onChange=\"SetModRecord('ModRec_G5','$i');\" ".$KeyDown1." $status />";
					
					if ($showlist == 1) 
						echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistCheck(1,".$i.",'".$PTransDtId."','-1');\" style='vertical-align:bottom;' title='Select Check'  /></span></label>";
		echo"	</td>";
										
		echo"  	<td width='105px;' align='center'><input type='text' name='txtChkJourBank[$i]' id='chkjourbank[$i]' value='$txtChkJourBank' maxlength='30' class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G5','$i');\" ".$KeyDown."  /></td>";
		echo"   <td width='70px;' align='center'><input type='text' name='txtChkJourValueDate[$i]' id='chkjourvaluedate[$i]' value='$txtChkJourValueDate' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:65px; height:20px;' onChange=\"SetModRecord('ModRec_G5','$i');\" ".$KeyDown." $status /></td>";
		echo"   <td width='45px;' align='center'><input type='text' name='txtChkJourCurrCode[$i]' id='chkjourcurrcode[$i]' value='$txtChkJourCurrCode' maxlength='10' class='".$ClassI."' style='font-size:12px; width:40px; height:20px;' readonly ></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount_D[$i]' id='chkjouramount[$i]' value='$txtChkJourAmount_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G5','$i'); SetAmount(5,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount1_D[$i]' id='chkjouramount1[$i]' value='$txtChkJourAmount1_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G5','$i'); SetAmount(6,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount2_D[$i]' id='chkjouramount2[$i]' value='$txtChkJourAmount2_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G5','$i'); SetAmount(7,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitChkJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-1-c', '".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex','$down'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}

function ShowTransDtGridChecks2($connection,$form,$PTransId,$PTransDtId,$arrayparams,$arrayaccess){
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
	$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
	$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
	$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
	$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
	$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
		$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate = $arrayparams['txtDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	$columnsarray = array("chkjournumber2", "chkjourbank2", "chkjourvaluedate2", "chkjouramount02", "chkjouramount21", "chkjouramount22");
	$columnsarrayall = array("chkjournumber2", "chkjourbank2", "chkjourvaluedate2", "chkjouramount02", "chkjouramount21", "chkjouramount22", "chkjouramount02_h", "chkjouramount21_h", "chkjouramount22_h", 
							 "chkjourledgerno2", "chkjourcurrcode2", "chkjourid2", "chkjourtype2", "chkjourbalamt02", "chkjourbalamt21", "chkjourbalamt22", "chkjourtotamt02", "chkjourtotamt21", "chkjourtotamt22");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
		
		
	if(($LedgerActivity==1 && $TransDtdbcd=='D') or ($LedgerActivity==2 && $TransDtdbcd=='C')) {
		$showlist = 0;
		$status = '';	
	}	
	else {
		$showlist = 1;
		$status = 'readonly';	
	}	
		
		
		$Motion = 1;
		$NextCell = 'billjournumber2[0]';
		$down = 0;
		
	
		if ($Module_TransStandardView==1 && ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1)){
			$TabIndex = 2 ;		
			}
		else 	
		$TabIndex = 1 ;		
	/////////////
	
	

	//Initializations for Div Ids
	$ContainerDiv = 'container6';
	$LoaderDiv    = 'Loader6';
	$HeaderDiv    = 'grid_headerDiv6';
	$ContentDiv   = 'grid_contentDiv6';
	$TableFocus   = 'table-focus-g6';
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='105px;' align='center'><span>Check No</span></th>";
		echo"  <th width='105px;' align='left'><span>Bank</span></th>";
		echo"  <th width='70px;' align='center'><span>Value Date</span></th>";
		echo"  <th width='45px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='95px;' align='center'><span>Amount</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
		echo"<input type='hidden' name='SelectedRec_G6' id='SelectedRec_G6' value='$SelectedRec_G6'>
			 <input type='hidden' name='SelectedRecColor_G6' id='SelectedRecColor_G6' value='$SelectedRecColor_G6'>
			 <input type='hidden' name='ModRec_G6' id='ModRec_G6' value='$ModRec_G6'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, ".
						  " (ifNull(TransDt_Amount,0)  - Round(Sum(ifNull(Chk_Amount,0)),9) ) As ChkAmount, ".
						  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Chk_Amount1,0)),9) ) As ChkAmount1, ".
						  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Chk_Amount2,0)),9) ) As ChkAmount2, ".
						  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
						  " From Temp_TransDt As TransDt Left Join Temp_ChkJourDt on TransDt.TransDt_ID = Temp_ChkJourDt.TransDt_ID ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtChkJourBalAmt02_M  = $sub_result2[1];
			$txtChkJourBalAmt21_M  = $sub_result2[2];
			$txtChkJourBalAmt22_M  = $sub_result2[3];
			
			$txtChkJourTotAmt02  = $sub_result2[4];
			$txtChkJourTotAmt21  = $sub_result2[5];
			$txtChkJourTotAmt22  = $sub_result2[6];
			
		 
			$query = " SELECT ChkJourDt_Id, TransDt_ID, Ledger_Number, Chk_Number, Chk_Bank, date_format(Chk_ValueDate,'%d/%m/%Y'), Chk_Type, ChkJourDt.Currency_Code, ".
					 " Round(ifNull(Chk_Amount,0), ".$DecimalBase."), Round(ifNull(Chk_Amount1,0), ".$DecimalBase1."), Round(ifNull(Chk_Amount2,0), ".$DecimalBase2."), ".
					 " ifNull(Chk_Amount,0), ifNull(Chk_Amount1,0), ifNull(Chk_Amount2,0) ".
					 " FROM Temp_ChkJourDt As ChkJourDt ".
					 " WHERE ChkJourDt.TransDt_ID = '".$PTransDtId."'";
				
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtChkJourBalAmt02  = $txtChkJourBalAmt02_M + $rowsdt[11];
				$txtChkJourBalAmt21  = $txtChkJourBalAmt21_M + $rowsdt[12];
				$txtChkJourBalAmt22  = $txtChkJourBalAmt22_M + $rowsdt[13];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_C['.$i.']\')"';
				
				echo"<tr id='CurRec_G6$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteChkJour('".$filepath."','".$form."','DEL','datagrid-2-c','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='105px;' align='left'>
							<input type='hidden' name='txtChkJourId2[$i]' id='chkjourid2[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtChkJourType2[$i]' id='chkjourtype2[$i]' value='".$rowsdt[6]."' >
							<input type='hidden' name='txtChkJourLedgerNo2[$i]' id='chkjourledgerno2[$i]' value='".$rowsdt[2]."' >
							<input type='hidden' name='txtChkJourAmount02[$i]'  id='chkjouramount02_h[$i]'  value='".$rowsdt[11]."' >
							<input type='hidden' name='txtChkJourAmount21[$i]' id='chkjouramount21_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtChkJourAmount22[$i]' id='chkjouramount22_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtChkJourBalAmt02[$i]' id='chkjourbalamt02[$i]'  value='$txtChkJourBalAmt02' >
							<input type='hidden' name='txtChkJourBalAmt21[$i]' id='chkjourbalamt21[$i]'  value='$txtChkJourBalAmt21' >
							<input type='hidden' name='txtChkJourBalAmt22[$i]' id='chkjourbalamt22[$i]'  value='$txtChkJourBalAmt22' >
							<input type='hidden' name='txtChkJourTotAmt02[$i]' id='chkjourtotamt02[$i]'  value='$txtChkJourTotAmt02' >
							<input type='hidden' name='txtChkJourTotAmt21[$i]' id='chkjourtotamt21[$i]'  value='$txtChkJourTotAmt21' >
							<input type='hidden' name='txtChkJourTotAmt22[$i]' id='chkjourtotamt22[$i]'  value='$txtChkJourTotAmt22' >
							<input type='text' name='txtChkJourNumber2[$i]' id='chkjournumber2[$i]' value='".$rowsdt[3]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onChange=\"SetModRecord('ModRec_G6','$i');\" ".$KeyDown." $status />";
							
							if ($showlist == 1) 
								echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistCheck(2,".$i.",'".$PTransDtId."','".$rowsdt[0]."');\" style='vertical-align:bottom;' title='Select Check'  /></span></label>";
				echo"	</td>";
	
										
				echo"  	<td width='105px;' align='center'><input type='text' name='txtChkJourBank2[$i]' id='chkjourbank2[$i]' value='".$rowsdt[4]."' maxlength='30' class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G6','$i');\" ".$KeyDown."  /></td>";
				echo"   <td width='70px;' align='center'><input type='text' name='txtChkJourValueDate2[$i]' id='chkjourvaluedate2[$i]' value='".$rowsdt[5]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:65px; height:20px;' onChange=\"SetModRecord('ModRec_G6','$i');\" ".$KeyDown." $status ></td>";
				echo"   <td width='45px;' align='center'><input type='text' name='txtChkJourCurrCode2[$i]' id='chkjourcurrcode2[$i]' value='".$rowsdt[7]."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:40px; height:20px;' readonly ></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount02_D[$i]' id='chkjouramount02[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G6','$i'); SetAmount(15,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount21_D[$i]' id='chkjouramount21[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G6','$i'); SetAmount(16,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount22_D[$i]' id='chkjouramount22[$i]' value='".$rowsdt[10]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G6','$i'); SetAmount(17,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitChkJour2('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-2-c', '".$PTransDtId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_C['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_2_C[".$i."]','1');LeaveGrid('chkjournumber2[$i]','billjournumber2[0]','tab3','$TabIndex')\" ";		
					
		//Initializations
		
		if(($LedgerActivity==1 && $TransDtdbcd=='D') or ($LedgerActivity==2 && $TransDtdbcd=='C'))
			$txtChkJourType2 = 'A'; 
		else $txtChkJourType2 = 'C';
		 
		
		if (empty($txtChkJourCurrCode2))  $txtChkJourCurrCode2  = $TransDtCurrCode;
		if (empty($txtChkJourValueDate2)) $txtChkJourValueDate2 = $TransDtValueDate;
		if (empty($txtChkJourLedgerNo2))  $txtChkJourLedgerNo2  = $LedgerNumber;
			
		
		$sub_query2 = " Select TransDt.TransDt_ID, ".
					  " Round((ifNull(TransDt_Amount,0)  - Sum(ifNull(Chk_Amount,0))), ".$DecimalBase.")  As ChkAmount_D, ".
					  " Round((ifNull(TransDt_Amount1,0) - Sum(ifNull(Chk_Amount1,0))), ".$DecimalBase1.") As ChkAmount1_D, ".
					  " Round((ifNull(TransDt_Amount2,0) - Sum(ifNull(Chk_Amount2,0))), ".$DecimalBase2.") As ChkAmount2_D, ".
					  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0), ".
					  " (ifNull(TransDt_Amount,0) -  Round(Sum(ifNull(Chk_Amount,0)),9) ) As ChkAmount, ".
					  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Chk_Amount1,0)),9) ) As ChkAmount1, ".
					  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Chk_Amount2,0)),9) ) As ChkAmount2 ".
					  " From Temp_TransDt As TransDt Left Join Temp_ChkJourDt on TransDt.TransDt_ID = Temp_ChkJourDt.TransDt_ID ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
					  
							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtChkJourBalAmt02  = $sub_result2[7];
		$txtChkJourBalAmt21  = $sub_result2[8];
		$txtChkJourBalAmt22  = $sub_result2[9];
			
		$txtChkJourTotAmt02  = $sub_result2[4];
		$txtChkJourTotAmt21  = $sub_result2[5];
		$txtChkJourTotAmt22  = $sub_result2[6];
			
		$txtChkJourAmount02_D  = $sub_result2[1];
		$txtChkJourAmount21_D  = $sub_result2[2];
		$txtChkJourAmount22_D  = $sub_result2[3];
		
		$txtChkJourAmount02  = $sub_result2[7];
		$txtChkJourAmount21  = $sub_result2[8];
		$txtChkJourAmount22  = $sub_result2[9];	
				
		echo"<tr id='CurRec_G6$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G6');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='105px;' align='left'>
					<input type='hidden' name='txtChkJourId2[$i]' id='chkjourid2[$i]' value='$txtChkJourId2' >
					<input type='hidden' name='txtChkJourType2[$i]' id='chkjourtype2[$i]' value='$txtChkJourType2' >
					<input type='hidden' name='txtChkJourLedgerNo2[$i]' id='chkjourledgerno2[$i]' value='$txtChkJourLedgerNo2' >
					<input type='hidden' name='txtChkJourAmount02[$i]'  id='chkjouramount02_h[$i]'  value='$txtChkJourAmount02' >
					<input type='hidden' name='txtChkJourAmount21[$i]' id='chkjouramount21_h[$i]' value='$txtChkJourAmount21' >
					<input type='hidden' name='txtChkJourAmount22[$i]' id='chkjouramount22_h[$i]' value='$txtChkJourAmount22' >
					<input type='hidden' name='txtChkJourBalAmt02[$i]' id='chkjourbalamt02[$i]'  value='$txtChkJourBalAmt02' >
					<input type='hidden' name='txtChkJourBalAmt21[$i]' id='chkjourbalamt21[$i]'  value='$txtChkJourBalAmt21' >
					<input type='hidden' name='txtChkJourBalAmt22[$i]' id='chkjourbalamt22[$i]'  value='$txtChkJourBalAmt22' >
					<input type='hidden' name='txtChkJourTotAmt02[$i]' id='chkjourtotamt02[$i]'  value='$txtChkJourTotAmt02' >
					<input type='hidden' name='txtChkJourTotAmt21[$i]' id='chkjourtotamt21[$i]'  value='$txtChkJourTotAmt21' >
					<input type='hidden' name='txtChkJourTotAmt22[$i]' id='chkjourtotamt22[$i]'  value='$txtChkJourTotAmt22' >
					<input type='text' name='txtChkJourNumber2[$i]' id='chkjournumber2[$i]' value='$txtChkJourNumber2'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;'   onBlur=\"LeaveGrid('chkjournumber2[$i]','billjournumber2[0]','tab3','".$TabIndex."')\"   onChange=\"SetModRecord('ModRec_G6','$i');\" ".$KeyDown1." $status />";
					
					if ($showlist == 1) 
						echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistCheck(2,".$i.",'".$PTransDtId."','-1');\" style='vertical-align:bottom;' title='Select Check'  /></span></label>";
		echo"	</td>";
										
		echo"  	<td width='105px;' align='center'><input type='text' name='txtChkJourBank2[$i]' id='chkjourbank2[$i]' value='$txtChkJourBank2' maxlength='30' class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G6','$i');\" ".$KeyDown."  /></td>";
		echo"   <td width='70px;' align='center'><input type='text' name='txtChkJourValueDate2[$i]' id='chkjourvaluedate2[$i]' value='$txtChkJourValueDate2' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:65px; height:20px;' onChange=\"SetModRecord('ModRec_G6','$i');\" ".$KeyDown." $status /></td>";
		echo"   <td width='45px;' align='center'><input type='text' name='txtChkJourCurrCode2[$i]' id='chkjourcurrcode2[$i]' value='$txtChkJourCurrCode2' maxlength='10' class='".$ClassI."' style='font-size:12px; width:40px; height:20px;' readonly ></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount02_D[$i]' id='chkjouramount02[$i]' value='$txtChkJourAmount02_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G6','$i'); SetAmount(15,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode2."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount21_D[$i]' id='chkjouramount21[$i]' value='$txtChkJourAmount21_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G6','$i'); SetAmount(16,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode2."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtChkJourAmount22_D[$i]' id='chkjouramount22[$i]' value='$txtChkJourAmount22_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G6','$i'); SetAmount(17,$DecimalBase1,$DecimalBase2,'".$txtChkJourCurrCode2."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_C[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitChkJour2('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-2-c', '".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex','$down'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}
function ShowTransDtGridBills($connection,$form,$PTransId,$PTransDtId,$arrayparams,$arrayaccess){
	
	
		$Module_Project = $arrayparams['Module_Project'];
		$Module_CostCenter = $arrayparams['Module_CostCenter'];
		$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
		$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
		$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
		$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
		$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
		$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
		$ModuleInvoiceGrid = $arrayparams['Module_InvoiceGridPV'];
		$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
		$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
		
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate = $arrayparams['txtDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	if($ModuleInvoiceGrid==1) {
		/// grid Motion
		$Motion = 1;
		$NextCell = 'linkref[0]';
		$down = 0;
		
		
		if ($Module_TransStandardView==1 && ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1) ){
			$TabIndex = 3 ;		
			}
		else 	
		$TabIndex = 2 ;
		
	}
	else {
		$Motion = 1;
		$down = 0;
		$NextCell = 'linkref[0]';
		$TabIndex = 3 ;		
	}
	/////////////
	
	
	
	$columnsarray = array("billjournumber", "billjourbank", "billjourvaluedate", "billjouramount", "billjouramount1", "billjouramount2");
	$columnsarrayall = array("billjournumber", "billjourbank", "billjourvaluedate", "billjouramount", "billjouramount1", "billjouramount2", "billjouramount_h", "billjouramount1_h", "billjouramount2_h", 
							 "billjourledgerno", "billjourcurrcode", "billjourid", "billjourtype", "billjourbalamt", "billjourbalamt1", "billjourbalamt2", "billjourtotamt", "billjourtotamt1", "billjourtotamt2");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
		
		
	if(($LedgerActivity==3 && $TransDtdbcd=='D') or ($LedgerActivity==4 && $TransDtdbcd=='C')) {
		$showlist = 0;
		$status = '';	
	}	
	else {
		$showlist = 1;
		$status = 'readonly';	
	}	
		

	//Initializations for Div Ids
	$ContainerDiv = 'container7';
	$LoaderDiv    = 'Loader7';
	$HeaderDiv    = 'grid_headerDiv7';
	$ContentDiv   = 'grid_contentDiv7';
	$TableFocus   = 'table-focus-g7';	
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='105px;' align='center'><span>Bill No</span></th>";
		echo"  <th width='105px;' align='left'><span>Bank</span></th>";
		echo"  <th width='70px;' align='center'><span>Value Date</span></th>";
		echo"  <th width='45px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='95px;' align='center'><span>Amount</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
		echo"<input type='hidden' name='SelectedRec_G7' id='SelectedRec_G7' value='$SelectedRec_G7'>
			 <input type='hidden' name='SelectedRecColor_G7' id='SelectedRecColor_G7' value='$SelectedRecColor_G7'>
			 <input type='hidden' name='ModRec_G7' id='ModRec_G7' value='$ModRec_G7'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, ".
						  " (ifNull(TransDt_Amount,0)  - Round(Sum(ifNull(Bill_Amount,0)),9) ) As BillAmount, ".
						  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Bill_Amount1,0)),9) ) As BillAmount1, ".
						  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Bill_Amount2,0)),9) ) As BillAmount2, ".
						  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
						  " From Temp_TransDt As TransDt Left Join Temp_BillJourDt on TransDt.TransDt_ID = Temp_BillJourDt.TransDt_ID ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtBillJourBalAmt_M   = $sub_result2[1];
			$txtBillJourBalAmt1_M  = $sub_result2[2];
			$txtBillJourBalAmt2_M  = $sub_result2[3];
			
			$txtBillJourTotAmt   = $sub_result2[4];
			$txtBillJourTotAmt1  = $sub_result2[5];
			$txtBillJourTotAmt2  = $sub_result2[6];
			
		 
			$query = " SELECT BillJourDT_ID, TransDt_ID, Ledger_Number, Bill_Number, Bill_Bank, date_format(Bill_ValueDate,'%d/%m/%Y'), Bill_Type, BillJourDt.Currency_Code, ".
					 " Round(ifNull(Bill_Amount,0), ".$DecimalBase."), Round(ifNull(Bill_Amount1,0), ".$DecimalBase1."), Round(ifNull(Bill_Amount2,0), ".$DecimalBase2."), ".
					 " ifNull(Bill_Amount,0), ifNull(Bill_Amount1,0), ifNull(Bill_Amount2,0) ".
					 " FROM Temp_BillJourDt As BillJourDt ".
					 " WHERE BillJourDt.TransDt_ID = '".$PTransDtId."'";
				
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtBillJourBalAmt   = $txtBillJourBalAmt_M  + $rowsdt[11];
				$txtBillJourBalAmt1  = $txtBillJourBalAmt1_M + $rowsdt[12];
				$txtBillJourBalAmt2  = $txtBillJourBalAmt2_M + $rowsdt[13];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_B['.$i.']\')"';
				
				echo"<tr id='CurRec_G7$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteBillJour('".$filepath."','".$form."','DEL','datagrid-1-b','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='105px;' align='left'>
							<input type='hidden' name='txtBillJourId[$i]' id='billjourid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtBillJourType[$i]' id='billjourtype[$i]' value='".$rowsdt[6]."' >
							<input type='hidden' name='txtBillJourLedgerNo[$i]' id='billjourledgerno[$i]' value='".$rowsdt[2]."' >
							<input type='hidden' name='txtBillJourAmount[$i]'  id='billjouramount_h[$i]'  value='".$rowsdt[11]."' >
							<input type='hidden' name='txtBillJourAmount1[$i]' id='billjouramount1_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtBillJourAmount2[$i]' id='billjouramount2_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtBillJourBalAmt[$i]' id='billjourbalamt[$i]'  value='$txtBillJourBalAmt' >
							<input type='hidden' name='txtBillJourBalAmt1[$i]' id='billjourbalamt1[$i]'  value='$txtBillJourBalAmt1' >
							<input type='hidden' name='txtBillJourBalAmt2[$i]' id='billjourbalamt2[$i]'  value='$txtBillJourBalAmt2' >
							<input type='hidden' name='txtBillJourTotAmt[$i]' id='billjourtotamt[$i]'  value='$txtBillJourTotAmt' >
							<input type='hidden' name='txtBillJourTotAmt1[$i]' id='billjourtotamt1[$i]'  value='$txtBillJourTotAmt1' >
							<input type='hidden' name='txtBillJourTotAmt2[$i]' id='billjourtotamt2[$i]'  value='$txtBillJourTotAmt2' >
							<input type='text' name='txtBillJourNumber[$i]' id='billjournumber[$i]' value='".$rowsdt[3]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onChange=\"SetModRecord('ModRec_G7','$i');\" ".$KeyDown." $status />";
							
							if ($showlist == 1) 
								echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistBill(1,".$i.",'".$PTransDtId."','".$rowsdt[0]."');\" style='vertical-align:bottom;' title='Select Bill'  /></span></label>";
				echo"	</td>";
	
										
				echo"  	<td width='105px;' align='center'><input type='text' name='txtBillJourBank[$i]' id='billjourbank[$i]' value='".$rowsdt[4]."' maxlength='30' class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G7','$i');\" ".$KeyDown."  /></td>";
				echo"   <td width='70px;' align='center'><input type='text' name='txtBillJourValueDate[$i]' id='billjourvaluedate[$i]' value='".$rowsdt[5]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G7','$i');\" ".$KeyDown." $status ></td>";
				echo"   <td width='45px;' align='center'><input type='text' name='txtBillJourCurrCode[$i]' id='billjourcurrcode[$i]' value='".$rowsdt[7]."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount_D[$i]' id='billjouramount[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G7','$i'); SetAmount(8,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount1_D[$i]' id='billjouramount1[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G7','$i'); SetAmount(9,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount2_D[$i]' id='billjouramount2[$i]' value='".$rowsdt[10]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G7','$i'); SetAmount(10,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_B[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitBillJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-1-b', '".$PTransId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_B['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_B[".$i."]','1');LeaveGrid('billjournumber[$i]','linkref[0]','tab1','".$TabIndex."')\" ";		
					
		//Initializations
		
		if(($LedgerActivity==3 && $TransDtdbcd=='D') or ($LedgerActivity==4 && $TransDtdbcd=='C'))
			$txtBillJourType = 'A'; 
		else $txtBillJourType = 'C';
		 
		 
		if (empty($txtBillJourCurrCode))  $txtBillJourCurrCode  = $TransDtCurrCode;
		if (empty($txtBillJourValueDate)) $txtBillJourValueDate = $TransDtValueDate;
		if (empty($txtBillJourLedgerNo))  $txtBillJourLedgerNo  = $LedgerNumber;
		
		$sub_query2 = " Select TransDt.TransDt_ID, ".
					  " Round((ifNull(TransDt_Amount,0)  - Sum(ifNull(Bill_Amount,0))), ".$DecimalBase.")  As BillAmount_D, ".
					  " Round((ifNull(TransDt_Amount1,0) - Sum(ifNull(Bill_Amount1,0))), ".$DecimalBase1.") As BillAmount1_D, ".
					  " Round((ifNull(TransDt_Amount2,0) - Sum(ifNull(Bill_Amount2,0))), ".$DecimalBase2.") As BillAmount2_D, ".
					  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0), ".
					  " (ifNull(TransDt_Amount,0) -  Round(Sum(ifNull(Bill_Amount,0)),9) ) As BillAmount, ".
					  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Bill_Amount1,0)),9) ) As BillAmount1, ".
					  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Bill_Amount2,0)),9) ) As BillAmount2 ".
					  " From Temp_TransDt As TransDt Left Join Temp_BillJourDt on TransDt.TransDt_ID = Temp_BillJourDt.TransDt_ID ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
					  
							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtBillJourBalAmt   = $sub_result2[7];
		$txtBillJourBalAmt1  = $sub_result2[8];
		$txtBillJourBalAmt2  = $sub_result2[9];
			
		$txtBillJourTotAmt   = $sub_result2[4];
		$txtBillJourTotAmt1  = $sub_result2[5];
		$txtBillJourTotAmt2  = $sub_result2[6];
			
		$txtBillJourAmount_D   = $sub_result2[1];
		$txtBillJourAmount1_D  = $sub_result2[2];
		$txtBillJourAmount2_D  = $sub_result2[3];
			
		$txtBillJourAmount   = $sub_result2[7];
		$txtBillJourAmount1  = $sub_result2[8];
		$txtBillJourAmount2  = $sub_result2[9];	
				
		echo"<tr id='CurRec_G7$i' class='$bgcolor $TableFocus' >";
		echo"<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G7');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='105px;' align='left'>
					<input type='hidden' name='txtBillJourId[$i]' id='billjourid[$i]' value='$txtBillJourId' >
					<input type='hidden' name='txtBillJourType[$i]' id='billjourtype[$i]' value='$txtBillJourType' >
					<input type='hidden' name='txtBillJourLedgerNo[$i]' id='billjourledgerno[$i]' value='$txtBillJourLedgerNo' >
					<input type='hidden' name='txtBillJourAmount[$i]'  id='billjouramount_h[$i]'  value='$txtBillJourAmount' >
					<input type='hidden' name='txtBillJourAmount1[$i]' id='billjouramount1_h[$i]' value='$txtBillJourAmount1' >
					<input type='hidden' name='txtBillJourAmount2[$i]' id='billjouramount2_h[$i]' value='$txtBillJourAmount2' >
					<input type='hidden' name='txtBillJourBalAmt[$i]' id='billjourbalamt[$i]'  value='$txtBillJourBalAmt' >
					<input type='hidden' name='txtBillJourBalAmt1[$i]' id='billjourbalamt1[$i]'  value='$txtBillJourBalAmt1' >
					<input type='hidden' name='txtBillJourBalAmt2[$i]' id='billjourbalamt2[$i]'  value='$txtBillJourBalAmt2' >
					<input type='hidden' name='txtBillJourTotAmt[$i]' id='billjourtotamt[$i]'  value='$txtBillJourTotAmt' >
					<input type='hidden' name='txtBillJourTotAmt1[$i]' id='billjourtotamt1[$i]'  value='$txtBillJourTotAmt1' >
					<input type='hidden' name='txtBillJourTotAmt2[$i]' id='billjourtotamt2[$i]'  value='$txtBillJourTotAmt2' >
					<input type='text' name='txtBillJourNumber[$i]' id='billjournumber[$i]' value='$txtBillJourNumber'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;'   onBlur=\"LeaveGrid('billjournumber[$i]','linkref[0]','tab1','".$TabIndex."')\"   onChange=\"SetModRecord('ModRec_G7','$i');\" ".$KeyDown." $status />";
					
					if ($showlist == 1) 
						echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistBill(1,".$i.",'".$PTransDtId."','-1');\" style='vertical-align:bottom;' title='Select Bill'  /></span></label>";
		echo"	</td>";
										
		echo"  	<td width='105px;' align='center'><input type='text' name='txtBillJourBank[$i]' id='billjourbank[$i]' value='$txtBillJourBank' maxlength='30' class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G7','$i');\" ".$KeyDown."  /></td>";
		echo"   <td width='70px;' align='center'><input type='text' name='txtBillJourValueDate[$i]' id='billjourvaluedate[$i]' value='$txtBillJourValueDate' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:65px; height:20px;' onChange=\"SetModRecord('ModRec_G7','$i');\" ".$KeyDown." $status /></td>";
		echo"   <td width='45px;' align='center'><input type='text' name='txtBillJourCurrCode[$i]' id='billjourcurrcode[$i]' value='$txtBillJourCurrCode' maxlength='10' class='".$ClassI."' style='font-size:12px; width:40px; height:20px;' readonly ></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount_D[$i]' id='billjouramount[$i]' value='$txtBillJourAmount_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G7','$i'); SetAmount(8,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrCode."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount1_D[$i]' id='billjouramount1[$i]' value='$txtBillJourAmount1_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G7','$i'); SetAmount(9,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrCode."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount2_D[$i]' id='billjouramount2[$i]' value='$txtBillJourAmount2_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G7','$i'); SetAmount(10,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrCode."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_B[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitBillJour('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-1-b', '".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex','$down'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}

function ShowTransDtGridBills2($connection,$form,$PTransId,$PTransDtId,$arrayparams,$arrayaccess){
	
	$Module_Project = $arrayparams['Module_Project'];
		$Module_CostCenter = $arrayparams['Module_CostCenter'];
		$Module_RestrictProjCost = $arrayparams['Module_RestrictProjCost'];
		$Module_RestrictCost = $arrayparams['Module_RestrictCost'];
		$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
		$Module_ShowBalance = $arrayparams['Module_ShowBalance'];
		$ModuleRestrictCashType = $arrayparams['ModuleRestrictCashType'];
		$Module_MultiCashType = $arrayparams['Module_MultiCashType'];
		$ModuleInvoiceGrid = $arrayparams['Module_InvoiceGridPV'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
		$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
		
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate = $arrayparams['txtDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	if($ModuleInvoiceGrid==1) {
		/// grid Motion
		$Motion = 1;
		$NextCell = 'linkref2[0]';
		$down = 0;
		
		
		if ($Module_TransStandardView==1 && ($Module_Project == 1 || $Module_CostCenter == 1 || $Module_ProjectDt == 1 || $Module_CostCenterDt == 1) ){
			$TabIndex = 3 ;		
			}
		else 	
		$TabIndex = 2 ;
		
	}
	else {
		$Motion = 1;
		$down = 0;
		$NextCell = 'linkref2[0]';
		$TabIndex = 3 ;		
	}
	/////////////
	
	$columnsarray = array("billjournumber2", "billjourbank2", "billjourvaluedate2", "billjouramount02", "billjouramount21", "billjouramount22");
	$columnsarrayall = array("billjournumber2", "billjourbank2", "billjourvaluedate2", "billjouramount02", "billjouramount21", "billjouramount22", "billjouramount02_h", "billjouramount21_h", "billjouramount22_h", 
							 "billjourledgerno2", "billjourcurrcode2", "billjourid2", "billjourtype2", "billjourbalamt02", "billjourbalamt21", "billjourbalamt22", "billjourtotamt02", "billjourtotamt21", "billjourtotamt22");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
		
		
	if(($LedgerActivity==3 && $TransDtdbcd=='D') or ($LedgerActivity==4 && $TransDtdbcd=='C')) {
		$showlist = 0;
		$status = '';	
	}	
	else {
		$showlist = 1;
		$status = 'readonly';	
	}	
		

	//Initializations for Div Ids
	$ContainerDiv = 'container8';
	$LoaderDiv    = 'Loader8';
	$HeaderDiv    = 'grid_headerDiv8';
	$ContentDiv   = 'grid_contentDiv8';
	$TableFocus   = 'table-focus-g8';	
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='105px;' align='center'><span>Bill No</span></th>";
		echo"  <th width='105px;' align='left'><span>Bank</span></th>";
		echo"  <th width='70px;' align='center'><span>Value Date</span></th>";
		echo"  <th width='45px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='95px;' align='center'><span>Amount</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base1</span></th>";
		echo"  <th width='95px;' align='center'><span>$Base2</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:635px;'>";
		echo"<input type='hidden' name='SelectedRec_G87' id='SelectedRec_G8' value='$SelectedRec_G8'>
			 <input type='hidden' name='SelectedRecColor_G8' id='SelectedRecColor_G8' value='$SelectedRecColor_G8'>
			 <input type='hidden' name='ModRec_G8' id='ModRec_G8' value='$ModRec_G8'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, ".
						  " (ifNull(TransDt_Amount,0)  - Round(Sum(ifNull(Bill_Amount,0)),9) ) As BillAmount, ".
						  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Bill_Amount1,0)),9) ) As BillAmount1, ".
						  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Bill_Amount2,0)),9) ) As BillAmount2, ".
						  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0) ".
						  " From Temp_TransDt As TransDt Left Join Temp_BillJourDt on TransDt.TransDt_ID = Temp_BillJourDt.TransDt_ID ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
						  
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtBillJourBalAmt02_M  = $sub_result2[1];
			$txtBillJourBalAmt21_M  = $sub_result2[2];
			$txtBillJourBalAmt22_M  = $sub_result2[3];
			
			$txtBillJourTotAmt02  = $sub_result2[4];
			$txtBillJourTotAmt21  = $sub_result2[5];
			$txtBillJourTotAmt22  = $sub_result2[6];
			
		 
			$query = " SELECT BillJourDT_ID, TransDt_ID, Ledger_Number, Bill_Number, Bill_Bank, date_format(Bill_ValueDate,'%d/%m/%Y'), Bill_Type, BillJourDt.Currency_Code, ".
					 " Round(ifNull(Bill_Amount,0), ".$DecimalBase."), Round(ifNull(Bill_Amount1,0), ".$DecimalBase1."), Round(ifNull(Bill_Amount2,0), ".$DecimalBase2."), ".
					 " ifNull(Bill_Amount,0), ifNull(Bill_Amount1,0), ifNull(Bill_Amount2,0) ".
					 " FROM Temp_BillJourDt As BillJourDt ".
					 " WHERE BillJourDt.TransDt_ID = '".$PTransDtId."'";
				
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtBillJourBalAmt02  = $txtBillJourBalAmt02_M + $rowsdt[11];
				$txtBillJourBalAmt21  = $txtBillJourBalAmt21_M + $rowsdt[12];
				$txtBillJourBalAmt22  = $txtBillJourBalAmt22_M + $rowsdt[13];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_B['.$i.']\')"';
				
				echo"<tr id='CurRec_G8$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteBillJour('".$filepath."','".$form."','DEL','datagrid-2-b','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='105px;' align='left'>
							<input type='hidden' name='txtBillJourId2[$i]' id='billjourid2[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtBillJourType2[$i]' id='billjourtype2[$i]' value='".$rowsdt[6]."' >
							<input type='hidden' name='txtBillJourLedgerNo2[$i]' id='billjourledgerno2[$i]' value='".$rowsdt[2]."' >
							<input type='hidden' name='txtBillJourAmount02[$i]'  id='billjouramount02_h[$i]'  value='".$rowsdt[11]."' >
							<input type='hidden' name='txtBillJourAmount21[$i]' id='billjouramount21_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtBillJourAmount22[$i]' id='billjouramount22_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtBillJourBalAmt02[$i]' id='billjourbalamt02[$i]'  value='$txtBillJourBalAmt02' >
							<input type='hidden' name='txtBillJourBalAmt21[$i]' id='billjourbalamt21[$i]'  value='$txtBillJourBalAmt21' >
							<input type='hidden' name='txtBillJourBalAmt22[$i]' id='billjourbalamt22[$i]'  value='$txtBillJourBalAmt22' >
							<input type='hidden' name='txtBillJourTotAmt02[$i]' id='billjourtotamt02[$i]'  value='$txtBillJourTotAmt02' >
							<input type='hidden' name='txtBillJourTotAmt21[$i]' id='billjourtotamt21[$i]'  value='$txtBillJourTotAmt21' >
							<input type='hidden' name='txtBillJourTotAmt22[$i]' id='billjourtotamt22[$i]'  value='$txtBillJourTotAmt22' >
							<input type='text' name='txtBillJourNumber2[$i]' id='billjournumber2[$i]' value='".$rowsdt[3]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onChange=\"SetModRecord('ModRec_G8','$i');\" ".$KeyDown." $status />";
							
							if ($showlist == 1) 
								echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistBill(2,".$i.",'".$PTransDtId."','".$rowsdt[0]."');\" style='vertical-align:bottom;' title='Select Bill'  /></span></label>";
				echo"	</td>";
	
										
				echo"  	<td width='105px;' align='center'><input type='text' name='txtBillJourBank2[$i]' id='billjourbank2[$i]' value='".$rowsdt[4]."' maxlength='30' class='".$ClassE."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G8','$i');\" ".$KeyDown."  /></td>";
				echo"   <td width='70px;' align='center'><input type='text' name='txtBillJourValueDate2[$i]' id='billjourvaluedate2[$i]' value='".$rowsdt[5]."' maxlength='10' class='".$ClassE." date' style='font-size:12px; width:80px; height:20px;' onChange=\"SetModRecord('ModRec_G8','$i');\" ".$KeyDown." $status ></td>";
				echo"   <td width='45px;' align='center'><input type='text' name='txtBillJourCurrCode2[$i]' id='billjourcurrcode2[$i]' value='".$rowsdt[7]."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount02_D[$i]' id='billjouramount02[$i]' value='".$rowsdt[8]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G8','$i'); SetAmount(18,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount21_D[$i]' id='billjouramount21[$i]' value='".$rowsdt[9]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G8','$i'); SetAmount(19,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount22_D[$i]' id='billjouramount22[$i]' value='".$rowsdt[10]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G8','$i'); SetAmount(20,$DecimalBase1,$DecimalBase2,'".$rowsdt[7]."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_B[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitBillJour2('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-2-b', '".$PTransId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_B['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_2_B[".$i."]','1');LeaveGrid('billjournumber2[$i]','linkref2[0]','tab3','".$TabIndex."')\" ";			
		//Initializations
		
		if(($LedgerActivity==3 && $TransDtdbcd=='D') or ($LedgerActivity==4 && $TransDtdbcd=='C'))
			$txtBillJourType2 = 'A'; 
		else $txtBillJourType2 = 'C';
		 
		 
		if (empty($txtBillJourCurrCode2))  $txtBillJourCurrCode2  = $TransDtCurrCode;
		if (empty($txtBillJourValueDate2)) $txtBillJourValueDate2 = $TransDtValueDate;
		if (empty($txtBillJourLedgerNo2))  $txtBillJourLedgerNo2  = $LedgerNumber;
		
		$sub_query2 = " Select TransDt.TransDt_ID, ".
					  " Round((ifNull(TransDt_Amount,0)  - Sum(ifNull(Bill_Amount,0))), ".$DecimalBase.")  As BillAmount_D, ".
					  " Round((ifNull(TransDt_Amount1,0) - Sum(ifNull(Bill_Amount1,0))), ".$DecimalBase1.") As BillAmount1_D, ".
					  " Round((ifNull(TransDt_Amount2,0) - Sum(ifNull(Bill_Amount2,0))), ".$DecimalBase2.") As BillAmount2_D, ".
					  " ifNull(TransDt_Amount,0), ifNull(TransDt_Amount1,0), ifNull(TransDt_Amount2,0), ".
					  " (ifNull(TransDt_Amount,0) -  Round(Sum(ifNull(Bill_Amount,0)),9) ) As BillAmount, ".
					  " (ifNull(TransDt_Amount1,0) - Round(Sum(ifNull(Bill_Amount1,0)),9) ) As BillAmount1, ".
					  " (ifNull(TransDt_Amount2,0) - Round(Sum(ifNull(Bill_Amount2,0)),9) ) As BillAmount2 ".
					  " From Temp_TransDt As TransDt Left Join Temp_BillJourDt on TransDt.TransDt_ID = Temp_BillJourDt.TransDt_ID ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID, TransDt_Amount, TransDt_Amount1, TransDt_Amount2 ";
					  
							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtBillJourBalAmt02  = $sub_result2[7];
		$txtBillJourBalAmt21  = $sub_result2[8];
		$txtBillJourBalAmt22  = $sub_result2[9];
			
		$txtBillJourTotAmt02  = $sub_result2[4];
		$txtBillJourTotAmt21  = $sub_result2[5];
		$txtBillJourTotAmt22  = $sub_result2[6];
			
		$txtBillJourAmount02_D  = $sub_result2[1];
		$txtBillJourAmount21_D  = $sub_result2[2];
		$txtBillJourAmount22_D  = $sub_result2[3];
			
		$txtBillJourAmount02  = $sub_result2[7];
		$txtBillJourAmount21  = $sub_result2[8];
		$txtBillJourAmount22  = $sub_result2[9];	
				
		echo"<tr id='CurRec_G8$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G8');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='105px;' align='left'>
					<input type='hidden' name='txtBillJourId2[$i]' id='billjourid2[$i]' value='$txtBillJourId2' >
					<input type='hidden' name='txtBillJourType2[$i]' id='billjourtype2[$i]' value='$txtBillJourType2' >
					<input type='hidden' name='txtBillJourLedgerNo2[$i]' id='billjourledgerno2[$i]' value='$txtBillJourLedgerNo2' >
					<input type='hidden' name='txtBillJourAmount02[$i]'  id='billjouramount02_h[$i]'  value='$txtBillJourAmount02' >
					<input type='hidden' name='txtBillJourAmount21[$i]' id='billjouramount21_h[$i]' value='$txtBillJourAmount21' >
					<input type='hidden' name='txtBillJourAmount22[$i]' id='billjouramount22_h[$i]' value='$txtBillJourAmount22' >
					<input type='hidden' name='txtBillJourBalAmt02[$i]' id='billjourbalamt02[$i]'  value='$txtBillJourBalAmt02' >
					<input type='hidden' name='txtBillJourBalAmt21[$i]' id='billjourbalamt21[$i]'  value='$txtBillJourBalAmt21' >
					<input type='hidden' name='txtBillJourBalAmt22[$i]' id='billjourbalamt22[$i]'  value='$txtBillJourBalAmt22' >
					<input type='hidden' name='txtBillJourTotAmt02[$i]' id='billjourtotamt02[$i]'  value='$txtBillJourTotAmt02' >
					<input type='hidden' name='txtBillJourTotAmt21[$i]' id='billjourtotamt12[$i]'  value='$txtBillJourTotAmt21' >
					<input type='hidden' name='txtBillJourTotAmt22[$i]' id='billjourtotamt22[$i]'  value='$txtBillJourTotAmt22' >
					<input type='text' name='txtBillJourNumber2[$i]' id='billjournumber2[$i]' value='$txtBillJourNumber2'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onBlur=\"LeaveGrid('billjournumber2[$i]','linkref2[0]','tab3','".$TabIndex."')\"  onChange=\"SetModRecord('ModRec_G8','$i');\" ".$KeyDown1." $status />";
					
					if ($showlist == 1) 
						echo"<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistBill(2,".$i.",'".$PTransDtId."','-1');\" style='vertical-align:bottom;' title='Select Bill'  /></span></label>";
		echo"	</td>";
										
		echo"  	<td width='105px;' align='center'><input type='text' name='txtBillJourBank2[$i]' id='billjourbank2[$i]' value='$txtBillJourBank2' maxlength='30' class='".$ClassI."' style='font-size:12px; width:100px; height:20px;' onChange=\"SetModRecord('ModRec_G8','$i');\" ".$KeyDown."  /></td>";
		echo"   <td width='70px;' align='center'><input type='text' name='txtBillJourValueDate2[$i]' id='billjourvaluedate2[$i]' value='$txtBillJourValueDate2' maxlength='10' class='".$ClassI." date' style='font-size:12px; width:65px; height:20px;' onChange=\"SetModRecord('ModRec_G8','$i');\" ".$KeyDown." $status /></td>";
		echo"   <td width='45px;' align='center'><input type='text' name='txtBillJourCurrCode2[$i]' id='billjourcurrcode2[$i]' value='$txtBillJourCurrCode2' maxlength='10' class='".$ClassI."' style='font-size:12px; width:40px; height:20px;' readonly ></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount02_D[$i]' id='billjouramount02[$i]' value='$txtBillJourAmount02_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G8','$i'); SetAmount(18,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrCode2."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount21_D[$i]' id='billjouramount21[$i]' value='$txtBillJourAmount21_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G8','$i'); SetAmount(19,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrCode2."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtBillJourAmount22_D[$i]' id='billjouramount22[$i]' value='$txtBillJourAmount22_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onChange=\"SetModRecord('ModRec_G8','$i'); SetAmount(20,$DecimalBase1,$DecimalBase2,'".$txtBillJourCurrCode2."','".$Base1."','".$Base2."','".$txtDate."',$i);\" ".$KeyDown." /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_B[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitBillJour2('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-1-b', '".$PTransId."', '".$PTransDtId."','$NextCell','$Motion','$TabIndex','$down'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}

function ShowTransDtGridInvoices($connection,$form,$PTransId,$PTransDtId,$arrayparams,$arrayaccess){
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate   = $arrayparams['txtDate'];
	$TransType = $arrayparams['TransType'];
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	$columnsarray = array("linkref", "acclinkpaidamt");
	$columnsarrayall = array("linkref", "linkid", "acclinknettopay", "acclinkpaidamt", "acclinkremainamt", "acclinkcurrcode", "acclinkid", "linktype", "acclinknettotamt_h", "acclinknettotamt1_h", "acclinknettotamt2_h", 
							 "acclinknettopay_h", "acclinknettopay1_h", "acclinknettopay2_h", "acclinkpaidamt_h", "acclinkpaidamt1_h", "acclinkpaidamt2_h", "acclinkremainamt_h", "acclinkremainamt1_h", "acclinkremainamt2_h", 
							 "acclinkbalamt", "acclinkbalamt1", "acclinkbalamt2", "acclinktotamt", "acclinktotamt1", "acclinktotamt2");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
		
		
	$vMainCompanyName=explode('_',$_SESSION['dbName']);  
	$DirCompany  = $vMainCompanyName[0];
	$DirYear	 = $vMainCompanyName[1]; 

	//Initializations for Div Ids
	$ContainerDiv = 'container9';
	$LoaderDiv    = 'Loader9';
	$HeaderDiv    = 'grid_headerDiv9';
	$ContentDiv   = 'grid_contentDiv9';
	$TableFocus   = 'table-focus-g9';	
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:475px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='105px;' align='center'><span>Inv Ref.</span></th>";
		echo"  <th width='50px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='95px;' align='center'><span>Net To Pay</span></th>";
		echo"  <th width='95px;' align='center'><span>Paid Amt</span></th>";
		echo"  <th width='95px;' align='center'><span>Remain Amt</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:475px;'>";
		echo"<input type='hidden' name='SelectedRec_G9' id='SelectedRec_G9' value='$SelectedRec_G9'>
			 <input type='hidden' name='SelectedRecColor_G9' id='SelectedRecColor_G9' value='$SelectedRecColor_G9'>
			 <input type='hidden' name='ModRec_G9' id='ModRec_G9' value='$ModRec_G9'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, ".
						  " (ifNull(Transdt_Amount,0) - Round(Sum(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0)),9) ) As PaidAmnt, ".
						  " (ifNull(Transdt_Amount1,0) - Round(Sum(ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0)),9) ) As PaidAmnt1, ".
						  " (ifNull(Transdt_Amount2,0) - Round(Sum(ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0)),9) ) As PaidAmnt2, ".
						  " ifNull(Transdt_Amount,0), ifNull(Transdt_Amount1,0), ifNull(Transdt_Amount2,0) ".
						  " From Temp_TransDt As TransDt Left Join ".$_SESSION['dbLabel'].".Temp_AccLink on TransDt.TransDt_ID = Temp_AccLink.Journal_ID ".
						  " And Temp_AccLink.Journal_Type = '".$TransType."' ".
						  " And upper(Temp_AccLink.CompanyName) = upper('".$DirCompany."') ".
						  " And upper(Temp_AccLink.DirYear) = upper('".$DirYear."') ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID, Transdt_Amount, Transdt_Amount1, Transdt_Amount2 ";
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtAccLinkBalAmt_M   = $sub_result2[1];
			$txtAccLinkBalAmt1_M  = $sub_result2[2];
			$txtAccLinkBalAmt2_M  = $sub_result2[3];
			
			$txtAccLinkTotAmt   = $sub_result2[4];
			$txtAccLinkTotAmt1  = $sub_result2[5];
			$txtAccLinkTotAmt2  = $sub_result2[6];
			
		 
			$query = " Select Acc_Id, Link_Ref, Link_Type, Round(ifNull(Sale_NetTotal,0), ".$DecimalBase."), Round(ifNull(Sale_NetToPay,0), ".$DecimalBase."), ".
					 " Round(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0), ".$DecimalBase."), Round(ifNull(Sale_RemainAmnt,0), ".$DecimalBase."), ".
					 " ifNull(Sale_NetTotal,0), ifNull(Sale_NetTotalBase1,0), ifNull(Sale_NetTotalBase2,0), ".
					 " ifNull(Sale_NetToPay,0), ifNull(Sale_NetToPayBase1,0), ifNull(Sale_NetToPayBase2,0), ".
					 " ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0), ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0), ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0), ".
					 " ifNull(Sale_RemainAmnt,0), ifNull(Sale_RemainAmntBase1,0), ifNull(Sale_RemainAmntBase2,0), Link_ID ".
					 " From ".$_SESSION['dbLabel'].".Temp_AccLink As AccLink ".
					 " Where AccLink.Journal_Id = '".$PTransDtId."' ".
					 " And AccLink.Journal_Type = '".$TransType."' ".
					 " And upper(AccLink.CompanyName) = upper('".$DirCompany."') ".
					 " And upper(AccLink.DirYear) = upper('".$DirYear."') ";
			
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtAccLinkBalAmt   = $txtAccLinkBalAmt_M  + $rowsdt[13];
				$txtAccLinkBalAmt1  = $txtAccLinkBalAmt1_M + $rowsdt[14];
				$txtAccLinkBalAmt2  = $txtAccLinkBalAmt2_M + $rowsdt[15];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_I['.$i.']\')"';
				
				echo"<tr id='CurRec_G9$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteAccLink('".$filepath."','".$form."','DEL','datagrid-1-i','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='105px;' align='left'>
							<input type='hidden' name='txtAccLinkId[$i]' id='acclinkid[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtLinkID[$i]'  id='linkid[$i]'  value='".$rowsdt[19]."' >
							<input type='hidden' name='txtLinkType[$i]'  id='linktype[$i]'  value='".$rowsdt[2]."' >
							<input type='hidden' name='txtAccLinkNetTotAmt[$i]'  id='acclinknettotamt_h[$i]'  value='".$rowsdt[7]."' >
							<input type='hidden' name='txtAccLinkNetTotAmt1[$i]' id='acclinknettotamt1_h[$i]'  value='".$rowsdt[8]."' >
							<input type='hidden' name='txtAccLinkNetTotAmt2[$i]' id='acclinknettotamt2_h[$i]'  value='".$rowsdt[9]."' >
							<input type='hidden' name='txtAccLinkNetToPay[$i]'  id='acclinknettopay_h[$i]'  value='".$rowsdt[10]."' >
							<input type='hidden' name='txtAccLinkNetToPay1[$i]' id='acclinknettopay1_h[$i]' value='".$rowsdt[11]."' >
							<input type='hidden' name='txtAccLinkNetToPay2[$i]' id='acclinknettopay2_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtAccLinkPaidAmt[$i]'  id='acclinkpaidamt_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtAccLinkPaidAmt1[$i]' id='acclinkpaidamt1_h[$i]' value='".$rowsdt[14]."' >
							<input type='hidden' name='txtAccLinkPaidAmt2[$i]' id='acclinkpaidamt2_h[$i]' value='".$rowsdt[15]."' >
							<input type='hidden' name='txtAccLinkRemainAmt[$i]'  id='acclinkremainamt_h[$i]' value='".$rowsdt[16]."' >
							<input type='hidden' name='txtAccLinkRemainAmt1[$i]' id='acclinkremainamt1_h[$i]' value='".$rowsdt[17]."' >
							<input type='hidden' name='txtAccLinkRemainAmt2[$i]' id='acclinkremainamt2_h[$i]' value='".$rowsdt[18]."' >
							<input type='hidden' name='txtAccLinkBalAmt[$i]' id='acclinkbalamt[$i]'  value='$txtAccLinkBalAmt' >
							<input type='hidden' name='txtAccLinkBalAmt1[$i]' id='acclinkbalamt1[$i]'  value='$txtAccLinkBalAmt1' >
							<input type='hidden' name='txtAccLinkBalAmt2[$i]' id='acclinkbalamt2[$i]'  value='$txtAccLinkBalAmt2' >
							<input type='hidden' name='txtAccLinkTotAmt[$i]' id='acclinktotamt[$i]'  value='$txtAccLinkTotAmt' >
							<input type='hidden' name='txtAccLinkTotAmt1[$i]' id='acclinktotamt1[$i]'  value='$txtAccLinkTotAmt1' >
							<input type='hidden' name='txtAccLinkTotAmt2[$i]' id='acclinktotamt2[$i]'  value='$txtAccLinkTotAmt2' >
							<input type='text' name='txtLinkRef[$i]' id='linkref[$i]' value='".$rowsdt[1]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onChange=\"SetModRecord('ModRec_G9','$i');\" ".$KeyDown." />";
				echo"		<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistInvoices(1,".$i.",'".$PTransDtId."');\" style='vertical-align:bottom;' title='Select Invoice'  /></span></label>";
				echo"	</td>";
	
										
				echo"   <td width='50px;' align='center'><input type='text' name='txtAccLinkCurrCode[$i]' id='acclinkcurrcode[$i]' value='".$TransDtCurrCode."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkNetToPay_D[$i]' id='acclinknettopay[$i]' value='".$rowsdt[4]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkPaidAmt_D[$i]' id='acclinkpaidamt[$i]' value='".$rowsdt[5]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onKeyup='CalcRemainAmount(1,$i);' onChange=\"SetModRecord('ModRec_G9','$i'); \" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkRemainAmt_D[$i]' id='acclinkremainamt[$i]' value='".$rowsdt[6]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_I[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitAccLink('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-1-i', '".$PTransId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_I['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_I[".$i."]','1');LeaveToMainGrid('linkref[$i]','ledgernumber','RecIndex_G1','')\" ";			
		
		$sub_query2 = " Select TransDt.TransDt_ID, ".
					  " Round((ifNull(Transdt_Amount,0) - Sum(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0))), ".$DecimalBase1.")  As PaidAmnt_D, ".
					  " Round((ifNull(Transdt_Amount1,0) - Sum(ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0))), ".$DecimalBase1.") As PaidAmnt1_D, ".
					  " Round((ifNull(Transdt_Amount2,0) - Sum(ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0))), ".$DecimalBase1.") As PaidAmnt2_D, ".
					  " ifNull(Transdt_Amount,0), ifNull(Transdt_Amount1,0), ifNull(Transdt_Amount2,0), ".
					  " (ifNull(Transdt_Amount,0) - Round(Sum(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0)),9) ) As PaidAmnt, ".
					  " (ifNull(Transdt_Amount1,0) - Round(Sum(ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0)),9) ) As PaidAmnt1, ".
					  " (ifNull(Transdt_Amount2,0) - Round(Sum(ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0)),9) ) As PaidAmnt2 ".
					  " From Temp_TransDt As TransDt Left Join ".$_SESSION['dbLabel'].".Temp_AccLink on TransDt.TransDt_ID = Temp_AccLink.Journal_ID ".
					  " And Temp_AccLink.Journal_Type = '".$TransType."' ".
					  " And upper(Temp_AccLink.CompanyName) = upper('".$DirCompany."') ".
					  " And upper(Temp_AccLink.DirYear) = upper('".$DirYear."') ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID, Transdt_Amount, Transdt_Amount1, Transdt_Amount2 ";

							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtAccLinkBalAmt   = $sub_result2[7];
		$txtAccLinkBalAmt1  = $sub_result2[8];
		$txtAccLinkBalAmt2  = $sub_result2[9];
			
		$txtAccLinkTotAmt   = $sub_result2[4];
		$txtAccLinkTotAmt1  = $sub_result2[5];
		$txtAccLinkTotAmt2  = $sub_result2[6];
			
		$txtAccLinkPaidAmt_D = $sub_result2[1];
	
		$txtAccLinkPaidAmt   = $sub_result2[7];
		$txtAccLinkPaidAmt1  = $sub_result2[8];
		$txtAccLinkPaidAmt2  = $sub_result2[9];
				
		echo"<tr id='CurRec_G9$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G9');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='105px;' align='left'>
					<input type='hidden' name='txtAccLinkId[$i]' id='acclinkid[$i]' value='$txtAccLinkId' >
					<input type='hidden' name='txtLinkID[$i]'  id='linkid[$i]'  value='$txtLinkID' >
					<input type='hidden' name='txtLinkType[$i]'  id='linktype[$i]'  value='$txtLinkType' >
					<input type='hidden' name='txtAccLinkNetTotAmt[$i]'  id='acclinknettotamt_h[$i]'  value='$txtAccLinkNetTotAmt' >
					<input type='hidden' name='txtAccLinkNetTotAmt1[$i]' id='acclinknettotamt1_h[$i]'  value='$txtAccLinkNetTotAmt1' >
					<input type='hidden' name='txtAccLinkNetTotAmt2[$i]' id='acclinknettotamt2_h[$i]'  value='$txtAccLinkNetTotAmt2' >
					<input type='hidden' name='txtAccLinkNetToPay[$i]'  id='acclinknettopay_h[$i]'  value='$txtAccLinkNetToPay' >
					<input type='hidden' name='txtAccLinkNetToPay1[$i]' id='acclinknettopay1_h[$i]' value='$txtAccLinkNetToPay1' >
					<input type='hidden' name='txtAccLinkNetToPay2[$i]' id='acclinknettopay2_h[$i]' value='$txtAccLinkNetToPay2' >
					<input type='hidden' name='txtAccLinkPaidAmt[$i]'  id='acclinkpaidamt_h[$i]' value='$txtAccLinkPaidAmt' >
					<input type='hidden' name='txtAccLinkPaidAmt1[$i]' id='acclinkpaidamt1_h[$i]' value='$txtAccLinkPaidAmt1' >
					<input type='hidden' name='txtAccLinkPaidAmt2[$i]' id='acclinkpaidamt2_h[$i]' value='$txtAccLinkPaidAmt2' >
					<input type='hidden' name='txtAccLinkRemainAmt[$i]'  id='acclinkremainamt_h[$i]' value='$txtAccLinkRemainAmt' >
					<input type='hidden' name='txtAccLinkRemainAmt1[$i]' id='acclinkremainamt1_h[$i]' value='$txtAccLinkRemainAmt1' >
					<input type='hidden' name='txtAccLinkRemainAmt2[$i]' id='acclinkremainamt2_h[$i]' value='$txtAccLinkRemainAmt2' >
					<input type='hidden' name='txtAccLinkBalAmt[$i]' id='acclinkbalamt[$i]'  value='$txtAccLinkBalAmt' >
					<input type='hidden' name='txtAccLinkBalAmt1[$i]' id='acclinkbalamt1[$i]'  value='$txtAccLinkBalAmt1' >
					<input type='hidden' name='txtAccLinkBalAmt2[$i]' id='acclinkbalamt2[$i]'  value='$txtAccLinkBalAmt2' >
					<input type='hidden' name='txtAccLinkTotAmt[$i]' id='acclinktotamt[$i]'  value='$txtAccLinkTotAmt' >
					<input type='hidden' name='txtAccLinkTotAmt1[$i]' id='acclinktotamt1[$i]'  value='$txtAccLinkTotAmt1' >
					<input type='hidden' name='txtAccLinkTotAmt2[$i]' id='acclinktotamt2[$i]'  value='$txtAccLinkTotAmt2' >
					<input type='text' name='txtLinkRef[$i]' id='linkref[$i]' value='$txtLinkRef'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onBlur=\"LeaveToMainGrid('linkref[$i]','ledgernumber','RecIndex_G1','')\"   onChange=\"SetModRecord('ModRec_G9','$i');\" ".$KeyDown1." />";
		echo"		<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistInvoices(1,".$i.",'".$PTransDtId."');\" style='vertical-align:bottom;' title='Select Invoice'  /></span></label>";			
		echo"	</td>";
										
		echo"   <td width='50px;' align='center'><input type='text' name='txtAccLinkCurrCode[$i]' id='acclinkcurrcode[$i]' value='".$TransDtCurrCode."' maxlength='10' class='".$ClassI."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkNetToPay_D[$i]' id='acclinknettopay[$i]' value='$txtAccLinkNetToPay_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkPaidAmt_D[$i]' id='acclinkpaidamt[$i]' value='$txtAccLinkPaidAmt_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onKeyup='CalcRemainAmount(1,$i);' onChange=\"SetModRecord('ModRec_G9','$i'); \" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkRemainAmt_D[$i]' id='acclinkremainamt[$i]' value='$txtAccLinkRemainAmt_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_I[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitAccLink('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-1-i', '".$PTransId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}

function ShowTransDtGridInvoices2($connection,$form,$PTransId,$PTransDtId,$arrayparams,$arrayaccess){
	
	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$DefaultCur   = $arrayparams['DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$txtDate   = $arrayparams['txtDate'];
	$TransType = $arrayparams['TransType'];
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
	
	
	$columnsarray = array("linkref2", "acclinkpaidamt20");
	$columnsarrayall = array("linkref2", "linkid2", "acclinknettopay20", "acclinkpaidamt20", "acclinkremainamt20", "acclinkcurrcode2", "acclinkid2", "linktype2", "acclinknettotamt20_h", "acclinknettotamt21_h", "acclinknettotamt22_h", 
							 "acclinknettopay20_h", "acclinknettopay21_h", "acclinknettopay22_h", "acclinkpaidamt20_h", "acclinkpaidamt21_h", "acclinkpaidamt22_h", "acclinkremainamt20_h", "acclinkremainamt21_h", "acclinkremainamt22_h", 
							 "acclinkbalamt20", "acclinkbalamt21", "acclinkbalamt22", "acclinktotamt20", "acclinktotamt21", "acclinktotamt22");
	$columnslist = implode(',', $columnsarray);
	$columnslistall = implode(',', $columnsarrayall);
		

	//Select Data From TransDt For Selected Record
	$sub_query = " Select TransDt.Ledger_Number, Ledger.Ledger_Name, ifNull(Ledger.Ledger_Activity,0), ".
				 " TransDt.Currency_Code, Date_Format(TransDt_ValueDate,'%d/%m/%Y') As TransDt_ValueDate, Transdt_dbcd ".
				 " From Temp_TransDt As TransDt ".
				 " Inner Join Ledger on TransDt.Ledger_Number = Ledger.Ledger_Number ".
				 " Where TransDt.TransDt_ID = '".$PTransDtId."' ";
							  
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);

	$LedgerNumber     = $sub_result[0];
	$LedgerName       = $sub_result[1];
	$LedgerActivity   = $sub_result[2];
	$TransDtCurrCode  = $sub_result[3];
	$TransDtValueDate = $sub_result[4];
	$TransDtdbcd      = $sub_result[5];
		
		
	$vMainCompanyName=explode('_',$_SESSION['dbName']);  
	$DirCompany  = $vMainCompanyName[0];
	$DirYear	 = $vMainCompanyName[1]; 

	//Initializations for Div Ids
	$ContainerDiv = 'container10';
	$LoaderDiv    = 'Loader10';
	$HeaderDiv    = 'grid_headerDiv10';
	$ContentDiv   = 'grid_contentDiv10';
	$TableFocus   = 'table-focus-g10';	
	
	echo"<div id='$ContainerDiv' style='position:relative; z-index:1;' >";
	echo"	<div id='$LoaderDiv' class=''><div></div></div>";
	echo" 	<div id='$HeaderDiv' class='grid_headerDiv' style='width:263px;'>";
	echo"  	<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:475px;'>";
	
		echo"<tr height='25px;'>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='105px;' align='center'><span>Inv Ref.</span></th>";
		echo"  <th width='50px;' align='center'><span>Curr.</span></th>";
		echo"  <th width='95px;' align='center'><span>Net To Pay</span></th>";
		echo"  <th width='95px;' align='center'><span>Paid Amt</span></th>";
		echo"  <th width='95px;' align='center'><span>Remain Amt</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"  <th width='25px;' align='center'><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='$ContentDiv' class='grid_contentDiv' onscroll=\"ScrollHeader('$HeaderDiv','$ContentDiv')\" style='width:280px;height:135px;'>";
		echo"<table border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:475px;'>";
		echo"<input type='hidden' name='SelectedRec_G10' id='SelectedRec_G10' value='$SelectedRec_G10'>
			 <input type='hidden' name='SelectedRecColor_G10' id='SelectedRecColor_G10' value='$SelectedRecColor_G10'>
			 <input type='hidden' name='ModRec_G10' id='ModRec_G10' value='$ModRec_G10'>";  
			 
			 
		if ($PTransId != '' && $PTransDtId != '') {

			$sub_query2 = " Select TransDt.TransDt_ID, ".
						  " (ifNull(Transdt_Amount,0) - Round(Sum(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0)),9) ) As PaidAmnt, ".
						  " (ifNull(Transdt_Amount1,0) - Round(Sum(ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0)),9) ) As PaidAmnt1, ".
						  " (ifNull(Transdt_Amount2,0) - Round(Sum(ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0)),9) ) As PaidAmnt2, ".
						  " ifNull(Transdt_Amount,0), ifNull(Transdt_Amount1,0), ifNull(Transdt_Amount2,0) ".
						  " From Temp_TransDt As TransDt Left Join ".$_SESSION['dbLabel'].".Temp_AccLink on TransDt.TransDt_ID = Temp_AccLink.Journal_ID ".
						  " And Temp_AccLink.Journal_Type = '".$TransType."' ".
						  " And upper(Temp_AccLink.CompanyName) = upper('".$DirCompany."') ".
						  " And upper(Temp_AccLink.DirYear) = upper('".$DirYear."') ".
						  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
						  " Group By TransDt.TransDt_ID, Transdt_Amount, Transdt_Amount1, Transdt_Amount2 ";
								  
			//echo $sub_query2;
			$sub_result2 = selectData($connection, $sub_query2);
			
			$txtAccLinkBalAmt20_M  = $sub_result2[1];
			$txtAccLinkBalAmt21_M  = $sub_result2[2];
			$txtAccLinkBalAmt22_M  = $sub_result2[3];
			
			$txtAccLinkTotAmt20  = $sub_result2[4];
			$txtAccLinkTotAmt21  = $sub_result2[5];
			$txtAccLinkTotAmt22  = $sub_result2[6];
			
		 
			$query = " Select Acc_Id, Link_Ref, Link_Type, Round(ifNull(Sale_NetTotal,0), ".$DecimalBase."), Round(ifNull(Sale_NetToPay,0), ".$DecimalBase."), ".
					 " Round(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0), ".$DecimalBase."), Round(ifNull(Sale_RemainAmnt,0), ".$DecimalBase."), ".
					 " ifNull(Sale_NetTotal,0), ifNull(Sale_NetTotalBase1,0), ifNull(Sale_NetTotalBase2,0), ".
					 " ifNull(Sale_NetToPay,0), ifNull(Sale_NetToPayBase1,0), ifNull(Sale_NetToPayBase2,0), ".
					 " ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0), ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0), ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0), ".
					 " ifNull(Sale_RemainAmnt,0), ifNull(Sale_RemainAmntBase1,0), ifNull(Sale_RemainAmntBase2,0), Link_ID ".
					 " From ".$_SESSION['dbLabel'].".Temp_AccLink As AccLink ".
					 " Where AccLink.Journal_Id = '".$PTransDtId."' ".
					 " And AccLink.Journal_Type = '".$TransType."' ".
					 " And upper(AccLink.CompanyName) = upper('".$DirCompany."') ".
					 " And upper(AccLink.DirYear) = upper('".$DirYear."') ";
			
			//echo $query;
						
			$result=mysql_query($query,$connection);
			$totalrows = mysql_num_rows($result);
		      
			while($rowsdt=mysql_fetch_array($result)){
			    				
				$recno = $i+1;
				
				if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';
				
				$txtAccLinkBalAmt20  = $txtAccLinkBalAmt20_M + $rowsdt[13];
				$txtAccLinkBalAmt21  = $txtAccLinkBalAmt21_M + $rowsdt[14];
				$txtAccLinkBalAmt22  = $txtAccLinkBalAmt22_M + $rowsdt[15];
					
				
				$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_I['.$i.']\')"';
				
				echo"<tr id='CurRec_G10$i' class='$bgcolor $TableFocus'>";		
				echo "	<td width='25px;' align='center' valign='center'><img src='img/delete.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"DeleteAccLink('".$filepath."','".$form."','DEL','datagrid-2-i','".$PTransId."','".$PTransDtId."','".$rowsdt[0]."');return false;\" title='Delete Record'  ></td>";
				
				echo "	<td width='105px;' align='left'>
							<input type='hidden' name='txtAccLinkId2[$i]' id='acclinkid2[$i]' value='".$rowsdt[0]."' >
							<input type='hidden' name='txtLinkID2[$i]'  id='linkid2[$i]'  value='".$rowsdt[19]."' >
							<input type='hidden' name='txtLinkType2[$i]'  id='linktype2[$i]'  value='".$rowsdt[2]."' >
							<input type='hidden' name='txtAccLinkNetTotAmt20[$i]'  id='acclinknettotamt20_h[$i]'  value='".$rowsdt[7]."' >
							<input type='hidden' name='txtAccLinkNetTotAmt21[$i]' id='acclinknettotamt21_h[$i]'  value='".$rowsdt[8]."' >
							<input type='hidden' name='txtAccLinkNetTotAmt22[$i]' id='acclinknettotamt22_h[$i]'  value='".$rowsdt[9]."' >
							<input type='hidden' name='txtAccLinkNetToPay20[$i]'  id='acclinknettopay20_h[$i]'  value='".$rowsdt[10]."' >
							<input type='hidden' name='txtAccLinkNetToPay21[$i]' id='acclinknettopay21_h[$i]' value='".$rowsdt[11]."' >
							<input type='hidden' name='txtAccLinkNetToPay22[$i]' id='acclinknettopay22_h[$i]' value='".$rowsdt[12]."' >
							<input type='hidden' name='txtAccLinkPaidAmt20[$i]'  id='acclinkpaidamt20_h[$i]' value='".$rowsdt[13]."' >
							<input type='hidden' name='txtAccLinkPaidAmt21[$i]' id='acclinkpaidamt21_h[$i]' value='".$rowsdt[14]."' >
							<input type='hidden' name='txtAccLinkPaidAmt22[$i]' id='acclinkpaidamt22_h[$i]' value='".$rowsdt[15]."' >
							<input type='hidden' name='txtAccLinkRemainAmt20[$i]'  id='acclinkremainamt20_h[$i]' value='".$rowsdt[16]."' >
							<input type='hidden' name='txtAccLinkRemainAmt21[$i]' id='acclinkremainaAmt21_h[$i]' value='".$rowsdt[17]."' >
							<input type='hidden' name='txtAccLinkRemainAmt22[$i]' id='acclinkremainamt22_h[$i]' value='".$rowsdt[18]."' >
							<input type='hidden' name='txtAccLinkBalAmt20[$i]' id='acclinkbalamt20[$i]'  value='$txtAccLinkBalAmt20' >
							<input type='hidden' name='txtAccLinkBalAmt21[$i]' id='acclinkbalamt21[$i]'  value='$txtAccLinkBalAmt21' >
							<input type='hidden' name='txtAccLinkBalAmt22[$i]' id='acclinkbalamt22[$i]'  value='$txtAccLinkBalAmt22' >
							<input type='hidden' name='txtAccLinkTotAmt20[$i]' id='acclinktotamt20[$i]'  value='$txtAccLinkTotAmt20' >
							<input type='hidden' name='txtAccLinkTotAmt21[$i]' id='acclinktotamt21[$i]'  value='$txtAccLinkTotAmt21' >
							<input type='hidden' name='txtAccLinkTotAmt22[$i]' id='acclinktotamt22[$i]'  value='$txtAccLinkTotAmt22' >
							<input type='text' name='txtLinkRef2[$i]' id='linkref2[$i]' value='".$rowsdt[1]."'  maxlength='20' class='".$ClassE."' style='font-size:12px; width:75px; height:20px;' onChange=\"SetModRecord('ModRec_G10','$i');\" ".$KeyDown." />";
				echo"		<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistInvoices(2,".$i.",'".$PTransDtId."');\" style='vertical-align:bottom;' title='Select Invoice'  /></span></label>";
				echo"	</td>";
	
										
				echo"   <td width='50px;' align='center'><input type='text' name='txtAccLinkCurrCode2[$i]' id='acclinkcurrcode2[$i]' value='".$TransDtCurrCode."' maxlength='10' class='".$ClassE."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkNetToPay20_D[$i]' id='acclinknettopay20[$i]' value='".$rowsdt[4]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkPaidAmt20_D[$i]' id='acclinkpaidamt20[$i]' value='".$rowsdt[5]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onKeyup='CalcRemainAmount(2,$i);' onChange=\"SetModRecord('ModRec_G10','$i'); \" ".$KeyDown." /></td>";
				echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkRemainAmt20_D[$i]' id='acclinkremainamt20[$i]' value='".$rowsdt[6]."' maxlength='20' class='".$ClassE."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
				
				echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_I[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitAccLink2('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-2-i', '".$PTransId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
				echo"	<td width='25px;' align='center'>$recno</td>";
				echo "</tr>";
			
				$i+=1;
				
				
			}
			
		}
		
		
		
		$recno = $i+1;
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	

		$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$columnslist.'\',\'imgSubmit_2_I['.$i.']\')"';
		$KeyDown1  = "onkeydown=\"return dokey1(event,this,".$i.",'".$columnslist."','imgSubmit_2_I[".$i."]','1');LeaveToMainGrid('linkref2[$i]','ledgernumber2','RecIndex_G2','')\" ";						
		
		$sub_query2 = " Select TransDt.TransDt_ID, ".
					  " Round((ifNull(Transdt_Amount,0) - Sum(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0))), ".$DecimalBase1.")  As PaidAmnt_D, ".
					  " Round((ifNull(Transdt_Amount1,0) - Sum(ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0))), ".$DecimalBase1.") As PaidAmnt1_D, ".
					  " Round((ifNull(Transdt_Amount2,0) - Sum(ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0))), ".$DecimalBase1.") As PaidAmnt2_D, ".
					  " ifNull(Transdt_Amount,0), ifNull(Transdt_Amount1,0), ifNull(Transdt_Amount2,0), ".
					  " (ifNull(Transdt_Amount,0) - Round(Sum(ifNull(Sale_PaidAmnt,0) + ifNull(Sale_ReturnAmnt,0)),9) ) As PaidAmnt, ".
					  " (ifNull(Transdt_Amount1,0) - Round(Sum(ifNull(Sale_PaidAmntBase1,0) + ifNull(Sale_ReturnAmntbase1,0)),9) ) As PaidAmnt1, ".
					  " (ifNull(Transdt_Amount2,0) - Round(Sum(ifNull(Sale_PaidAmntBase2,0) + ifNull(Sale_ReturnAmntbase2,0)),9) ) As PaidAmnt2 ".
					  " From Temp_TransDt As TransDt Left Join ".$_SESSION['dbLabel'].".Temp_AccLink on TransDt.TransDt_ID = Temp_AccLink.Journal_ID ".
					  " And Temp_AccLink.Journal_Type = '".$TransType."' ".
					  " And upper(Temp_AccLink.CompanyName) = upper('".$DirCompany."') ".
					  " And upper(Temp_AccLink.DirYear) = upper('".$DirYear."') ".
					  " Where TransDt.TransDt_ID = '".$PTransDtId."' ".
					  " Group By TransDt.TransDt_ID, Transdt_Amount, Transdt_Amount1, Transdt_Amount2 ";

							  
		//echo $sub_query2;
		$sub_result2 = selectData($connection, $sub_query2);
		
		$txtAccLinkBalAmt20  = $sub_result2[7];
		$txtAccLinkBalAmt21  = $sub_result2[8];
		$txtAccLinkBalAmt22  = $sub_result2[9];
			
		$txtAccLinkTotAmt20  = $sub_result2[4];
		$txtAccLinkTotAmt21  = $sub_result2[5];
		$txtAccLinkTotAmt22  = $sub_result2[6];
			
		$txtAccLinkPaidAmt20_D = $sub_result2[1];
	
		$txtAccLinkPaidAmt20  = $sub_result2[7];
		$txtAccLinkPaidAmt21  = $sub_result2[8];
		$txtAccLinkPaidAmt22  = $sub_result2[9];
				
		echo"<tr id='CurRec_G10$i' class='$bgcolor $TableFocus' >";
		echo"	<td width='25px;' align='center' valign='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$columnslistall."','".$i."','ModRec_G10');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
			  
		echo "	<td width='105px;' align='left'>
					<input type='hidden' name='txtAccLinkId2[$i]' id='acclinkid2[$i]' value='$txtAccLinkId2' >
					<input type='hidden' name='txtLinkID2[$i]'  id='linkid2[$i]'  value='$txtLinkID2' >
					<input type='hidden' name='txtLinkType2[$i]'  id='linktype2[$i]'  value='$txtLinkType2' >
					<input type='hidden' name='txtAccLinkNetTotAmt20[$i]'  id='acclinknettotamt20_h[$i]'  value='$txtAccLinkNetTotAmt20' >
					<input type='hidden' name='txtAccLinkNetTotAmt21[$i]' id='acclinknettotamt21_h[$i]'  value='$txtAccLinkNetTotAmt21' >
					<input type='hidden' name='txtAccLinkNetTotAmt22[$i]' id='acclinknettotamt22_h[$i]'  value='$txtAccLinkNetTotAmt22' >
					<input type='hidden' name='txtAccLinkNetToPay20[$i]'  id='acclinknettopay20_h[$i]'  value='$txtAccLinkNetToPay20' >
					<input type='hidden' name='txtAccLinkNetToPay21[$i]' id='acclinknettopay21_h[$i]' value='$txtAccLinkNetToPay21' >
					<input type='hidden' name='txtAccLinkNetToPay22[$i]' id='acclinknettopay22_h[$i]' value='$txtAccLinkNetToPay22' >
					<input type='hidden' name='txtAccLinkPaidAmt20[$i]'  id='acclinkpaidamt20_h[$i]' value='$txtAccLinkPaidAmt20' >
					<input type='hidden' name='txtAccLinkPaidAmt21[$i]' id='acclinkpaidamt21_h[$i]' value='$txtAccLinkPaidAmt21' >
					<input type='hidden' name='txtAccLinkPaidAmt22[$i]' id='acclinkpaidamt22_h[$i]' value='$txtAccLinkPaidAmt22' >
					<input type='hidden' name='txtAccLinkRemainAmt20[$i]'  id='acclinkremainamt20_h[$i]' value='$txtAccLinkRemainAmt20' >
					<input type='hidden' name='txtAccLinkRemainAmt21[$i]' id='acclinkremainaAmt21_h[$i]' value='$txtAccLinkRemainAmt21' >
					<input type='hidden' name='txtAccLinkRemainAmt22[$i]' id='acclinkremainamt22_h[$i]' value='$txtAccLinkRemainAmt22' >
					<input type='hidden' name='txtAccLinkBalAmt20[$i]' id='acclinkbalamt20[$i]'  value='$txtAccLinkBalAmt20' >
					<input type='hidden' name='txtAccLinkBalAmt21[$i]' id='acclinkbalamt21[$i]'  value='$txtAccLinkBalAmt21' >
					<input type='hidden' name='txtAccLinkBalAmt22[$i]' id='acclinkbalamt22[$i]'  value='$txtAccLinkBalAmt22' >
					<input type='hidden' name='txtAccLinkTotAmt20[$i]' id='acclinktotamt20[$i]'  value='$txtAccLinkTotAmt20' >
					<input type='hidden' name='txtAccLinkTotAmt21[$i]' id='acclinktotamt21[$i]'  value='$txtAccLinkTotAmt21' >
					<input type='hidden' name='txtAccLinkTotAmt22[$i]' id='acclinktotamt22[$i]'  value='$txtAccLinkTotAmt22' >
					<input type='text' name='txtLinkRef2[$i]' id='linkref2[$i]' value='$txtLinkRef2'  maxlength='20' class='".$ClassI."' style='font-size:12px; width:75px; height:20px;' onBlur=\"LeaveToMainGrid('linkref2[$i]','ledgernumber2','RecIndex_G2','')\"    onChange=\"SetModRecord('ModRec_G10','$i');\" ".$KeyDown1." />";
		echo"		<label><span><img src='img/openlist.png' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"showlistInvoices(2,".$i.",'".$PTransDtId."');\" style='vertical-align:bottom;' title='Select Invoice'  /></span></label>";			
		echo"	</td>";
										
		echo"   <td width='50px;' align='center'><input type='text' name='txtAccLinkCurrCode2[$i]' id='acclinkcurrcode2[$i]' value='".$TransDtCurrCode."' maxlength='10' class='".$ClassI."' style='font-size:12px; width:45px; height:20px;' readonly ></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkNetToPay20_D[$i]' id='acclinknettopay20[$i]' value='$txtAccLinkNetToPay20_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkPaidAmt20_D[$i]' id='acclinkpaidamt20[$i]' value='$txtAccLinkPaidAmt20_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;' onBlur='CheckAmount(this.id,$i);' onKeyup='CalcRemainAmount(2,$i);' onChange=\"SetModRecord('ModRec_G10','$i'); \" ".$KeyDown." /></td>";
		echo"	<td width='95px;' align='center'><input type='text' name='txtAccLinkRemainAmt20_D[$i]' id='acclinkremainamt20[$i]' value='$txtAccLinkRemainAmt20_D' maxlength='20' class='".$ClassI."' style='text-align:right; font-size:12px; width:90px; height:20px;'  readonly /></td>";
			
		echo "	<td width='25px;' align='center' valign='center'><img src='img/confirm-1.png' id='imgSubmit_2_I[$i]' width='20' height='20' border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"SubmitAccLink2('".$i."','".$filepath."', '".$form."', 'ADD', 'datagrid-2-i', '".$PTransId."', '".$PTransDtId."'); return false;\" title='Submit Record' style='vertical-align:bottom;' ></td>";
		echo"	<td width='25px;' align='center'>$recno</td>";
		echo"</tr>";  

		
	echo"	</table>";  
	echo"	</div>";
	
	echo"</div>";
	
	
}


function ShowTransTotals($connection,$PTransId,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_TransStandardView = $arrayparams['Module_TransStandardView'];
	

	$sub_query = " Select Trans_Date, Currency_Code, ifNull(Trans_AmountDb1,0), ifNull(Trans_AmountDb2,0), ifNull(Trans_Amountcd1,0), ifNull(Trans_Amountcd2,0)  ".
				 " From Temp_Trans As Trans ".
				 " Where Trans_ID = ".$PTransId;
			 
	//echo $sub_query;
	$sub_result = selectData($connection, $sub_query);
	
	$txtDate = $sub_result[0];
	$txtCurr = $sub_result[1];
	$txtTransAmountDb1   = $sub_result[2];
	$txtTransAmountDb2   = $sub_result[3];
	$txtTransAmountCd1   = $sub_result[4];
	$txtTransAmountCd2   = $sub_result[5];
	$txtTransBalance1  = $txtTransAmountDb1 - $txtTransAmountCd1;
	$txtTransBalance2  = $txtTransAmountDb2 - $txtTransAmountCd2;
	
	$txtTransAmtDb    = ConvertCurrency($Base1, $txtCurr, insertMySQLDate($txtDate), $txtTransAmountDb1, $connection); 
	$txtTransAmtCr    = ConvertCurrency($Base1, $txtCurr, insertMySQLDate($txtDate), $txtTransAmountCd1, $connection);
	$txtTransAmtBal   = $txtTransAmtDb - $txtTransAmtCr;
	
	echo"	  <table valign='top' width='240' border='0' cellspacing=0 cellpadding=0>
				<tr>
					<td id='total-top' style='width:240px;height:12px;'></td>
				</tr>
				<tr>
					<td id='total-content' style='width:240px;height:12px;'>";
				
				echo"	<table border='0' cellspacing=0 width=100% >";
					
			if ($Module_TransStandardView == 1) {				
				echo"	<tr>
							<td valign='middle' align='center' width='15%'><span class='style10'>$Base1</span></td> 
							<td valign='middle' align='right' width='25%'><span class='style10'>Debit</span>&nbsp;</td>
							<td align='left'><input type='text' id='transamnt1db'  name='txtTransAmountDb1' value='".number_format($txtTransAmountDb1,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style10'>Credit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transamnt1cd' name='txtTransAmountCd1' value='".number_format($txtTransAmountCd1,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style10'>Balance</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transbalance1' name='txtTransBalance1' value='".number_format($txtTransBalance1,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
							
							if($txtTransBalance1<0) echo " color:#FF0000;";
							echo" ' readonly></td>
						</tr>
						<tr>
							<td valign='middle' align='center' width='15%'><span class='style11'>$Base2</span></td> 
							<td valign='middle' align='right' width='25%'><span class='style11'>Debit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transamnt2db' name='txtTransAmountDb2' value='".number_format($txtTransAmountDb2,$DecimalBase2)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>	
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style11'>Credit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transamnt2cd' name='txtTransAmountCd2' value='".number_format($txtTransAmountCd2,$DecimalBase2)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style11'>Balance</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transbalance2' name='txtTransBalance2' value='".number_format($txtTransBalance2,$DecimalBase2)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;";
						if($txtTransBalance2<0) echo " color:#FF0000;";
							echo " ' readonly></td>
						</tr>";
				
				echo"	<input type='hidden' id='transamntdb'  name='txtTransAmtDb' value='".number_format($txtTransAmtDb,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transamntcd'  name='txtTransAmtCr' value='".number_format($txtTransAmtCr,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transbalance'  name='txtTransAmtBal' value='".number_format($txtTransAmtBal,$DecimalBase1)."'>";
			} else {
				echo"	<tr>
							<td valign='middle' align='center' width='15%'><span class='style10'>$txtCurr</span></td> 
							<td valign='middle' align='right' width='25%'><span class='style10'>Debit</span>&nbsp;</td>
							<td align='left'><input type='text' id='transamntdb'  name='txtTransAmtDb' value='".number_format($txtTransAmtDb,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style10'>Credit</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transamntcd' name='txtTransAmtCr' value='".number_format($txtTransAmtCr,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>
						<tr><td valign='middle' align='right' width='40%' colspan='2'><span class='style10'>Balance</span>&nbsp;</td> 
							<td align='left'><input type='text' id='transbalance' name='txtTransAmtBal' value='".number_format($txtTransAmtBal,$DecimalBase1)."' maxlength='20' style='text-align:right; width:120px; height=20px;background:#C6D4E5;' readonly></td>
						</tr>";
						
				echo"	<input type='hidden' id='transamnt1db'  name='txtTransAmountDb1' value='".number_format($txtTransAmountDb1,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transamnt1cd'  name='txtTransAmountCd1' value='".number_format($txtTransAmountCd1,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transbalance1'  name='txtTransBalance1' value='".number_format($txtTransBalance1,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transamnt2db'  name='txtTransAmountDb2' value='".number_format($txtTransAmountDb2,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transamnt2cd'  name='txtTransAmountCd2' value='".number_format($txtTransAmountCd2,$DecimalBase1)."'>";
				echo"	<input type='hidden' id='transbalance2'  name='txtTransBalance2' value='".number_format($txtTransBalance2,$DecimalBase1)."'>";
			}
						
	echo "					
						</table>
					</td>
				</tr>";
			
	echo "		<tr>
					<td id='total-bottom' style='width:240px;height:12px;'></td>
				</tr>
			  </table>";
}

function showDataGridFooter($filepath,$form,$transid,$gridid,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction) {
	
	echo"<div class='grid_toolbarDiv'>
			<table width='100%' border=0 cellpadding='0' cellspacing='0'>
			<tr>";
	
	$rangefrom = ($startrange + 1);
	if (($startrange + $perpage) >= $totalrows) $rangeto = $totalrows; else $rangeto = ($startrange + $perpage);
				
	if ($totalrows > 0)			
		echo"	<td align='left' style='width:33%; padding-left:10px;border-top:1px solid #000000;'><span class='toolbar_style'>View ".$rangefrom." - ".$rangeto." of ".$totalrows."</span></td>";
	else
		echo"	<td align='left' style='width:33%; padding-left:10px;border-top:1px solid #000000;'><span class='toolbar_style'>No records to view</span></td>";
	
	echo"		<td align='center' style='width:15%;border-top:1px solid #000000;'><span class='toolbar_style'>Per Page </span><span class='toolbar_style'><select name='perpage' id='perpage' value='$perpage' onchange=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'CP','".$orderby."','".$morelines."',0,this.value,'".$totalrows."','".$direction."');\">";
				  
				if ($perpage==10)		 $s10 = 'selected';
				else if ($perpage==20)	 $s20 = 'selected';
				else if ($perpage==30)	 $s30 = 'selected';
				else if ($perpage==40)	 $s40 = 'selected';
				else if ($perpage==50)	 $s50 = 'selected';
				else if ($perpage==100)	 $s100 = 'selected';
				else if ($perpage==1000) $s1000 = 'selected';

								
				echo'<option '.$s10.' value="10">10</option>
					 <option '.$s20.' value="20">20</option>
					 <option '.$s30.' value="30">30</option>
					 <option '.$s40.' value="40">40</option>
					 <option '.$s50.' value="50">50</option>
					 <option '.$s100.' value="100">100</option>
					 <option '.$s1000.' value="1000">1000</option>
					 </select></span>
				</td>';
				
	echo"		<td  style='width:10%;border-right:1px solid #000000;text-align:right;border-top:1px solid #000000;'>";
				
				if($startrange > 0)
					echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='pointer'\"    onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'F','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";

				$diff= $startrange - $perpage;
					
				if (($diff) >= 0 ){
					$prvstartrange = $startrange - $perpage;
					echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='pointer'\"  onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'P','".$orderby."','".$morelines."','".$prvstartrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";
				}
				else
					echo "&nbsp;";
			
	echo"		</td><td  align='left' style='width:10%;border-top:1px solid #000000;'>&nbsp;";
			
			
				if ($startrange <= $totalrows){
					$startrange1 = $startrange + $perpage;
					if  ($startrange1 < $totalrows){ 
						echo"<img src='img/arrowright.png' title='Next' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'N','".$orderby."','".$morelines."','".$startrange1."','".$perpage."','".$totalrows."','".$direction."');return false;\" onmouseover=\"style.cursor='pointer'\" >";
						echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='pointer'\" onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'L','".$orderby."','".$morelines."','".$prvstartrange."','".$perpage."','".$totalrows."','".$direction."');return false;\" >";	
					}
				} else
					echo "&nbsp;";
			
				
				$currentpage = ($startrange / $perpage) + 1;
				$totalpages  = ceil($totalrows / $perpage);
				
	echo"		</td><td style='text-align:center; width:14%;border-top:1px solid #000000;'>&nbsp;<span class='toolbar_style'>Page $currentpage / $totalpages</span></td>";
	

	
	echo"		<td style='text-align:center; width:6%;border-top:1px solid #000000;'><img src='img/Refresh-icon.png'  onMouseOver=\"style.cursor='pointer'\"  width='20' height='20' onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$transid."', '".$gridid."', 1, '".$dftorderby."', 'R','".$orderby."','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\" ></td>";
	echo"		<td align='right' style='width:12%; padding-right:10px;border-top:1px solid #000000;'>&nbsp;</td>";
	
	echo"	</tr>";
	echo"	</table></div>";

}



function ShowDelivery($keyword,$keywordlot,$vers,$status,$connection,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	
	$useraccount1 = $arrayparams['useraccount1'];
	$dbLabel = $arrayparams['dbLabel'];
	$Module_ProjectParent = $arrayparams['Module_ProjectParent'];
	$Module_CostCenterParent = $arrayparams['Module_CostCenterParent'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
		
	
$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$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='".$useraccount1."' ".
			  " 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 ".$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='".$useraccount1."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
//echo $querycostcent;
			   
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$dbLabel.".ledgerstk  ".
			  " where user_account='".$useraccount1."' ".
			  " 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 ".$dbLabel.".loginstk where user_account='".$useraccount1."' 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 ".$dbLabel.".loginstk where user_account='".$useraccount1."' 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;	

	
	echo"
		
		 <input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
		 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
		 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>		 
		 ";
	
	//Select Data From Delivery For Selected Record
	$query = " Select Distinct Deliveries.Delivery_ID, Delivery_Reference, Date_Format(Delivery_Date,'%d/%m/%Y') As Delivery_Date, ".
	         " Deliveries.Client_code As ClientCode, Clients.Ledger_name As ClientName,    ".
			 " Deliveries.Salesman_code As SalesmanCode, Salesmen.Ledger_name As SalesmanName, Delivery_Remark,Deliveries.USER_Account ".
			 " ,Delivery_Date as delivery_dateord, costcent_code ".
			 " From Deliveries As Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join ClientsDetail on Deliveries.Detail_Code = ClientsDetail.Detail_Code ".
			 " Left Join Salesmen on Deliveries.Salesman_code = Salesmen.Ledger_Number ".
			// " Left Join Journal on Deliveries.Journal_ID = Journal.Journal_ID ".
			 " Left Join delivery_deliverystatus on Deliveries.Delivery_id = delivery_deliverystatus.Delivery_id ";
if ($projectexist >	0)
  $query= $query." Inner join ( select distinct project_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and Deliveries.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and Deliveries.project_code = Q1.project_code ))  ";
							 
if ($costcentexist > 0)
  $query= $query." Inner join ( select distinct costcent_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and Deliveries.costcent_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and Deliveries.costcent_code = Q2.costcent_code )) ";							 
			


			$query=$query." Where 1=1    ";
	

if($ledgerinclude== 1 && $ledgerexist > 0) {
	$query = $query." And Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"              where ledgerstk.user_account='".$useraccount1."' ".
								"              and (ledger_number is not null and ledger_number != '') ".
								"              and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"            ) ";
}
if($ledgerinclude== 0 && $ledgerexist > 0){
	$query = $query." And Not Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"                  where ledgerstk.user_account='".$useraccount1."' ".
								"          	       and (ledger_number is not null and ledger_number != '') ".
								"                  and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"                ) ";	
}
	
if($status=='')
	$query = $query." and ( delivery_deliverystatus.status_id is null
							)";
	$query = $query." and Deliveries.Journal_Type = 'DI' ";
if($vers==1 and $keyword!='')
	/*$query = $query." and ( Deliveries.delivery_reference like ('%$keyword%')  
							or  Deliveries.Client_code like ('%$keyword%')  
							or  Clients.Ledger_name  like ('%$keyword%')  
							or  Date_Format(Delivery_Date,'%d/%m/%Y')   like ('%$keyword%')
							)";
*/
     $query = $query." and ( Deliveries.delivery_reference = '$keyword'  
							
							)";

if($vers==2 and $keywordlot!='')
	$query = $query." and exists ( select 1 from delivery_lotnumber where lotnumber_number like ('%$keywordlot%')
									and deliverydt_id in (select deliverydt_id from deliverydt where delivery_id = Deliveries.delivery_id)
							)";
if($vers==3 and $status!='')
	$query = $query." and ( delivery_deliverystatus.status_id = $status and ifNull(deliverystatus_last,0) = 1
							)";

							
	$query = $query ."order by delivery_dateord desc ";
	         
		//	echo $query; 
		//exit;	 
			 
		echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:720px;'>";
		echo"<col width=70>";
		echo"<col width=70>";
		echo"<col width=70>";
		echo"<col width=200>";
		echo"<col width=130>";
		echo"<col width=65>";
		echo"<col width=200>";
		echo"<col width=80>";
		echo"  <th align='center' ><span>Reference</span></th>";
		echo"  <th align='center' ><span>Date</span></th>";
		echo"  <th align='center' ><span>Client</span></th>";
		echo"  <th align='center' ><span>Client Name</span></th>";
		echo"  <th align='center' ><span>Salesmen</span></th>";
		echo"  <th align='center' ><span>User</span></th>";
		echo"  <th align='center' ><span>Remark</span></th>";
		echo"  <th align='center' ><span>Coscenter</span></th>";
	//echo $query;
	$i=0;
	$result=mysql_query($query,$connection) or die(mysql_error());
    while ($row=mysql_fetch_array($result)){
	
	
	if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

				
				
	echo"<tr height=20 id='$row[0]' class='$bgcolor table-focus-g1'  onclick=\"returncolor(this,'SelectedRec','SelectedRecColor','$row[0]');showrelationtodt('$row[0]')\" >";
	echo"<td>$row[1]</td>";
	echo"<td>$row[2]</td>";
	echo"<td>$row[3] </td>";
	echo"<td>$row[4]</td>";
	echo"<td>$row[6]</td>";
	echo"<td>$row[8]</td>";
	echo"<td>$row[7]</td>";
	echo"<td>$row[10]</td>";
	echo"</tr>";
	$i+=1;
	}
	echo"</table>";
	

	
	

}

function ShowDeliveryDt($PSaId,$connection,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
		
		
	echo"
		
		 <input type='hidden' name='deliverydt_quantity' id='deliverydt_quantity' value='$deliverydt_quantity'>
		 <input type='hidden' name='SelectedRecDt' id='SelectedRecDt' value='$SelectedRecDt'>
		 <input type='hidden' name='SelectedRecColorDt' id='SelectedRecColorDt' value='$SelectedRecColorDt'>
		 
		 ";
	
				   
			$query = " Select DeliveryDt.DeliveryDt_id, DeliveryDt.Item_CodePacked, DeliveryDt.Item_code, Item_Description, DeliveryDt.Warehouse_Code, ".
					 " DeliveryDt_Remark, DeliveryDt_Quantity, Round(DeliveryDt_Price, ".$DecimalBase."), DeliveryDt_PriceBase1, DeliveryDt_PriceBase2, ".
					 " Round(DeliveryDt_DiscountPer, ".$DecimalBase."),  ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_Price)-((DeliveryDt_Quantity*DeliveryDt_Price*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As Total, ".
					 " Round(DeliveryDt_PriceTTC, ".$DecimalBase."), ".
					 " Round(((DeliveryDt_Quantity*DeliveryDt_PriceTTC)-((DeliveryDt_Quantity*DeliveryDt_PriceTTC*ifNull(DeliveryDt_DiscountPer,0))/100)), ".$DecimalBase.") As TotalTTC, ".
					 " DeliveryDt.Item_Unit2Qty, Item_UnitPurchaseDesc, Item_UnitSalesDesc, DeliveryDt.Project_code, DeliveryDt.CostCent_Code,  ".
					 " SorderDt.SorderDt_Quantity, ifNull(item_withserialno,0) As ItemWithSerialNo, Sorder.Sorder_reference, Count(SerialNo_Code) As CountSerialNo,  ".
					 " Date_Format(DeliveryDt_ExpireDate,'%d/%m/%Y') As DeliveryDt_ExpireDate, DeliveryDt_BatchNo, DeliveryDt.DeliveryDt_ClosingQuantity, ifNull(DeliveryDt_Promotion,0), ".
					 " Round(ifNull(MeanCostCur(DeliveryDt.Item_code, DeliveryDt.Delivery_Date, 1, '".$Base1."', DeliveryDt.Currency_Code),0), ".$DecimalBase.") as ItemCost ".
					 " From DeliveryDt As DeliveryDt ".
					 " Left Join Items on DeliveryDt.Item_code = Items.Item_code ".
					 " Left Join Purchasesserialno on DeliveryDt.DeliveryDt_ID = Purchasesserialno.PurchaseDt_ID ".
					 " And Purchasesserialno.Journal_Type = 'DI' ".
					 " And Purchasesserialno.DeliveryDt_ID Is Not Null ".
					 " Left Join SorderDt on SorderDt.SorderDt_ID = DeliveryDt.RelatedDt_Id  ".
					 " Left Join Sorder on Sorder.Sorder_ID = SorderDt.Sorder_ID  ".
					 " Where DeliveryDt.Delivery_ID = ".$PSaId.
					 " Group By  DeliveryDt.DeliveryDt_ID ";
					 
	       //  echo $query;
			 
			 
			 
		echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:670px;'>";
		echo"<col width=90>";
		echo"<col width=190>";
		echo"<col width=60>";
		echo"<col width=40>";
		echo"<col width=200>";
		echo"  <th align='center' ><span>Item Code</span></th>";
		echo"  <th align='center' ><span>Description</span></th>";
		echo"  <th align='center' ><span>Warehouse</span></th>";
		echo"  <th align='center' ><span>Qty</span></th>";
		echo"  <th align='center' ><span>Remark</span></th>";
	//echo $query;
	$i=0;
	$result=mysql_query($query,$connection) or die(mysql_error());
    while ($row=mysql_fetch_array($result)){
	
	
	if ($i%2==0) $bgcolor='bgcolor1';
				else $bgcolor='bgcolor2';

				
				
	echo"<tr height=20 id='deliverydt$row[0]' class='$bgcolor table-focus-g1'  
					onclick=\"returncolor(this,'SelectedRecDt','SelectedRecColorDt','$row[0]');showrelationtolot('$row[0]','$row[6]')\" 
					
					
					>";
	echo"<td>$row[2]</td>";
	echo"<td>$row[3]</td>";
	echo"<td>$row[4]</td>";
	echo"<td>$row[6]</td>";
	echo"<td>$row[5]</td>";
	echo"</tr>";
	$i+=1;
	}
	echo"</table>";
	

	
	

}

function Show_Delivery_DeliveryStatus($delivery_id,$connection,$arrayparams,$arrayaccess,$useraccount){


echo"<input type='hidden' name='delivery_id' id='delivery_id' value='$delivery_id'>";



	$query="select deliverystatus_id, date_format(deliverystatus_date,'%d/%m/%Y'), status_id, username, remark, delivery_id 
			from delivery_deliverystatus,deliverystatus_setup
			where delivery_id = '$delivery_id' 
			and deliverystatus_setup.id =  status_id
			order by deliverystatus_level
			";
//echo $query;		
$result=mysql_query($query,$connection) or die(mysql_error());
 

 
$i=0;

 


echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:480px;'>";
echo"<tr height=25><td colspan=4><span class='style4'>
<font color=#1e87a5><b>Delivery Status</b></span></td></tr>";
echo"
	<col width=35>
	<col width=85>
	<col width=95>
	<col width=80>
	<col width=100>
	<col width=35>
	";
	
	 
	echo"<th ><span class='style4'></span></th>
		<th ><span class='style4'>Status</span></th>
		<th ><span class='style4'>Date</span></th>
		<th ><span class='style4'>Username</span></th>
		<th ><span class='style4'>Remark</span></th>
		<th ><span class='style4'></span></th>";
		
		echo"<input type='hidden' name='selectedtr_status' id='selectedtr_status' value='$selectedtr_status'>
		 <input type='hidden' name='selectedtrcolor_status' id='selectedtrcolor_status' value='$selectedtrcolor_status'>";

while($row=mysql_fetch_row($result)){
	
	
		if ($i%2==0)
		$bgcolor='#F5FAFF';
	else	
		$bgcolor='#FFFFFF';
	 
	 
		$deliverystatus_id[$i] = $row[0];
		$dd = 'deliverystatus_date'.$i;
		$$dd = $row[1];
		$status_id[$i] = $row[2];
		$remark[$i] = $row[4];
		$username[$i] = $row[3];
		
		 
			 
	
			$rownum = $i+1;
			echo"<tr height='20px' id='status$i' onclick=\"returncolor(this,'selectedtr_status','selectedtrcolor_status')\"  style='background-color:$bgcolor'>";
			
			echo"<td>";
			//<img src='img/delete.png' onmouseover=\"style.cursor='pointer'\" name='' title='Delete' onclick=\"deletenhmsremark('$row[0]','$row[4]')\">
			echo"</td>";
			
			
		echo"<td ><span class='style5'>";
			
			
			//<input type='text'  class='roundedtext_bonusdetail' style='width:500px' name='status_id[$i]' id='status_id[$i]' value='".$status_id[$i]."'>
			
			$query="select id,name,deliverystatus_level from deliverystatus_setup order by deliverystatus_level ";
			$resultstatus=mysql_query($query,$connection) or die(mysql_error());
				while($curf=mysql_fetch_row($resultstatus)){

					if ($status_id[$i]==$curf[0]){
						echo " $curf[1] ";
						$lastlevel=$curf[2];
						}
					
					}
			 
			
			echo"</span></td>";	
			
			$ClassE = 'grid_roundedtext1';
			$ClassI = 'grid_roundedtext2';
			echo'<td >';
			$dd = 'deliverystatus_date'.$i;
			echo"<input type='text'  class='roundeddate_attendcostcent'  style='width:70px' class='".$ClassE." date'   name='deliverystatus_date$i' id='deliverystatus_date$i'   value='".$$dd."'>";

			$name="deliverystatus_date".$i."";
			echo"
			<A HREF='#' onClick=\"displayCalendar(document.deliverystatus.".$name.",'dd/mm/yyyy',this);  return false; \" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A>
";
			echo'</td>';
			
			//$username[$i] = $useraccount;
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:70px' name='username[$i]'  readonly id='username[$i]' value='".$username[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='remark[$i]' id='remark[$i]' value='".$remark[$i]."'>
			 
			</span></td>";	
			
			
			echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitnhmsremark('$i','e')\"></td>";
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='deliverystatus_id[$i]' id='deliverystatus_id[$i]' value='".$deliverystatus_id[$i]."' ></span>";
			
			$i+=1;
			
		}

			echo"<tr height='20px' id='status$i' onclick=\"returncolor(this,'selectedtr_status','selectedtrcolor_status')\"  style='background-color:$bgcolor'>";
			
			echo"<td><img src='img/undo.png' onmouseover=\"style.cursor='pointer'\" name='' title='Clear' onclick=\"cleartdmsemployee('$i')\"></td>";
	
			echo"<td ><span class='style5'>";
			
			/* $querylog="SELECT ifnull(deliverystatus_level,0) deliverystatus_level from ".$_SESSION['dbLabel'].".users where User_account='".$_SESSION['useraccount']."' 	";

			$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
			while($rows=mysql_fetch_array($resultlog)){
				extract($rows);
			} */
			
			$query_checkStatus="select FI_DeliveryStatusFrom as statusfrom, FI_DeliveryStatusTo as statusto from ".$_SESSION['dbLabel'].".login 
				where User_account='".$_SESSION['useraccount']."' "; 
			$res_checkstatus=mysql_query($query_checkStatus,$connection) or die(mysql_error());
			$row_checkstatus = mysql_fetch_row($res_checkstatus);
			$statusfrom = $row_checkstatus[0]; 
			$statusto = $row_checkstatus[1];

	
			if($statusto > $lastlevel){
			//<input type='text'  class='roundedtext_bonusdetail' style='width:500px' name='status_id[$i]' id='status_id[$i]' value='".$status_id[$i]."'>
			$query="select id,name from deliverystatus_setup where deliverystatus_level>if('$lastlevel'='',-1,'$lastlevel') order by deliverystatus_level limit 1";
			//echo $query;
			echo"<select id='status_id[$i]' name='status_id[$i]'>";
			
			
			$resultstatus=mysql_query($query,$connection) or die(mysql_error());
				while($curf=mysql_fetch_row($resultstatus)){
				echo"<option ";
					if ($status_id[$i]==$curf[0])
						echo " selected ";
					echo" value='$curf[0]'>$curf[1] </option>";
					}
			 
			echo"</select>";
			echo"</span></td>";	
			
			$ClassE = 'grid_roundedtext1';
			$ClassI = 'grid_roundedtext2';
			echo'<td >';
			$dd = 'deliverystatus_date'.$i;
			if($$dd=='')
				$$dd = displayToday();
			echo"<input type='text'  class='roundeddate_attendcostcent'  style='width:70px' class='".$ClassE." date'   name='deliverystatus_date$i' id='deliverystatus_date$i'   value='".$$dd."'>";

			$name="deliverystatus_date".$i."";
			echo"
			<A HREF='#' onClick=\"displayCalendar(document.deliverystatus.".$name.",'dd/mm/yyyy',this);  return false; \" NAME=\"anchor1\" ID=\"anchor1\"><img src='./img/cal.gif' border=0 align=center></A>";
			echo'</td>';
			
			$username[$i] = $useraccount;
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:70px' name='username[$i]'  readonly id='username[$i]' value='".$username[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='remark[$i]' id='remark[$i]' value='".$remark[$i]."'>
			 
			</span></td>";	
			
		
			
			
			
			echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitstatus('$i','i')\"></td>";
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='deliverystatus_id[$i]' id='deliverystatus_id[$i]' value='".$deliverystatus_id[$i]."' ></span>";
				}
echo"</table>";	


}	

function Show_Delivery_Deliverylot($deliverydt_id,$connection,$arrayparams,$arrayaccess,$useraccount){


echo"<input type='hidden' name='deliverydt_id' id='deliverydt_id' value='$deliverydt_id'>";

$query="select item_code from deliverydt
			where deliverydt_id = '$deliverydt_id'";
$result=mysql_query($query,$connection) or die(mysql_error());
$row=mysql_fetch_row($result);
$item_code = $row[0];

$query="select lotnumber_id, lotnumber_qty, lotnumber_number, deliverydt_id from delivery_lotnumber
			where deliverydt_id = '$deliverydt_id'";
//echo $query;		
$result=mysql_query($query,$connection) or die(mysql_error());
 
$i=0; 
echo"<table onMouseOver=\"style.cursor='pointer'\"  border='0' align='left' id='grid_datatable' class='grid_data' cellpadding='0' cellspacing='1' style='width:275px;'>";
echo"<tr height=25><td colspan=4><span class='style4'>
<font color=#1e87a5><b>Delivery Lot#</b>&nbsp;&nbsp;&nbsp;&nbsp;<img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitlot('$i','e')\"></span></td></tr>";
echo"
	<col width=35>
	<col width=200>
	<col width=100>
	<col width=35>
	";
	
	 
	echo"<th ><span class='style4'></span></th>
		<th ><span class='style4'>Lot #</span></th>
		<th ><span class='style4'>Qty</span></th>
		<th ><span class='style4'></span></th>";
		
		echo"<input type='hidden' name='selectedtr_lot' id='selectedtr_lot' value='$selectedtr_lot'>
		 <input type='hidden' name='selectedtrcolor_lot' id='selectedtrcolor_lot' value='$selectedtrcolor_lot'>";

while($row=mysql_fetch_row($result)){
	
	
		if ($i%2==0)
		$bgcolor='#F5FAFF';
	else	
		$bgcolor='#FFFFFF';
	 
	 
		$lotnumber_id[$i] = $row[0];
	 	$lotnumber_qty[$i] = $row[1];
		$lotnumber_number[$i] = $row[2];
	 
			 
	
			$rownum = $i+1;
			echo"<tr height='20px' id='lot$i' onclick=\"returncolor(this,'selectedtr_lot','selectedtrcolor_lot')\"  ondblclick=\"showwindow('$row[2]','$item_code')\"  style='background-color:$bgcolor'>";
			
			echo"<td>
			<img src='img/delete.png' onmouseover=\"style.cursor='pointer'\" name='' title='Delete' onclick=\"deletelot('$row[0]','$row[4]')\">";
			echo"</td>";
			
			echo"<td ><span class='style5'>
			<input type='text' style='width:199px' class='roundedtext_bonusdetail' style='width:70px' name='lotnumber_number[$i]'  id='lotnumber_number[$i]' value='".$lotnumber_number[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='lotnumber_qty[$i]' id='lotnumber_qty[$i]' value='".$lotnumber_qty[$i]."'>
			 
			</span></td>";	
			
			
			//echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitlot('$i','e')\"></td>";//
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='lotnumber_id[$i]' id='lotnumber_id[$i]' value='".$lotnumber_id[$i]."' ></span>";
			
			$i+=1;
			
		}
		
			while($i<10 and ($deliverydt_id !='' and $deliverydt_id !='-1') ){
			echo"<tr height='20px' id='lot$i' onclick=\"returncolor(this,'selectedtr_lot','selectedtrcolor_lot')\"  style='background-color:$bgcolor'>";
			
			echo"<td><img src='img/undo.png' onmouseover=\"style.cursor='pointer'\" name='' title='Clear' onclick=\"cleartdmsemployee('$i')\"></td>";
	
			echo"<td ><span class='style5'>
			<input type='text'  style='width:199px'  class='roundedtext_bonusdetail' style='width:70px' name='lotnumber_number[$i]'   id='lotnumber_number[$i]' value='".$lotnumber_number[$i]."'>
			 
			</span></td>";	
			
		
			echo"<td ><span class='style5'>
			<input type='text'  class='roundedtext_bonusdetail' style='width:100px' name='lotnumber_qty[$i]' id='lotnumber_qty[$i]' value='".$lotnumber_qty[$i]."'>
			 
			</span></td>";	
			
		 		
			//echo"<td><img src='img/confirm.png' onmouseover=\"style.cursor='pointer'\" name='' title='Save' onclick=\"submitlot('$i','i')\"></td>";
			echo"<td></td>";
			echo"</tr>";
			echo"<span class='style5'><input type='hidden'  name='lotnumber_id[$i]' id='lotnumber_id[$i]' value='".$lotnumber_id[$i]."' ></span>";
			
			$numberofrecords=$i;
			if($numberofrecords<0)
				$numberofrecords=0;
			echo"<span class='style5'><input type='hidden'  name='numberofrecords' id='numberofrecords' value='".$numberofrecords."' ></span>";
			
			$i+=1;
			}
echo"</table>";	


}	

function GetReference($keyword,$vers,$statusfrom, $statusto,$connection,$arrayparams,$arrayaccess){

	$DecimalBase  = $arrayparams['DecimalBase'];
	$DecimalBase1 = $arrayparams['DecimalBase1'];
	$DecimalBase2 = $arrayparams['DecimalBase2'];
	$Journal_DefaultCur = $arrayparams['Journal_DefaultCur'];
	$Base1 = $arrayparams['Base1'];
	$Base2 = $arrayparams['Base2'];
	
	$Module_Project = $arrayparams['Module_Project'];
	$Module_CostCenter = $arrayparams['Module_CostCenter'];
	$Module_ProjectDt = $arrayparams['Module_ProjectDt'];
	$Module_CostCenterDt = $arrayparams['Module_CostCenterDt'];
	
	$txtJourDate = $arrayparams['txtJourDate'];
	
	
	$useraccount1 = $arrayparams['useraccount1'];
	$dbLabel = $arrayparams['dbLabel'];
	$Module_ProjectParent = $arrayparams['Module_ProjectParent'];
	$Module_CostCenterParent = $arrayparams['Module_CostCenterParent'];
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid.php';
	
	$i = 0;
		
	
$queryproject=" select distinct project.project_code, project_name ".
              " from project left join ".$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='".$useraccount1."' ".
			  " 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 ".$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='".$useraccount1."' ".
			   " and (loginstk.costcent_code is not null and loginstk.costcent_code != '') ".
			   " order by costcent.costcent_code";
//echo $querycostcent;
			   
$costcentresult = mysql_query($querycostcent,$connection) or die(mysql_error());
$costcentexist  = mysql_num_rows($costcentresult);		  

$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$dbLabel.".ledgerstk  ".
			  " where user_account='".$useraccount1."' ".
			  " 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 ".$dbLabel.".loginstk where user_account='".$useraccount1."' 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 ".$dbLabel.".loginstk where user_account='".$useraccount1."' 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;	

	
	
	
	//Select Data From Delivery For Selected Record
	$query = " Select Distinct Deliveries.Delivery_ID, Delivery_Reference, Date_Format(Delivery_Date,'%d/%m/%Y') As Delivery_Date, ".
	         " Deliveries.Client_code As ClientCode, Clients.Ledger_name As ClientName,    ".
			 " Deliveries.Salesman_code As SalesmanCode, Salesmen.Ledger_name As SalesmanName, Delivery_Remark,Deliveries.USER_Account ".
			 " ,Delivery_Date as delivery_dateord ".
			 " From Deliveries As Deliveries ".
			 " Left Join Clients on Deliveries.Client_code = Clients.Ledger_Number ".
			 " Left Join ClientsDetail on Deliveries.Detail_Code = ClientsDetail.Detail_Code ".
			 " Left Join Salesmen on Deliveries.Salesman_code = Salesmen.Ledger_Number ".
			// " Left Join Journal on Deliveries.Journal_ID = Journal.Journal_ID ".
			 " Left Join delivery_deliverystatus on Deliveries.Delivery_id = delivery_deliverystatus.Delivery_id ";
if ($projectexist >	0)
  $query= $query." Inner join ( select distinct project_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (project_code is not null and project_code != '') ".
							 "			 ) Q1 on (($Module_ProjectParent=1 and Deliveries.project_code like concat(Q1.project_code,'%')) or ($Module_ProjectParent=0 and Deliveries.project_code = Q1.project_code ))  ";
							 
if ($costcentexist > 0)
  $query= $query." Inner join ( select distinct costcent_code from ".$dbLabel.".loginstk ".
							 "			   where loginstk.user_account='".$useraccount1."' ".
							 "             and (costcent_code is not null and costcent_code != '') ".
							 "			 ) Q2 on (($Module_CostCenterParent=1 and Deliveries.costcent_code like concat(Q2.costcent_code,'%')) or ($Module_CostCenterParent=0 and Deliveries.costcent_code = Q2.costcent_code )) ";							 
			


			$query=$query." Where 1=1    ";
	

if($ledgerinclude== 1 && $ledgerexist > 0) {
	$query = $query." And Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"              where ledgerstk.user_account='".$useraccount1."' ".
								"              and (ledger_number is not null and ledger_number != '') ".
								"              and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"            ) ";
}
if($ledgerinclude== 0 && $ledgerexist > 0){
	$query = $query." And Not Exists ( select 1 from ".$dbLabel.".ledgerstk ".
								"                  where ledgerstk.user_account='".$useraccount1."' ".
								"          	       and (ledger_number is not null and ledger_number != '') ".
								"                  and Deliveries.Client_Code like concat(ledgerstk.ledger_number,'%') ".
								"                ) ";	
}
	
	$query = $query." and Deliveries.Journal_Type = 'DI' ";
if($vers==1 and $keyword!='')
     $query = $query." and ( Deliveries.delivery_reference = '$keyword'  
							
							)";
if($statusfrom != '') $query = $query."and delivery_deliverystatus.status_id >= ".($statusfrom-1)." ";
if($statusto != '') $query = $query."and delivery_deliverystatus.status_id <= $statusto ";
		
	$query = $query ."order by delivery_dateord desc ";
	//echo $query; exit;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $row=mysql_fetch_array($result);
	$deliveryid = $row[0];
	if($deliveryid !=''){
/* echo"
		
		 <input type='hidden' name='SelectedRec' id='SelectedRec' value='$SelectedRec'>
		 <input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value='$SelectedRecColor'>
		 <input type='hidden' name='ModRec_G1' id='ModRec_G1' value='$ModRec_G1'>		
		
		 "; */
	return $deliveryid;
	
	} else return 0;
}

?>