#!/usr/local/bin/perl

%results = parse_form();
if (!%results){
print "Content-type: text/html\n\n";
print<<BEGIN
<HTML>
<HEAD>
<TITLE>Names and Values</TITLE>
</HEAD>
<BODY bgcolor="blue" text="white">
<H2>Ordering your new bed</H2>
<FORM  METHOD=POST action="sendcookie.pl" >
Name: <INPUT TYPE="text" NAME="name">
Address: <INPUT TYPE="text" NAME="address" SIZE=30>
<P>City: <INPUT TYPE="text" NAME="city">
State: <INPUT TYPE="text" NAME="state" SIZE=2 MAXLENGTH=2>
Zipcode: <INPUT TYPE="text" NAME="zip" SIZE=5 MAXLENGTH=5>
Customer Code: <INPUT TYPE="password" NAME="code" SIZE=8>
<HR>
<B>Type of wood:</B>
<SELECT NAME="woodtype">
<OPTION VALUE="Mahogany">Mahogany
<OPTION VALUE="Maplewood">Maplewood
<OPTION VALUE="Pine">Pine
<OPTION VALUE="Cherry">Cherry
</SELECT>
<B>Size:</B>
<INPUT TYPE="radio" NAME="size" VALUE="K">King
<INPUT TYPE="radio" NAME="size" VALUE="Q">Queen
<INPUT TYPE="radio" NAME="size" VALUE="T">Twin
<INPUT TYPE="radio" NAME="size" VALUE="S">Single
<P><B>Extras:</B>
<INPUT TYPE="checkbox" NAME="extras" VALUE="foot">Footboard
<INPUT TYPE="checkbox" NAME="extras" VALUE="drawers">Drawers (for
underneath)
<INPUT TYPE="checkbox" NAME="extras" VALUE="casters">Casters
<INPUT TYPE="checkbox" NAME="extras" VALUE="nosqueak">Squeak proofing
<HR>Please share any suggestions or comments with us:
<TEXTAREA NAME="comments" ROWS=3 COLS=65 WRAP>Comments?</TEXTAREA>
<HR>
<INPUT TYPE="SUBMIT" VALUE="Order Bed" NAME="submit">
<INPUT TYPE="RESET" VALUE="Edit" NAME="startover">
</FORM>
</BODY>
</HTML>
BEGIN
  ;
  return;
}
else{
print "Set-Cookie:information= $formdata{'$key'}\n";
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>Thanks for choosing!</TITLE></HEAD><BODY>";
print<<END
<FORM  METHOD=POST action="readfile2.pl" >
Name: <INPUT TYPE="text" NAME="name" value=$formdata{'name'}>
Address: <INPUT TYPE="text" NAME="address" SIZE=30
value=$formdata{'address'}>
<P>City: <INPUT TYPE="text" NAME="city" value=$formdata{'city'}>
State: <INPUT TYPE="text" NAME="state" SIZE=2 MAXLENGTH=2
value=$formdata{'state'}>
Zipcode: <INPUT TYPE="text" NAME="zip" SIZE=5 MAXLENGTH=5
value=$formdata{'zip'}>
Customer Code: <INPUT TYPE="password" NAME="code" SIZE=8
value=$formdata{'code'}>
<HR>
<B>Type of wood:</B>
<SELECT NAME="woodtype" value=$formdata{'woodtype'}>
<OPTION VALUE="Mahogany" value=$formdata{'woodtype'}>Mahogany
<OPTION VALUE="Maplewood" value=$formdata{'woodtype'}>Maplewood
<OPTION VALUE="Pine" value=$formdata{'woodtype'}>Pine
<OPTION VALUE="Cherry"value=$formdata{'woodtype'} >Cherry
</SELECT>
<B>Size:</B>
<INPUT TYPE="radio" NAME="size" VALUE="K" value=$formdata{'size'}>King
<INPUT TYPE="radio" NAME="size" VALUE="Q" value=$formdata{'size'} >Queen
<INPUT TYPE="radio" NAME="size" VALUE="T" value=$formdata{'size'} >Twin
<INPUT TYPE="radio" NAME="size" VALUE="S" value=$formdata{'size'} >Single
<P><B>Extras:</B>
<INPUT TYPE="checkbox" NAME="extras"
VALUE="foot" value=$formdata{'extras'} >Footboard
<INPUT TYPE="checkbox" NAME="extras" VALUE="drawers"
value=$formdata{'extras'} >Drawers (for
underneath)
<INPUT TYPE="checkbox" NAME="extras" VALUE="casters"
value=$formdata{'extras'} >Casters
<INPUT TYPE="checkbox" NAME="extras" VALUE="nosqueak"
value=$formdata{'extras'} >Squeak proofing
<HR>Please share any suggestions or comments with us:
<TEXTAREA NAME="comments" ROWS=3 COLS=65 WRAP value=$formdata{'comments'} >Comments?</TEXTAREA>
<HR>
<INPUT TYPE="SUBMIT" VALUE="Order Bed" NAME="submit">
<INPUT TYPE="RESET" VALUE="Start Over" NAME="startover">       
</FORM>
</BODY>
</HTML>
END
  ;
}

sub parse_form{
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
	@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
	read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
} else {
print "Content-type: text/html\n\n";
	print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
	($key, $value) = split (/=/, $pair);
	$key =~ tr/+/ /;
	$key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
	$formdata{$key} .= ", $value";
} else {
	$formdata{$key} = $value;
}
}
}
1;
