#!/usr/bin/perl

use CGI qw(:standard);
if (!(cookie('basket'))) {
  print redirect('http://127.0.0.1/cgi-bin/shopping/main.pl');
} else {
  #preprocessing - gather cookies and form fields
  @names=(Squirrels,Rabbits,Dogs,Cows,Trees);
  @prices=(5,6,4,6,12);
  $basket=cookie('basket');
  @quantity=split(/,/, $basket);
  print header();

# send out the page
  print start_html("Josie's Fine Caramel Treats");
  print h2("Josie's Fine Caramel Treats");
  print p("Welcome to the checkout, where you can prepare to pay for your caramels.");
  print hr();
  print start_form('POST','final.pl');
  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>Caramel @names[$i]</TD><TD>";
    print "\$@prices[$i].00</TD><TD>";
    print "@quantity[$i]</TD><TD>";
    print '$',@quantity[$i]*@prices[$i],'.00</TD>';
    print hidden(-name=>"item$item",-value=>@quantity[$i]);
    $total+=@quantity[$i]*@prices[$i];
    print '</TR>';
  }
  print "</TR><TD></TD><TD></TD><TD></TD><TD>\$$total.00</TD></TR>";
  print "</TABLE>";
  print '<P>All orders are shipped personally by our payment collection specialists, Guido and Fluffy.  If we have to hunt you down, we will.<BR>';
  print 'Name:',textfield('name');
  print ' Phone Number:', textfield('phone'), '<BR>';
  print 'Address:<BR>',textarea(-name=>'address',-rows=>4,-columns=>20),'&nbsp;';
  print submit('Submit your order');
  print end_form();
  print end_html();
}