<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


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

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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}
$fromdate   =$_POST["fromdate"];
$todate     =$_POST["todate"];

$querydate="select search_startingdate from setup";
$resultdate=mysql_query($querydate,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdate)){
	extract($rows);
}	
$MainCompanyName=explode('_',$_SESSION['dbName']); 
$QueryDir = " Select Date_Format(StartFinancialDate,'%d/%m/%Y') 
			From ".$_SESSION['dbLabel'].".Directories ". 	
			" Where  Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			" and Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
//echo $QueryDir;			
$ResultDir=mysql_query($QueryDir,$connection) or die(mysql_error());   
$RowsDir=mysql_fetch_row($ResultDir);
$StartFinancialDate = $RowsDir[0];


if ($fromdate == '') {
if ($search_startingdate == '-1') {
	$fromdate = $StartFinancialDate;
}

else if($search_startingdate == '0') {
	$fromdate = date('01/m/Y');
}

else if($querydate > '0') {
	$added_timestamp = strtotime('-'.$search_startingdate.' month', time());

	$fromdate = date('01/m/Y', $added_timestamp); 
}
}

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

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


$loadPage = $_GET['loadPage'];
$tlId = $_GET['tlId'];
	

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Search Transfer</title>
<script type="text/javascript" language="javascript">

function loadRecord(tldtId){
	opener.document.wayblog.ltdtId.value = tldtId;
	opener.document.wayblog.modedt.value = 'loadFromTransport';
	opener.document.wayblog.submit();
	self.close();
}

function searchPage(){
	document.search.action='Form_LoadTransportLogistic.php';
	document.search.submit();
}

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


function set_limit(){
	perpage=document.search.perpage.value;
//	document.search.action='Form_LoadTransportLogistic.php?perpage='+perpage;
	document.search.submit();
}

function clearform()
{
    document.search.fromdate.value = '';
	document.search.todate.value = '';
  
  if (document.search.fromproject)
	document.search.fromproject.value = '';
    
  document.search.action='Form_LoadTransportLogistic.php';
  document.search.submit();

}


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

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

function SubmitForm()
{	
  
 
  //document.search.keyword.value='';
  document.search.action='Form_LoadTransportLogistic.php?startrange=0';
  document.search.submit();

}

function RefreshPage(perpage)
{
  
  //document.search.keyword.value='';
  document.search.action='Form_LoadTransportLogistic.php?startrange=0&perpage='+perpage;
  document.search.submit();

}

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

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

  document.search.submit();

}


function showlistProject(str){

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

</script>

<style type="text/css">

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

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 9px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

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

</head>
<body onload="loadTransport('$loadPage')" class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='mainmode' value=''>
<input type='hidden' name='olId' value=''>
<input type='hidden' name='tlId' value='$tlId'>
HERE;

$PRef = GetSQLValueString($keyword, 'text');
		
$querysearch=" select logistic_transportdt.LTdt_id as LTdt_id, date_format(LT_Date,'%d-%m-%Y') as LTDate, LTdt_ContainerNo, LT_ProjectCode, project_name, LTdt_ContainerSize, LTdt_Status
			from logistic_transportdt
			inner join logistic_transport on logistic_transport.LT_Id = logistic_transportdt.LT_Id
			inner join project on project_code = LT_ProjectCode 
			left join Logistic_Waybill on Logistic_Waybill.LTdt_id = logistic_transportdt.LTdt_id
			where if ('$PCode' = '', 1=1, LT_ProjectCode = '$PCode') and  date_format(LT_Date,'%Y-%m-%d') between '".explodeword($fromdate)."' and '".explodeword($todate)."'
			and ifnull(Logistic_Waybill.LTdt_id,-1) = -1
			order by LT_Date, LTdt_ContainerNo, LTdt_ContainerSize ";

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
$querysearch .= " limit $startrange, $perpage ";//echo $querysearch;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='850px'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; ' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Containers</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

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


echo"<table border='0' style='table-layout:fixed'>
	   <col width=20>
	  <col width=70>
	  <col width=120>
	  <col width=150>
	  <col width=150>
	  <col width=150>
	  <col width=150>
	  <col width=100>";

echo "	<tr height='30px' class='Search-header-bg'>";	  
echo" <td align='left' colspan='2'><span class='style4'>File</span></td>
	      <td align='left' colspan='3'><span class='style4'>
		    <input type='text' name='fromproject' id='fromproject' value='$PCode' size='15' maxlength='20' onkeydown=\"return kd(event)\" /></span>
		    <label><span class='style4'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick=\"showlistProject('1');\"  /></span></label>
			<button type='button' id='btnSubmit' name='btnSubmit' onclick=\"searchPage();return false;\" title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick=\"clearform();return false;\" title='Cancel'><span><span>Cancel</span></span></button>
			</td>";
echo" <td align='left' colspan='3'><span class='style4'>From&nbsp;</span><input type='text' name='fromdate' id='fromdate' value='$fromdate' maxlength='10' size='9' onkeydown=\"return kd(event)\">
		<A HREF=\"#\" onClick=\"displayCalendar(document.search.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
		&nbsp; &nbsp;<span class='style4'>To&nbsp;</span><input type='text' name='todate' id='todate' value='$todate' maxlength='10' size='9' onkeydown=\"return kd(event)\">
		<A HREF=\"#\" onClick=\"displayCalendar(document.search.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
		</td>
	</tr>";	
echo "</table>
		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>"; 
echo" </tr>";

echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:769px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage('$perpage');\">";
              
            $perpages = $config['trowlimit'];
			foreach ($perpages as $val){
			echo" <option value = '$val' "; if ($perpage ==$val) echo "selected"; 
			echo" >$val</option> ";
			}
	echo'	
				 </select></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

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

	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:800px;'>
			<table border='0'  cellpadding='0' cellspacing='1' style='width:800px;'>
			  <col width=20>
			  <col width=100>
			  <col width=120>
			  <col width=150>
			  <col width=200>
			  <col width=100>
			  <col width=100>";
			  
	if($message!=''){
		echo"<tr class='Search-header-bg'>
				<td height='15' align='center' colspan='8' class='searchmessage'>".$message."</td>
			</tr>";
	}	  
	
	echo"<tr class='Search-Label-bg'>
			<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>Date </span></td>
			<td  height='25' align='center'><span class='style4'>Container # </span></td>
			<td  height='25' align='center' colspan='2'><span class='style4'>File </span></td>	
			<td  height='25' align='center' ><span class='style4'>Container Size</span></td>
			<td  height='25' align='center'><span class='style4'>Status</span></td>
		</tr>";
		 

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

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
				
	echo'<tr '.$Color.' ';
	echo ">";
		
		
	echo'<td align="left"><span class="style5"><img  src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="loadRecord(\''.$LTdt_id.'\');" title="Load Record" style="width: 20px; height: 20px; " ></span></td>';
	
 
	echo'	<td align="left"><span class="style5">'.$LTDate.'</span></td>
			<td align="left"><span class="style5">'.$LTdt_ContainerNo.'</span></td>
			<td align="left"><span class="style5">'.$LT_ProjectCode.'</span></td>
			<td align="left"><span class="style5">'.$project_name.'</span></td>
			<td align="left"><span class="style5">'.$LTdt_ContainerSize.'</span></td>
			<td align="left"><span class="style5">'.$LTdt_Status.'</span></td>
		</tr>';
		
		
			
}
							

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

echo"   <tr ><td id='search-content1'>&nbsp;</td><td id='search-content2'>&nbsp;</td><td id='search-content3'>&nbsp;</td></tr>";
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:779px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";

		
		if($startrange>0)
			echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

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

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

print <<<HERE
</form>
</body>

HERE;
function explodeword($date)
{

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