#!/usr/local/bin/perl
$| = 1;

$html_header = "Content-type: text/html\n\n";

$gmtTime = `date -u +%H:%M:%S`;
chop $gmtTime;
$showTime = `date -u +%H%M`;
chop $showTime;
$gmtDay = `date -u +%Y-%m-%d`;
chop $gmtDay;
	
$styleSheet = "/home/hfradio/html/cgi-bin/styles.format";
$theMenu = "/home/hfradio/html/cgi-bin/menu2.format";

$remoteHost = $ENV{'REMOTE_HOST'};
	
&GetFormInput;

$solarReport = "";
doSolarData();
initSomeStuff();

%colorTable = (
"0" => "Black",
"1" => "Brown",
"2" => "Red",
"3" => "Orange",
"4" => "Yellow",
"5" => "Green",
"6" => "Blue",
"7" => "Violet",
"8" => "Gray",
"9" => "White"
);

%colorMult = (
"0" => "1",
"1" => "10",
"2" => "100",
"3" => "1000",
"4" => "10000",
"5" => "100000",
"6" => "1000000",
"7" => "10000000",
"8" => "100000000",
"9" => "1000000000",
".01" => "0.01",
".1" => "0.1"
);

print $html_header;

$result = "";
$one = "";
$two = "";
$three="";

if (exists ($field{'rescol'})) {
	$one = $field{'rescol'} + 0;
	$two = $field{'res2'} + 0;
	$three = $colorMult{$field{'res3'}};
	$fdigit = $one . $two;
	$newValue = $fdigit * $three;
	$mValue = "";
	if ($newValue > 1000000) {
		$mValue = $newValue / 1000000;
		$mValue = "(" . $mValue . "M Ohm)";
	} elsif ($newValue > 1000) {
		$mValue = $newValue / 1000;
		$mValue = "(" . $mValue . "K Ohm)";
	}

	$result .= "<b>Your result is:</b><br>\n";

	$result .= "<b>$newValue</b> $mValue with $field{'res4'}\% tolerance.<br>\n<br>\n";

} elsif (exists ($field{'resval'})) {
	my ($theValue) = $field{'resval'} + 0;

#	$three = "Black";
#	$newValue = $theValue / 1;

	if ($theValue < 100) {
		$three = "Black";
		$newValue = $theValue / 1;
	} elsif (($theValue >= 100) && ($theValue < 1000)) {
		$three = "Brown";
		$newValue = $theValue / 10;
	} elsif (($theValue >= 1000) && ($theValue < 10000)) {
		$three = "Red";
		$newValue = $theValue / 100;
	} elsif (($theValue >= 10000) && ($theValue < 100000)) {
		$three = "Orange";
		$newValue = $theValue / 1000;
	} elsif (($theValue >= 100000) && ($theValue < 1000000)) {
		$three = "Yellow";
		$newValue = $theValue / 10000;
	} elsif (($theValue >= 1000000) && ($theValue < 10000000)) {
		$three = "Green";
		$newValue = $theValue / 100000;
	} elsif (($theValue >= 10000000) && ($theValue < 100000000)) {
		$three = "Blue";
		$newValue = $theValue / 1000000;
	} elsif (($theValue >= 100000000) && ($theValue < 1000000000)) {
		$three = "Violet";
		$newValue = $theValue / 10000000;
	} elsif (($theValue >= 1000000000) && ($theValue < 10000000000)) {
		$three = "Gray";
		$newValue = $theValue / 100000000;
	} elsif (($theValue >= 10000000000) && ($theValue < 100000000000)) {
		$three = "White";
		$newValue = $theValue / 1000000000;
	}

	$two = $newValue % 10;
	$one = $newValue / 10;
	$one =~ s/^(\d+?)\..*/$1/;
	$two = $colorTable{$two};
	$one = $colorTable{$one};
	$result .= "<b>Your result is:</b><br>\n";
	$result .= "For a value of $theValue, the color code is<br>\n";
	$result .= "<b>$one $two $three</b><br>\n<br>\n";
}


print <<hi1;
<html>
<head>
<title>Resistor Color Code Calculator</title>
$_style
</head>
<body background="http://hfradio.org/graphics/backmap.gif" bgcolor="#FFFFFF">
<div align=center>
<table width="100%"><tr>
<td valign=top>
<img src="http://hfradio.org/graphics/hfr-l.gif" width=171 height=82 border=0 alt="hfradio.org" vspace="0"><br>
<!-- img src="http://hfradio.org/graphics/swl.gif" width=168 height=30 border=0 alt="SWL Search from NW7US" vspace="0" -->
</td>
<td>
<center>
<br><br>
<font face="Verdana, Arial, sans-serif" size="-1">
$_menu
<br>
<small>
$solarReport
</small>
</font>
</center>

</td>
</tr>
</table>
</div>
<hr noshade>
<div align=center>
<table width="100%">
<tr>
<td valign=top>
<form action="http://hfradio.org/resistor/" method="post" enctype="application/x-www-form-urlencoded">
<b>Enter the Resistor Value</b> like, '120000' for 120 K Ohms<br>
<input type="text" name="resval" size=9 maxlength=15><br>
<input type="submit" value="Create Color Bands"><br>
</form>
<br> -- or --
<form action="http://hfradio.org/resistor/" method="post" enctype="application/x-www-form-urlencoded">
<b>Enter the Bands to decode</b><br>
Band One: <select name="rescol">
<option value="0">Black</option>
<option value="1">Brown</option>
<option value="2">Red</option>
<option value="3">Orange</option>
<option value="4">Yellow</option>
<option value="5">Green</option>
<option value="6">Blue</option>
<option value="7">Violet</option>
<option value="8">Gray</option>
<option value="9">White</option>
</select>
Band Two: <select name="res2">
<option value="0">Black</option>
<option value="1">Brown</option>
<option value="2">Red</option>
<option value="3">Orange</option>
<option value="4">Yellow</option>
<option value="5">Green</option>
<option value="6">Blue</option>
<option value="7">Violet</option>
<option value="8">Gray</option>
<option value="9">White</option>
</select><br>
Band Three: <select name="res3">
<option value="0">Black</option>
<option value="1">Brown</option>
<option value="2">Red</option>
<option value="3">Orange</option>
<option value="4">Yellow</option>
<option value="5">Green</option>
<option value="6">Blue</option>
<option value="7">Violet</option>
<option value="8">Gray</option>
<option value="9">White</option>
<option value=".01">Silver</option>
<option value=".1">Gold</option>
</select>
Band Four: <select name="res4">
<option value="5">Gold</option>
<option value="10">Silver</option>
<option value="0">None</option>
</select><br>
<input type="submit" value="Translate color bands"><br>
</form>
</td>
<td><br></td>
<td valign=top>
$result
<b>Fill out either form, to find out the value of a resistor, or the color bands for a value of a resistor.</b>
</td>
</tr>
</table>
</div>
<hr noshade>
<br><br>
<div align=center>
$_menu
</div>
<br>
<hr noshade>
<br>
<div align=center>
<img src="http://hfradio.org/cgi-bin/Count.cgi?df=nw7us-resister.dat|dd=E|ft=0">
</div>
<br><br>
<div align=center>
<A HREF="http://www.topsitelists.com/world/w5www/topsites.cgi?ID=9" target="oWin"><IMG SRC="http://hfradio.org/graphics/top100.jpg" border=2 width=468 height=60></A><br>
<br>
<A HREF="http://www.topsitelists.com/world/w5www/topsites.cgi?ID=9" target="oWin">Click here to Vote for this site</a>.
</div>
<br><br>
<!-- BEGIN LINKEXCHANGE CODE --> 
<center>
<iframe src="http://leader.linkexchange.com/32/X1063193/showiframe?" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no>
<a href="http://leader.linkexchange.com/32/X1063193/clickle" target="leWin"><img width=468 height=60 border=0 ismap alt="" src="http://leader.linkexchange.com/32/X1063193/showle?"></a>
</iframe>
<br>
<a href="http://leader.linkexchange.com/32/X1063193/clicklogo" target="leWin"><img src="http://leader.linkexchange.com/32/X1063193/showlogo?" width=468 height=16 border=0 ismap alt=""></a>
<br>
</center>
<!-- END LINKEXCHANGE CODE --></body>
</html>
hi1


sub GetFormInput {

	(*fval) = @_ if @_ ;

	local ($buf);
	if ($ENV{'REQUEST_METHOD'} eq 'POST') {
		read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
	}
	else {
		$buf=$ENV{'QUERY_STRING'};
	}
	if ($buf eq "") {
			return 0 ;
		}
	else {
 		@fval=split(/&/,$buf);
		foreach $i (0 .. $#fval){
			($name,$val)=split (/=/,$fval[$i],2);
			$val=~tr/+/ /;
			$val=~ s/%(..)/pack("c",hex($1))/ge;
			$name=~tr/+/ /;
			$name=~ s/%(..)/pack("c",hex($1))/ge;

			if (!defined($field{$name})) {
				$field{$name}=$val;
			}
			else {
				$field{$name} .= ",$val";
			}


		   }
		}
return 1;
}

sub doSolarData {

	$solarReport = "";

	$solarReport = "| Current Solar Flux: <b>";
	open (PIN, "/hfradio/html/prop/.flux.txt");
	my ($line) = "";
	while (<PIN>) {
		$line = $_;
		$line =~ tr/\n\r\t//d;
		$solarReport .= $line;
	};
	close(PIN);
	$solarReport .= "</b> | A-index: <b>";
	open (PIN, "/hfradio/html/prop/.aindex.txt");
	while (<PIN>) {
		$line = $_;
		$line =~ tr/\n\r\t//d;
		$solarReport .= $line;
	};
	close(PIN);
	$solarReport .= "</b> |<br>\n| K-index: <b>";
	open (PIN, "/hfradio/html/prop/.kindex.txt");
	while (<PIN>) {
		$line = $_;
		$line =~ tr/\n\r\t//d;
		$solarReport .= $line;
	};
	close(PIN);
	$solarReport .= "</b> | Aurora: <b>";
	open (PIN, "/hfradio/html/prop/.aurora_level.txt");
	while (<PIN>) {
		$line = $_;
		$line =~ tr/\n\r\t//d;
		$solarReport .= $line;
	};
	close(PIN);
	$solarReport .= "</b> | <a href=\"http://hfradio.org/propagation.html\">more...</a>\n";

}

sub initSomeStuff {

        open (STYLE, "$styleSheet") || die "Could not read $styleSheet: $!\n";
        $_style = "";
        while (<STYLE>) {
                $_style .= $_;
        }
        close (STYLE);

        open (MENU, "$theMenu") || die "Could not read $theMenu: $!\n";
        $_menu = "";
        while (<MENU>) {
                $_menu .= $_;
        }
        close (MENU);

}
