<?php
//====================================================================================
// This php shopping cart application
// You can view a cart, add an item to a cart, delete an item, and clear entire cart  
// The shopping cart can be stored either as a session cart, or a database cart
//====================================================================================
    include('shopCart.inc');

    session_start();					//get a handle to the session 
    
    if (! $_SESSION[cust_id])				//if there is no session for customer				
	header("Location: shop.php");			//redirect to login page

    $cust_id = $_SESSION['cust_id'];			//get cust id from previous session			
    $fname   = $_SESSION['fname'];			//get firstname from previous session			

    $item     = $_POST['item'];				//get HTML form entry fields 
    $size     = $_POST['size'];		
    $color    = $_POST['color'];
    $quantity = $_POST['quantity'];		
    $type     = $_POST['type'];		

    $prices = array(Shirt=>34.99, Pants=>44.99, Suit=>299.99, Hat=>24.99, Shoes=>75);
    $price  = $prices[$item];

    $cart = new Cart('shopcart',$type,'localhost',null,'demo2','demo2','demo2',$cust_id);	#instantiate a cart object

    if ($_POST['view'])			#if view cart button
    {
	    $cart->viewCart();
	    $msg  = "Items in your Shopping Cart";
    }    
    if ($_POST['add'])			#if add item button
    {
	validate();	
	if (! $msg)		
	{
	    $cart->addToCart($item,$size,$color,$quantity,$price);
	    $msg  = "Item Added to your Shopping Cart";
	}
    }    
    if ($_POST['delete'])		#if delete item button
    {
	$cart->deleteFromCart($item);
	$msg  = "Item Deleted from your Shopping Cart";
    }
    if ($_POST['clear'])		#if clear cart button
    {
	$cart->clearCart();		
	$msg  = "Shopping Cart has been cleared";
    }
    
    display();

//=============================================================================
    function validate()
    {
	global $item, $size, $color, $quantity;   
	global $item_error,$size_error,$color_error,$msg;   

	if (! $item) {
	    $msg        = 'error';     
	    $item_error = '*';
	}
	if (! $size) {
	    $msg        = 'error';     
	    $size_error = '*';
	}
	if (! $color) {
	    $msg        = 'error';     
	    $color_error = '*';
	}
	if (! $quantity) {
	    $quantity = 1;     
	}
	if ($msg == 'error')
	    $msg  = 'Please enter required field(s) above!';     
    }

//=============================================================================
    function display()
    {
	global $item, $size, $color, $quantity, $type, $prices, $price, $cart, $fname;   
	global $item_error,$size_error,$color_error,$msg;   

	$item_selected[$item]   = 'selected';		//create 3 keyed arrays
	$size_selected[$size]   = 'selected';		//assign the element for the option selected 
	$color_selected[$color] = 'selected';		//the value 'selected'

	if (! $type) $type   = 'session';
	$type_checked[$type] = 'checked';		//check 'session' by default

	print "
	    <html>
	    <head>
	    <title>OO Shopping Cart</title>
	    <style>
	        a {text-decoration:none; color:brown}
	    </style>
	    </head>
	    <body bgcolor=lightyellow>
	    <h1><center>Object Oriented Shopping Cart</center></h1>
 	    <form method=POST action=$_SERVER[PHP_SELF]>
            <fieldset style='width:280px; height:180px; border-color:brown'>
            <legend>$fname - Enter item to your shopping cart</legend>";

	print "
	    <table>
    	    <tr><td><b>Item  
    	    <td><select name=item> 
    	        <option value='' selected>Please Select   </option>
    	        <option value=''>         ---------------------</option>
    	        <option value=Shirt $item_selected[Shirt]>Shirt @ \${$prices[Shirt]} </option>
    	        <option value=Pants $item_selected[Pants]>Pants @ \${$prices[Pants]} </option>
    	        <option value=Suit  $item_selected[Suit] >Suit  @ \${$prices[Suit] } </option>
    	        <option value=Hat   $item_selected[Hat]  >Hat   @ \${$prices[Hat]  } </option>
    	        <option value=Shoes $item_selected[Shoes]>Shoes @ \${$prices[Shoes]} </option>
                </select>
                <font color=red> $item_error </font>
    	    <tr><td><b>Size  
    	    <td><select name=size> 
    	        <option value='' selected>Please Select    </option>
    	        <option value=''>         --------------------- </option>
    	        <option $size_selected[small]>  small  </option>
    	        <option $size_selected[x_small]>x_small</option>
    	        <option $size_selected[medium]> medium </option>
    	        <option $size_selected[large]>  large  </option>
    	        <option $size_selected[x_large]>x_large</option>
                </select>
                <font color=red> $size_error </font>
    	    <tr><td><b>Color  
    	    <td><select name=color> 
    	        <option value='' selected>Please Select   </option>
    	        <option value=''>         ---------------------</option>
    	        <option $color_selected[white]>white  </option>
    	        <option $color_selected[blue]> blue   </option>
    	        <option $color_selected[brown]>brown  </option>
    	        <option $color_selected[grey]> grey   </option>
    	        <option $color_selected[black]>black  </option>
                </select>
                <font color=red> $color_error </font>
            <tr><td><b>Quantity
                <td><input type=text name=quantity value='$quantity' size=15>
                <font color=red> $qty_error </font>
	    <tr><td><input type=submit name=view   value='View Cart'>
	        <td><input type=submit name=add    value='  Add  '>
	            <input type=submit name=delete value='Delete'>
	            <input type=submit name=clear  value=' Clear '>
	    <tr><td><b>
	        <td><input type=radio name=type value=session  $type_checked[session]>  session
	            <input type=radio name=type value=database $type_checked[database]> database
            </table>
            </fieldset>
            <br><font color=red> $msg  </font>
            </form>
            ";

        print $cart;			//this automatically calls the __toString() method
    }

//=============================================================================================
?>
<hr/>
<center>
<a href=shop.php>       login   </a> |
                        shop         |
<a href=shopProf.php>   profile </a> |
<a href=shop.php?out=y> logout  </a>
</center>
<?php include "../include.php"; ?>		<!-- hyperlink to see the code -->
</body>
</html>