#!/usr/local/bin/perl5

# 2:45

require ("forms.pl");
%FORM = &forms::Handle;

require("unitlist.pl");

$fields=0;

&genericlines;

@list=("energy", "pressure", "volume", "mass", "power");

# @listkind=(
#	   [$FORM{'energy_value'}, $FORM{'energyin'}, $FORM{'energout'}],
#	   [$FORM{'pressure_value'}, $FORM{'pressurein'}, $FORM{'pressureout'}],
#	   [$FORM{'volume_value'}, $FORM{'volumein'}, $FORM{'volumeout'}],
#	   [$FORM{'mass_value'}, $FORM{'massin'}, $FORM{'massout'}],
#	   [$FORM{'power_value'}, $FORM{'powerin'}, $FORM{'powerout'}],
#	   );

$listkind[0][0]=$FORM{'energy_value'};
$listkind[0][1]=$FORM{'energyin'};
$listkind[0][2]=$FORM{'energyout'};
$listkind[1][0]=$FORM{'pressure_value'};
$listkind[1][1]=$FORM{'pressurein'};
$listkind[1][2]=$FORM{'pressureout'};
$listkind[2][0]=$FORM{'volume_value'};
$listkind[2][1]=$FORM{'volumein'};
$listkind[2][2]=$FORM{'volumeout'};
$listkind[3][0]=$FORM{'mass_value'};
$listkind[3][1]=$FORM{'massin'};
$listkind[3][2]=$FORM{'massout'};
$listkind[4][0]=$FORM{'power_value'};
$listkind[4][1]=$FORM{'powerin'};
$listkind[4][2]=$FORM{'powerout'};

$count=0;
while ($count<=4) {
    if ($listkind[$count][0]!=0) {
	$fields++;
	$input=$listkind[$count][1];
	$value=$listkind[$count][0];
	$outkind=$listkind[$count][2];
	$convertcommon=$units{$input}*$value;
	$output=$convertcommon/$units{$outkind};
	$lines=$lines . "<tr>
        <td>$value $input</td>
        <td>$output $outkind</td>
        </tr>";

    }
    $count++;
}

if ($FORM{'temperature_value'}) {
    $value=$FORM{'temperature_value'};
    
    $fields++;
    
    %changecelsius = ("C", $value,
                      "F", (5/9*($value-32)),
                      "K", $value-273.15,
                      "R", (5/9*($value-459.67-32)),
                      );

    $intermediate=$changecelsius{$FORM{'temperaturein'}};
    
    %tempout=("C", $intermediate,
              "F", (9/5*$intermediate+32),
              "K", $intermediate+273.15,
              "R", (9/5*$intermediate+32+459.67),
              );

    $lines=$lines . "<tr>
        <td>$value $FORM{temperaturein}</td>
        <td>$tempout{$FORM{temperatureout}} $FORM{temperatureout}</td>
        </tr>";
    
    
}

if ($fields==0) {
    &return_blank;
} else {
    &return_conversion;
}

# no answer or first try

sub return_blank {
    print <<end_element_form;
Content-type:text/html

<HTML>
<TITLE>Unit Conversions</TITLE>
<BODY BGCOLOR="#FFFFFF">
$formlines
</BODY>
</HTML>

end_element_form
}

# correct answer

sub return_conversion {

    print <<end_element_form;
Content-type:text/html

<HTML>
<TITLE>Converted</TITLE>
<BODY BGCOLOR="#FFFFFF">
<H2><font size="+3">C</font>onversion</H2>
<center>
<table border=1>
        <tr>
        <th>Input</th>
        <th>Output</th>
        </tr>
        $lines
</table>
</center>

<hr>
<h2><font size="+3">A</font>nother <font size="+3">C</font>onversion</h2>
$formlines
</BODY>
</HTML>

end_element_form
}

sub genericlines {
    $formlines='<a name="top">
<center>
[<a href="#energy">Energy</a> | <a href="#pressure">Pressure</a> |
<a href="#volume">Volume</a> | <a href="#temperature">Temperature</a> |
<a href="#mass">Mass</a> | <a href="#power">Power</a>]
</center>

<form method=post action="http://the-tech.mit.edu/~davhsu/cgi-bin/unit.pl">

<table width=100% border=0>
<tr>
<th>Value</th>
<th>Input Unit</th>
<th>Output Unit</th>
</tr>

<a name="energy">

<th align=right>Energy
        <input type="text" name="energy_value" size="10">
</th>
<td align=left>
        <input type="radio" value="J" name="energyin">J
        <input type="radio" value="cal" name="energyin">cal
        <input type="radio" value="BTU" name="energyin">BTU
        </td>
        <td align=left>
        <input type="radio" value="J" name="energyout">J
        <input type="radio" value="cal" name="energyout">cal
        <input type="radio" value="BTU" name="energyout">BTU
        </td>
        </tr>

<tr>
<a name="pressure">
<th align=right>Pressure

        <input type="text" name="pressure_value" size="10">
</th>
<td align=left>
        <input type="radio" value="Pa" name="pressurein">Pa
        <input type="radio" value="atm" name="pressurein">atm
        <input type="radio" value="bar" name="pressurein">bar
        <input type="radio" value="mmHg" name="pressurein">mm Hg
        <input type="radio" value="psi" name="pressurein">psi
        </td>
        <td align=left>
        <input type="radio" value="Pa" name="pressureout">Pa
        <input type="radio" value="atm" name="pressureout">atm
        <input type="radio" value="bar" name="pressureout">bar
        <input type="radio" value="mmHg" name="pressureout">mm Hg
        <input type="radio" value="psi" name="pressureout">psi
        </td>
        </tr>

<tr>

<a name="volume">
<th align=right>Volume
        <input type="text" name="volume_value" size="10">
</th>
<td align=left>
        <input type="radio" value="m^3" name="volumein">m^3
        <input type="radio" value="cm^3" name="volumein">cm^3
        <input type="radio" value="L" name="volumein">L
        <input type="radio" value="ft^3" name="volumein">ft^3
        </td>
        <td align=left>
        <input type="radio" value="m^3" name="volumeout">m^3
        <input type="radio" value="cm^3" name="volumeout">cm^3
        <input type="radio" value="L" name="volumeout">L
        <input type="radio" value="ft^3" name="volumeout">ft^3
        </td>
        </tr>

<tr>

<a name="temperature">
<th align=right>Temperature
        <input type="text" name="temperature_value" size="10">
</th>
<td align=left>
        <input type="radio" value="C" name="temperaturein">&#176 C
        <input type="radio" value="F" name="temperaturein">&#176 F
        <input type="radio" value="K" name="temperaturein">K
        <input type="radio" value="R" name="temperaturein">&#176 R
        </td>
        <td align=left>
        <input type="radio" value="C" name="temperatureout">&#176 C
        <input type="radio" value="F" name="temperatureout">&#176 F
        <input type="radio" value="K" name="temperatureout">K
        <input type="radio" value="R" name="temperatureout">&#176 R
        </td>
        </tr>

<a name="mass">
<th align=right>Mass
        <input type="text" name="mass_value" size="10">
</th>
<td align=left>
        <input type="radio" value="kg" name="massin">kg
        <input type="radio" value="lb" name="massin">lb
        </td>
        <td align=left>
        <input type="radio" value="kg" name="massout">kg
        <input type="radio" value="lb" name="massout">lb
        </td>
        </tr>

<a name="power">
<th align=right>Power
        <input type="text" name="power_value" size="10">
</th>
<td align=left>
        <input type="radio" value="watt" name="powerin">watt
        <input type="radio" value="hp" name="powerin">hp
        </td>
        <td align=left>
        <input type="radio" value="watt" name="powerout">watt
        <input type="radio" value="hp" name="powerout">hp
        </td>
        </tr>

</table>
<p>
<center>
<input type="submit" value="Convert">
</center>
</p>
</form>';
}
