#! /usr/local/bin/perl

# defaults, change these and the #! line to move
$datadir = ".";
$dataurl = "/challenge/Archive/98-99/awardsday";
$header = "header.html";
$footer = "footer.html";
$mail = "chidhkra\@mode.lanl.k12.nm.us";
$datafile = "data.txt";

# the all important error sub routine
sub error
{
	($title, @rest) = @_;
	$myform = "<center><table border=1><tr><th>key</th><th>value</th></tr>";
	foreach $key (sort(keys(%form)))
		{ $myform .= "<tr><td>$key</td><td>$form{$key}</td></tr>"; }
	$myform .= "</table></center>";
	print <<"ERROR";
Content-Type: Text/html


<html>
<head>
<title>$title</title>
</head>
<body bgcolor="black" text="yellow">
<h1>Ooops...</h1>
<img src="$dataurl/boom.gif" align=left>
<h1>$title</h1>
The following error has occurred:
<hr>
@rest
<br clear=all>
<hr>
Form data:
<pre>
$myform
</pre>
<hr>
If you were not expecting this error, please report it to <a
href="mailto:$mail">chidhkra\@mode.lanl.k12.nm.us</a>. <p>
<p>
Sorry for the problem<p>
$foot
ERROR
	exit(0);
}

sub checkperson
{
	local($pre, %form) = @_;
	
	local($pres) = sprintf("%ss", $pre);
	error("$pre missing or incomplete", 
		"$pres entry was missing or incomplete from the submission")
		unless $form{$pres};
	error("form sent wrong data", "$pres was out of range")
		unless ($form{$pres} eq "Male" ||
		$form{$pres} eq "Female" ||
		$form{$pres} eq "-");
}

#from sanity check needs check person
sub formsanity
{
	local(%form) = @_;
	local($pre);

	foreach $pre ("t1", "t2", "a", "d", "s1", "s2", "s3", "s4", "s5")
	{
		checkperson($pre, %form);
	}
}
	
# setsexopt, sets up the html options for different Genders passed in
sub setsexopt
{
	local($sex) = @_;
	$sexopt = "<option>Male<option selected>-<option>Female";
	$sexopt = "<option selected>Male<option>Female" if $sex eq
	"Male";
	$sexopt = "<option>Male<option selected>Female" if $sex eq
	"Female";
	return($sexopt);
} 

# settourday, sets up the html options for which day they want to tour
sub settourday
{
	local($tourday) = @_;
	$tourdayopt = "<option selected>Tuesday<option>Wednesday";
	$tourdayopt = "<option selected>Tuesday<option>Wednesday"
	  if $tourday eq "Tuesday";
	$tourdayopt = "<option>Tuesday<option selected>Wednesday"
	  if $tourday eq "Wednesday";
	return ($tourdayopt);
}

# setcomingopt, sets the html options for different answers to coming
sub setcomingopt
{
	local($coming) = @_;
	local($comingopt);
	if($coming eq "on")
	{
		$comingopt = "checked";
	}
	return($comingopt);
}

# print form, prints the form out, requires, setsexopt, setcomingopt
sub printform
{
	local($msg, %form) = @_;
	local($action) = $ENV{'SCRIPT_NAME'};
	
	local($monthopt) = "<option>April";
	$monthopt = "<option selected>April<option>May" if $form{'month'} eq "April";
	local($dayopt) = "<option>28<option>29<option selected>1<option>2<option>3";
	$dayopt = "<option selected>28<option>29<option>1<option>2<option>3" if
	$form{'day'} eq "28";
	$dayopt = "<option>28<option selected>29<option>1<option>2<option>3" if
	$form{'day'} eq "29";
	$dayopt = "<option>28<option>29<option>1<option selected>2<option>3" if
	$form{'day'} eq "2";
	$dayopt = "<option>28<option>29<option>1<option>2<option selected>3" if
	$form{'day'} eq "3";
	local($t1s) = setsexopt($form{'t1s'});
	local($t2s) = setsexopt($form{'t2s'});
	local($as) = setsexopt($form{'as'});
	local($ds) = setsexopt($form{'ds'});
	local($s1s) = setsexopt($form{'s1s'});
	local($s2s) = setsexopt($form{'s2s'});
	local($s3s) = setsexopt($form{'s3s'});
	local($s4s) = setsexopt($form{'s4s'});
	local($s5s) = setsexopt($form{'s5s'});
	local($t1c) = setcomingopt($form{'t1c'});
	local($t2c) = setcomingopt($form{'t2c'});
	local($ac) = setcomingopt($form{'ac'});
	local($dc) = setcomingopt($form{'dc'});
	local($s1c) = setcomingopt($form{'s1c'});
	local($s2c) = setcomingopt($form{'s2c'});
	local($s3c) = setcomingopt($form{'s3c'});
	local($s4c) = setcomingopt($form{'s4c'});
	local($s5c) = setcomingopt($form{'s5c'});
	local($t1h) = setcomingopt($form{'t1h'});
	local($t2h) = setcomingopt($form{'t2h'});
	local($ah) = setcomingopt($form{'ah'});
	local($dh) = setcomingopt($form{'dh'});
	local($s1h) = setcomingopt($form{'s1h'});
	local($s2h) = setcomingopt($form{'s2h'});
	local($s3h) = setcomingopt($form{'s3h'});
	local($s4h) = setcomingopt($form{'s4h'});
	local($s5h) = setcomingopt($form{'s5h'});
	local($distance) = setcomingopt($form{'distance'});
	local($tourday) = settourday($form{'tourday'});
	if($msg ne "")
	{
		print "<b>The following problems were found with your
		form:</b><p><ul>", $msg, "</ul><hr>";
	} 
	print <<"ENDFORM";
If you plan to attend, please fill out and submit this form by APRIL 7.<p>
<b>Note:</b> Teams must submit a final report in order to attend Awards Day<p>
<hr>

<form action="$action" method=post>
<b>School Name:</b> <input type="text" value="$form{'school'}" name="school" size="40"><p>
<b>Team Number:</b> <input type="text" value="$form{'team'}" name="team" size="3" maxlength="3"><p>

<hr>
<b>Notes:</b> Students and Teachers <b>Not</b> on this list will <b>Not</b> be
allowed to go on the tours. Your name on this form must be exactly as it appears
on your picture identification, which you must bring to be admitted to the program.<p>
<hr>
<table border=0>
<tr>
<th></th><th>Coming:</th><th>First:</th><th>Last:</th><th>Username:</th><th>Gender:</th><th>US Citizen:</th>
</tr>
<tr>
<th align=right rowspan=2>Teachers:</th>
<td><input type="checkbox" name="t1c" value=on $t1c></td>
<td><input type="text" name="t1f" value="$form{'t1f'}" size="10"></td>
<td><input type="text" name="t1l" value="$form{'t1l'}" size="10"></td>
<td><input type="text" name="t1m" value="$form{'t1m'}" size="10"></td>
<td><select name="t1s">$t1s</select></td>
<td><input type="checkbox" name="t1h" value=on $t1h></td>
</tr>
<td><input type="checkbox" name="t2c" value=on $t2c></td>
<td><input type="text" name="t2f" value="$form{'t2f'}" size="10"></td>
<td><input type="text" name="t2l" value="$form{'t2l'}" size="10"></td>
<td><input type="text" name="t2m" value="$form{'t2m'}" size="10"></td>
<td><select name="t2s">$t2s</select></td>
<td><input type="checkbox" name="t2h" value=on $t2h></td>
</tr><tr>
<td align=right><b>Project Advisor</b><!--<i> (If attending)</i>--><b>:</b></td>
<td><input type="checkbox" name="ac" value=on $ac></td>
<td><input type="text" name="af" value="$form{'af'}" size="10"></td>
<td><input type="text" name="al" value="$form{'al'}" size="10"></td>
<td><input type="text" name="am" value="$form{'am'}" size="10"></td>
<td><select name="as">$as</select></td>
<td><input type="checkbox" name="ah" value=on $ah></td>
</tr>
<tr>
<td align=right><b>Driver or Chaperone</b><i> (If not teacher)</i><b>:</b></td>
<td><input type="checkbox" name="dc" value=on $dc></td>
<td><input type="text" name="df" value="$form{'df'}" size="10"></td>
<td><input type="text" name="dl" value="$form{'dl'}" size="10"></td>
<td><input type="text" name="dm" value="$form{'dm'}" size="10"></td>
<td><select name="ds">$ds</select></td>
<td><input type="checkbox" name="dh" value=on $dh></td>
</tr>
<tr>
<th align=right>Students:</th>
<td><input type="checkbox" name="s1c" value=on $s1c></td>
<td><input type="text" name="s1f" value="$form{'s1f'}" size="10"></td>
<td><input type="text" name="s1l" value="$form{'s1l'}" size="10"></td>
<td><input type="text" name="s1m" value="$form{'s1m'}" size="10"></td>
<td><select name="s1s">$s1s</select></td>
<td><input type="checkbox" name="s1h" value=on $s1h></td>
</tr>
<tr>
<th></th>
<td><input type="checkbox" name="s2c" value=on $s2c></td>
<td><input type="text" name="s2f" value="$form{'s2f'}" size="10"></td>
<td><input type="text" name="s2l" value="$form{'s2l'}" size="10"></td>
<td><input type="text" name="s2m" value="$form{'s2m'}" size="10"></td>
<td><select name="s2s">$s2s</select></td>
<td><input type="checkbox" name="s2h" value=on $s2h></td>
</tr>
<tr>
<th></th>
<td><input type="checkbox" name="s3c" value=on $s3c></td>
<td><input type="text" name="s3f" value="$form{'s3f'}" size="10"></td>
<td><input type="text" name="s3l" value="$form{'s3l'}" size="10"></td>
<td><input type="text" name="s3m" value="$form{'s3m'}" size="10"></td>
<td><select name="s3s">$s3s</select></td>
<td><input type="checkbox" name="s3h" value=on $s3h></td>
</tr>
<tr>
<th></th>
<td><input type="checkbox" name="s4c" value=on $s4c></td>
<td><input type="text" name="s4f" value="$form{'s4f'}" size="10"></td>
<td><input type="text" name="s4l" value="$form{'s4l'}" size="10"></td>
<td><input type="text" name="s4m" value="$form{'s4m'}" size="10"></td>
<td><select name="s4s">$s4s</select></td>
<td><input type="checkbox" name="s4h" value=on $s4h></td>
</tr>
<tr>
<th></th>
<td><input type="checkbox" name="s5c" value=on $s5c></td>
<td><input type="text" name="s5f" value="$form{'s5f'}" size="10"></td>
<td><input type="text" name="s5l" value="$form{'s5l'}" size="10"></td>
<td><input type="text" name="s5m" value="$form{'s5m'}" size="10"></td>
<td><select name="s5s">$s5s</select></td>
<td><input type="checkbox" name="s5h" value=on $s5h></td>
</tr>
</table>
<hr>
<table border=0>
<tr>
<td rowspan=2><b>Team Contact Person:</b><br><i>(Person to contact about registration)</i></td>
<th>First:</th><th>Last:</th><th>Email:</th><th>Phone:</th>
</tr>
<tr>
<td><input type="text" name="contactf" value="$form{'contactf'}" size="10"></td>
<td><input type="text" name="contactl" value="$form{'contactl'}" size="10"></td>
<td><input type="text" name="contactm" value="$form{'contactm'}" size="20"></td>
<td><input type="text" name="contactp" value="$form{'contactp'}" size="14"></td>
</tr>
</table>
<hr>
Are you traveling more than 150 miles to get to Los Alamos? 
<input type=checkbox name="distance" value=on $distance>
<P>
Tours will take place from 1:00 to 5:00 on Tuesday, from 8:00 to 9:30
on Wednesday, and from 1:00 to 5:00 on Wednesday.  The afternoon tours
each day will be the same.  Which afternoon would your team like to go
on a tour?  (select just one day) 
<select name="tourday">$tourday</select>
<!---
<hr>
I will attend and be responsible for the supervision of the students on this
team.
<table>
<tr>
<td align=center><input type=text name="signature" value="$form{'signature'}" size="40"></td>
<td align=center><input type=text name="sigdate" value="$form{'sigdate'}" size="10"></td>
</tr>
<tr>
<th align=center>Signature of teacher / Adult accompanying team</th>
<th align=center>Date</th>
</tr>
</table>
--->
<hr>
Once you are done filling out this form, please <input type="submit"
name="submit" value="submit"> it. Or you can <a href="$action">start over</a>.
<P> Thank You<p>
</form>
ENDFORM
}

sub checkdata
{
	local(%form) = @_;
	local($msg) = "";
	
	if($form{'team'} > 128 || $form{'team'} < 1)
		{$msg .= "<li>Team number $form{'team'} is out of range<p>";}
	foreach $pre ("t1", "t2", "a", "d", "s1", "s2", "s3", "s4", "s5")
	{
		$coming = sprintf("%sc", $pre);
		$last = sprintf("%sl", $pre);
		$first = sprintf("%sf", $pre);
		$middle = sprintf("%sm", $pre);
		$sex = sprintf("%ss", $pre);
		if($form{$coming} eq "on")
		{
			$msg .= "<li>Last name missing for $form{$first}<p>" 
				unless $form{$last};
			$msg .= "<li>First name missing for $form{$last}<p>"
				unless $form{$first};
			$msg .= "<li>Gender missing for $form{$last}<p>"
				unless ($form{$sex} ne "-");
		}
	}
	$msg .= "<li>Contact name was missing<p>"
		unless ($form{'contactf'} && $form{'contactl'});
	$msg .= "<li>Contact email was missing<p>"
		unless $form{'contactm'};
	$msg .= "<li>Contact phone number was missing<p>"
		unless $form{'contactp'};
	return($msg);
}

sub escape
{
	local(@list) = @_;
	
	foreach $var (@list)
	{
		$var =~ s/\\/\\\\/g;
		$var =~ s/:/\\:/g;
		$var =~ s/!/\\!/g
	}
	return(@list);
}
sub saveindividual
{
	local($title, $pre, %form) = @_;
	
	local($last) = sprintf("%sl", $pre);
	local($first) = sprintf("%sf", $pre);
	local($middle) = sprintf("%sm", $pre);
	local($sex) = sprintf("%ss", $pre);
	local($us) = sprintf("%sh", $pre);
	$us = $form{$us} eq "on" ? "US" : "foreign";
	#error("We made it this far!");
	($title, $last, $first, $middle, $sex) = escape($title, $form{$last}, 
		$form{$first}, $form{$middle}, $form{$sex});
	return "$title:$last:$first:$middle:$sex:$us\n";
}

sub savedata
{
	local(%form) = @_;
	
	error("Datafile missing", "The data file $datafile does not exist")
		unless -e $datafile;
	open(datafile, ">> $datafile") ||
		error("Problem opening datafile", "Cannot open datafile
		$datafile: $!");
	($form{'team'}, $form{'school'}) =
		escape($form{'team'}, $form{'school'});	
	$form{'distance'} = $form{'distance'} eq "on" ? "far" : "near";
	print datafile "#$form{'team'}:$form{'school'}:$form{'distance'}\n";
	print datafile saveindividual("teacher", "t1", %form) 
		unless $form{'t1c'} ne "on";
	print datafile saveindividual("teacher", "t2", %form) 
		unless $form{'t2c'} ne "on";
	print datafile saveindividual("advisor", "a", %form) 
		unless $form{'ac'} ne "on";
	print datafile saveindividual("driver", "d", %form) 
		unless $form{'dc'} ne "on";
	print datafile saveindividual("student", "s1", %form) 
		unless $form{'s1c'} ne "on";
	print datafile saveindividual("student", "s2", %form) 
		unless $form{'s2c'} ne "on";
	print datafile saveindividual("student", "s3", %form) 
		unless $form{'s3c'} ne "on";
	print datafile saveindividual("student", "s4", %form) 
		unless $form{'s4c'} ne "on";
	print datafile saveindividual("student", "s5", %form) 
		unless $form{'s5c'} ne "on";
        ($form{'contactf'}, $form{'contactl'}, $form{'contactm'}, $form{'contactp'}) =
        escape($form{'contactf'}, $form{'contactl'}, $form{'contactm'}, $form{'contactp'});
        print datafile "contact:$form{'contactf'}:$form{'contactl'}:$form{'contactm'}:$form{'contactp'}\n";
	print datafile "Tour day:$form{'tourday'}\n";
#       print datafile saveindividual("contact", "con", %form);
	close(datafile);
}

sub printthanks
{
	local(%form) = @_;
	local($seeyou) = "April 27th";
	$seeyou = "April 28th" unless $form{'distance'} eq "on";
	print<<"THANKS";
Your registration is complete. If you have any problems or have any changes,
please send mail to <a href="mailto:chidhkra\@mode.lanl.k12.nm.us">
chidhkra\@mode.lanl.k12.nm.us</a>. We're looking forward to seeing you on $seeyou.
<p>
THANKS
}

# get the header and footer, report errors if necessary
-e "$datadir/$header" || &error("Cannot open header file",
	"Header file $datadir/$header does not exist.");
open(header, "$datadir/$header") 
	|| &error("Cannot open header file",
	"Cannot open header file $datafile/$header because $!.");
$head = "";
while(<header>)
	{$head .= $_; }
close(header);
-e "$datadir/$footer" || &error("Cannot open footer file",
	"Footer file $datadir/$footer does not exist.");
open(footer, "$datadir/$footer") 
	|| &error("Cannot open footer file",
	"Cannot open footer file $datafile/$footer because $!.");
$foot = "";
while(<footer>)
	{$foot .= $_; }
close(footer);

# get our data from the web form
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $form{$name} = $value;
}

# time to do our work
#error("Form Check!", "This is just a form check.");
if($form{'submit'} ne "submit") # ie, this is the first request
{
	print "Content-type: text/html\n\n\n";
	print $head;
	printform();
	print $foot;
}
else	# we have data
{
	formsanity(%form);	# check the form out for sanity
	#error("Checking...", "Checking...");
	
	$msg = checkdata(%form);
	if(!$msg)
	{
		savedata(%form);
		print "Content-type: text/html\n\n\n";
		print $head;
		printthanks(%form);
		print $foot;
	}
	else
	{
		print "Content-type: text/html\n\n\n";
		print $head;
		printform($msg, %form);
		print $foot;
	}
}
exit(0);
