#!/usr/bin/perl

use CGI qw(:standard);

@cookies=sort qw/peanut mint chocolate butter mm/;

# Recover the previous cookie order from the magic cookie.
# The cookie has been formatted as an associative array
# mapping cookie name to the number of cookies package ordered.
%orders = cookie('cookie_order');
$ID = cookie('ID');
#if there is no unique ID create one
if (length($ID) eq 0) {
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	$ID = "CS401_$year\_$mon\_$mday:$hour:$min:$sec"; 
}

# Recover the new order(s) from the parameter
@neworder = param;
foreach $na (@neworder) {
	$val = param($na);
	$orders{$na} = $val;
}

# Put them in a cookie
# Print the header, incorporating the cookie and the expiration date...
$the_cookie = cookie(-name=>'cookie_order',
		     -value=>\%orders,
		     -expires=>'+1h');
$cookie2 = cookie(-name=>'ID',
                  -value=>$ID,
                  -expires=>'+3333h');
# Print the header, incorporating the cookie and the expiration date...
print header(-cookie=>[$the_cookie,$cookie2]);
# Now we're ready to create our HTML page.
print header('text/html');
print start_html('cookie order'),
      h1('Cookie Order'),
      "Order# = $ID<BR>",
      "Please confirm the order",
      hr;
print "previous order<br>";
foreach $key (keys %orders) {
	print "$key=$orders{$key}<br>";
}
print start_form("POST", "/cgi-bin/cp.pl");
print "Order#=$ID<br>";
print "<table BORDER=2>";
print "<tr><td><b>Cookie</b></td><td><b># of boxes</b></td></tr>";
foreach $cookie (@cookies) {
	print "<tr><td>$cookie</td><td align=right>$orders{$cookie}</td></tr>";
}
print "</table>";
    print submit(-name=>"action", -value=>"confirm");
    print submit(-name=>"action", -value=>"cancel");
    end_form;
print end_html;


