#!/usr/bin/perl

# students can use for withdrawing or reactivating their applications 

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

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

# remove white space appropriately and log parameters
for $key ( param() ) {
  $input{$key} = param($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;
}
$id=$input{"id"};

printhead();
$first=readcell(First,$id);
$last=readcell(Last,$id);
print ("<h1><font size=5>Physics REU program: application for $first $last</font>
</h1><p>\n");

if ( ($id) and ($input{"email"} eq readcell("email",$id)) ) {
  if ( $input{"formdone"} eq "Withdraw" ) {
    writecell(1,"withdraw",$id);
    $timedate = localtime;
    writecell($timedate,"wdtime",$id);
    print qq(We appreciate your thoughtfulness in letting us know
      and hope you have a rewarding summer.  Should your plans change,
      you can reactivate your application from the status page.);
    sendlett($input{"email"},"withdraw");
  }
  elsif ($input{"formdone"} eq "Reactivate") {
    writecell(2,"withdraw",$id);
    $timedate = localtime;
    writecell($timedate,"wdtime",$id);
    print qq(Your application has been returned to the group under
      consideration.);
    sendlett($input{"email"},"reactivate");
  }
  print qq(<form action="/REU/appwithdraw" method="post" enctype="multipart/form-data">);
  printhid("id", "email");

  @insertfields=qw(email First Last withdraw recindex);
  for $key (@insertfields) {
    $input{$key} = readcell($key,$id);
  }

  if ( ($input{withdraw} eq "0") or ($input{withdraw} eq "2") ) { 
    printfield("withdraw");
  }
  elsif ($input{withdraw} eq "1") {
    printfield("reactivate");
  }
  else {
    print qq(<p> Contact reu\@london.ucdavis.edu for assistance.);
  }  
  print "</form><p>\n";
}
else {
  print qq(You must enter an id code to access this page.\n);
}

print qq(&nbsp;<p>&nbsp;<p>\n
  <form action="/REU/appstatus" method="post" enctype="multipart/form-data">\n);
printhid("id", "email");
print qq(<input type="submit" name="formdone" value="Back to status page">\n);

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

