#!/usr/local/bin/perl

  # Read in text
  if (($ENV{'REQUEST_METHOD'} eq "GET")) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

#  print "<PRE>\n";
#  print $in, "\n"; 
#  print "</PRE>\n";

  @in = split(/&/,$in);





  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Split into key and value.  
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;

    # Associate key and value
    $in{$key} .= "\n" if (defined($in{$key})); # \n is the multiple separator
    $in{$key} .= $val;

}


if ($in{"email"} eq "") {
   open(GRADER, ">>../grader-forms/no-email-addresses"); 
}
else {
   open(GRADER, ">../grader-forms/" . $in{"email"}); 
}
print GRADER "\nName:  ", $in{"firstname"}, " ", $in{"lastname"}, "\n"; 
print GRADER "BU ID:  ", $in{"buid"}, "\n"; 
print GRADER "Email:  ", $in{"email"}, "\n"; 
print GRADER "Phone:  ", $in{"phone"}, "\n"; 
print GRADER "Address:  \n\n", $in{"address"}, "\n\n"; 
print GRADER "Major:  ", $in{"major"}, "\n"; 
print GRADER "Year:  ", $in{"year"}, "\n"; 
print GRADER "GPA:  ", $in{"gpa"}, "\n\n"; 
print GRADER "First Choice:  ", $in{"firstchoice"}, " - ", $in{"firstgrade"}, "\n"; 
print GRADER "Second Choice:  ", $in{"secondchoice"}, " - ", $in{"secondgrade"}, "\n"; 
print GRADER "Third Choice:  ", $in{"thirdchoice"}, " - ", $in{"thirdgrade"},  "\n\n"; 
print GRADER "Times graded before:  ", $in{"timesgraded"}, "\n"; 
print GRADER "Previous courses:\n\n", $in{"pastcourses"}, "\n\n"; 
print GRADER "Comments: \n\n ", $in{"comments"}, "\n\n"; 
close(GRADER); 


    print "Content-type: text/html\n\n";
    print "<HTML>\n";
    print "<HEAD>\n";
    print "<TITLE> Grader Application: Response</TITLE>\n";
    print "</HEAD>\n";
    print "<BODY>\n";
print "<p> <hr> <p> ";
print "<h4> You have submitted the following information to our grader
database:</h4>";
print "<p> <hr> <p> ";
print "<pre>";
print "\nName:  ", $in{"firstname"}, " ", $in{"lastname"}, "\n"; 
print "BU ID:  ", $in{"buid"}, "\n"; 
print "Email:  ", $in{"email"}, "\n"; 
print "Phone:  ", $in{"phone"}, "\n"; 
print "Address:  \n\n", $in{"address"}, "\n\n"; 
print "Major:  ", $in{"major"}, "\n"; 
print "Year:  ", $in{"year"}, "\n"; 
print "GPA:  ", $in{"gpa"}, "\n\n"; 
print "First Choice:  ", $in{"firstchoice"}, " - ", $in{"firstgrade"}, "\n"; 
print "Second Choice:  ", $in{"secondchoice"}, " - ", $in{"secondgrade"}, "\n"; 
print "Third Choice:  ", $in{"thirdchoice"}, " - ", $in{"thirdgrade"},  "\n\n"; 
print "Times graded before:  ", $in{"timesgraded"}, "\n"; 
print "Previous courses:\n\n", $in{"pastcourses"}, "\n\n"; 
print "Comments: \n\n ", $in{"comments"}, "\n\n"; 

print "</pre>";
print "<p> <hr> <p> ";
    print "Thanks for applying for a grader position. You 
may resubmit the form at any time to update your information. We will
be posting the grading assignments during the second week of classes.\n"; 
print "<p> <hr> <p>" ;
    print "</BODY>\n";
    print "</HTML>\n";






