#!/usr/bin/perl

use CGI qw(:standard);

#preprocessing - gather cookies and form fields
@names=(Squirrels,Rabbits,Dogs,Cows,Trees);
@prices=(5,6,4,6,12);
$basket=cookie('basket');
if ($basket){
  @quantity=split(/,/, $basket);
  $goods=join(",",@quantity);
} else {
  $goods='0,0,0,0,0';
}
if (param('item')) {
#add the item to the cart
@quantity[param('item')-1]=param('quant');
$goods=join(",",@quantity);
}
if ($goods ne $basket) {
  $cookie = cookie(-name=>'basket',
		-value=>$goods,
		-expires=>'+15m',
                -path=>'/');
  print header(-cookie=>$cookie);
} else {
  print header();
}
# send out the page
print start_html("Josie's Fine Caramel Treats");
print h1("Josie's Fine Caramel Treats");
print p("Welcome to Josie's Fine Caramel Treats, where we treat you like a fine treat.");
print p("Caramel lovers the world over have visited our store in search of the finest caramels.");
print hr();
print "<TABLE BORDER=1>";
print "<TR><TH>Item</TH><TH>Price/lb.</TH><TH>Quantity (lbs.)</TH><TH>Total</TH></TR>";
for ($i=0; $i<5; $i++) {
  $item=$i+1;
  print "<TR><TD><A HREF='item$item.pl'>Caramel @names[$i]</A></TD><TD>";
  print "\$@prices[$i].00</TD><TD>";
  print "@quantity[$i]</TD><TD>";
  print '$',@quantity[$i]*@prices[$i],'.00</TD></TR>';
  $total+=@quantity[$i]*@prices[$i];
}
print "</TR><TD></TD><TD></TD><TD></TD><TD>\$$total.00</TD></TR>";
print "</TABLE>";
print "<A HREF='register.pl'>Advance to the checkout</A>";
print end_html();