<?php
//search
session_start();
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");

$querylog=" select ifNull(FI_ItemAuction,0) As FI_ItemAuction ".
		 " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


//Access Mode On Insert/Edit/Delete
$PrdDelMode = 1;
$PrdEditMode =1;
$PrdPrevMode = 1;

$PrevHint = '';
$EditHint = '';
$DelHint = '';

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

$MainCompanyName=explode('_',$_SESSION['dbName']);

$queryperiod= " Select ifNull(LockViewPeriod,0) As LockViewPeriod, ViewPeriodDate, ifNull(ViewLockDaily,0) As ViewLockDaily, ViewNumberOfDays ".
              " From ".$_SESSION['dbLabel'].".ListUserDir  ".
			  " Where Usercode='".$_SESSION['useraccount']."' ".
			  " And Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			  " And Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
			  
$resultperiod = mysql_query($queryperiod,$connection) or die(mysql_error());
$rowsperiod   = mysql_num_rows($resultperiod);

while($rows=mysql_fetch_array($resultperiod)){
	extract($rows);
}	
			  
//echo $queryperiod;	

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

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


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">
<title>Search Production Templates</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
	document.search.keyword.value='';

	document.search.action='Form_ItemAuctionSearch.php';
	document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.keyword.focus();
	
	ChangeLocation();
}

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

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


function DeleteRecord(auction_id, Ref){

	if (auction_id != '') {
		if (confirm ( "Are you sure you want to Delete this record with reference = "+Ref+" ?") )
		{
			document.search.action="Form_ItemAuction_Action.php?mainmode=delete&txtAuctionId="+auction_id;
			document.search.submit();
		}
	}
	
}

function UpdateRecord(auction_id){
	document.search.action="Form_ItemAuction_Action.php?mainmode=search&txtAuctionId="+auction_id;
	document.search.submit();
	
	document.search.PMode.value=1;
	
	opener.document.itemauction.txtAuctionId.value=auction_id;
	opener.document.itemauction.mainaction.value='edit';
	opener.document.itemauction.actionmode.value='';
	
	//self.close();
	

}

function ChangeLocation()
{
    
	if (document.search.PMode.value == 1) {
		opener.document.itemauction.action='Form_ItemAuction.php';
		opener.document.itemauction.submit();
		self.close();
	} else {
		opener.document.sales.txtAuctionId.value='';
		opener.document.sales.mainaction.value='';
		opener.document.sales.actionmode.value='';
	}
}



function SelectOrderBy()
{
  document.search.keyword.value='';

  document.search.action='Form_ProductionTemplateSearch.php';
  document.search.submit();

}

function SelectSearchBy()
{	

  //document.search.keyword.value='';
  //document.search.action='Form_ProductionTemplateSearch.php';
  document.search.submit();

}

function SubmitForm()
{	
 
  //document.search.keyword.value='';
  document.search.action='Form_ProductionTemplateSearch.php?startrange=0';
  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 RefreshPage()
{
  document.search.keyword.value='';

  document.search.action='Form_ProductionTemplateSearch.php?startrange=0';
  document.search.submit();

}

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

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	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 onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>

HERE;


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

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
$querysearch=" Select item_auction.auction_id as auction_id, item_auction.auction_reference as auction_reference, date_format(item_auction.auction_startDate,'%d-%m-%Y') as auction_startDate, ".
			" date_format(item_auction.auction_endDate,'%d-%m-%Y') as auction_endDate,  ".
			" item_auction.item_code as item_code, item_description, item_auction.auction_qty as auction_qty, ifnull(temp_item_auction.auction_id, -1) as temp_id ".
            " From item_auction as item_auction Left Join items on items.item_code = item_auction.item_code ".
			" left join temp_item_auction on temp_item_auction.auction_id = item_auction.auction_id".
			 " Where (($searchby = 3 and ifNull(item_auction.auction_reference,-1) Like Concat(ifNull($PRef, ifNull(item_auction.auction_reference,-1)),'%')) Or ".
			 "        ($searchby = 1 and Date_Format(item_auction.auction_startDate,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(item_auction.auction_startDate,'%Y-%m-%d'))) Or  ".
			 "        ($searchby = 2 and Date_Format(item_auction.auction_endDate,'%Y-%m-%d') = ifNull(Date_Format($PRef,'%Y-%m-%d'), Date_Format(item_auction.auction_endDate,'%Y-%m-%d')) ) ".
			 "        ) " ;
			 

if ($orderby == 1)
	$querysearch= $querysearch." Order By item_auction.auction_startDate Desc, item_auction.auction_reference Desc ";
else if ($orderby == 2)
	$querysearch= $querysearch." Order By item_auction.auction_reference Desc ";	
	
			 
//echo $querysearch;			 

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


echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='860px'>";
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 Items Auction
			</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=25>
	  <col width=20>
	  <col width=20>
	  <col width=100>
	  <col width=100>
	  <col width=400>
	  <col width=130>

	  <tr class='Search-header-bg'>";
	  
echo" <td align='left' colspan='3'><span class='style4'>Order By</span></td>		
	  <td align='left' colspan='2'><span class='style4'><select name='orderby' id='orderby' value='$orderby' onchange='SelectOrderBy();' style='width:120px;'>";
		
		if ($orderby==1)
			$s11 = 'selected';
		else if ($orderby==2)
			$s12 = 'selected';
			
		echo"<option ".$s11." value='1'>Start Date</option>
			 <option ".$s12." value='2'>Reference</option>
			 </select></span></td><td colspan='2'>&nbsp;</td>";
			 
	 
echo" </tr>";
echo" <tr class='Search-header-bg'>";	  
echo"   <td align='left' colspan='3'><span class='style4'>Search By</span></td>
		<td align='left' colspan='4'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' style='width:120px;' maxlength='30' onkeydown='return kd(event)'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
	
			<select name='searchby' id='searchby' value='$searchby' onchange='SelectSearchBy();'>";
		
			if ($searchby==1)
				$s21 = 'selected';
			else if ($searchby==2)
				$s22 = 'selected';
			else if ($searchby==3)
				$s23 = 'selected';
			
			echo"<option ".$s21." value='1'>Start Date (yyyy-mm-dd)</option>
				 <option ".$s22." value='2'>End Date</option>
				 <option ".$s23." value='3'>Reference</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>
</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:1025px;'>
		<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();\">";
              
			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."')>";

			$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:790px;'>
		<table border='0'  cellpadding='0' cellspacing='1' >
		 <col width=20>
		  <col width=20>
		  <col width=70>	
		  <col width=90>
		  <col width=90>
		  <col width=350>
		  <col width=150>";

		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'>&nbsp;</span></td>
		<td  height='25' align='left'><span class='style4'>Reference </span></td>
		<td  height='25' align='center'><span class='style4'>Start Date </span></td>
		<td  height='25' align='center'><span class='style4'>End Date </span></td>
		<td  height='25' align='center'><span class='style4'>Item Code/Description</span></td>
		<td  height='25' align='center'><span class='style4'>Quantity</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."'";	
			
	
	$EditHint = 'Edit Record';
	$DelHint  = 'Delete Record';
	
	$PrdEditMode = 1;
	if ($FI_ItemAuction == 0)	$PrdEditMode = 0; 
	else if($temp_id != -1) $PrdEditMode = 0; 
	
	$PrdDelMode =1;
	if ($FI_ItemAuction == 0)	$PrdDelMode =0; 
	else if($temp_id != -1) $PrdEditMode = 0; 
	
	echo'<tr '.$Color.'>';
		
		if($PrdEditMode==1)   
			echo'<td align="center"><span class="style5"><img src="img/edit.png" onMouseOver="style.cursor=\'hand\'" onclick="UpdateRecord('.$auction_id.');" title="'.$EditHint.'"></span></td>';
		else echo'<td align="center">&nbsp;</td>';			
		
		if($PrdDelMode==1) 	
			echo'<td align="center"><span class="style5"><img src="img/delete.png" onMouseOver="style.cursor=\'hand\'" onclick="DeleteRecord('.$auction_id.', \''.$auction_reference.'\');" title="'.$DelHint.'" ></span></td>';
		else echo'<td align="center">&nbsp;</td>';		
		
	echo'	<td align="left"><span class="style5">'.$auction_reference.'</span></td>
			<td align="center"><span class="style5">'.$auction_startDate.'</span></td>
			<td align="center"><span class="style5">'.$auction_endDate.'</span></td>
			<td align="center"><span class="style5">'.$item_code.' - '.$item_description.'</span></td>
			<td align="left"><span class="style5">'.$auction_qty.'</span></td>
		</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:920px;' 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."')>";
				
		//$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."')>";
		}
		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."')>";
				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 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;

?>