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


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

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



if (empty($option)) $option = 1;
if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 3;

if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;

if($txtDate == '') $txtDate = date('d/m/Y');
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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 Archive</title>
<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>

<script type="text/javascript" language="javascript">

function clearform()
{
  document.search.keyword.value='';
   document.search.action='Form_ArchiveSearch.php';
  document.search.submit();

}

function SetNextPage(startrange,perpage,option){ 

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

function SetPrvPage(startrange,perpage,option){

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


function DeleteRecord(SaId, SaRef, option){

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


function SelectRecord(archive_id){
	opener.document.archives.archive_id.value =	archive_id;
	opener.document.archives.mainaction.value =	'edit';
	opener.document.archives.submit();	
	self.close();
}


function SelectSearchBy()
{	

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

}




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


function SubmitForm(){
	//document.search.action = "Form_ArchiveSearch.php";
	document.search.submit();
}
</script>

<style type="text/css">

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

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body  class='bodybackground'>
<form name='search' method='post' action="" >
<input type='hidden' name='SelectedRec' id='SelectedRec' value=''>
<input type='hidden' name='SelectedRecColor' id='SelectedRecColor' value=''>
HERE;

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

$querysearch ="select auto, date_format(Check_DateFormat(issuedate),'%d-%m-%Y') as issuedate, companyname,documentnumber, substr(shipper,1,40) as shipper, producttype from archiving where 1=1 ";
if($searchby == 2) $querysearch =$querysearch."  and documentnumber  like ('%".$keyword."%') ";
if($searchby == 3) $querysearch =$querysearch."  and auto  like ('%".$keyword."%') ";
	$querysearch =$querysearch." order by auto desc ";
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
	 

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

$FormTitle = 'List of Archives';

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

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
		 
echo"<table border='0' cellspacing='1' style='table-layout:fixed'>
	  <col width=100><col width=740>";
	
	
echo" <tr class='Search-header-bg' height='30px' bgcolor='#DDDDDD'>	  	  
	    <td align='left'><span class='style4'>Search By</span></td>
		<td align='left' colspan='3'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' size='15' maxlength='30' onkeydown='return kd(event)'/>
		</span>
			
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
			if ($searchby==0)		$s20 = 'selected';
			else if ($searchby==1)	$s21 = 'selected';
			else if ($searchby==2)	$s22 = 'selected';
			else if ($searchby==3)	$s23 = 'selected';				
			echo"<option ".$s20." value='0'></option>
				<option ".$s21." value='1'>Date (yyyy-mm-dd)</option>
				<option ".$s22." value='2'>Document Number</option>
				<option ".$s23." value='3'>Auto #</option>";
			echo "</select>";	
				 
echo"		<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='clearform();return false;' title='Cancel'><span><span>Cancel</span></span></button>
		</td>";
echo" </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:809px;'>
	<table width='100%' border=0>
	<tr>
	<td align='right' style='width:300px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange=\"RefreshPage();\">";
	  
	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></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."','".$option."')>";

			$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."','".$option."')>";
			}
			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."','".$option."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."','".$option."')>";	
				}
			} 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:860px;'>
			<table border='0'  cellpadding='0' cellspacing='1' style='width:890px;' >
			  <col width=20><col width=20><col width=50><col width=100><col width=150><col width=150><col width=250><col width=150>";	  
	if($message!=''){
		echo"<tr class='Search-header-bg'>
				<td height='15' align='center' colspan='18' 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'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>Auto </span></td>
			<td  height='25' align='center'><span class='style4'>Date </span></td>
			<td  height='25' align='center'><span class='style4'>Company Name</span></td>";
	echo"	<td  height='25' align='center'><span class='style4'>Document Number</span></td>
			<td  height='25' align='center'><span class='style4'>Shipper</span></td>
			<td  height='25' align='center'><span class='style4'>Product Type</span></td>";
	echo"</tr>";

$i = 0;
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 onMouseOver=\"style.cursor='pointer'\"  id='row$ct' ".$Color.">";
	echo'<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick=\'SelectRecord("'.$auto.'");\' title="Edit"  style="width: 20px; height: 20px; "></span></td>';	
	echo'<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick=\'DeleteRecord("'.$auto.'");\' title="Delete" style="width: 20px; height: 20px; "></span></td>';
		
	echo'	<td align="left"><span class="style5">'.$auto.'</span></td>
			<td align="left"><span class="style5">'.$issuedate.'</span></td>
			<td align="center"><span class="style5">'.$companyname.'</span></td>
			<td align="center"><span class="style5">'.$documentnumber.'</span></td>';
	echo'	<td align="center"><span class="style5">'.$shipper.'</span></td>
			<td align="left"><span class="style5">'.$producttype.'</span></td>';
	echo'</tr>';
	

}
							

echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";		
		
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:809px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:280px;'>&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."','".$option."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."','".$option."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."','".$option."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."','".$option."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

          

		  

print <<<HERE

</form>
</body>

HERE;

?>