#!/usr/bin/perl

use CGI qw/:standard/;
use DBI;
require 'appcom';

$databasename = "reu";
$db = DBI->connect("DBI:mysql:reu","root","");
$tablename = "applicants";

for $key ( param() ) {
  $input{$key} = param($key);
  unless ($inputm{$key}) {
    $input{$key} =~ s/^\s+//;
    $input{$key} =~ s/\s+$//;
    $input{$key} =~ s/\n\s{0,}\n/\n\t/g;
    $input{$key} =~ s/[ \t\r\f]{2,}/ /g;
    $input{$key} =~ s/\n[ \t\r\f]/\n\n/g;
  }
}
$email = $input{email};
$id = $input{"id"};
$id ||= $input{"idverify"};
$warning = "";

# start html page
printhead();
print qq(<h1><font size=5>Physics REU at UC Davis: June 12-August 19, 2011 
  </font></h1><p>\n);

if ( $id and param(email) ) {
  @insertfields=qw(email First Last r1name r2name r3name r1done r2done r3done
    transdone withdraw recindex);
  for $key (@insertfields) {
    $input{$key} = readcell($key,$id);
  }
  @trans=split(/,/,readcell("transcript",$id));

  if ( not $input{email}) {
    print qq(Your id code does not match our database.  Use your browser's
back button to try again.);  
  }
  elsif ( lc($input{email}) ne lc($email) ) {
    print qq(<form action="/REU/appstatus" method="post" enctype="multipart/form-data">\n);
    printhid("id");
    print qq(Your e-mail does not match our database.  Please use the
      address you listed in your application.\n);
    print qq(<p>Correct or re-enter your e-mail address: );
    print qq(<input type=text name=email size=40 value="$email"><p>);
    printfield("status");
    print "</form><p>\n";
  }
  else {
    if ( $input{"formdone"} eq "Submit Transcript" ) {
      $size=@trans;
      writecell($size,"transdone",$id);
      $input{"transdone"} = $size;
    }
    elsif ($input{"formdone"} eq "Cancel reminder") {
      $i = param("i");
      $r="r$i";
      $input{$r} = "";
    }
    elsif ($input{"formdone"} eq "Send reminder") {
      $i = param("i");
      $rmail="r$i"."mail"; 
      $refmail=readcell("$rmail",$id);
      $samemail=1;
      if ($refmail ne $input{"$rmail"}) {
        $refmail=$input{"$rmail"};
        writecell($refmail,"$rmail",$id);
        $samemail=0;
      }
      $rsend="r$i"."send";
      $refid=readcell("r$i"."id",$id);
      if ( readcell($rsend,$id) eq "no") {
        sendlett("$refmail","requestref");
        $timedate = time();
        writecell($timedate,"$rsend",$id);
      }
      else {
        $timedate = time();
        if ( ($timedate - readcell($rsend,$id) < 86400) and $samemail ) {
          $warning="<font color=blue> No additional reminder sent; previous message was less than one day ago.</font><p>";
        }
        else { 
          sendlett("$refmail","remindref");
          writecell($timedate,"$rsend",$id);
        }
      }
    }

    print "$warning\n";
    print qq(<b><u>Application for $input{First} $input{Last}</u><p>\n);
    $imax=2;
    $imax=3 if $input{r3name};
    $complete=0;
    for $i (1 .. $imax) {
      $rdone="r$i"."done";
      $rname="r$i"."name";
      $present="no";
      if ($input{$rdone}) {$present="yes"; $complete+=1;}
      print qq(<form action="/REU/appremind" method="post" enctype="multipart/form-data">\n);
      printhid("id","email","r1","r2","r3");
      print qq(Letter from $input{$rname}: $present&nbsp;&nbsp;&nbsp;\n);
      if ($present eq "no") {
        $r="r$i";
        if ($input{"$r"}) {
	  unless ($warning) {
            print qq(<font color=blue> Reminder sent</font><br>\n);
          }
        }
        else {
          $remind="remind$i";
          printfield($remind);
        }
      }
      print qq(</form>);
    }
    if ($complete lt 2) {$complete = "Not Complete";}
    else {$complete = "Complete";}
    $present = "no";
    if ($input{transdone} or ( $input{"formdone"} eq $fieldtext{transub}{value})) {
      $present = "yes ($input{transdone})";
    }
    else {$complete = "Not Complete";}
    print qq(<form action="/REU/apptran" method="post" enctype="multipart/form-data">\n);
    printhid("id","email","r1","r2","r3");
    print qq(Transcript: $present&nbsp;&nbsp;&nbsp;\n);
    if ($#trans gt $input{"transdone"}) {
      $filename=$trans[-1];
      printfield("viewtran","deltran");
      print "Click here to finalize transcript: ";
      printfield("transub");  
    }
    else {
      if ($present =~ /yes/) {
        print qq(<input type=submit name="formdone" value="Upload another transcript?">\n); 
      }
      else {print qq(<input type=submit name="formdone" value="Upload transcript?">\n); 
      }
    }
    print qq(</form>\n);
    
    print qq(<p>Overall application: <font color=red> $complete</font><p></b>\n);
    if ( ($input{withdraw} eq "0") or ($input{withdraw} eq "2") ) {
      print qq(<p>If you make other summer plans and are no longer interested in
the UC Davis REU Program, please let us know <a href="/REU/appwithdraw?id=$id&email=$email">here</a>.);
    }
    elsif ($input{withdraw} eq "1") {
      print qq(<p>Your application is not currently under consideration.  You can reactivate it <a href="/REU/appwithdraw?id=$id&email=$email">here</a>.);
    }
  }
}
else {
  print qq(<form action="/REU/appstatus" method="post" enctype="multipart/form-data">\n);
  print qq(Enter ID code: <input type="text" name="id" size="14" value="$id">);
  printfield("id");
  print qq(Enter e-mail address: <input type=text name="email" size=40><p>);
  printfield("status");
  print qq(</form>\n);
  print qq(<p>Use "Check status" for uploading transcripts or checking on letters
of recommendation.);
}
unless ( $input{"transdone"} or ($input{"formdone"} eq $fieldtext{transub}{value}) or ($id and not $input{email}) or (lc($input{email}) ne lc($email)) ) {

print <<END;
<p>
We do not accept transcripts by regular mail or e-mail. 
Remember that the transcript need not be an official copy.
Usually students can access an electronic version, then
save it or print it to a file.  If that doesn't work and
you can't figure out any other way of obtaining a copy
of your grades, order a transcript sent to yourself, then
scan it and upload it here.
</p>
END
}

print qq(<p>&nbsp;<p>For help contact reu\@london.ucdavis.edu);

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