#!/usr/bin/perl
#
# Original script written by Joel Hahn, 2001.

use CGI::Carp qw(fatalsToBrowser);

# Get the input
$buffer=$ENV{'QUERY_STRING'};

($file, $prod) = split(/&/, $buffer);

# Check for which product line was selected
if ($file eq "firsted1") {
 $prodline="First Edition";
}
elsif ($file eq "firsted2") {
 $prodline="First Edition";
# $part=", part 2";
}
elsif ($file eq "firsted3") {
 $prodline="First Edition";
# $part=", part 3";
}
elsif ($file eq "seced1") {
 $prodline="Second Edition";
# $part=", part 1";
}
elsif ($file eq "seced2") {
 $prodline="Second Edition";
# $part=", part 2";
}
elsif ($file eq "thirded") { $prodline="Third Edition"; }
elsif ($file eq "alq") {  $prodline="Al-Qadim";  }
elsif ($file eq "birth") {  $prodline="Birthright";  }
elsif ($file eq "dsun") {  $prodline="Dark Sun";  }
elsif ($file eq "dlance") {  $prodline="Dragonlance";  }
elsif ($file eq "eberron") {  $prodline="Eberron";  }
elsif ($file eq "forg1") {
 $prodline="Forgotten Realms";
# $part=", part 1";
}
elsif ($file eq "forg2") {
 $prodline="Forgotten Realms";
# $part=", part 2";
}
elsif ($file eq "forg3") {
 $prodline="Forgotten Realms";
# $part=", part 3";
}
elsif ($file eq "kara") {  $prodline="Kara-Tur";  }
elsif ($file eq "grey") {  $prodline="Greyhawk";  }
elsif ($file eq "lankh") {  $prodline="Lankhmar";  }
elsif ($file eq "myst") {  $prodline="Mystara";  }
elsif ($file eq "plane") {  $prodline="Planescape";  }
elsif ($file eq "rloft") {  $prodline="Ravenloft";  }
elsif ($file eq "sjam") {  $prodline="Spelljammer";  }
elsif ($file eq "dnd1") {
 $prodline="Dungeons &amp; Dragons";
# $part=", part 1";
}
elsif ($file eq "dnd2") {
 $prodline="Dungeons &amp; Dragons";
# $part=", part 2";
}
elsif ($file eq "dnd3") {
 $prodline="Dungeons &amp; Dragons";
# $part=", part 3";
}
elsif ($file eq "dnd4") {
 $prodline="Dungeons &amp; Dragons";
# $part=", part 4";
}
else {
 $file="firsted1";
 $prodline="First edition";
# $part=", part 1";
}

# Open the relevant datafile, dump all of the data into the @LINES array
open(FILEDAT,"$file.dat") || die "Can't Open $file.dat: $!\n";
@LINES=<FILEDAT>;
close(FILEDAT);
$SIZE=@LINES;

# Start printing the form page
print "Content-Type: text/html\n\n";
print "<html><head><title>Rate $prodline Products</title></head>\n";
print "<body text=#000000 bgcolor=#FFFFFF><h1>Rate $prodline Products</h1>\n";
print "<p>\n";
print "<i>Note: 0 is awful, 10 is excellent.  If you do not wish to vote\n";
print "for a product, either leave the buttons blank or vote\n";
print "'N/A'</i>\n";
#print "<hr>\n";
print "<form method=post action=\"http://www.hahnlibrary.net/rpgs/rate/dorate.cgi\">\n";
#print "<form method=post action=\"http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query\">\n";
print "<input type=hidden name=filename value=$file>\n";

# Get the product name, set up the radio buttons, using the product code for
# an identifier
#print "<p>|$prod|<p>\n";
for ($i=0;$i<=$SIZE-1;$i++) {
   $_=$LINES[$i];
   ($prodcode, $prodname, $votes, $rawtotal, $ratenums)= split(/\|/, $_);
#   print "|$prodcode|<br>\n";

   if ($prodcode eq $prod) {
     if ($rawtotal==0) {
     $avg=0;
     }
     else {
       $rawavg=$rawtotal/$votes;
       $exp=2;
       $avg= sprintf("\%.${exp}f", $rawavg);
     }
     $color ="<font color=0000FF>";
     if ($avg > 1) { $color = "<font color=0000CC>"; }
     if ($avg > 2) { $color = "<font color=330099>"; }
     if ($avg > 3) { $color = "<font color=660099>"; }
     if ($avg > 4) { $color = "<font color=990099>"; }
     if ($avg > 5) { $color = "<font color=BB00BB>"; }
     if ($avg > 6) { $color = "<font color=990099>"; }
     if ($avg > 7) { $color = "<font color=990066>"; }
     if ($avg > 8) { $color = "<font color=990033>"; }
     if ($avg > 9) { $color = "<font color=CC0000>"; }
     if ($avg >=10) { $color = "<font color=FF0000>"; }

     print "<hr><font size=+2>$prodname</font><br>\n";
     print "<p><b>Rating / Votes</b><br>\n";
     print "<b>$color$avg</font> / $votes</b><br><p>\n";

     $_=$ratenums;
     ($r0, $r1, $r2, $r3, $r4, $r5, $r6, $r7, $r8, $r9, $r10)= split(/\,/, $_, 11);
     print "<font color=0000FF>0:$r0</font> &nbsp\;\n";
     print "<font color=0000CC>1:$r1</font> &nbsp\;\n";
     print "<font color=330099>2:$r2</font> &nbsp\;\n";
     print "<font color=660099>3:$r3</font> &nbsp\;\n";
     print "<font color=990099>4:$r4</font> &nbsp\;\n";
     print "<font color=BB00BB>5:$r5</font> &nbsp\;\n";
     print "<font color=990099>6:$r6</font> &nbsp\;\n";
     print "<font color=990066>7:$r7</font> &nbsp\;\n";
     print "<font color=990033>8:$r8</font> &nbsp\;\n";
     print "<font color=CC0000>9:$r9</font> &nbsp\;\n";
     print "<font color=FF0000>10:$r10</font> \n";

     print "<p><hr><p>\n";

     print "<input type=radio name=$prodcode value=11>N/A\n";
     print "<input type=radio name=$prodcode value=0>0\n";
     print "<input type=radio name=$prodcode value=1>1\n";
     print "<input type=radio name=$prodcode value=2>2\n";
     print "<input type=radio name=$prodcode value=3>3\n";
     print "<input type=radio name=$prodcode value=4>4\n";
     print "<input type=radio name=$prodcode value=5>5\n";
     print "<input type=radio name=$prodcode value=6>6\n";
     print "<input type=radio name=$prodcode value=7>7\n";
     print "<input type=radio name=$prodcode value=8>8\n";
     print "<input type=radio name=$prodcode value=9>9\n";
     print "<input type=radio name=$prodcode value=10>10\n";
   }

#   for ($j=0;$j<=10;$j++) {
#      print "<input type=radio name=$prodcode value=$j>$j\n";
#   }
   print "<p>\n";
}

# Finish printing the form page
print "<p><input type=submit value=\"Send\">\n";
print "<input type=reset value=\"Clear\">\n";
print "</form>\n";
print "<p><hr><p>\n";
print "Return to the <a href=\"http://www.hahnlibrary.net/rpgs/rate/disonerate.cgi?$file\">$prodline selection page</a>.<br>\n";
print "Return to the <a href=\"http://www.hahnlibrary.net/rpgs/rate/display2.html\">product line selection page</a>.<br>\n";
print "<a href=\"www.hahnlibrary.net/rpgs/rate/response.html\">E-mail the survey maintainer</a>.\n";
print "</body></html>";
