#!/usr/local/bin/perl

require "/export/home/http/cgi-bin/cgi-lib.pl";

$setfile="setcs246";

MAIN:
{
  &ReadParse;
  unless ($in{set}) {
    print "Content-type: text/html\n\n";
    &ReadData($in{course});
    &PrintTop;
    &PrintTable;
    &PrintForm;
    &PrintBottom;
  } else {
    &ReadData($in{course});
    &PrintTop;
    $i=0;
    foreach $lab (@ColLabels) {
      if ($ColLabels[$i++] eq $in{'date'}) {
        $date=$i-1;
      }
    }
    $i=0;
    foreach $lab (@RowLabels) {
      if ($RowLabels[$i++] eq $in{'time'}) {
        $time=$i;
      }
    }
    $username=$in{'username'};
    $set=$in{'set'};
    &Confirm;
    &PrintTable;
    &PrintForm;
    &PrintBottom;
  }
}

sub Confirm
{
  if ($settings{$username}) {
    @aname=split(/ /,$settings{$username});
    if ($set == 1) {
      unless ($settings{$time.",".$date}) {
        $settings{$time.",".$date}=$username;
        if(!open(ADD,">>".$setfile)) {
          &FileError;
        } else {
          print ADD "$time,$date=$username\n";
          close(ADD);
        }
        print "<h3>OK $aname[0]. <br>Your appointment was accepted. \n";
        print "<br>See you on $ColLabels[$date] at $RowLabels[$tiome-1].</h3><br>\n";
      } else {
        print "<h3>Sorry $aname[0]. <br>That time has been reserved.\n";
        print "<br>Please select a different time.</h3><br>\n";
      }
    } else {
      if ($settings{$time.",".$date} eq $username) {
        $settings{$time.",".$date}="";
        if(!open(ADD,">>".$setfile)) {
          &FileError;
        } else {
          print ADD "$time,$date=\n";
          close(ADD);
        }
        print "<h3>OK $aname[0]. <br>Your appointment was canceled. </h3><br>\n";
      } else {
        print "<h3>Sorry $aname[0]. <br>The appointment on $ColLabels[$date] at $RowLabels[$tiome-1] is not yours. <br>You can cancel only your appointments.</h3><br><br>\n";
      }
    }
  } else {
  print <<"EOT4";
<h3>Sorry $username!<br>
Only students from <i>$settings{'ClassName'}</i> can set <br>appointments for $settings{'Class#'} office hours.</h3><br>
EOT4
  }
}

sub PrintTop
{
  print <<"EOT1";

<html>
<head><title>$title</title></head>
<body bgcolor=#ffffff>
<h1>$title</h1>
EOT1
}

sub PrintBottom
{
  print "<p>Back to <a href=\"$settings{URL}\">$settings{ClassName}</a>.<br>\n"; 
  print "</body></html>\n";
}

sub PrintTable
{
  print "<TABLE ALIGN=ABSCENTER BORDER=1 CELLSPACING=0 CELLPADDING=1 >\n";
  for ($i=0;$i<=$Nrows;$i++) {
    print "<tr>\n";
    for ($j=0;$j<=$Ncols;$j++) {
      $location=$i.",".$j;
      if ($i==0) {
        $ttext=$ColLabels[$j];
	$cstr="bgcolor=gray "
      } elsif ($j==0) {
        $ttext=$RowLabels[$i-1];
        $cstr="bgcolor=gray "
      } else {
        $ttext=$settings{$location};
        if ($settings{$ttext} || !$ttext) {
          if ($ttext) {
	    if($ttext eq " --- ") {
              $cstr="bgcolor=yellow ";
            } else {
              $cstr="bgcolor=red ";
              ($t1,$t2)=split(/ /,$settings{$ttext});
              $ttext=$t1." ".substr($t2,0,1);
            }
          } else {
            $cstr="bgcolor=green ";
          }
        } else {
          $cstr="bgcolor=yellow ";
        }
      }
      print "<td align=center valign=center $cstr";
      print "width=\"90\">$ttext&nbsp;</td>\n";
    }
    print "</tr>\n";
  }
  print "</table>\n";
}

sub PrintForm
{
  print <<"EOT3";
<form method=get action="http://mainline.brynmawr.edu/cgi-bin/bbutoi/appointments.perl">
Enter your username: <input type="text" name="username" size=8><br>
Enter your option: 
 <input type="radio" name="set" value=1>Set an Appointment
   <input type="radio" name="set" value=2>Cancel an Appointment<br>
EOT3
  print "Select Date: <select name=\"date\">\n";  
  for ($i=1;$i<=$Ncols;$i++) {
    print "<option>$ColLabels[$i]\n";
  }
  print "</select>\n";
  print "Select Time: <select name=\"time\">\n";
  for ($i=1;$i<=$Nrows;$i++) {
    print "<option>$RowLabels[$i-1]\n";
  }
  print "</select><br>\n";
  print <<"EOT4";
<input type=submit value="  Send Request  ">   
<input type=reset value="  Clear Request  ">
EOT4
}

sub ReadData
{
  if($in{course}) {
    $setfile=$in{course};
  }
  if(!open(INPUT,$setfile)) {
    print <<"EOT2";

<html>
<head><title>ERROR:Appointments Log</title></head>
<body bgcolor=#ffffff>
<center><h1>ERROR: Appointments Log</h1></center>
<h3>Can not open settings file $setfile</h3>
Please check the settings and try again.
</body>
<html>
EOT2

    exit; 
  } else {
    while (<INPUT>) {
      chomp($_);
      split(/=/);
      $settings{$_[0]}=$_[1];
    }
    close(INPUT);
    $Ncols=$settings{'Ncols'};
    $Nrows=$settings{'Nrows'};
    $title=$settings{'title'};
    @ColLabels=split(/,/,$settings{'ColLabels'});
    @RowLabels=split(/,/,$settings{'RowLabels'});
  }
}