#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);
use strict;

print header;
print start_html("Order Form - Step 2");
print <<EndHead;
<h2 align="CENTER">Order Form - Step 2</h2>
Here's what you've ordered:<br>
<form action="order2.cgi" method="POST">
EndHead

open(INF, "data.db" ) or &dienice("Can't open data.db:<br> $! \n");
flock(INF, LOCK_SH );    # shared lock
seek(INF, 0, SEEK_SET ); # rewind to beginning
my @data = <INF>;
close(INF);

my $subtotal = 0;
foreach my $i (@data) {
    chomp($i);
    my($stocknum, $name, $status, $price ) = split (/\|/, $i );
    if (param($stocknum)) {
        my($qty) = param($stocknum);
        $subtotal = $subtotal + ($price * $qty);
        print qq(<b>$name</b> (#$stocknum) - $price ea., qty: $qty<br>\n);
        print qq(<input type="hidden" name="$stocknum" value="$qty">\n);
    }
}

if ($subtotal == 0 ) {
    &dienice("You didn't order anything!");
}

print <<EndForm;
<p>
Subtotal:<br> \$$subtotal
<p>
Please enter your shipping information:<br><br>
<pre>

        Your Name: <input type="text" name="name" size=50>
 Shipping Address: <input type="text" name="ship_addr" size=50>
             City: <input type="text" name="ship_city" size=50>
   State/Province: <input type="text" name="ship_state" size=30>
  ZIP/Postal Code: <input type="text" name="ship_zip" size=30>
          Country: <input type="text" name="ship_country" size=30>
            Phone: <input type="text" name="phone" size=30>
            Email: <input type="text" name="email" size=30>
</pre>
Payment Method:
<select name="paytype">
<option value="cc">Credit Card
<option value="check">Check/Money Order
<option>Paypal
</select>
<br><br>

<input type="submit" value="Place Order">
</form>
EndForm

print end_html;

sub dienice {
    my ($msg) = @_;
    print "<h2>Error</h2>\n";
    print $msg;
    exit;
}