<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);



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");
	
	if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
		mysql_query("SET NAMES 'utf8'"); 
		mysql_query('SET CHARACTER SET utf8'); 
		mysql_set_charset('utf8', $connection);
	}
	
	if ($form == 'DI') {
		ShowDeliveryDtGrid($connection,$form,$transid,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],$dftorderby);
	}	
	
	
	
}


function ShowDeliveryDtGrid($connection,$form,$Manifest_Auto,$dftorderby){
		
		$sub_query = "SELECT currency_code FROM Temp_Deliveries WHERE Delivery_ID = '".$PSaId."'";
		$sub_result = selectData($connection, $sub_query);
	
		$querycntr = "select type_code from containertype order by type_code";						
		

	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid_Manifest.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;
	
	$columnsarrayall = array("container", "txtcntrtype", "marks", "package","txtdes","weight","measurement", "bctc_remark");
	
	$orderarray = implode(',', $columnsarrayall);
	$i=0;
	
	
	$TableWidthStyle = 'width:1075px;';
	
	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='1' align='left' id='grid_datatable' class='grid_data'  cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	echo "<col width=20><col width=20><col width=40><col width=60><col width=175><col width=50><col width=160>
	<col width=60><col width=200><col width=90><col width=90><col width=150><col width=20>";

		echo"<tr height='25'>";
		echo"  <th  align='center'  ><span>&nbsp;</span></th>";
		echo"  <th  align='center'  ><span>&nbsp;</span></th>";
		echo"  <th  align='center'  ><span>SEQ</span></th> ";	
		echo"  <th  align='center'  ><span>Type</span></th> ";
		echo"  <th  align='center'  ><span>Container Package/flag</span></th>";
		echo"  <th  align='center'  ><span>size</span></th>";
		echo"  <th  align='center'  ><span>Marksand nos of goods</span></th> ";	
		echo"  <th  align='center'  ><span>Packages</span></th> ";	
		echo"  <th  align='center'  ><span>description</span></th> ";	
		echo"  <th  align='center'  ><span>gross weight</span></th>";
		echo"  <th  align='center'  ><span>measurement</span></th> ";	
		echo"  <th  align='center'  ><span>BCTC Remark</span></th> ";
		echo"  <th  align='center'  ><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class='grid_contentDiv' style='overflow-x:scroll;' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='1' align='left' ";
		echo "id='grid_datatable' class='grid_data' ";
		echo " cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		
		echo "<col width=20><col width=20><col width=40><col width=60><col width=175><col width=50><col width=160>
			<col width=60><col width=200><col width=90><col width=90><col width=150><col width=20>";
		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 ($Manifest_Auto != '' ) {
		
			$j=1;
					   
			$query = "select Voyage_year, voyage_number, voyage_destination, voyage_BOLnumber, voyage_descripiton, BOL_type, BOL_pagenumber
					from manifest  
					where manifest_auto='$Manifest_Auto' ";
	
			$result=mysql_query($query,$connection);
			$row=mysql_fetch_row($result);
				$Voyage_year		= $row[0];
				$voyage_number		= $row[1];
				$voyage_destination	= $row[2];
				$voyage_BOLnumber	= $row[3];
				$voyage_descripiton	= $row[4];
				$BOL_type			= $row[5];
				$BOL_pagenumber		= $row[6];
		$keyPress = " onKeyPress=\"return submitenter(this,event);\" ";	
		if($Manifest_Auto != ''){	
			$query = " select Container, Marks, Packages, Container_Weight, CubeMeters,manifestdt.auto, descriptiongood, manifestdt.detailsequence,ctrflag, bctc_remark,
					   detailtype, ifnull(container_type,type_id)
					   from manifestdt
					   inner join manifestdesc on manifestdt.manifest_manifestauto=manifestdesc.manifest_manifestauto
					   and	manifestdesc.detailsequence=manifestdt.detailsequence
					   left join container on cntr_code = manifestdt.Container
					   where manifestdt.manifest_manifestauto='$Manifest_Auto'
					   group by manifestdt.auto";
			
			
			//echo $query;
		
			$resultdes=mysql_query($desc,$connection);
		
			
			//echo $query;
			$resultdt=mysql_query($query,$connection);
			
				while($row=mysql_fetch_array($resultdt)){
					$stylecontainer='';
					
					
					$selectcontainer="select count(cntr_code) from container where cntr_code='$row[0]'";
				//	echo $selectcontainer;
					$resultcontainer=mysql_query($selectcontainer,$connection);
					if($rowcont=mysql_fetch_array($resultcontainer)){
					if($rowcont[0]<1)
					$stylecontainer="font-weight:bold;font-style:italic;";
				
					}
					
			$columnsarrayall = array("txtseqdt", "txtDetailType", "container", "txtcntrtype", "marks", "package","txtdes","weight","measurement", "bctc_remark");
			$orderarray = implode(',', $columnsarrayall);
			
			$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$orderarray.'\',\'imgSubmit1['.$i.']\')"';
			
		echo "<tr height=20>
			<td  align='center'>$j</td>
			<td  align='center'><img src='img/delete.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"DeleteRecord('$row[5]');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
		echo "<td><input type='text' name='txtseqdt[$i]' id='txtseqdt[$i]' value='$row[7]' class='".$ClassI."' style='font-size:12px; width:38px; height:20px;'  ".$KeyDown." ".$keyPress.">		</td>";
		echo "<td align='center' ><select name='txtDetailType[$i]' id='txtDetailType[$i]' class='".$ClassI."' style='font-size:12px; width:55px; height:20px;' ".$KeyDown." ".$keyPress.">
					<option value='1' ".($row[10] == '1'?'selected':'').">Container</option>
					<option value='2' ".($row[10] == '2'?'selected':'').">Seal</option>
					<option value='3' ".($row[10] == '3'?'selected':'').">Size</option>
					<option value='4' ".($row[10] == '4'?'selected':'').">Full/Empty</option>
					<option value='5' ".($row[10] == '5'?'selected':'').">Desc.</option>
				 </select></td>";	
		echo "<td align='LEFT'>&nbsp;&nbsp;&nbsp;
			<input type='text'   name='txtcontainer[$i]'  id='container[$i]' value='".htmlspecialchars($row[0], ENT_QUOTES)."'    class='".$ClassI."' style='font-size:12px; width:122px; height:20px; $stylecontainer'  
				onchange=\"checkContainer('$i', '1');getSize('$i','c');\"  ".$KeyDown." ".$keyPress.">";
										
									
			echo "<input type=hidden name='flag[$i]' id='flag[$i]' value='$row[8]'>";
			if ($row[8]!='*'){
				echo "<input type='image' src='img/btnopen.jpg' title='Select container ' width='15' height='17 border='0' onMouseOver=\"style.cursor='pointer'\" onclick=\"opencontainer('$i'); return false\" style='cursor: pointer;'>";
	
				echo "<img src='img/check.png' onmouseover=\"style.cursor='hand'\"   style='width:15px; height: 15px; '>";
			}
			ELSE				
				{
					echo "<img src='img/blockcheck.png' onmouseover=\"style.cursor='hand'\"   style='width:15px; height: 15px; '>";
				}
			echo "</td>";
			echo "<td align='center'><select type='text' name='txtcntrtype[$i]' id='txtcntrtype[$i]' onchange=\"getSize('$i','s');\" class='".$ClassI."' style='font-size:12px; width:48px; height:20px;'  ".$KeyDown." ".$keyPress.">";
			echo "		<option value = ''></option>";
			$rescntr = mysql_query($querycntr, $connection);
			while($rwcntr = mysql_fetch_array($rescntr)){
				echo "	<option value = '".htmlspecialchars($rwcntr[0], ENT_QUOTES)."' ".(htmlspecialchars($rwcntr[0], ENT_QUOTES)==htmlspecialchars($row[11], ENT_QUOTES)?'selected':'').">$rwcntr[0]</option>";
			}	
			echo "</select></td>";
			echo "<td align='center'><input type='text'   name='txtmarks[$i]'  id='marks[$i]' value='$row[1]'   class='".$ClassI."' style='font-size:12px; width:145px; height:20px;'  
						 ".$KeyDown." ".$keyPress.">
				</td>
			<td align='center'><input type='text'   name='txtpackage[$i]'  id='package[$i]' value='$row[2]'  class='".$ClassI."' style='font-size:12px; width:55px; height:20px;'  
				 ".$KeyDown." ".$keyPress.">";
			echo "</td>";
						
			echo "<td align=center><input type='text'   name='txtdes[$i]'  id='txtdes[$i]' value='".$row[6]."'    class='".$ClassI."' style='width:195px'	".$KeyDown." ".$keyPress."></td>";
			echo "<td align='center'>";
			echo "<input type='text'   name='txtweight[$i]'  id='weight[$i]' value='$row[3]'   class='".$ClassI."' style='font-size:12px; width:85px; height:20px;'  
					  ".$KeyDown." ".$keyPress.">";
			echo "</td>		
				<td align='center'><input type='hidden' name='Mandt_cont_id[$i]' value='$row[7]' id='Mandt_cont_id[$i]'> 
				<input type='text'   name='txtmeasurement[$i]'  id='measurement[$i]' value='$row[4]'  class='".$ClassI."' style='font-size:12px; width:85px; height:20px;'  
						".$KeyDown." ".$keyPress."></td>
				<td align='center'>";
			echo "<input type='text'  name='bctc_remark[$i]'  id='bctc_remark[$i]' value='$row[9]'   class='".$ClassI."' 	style='font-size:12px; width:150px; height:20px;'  
					".$KeyDown." ".$keyPress."></td>		
				<td align='center'>
				<input type='button' id='imgSubmit1[$i]' onclick=\"SubmitRecordGrid('".$i."','$row[5]','$row[7]'); 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></tr>";
				
		

				$i+=1;
				
			$j++;
			}
			
		}
		//}
		
	
		$recno = $i+1;
		$mode = 'i';
		
		if ($i%2==0) $bgcolor='bgcolor1';
		else $bgcolor='bgcolor2';	
		
		

		$KeyDown   = 'onkeydown="return dokey(event,this, '.$i.',\''.$orderarray.'\',\'imgSubmit1['.$i.']\');"';
		echo "<tr><td  align='center'></td>
				<td  align='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordGrid('".$i."');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>";
		echo "<td><input type='text' name='txtseqdt[$i]' id='txtseqdt[$i]' value='$txtseqdt[$i]' class='".$ClassI."' style='font-size:12px; width:38px; height:20px;' ".$KeyDown." ".$keyPress." readonly></td>";		
		echo "<td align='center' ><select name='txtDetailType[$i]' id='txtDetailType[$i]' class='".$ClassI."' style='font-size:12px; width:55px; height:20px;' ".$KeyDown." ".$keyPress.">
					<option value='1'>Container</option>
					<option value='2'>Seal</option>
					<option value='3'>Size</option>
					<option value='4'>Full/Empty</option>
					<option value='5'>Desc.</option>
				 </select></td>";	
		echo "<td align='left'>&nbsp;&nbsp;&nbsp;
				<input type='text'   name='txtcontainer[$i]'  id='container[$i]' value='".htmlspecialchars($container[$i], ENT_QUOTES)."'     class='".$ClassI."' style='font-size:12px; width:122px; height:20px;'  
					 ".$KeyDown." ".$keyPress." onchange=\"checkContainer('$i', '1');getSize('$i','c');\" >";
		 echo "<input type='image' src='img/btnopen.jpg' title='Select container ' width='15' height='17 border='0' onmouseover=\"style.cursor='hand'\" onclick=\"opencontainer('$i'); return false\" style='cursor: pointer;'>";
		echo "	<input type=checkbox name='flag[$i]' id='flag[$i]' onclick=\"checkContainer('$i','2');\" ></td>";
		
		
		echo "<td align='center' ><select type='text' name='txtcntrtype[$i]' id='txtcntrtype[$i]' class='".$ClassI."' onchange=\"getSize('$i','s');\" style='font-size:12px; width:48px; height:20px;' ".$KeyDown." ".$keyPress.">";
		echo "		<option value = ''></option>";
		$rescntr = mysql_query($querycntr, $connection);
		while($rwcntr = mysql_fetch_array($rescntr)){
			echo "	<option value = '".htmlspecialchars($rwcntr[0], ENT_QUOTES)."'>$rwcntr[0]</option>";
		}		
		echo "</select></td>";	
		echo "<td align='center'>
			<input type='text'   name='txtmarks[$i]'  id='marks[$i]'	value='$txtmarks[$i]'   class='".$ClassI."' 	style='font-size:12px; width:145px; height:20px;' 
			 ".$KeyDown." ".$keyPress.">
			</td><td align='center'>
			<input type='text'   name='txtpackage[$i]'  id='package[$i]' value='$txtpackage[$i]'   class='".$ClassI."' 	style='font-size:12px; width:55px; height:20px;' 
				 ".$KeyDown." ".$keyPress.">
			</td>
			<td align='center'>
			<input type='text'   name='txtdes[$i]'  id='txtdes[$i]' value='".htmlspecialchars($txtdes[$i], ENT_QUOTES)."'   class='".$ClassI."' style='width:195px'
				".$KeyDown." ".$keyPress."></td>
			<td align='center'>	<input type='text'   name='txtweight[$i]'  id='weight[$i]'	value='$txtweight[$i]'   class='".$ClassI."' style='font-size:12px; width:85px; height:20px;'  
					".$KeyDown." ".$keyPress.">
			<label><span>";
			
		echo "</td>	<td align='center'>";
		echo "<input type='text'  name='txtmeasurement[$i]'  id='measurement[$i]' value='$txtmeasurement[$i]'   class='".$ClassI."' 	style='font-size:12px; width:85px; height:20px;'  
				 ".$KeyDown." ".$keyPress.">
		</td> <td align='center'>";
		echo "<input type='text'  name='bctc_remark[$i]'  id='bctc_remark[$i]' value='$bctc_remark[$i]'   class='".$ClassI."' 	style='font-size:12px; width:150px; height:20px;'  
				 ".$KeyDown." ".$keyPress."></td>
		<td align='center'>
		<input type='button' id='imgSubmit1[$i]' onclick=\"SubmitRecordGrid('".$i."','','".$sequence."'); 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>
		</tr>";
			
	
		
 

		$i+=1;	
		

	echo"	</table>";  
	echo"	</div>";
	$querysum = " select count(Container)
			   from manifestdt
			   inner join manifestdesc on manifestdt.manifest_manifestauto=manifestdesc.manifest_manifestauto
			   and	manifestdesc.detailsequence=manifestdt.detailsequence
			   where manifestdt.manifest_manifestauto='$Manifest_Auto' and ifnull(ctrflag,'') != '*'
			   group by manifestdt.manifest_manifestauto"; 
	$resum = mysql_query($querysum, $connection);		   
	$rwsum = mysql_fetch_row($resum);
	$numr = $rwsum[0];
	
	$querysum = " select sum(ifnull(Packages,0)), sum(ifnull(replace(Container_Weight,',','.'),0))
			   from manifestdt
			   inner join manifestdesc on manifestdt.manifest_manifestauto=manifestdesc.manifest_manifestauto
			   and	manifestdesc.detailsequence=manifestdt.detailsequence
			   where manifestdt.manifest_manifestauto='$Manifest_Auto' 
			   group by manifestdt.manifest_manifestauto"; //echo $querysum;
	$resum = mysql_query($querysum, $connection);		   
	$rwsum = mysql_fetch_row($resum);
	$sumPkg = $rwsum[0];
	$sumWeight = $rwsum[1];
	$sumWeight = str_replace('.',',', str_replace(',','',number_format($sumWeight,2)));
	
	echo" 	<div id='grid_headerDiv' class='grid_headerDiv'>";
	echo"  	<table border='1' align='left' id='grid_datatable' class='grid_data'  cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	echo "<col width=20><col width=20><col width=50><col width=175><col width=160>
	<col width=60><col width=200><col width=90><col width=90><col width=190><col width=20>";
	echo "<tr height='20px'> 
			<td colspan='5'><b>Total Container: $numr</b></td> 
			<td colspan='3'><b>Total Packages: $sumPkg</b></td> 
			<td colspan='3'><b>Total Weight: $sumWeight</b></td>
		</tr>";
	echo "</table></div>";
	//showDataGridFooter("Forms_DataGrid_Manifest.php",$form,$Manifest_Auto,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
		showDataGridFooter($filepath,$form,$PSaId,'datagrid',$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction);
	
	echo"</div>";
	
}


function ShowitemGrid($connection,$form,$Manifest_Auto,$dftorderby,$action,$orderby,$morelines,$startrange,$perpage,$totalrows,$direction){
	//ECHO "XXX";
	
	$query_setup="select ifnull(delivery_maxrecords,1000000) from setup";
	$result_setup=mysql_query($query_setup,$connection);
	$row_numofrows=mysql_fetch_row($result_setup);
	$numofrows = $row_numofrows[0];
	
	$querycur = "select currency_code from currency order by currency_code ";
		
	
	//Initializations
	
	$ClassE = 'grid_roundedtext1';
	$ClassI = 'grid_roundedtext2';
	
	$filepath = 'Forms_DataGrid_Manifest.php';
	
	$i = 0;
	
	$columnslistall = implode(',', $columnsarrayall);
	
	$columnsarrayall = array("itemcode", "itemname", "description","amount","curr","quantity","discount");
	//array_push($columnsarrayhead, 'txtSalesmanName');
	$orderarray = implode(',', $columnsarrayall);
	$i=0;
	
	
	
	$TableWidthStyle = 'width:100%;';
	
	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='1' align='left' ";
	echo "id='grid_datatable' class='grid_data' ";
	echo "cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
	echo "<col width=25><col width=25><col width=150><col width=100><col width=230><col width=150><col width=50px><col width=80><col width=120><col width=25>";

		echo"<tr height='25'>";
		echo"  <th  align='center'  ><span>&nbsp;</span></th>";
		echo"  <th  align='center'  ><span>&nbsp;</span></th>";
		echo"  <th  align='center'  ><span>Item Code</span></th>";
		// onclick=\"ReloadGrid('".$filepath."', '".$form."', '".$PSaId."', 'datagrid', 1, '".$dftorderby."', 'COR','Items.Item_Description','".$morelines."','".$startrange."','".$perpage."','".$totalrows."','".$direction."');\"
		echo"  <th  align='center'  ><span>Item Name</span></th> ";	
		echo"  <th  align='center'  ><span>Description</span></th> ";	
		echo"  <th  align='center'  ><span> Amount</span></th>";
		echo"  <th  align='center'  ><span> Currency </span></th>";
		echo"  <th  align='center'  ><span>Quantity</span></th> ";	
		echo"  <th  align='center'  ><span>Rebate</span></th>";	
		
		echo"  <th  align='center'  ><span>&nbsp;</span></th>";
		echo"</tr>";
		echo"</table></div>";
		
		echo"<div id='grid_contentDiv' class=' itemgrid' onscroll=\"ScrollHeader('grid_headerDiv','grid_contentDiv')\">";
		echo"<table border='1' align='left' ";
		echo "id='grid_datatable' class='grid_data' ";
		echo " cellpadding='0' cellspacing='1' style='$TableWidthStyle'>";
		
	echo "<col width=25><col width=25><col width=150><col width=100><col width=230><col width=150><col width=50px><col width=80><col width=120><col width=25>";

		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'>";  
		$l=1;
			 $query = "select 
				BOL_identification
				from manifest  
				where manifest_auto='$Manifest_Auto' ";
				$resultdt=mysql_query($query,$connection);
				if($row=mysql_fetch_array($resultdt)){
					$BOL_identification=$row[0];
				}
						
			$query = "select manifestdtl_id, tax_description, item_name,DESCRIPTION, tax_amount, quantity , discount, currencycode
					from manifestdtl
					where manifest_manifestauto='$Manifest_Auto'";
			$resultdt=mysql_query($query,$connection);
				while($row=mysql_fetch_array($resultdt)){
					$KeyDown   = 'onkeydown="return dokey(event,this,'.$i.',\''.$orderarray.'\',\'imgSubmit['.$i.']\')"';
echo "<tr>
			<td  align='center'>$l</td>
			<td  align='center'><img src='img/delete.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" 
			onclick=\"DeleteRecorditem('$row[0]');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>
			<td align='center'>";
						
			echo "<select style='width:140px' name='txtitemcode[$i]'  id='itemcode[$i]' 
			onKeyPress='return submitenter(this,event)' ";
print <<<HERE

			onchange="selectitemname(this,'$i')"
HERE;
ECHO 	$KeyDown." >";
				$query=" select item_code ,item_description from items where SubGroup_code !='TARROS'";
				//echo $query;
				//exit;
				$res=mysql_query($query,$connection);
				echo "<option value=''></option>";
				while($rows=mysql_fetch_array($res)){
					
					echo "<option value='$rows[0]'  id='$rows[1].$i'";
				
				if ($rows[0]==$row[1]){echo "selected";}
				echo ">$rows[0]</option>";
				}
				echo "</select>";
					
			echo "</td>
			<td align='center'>
				<input type='text'   name='txtitemname[$i]'  id='itemname[$i]' 	value='$row[2]'    class='".$ClassI."' 	style='font-size:12px; width:90px; height:20px;'  
					onKeyPress='return submitenter(this,event)' ".$KeyDown.">
				</td>
			<td align='center'>
				<input type='text'   name='txtdescription[$i]'  id='description[$i]' value='$row[3]'   class='".$ClassI."'  style='font-size:12px; width:245px; height:20px;' 
				onKeyPress='return submitenter(this,event)'	".$KeyDown.">
					
			</td>
			
			<td align='center'>
				<input type='text'   name='txtamount[$i]'  id='amount[$i]'	value='$row[4]'  class='".$ClassI."'  style='font-size:12px; width:120px; height:20px;' 
				onKeyPress='return submitenter(this,event)'	".$KeyDown.">
					
			</td>
			<td align='center'>
				<select  name='txtcurr[$i]'  id='curr[$i]' 	 class='".$ClassI."'  style='font-size:12px; width:50px; height:20px;' onKeyPress='return submitenter(this,event)'		".$KeyDown.">
			";
			echo "<option value='' ></option>";
			$rescur  = mysql_query($querycur, $connection);
			while($rwcur = mysql_fetch_array($rescur)){
				echo "<option value='$rwcur[0]' ".($rwcur[0] == $row[7]?'selected':'').">$rwcur[0]</option>";
			}
			echo "</select></td>
			<td align='center'><input type='hidden' name='Mandt_cont_id[$i]' value='$txtMandt_cont_id' id='Mandt_cont_id[$i]'> 
			<input type='text'   name='txtquantity[$i]'  id='quantity[$i]' 	value='$row[5]'    class='".$ClassI."' 	style='font-size:12px; width:75px; height:20px;'  onKeyPress='return submitenter(this,event)' ".$KeyDown.">
				
			</td>
			
			<td align='center'>
				<input type='text'   name='txtdiscount[$i]'  id='discount[$i]' value='$row[6]'  class='".$ClassI."' style='font-size:12px; width:120px; height:20px;' onKeyPress='return submitenter(this,event)'
				 ".$KeyDown.">
			
				</td>
			<td align='center'>
			<input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecorditemGrid('".$i."','$row[0]'); 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>
			</tr>";
				
		

				$i+=1;
				$l++;
			
			}
			
			
		
		
		if($i>=$numofrows)
		echo"<tr><td colspan=4>Maximum No of rows </td></tr>";
		//	$k=2;
		//else
		
		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.',\''.$orderarray.'\',\'imgSubmit['.$i.']\')"';
			echo "<tr>
			<td  align='center'></td>
			<td  align='center'><img src='img/undo-1.png' width='20' height='20' border='0' onmouseover=\"style.cursor='pointer'\" onclick=\"ClearRecordItemGrid('".$i."');return false;\" title='Clear Record' style='vertical-align:bottom;'></td>
			
			<td align='center'>";
			echo "<select style='width:140px' name='txtitemcode[$i]'  id='itemcode[$i]' ".$KeyDown."   onKeyPress='return submitenter(this,event)'";
				print <<<HERE

			onchange="selectitemname(this,'$i')"
HERE;
			ECHO "	>";
			$query="select item_code,ITEM_DESCRIPTION from items where subgroup_code !='TARROS'";
			
			
			$res=mysql_query($query,$connection);
			echo "<option value=''></option>";
			while($rows=mysql_fetch_array($res)){
				echo "<option value='$rows[0]'";
			
		//	if ($rows[0]==$row[1])echo "selected";
		
		
			echo "  id='$rows[1]' >$rows[0]</option>";
			}
			echo "</select>";
//echo $query;											
			echo "</td>
			<td align='center'>
			<input type='text'   name='txtitemname[$i]'  id='itemname[$i]' value='$txtitemname[$i]'   class='".$ClassI."' style='font-size:12px; width:90px; height:20px;'  
			onKeyPress='return submitenter(this,event)'	 ".$KeyDown.">
				</td>
			<td align='center'>
			<input type='text'   name='txtdescription[$i]'  id='description[$i]' value='$txtdescription[$i]'   class='".$ClassI."'  onKeyPress='return submitenter(this,event)' 
			style='font-size:12px; width:245px; height:20px;'  ".$KeyDown.">
					
			</td>
			
			<td align='center'>
				<input type='text'   name='txtamount[$i]'  id='amount[$i]' 	value='$txtamount[$i]'   class='".$ClassI."'  style='font-size:12px; width:120px; height:20px;' 
				onKeyPress='return submitenter(this,event)'		".$KeyDown.">
			</td>
			<td align='center'>
				<select  name='txtcurr[$i]'  id='curr[$i]' 	value='$txtcurr[$i]'  class='".$ClassI."'  style='font-size:12px; width:50px; height:20px;' onKeyPress='return submitenter(this,event)'		".$KeyDown.">
			";
			echo "<option value='' ></option>";
			$rescur  = mysql_query($querycur, $connection);
			while($rwcur = mysql_fetch_array($rescur)){
				echo "<option value='$rwcur[0]' >$rwcur[0]</option>";
			}
			echo "</select></td>
			<td align='center'><input type='hidden' name='Mandt_cont_id[$i]' value='$txtMandt_cont_id' id='Mandt_cont_id[$i]'> 
			<input type='text'   name='txtquantity[$i]'  id='quantity[$i]'	value='1'  class='".$ClassI."' 	style='font-size:12px; width:75px; height:20px;'  
				onKeyPress='return submitenter(this,event)'	 ".$KeyDown.">
				
			</td>
			<td align='center'>
			<input type='text'   name='txtdiscount[$i]'  id='discount[$i]'	value='0'   class='".$ClassI."' 	style='font-size:12px; width:115px; height:20px;'  
			onchange=\"SetModRecord('ModRec_G1','$i'); 	CheckLookup('chkcodeexists','items','item_code',this.value,'lstcontainerCode[$i]'); 
			GetSelectedItem(this.id,'getitem','".$i."','".$txtDate."','".$txtCurrCode."','".$txtClientCode."');
			GetSelectedItemQty('getitemqty','".$i."','".$txtDate."');\" onkeyup=\"AutoCompleteData(event,this.id,'itemcode[$i]','itemdescription[$i]','items');\" 	onKeyPress='return submitenter(this,event)' ".$KeyDown.">
				
				</td>
			<td align='center'>
			<input type='button' id='imgSubmit[$i]' onclick=\"SubmitRecorditemGrid('".$i."','$row[0]'); 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>
		
			</tr>";
				
		
			
	

			$i+=1;	
		}

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





function showDataGridFooter($filepath,$form,$Manifest_Auto,$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('".$Manifest_Auto."');\">";
				  
				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> $perpage</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('".$perpage."');return false;\" >";
				//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('".$perpage."');return false;\" >";
					//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('".$perpage."');return false;\" onmouseover=\"style.cursor='pointer'\" >";
						echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='pointer'\" onclick=\"ReloadGrid('".$perpage."');return false;\" >";	
						/* 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."');re turn false;\" >";	*/
					}
				} else
					echo "&nbsp;";
			
				
				$currentpage = ($startrange / $perpage) + 1;
				$totalpages  = ceil($totalrows / $perpage);

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

}



function Showtotalamount($connection,$Manifest_Auto){
	
	
$querysetup = "select decimalbase from setup";
$ressetup = mysql_query($querysetup, $connection);	
$rwsetup = mysql_fetch_row($ressetup);
$decimalbase = $rwsetup[0];

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

$tarrosamoun=" select ifnull(replace(tax1_amount,',','.'),0), ifnull(replace(tax2_amount,',','.'),0), ifnull(replace(tax3_amount,',','.'),0), 
				ifnull(replace(tax4_amount,',','.'),0), ifnull(replace(tax5_amount,',','.'),0), ifnull(replace(tax6_amount,',','.'),0), 
				tax1_desc, tax2_desc, tax3_desc, tax4_desc, tax5_desc, tax6_desc, currency, Manifest_sysdate 
			   from manifest where manifest_auto='$Manifest_Auto'";
$queryres=mysql_query($tarrosamoun,$connection);
//echo $tarrosamoun;
if($row=mysql_fetch_array($queryres)){
	
	$tax1_amount = $row[0];
	if($tax1_amount == '') $tax1_amount = 0;
	$tax2_amount = $row[1];
	if($tax2_amount == '') $tax2_amount = 0;
	$tax3_amount = $row[2];
	if($tax3_amount == '') $tax3_amount = 0;
	$tax4_amount = $row[3]; 
	if($tax4_amount == '') $tax4_amount = 0;
	$tax5_amount = $row[4];
	if($tax5_amount == '') $tax5_amount = 0;
	$tax6_amount = $row[5];
	if($tax6_amount == '') $tax6_amount = 0;
	
	$currency = $row[12];
	$systdate = $row[13];
	$tarrosamnt=$tax1_amount+$tax2_amount+$tax3_amount+$tax4_amount+$tax5_amount+$tax6_amount;
	
	$tarrosvat = ($tax1_amount*getVAT($row[6], $connection)/100) + ($tax2_amount*getVAT($row[7], $connection)/100) + ($tax3_amount*getVAT($row[8], $connection)/100) + 
				 ($tax4_amount*getVAT($row[9], $connection)/100) + ($tax5_amount*getVAT($row[10], $connection)/100) + ($tax6_amount*getVAT($row[11], $connection)/100);
}


$amountquery="select (tax_amount * quantity), currencycode from manifestdtl where manifest_manifestauto='$Manifest_Auto'";
$queryres=mysql_query($amountquery,$connection);
//echo $amountquery;
$totalamount = 0;
while($row=mysql_fetch_array($queryres)){
	
	$totalamount = $totalamount + ConvertCurrency($row[1], $currency, $systdate, $row[0], $connection);
	
}


$vatamount=0;
$netamount=0;			
$discountamount=0;			
$vatamountquery="select (tax_amount*quantity - discount)*item_vat/100 as vatamount, currencycode from items
				inner join manifestdtl on manifestdtl.tax_description=items.item_code where manifest_manifestauto='$Manifest_Auto'";
$queryvatres=mysql_query($vatamountquery,$connection);

while($rows=mysql_fetch_array($queryvatres)){
	$vatamount=$vatamount + ConvertCurrency($rows[1], $currency, $systdate, $rows[0], $connection);
	
}

$vatamount = $vatamount + $tarrosvat;


$discountquery="select discount, currencycode from manifestdtl where manifest_manifestauto='$Manifest_Auto'";
$querydis=mysql_query($discountquery,$connection);

while($rows=mysql_fetch_array($querydis)){
	$discountamount=$discountamount + ConvertCurrency($rows[1], $currency, $systdate, $rows[0], $connection);
}
$netamount = $totalamount + $vatamount - $discountamount;
echo "</td>
	<td><b>total Amount:</b></td><td> ".number_format($totalamount, $decimalbase)."</td><td>&nbsp;&nbsp;</td>
	<td><b>Vat Amount:</b></td><td>".number_format($vatamount, $decimalbase)."</td><td>&nbsp;&nbsp;</td>
	<td><b><font color='red' >total discount:</font></b></td><td><font color='red' >".number_format($discountamount, $decimalbase)."</font></td><td>&nbsp;&nbsp;</td>
	<td><b>Tarros Amount:</b></td><td>".number_format($tarrosamnt, $decimalbase)."</td><td>&nbsp;&nbsp;</td>
	<td><b>Net Amount:</b></td><td>";
$net=$tarrosamnt+$netamount;
echo number_format($net, $decimalbase)."</td><td>&nbsp;&nbsp;</td>

</tr></table>";
	
}

function getVAT($item_code, $connection){
	$query = "select ifnull(item_vat,0) from items where item_code='$item_code';";
	$res = mysql_query($query, $connection);
	$rw = mysql_fetch_row($res);
	$vat = $rw[0];
	if($vat == '') $vat = 0;
	return $vat;
}

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

}
?>