#!/usr/local/bin/perl 
my $INCLUDE_HEADER=($ENV{'DOCUMENT_ROOT'}||"/home/httpd/html");
require "/home/sites/site3/lokad/header1.inc";
use CGI;
$q=new  CGI;
print "International Axis and Allies Players Association Database</i></H1>
</center>";

$qParent = $q->param('code');
$qnr = $q->param('nr');
$qPlayernr=$q->param('nr');
$qchallanger=$q->param('selchal');
$alias=$ENV{'REMOTE_USER'}; 

if ($qchallanger eq "") { &ViewChallengers; }
else {&Accept;}


sub Gametype {
   ($pt)=@_;
   $pt=~ s/^#/PBEM with GM/g;
   $pt=~ s/^C/CD rom -live/g;
   $pt=~ s/^D/PBEM without GM/g;
   $pt=~ s/^L/Live AOL HQ/g;
   $pt=~ s/^F/Face to Face/g;
   $pt=~ s/^I/Internet/g;
   return(($pt)); 
}
sub Gtype {
   ($pt)=@_;
   $pt=~ s/^#/Normal/g;
   $pt=~ s/^T/Tournament/g;
   return($pt);
}

sub Tournaments {
$queryt="select * from tournies";
print "<TD> <SELECT name=\"Tour\">";
die "prepare\n" unless ($cursort = $dbh->prepare($queryt)); $cursort->execute;
while (@rowt=$cursort->fetchrow) {
  print "<option value = \"@rowt[0]\">@rowt[0]";
}
print "</select>";
}
sub ViewChallengers {

    $checkch="select chgames.chpl1, chgames.chplaytype, chgames.chdate,players.rank, players.poolcomment, chgames.chgametype from chgames, players where chpl2 = ? and players.alias = chgames.chpl1 ";                     
    $cur2=$dbh->prepare($checkch) or die "Could not prepare database\n";   
    $cur2->execute($alias);   
    print "<center><table><td><td>Opponent<td>Playtype<td>Date<td>Rank<td>Poolcomment<td>Gametype<tr>";
    while  (($oalias, $oplaytype, $odate, $rank, $comment,$gamtype)=$cur2->fetchrow)  {
      $plt=&Gametype($oplaytype);
      $gat=&Gtype($gamtype);
      print "<form METHOD=POST ACTION=/cgi-local/axis/Acceptch.cgi><td><INPUT TYPE=submit VALUE='Process challenge'><td><b><input type=hidden value=$oalias name=selchal>$oalias</b><td>$plt<td>, $odate,<td> $rank,<td> $comment <td>$gat<tr></form>";
      }   
   print "</table>"; 

}

sub Accept {
$query="select playercode, playernumber from players where alias = ?";
die "prepare\n" unless ($pcursor = $dbh->prepare($query));
$pcursor->execute($alias);
if (($qParent, $qPlayernr)=$pcursor->fetchrow) {
   $checkch="select chpl1, chplaytype, chdate,chpl2,chgametype from chgames where chpl1 = ? and chpl2 = ?";
   die "prepare\n" unless ($cursor = $dbh->prepare($checkch));
   $cursor->execute($qchallanger,$alias);  
   if (@row = $cursor->fetchrow ){
      &Acceptform; }
   else
     {
       print "<h4>You do not have any challanges</h4><br>";
     }
}
else
    { &Error_message("<center><h4>Your playerinfo do not match Retry? </h4><br><br>"); }

}
# End of Accept subrutine

sub Acceptform {
print("<FORM METHOD=POST ACTION=/cgi-local/axis/processch.cgi><table><td>");
 $maxcount=3;   
    $pt=""; 
       	if (@row[1] eq "#"){$pt="PBEM with GM";}
  	if (@row[1] eq "D"){$pt="PBEM without GM";}
  	if (@row[1] eq "C"){$pt="CD rom -live";}
	if (@row[1] eq "L"){$pt="Live AOL HQ";}
	if (@row[1] eq "F"){$pt="Face to face";}
	if (@row[1] eq "I"){$pt="Internet";}

         print "<tr>\n";
         printf("<td><input type=hidden value=%s name=box%d> Accept challenge from<td> <b> %s</b><td> playtype <b>%s</b> <TR>",$qchallanger,$maxcount,$qchallanger,$pt);
	$maxcount-= 1;
if ($row[4] eq 'T'){
print "<tr><td>Tournament"; 
&Tournaments;
print "<tr>\n";}
print "<input type=hidden name=Gtype value=$row[4]>\n";
print("<input type=hidden name=Player1 value=$alias>");
print("<input name='Playernr' type=hidden value=$qPlayernr>");
print("<input name='Code' type=hidden value=$qParent>");
print("<TD><H4>The IPC of your bid: </H4> <TD> <input name='bid' value=''> <TR> </TABLE>");
print("The description of your bid<br> <textarea name=comments value=\"$comments\" rows=5 cols=51 wrap=virtual></textarea>");
print "<table><td>";
print("<INPUT TYPE=submit VALUE='Accept Challenge'> </FORM>");
print("<FORM METHOD=POST ACTION=/cgi-local/axis/declinech.cgi>");
print("<td><INPUT TYPE=SUBMIT value='Decline Challenge'>");
print("<input type=hidden name=\"Player1\" value =\"$row[0]\">");
print("<input type=hidden name=\"Player2\" value =\"$row[3]\"></FORM><tr></table>");

}
sub Error_message {
     print "@_";  
     print("<FORM METHOD=POST ACTION=/cgi-local/axis/Acceptch.cgi><TABLE>");
     print("<TD>Player number <TD><input name=nr value=$qPlayernr><TR>"); 
     print("<TD>Player code <TD><input name=code type=password value=$qParent><TR>"); 
     print("<TD> <INPUT TYPE=submit VALUE='Check challenges'></table></FORM>"); 
}

print "<BR><BR> <A HREF=/cgi-local/axis/players.cgi><IMG src=/images/back.gif></A>"; 

