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

if($_POST["Delete"] == "Clear All")
{
	include("connect.php");
	$result5 = mysql_query("SELECT * FROM temcart WHERE temID = '$username'");
	$total_records = mysql_num_rows($result5);
	
	for($i=0; $i<$total_records; $i++){
	$row5 = mysql_fetch_array($result5);
	
	$temQuantity = $row5["temQuantity"];
	
	$ISBN = $row5["temItem"];
	
	$result4 = mysql_query("SELECT * FROM item WHERE ISBN = '$ISBN'");
	$row = mysql_fetch_array($result4);
	$oldQuantity = $row["itemQuantity"];
	$oldSales = $row["itemSales"];

	$itemDate = $row["itemDate"];
	$date = date("Y-m-d");
	
if(date("Y-m-d",strtotime($date))>date("Y-m-d",strtotime($itemDate))){
	$newQuantity = $oldQuantity + $temQuantity;
	$newSales = $oldSales - $temQuantity;
	$result2 = mysql_query("UPDATE item SET itemQuantity = '$newQuantity', itemSales = '$newSales' WHERE ISBN = '$ISBN'");
}else{
	$newSales = $oldSales - $temQuantity;
	$result2 = mysql_query("UPDATE item SET itemSales = '$newSales' WHERE ISBN = '$ISBN'");
}

	$query=mysql_query("delete from temcart where temID='$username'&&temItem='$ISBN'");
	}
	
	echo "<script>alert('All item is remove from cart.')</script>";
	echo "<script language='javascript'>window.location='bookmenu.php'</script>"; 
}
if($_POST["Back"] == "Back")
{
	echo "<script language='javascript'>window.location='bookmenu.php'</script>"; 
}
if($_SESSION['membership']!= null)
{
?>
<?php
session_start();
$searchBy = $_GET["searchBy"];
$keyword = $_GET["keyword"];
?>

<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>

<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."");   
}
?>

<center>
<h2>Submit Cart</h2>
</center>

<?php

$result = mysql_query("SELECT * FROM temcart WHERE temID = '$username'");
$num_results = mysql_num_rows($result);

echo "<div id='cart'>";
echo "<table border='1' cellspacing='0' cellpadding='5' bordercolor='white' width='90%' align='center'>";
echo "<tr bgcolor='gray' style='color:black'>";
echo "<th colspan='6' align='left'>";
echo "STEP 1 - CHECK YOUR ORDER LIST";
echo "</th>";
echo "</tr>";
echo "<tr bgcolor='gray' style='color:black'>";
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>";

if($num_results!=0){
for($i=0; $i <$num_results; $i++)
{
	$row = mysql_fetch_array($result);
	$temQ = $row["temQuantity"]; 
	$temP = $row["temPrice"];
	$ship = $row3["shipCost"];
	$subtotal = $temQ * $temP;
	$total = $total + $subtotal;
	
	$tquantity = $tquantity + $temQ;
	
	echo "<tr>";
	echo "<td>".($i + 1)."</td>";
	echo "<td>".$row["temItem"]."</td>";
	echo "<td>".$row["temName"]."</td>";
	echo "<td align='center'>".$row["temQuantity"]."</td>";
	echo "<td>RM";
	printf("%10.2f",$row["temPrice"]);
	echo "</td>";
	echo "<td>RM";
	printf("%10.2f",$subtotal);
	echo "</td>";
	echo "</tr>";
}

echo "<tr>";
echo "<th colspan='5' align='left'>Total</td>";
echo "<th align='left'>RM ";
printf("%10.2f",$total);
echo "</th>";
echo "</tr>";

echo "<tr bgcolor='gray' style='color:black'>";
echo "<th colspan='3' align='left'>Transport Charges</th>";

echo "<form name='pform' method='post' action='temcartship.php'>";
echo "<td align='center'>";

$result2 = mysql_query("SELECT * FROM tem WHERE temID = '$username'");
$row2 = mysql_fetch_array($result2);

	echo "<select name='temShip' onchange='this.form.submit()' value='Save' name='Save''>";
	$temshipArray = array("None", "POSLAJU", "AIRMAIL");
	for($j = 0; $j < 3 ; $j++)
	{if($row2["temship"] == $temshipArray[$j])
	{echo "<option selected>".$temshipArray[$j];
	}else
	{echo "<option>".$temshipArray[$j];
	}
	}
	echo "</select>";
echo "</td>";
echo "</form>";
echo "<td></td>";
echo "<th align='left'>RM ";

$shipType = $row2[temship];

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

printf("%10.2f",$row3["shipCost"]);
echo "</th>";
echo "</tr>";
echo "</form>";
echo "<tr bgcolor='gray' style='color:black'>";
	echo "<th colspan='3' align='left'>Total</th>";
	echo "<td align='center'>";
	echo "<font color='white'>";
	printf($tquantity);
	echo "</font>";
	echo "</td>";
	echo "<td></td>";
	echo "<th align='left'>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='90%' align='center'>";

$result4 = mysql_query("SELECT * FROM membership WHERE memberLname = '$username'");
$row4 = mysql_fetch_array($result4);
echo "<tr bgcolor='gray' style='color:black'>";
echo "<th colspan='2' align='left'>";
echo "STEP 2 - ENTER YOUR INFORMATION";
echo "</th>";
echo "</tr>";
echo "<form name='form' method='POST' action='submitcart.php'>";
echo "<tr>";
echo "<td width='30%'>Name: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartName' size='100' value='".$row4["memberName"]."'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Address: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartAddress' maxlength='100' size='100' value='".$row4["memberAddress"]."'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Phone No.: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartTel' maxlength='15' value='".$row4["memberHnum1"]." ".$row4["memberHnum2"]."'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Email: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartEmail' maxlength='50' value='".$row4["memberEmail"]."'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>Post Code: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartCode' value='".$row4["memberPcode"]."'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>City: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartCity' value='".$row4["memberCity"]."'>";
echo "</td>";
echo "</tr>";

echo "<tr>";
echo "<td>State: </td>";
echo "<td align='left'>";
echo "<input type='text' name='cartState' value='".$row4["memberState"]."'>";
echo "</td>";
echo "</tr>";
echo "</table>";

?>

<table border="1" cellspacing="0" cellpadding="5" bordercolor="white" width="90%" align="center">
<tr bgcolor="gray" style="color:black">
<th colspan="2" align='left'>STEP 3 - CHOOSE YOUR PAYING METHOD</th>
</tr>

<tr>
<td width="60%">Choose to your paying method here: 
<br/>
(Maybank2U/Cash or Cheque Bank In/Credit Card/Shop Collect)</td>
<td><select name="cartPay"><option value="">Choose One</option>
<option value="Bank In">Cash/Cheque Bank In</option>
<option value="Shop Collect">Shop Collect</option>
</td>
</tr>
</table>
<?php

echo "<table align='center' width='90%'>";
echo "<tr bgcolor='gray' style='color:black'>";
echo "<th colspan='2' align='left'>STEP 4 - Submit Your Order</th>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>If everything is ok, is time to submit your order to us!</td>";
echo "<tr>";
echo "<tr>";
	echo "<input type='hidden' name='temShip' value=".$row2["temship"].">";
	echo "<input type='hidden' name='cartTotal' value= $grandtotal>";
	echo "<input type='hidden' name='total' value= $total>";
	include ("temcartID.php");
	echo "<input type='hidden' name='cartNo' value='".$index."'>";
	echo "<td colspan='5' align='center'>
	<input type='button' name='Submit' value='Submit' onclick='submitcheck(cartPay.value,cartAddress.value,cartTel.value,cartEmail.value,cartCode.value,cartCity.value,cartState.value)'>
	<input type='submit' value='Back To Cart' name='Back'>
	<input type='submit' value='Clear All' name='Delete'></td>";
	echo "</tr>";
	
echo "</form>";
}else{
	echo "<tr>";
	echo "<td colspan='7' align='center'>";
	echo "<br/>";
	echo "<h3>No item in cart.</h3>";
	echo "</td>";
	echo "</tr>";
	
	echo "<table align='center'>";
	echo "<form name='nocartform' method='post' action='submitcart.php'>";
	echo "<tr>";
	echo "<td colspan='5' align='center'>";
	echo "<input type='submit' value='Back' name='Back'></td>";
	echo "</tr>";
	echo "</form>";
}

echo "</table>";
echo "</div>";
$db = mysql_close();

}else{
	echo "<script language='javascript'>alert ('Please login or register before check your cart.')</script>";
	echo "<script language='javascript'>window.location='index.php'</script>";
}
?>

	<script language="Javascript">
	function submitcheck(a,b,c,d,e,f,g){
	if((a=="")||(b=="")||(c=="")||(d="")||(e="")||(f="")||(g="")){
	alert("Please check your cart information.");
	}else{
	this.form.submit();
	}
}
</script>


<br/><br/>
</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>