#!/usr/bin/perl
#
#
print "Content-type:text/html\n\n";
#
require ("cgi-lib.pl");
&ReadParse;   #get the form arguments.
#
# respond with an html file to the user.
#
$ok = "";

$rn=$in{'rname'};
$rn =~ s/^\s+//;
$rn =~ s/\s+$//;
if (length($rn)<2) { $ok="Invalid ranking name";goto cont1;}

$ead=$in{'eaddress'};
($eid,$eho)=split("@",$ead);
if (length($eid)<1) { $ok="Invalid email address";goto cont1;}
if (length($eho)<1) { $ok="Invalid email address";goto cont1;}

$order=$in{'order'};
# other checks here

cont1:
#
if (length($ok) == 0) {
  ($sec,$min,$hou,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  $year=$year+1900;
  $mon++;
  @names = (
"Brazil",
"Croatia",
"Mexico",
"Cameroon",
"Spain",
"Netherlands",
"Chile",
"Australia",
"Colombia",
"Greece",
"IvoryCoast",
"Japan",
"Uruguay",
"CostaRica",
"England",
"Italy",
"Switzerland",
"France",
"Ecuador",
"Honduras",
"Argentina",
"BosniaHerz",
"Iran",
"Nigeria",
"Germany",
"Portugal",
"Ghana",
"USA",
"Belgium",
"Algeria",
"Russia",
"SouthKorea"
);
$html=
"
<html>
<h4>
Rankings accepted for $ead at $year-$mon-$mday $hou:$min:$sec <br> Name:$rn<p>
You may submit new rankings at any time.<BR>
The last submission received before June 12 will be the one that counts.<BR><BR>
Rankings submitted, correct if necessary by pressing BACK and resubmitting.<BR>
Your ranking submission:<br>
";
$emsg=
"
$year-$mon-$mday $hou:$min:$sec
Ranking submission confirmation for $ead
Cool name:$rn

";

  $nn=0;
@xo = split(/,/, $order);
$html .= '<table border="1"><tr>';
foreach (@xo){
    $nn++;
    $cmap = '<br><img src="../WorldCup/png/'.(0+$_).'.png" width=48px;>' ;
    $html .= '<td style="text-align:center;">' . $nn . " " . $names[-1+$_] . $cmap . '</td>';
    $emsg .=  $nn . " " . $names[-1+$_] . "\n" ;
    if( 0 == ($nn % 8) ){ $html .= '</tr>'; }
}
$html .= '</table>';


# place a record in the data file
  open (DATAFILE, ">> /home/lrs/soccerpoolers.d");
  flock (DATAFILE, 2);
($sec,$min,$hou,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
  $year=$year+1900;
  $mon++;

printf DATAFILE "%04d%02d%02d%02d%02d%02d %-40s %-40s %s\n", $year, $mon, $mday, $hou, $min, $sec, $rn, $ead, $order ;

  flock (DATAFILE, 8);
  close DATAFILE;

$html .= '</html>';
print $html;

open (MAIL, "| /usr/sbin/sendmail -oi -n -t" );
print MAIL <<MAIL_MESSAGE;
To:$ead
Bcc:bszk\@uoguelph.ca
Subject: LRS Soccer pool
From:$in{'eaddress'}
$emsg
MAIL_MESSAGE
close MAIL;

}

else {

print <<WEB_PAGE;
<html>
<h3>
Sorry, there is a problem with your submission:<br><br>
<font color=red>
<i>
$ok
</i>
</font>
<br><br>
Go back and try again.
</h3>
</html>
WEB_PAGE

}

exit;    # end of user script. 
