<?php

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


include("config.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);
}



$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);
}

$query_login="select login_belongstogrp from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());

$row_login=mysql_fetch_row($resultlogin);
$belongstogroup=$row_login[0];

$crfttype = $_GET['type'];

if($crfttype == 'CRF') $title = 'CRF';
else if($crfttype == 'CT') $title = 'CT';

print <<<HERE
<head>
<link rel="stylesheet" href="default.css" type="text/css" />
<title>Search $crfttype</title>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<script type="text/javascript" language="javascript">
			

function clearform()
{
	var crfttype = document.search.crfttype.value;			
	document.search.PRef.value='';
	document.search.action='Form_CRFTSearch.php?type='+crfttype;
	document.search.submit();

}

function SetNextPage(startrange,perpage){
	var crfttype = document.search.crfttype.value;			
	document.search.action='Form_CRFTSearch.php?type='+crfttype+'&PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	var crfttype = document.search.crfttype.value;	
	document.search.action='Form_CRFTSearch.php?type='+crfttype+'&PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function set_limit(){
	var crfttype = document.search.crfttype.value;	
	perpage=document.search.perpage.value;
	document.search.action='Form_CRFTSearch.php?type='+crfttype+'&perpage='+perpage;
	document.search.submit();
}


function refreshSearch(){
	document.search.submit();
}

function printcrft (crft_id, ctype){ 
	var str = 'Form_CRFTPreview.php?crft_id='+crft_id+'&crfttype='+ctype;
	window.open(str);
}

function editcrft(crft_id, ctype){
	opener.document.fromcr.crft_id.value = crft_id;
	opener.document.fromcr.showinfo.value = 1;
	opener.document.fromcr.mode.value = 'editRecord';
	opener.document.fromcr.submit();
	self.close();
}

function deletecrft(crft_id, ctype, cref){
	if(confirm('Are You sure You want to Delete Record Reference: '+cref)){
		document.search.action = 'Form_CRFT_action.php?mode=deletecrft&crfttype='+ctype+'&crft_id='+crft_id;
		document.search.submit();
	}	
	
}

function ShowHideDetails(crftid){
	vSlideDivId = '#displaydetails'+crftid;
	$(vSlideDivId).slideToggle();
}
</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;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; }
.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>

</head>
<body >
<form name='search' id='search' method='post' action=''>
<input type='hidden' name=crfttype value='$crfttype'>
HERE;

if ($vOpt == '') $vOpt = '1';

echo"<table border='0' style='table-layout:fixed'>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=20>
	  <col width=90>
	  <col width=90>
	  <col width=100>
	  <col width=90>
	  <col width=120>
	  <col width=120>
	 		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='10'><span class='style16'>List of $crfttype</span></td></tr>
	  <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left' colspan='3'><span class='style11'>Search by</span></td>
		<td align='left' colspan=5><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='100'/>
			<select name='vOpt' id ='vOpt' >
				<option value='1' ".($vOpt == '1'?'selected':'').">Reference</option>
				<option value='2' ".($vOpt == '2'?'selected':'').">B\L No.</option>
				<option value='3' ".($vOpt == '3'?'selected':'').">VOY. No.</option>
				<option value='4' ".($vOpt == '4'?'selected':'').">Manifest Auto</option>	
				<option value='5' ".($vOpt == '5'?'selected':'').">Container</option>	
			</select>
			<input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		    <input type='button' name='button' value='Find' onclick=\"refreshSearch();\" style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='left' colspan='2' valign='center'><span class='style25'>Result per page </span><span class='style11'>
		<select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
              


        if ($perpage==20)
			$s20 = 'selected';
		else if ($perpage==30)
			$s30 = 'selected';
		else if ($perpage==40)
			$s40 = 'selected';
		else if ($perpage==50)
			$s50 = 'selected';

			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s40.' value="40">40</option>
			 <option '.$s50.' value="50">50</option>
			 </select></span>';
		
		echo "</td>";
	
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='10'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='center'><span class='style25'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style25'>Ref</span></td>
		<td  height='25' align='left'><span class='style25'>Manifest #</span></td>
		<td  height='25' align='left'><span class='style25'>BL Number</span></td>
		<td  height='25' align='left'><span class='style25'>voyage year </span></td>
		<td  height='25' align='center' ><span class='style25'>Voyage Number </span></td>		
		<td  height='25' align='center' ><span class='style25'>$crfttype Date </span></td>		
	  </tr>";

$querysearch = " select distinct CRFT_Header.crft_id, reference, BL_number, voy_number, voy_year, manifest_auto, date_format(crft_date, '%d-%m-%Y') as crft_date 
				from CRFT_Header
				left join manifestdt on CRFT_Header.manifest_auto = manifestdt.manifest_manifestauto and if('$crfttype' = 'CRF',manifestdt.crft_id = CRFT_Header.crft_id, manifestdt.crft_id2 = CRFT_Header.crft_id )
				where cr_type='$crfttype'  ";

$querysearch=$querysearch." and ( (reference like"."('%".$PRef."%') and $vOpt = '1') or (BL_number like"."('%".$PRef."%') and $vOpt = '2' ) or 
									(voy_number like"."('%".$PRef."%') and $vOpt = '3') or (manifest_auto like"."('%".$PRef."%') and $vOpt = '4') or
									(manifestdt.container like"."('%".$PRef."%') and $vOpt = '5') 
								) ";
$querysearch=$querysearch." order by reference desc";

$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
echo"<input type='hidden' name='startranger' id='startranger' value='$startrange'>";

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


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 "	<td align=center ><img border='0' src='img/edit.png' width='17' height='17' title='Edit Record' onmouseover=\"this.style.cursor='pointer';\" OnClick=\"editcrft('$crft_id', '$crfttype');\" ></td>
			<td align=center ><img border='0' src='img/delete.png' width='17' height='17' title='Delete Record' onmouseover=\"this.style.cursor='pointer';\" OnClick=\"deletecrft('$crft_id', '$crfttype','$reference');\" ></td>
			<td align=center ><img border='0' src='img/mag.jpg' width='10' height='11' title='print Sorder' onmouseover=\"this.style.cursor='pointer';\" OnClick=\"printcrft('$crft_id', '$crfttype');\" ></td>
			<td><img src='img/details.png' onMouseOver=\"style.cursor=\'hand\'\"  onclick=\"ShowHideDetails('$crft_id');\" title='Show Details' style='width:20px; height:20px;'></span></td>
			<td><span class='style15'>$reference</span></td>
			<td><span class='style15'>$manifest_auto</span></td>
			<td><span class='style15'>$BL_number</span></td>
			<td ><span class='style15'>$voy_year</span></td>
			<td><span class='style15'>$voy_number</span></td>
			<td><span class='style15'>$crft_date</span></td>
		</tr>";

	$querydt = "select container, ifnull(type_id,container_type) 
				from manifestdt
				left join container on cntr_code = manifestdt.container
				where (ifnull(CTRFlag,'') <>'*' or detailtype = 1) and if('$crfttype' = 'CRF',crft_id = '$crft_id', crft_id2 = '$crft_id' )  ";	
	$resdt = mysql_query($querydt, $connection);
	$numr = mysql_num_rows($resdt);
	if ($numr > 0){
		$SlideDivId = 'displaydetails'.$crft_id;
		
		echo "<tr><td colspan='10'><div id='$SlideDivId' class='div-search-details slideAll' style='display:none; margin-left:80px;'><table width=70%><col width=50%><col width=50%>";
		while($rowdt = mysql_fetch_array($resdt)){
			echo "<tr bgcolor='#CCD8E6'><td>$rowdt[0]</td><td>$rowdt[1]</td></tr>";
		}
		echo "</table></div></td></tr>";	
		
	}
}
			
	
echo"   <tr><td colspan='18'>&nbsp;</td></tr>
		<tr><td colspan=18><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"<table>";

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


          

		  

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

HERE;

?>