#!/usr//bin/perl -w

# make files readable
umask(011);

# disable buffering
$| = 1;

# Shorten our error logs
#open(STDERR,">/dev/null");

# read in STDIN from the browser
$size_of_form_information = $ENV{'CONTENT_LENGTH'};
read (STDIN, $form_info, $size_of_form_information );


@key_value_pairs = split (/&/, $form_info);
foreach $key_value (@key_value_pairs) {
	($key, $value) = split (/=/, $key_value);
	$value =~ tr/+/ /;
	$value =~ s/%([\dA-Fa-f]{2})/pack ("C",hex ($1))/eg;
	$value =~ s/\r\n\r\n/<p>/g;     # convert paragraph marks to html pgraps
	$value =~ s/\r\n/<br>/g;                # same but for dos
	$value =~ s/\n/<br>/g;          # same but for dos
#	print "<tr><td><b>$key</b><td>$value";
	$$key = $value;
}
$i=0;
open (SAVEFILE, "< /home/sjg/hhi/$savefile");
while (<SAVEFILE>) {
     if (!(m/^\s*#/)) {
	  ($name, $value)=split(/=/,$_);	
	  $$name = $value;
#	  print "$i $name = $value";
	  $sname{$i}=$name;
	  $svalue{$i}=$value;
	  $i = $i+1;
     }
}
close SAVEFILE;

# Print HTML Response
print "Content-type: text/html \n";
print "
<HTML>
<HEAD>
<link href=\"style3.css\" rel=\"stylesheet\" type=\"text/css\">
<script language=javascript type=\"text/javascript\" src=\"heating.js\"></script>
<TITLE>Home Energy Intensity Calculator</TITLE></HEAD>
<body onload=\"SumTotal()\">
<CENTER>
<h2>Home Energy Intensity Calculator</h2>
<form name=heating method=post action=\"save.pl\">
";
open (ITEMFILE, "< /home/sjg/hhi/heating.item");
while (<ITEMFILE>) {
     if (!(m/^\s*#/))  {
	  ($type, $iname, $value, $description,$price)=split(/\,/,$_);
	  $j=0;  
	  $checked="";
	  $selected="";
#	  if ($j<$i) {print "$j less than $i";}
#	  if ($sname{$j} ne $iname) {print "$sname{$j} ne $iname";}
	  while ($j<$i && $sname{$j} ne $iname) {$j=$j+1;}
	  if ($j<$i) {$value = $svalue{$j}}
	  if ($value eq "on") {$checked="checked";}
	  if ($type eq "select") {$selvalue=$value; print "$description <select name=$iname onClick=\"SumTotal()\"> $price \n"; }
	  if ($type eq "option") {
#		 print "selvalue=$selvalue";
                 if ($selvalue-$price==0) {$selected="selected";} 
	         print "<option $selected value=$price onClick=\"SumTotal()\"> $description \n"; 
          }
	  if ($type eq "endselect") {print "</select> \n <br>"; }
	  if ($type eq "paragraph") {print "$description \n <br>"; }
	  if ($price!=0) {$price = "\$" . $price;} else {$price="";}
	  if ($type eq "checkbox") {print "$description <a href=terms.html\#$iname onclick=\"window.open (this.href, 'child', 'height=200,width=300,scrollbars'); return false\">help</a> <input TYPE=$type name=$iname $checked onClick=\"SumTotal()\"> $price <br> \n"; }
	  if ($type eq "text") {print "$description <a href=terms.html\#$iname onclick=\"window.open (this.href, 'child', 'height=200,width=300,scrollbars'); return false\">help</a> <input TYPE=$type name=$iname value=\"$value\" size=30 onkeypress=\"return disableEnterKey(event)\"\> $price <br> \n";}
          if ($type eq "buttons") {print "<br> Home Energy Intensity: <input type=text name=total> <input type=\"submit\" name=\"save\" value=\"Save Configuration\" onClick=\"SumTotal()\"> <br>";}
     }
}
close (ITEMFILE);

print "
<p>
<br>
Home Heating Intensity: <input type=text name=hhi size=5> BTU/(sqft HDD)</p>
Home Cooling Intensity: <input type=text name=hci size=5> BTU/(sqft CDD)</p>
Home Energy Intensity: <input type=text name=total size=5> BTU/(sqft HDD)</p>
<input type=\"submit\" name=\"save\" value=\"Save Entries\" onClick=\"SumTotal()\">

</CENTER>
</body>
</html>
";
