#!/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 'new_animal'
@neworder = param;
foreach $na (@neworder) {
	$val = param($na);
	$orders{$na} = $val;
}

# Put them in a cookie
$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 start_html('cookie order'),
      h1('Cookie Order'),
      "Your ID = $ID<BR>",
      "Enter the number of boxs for each cookie you like to order",
      hr,
      start_form("POST", "/cgi-bin/op.pl");
print "previous order<br>";
foreach $key (keys %orders) {
	print "$key=$orders{$key}<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>", 
              textfield(-name=>"$cookie",
                        -default=>"$orders{$cookie}",
                        -size=>10), 
              "</td></tr>";
}
print "</table>";
    print submit(-name=>"action", -value=>"Order");
    print reset;
    end_form;
print end_html;


