<?php

$total_records = mysql_num_rows($result);
$records_per_page = 10;
$page   = intval($_GET['page']);

$adjacents  = intval($_GET['adjacents']);
if($adjacents<=0) $adjacents = 4;

if($page<=0)  $page  = 1;
//$tpages = ($_GET['tpages']) ? intval($_GET['tpages']) : 20; // 20 by default

if($total_records){
   if($total_records<$records_per_page){
   $tpages = 1;
   }
   else if($total_records%$records_per_page){
   $tpages = (int)($total_records/$records_per_page) + 1;
   }else{
   $tpages = $total_records/$records_per_page;                     
   }
}
else{
   $tpages = 0;
}

//if(isset($_GET["Pages"])){
//	$page = intval($_GET["Pages"]);
//	}else{
//	$page = 1;
//}

$offset = ($page - 1)*$records_per_page;
@mysql_data_seek($result, $offset);

echo "<div id='main'>";
echo "<center>";
echo "<h3>Our Book List</h3>";
echo "<b>Find Your Favourite Books At Here!</b>";

echo "<div id='bookmenu'>";

echo "<table width='90%' border='0'>";
echo "<tr>";
echo "<td colspan='5'>";
echo "Total books found: ".$total_records."";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='5'><hr/></td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='2'>Book Description</td>";
echo "<td width='4%'></td>";
echo "<td width='10%'>Price</td>";
echo "</tr>";

echo "<tr>";
echo "<td colspan='5'><hr/></td>";
echo "</tr>";

$j = 1;
if($j<$records_per_page);{
for($i=0; $i<$records_per_page; $i++)
{
	$row = mysql_fetch_array($result);
	if($row["itemName"]!=""){
	echo "<form name='qform' method='GET' action='addcart.php'>";
	echo "<tr>";
	echo "<td rowspan='4' width='10%'>";
	echo "<a href='bookviewpage.php?ISBN=".$row["ISBN"]."'><img src=/bookstore/bookstaff/".$row["itemImage"]." border='0' width='80' height='120'></a>";
	echo "</td>";	
	echo "<td><input type='hidden' name='itemName' value='".$row["itemName"]."'><a href='bookviewpage.php?ISBN=".$row["ISBN"]."' style='font-size:12pt'><b>".$row["itemName"]."</a></b></td>";
	echo "<td></td>";
	echo "<td><input type='hidden' name='itemPrice' value='".$row["itemPrice"]."'><b>RM";
	printf("%10.2f",$row["itemPrice"]);
	echo "</b></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><b>ISBN: </b><input type='hidden' name='ISBN' value='".$row["ISBN"]."'>".$row["ISBN"]."</td>";
	echo "<td></td>";
	echo "<td></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><b>Author: </b>".$row["itemAuthor"]."</td>";
	echo "<td></td>";
	echo "<td><a href='bookviewpage.php?ISBN=".$row["ISBN"]."'>View Details</a></td>";
	echo "</tr>";
	
	echo "<tr>";
	echo "<td><b>Category: </b>".$row["itemCategory"]."</td>";
	echo "<td align='right'>";
		
if($row[itemQuantity]>0){
	$tem = $row[itemQuantity];
	echo "<name='temQuantity'>";
	echo "<select name='temQuantity'>";
	for($j=1; $j<=$tem; $j++)
	{
	echo "<option value='$j'>".$j;
	echo "</option>";
	}
	echo "</select>";
	echo "</td>";
	echo "<td>";
	echo "<input type='submit' name='add' value='Add Cart'>";
	echo "</td>";
	echo "<td width='10%'><img src='instock.gif' width='35' height='35' alt='In Stock Item'></td>";
}else{
	echo "<name='temQuantity' value='".$row["temQuantity"]."'>";
	echo "<select name='temQuantity' onchange='this.form.submit()' value='Save' name='Save''>";
	$temQuantityArray = array("1", "2", "3", "4", "5", "6", "7", "8", "9");
	for($m = 0; $m < 9 ; $m++)
	{if($row["temQuantity"] == $temQuantityArray[$m])
	{echo "<option selected>".$temQuantityArray[$m];
	}else{
	echo "<option>".$temQuantityArray[$m];
	}
	}
	echo "</select>";
	echo "<input type='hidden' name='ISBN' value='$row[ISBN]'>";
	echo "<input type='hidden' name='itemPrice' value='$row[itemPrice]'>";
	echo "</td>";
	echo "<td>";
	echo "<input type='submit' name='add' value='Add Cart'>";
	echo "</td>";
	echo "<td width='10%'><img src='preorder.gif' width='35' height='35' alt='Pre Order Item'></td>";
}
	echo "</tr>";
	
	echo "<tr>";
	echo "<td colspan='5'><hr/></td>";
	echo "</tr>";
	echo "</form>";
	}
	}
$j++;
}
	
echo "</table>";
echo "</div>";

echo "<hr/>";

echo "</div>";

echo"<center>";
if(($keyword=="")||($searchBy=="")){
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
}else{
$reload = $_SERVER['PHP_SELF'] . "?keyword=" . $keyword . "&searchBy=" . $searchBy . "&search=Search&tpages=" . $tpages . "&amp;adjacents=" . $adjacents;
}

function paginate_three($reload, $page, $tpages, $adjacents) {
	
	$prevlabel = "&lsaquo; Prev";
	$nextlabel = "Next &rsaquo;";
	
	$out = "<div class=\"pagin\">\n";
	
	// previous
	if($page==1) {
		$out.= "<span>" . $prevlabel . "</span>\n";
	}
	elseif($page==2) {
		$out.= "<a href=\"" . $reload . "\">" . $prevlabel . "</a>\n";
	}
	else {
		$out.= "<a href=\"" . $reload . "&amp;page=" . ($page-1) . "\">" . $prevlabel . "</a>\n";
	}
	
	// first
	if($page>($adjacents+1)) {
		$out.= "<a href=\"" . $reload . "\">1</a>\n";
	}
	
	// interval
	if($page>($adjacents+2)) {
		$out.= "...\n";
	}
	
	// pages
	$pmin = ($page>$adjacents) ? ($page-$adjacents) : 1;
	$pmax = ($page<($tpages-$adjacents)) ? ($page+$adjacents) : $tpages;
	for($i=$pmin; $i<=$pmax; $i++) {
		if($i==$page) {
			$out.= "<span class=\"current\">" . $i . "</span>\n";
		}
		elseif($i==1) {
			$out.= "<a href=\"" . $reload . "\">" . $i . "</a>\n";
		}
		else {
			$out.= "<a href=\"" . $reload . "&amp;page=" . $i . "\">" . $i . "</a>\n";
		}
	}
	
	// interval
	if($page<($tpages-$adjacents-1)) {
		$out.= "...\n";
	}
	
	// last
	if($page<($tpages-$adjacents)) {
		$out.= "<a href=\"" . $reload . "&amp;page=" . $tpages . "\">" . $tpages . "</a>\n";
	}
	
	// next
	if($page<$tpages) {
		$out.= "<a href=\"" . $reload . "&amp;page=" . ($page+1) . "\">" . $nextlabel . "</a>\n";
	}
	else {
		$out.= "<span>" . $nextlabel . "</span>\n";
	}
	
	$out.= "</div>";
	
	return $out;
}
echo paginate_three($reload, $page, $tpages, $adjacents);

echo "</center>";

$db = mysql_close();

?>