#!/usr/bin/perl -w 
use strict;
use CGI qw(:standard);
use CGI::Carp 'fatalsToBrowser';
$CGI::POST_MAX=1024*1;
$CGI::DISABLE_UPLOADS = 1;

print "Content-type: text/html\n\n";

my(@required) = qw(user password);
my ($city,$county,$agency);

##############################################################
# parse form input into hash and check for valid input
##############################################################
read(STDIN, my($buffer), $ENV{'CONTENT_LENGTH'});
my(@pairs) = split(/&/, $buffer);
my(%response);
my($pair);
foreach $pair (@pairs) {
  my($name, $value) = split(/=/, $pair);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $response{$name} = $value;
}

##############################################################
# check for valid responses
##############################################################
for ( @required ) {
  if ( $response{$_} eq "" ) {
      print "<body bgcolor=\"#ffffff\">\n";
      print "<p>\n";
      print "<font size=\"+1\"><center><b>ERROR!</b></center></font><br>\n";
      print "<center><b>You need to go back and enter a valid $_ before submitting</b></center>\n";

      print "</body>\n";
      print "</html>\n";

      exit;

  }
}
##############################################################
# authenticate username and password
##############################################################
open (F, "./user.pl");
while (<F>){
   my ($user,$pword,$cty,$agy,$coty) = split(/\|/);
   if ($user eq $response{'user'} && $pword eq $response{'password'}){
      $city = $cty;
      $county = $coty;
      $agency = $agy;
      &amberInfo;
      exit;
   }
}

print "<body bgcolor=\"#ffffff\">\n";
print "<p>\n";
print "<font size=\"+1\"><center><b>ERROR!</b></center></font><br>\n";
print "<center><b>Authentication Failed!!!</b></center>\n";
print "</body>\n";
print "</html>\n";

exit;

sub amberInfo
{
print<<EOF;
<html><head></head><body bgcolor="#ffffff">
<form action="sendAmber.pl" method="post">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  <tr>
    <td width="33%">
    <img border="0" src="../amber/nws.gif" width="117" height="112" align="left"></td>
    <td width="33%">
    <p align="center"><font size="5"><b>Amber Alert Info</b></font></td>
    <td width="34%">
    <img border="0" src="../amber/logo3.gif" width="134" height="75" align="right"></td>
  </tr>
</table>
<div align="center">
  <center>
<table border="1" width="591" cellspacing="0" bordercolor="#2850b0" bgcolor="#cccccc" style="border-collapse: collapse;" cellpadding="0" height="408">
<tbody><tr>
  <td height="42" width="283">
    <em>Originator's Name</em><br>
    <input type="text" size="30" maxlength="256" name="name1">
  </td>
  <td height="42" width="305">
    <em>Agency<br></em>
    <input type="text" size="30" maxlength="256" name="agency1" 
EOF
print "value=\"$agency\">";
print<<EOF;
  </td>
</tr>
<tr>
  <td height="42" width="283">
    <em>City<br></em>
    <input type="text" size="30" maxlength="256" name="city1"
EOF
print "value=\"$city\">";
print<<EOF;
  </td>
  <td height="42" width="304">
    <i>County<b> </b></i><b><br></b><select name="county1" size="1">
EOF
foreach my $co ('Carroll_TN','Chester_TN','Crockett_TN','Decatur_TN','Dyer_TN','Fayette_TN','Gibson_TN','Hardeman_TN',
                'Hardin_TN','Haywood_TN','Henderson_TN','Henry_TN','Lake_TN','Lauderdale_TN','Madison_TN','McNairy_TN',
                'Obion_TN','Shelby_TN','Tipton_TN','Weakley_TN'){
   my ($co2,$st) = (substr($co,0,-3),substr($co,-2)); 
   if ($county eq $co){
      print "</option><option value=$co selected>$co2";
   } else {
      print "</option><option value=$co>$co2";
   }
}
print<<EOF;
    </option></select></td>
</tr>
  <tr>
  <td colspan="2" height="236" width="589">
    <p><i>Amber Information</i><textarea name="details" rows="13" cols="71"></textarea></td>
  </tr>
</tbody></table>
  </center>
</div>
<p></p>
<p></p>
<p></p><center>
       <input type="submit" value="Submit"> <input type="reset" value="Reset"> 
       <p>&nbsp;</p>
       <p>&nbsp;</p>
</center><p></p>
</form>
</body></html>
EOF

}