<?php
	function parseLookupTable($array, $tableStart, $tableEnd,
								$rowStart, $rowEnd, $colStart, $colEnd, $endLast)
	{
		$lookupTableString = "";

		$lookupTableString .= $tableStart;
		$arrayValues = array_values($array);
		$arrayLength = count($arrayValues);
		
		for ($i = 0; $i < $arrayLength; $i++) {
			// Get column title value
			$rowVal = $arrayValues[$i];
			$lookupTableString .= $rowStart;
			for ($j = 0; $j < $arrayLength; $j++) {
				$lookupTableString .= $colStart;
				// Get row title value
				$colVal = $arrayValues[$j];
				// Put into the array the quotient of the two
				$lookupTableString .= $rowVal / $colVal;
				if (($endLast) || ($j < $arrayLength - 1)) {
					$lookupTableString .= $colEnd;
				}
			}
			if (($endLast) || ($i < $arrayLength - 1)) {
				$lookupTableString .= $rowEnd;
			}
		}
		$lookupTableString .= $tableEnd;
		return $lookupTableString;
	}

	function createJSLookupTable($lookupTableName, $array)
	{
		echo "var ".$lookupTableName." = ".parseLookupTable($array, "[", "]]", "[", "],", "", ",", false).";\n";
	}
	
	function displayLookupTable($array)
	{
		echo parseLookupTable($array, "<table>", "</table>", "<tr>", "</tr>", "<td>", "</td>", true);
	}

	function createConversionForm($property, $jsLookupTable, $conversionArray)
	{
		$formName = strtolower($property)."Form";
		$submitFormCode = "submitForm(document.".$formName.", ".$jsLookupTable.");";

		echo "\t\t\t\t\t<h4>".$property."</h4>\n";
		echo "\t\t\t\t\t<form name=\"".$formName."\">\n";
		echo "\t\t\t\t\t\t<input type=\"text\" name=\"input\" onkeyup=\"".$submitFormCode."\" />\n";
		echo "\t\t\t\t\t\t<select name=\"fromUnit\" onmouseup=\"".$submitFormCode."\">\n";

		$unitArray = array_keys($conversionArray);
		for ($i = 0; $i < count($unitArray); $i++) {
			echo "\t\t\t\t\t\t\t\t<option value=\"".$i."\">".$unitArray[$i]."</option>\n";
		}
		echo "\t\t\t\t\t\t</select>\n";
		echo "\t\t\t\t\t\tto\n";
		echo "\t\t\t\t\t\t<select name=\"toUnit\" onmouseup=\"".$submitFormCode."\">\n";

		for ($i = 0; $i < count($unitArray); $i++) {
			echo "\t\t\t\t\t\t\t\t<option value=\"".$i."\">".$unitArray[$i]."</option>\n";
		}
		
		echo "\t\t\t\t\t\t</select>\n";
		echo "\t\t\t\t\t\t<input type=\"text\" name=\"result\" readonly=\"true\" />\n";
		echo "\t\t\t\t\t</form>\n";	
	}
?>
<script type="text/javascript">
<!--
	function convert(fromUnit, toUnit, value, lookupTable)
	{
		var valueNum = parseFloat(value);
		var newValue = valueNum * lookupTable[fromUnit][toUnit];
		return newValue;
	}
	
	function submitForm(form, lookupTable)
	{
		var result = convert(form.fromUnit.value, form.toUnit.value, form.input.value, lookupTable);
		form.result.value = result;
	}
-->
</script>

<?php
	$pageTitle = "Unit Conversions";
	include("../header.php");
?>

					<div id="main-content">

<?php
	// Values are in inches
	$lengthConversionArray = array(
		"Inches" => 1,
		"Feet" => 12,
		"Yards" => 36,
		"Millimeters" => 0.0393700787,
		"Centimeters" => 0.393700787,
		"Meters" => 39.3700787,
		"Kilometers" => 39370.0787
	);

	// Values are in ounces
	$volumeConversionArray = array(
		"Ounces" => 1,
		"Cups" => 8,
		"Pints" => 16,
		"Quarts" => 32,
		"Gallons" => 128,
		"Liter" => 33.8140227
	);

	// Values are in ounces
	$weightConversionArray = array(
		"Ounces" => 1,
		"Pounds" => 16,
		"Tons" => 32000,
		"Grams" => 0.0352739619,
		"Kilograms" => 35.3739619
	);


	// Create JavaScript arrays
	echo indent(6)."<script type=\"text/javascript\">\n";
	echo indent(6)."<!--\n";

	echo indent(7);
	createJSLookupTable("lengthLookupTable", $lengthConversionArray);
	echo indent(7);
	createJSLookupTable("volumeLookupTable", $volumeConversionArray);
	echo indent(7);
	createJSLookupTable("weightLookupTable", $weightConversionArray);

	echo indent(6)."-->\n";
	echo indent(6)."</script>\n";

	createConversionForm("Length", "lengthLookupTable", $lengthConversionArray);
	createConversionForm("Volume", "volumeLookupTable", $volumeConversionArray);
	createConversionForm("Weight", "weightLookupTable", $weightConversionArray);

	//	displayLookupTable($lengthConversionArray);
?>

					</div> <!-- closes main-content --></td>

<?php
	include("../footer.php");
?>