<?php

	include "db.inc";

	/* First, let's check for cookies, then if none are set, we create a new one	*/

	if (!($_COOKIE['cartID'])) {
		$timestamp = time();	// keep the timestamp the same for all operations.
		$cartID = md5($timestamp);	// md5 hash becomes the unique session identifier
		$expire_time = $timestamp+60*60*24*1; // One Day to make purchases
		setcookie("cartID", $cartID, $expire_time);

		} else {
		$cartID = $_COOKIE['cartID'];
		}

	/*	Now, let's prepare for any information this page got passed via GET.	*/

	if (($_GET['productID'])
		&& ($_GET['quantity'])
		 && ($_GET['nb'])
		  && ($_GET['ref_url'])) {

		  /* Just to be safe, let's get the price from the database,
		   * which will also let us do some error checking.
		   */

		  $price_check = "SELECT price FROM products WHERE publicID='" . $_GET['productID'] . "'";
		  $price_result = mysql_query($price_check);

		  if ($price_result) {
				$price_row = mysql_fetch_array($price_result);
				$price = $price_row['price'];
		  		} else {
				print "Illegal product no. " . $_GET['productID'] . "! Nice try.";
				}


		  $cartquery = "INSERT INTO carts SET cartID='" . $cartID .
		  													"',productID='" . $_GET['productID'] .
															 "', nb='" . $_GET['nb'] .
															  "', quantity='" . $_GET['quantity'] .
															   "', price='" . $price .
															    "', created_on=NOW()";
		 $result = mysql_query($cartquery);
		if (!$result) {
		 	print "Query failed:<br>";
		 	print $cartquery;
	 		} else {
		 	//print "CartID: " . $cartID . " Query succeeded:<br>";
		 	//print "<hr>";
		 	}
		}


		/* Deleting an item? No problem.	*/

		if ((is_array($_POST['delete'])) && ($_POST['button'] == "Delete"))	{
			$del_array = $_POST['delete'];

			foreach ($del_array as $d) {
				$dq = "DELETE FROM carts WHERE uid='" . $d . "'";
				$dr = mysql_query($dq);
				}
			}

		if (($_POST['button'] == "Printable Check Form") OR ($_POST['button'] == "Order with PayPal")) {
			include "cashier.php";
			die;
			}

?>
<table width=100% cellspacing="3" border=0 align=center>
<tr>
	<td align="left"><a href="catalog.php"><strong>&lt;| Back to Catalog</strong></a></td>
	<td align="center"><a href="cart.php">Reload Cart</a></td>
	<td align="right"><a href="catalog.php"><strong>Continue Shopping |&gt;</strong></a></td>
</tr>
</table>
<hr>
<table cellspacing="1" border="0" cellpadding="5" width=80% align=center>
<form action="cart.php" method="POST">
<?php
		/*	Read the cart	*/

		$scquery = "SELECT * FROM carts WHERE cartID='" . $cartID . "' ORDER BY productID";
		$scresult = mysql_query($scquery);
		$i = 1;
		while ($scrow = mysql_fetch_array($scresult)) {

			/* get product details from products	*/
			$pquery = "SELECT * FROM products WHERE publicID='" . $scrow['productID'] . "'";
			$presult = mysql_query($pquery);
			$prow = mysql_fetch_array($presult);

			if ($i % 2)	{	$daisycolor = "#ccffdd";	} else {	$daisycolor = "#ffffff";	}
			print "<tr bgcolor=" . $daisycolor . "><td align=center><input type=checkbox name=delete[] value=\"" . $scrow['uid'] . "\"></td>";
			print "<td>" . $scrow['quantity'] . "&nbsp;\"<b>" . $prow['name'] . "\"</b> " . $prow['type'];

			if ($scrow['quantity'] > 1) {	print "s";	}
			print " (" . $scrow['nb'] . ")</td>";

			$subtotal = $prow['price'] * $scrow['quantity'];
			print "<td align=right>$" . $prow['price'] . "&nbsp;x&nbsp;" . $scrow['quantity'] . "=</td><td align=right>" . $subtotal . "&nbsp;&nbsp;&nbsp;</td></tr>";
			$total = $total + $subtotal;
			$i++;
			}

?>
<tr bgcolor="#ffdd45">
	<td><input type=submit name=button value="Delete"></td><input type="hidden" name="cartID" value="<?php echo $cartID; ?>">
	<td width=80%>&nbsp;</td><td align=right><b>TOTAL:&nbsp;</b></td><td align="right">$<?php echo $total; ?>&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr>
	<td></td><td colspan=3 align=right></td>
</tr>
</table>
<table align="center" cellspacing="5">
<tr>
	<td colspan=3>
	You have the option of paying via PayPal or by check. Please enter your shipping address below:
	<br><br>
	</td>
</tr>
<tr>
	<td>E-mail address</td>
	<td><input type="text" name="ship_email" size="25" maxlength="100"><br>
	Your e-mail address will never be sold by anyone associated with this site; we need it for order confirmation!
	</td>
</tr>
<tr>
	<td>Name</td>
	<td><input type="text" name="ship_name" size="15" maxlength="40"></td>
</tr>
<tr>
	<td>Address</td>
	<td><textarea cols="25" rows="2" name="ship_address"></textarea></td>
</tr>
<tr>
	<td>City</td>
	<td><input type="text" name="ship_city" size="15" maxlength="40"></td>
</tr>
<tr>
	<td>State or Province</td>
	<td><input type="text" name="ship_state" size="2" maxlength="4"></td>
</tr>
<tr>
	<td>ZIP or Postal Code</td>
	<td><input type="text" name="ship_ZIP" size="5" maxlength="10"></td>
</tr>
<tr>
	<td>Country*</td>
	<td>
	<select name="ship_country">
	<option value="USA">United States (add $2 shipping &amp; handling)</option>
	<option value="CANADA">Canada (add US$4 shipping &amp; handling)</option>
	<option value="other">Other (add US$8 shipping &amp; handling)</option>
	</select><br>
	*If selecting "other," please put your entire address, including city, postal code and country in the "Address" box.
	</td>
</tr>
<tr>
	<td align="center" colspan="2"><input type=submit name=button value="Printable Check Form">&nbsp;&nbsp;&nbsp;<input type=submit name=button value="Order with PayPal"></td>
</tr>
</form>
</table>