<?php
include("sessionLogin.php");
include("connect.php");
$user = $_SESSION['client'];
$check = mysql_query("select * from customer where userID='$user'");
$display = mysql_fetch_array($check);
$no = $display['no'];

$check2 = mysql_query("select * from memberaddcart where customerID='$no' and status='incart'");
$num = mysql_num_rows($check2);
?>
<html>
<head>
<link href="website.css" rel="stylesheet" type="text/css"/>
</head>
<body class=client>

<hr/>
<table align=left>
<tr>
	<td><a href="indexLogin.php">Home</a> >> Shopping cart</td>
	</tr>
	</table>
	<br/>
<hr/>
<h3 align=left>Shopping Cart</h3>
<h4 align=center>Currently you have <?php echo $num;?> item(s) in cart.</h4>
<form action='' method=POST>
<table width=850 class='inner' cellpadding=5 align=center>
<tr class=menuhead>
	<th>No</th>
	<th>Goods in cart</th>
	<th>Added quantity</th>
	<th>Unit Price</th>
	<th>Subtotal</th>
	<th>Action</th>
	</tr>
<?php
	$a=1;
	$query=mysql_query("select * from memberaddcart where customerID='$no' and status='incart'");
	if(mysql_num_rows($query)==0){
		echo "<tr><td colspan=6 align=center>No item in cart</td></tr>";
	}
	while($display=mysql_fetch_array($query)){
		$productID = $display['productID'];
		$checkpic = $productID.".";
		$companyID = $display['companyID'];
		$quantity = $display['quantity'];
		$price = $display['price'];
		
		$query2=mysql_query("select * from product where id='$productID'");
		$display2=mysql_fetch_array($query2);
		$pic = $display2['productPic'];
		$name = $display2['pName'];
		$dollar = $display2['dollar'];
		
		$query3=mysql_query("select * from promotion where pID='$productID'");
		$display3=mysql_fetch_array($query3);
		$status=$display3['status'];
		$promotion=$display3['promotionprice'];
		
		if($status=="on"){
			$price = $promotion;
			$show = "Promotion Price!";
		}
		
	echo "<tr class=menu>";
	echo "<td>$a</td>";
	echo "<td>
			<table width=350 border=0><tr><td rowspan=2 width=60>";
			if ($pic == $checkpic){
				echo"<img src='../company/album/NoImage.jpg' width=50 height=50 /></td>";
			}else{
				echo"<img src='../company/album/$pic' width=50 height=50 /></td>";
			}	
			echo"
			<td>$productID</td>
			<td><font color=red>$show</font></td>
			</tr>
			<tr><td colspan=2>$name</td></tr>
			</table>";
	echo "<td align=center><form>
			<input type=hidden name=user value=$no />
			<input type=hidden name=code value=$productID />
			<select name=qtr onchange=saveData(this.value,user.value,code.value)>";
			for($i=1;$i<11;$i++){
				
				if($quantity==$i){
					echo "<option value=$i selected>$i</option>";
				}else{
					echo "<option value=$i>$i</option>";
				}
			}
			
	echo "</select></form>
		</td>";
	$subtt = $quantity * $price;
	echo "<td align=center>$price</td>";
	echo "<td align=center>$subtt</td>";
	echo "<td align=center><a href='deleteCart.php?user=$no&code=$productID'>Delete</a></td>";
	
	$ttqtr = $quantity + $ttqtr;
	$ttprice = $subtt + $ttprice;
	$a++;
	$show="";
	}
	
	echo "<tr>";
	echo "<td colspan=6 align=right>
			<table width=300>
				<tr>
					<td>Selected Quantity :</td>
					<td align=right>$ttqtr</td>
					</tr>
				<tr>
					<td >Total Price :</td>
					<td align=right>$ttprice</td>
					</tr>
				</table>
			</td>";
	echo "</tr>";


?>	
</form>	
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "<form name='paypal' action='https://www.sandbox.paypal.com/cgi-bin/webscr' method='post' target='_parent'>";

$userA = $_SESSION['client'];
$checkA = mysql_query("select * from customer where userID='$userA'");
$displayA = mysql_fetch_array($checkA);
$noA = $displayA['no'];
$check2A = mysql_query("select * from memberaddcart where customerID='$noA' and status='incart'");

$numA = mysql_num_rows($check2A);

for($k=1; $k <=$numA; $k++)
{	
	$dis=mysql_fetch_array($check2A);
	$productIDA = $dis['productID'];
	$quantityA = $dis['quantity'];
	$priceA = $dis['price'];
	
	$query2A=mysql_query("select * from product where id='$productIDA'");
	$display2A=mysql_fetch_array($query2A);
	$nameA = $display2A['pName'];
	
	$query3A=mysql_query("select * from promotion where pID='$productIDA'");
	$display3A=mysql_fetch_array($query3A);
	$statusA=$display3A['status'];
	$promotionA=$display3A['promotionprice'];	
	if($statusA=="on"){
		$priceA = $promotionA;
	}

	echo "<input type='hidden' name='item_name_{$k}' value='$nameA'>";
	echo "<input type='hidden' name='quantity_{$k}' value='$quantityA'>";
	echo "<input type='hidden' name='amount_{$k}' value='$priceA'>";
	echo "<input type='hidden' name='item_code_{$k}' value='$productIDA'>";
}	
echo"
	<input type='hidden' name='custom' value='$noA'>
	<input type='hidden' name='cmd' value='_cart'>
	<input type='hidden' name='upload' value='1'>
	<input type='hidden' name='business' value='wai_1330158011_biz@gmail.com'>
	<input type='hidden' name='item_name' value='user'>
	<input type='hidden' name='currency_code' value='USD'>
	<input type='hidden' name='lc' value='US'>
	<input type='hidden' name='return' value='http://www.wauhaha.com/smart/online/index.php'>
	<input type='hidden' name='cancel_return' value='http://www.wauhaha.com/smart/online/index.php'>
	<input name='notify_url' value='http://www.wauhaha.com/smart/online/paypalreturn.php' type='hidden'>
";

echo"<tr class=menu>
	<td colspan=6 align=center><input type=submit value=Submit alt='Make payments with PayPal - it's fast, free and secure!'/></td>
	</tr>";	
echo"</form>";	
echo"</table>";
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
?>


<br/>
<hr/>
<script type='text/javascript'>
function saveData(qtr,user,code){
//document.getElementById('check').innerHTML=code+" "+qtr+" "+user;
//alert(qtr+" "+user+" "+code);

if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
	xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		var value=xmlhttp.responseText;
		if(value=="true"){
			window.location='viewCartForm.php';
		}else{
			alert(value);
		}
	}
}
xmlhttp.open('GET','updateCart.php?quantity='+qtr+'&user='+user+'&code='+code,true);
xmlhttp.send();
}

</script>


</body>
</html>