// convertunitsform.js version 1.0 2007-02-24
// 
// A JavaScript object for hooking into a basic HTML form and utilising 
// the class UnitsConverter to create a dynamic units converter 
//
// base: http://www.webaware.com.au/free/conversions/
//
// Copyright © 2007 WebAware Pty Ltd
//---------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// Full license: http://www.webaware.com.au/free/license.htm
//---------------------------------------------------------------------
// the select list of conversion types supports a CSS class name for 
// styling the conversion category separators:
//    unitConvCategoryOption
//---------------------------------------------------------------------
// methods:
// Install				intitialise members, add event hooks to HTML form
// FormCalculate		perform a unit conversion calculation for form
// UpdateFieldLabels	update form field labels per selected conversion
//---------------------------------------------------------------------

/* Minimal HTML form for the converter:

<form action="./" id="frmUnitConverter">
	<table class="inputform" align="center">
	<tr>
		<td colspan="2">
			<select name="unitConvType" id="unitConvType" size="1">
			<option value>-- No conversion types loaded --</option>
			</select>
		</td>
	</tr>
	<tr>
		<th id="unitConvInputLabel">Input value:</th>
		<td><input type="text" name="unitConvInput" id="unitConvInput" /><input type="submit" value="Convert"/></td>
	</tr>
	<tr>
		<th id="unitConvOutputLabel">Output value:</th>
		<td><input type="text" name="unitConvOutput" id="unitConvOutput" readonly="readonly" /></td>
	</tr>
	</table>
</form>

*/

// create an object for processing the conversion calculations for the form
var v_conversion = {

	// install the conversion class and add event hooks into the input form
	Install : function()
	{
		// UnitsConversion functions 
		this.uc = new UnitsConverter();
		
		// form fields and labels
		this.frmUnitConverter = document.getElementById("frmUnitConverter");
		this.unitConvType = document.getElementById("unitConvType");
		this.unitConvInputLabel = document.getElementById("unitConvInputLabel");
		this.unitConvOutputLabel = document.getElementById("unitConvOutputLabel");
		this.unitConvInput = document.getElementById("unitConvInput");
		this.unitConvOutput = document.getElementById("unitConvOutput");

		// populate the conversion type list
		if (this.unitConvType && this.uc) 
		{
			var conversions = this.uc.ConversionsCatalogue();
			var i = 0;
			
			// clear whatever is in the list now
			for (i = this.unitConvType.options.length; --i >= 0; )
				this.unitConvType.options[i] = null;
			
			this.unitConvType.options[0] = new Option("-- Select a conversion type --", "", false, false);
			i = 0;
			var units_cat = ""
			for(var c in conversions)
			{
				var catitem = conversions[c];

				if (units_cat != catitem.units_cat)
				{
					units_cat = catitem.units_cat;
					var catopt = new Option("-- " + units_cat + " --", "", false, false);
					catopt.className = "unitConvCategoryOption";
					this.unitConvType.options[++i] = catopt;
				}

				this.unitConvType.options[++i] = new Option(catitem.units_from + " to " + catitem.units_to, c, false, false);
			}
			
			// hook the onchange event for the list of conversion functions
			if (this.unitConvType.addEventListener)
				this.unitConvType.addEventListener("change", function() { v_conversion.UpdateFieldLabels(); }, false);
			else
				this.unitConvType.attachEvent("onchange", function() { v_conversion.UpdateFieldLabels(); });
		}
		
		// hook the onsubmit event for the form, and stop it from submitting the data to the server
		if (this.frmUnitConverter)
		{
			if (this.frmUnitConverter.addEventListener)
				this.frmUnitConverter.addEventListener("submit", function(e) { v_conversion.FormCalculate(); e.preventDefault(); }, false);
			else
				this.frmUnitConverter.attachEvent("onsubmit", function() { 
					v_conversion.FormCalculate(); 
					event.cancelBubble = true; 
					event.returnValue = false; 
				});
		}
	},

	// perform the calculation from form inputs
	FormCalculate : function()
	{
		if (this.unitConvInput && this.unitConvOutput && this.unitConvType && this.uc)
		{
			var errmsg = ""

			if (this.unitConvType.selectedIndex < 1 || (this.unitConvType.selectedIndex >= 1 && this.unitConvType.options[this.unitConvType.selectedIndex].value.length == 0))
				errmsg += "# Please select a conversion type\n";

			if (isNaN(parseFloat(this.unitConvInput.value)))
				errmsg += "# Input value is not a number\n";

			if (errmsg.length > 0)
				alert(errmsg)
			else
			{
				var iv = parseFloat(this.unitConvInput.value);
				this.unitConvInput.value = iv;
				this.unitConvOutput.value = this.uc.Convert(this.unitConvType.options[this.unitConvType.selectedIndex].value, iv);
			}
		}
	},

	// when the selected conversion function changes, change the labels on the input and output fields
	UpdateFieldLabels : function()
	{
		if (this.unitConvInputLabel && this.unitConvOutputLabel)
		{
			if (this.uc && this.unitConvType && this.unitConvType.selectedIndex > 0)
			{
				var fn = this.uc.DescribeConversion(this.unitConvType.options[this.unitConvType.selectedIndex].value);
				if (fn)
				{
					this.unitConvInputLabel.innerHTML = fn.units_from + ":";
					this.unitConvOutputLabel.innerHTML = fn.units_to + ":";
					return;
				}
			}
			this.unitConvInputLabel.innerHTML = "Input value:";
			this.unitConvOutputLabel.innerHTML = "Output value:";
		}
	}
}

// add a hook to the document's onload event, so that our object is initialised after page load completes
if (this.addEventListener) this.addEventListener("load", function() { v_conversion.Install(); }, false);
else if (this.attachEvent) this.attachEvent("onload", function() { v_conversion.Install(); });
