#! /usr/bin/perl -w
#
#  cgi-bin/seniors.pl:    process a badminton form and email 
#

use CGI qw(:standard);

$version = "1-jan-2001";
$q = CGI::new();

sub bail (msg) {
  print i("  Please go BACK using your browser, fix your form, and click on Register again");
  exit ;
}

$fname     = $q->param("fname");
$lname     = $q->param("lname");
$usab      = $q->param("usab");
$address   = $q->param("address");
$city      = $q->param("city");
$state     = $q->param("state");
$zip       = $q->param("zip");
$dphone    = $q->param("dphone");
$ephone    = $q->param("ephone");
$birthday  = $q->param("birthday");
$sex       = $q->param("sex");
$email     = $q->param("email");

# 35+

$ms[0] = $q->param("ms1");
$ws[0] = $q->param("ws1");
$md[0] = $q->param("md1");
$wd[0] = $q->param("wd1");
$xd[0] = $q->param("xd1");
$dp[0] = $q->param("dp1");
$xp[0] = $q->param("xp1");


# 40+ 

$ms[1] = $q->param("ms2");
$ws[1] = $q->param("ws2");
$md[1] = $q->param("md2");
$wd[1] = $q->param("wd2");
$xd[1] = $q->param("xd2");
$dp[1] = $q->param("dp2");
$xp[1] = $q->param("xp2");

$ms[2] = $q->param("ms3");
$ws[2] = $q->param("ws3");
$md[2] = $q->param("md3");
$wd[2] = $q->param("wd3");
$xd[2] = $q->param("xd3");
$dp[2] = $q->param("dp3");
$xp[2] = $q->param("xp3");

# 50+ 

$ms[3] = $q->param("ms4");
$ws[3] = $q->param("ws4");
$md[3] = $q->param("md4");
$wd[3] = $q->param("wd4");
$xd[3] = $q->param("xd4");
$dp[3] = $q->param("dp4");
$xp[3] = $q->param("xp4");

$ms[4] = $q->param("ms5");
$ws[4] = $q->param("ws5");
$md[4] = $q->param("md5");
$wd[4] = $q->param("wd5");
$xd[4] = $q->param("xd5");
$dp[4] = $q->param("dp5");
$xp[4] = $q->param("xp5");

# 60+ 

$ms[5] = $q->param("ms6");
$ws[5] = $q->param("ws6");
$md[5] = $q->param("md6");
$wd[5] = $q->param("wd6");
$xd[5] = $q->param("xd6");
$dp[5] = $q->param("dp6");
$xp[5] = $q->param("xp6");


$ms[6] = $q->param("ms7");
$ws[6] = $q->param("ws7");
$md[6] = $q->param("md7");
$wd[6] = $q->param("wd7");
$xd[6] = $q->param("xd7");
$dp[6] = $q->param("dp7");
$xp[6] = $q->param("xp7");

# 70+ 

$ms[7] = $q->param("ms8");
$ws[7] = $q->param("ws8");
$md[7] = $q->param("md8");
$wd[7] = $q->param("wd8");
$xd[7] = $q->param("xd8");
$dp[7] = $q->param("dp8");
$xp[7] = $q->param("xp8");

$ms[8] = $q->param("ms9");
$ws[8] = $q->param("ws9");
$md[8] = $q->param("md9");
$wd[8] = $q->param("wd9");
$xd[8] = $q->param("xd9");
$dp[8] = $q->param("dp9");
$xp[8] = $q->param("xp9");

# 80+ 

$ms[9] = $q->param("ms10");
$ws[9] = $q->param("ws10");
$md[9] = $q->param("md10");
$wd[9] = $q->param("wd10");
$xd[9] = $q->param("xd10");
$dp[9] = $q->param("dp10");
$xp[9] = $q->param("xp10");

$numevents   = $q->param("numevents");
$sum1        = $q->param("sum1");
$numdinner   = $q->param("numdinner");
$sum2        = $q->param("sum2");
$sum         = $q->param("sum");
$comments    = $q->param("comments");
$participant = $q->param("participant");
$date        = $q->param("date");


if ($numdinner eq "") {
  $numdinner = 0;
} 

####    write report

#open(MAIL,"|mailx -s SENIORS teuben\@astro.umd.edu");

$n = $#ms;

open(MAIL, ">/tmp/seniors.tmp.$$");
print MAIL "fname=",$fname,"\n";
print MAIL "lname=",$lname,"\n";
print MAIL "usab=",$usab,"\n";
print MAIL "address=",$address,"\n";
print MAIL "city=",$city,"\n";
print MAIL "state=",$state,"\n";
print MAIL "zip=",$zip,"\n";
print MAIL "dphone=",$dphone,"\n";
print MAIL "ephone=",$ephone,"\n";
print MAIL "birthday=",$birthday,"\n";
print MAIL "sex=",$sex,"\n";
print MAIL "email=",$email,"\n";
#
$count = 0;
for ($i=0; $i<=$n; $i++) {
  print MAIL "ms",$i+1,"=",$ms[$i],"\n";
  print MAIL "ws",$i+1,"=",$ws[$i],"\n";
  print MAIL "md",$i+1,"=",$md[$i],"\n";
  print MAIL "wd",$i+1,"=",$wd[$i],"\n";
  print MAIL "xd",$i+1,"=",$xd[$i],"\n";
  print MAIL "dp",$i+1,"=",$dp[$i],"\n";
  print MAIL "xp",$i+1,"=",$xp[$i],"\n";
  if ($ms[$i]) { $count++; }
  if ($ws[$i]) { $count++; }
  if ($md[$i]) { $count++; }
  if ($wd[$i]) { $count++; }
  if ($xd[$i]) { $count++; }
}
#
print MAIL "numevents=",$numevents,"\n";
print MAIL "sum1=",$sum1,"\n";
print MAIL "numdinner=",$numdinner,"\n";
print MAIL "sum2=",$sum2,"\n";
print MAIL "sum=",$sum,"\n";
print MAIL "participant=",$participant,"\n";
print MAIL "date=",$date,"\n";
#  comments= must be the last one, some scripts use it as a trigger!!
print MAIL "comments=",$comments;
close MAIL;
chmod 0666,"/tmp/seniors.tmp.$$";


####    report back to the form filler


@cat=("35+","40+","45+","50+","55+","60+","65+","70+","75+","80+");

print $q->header();
print hr, start_html;
print p("$fname, here is your 2002 Senior Nationals Registration Result:");
print p("Found $count events for you, and $numdinner dinner tickets");

if (!$fname) {
  print b("You have not supplied your first name");
  &bail("no fname");
}

if (!$lname) {
  print b("You have not supplied your last name");
  &bail("no lname");
}

if (!$state) {
  print b("You have not supplied a state");
  &bail("no state");
}

if (!$sex) {
  print b("You have not supplied your gender");
  print i("  Go BACK, change and submit again");
  exit;
} 

if (!$email) {
  print b("You have not supplied your email");
  print i("  Go BACK, change and submit again");
  exit;
}

if (!$usab) {
  print b("You have not supplied a USAB number");
  print p("use -1 if you plan to buy a new or re-register on site or don't know");
  &bail("bad usab");
}

$count = 0;
for ($i=0; $i<=$n; $i++) {
  if ($ms[$i]) { 
    $count++;
    print p("   $count: MS ($cat[$i])\n");
    if ($sex ne "m") { &bail("wrong sex"); }
  }
  if ($ws[$i]) { 
    $count++;
    print p("   $count: WS ($cat[$i])\n");
    if ($sex ne "f") { &bail("wrong sex"); }
  }

  if ($md[$i]) { 
    $count++;
    print p("   $count: MD ($cat[$i]) w/ $dp[$i]\n");
    if ($sex ne "m") { &bail("wrong sex"); }
  }

  if ($wd[$i]) { 
    $count++;
    print p("   $count: WD ($cat[$i]) w/ $dp[$i]\n");
    if ($sex ne "f") { &bail("wrong sex"); }
  }

  if ($xd[$i]) { 
    $count++;
    print p("   $count: XD ($cat[$i]) w/ $xp[$i]\n");
  }
}

$mysum = ($count + $numdinner)*25;
print p("Your total payment would be \$$mysum");
print b("Now sending registration off....");
print p("Don't forget to send in payments or use <A HREF=http://www.astro.umd.edu/~teuben/paypal.html>paypal.com</A>");
print p("Deadlines:  entrie forms: Feb 24, 2002");
print hr, "-Peter Teuben.";
print end_html;


#  email me  when success

system("mailx -s \"SENIORS2002 $fname $lname\" teuben\@astro.umd.edu  < /tmp/seniors.tmp.$$");
unlink "/tmp/seniors.tmp.$$";


