#!/usr/bin/perl

#
#     assign filename - read as input later
#

read(STDIN,$test,$ENV{'CONTENT_LENGTH'});  #put filename into string
($formfield,$formvalue) = split (/=/,$test);
#
$filename = $formvalue;      #  this could all be compacted, but why, really?
#
$input = "invalid";     #  if no sort order, alphabetical, or bad input
#
if ($filename eq "140_rgb.txt") {$text = "rgb"; $input = "ok";}
if ($filename eq "140_rbg.txt") {$text = "rbg"; $input = "ok";}
if ($filename eq "140_grb.txt") {$text = "grb"; $input = "ok";}
if ($filename eq "140_gbr.txt") {$text = "gbr"; $input = "ok";}
if ($filename eq "140_brg.txt") {$text = "brg"; $input = "ok";}
if ($filename eq "140_bgr.txt") {$text = "bgr"; $input = "ok";}
if ($input eq "invalid") {
      $text = "alphabetical"; 
      $filename = "140_alphabetical.txt";
}
#
#     start printing HTML back to user
#
print "Content-type: text/html\n\n";
print "<html><head><title>HTML - 140 Standard Colors</title></head>";
print "<body><center><h1>HTML - 140 Standard Colors</h1>";
print "This table is currently sorted in <strong> $text </strong> order<br>\n";
#
#     setup form for re-sort request
#
print "<form action=\"140_read.pl\" method=\"post\">\n";
print "Re-sort? &nbsp; &nbsp; ";
print "<input type=\"radio\" name=\"sort\" value=\"140_rgb.txt\"> rgb &nbsp;\n";
print "<input type=\"radio\" name=\"sort\" value=\"140_rbg.txt\"> rbg &nbsp;\n";
print "<input type=\"radio\" name=\"sort\" value=\"140_gbr.txt\"> gbr &nbsp;\n";
print "<input type=\"radio\" name=\"sort\" value=\"140_grb.txt\"> grb &nbsp;\n";
print "<input type=\"radio\" name=\"sort\" value=\"140_brg.txt\"> brg &nbsp;\n";
print "<input type=\"radio\" name=\"sort\" value=\"140_bgr.txt\"> bgr &nbsp;\n";
print "<input type=\"radio\" name=\"sort\" value=\"140_alphabetical.txt\" checked> alphabetical \n";
print " &nbsp; &nbsp; <input type=\"submit\" value=\" Sort \">";
print "</form> \n";
#
print "<p>\n";
#
#     setup color table and header row - 1 blank column betw dec and hex 
#
print "<table cellpadding=2 border=1 cellspacing=1 width=95\%> \n";

print "<tr><th>\#</th><th>name</th><th> r </th><th> g </th><th> b </th><th>&nbsp;</th><th> r </th><th> g </th><th> b </th><th>&nbsp;</th><th width=80\%> and it looks like this </th></tr> \n";



#
#     lines of file are made up of nine items:
#       number(1-140) name r-dec g-dec b-dec r-hex g-hex b-hex rgb-hex
#       eg:    1  aliceblue  240  248  255  F0  F8  FF  F0F8FF
# 
open (INFO,$filename);
while (<INFO>) {
	chop ($_);
     	@temp = split(/\s/,$_) ;	
     	print "<tr align=\"center\">";
        for ( $i = 0; $i <= 7; $i++ ) {
              print "<td> $temp[$i] </td>";
              if ( $i == 4 ) {
                  print "<td>&nbsp;</td>";     # blank column
              }
        }
        print "<td>&nbsp;</td>";               # blank column 2 - testing? 
        print "<td bgcolor=\"#$temp[8]\">&nbsp;</td></tr> \n"; 
}
#
#     close file and table and page
#
close (INFO);
print "</table> \n";
print "<p>";
print "Questions and Comments to \&lt;webster\@pvs.k12.nm.us\&gt;\n";
print "<p>";
print "</center></body></html> \n\n";

# end of script.
