<%
' Get Product ID
productID = TRIM( Request( "pid" ) )


' Add Item to cart
IF productID <> "" THEN
  sqlString = "SELECT cart_id FROM cart " &_
    "WHERE cart_userID=" & userID & " " &_
    "AND cart_productID=" & productID
  SET RS = Con.Execute( sqlString )
  IF RS.EOF THEN
  sqlString = "INSERT INTO cart ( " &_
    "cart_userID, " &_
    "cart_productID, " &_
    "cart_quantity " &_
    ") VALUES ( " &_
    userID & ", " &_
    productID & ", 1 )"
  ELSE
  sqlString = "UPDATE cart SET " &_
    "cart_quantity=cart_quantity+1 " &_
    "WHERE cart_id=" & RS( "cart_id" ) 
  END IF
  RS.Close
  SET RS = Nothing
  Con.Execute sqlString
END IF


' Update Shopping Cart Quantities
IF Request( "updateQ" ) <> "" THEN
SET RS = Server.CreateObject( "ADODB.Recordset" )
RS.ActiveConnection = Con
RS.CursorType = adOpenDynamic
RS.LockType = adLockOptimistic
sqlString = "SELECT cart_id, cart_quantity FROM cart " &_
  "WHERE cart_userID=" & userID
RS.Open sqlString
WHILE NOT RS.EOF
newQ = TRIM( Request( "pq" & RS( "cart_id" ) ) )
IF newQ = "" OR newQ = "0" THEN
  RS.Delete
ELSE
  IF isNumeric( newQ ) THEN
    RS( "cart_quantity" ) = newQ
  END IF
END IF
RS.MoveNext
WEND
RS.Close
SET RS = Nothing
END IF




%>
<html>
<head><title>Shopping Cart</title><meta name="Microsoft Border" content="none, default">
</head>
<body bgcolor="white">

<center>
<font face="Arial" size=3 color="darkgreen">
<b><%=username%>'s shopping cart:</b>
</font>

<%
' Get the shopping cart
sqlString = "SELECT cart_id, product_name, " &_
  "product_price, cart_quantity " &_
  "FROM cart, products " &_
  "WHERE cart_userID=" & userID & " " &_
  "AND cart_productID = product_id " &_
  "ORDER BY cart_id DESC"
SET RS = Con.Execute( sqlString )

IF RS.EOF THEN
%>
<p><b>You do not have any items in your shopping cart</b>
<p>
<form action="default.asp">
<input type="submit" value="Continue Shopping">
</form>
<% 
ELSE 
orderTotal = 0
%>
<form method="post" action="../astro1/cart.asp">
<input name="updateQ" type="hidden" value="1">
<input name="username" type="hidden" value="<%=username%>">
<input name="password" type="hidden" value="<%=password%>">
<table bgcolor="lightyellow" border=1
  cellpadding=4 cellspacing=0>
<tr bgcolor="lightgreen">
  <th>Product</th>
  <th>Price</th>
  <th>Quantity</th>
</tr>
<% 
WHILE NOT RS.EOF 
orderTotal = orderTotal + ( RS( "product_price" ) * RS( "cart_quantity" ) )
%>
<tr>
  <td>
  <%=Server.HTMLEncode( RS( "product_name" ) )%>
  </td>
  <td>
  <%=formatCurrency( RS( "product_price" ) )%>
  </td>
  <td>
  <input name="pq<%=RS( "cart_id" )%>" type="text" size=4
   value="<%=RS( "cart_quantity" )%>">
  </td>
</tr>
<% 
RS.MoveNext
WEND
%>
<tr bgcolor="yellow">
  <td colspan=2 align=right>
  <b>Order Total:</b>
  </td>
  <td>
  <%=formatCurrency( orderTotal )%>
  </td>
</tr>
<tr>
  <td colspan=3>
  <table border=0>
  <tr>
    <td align="right">
	<input type="submit" value="Update Cart">
	</td>
	</form>
	<form method="post" action="checkout.asp">
	<input name="username" type="hidden" value="<%=username%>">
	<input name="password" type="hidden" value="<%=password%>">
	<td>
	<input type="submit" value="Checkout">
	</td>
	</form>
	<form action="default.asp">
	<td>
	<input type="submit" value="Continue Shopping">
	</td>
	</form>
  </tr>
  </table>
  </td>
</tr>
</table>
<% END IF %>


</center>

</body>
</html>