#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

# Get the input

   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs

   @pairs = split(/&/, $buffer);

# Load the FORM variables

   foreach $pair (@pairs) {
       ($name, $value) = split(/=/, $pair);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    $FORM{$name} = $value;


}

&thank_you;

}


sub thank_you {

    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE>Math and You!</TITLE>\n";
    print "</HEAD>\n";
    print "<BODY BGCOLOR=#FFFFCC TEXT=#000000>\n";
    print "<H3>Here are those answers you requested...</H3>\n";
    print "\n";
    print "<P>\n";

# Simply list the choices the user made
    print "You Chose $FORM{singledgt}, $FORM{doubledgt}, and $FORM{dinner}. Fine choices.<P>";

# Add the two numbers together
    print "Did you know that $FORM{singledgt} plus $FORM{doubledgt} equals ";
    print $FORM{singledgt} + $FORM{doubledgt};

# Square the single digit
    print "<P>$FORM{singledgt} squared is ";
    print $FORM{singledgt}*$FORM{singledgt};

# How much bigger is the bigger digit than the smaller digit?
    print "<P>The number $FORM{doubledgt} is ";
    print $FORM{doubledgt} - $FORM{singledgt};
    print " greater than $FORM{singledgt}";

# Figure Tip
    print "<P>If you spent &#0036;$FORM{dinner} for dinner, the traditional 15% tip would be: &#0036;";
    print $FORM{dinner}*.15;
    print " for total of &#0036;";
    print $FORM{dinner}+($FORM{dinner}*.15);
    print ".";

# Use Branch to test if user spends over 100 dollars on dinner
    if ($FORM{dinner} < 100)
       {print "<P><b>Wait!</b> You didn't spend at least 100 dollars on a dinner date? Cheap!";}
    else
       {print "<P><b>Wait!</b> You really spend that much on dinner dates? Can we go out? I'm hungry.";}


    print "<P>\n";
    print "</BODY>\n";
    print "</HTML>\n";
    exit(0);
}