<?php
session_start();
$username = $_SESSION['membership'];

if($_POST["Back"] == "Back")
{
	echo "<script language='javascript'>window.location='history.php'</script>"; 
}

if($_SESSION['membership']!= null)
{
?>
<html>
<head>
<title>Online Bookshop - Welcome</title>
<link href="bookstore.css" rel="stylesheet" type="text/css"/>
</head>
<body style="background:lightyellow">
<center>
<div id="whole">
<div id="title">
<table width="100%" border="0">
<tr>
<td width="60%"><img src="Picture/Logo.gif" width="450" height="100"></td>
<td>
<form method="GET" action="http://www.google.com/search">
<input type="hidden" name="ie" value="UTF-8" >
<input type="hidden" name="oe" value="UTF-8" target="_blank">
<a href="http://www.google.com">
<img src="Picture/GoogleLogo.png" border="0" alt="Google" align="absmiddle" title="Google" >
</a>
<input type="text" name="q" size="20" maxlength="255" value="">
<input type="submit" name="btnG" value="Google Search">
</form>
</td>
</tr>
</table>
</div>
<div id="top">
<table border="0" width="100%">
<tr>

<?php
session_start();
$username = $_SESSION['membership'];
if($_SESSION['membership']!= null)
{
echo "<td align='left' width='75%'>";
echo "<a href='index.php'>Home Page</a>&nbsp;|&nbsp;<a href='bookmenu.php'>Our Book</a>&nbsp;|&nbsp;<a href='companyevent.php'>Event</a>&nbsp;|&nbsp;<a href='companyprofile.php'>About Us</a>&nbsp;|&nbsp;<a href='sitemap.php'>Site Map</a>&nbsp;|&nbsp;<a href='subtemcart.php'>Cart</a>&nbsp;|&nbsp;<a href='history.php'>Order History</a>&nbsp;|";
echo "</td>";

echo "<td align='right' width='25%'>";

echo "<b>You login as: </b><a href='profile.php'>".$username."</a>";
echo "&nbsp;<a href='logout.php'>(Logout)</a>";
echo "</td>";

}else{

echo "<td align='left' width='75%'>";
echo "<a href='index.php'>Home Page</a>&nbsp;|&nbsp;<a href='bookmenu.php'>Our Book</a>&nbsp;|&nbsp;<a href='companyevent.php'>Event</a>&nbsp;|&nbsp;<a href='companyprofile.php'>About Us</a>&nbsp;|&nbsp;<a href='sitemap.php'>Site Map</a>&nbsp;|&nbsp;<a href='Addmem.php'>Join Us</a>&nbsp;|&nbsp;<a href='login.php'>Member Login</a>&nbsp;|";
echo "</td>";

echo "<td align='right' width='25%'>";
echo "<b>You haven't login.</b>";
echo "&nbsp;<a href='login.php'>(Login)</a>";
echo "</td>";
}
?>
</tr>
</table>
</div>

<br/>
<div id="search">
<form action="bookmenu.php" name="searchform" method="GET">
<fieldset style="width:100%">
<legend>Find Your Books Here!</legend>
Search:<input type="text" size="100" name="keyword" value=""/>

<select name="searchBy" value="">
	<option value="itemName">Book Name</option>
	<option value="itemAuthor">Author</option>
	<option value="itemPublisher">Publisher</option>
	<option value="itemCategory">Category</option>
	</select>

<input type="submit" name="search" value="Search"/>
</fieldset>
</form>
</div>

<div id="links">
<h2>Category</h2>
<a href="bookmenu.php?keyword=Humanities&searchBy=itemCategory&search=Search">Humanities</a>
<a href="bookmenu.php?keyword=Business&searchBy=itemCategory&search=Search">Business & Management</a>
<a href="bookmenu.php?keyword=Engineering&searchBy=itemCategory&search=Search">Engineering</a>
<a href="bookmenu.php?keyword=Finance&searchBy=itemCategory&search=Search">Finance</a>
<a href="bookmenu.php?keyword=Sport&searchBy=itemCategory&search=Search">Sport</a>
<a href="bookmenu.php?keyword=Language&searchBy=itemCategory&search=Search">Language</a>
<a href="bookmenu.php?keyword=School&searchBy=itemCategory&search=Search">School Reference</a>
<a href="bookmenu.php?keyword=General&searchBy=itemCategory&search=Search">General</a>
<a href="bookmenu.php?keyword=Magazine&searchBy=itemCategory&search=Search">Magazine</a>
<a href="bookmenu.php?keyword=Children&searchBy=itemCategory&search=Search">Children</a>
<a href="bookmenu.php?keyword=Mandarin&searchBy=itemCategory&search=Search">Mandarin</a>
<a href="bookmenu.php?keyword=Malay&searchBy=itemCategory&search=Search">Malay</a>
<br/>
<a href='bookmenu.php?sort=itemSales DESC'>Top Sales</a>
<a href='bookmenu.php?sort=itemDate DESC'>New Item</a>

<br/><br/>
<hr/>
<div id="main">
<table>
<tr>
<td><center><h2>HOT Title</h2></center></td>
</tr>
<?php
include("leftbanner.php");
?>
</div>
</div>

<br/>

<div id="main">
<?php
include ("connect.php");
if($_GET["search"] == "Search")
{	
	$searchBy = $_GET["searchBy"];
	$keyword = $_GET["keyword"];
	if($_GET["sort"]!=""){
	$sort = $_GET["sort"];
	}else{
	$sort = num;
	}
	if(($keyword=="")||($searchBy=="")){
	echo "<script language='javascript'>alert('Please insert your keyword and search category.')</script>";
	echo "<script language='javascript'>window.location='bookmenu.php'</script>"; 
	}else{
	$result = mysql_query("SELECT * FROM item WHERE ".$searchBy." LIKE '%".$keyword."%' ORDER BY ".$sort."");
}
}
else if($_GET["reset"] == "Reset Result")
{
	$result = mysql_query("SELECT * FROM item ORDER BY num");
}
else
{
	$result = mysql_query("SELECT * FROM item ORDER BY ".$sort."");   
}
?>
<hr/>
<?php
include("connect.php");
$invNo=$_GET['hNo'];

$result = mysql_query("SELECT * FROM invoice WHERE invNo = '$invNo'");
$row = mysql_fetch_array($result);

$result2 = mysql_query("SELECT * FROM invitem WHERE invNo = '$invNo'");
$num_results = mysql_num_rows($result2);

$result4 = mysql_query("SELECT * FROM invstatus WHERE invNo = '$invNo'");
$num_results4 = mysql_num_rows($result4);

echo "<h3>Cart History - $invNo</h3>";
echo "<form name='orderviewform' method='post'>";

echo "<fieldset style='width:90%'>";
echo "<legend>Online Order - $invNo</legend>";
echo "<br/>";
echo "<table width='100%' border='0'>";
echo "<tr>";
echo "<td width='15%'><b>Buyer Name: </b></td>";
echo "<td colspan='5'>$row[custName]</td>";

echo "<td width='15%'><b>Invoice ID: </b></td>";
echo "<td width='25%'>$row[invNo]</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Address: </b></td>";
echo "<td colspan='5'>$row[invAddress],<br/>$row[invCode], $row[invCity], $row[invState]</td>";

echo "<td><b>Date: </b></td>";
echo "<td>".$row["invDate"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Contact No: </b></td>";
echo "<td colspan='2'>".$row["invTel"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Email: </b></td>";
echo "<td colspan='2'>".$row["invEmail"]."</td>";
echo "</tr>";

echo "<tr>";
echo "<td><b>Paying Method: </b></td>";
echo "<td colspan='6'>".$row["invPay"]."</td>";
echo "</tr>";

echo "</table>";

echo "<br/><br/>";


echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='100%'>";

echo "<div id='main2'>";
echo "<tr>";
echo "<th colspan='6'><center>Invoice Item List</center></th>";
echo "</tr>";

echo "<tr>";
echo "<th>No</th>";
echo "<th>ISBN</th>";
echo "<th>Item Description</th>";
echo "<th>Quantity</th>";
echo "<th>Price</th>";
echo "<th>Subtotal</th>";
echo "</tr>";

for($i=0; $i <$num_results; $i++)
{
	$row2 = mysql_fetch_array($result2);
	$invQ = $row2["invQuantity"]; 
	$invP = $row2["invPrice"];
	$subtotal = $invQ * $invP;
	$total = $total + $subtotal;
	
	$invquantity = $invquantity + $invQ;
	
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row2["invItem"]."</td>";
	echo "<td>";
	
	$result3 = mysql_query("SELECT itemName FROM item WHERE ISBN = $row2[invItem]");
	$row3 = mysql_fetch_array($result3);
	echo "$row3[itemName]";
	echo "</td>";
	echo "<td align='center'>".$row2["invQuantity"]."</td>";
	echo "<td align='center'>RM ";
	printf("%10.2f",$row2["invPrice"]);
	echo "</td>";
	echo "<td>RM";
	printf("%10.2f",$subtotal);
	echo "</td>";
	echo "</tr>";
}
echo "<tr bgcolor='gray'>";
	echo "<th colspan='3'>Total</th>";
	echo "<th>";
	echo "<center>";
	printf($invquantity);
	echo "</center>";
	echo "</th>";
	echo "<td></td>";
	echo "<th>RM";
	printf("%10.2f",$total);
	echo "</th>";
	echo "</tr>";

echo "<tr bgcolor='gray'>";
echo "<th colspan='3'>Shipping Method</th>";
echo "<th>";
echo "<center>";
echo $row["invShip"];
echo "</center>";
echo "</th>";

$shipType =  $row["invShip"];

$result3 = mysql_query("SELECT * FROM ship WHERE shipType = '$shipType'");
$row3 = mysql_fetch_array($result3);

echo "<td>";
echo "</td>";
echo "<th>RM ";
printf("%10.2f",$row3["shipCost"]);
echo "</th>";
echo "</tr>";

echo "<tr bgcolor='gray'>";
echo "<th colspan='3'>Grandtotal</th>";
echo "<th>";
echo "<center>";
printf($invquantity);
echo "</center>";
echo "</th>";
echo "<td>";
echo "</td>";
echo "<th>RM ";
$ship = $row3["shipCost"];
$grandtotal = $total + $ship;
	
printf("%10.2f",$grandtotal);
echo "</th>";
echo "</tr>";
echo "</table>";

echo "<br/>";

echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='100%'>";

echo "<tr>";
echo "<th colspan='4' bgcolor='gray'><center>Delivery Status</center></th>";
echo "</tr>";

echo "<tr bgcolor='gray' style='color:black'>";
echo "<th>No</th>";
echo "<th>Date</th>";
echo "<th>Status</th>";
echo "</tr>";

if($num_results!=0){
for($i=0; $i <$num_results4; $i++)
{
	$row4 = mysql_fetch_array($result4);	
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row4["invSDate"]."</td>";
	echo "<td>".$row4["invStatus"]."";
	echo "&nbsp;".$row4["invNote"]."</td>";
	echo "</tr>";
}
}else{
echo "<tr>";
echo "<td align='center' colspan='4'>Currently No Update For This Invoice.</td>";
echo "</tr>";
}
echo "</table>";

echo "<br/><br/>";

echo "<table>";
echo "<tr>";
	echo "<td colspan='6' align='center'>	
	<input type='submit' name='Back' value='Back'>";
	echo "</tr>";
echo "</table>";
echo "</fieldset>";
echo "</form>";

}else{
	echo "<script language='javascript'>alert ('Please login or register before check your cart.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>
</div>
<hr/>
<div id="bottomlink">
<br/>
<a href="index.php">Home Page</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="bookmenu.php">Our Book</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="Addmem.php"> Membership</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="companyevent.php">Event</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="companyprofile.php">About Us</a><br/><a href="sitemap.php">Site Map</a>
</div>

<div id="bottom">
<br/>
Copyright &#169; with Tian Tian Bookstore 2011. All Rights Reserved. 
</div>

</div>
</center>
</body>
</html>