<?Php
/*
* @author Istvan Franko
* @copyright 2014
*/

session_start();

if (!isset($_SESSION['cart'])) 
{
  $_SESSION['cart']=array(); 
}

if (!isset($_SESSION['loggedin'])) 
{
  $_SESSION['loggedin'] = '0'; 
}
?>

<html>

<head>
<title>Natty Notebooks Inc.</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>

<body>

<div id="container">

	<div id="header">
	<! header picture in css >
	</div>
	
	<div id="navigation">
		<?php include 'navigation-bar.php'; ?>
	</div>
	
<div id="aside">
	<!--Link for navigation-->
	<a href="product-list.php"><p style="text-align:center">Back to list</a></p>
	
	<fieldset style="width:85%"><legend>HELP</legend>
	<p>Please check that the selected product is in your shopping cart.</p>
	<p>Please enter the right quantity in the field provided.<p>
	<p>Give your personal information.<p> 
	<p>You can continue browsing or check out your basket with the links 
	on the bottom of the page.</p>
	</fieldset>
</div>

<div id="content">

<br>
<fieldset style="width:95%"><legend>CHECK OUT</legend>
<br>
<p><img width="120" src="images/shopping-cart.png" alt="Shopping Cart" style="float:left">


<?php

$con = mysql_connect("localhost","*****","*****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("university", $con);


echo "You have " . sizeof($_SESSION['cart']) . " item(s) in you cart.</p><hr/><br><br><br>";


//read session array to local array
while (list ($key, $val) = each ($_SESSION['cart'])) 
{
$cart[$key] = $val;
}

//check the local array length
$OrderCounter = count($cart);

echo "<form action='cart-check.php' method='post'>";
for ($x=0; $x != $OrderCounter; $x++)
{
$id = $cart[$x];

$query=Mysql_query("SELECT * FROM natty WHERE ItemID=$id ");
$row=mysql_fetch_object($query);
echo mysql_error();

//display cart details in a table
echo"
<table border='0' align='center'  cellspacing='5' cellpadding='5' style='width:650px;'>
	<tbody>
		<tr bgcolor='#e1e0e0'>
			<td class=border colspan='3' style='text-align: center;'>
				<a href='details.php?id=$row->ItemID'><h2>$row->ItemName</h2></a></td>
		</tr>
		<tr>
			<td class=border colspan='1' rowspan='3' style='width: 130px;'>
				<a href='images/$row->ItemPicture1'><img width='120' src='images/$row->ItemPicture1'></img></a></td>
			<td colspan='2' rowspan='1' style='width: 510px;'>
				</td>
		</tr>
		<tr bgcolor='#e1e0e0'>
			<td class=border colspan='2' rowspan='1' style='width: 510px;'>
				<p>The available quantity.<br> Please enter how much do you need from each stock?</p></td>
		</tr>
		<tr bgcolor='#e1e0e0'>
			<td class=border style='width: 400px;'><p>
				$row->ItemInMbro available in Middlesbrough Store</p></td>
			<td class=border style='width: 90px; text-align: center;'>
				<input type='text' name='ItemNumMbro[$x]' size='6'></td>
		</tr>
		<tr bgcolor='#e1e0e0'>
			<td class=border style='width: 130px; text-align: center;'>
				<a href='cart-remove.php?id=$x'>Remove</td>
			<td class=border style='width: 400px;'><p>
				$row->ItemInStockton available in Stockton Store</p></td>
			<td class=border style='width: 90px; text-align: center;'>
				<input type='text' name='ItemNumStock[$x]' size='6'></td>
		</tr>
	</tbody>
</table><hr>
";

}


//get user details from database
$userid = $_SESSION['loggedin'];
$result = mysql_query("SELECT * FROM users WHERE UserID='$userid'");
while ($row = mysql_fetch_array($result))
{
$FullName=$row['FullName'];
$Email=$row['Email'];
$Phone=$row['Phone'];
}

mysql_close($con);

if ($OrderCounter != 0)
{
	
//if the cart doesn't empty show the user details from
if ($_SESSION['loggedin'] > 0)
{
	echo "<h2> You are logged in </h2>";
	echo "<p>Your name is: " . $FullName . "<br><br>";
	echo "Your email is: " . $Email . "<br><br>";
	echo "Your phone number is: " . $Phone . "<br><br></p>";
	
	echo "
	<input type='hidden' name='name' value='$FullName'>
	<input type='hidden' name='email' value='$Email'>
	<input type='hidden' name='phone' value='$Phone'>

	<input id='button' style='float: right;' type='submit' value='Check out'>
	</from>
	";
}
else //if the customer logged in just show his details
{

	// send user details trough hidden form
	echo "
	<p>Name: <input type='text' name='name' size='40'><span class='error'>* required fields</span></p>
	<p>Email: <input type='text' name='email' size='50'><span class='error'>* required fields</span></p>
	<p>Phone: <input type='text' name='phone' size='50'></p>

	<input id='button' style='float: right;' type='submit' value='Check out'>
	</from>
	";
}
}
else
{
	echo "<p>Your cart is empty!!!</p>";
}

	if ($OrderCounter != 0)
	{
		echo "<br><p><a style='float: left' href='cart-empty.php'>Emptying the whole cart</p></a>";
	}
?>

</fieldset>

</div>

<div id="footer">
<p align="center">Copyright &#169; Istvan Franko, 2014</p>
</div>


</div>

</body>
</html>