<?php session_start() 
    
//session.cookie_lifetime = 0;
?>
    <!--
    Brian Platz
    CS85 Section 1643
    Professor Geddes
    April 20, 2012
    Assignment: Test 2 pizzaOrder.php
                associated files:
                    pizzaOrder.css        for styling
                    pizzaOrderSrc.php     to display source code
    --> 
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
      <title></title>
      <link rel="stylesheet" type="text/css" href="pizzaOrder.css" />

   </head>
   <body> 
      <?php
        extract
($_REQUEST);                         // create form variables
        
error_reporting(E_ALL & ~E_NOTICE);         // get rid of runtime notices
        
        
global $pizzaPrices$selPizza$selSize;
        
        function 
setPizzaArray() {    // set up pizza array with types and prices
            
global $pizzaPrices;
            
$pizzaPrices = array("Plain" => array("Small"=>"3.50"
                                                
"Medium"=>"6.25",
                                                
"Large"=>"8.00"
                                                
),
                                
"Vegetarian" => array("Small"=>"4.35"
                                                
"Medium"=>"7.60",
                                                
"Large"=>"12.00"
                                                
),
                                
"Pepperoni" => array("Small"=>"7.25"
                                                
"Medium"=>"10.75",
                                                
"Large"=>"14.00"
                                                
),
                                
"Hawaiian" => array("Small"=>"8.00"
                                                
"Medium"=>"12.50",
                                                
"Large"=>"15.50"
                                                
)
            );
            
            }

        function 
displayMenu() {    // displays menu
            
global $pizzaPrices;
            
            ECHO <<<HERE
                <h3 class='centerText'>Welcome to PHP*</h3>
                <h2 class='centerText'>* PHP Harry's Pizza</h2>
HERE;
            print 
"<table class='center'>
                    <tr><th></th><th>Small</th><th>Medium</th><th>Large</th></tr>"
;
            foreach (
$pizzaPrices as $zaType => $prices) {
                print 
"<tr><td class='centBold'>$zaType</td><td>".$pizzaPrices[$zaType]["Small"]."</td><td>".$pizzaPrices[$zaType]["Medium"]."</td><td>".$pizzaPrices[$zaType]["Large"]."</td></tr>";
                }
            print 
"</table>";    
            }
            
        function 
createSelects() {   // creates select boxes for pizza types and pizza sizes
            
global $selPizza$selSize$pizzaPrices;    
            
                
// create a selectbox for pizza type
            
$selPizza "<select class='center' name='type' >";
            foreach(
$pizzaPrices as $zaType => $prices) {
                
$selPizza .= "<option value='".$zaType."' >".$zaType."</option>";
                }
            
$selPizza .= "</select>";
            
              
// create a select box for pizza size
            
$selSize "<select class='center' name='size' >
                            <option value='Small'>Small</option>
                            <option value='Medium'>Medium</option>
                            <option value='Large'>Large</option>
                        </select>"
;
            }
        
        function 
printOrderForm() {   // prints form for ordering pizza
            
global $selPizza$selSize;
            
            
createSelects();
            ECHO <<<HERE
                <form id='myform' class='center' method='POST' >
                <h1>Order your pizza NOW!!</br>
                Pizza Type: 
$selPizza</br>
                Pizza Size: 
$selSize</br>
                <h1><button class='center' type='submit'>Order</button></h1>
                </form>
HERE;
            }
        
        function 
displayReceipt() {
            global 
$type$size$pizzaPrices;
            
            
$price $pizzaPrices[$type][$size];
            
$tax 0.0975 * (float) $price;
            
$total = (float) $price + (float) $tax;
            
            
$output "<h1 class='centerText'>Thank you for your order. Here is your receipt.</h1>
                <table class='wrapper' >
                  <tr><td>
                    <table class='receipt'>
                    <tr><th colspan='2'>PHP Harry's Pizza</th></tr>
                    <tr class='rec'><td class='centBold'>Kind of pizza</td><td class='rec'>
$type</td></tr>
                    <tr ><td class='centBold'>Size</td><td class='rec'>
$size</td></tr>
                    <tr ><td class='centBold'>Price</td><td class='money'>$ "
.number_format($price2'.'',' )."</td></tr>
                    <tr ><td class='centBold'>Tax</td><td class='money'>$ "
.number_format($tax2'.'',' )."</td></tr>
                    <tr ><td class='centBold'>Total</td><td class='money'>$ "
.number_format($total2'.'',' )."</td></tr>
                    </table>
                  </td></tr>
                </table></br>
                <form id='myform' class='center' method='POST' >
                <h1><button class='center' type='submit' name='done'>Pay Bill</button></h1>
                </form>"
;
                
            print 
$output;
            }
            
            
            
            
// the brains...
        
if ( !isset($_SESSION["placeAnOrder"] ) ) {    // *****  first time through...display menu and order form
        
                
$placeAnOrder true;                    // set a flag for next time through
                
$_SESSION["placeAnOrder"] = $placeAnOrder;
                
                
setPizzaArray();                        // create price list and store it
                
$_SESSION["pizzaPrices"] = $pizzaPrices;
                
displayMenu();                            // show the menu
                
printOrderForm();                        // show an order form
            
            
} else if ( $_SESSION["placeAnOrder"] ) {     // *****  has placed an order...display a receipt

                
global $type$size;
                
$pizzaPrices $_SESSION["pizzaPrices"];    // recall price list
                
                
$type $_REQUEST["type"];                    // get type and size of pizza ordered
                
$size $_REQUEST["size"];
                
                
displayReceipt();                        // display receipt
                
                
$placeAnOrder false;                    // mark order as processed
                
$_SESSION["placeAnOrder"] = $placeAnOrder;                
                
            } else {                                     
// *****  order has been processed...ward off future business...
            
                
ECHO <<<HERE
                <h1 class='centerText'>Thank you for your patronage</br>Your pizza will be delivered LATE and COLD!!</h1>
                <form id='myform' class='center' method='POST' >
                <h1><button class='center' type='submit' >Done</button></h1>
                </form>
HERE;

                
session_destroy();                         // reset session for next order
                
            
}
        
      
?>

<a href='../index.html#test2' >Main Page</a>
   </body>
</html>