#! /usr/bin/perl

use CGI ':standard';
use CGI::Carp "fatalsToBrowser";

# W. Sterner CS101   -- Homework 5, Problem 3, Page b
# Basic strategy is to receive initial input from Page a via param variables.
# Then check each data item against is constraints, with each successful
# validation incrementing a $done variable.  There are 8 items, so a fully
# validated input would have $done == 8.

# Two separate Forms elements are used. First to validate; Second to submit
# to Page c.  All Data variables have scope across both Forms elements.

    print header, start_html('Validate User Input');
print br;

#-----------------Begin first of two forms in this page.
print '<form action="http://people.cs.uchicago.edu/~sterner/cs101/testpl/pg53ba.pl" method="post">';

#-----------------Set Validation Counter Variable to 0 -- for each return check until all entries are validated.
$done = 0;
#Debug code: print br, "Done1=  $done";

#-----------------Recieve Data from First Input page (pg53aa.pl)
##--Personal Data
$firstN=param('Fname');
$lastN=param('Lname');
$addr=param('Address');
$city=param('City');
$USstate=param('State');
$Codezip=param('Zip');
$email=param('Email');
##-- Preference Data
$criticOf=param('expert');



#-----------------Validate First Name -- text field must be non-empty.
if ($firstN eq "") {
    print br, 'You have not entered your First Name.  Please do so here:  ';
    print "<input type=\"text\" size=\"15\" maxlength=\"20\" name=\"Fname\" >";
       # Gives new text form for revised input.
}
elsif ($firstN ne "") {    
    print br, br,    'First name:  <br/>';
    print "<input type=\"text\" size=\"15\" maxlength=\"20\" name=\"Fname\" value=\"$firstN\">";
       # Provides Validated Data
    $done = $done + 1;  # Increment Validation Counter
}
   

#-----------------Validate Last Name -- text field must be non-empty.
if ($lastN eq "") {
    print br, 'You have not entered your Last Name.  Please do so here:  ';
    print  '<input type="text" size="15" maxlength="20" name="Lname">';
}
elsif ($lastN ne "") {    
    print br, 'Last name:  <br/>';
    print  "<input type=\"text\" size=\"15\" maxlength=\"20\" name=\"Lname\" value=\"$lastN\">";
    $done = $done + 1;}

#-----------------Validate Address -- text field must be non-empty.
if ($addr eq "") {
    print br, 'You have not entered your Address.  Please do so here:  ';
    print  '<input type="text" size="15" maxlength="20" name="Address">';
}
elsif ($addr ne "") {
    print br, 'Address:  <br/>';
    print  "<input text type=\"text\" size=\"15\" maxlength=\"20\" name=\"Address\" value=\"$addr\">";
    $done = $done + 1;}

#-----------------Validate City -- text field must be non-empty.
if ($city eq "") {
    print br, 'You have not entered your City.  Please do so here:  ';
    print  '<input type="text" size="15" maxlength="20" name="City">';
}
elsif ($city ne "") {
    print br, 'City:  <br/>';
    print  "<input type=\"text\" size=\"15\" maxlength=\"20\" name=\"City\" value=\"$city\">";
    $done = $done + 1;}

#-----------------Validate State -- text field must be non-empty, and be 2 capital letters exactly.
if ($USstate eq "") {
    print br, 'You have not entered your State.  Please do so here:  ';
    print  '<input type="text" size="2" maxlength="2" name="State">';
}
else {
    $USstate =~ tr/[a-z]/[A-Z]/;  #translate lower case to upper case letters.
    if ($USstate !~ m/[A-Z][A-Z]/) {
	print br, 'Something is wrong with your State Code.',br;
	print "You entered:  $USstate    Please correct it here:  ";
        print  "<input type=\"text\" size=\"2\" maxlength=\"2\" name=\"State\" value=\"$USstate\">";
    }
    elsif ($USstate =~ m/[A-Z][A-Z]/) {
        print br, 'State:  <br/>';
        print  "<input type=\"text\" size=\"2\" maxlength=\"2\" name=\"State\" value=\"$USstate\">";
	$done = $done + 1;}
}

#-----------------Validate Zip -- text field must be non-empty, and precisely 5 digits.
if ($Codezip eq "") {
    print br, 'You have not entered your Zip.  Please do so here:  ';
    print '<input type="text" size="5" maxlength="5" name="Zip">';
}
elsif ( $Codezip !~ m/\d\d\d\d\d/) { #alt syntax: !~ m/[0-9]{5}/
    print br, "You entered:  $Codezip for your zipcode.   Please correct it here:  ";
    print  "<input type=\"text\" size=\"5\" maxlength=\"5\" name=\"Zip\" value=\"$Codezip\">";
}
elsif ( $Codezip =~ m/\d\d\d\d\d/) {
    print br, 'ZIP:  <br/>';
    print  "<input type=\"text\" size=\"5\" maxlength=\"5\" name=\"Zip\" value=\"$Codezip\">";
    $done = $done + 1;}

#-----------------Validate Email -- text field must be non-empty, and be of anyChar@anyChar.anyChar format.
if ($email eq "") {
    print br, 'You have not entered your Email.  Please do so here:  ';
    print  '<input type="text" size="15" maxlength="20" name="Email">';
}
elsif    ( $email !~ m/.+\@.+\..+/ ) {
    print br, "You entered:  $email for your email address.   Please correct here:  ";
    print  "<input type=\"text\" size=\"15\" maxlength=\"20\" name=\"Email\" value=\"$email\">";
}
elsif  ( $email =~ m/.+\@.+\..+/ )  {
    print br, 'Email:  <br/>';
    print  "<input type=\"text\" size=\"15\" maxlength=\"20\" name=\"Email\" value=\"$email\">";
    $done = $done + 1;}


#-----------------Validate Area of Expertise -- selection field must 
#be one of three recognized possbilities.
#Debug code: print br, 'Area of Expertise:B4 ', "$criticOf";

if ($criticOf eq "No Selection") {
    print br, 'You have not chosen an aread of critical expertise.',br,'Please do so here: ';
print br, br,'Area of Expertise: <br/>';
print '<select name="expert" size=1>';
print '<option > Dance Critic</option>';
print '<option > Film Critic</option>';
print '<option > Literary Critic</option>';
print '</select>';
}
else {
    print br, 'Area of Expertise: ',br; #   "$criticOf";
    print "<select name=\"expert\" size=\"1\" value=\"$criticOf\">";
    print "<option selected>$criticOf</option>";
    print '<option > Dance Critic</option>';
    print '<option > Film Critic</option>';
    print '<option > Literary Critic</option>';
    print '</select>';
    $done = $done + 1;
}

print br,  submit("Check Again");
print ' </form>';  #=============End of Validation Checking Form


#----------------Submission to Problem 3 Form c
#Debug code: print br, "Done2=  $done";
if ($done == 8) {print br,  "Your data has been verified.";
    print '<form action="http://people.cs.uchicago.edu/~sterner/cs101/testpl/pg53c.pl" method="post">';
    print br,  submit("Submit");
#-----------------Hidden Fields to transmit validated data
  print "<input  type=\"hidden\" size=\"15\" maxlength=\"20\" name=\"Fname\" value=\"$firstN\">";
  print "<input type=\"hidden\" size=\"15\" maxlength=\"20\" name=\"Lname\" value=\"$lastN\">";
  print "<input  type=\"hidden\" size=\"15\" maxlength=\"20\" name=\"Address\" value=\"$addr\">";
  print "<input type=\"hidden\" size=\"15\" maxlength=\"20\" name=\"City\" value=\"$city\">";
  print  "<input type=\"hidden\" size=\"2\" maxlength=\"2\" name=\"State\"  value=\"$USstate\">";
  print  "<input type=\"hidden\" size=\"5\" maxlength=\"5\" name=\"Zip\" value=\"$Codezip\">";
  print  "<input type=\"hidden\" size=\"15\" maxlength=\"20\" name=\"Email\" value=\"$email\">";
  print  "<input type=\"hidden\" size=\"20\" maxlength=\"20\"  name=\"expert\"  value=$criticOf>"; 

      # Can use the last line to illustrate 2 cases of  BAD SYNTAX in the last print statement
      # 1) Removing backslashs around "hidden" gives a fatal syntax error.
      #  Needs esc-" , (i.e., \" ) instead of explicit double-quote.
#syntax error at /home/sterner/html/cs101/testpl/pg53ba.pl line 170, near ""<input type="hidden"

    # 2) Note that here the input variables are to be evaluated: value=$criticOf 
    # and should be  enclosed within escaped double-quotes.
    # It should be:
#  print  "<input type=\"hidden\" size=\"20\" maxlength=\"20\"  name=\"expert\"  value=\"$criticOf\">"; 

#Note that here the input variables are to be evaluated AND  enclosed within quotes to get proper
# interpretation by receiving param function.
      #See Lash p. 194 for examples.
    print ' </form>';
	     } #=============End of Submission Form



## TEST quote syntax:
print 'Some padding text: value=$criticOf<br/> ';
##result:  Some padding text: value=$criticOf


print "value=$criticOf";  #evaluates variable and no quotes.
##result:  value=Dance Critic
#While this evaluates to the full content, only Dance is received in param.

print br, "value=\"$criticOf\""; #evaluates variable and places 
##within quotes.   This is needed for input of more than one 
##word to be properly passed in a form element such as a hidden form.
##result:  value="Dance Critic"

#print br,  "value="$criticOf""; #produces error
##syntax error at line 180, near ""value="$criticOf"

print  end_html;
