#!/usr/bin/perl

@input = <>;
chomp @input;
$title = $input[0];

print <<EOF;
<html>
<head>
<title>
$title
</title>
</head>
<body bgcolor="#ccccff">

<script language="javascript"><!-- Hide from old browsers

// buyItem - adds an item to the order
function buyItem(item, description, price, quant, orderstring) {
  if (quant <= 0) {
    rc = alert('The quantity entered is incorrect');
  } else {
    if (
       confirm('Add '+quant+' of item '+item+' ('+description+') to your order?'))
    {
      index = document.cookie.indexOf("TheOrder");
      countbegin = (document.cookie.indexOf("=", index) + 1);
      countend = document.cookie.indexOf(";", countbegin);
      if (countend <= 0) {
        countend = document.cookie.length;
      }
      document.cookie=
        "TheOrder="+   
        document.cookie.substring(countbegin, countend)+
        "["
        +orderstring+
        "]";
    }
  }
}
  
function reset() {
  document.cookie="TheOrder=;";
}
// -->
</script>

<center>  
<a href="index.html">[index]</a>&nbsp;
<a href="review.html">[review order]</a>&nbsp;
<a href="bbuy.html">[finish order]</a>&nbsp; <br>

<P><B>   
$title
</B>

<form name="add"
      action="never_submitted_url"
      method="post">
<table border=1>
<tr>
EOF

#print out the table headers
@th = split /\|/,$input[1];
foreach $s (@th) {
    ($s) = ($s =~ /\s*(\S.*\S)\s*/);
    print "  <th>$s</th>\n";
}
print "</tr>\n";

#   print out the table rows.  each row is a potentially complex affair,
#   with multiple select boxes.  There are four simple fields -- the
#   item code, the description, the quantity, and the price -- that 
#   are handled specially.  Additionally, all the fields are concatenated
#   into a single string to pass to the cgi script that handles the orders

$d = "document.add";  # so the names don't get so long...
for ( $i = 2;  $i <= $#input; $i++ ) {
    print "<tr>\n";
    $line = $input[$i];
    @td = split /\s*\|\s*/, $line;

#   print the table cell descriptions
    $j = 0;
    while ( $j <= $#td ) {
        print "  <td>\n";

        $s = $td[$j];
        $s =~ s/^\s*//;
        $s =~ s/\s*$//;
        $f = $th[$j];
        ($f) = ($f =~ /\s*(\S.*\S)\s*/);
        $field_name = $f . $i;
	push @fnames,("$field_name");
        if ( $f eq "item" ) { $item = $s; };
        if ( $f eq "description" ) {$description = $s;};
        if ( $f eq "qty" ) {$qty = "$d.${field_name}.value";};
        if ( $f eq "price" ) {$price = $s;};###
        push @val,($s);
#       text input box 
        if ( $s =~ /__/ ) {
            push @type,("text");
            $width = 4;
            if ( ($w) = ($s =~ /__:(\d+)/) ) {
                $width = $w;
            }
            print "    <input type=\"text\" ";
            print "name=\"$field_name\" size=\"$width\">\n";
        } 
#       a select box
        elsif ( $s =~ /^\{/ ) {
            push @type,("select");
            print "    <select name=\"$field_name\">\n";
            ($s) = ($s =~ /\{(.*)\}/);
            @s = split /\s*:\s*/,$s;
            foreach $o (@s) {
                print "      <option selected value=\"$o\">$o</option>\n";
            }
            print "    </select>\n"
        }
#       a description, price, or other fixed text field
        else {
            push @type,("label");
            print "    $s\n";
        }
        print "  </td>\n";
        $j++;
    }
#   the "add" button at the end...
    print "  <td>\n";
    print "    <input type=\"button\" name=\"add\" value=\"add\"";
    print " onclick=\"buyItem(";
    print "'$item','$description','$price',$qty,";
    $k = 0;
    foreach $n (@fnames) {
        print "+'|'+" if $k > 0;
        print "'$n:'+";
        if ( $type[$k] eq "label" ) {
            print "'$val[$k]'";
        } elsif ( $type[$k] eq "text" ) {
            print "$d.$n.value";
        } else {
            print "$d.$n.options[$d.$n.selectedIndex].text";
        }
        $k++;
    }
    print ")\">\n  </td>\n</tr>";
}
print <<EOF;
</table>
</form>
</body>
</html>
EOF
