// *** Global Variable & Data Definitions **************************************
var property = new Array();
var unit = new Array();
var factor = new Array();

property[0] = "----------";
unit[0] = new Array("------------");
factor[0] = new Array(0);

property[1] = "Length";
unit[1] = new Array("Metre", "Centimetre", "Kilometre", "Foot", "Inch", "Micrometre", "Millimetre", "Mile (int'l nautical)", "Mile (UK nautical)", "Mile (US nautical)", "Mile (US statute)", "Yard");
factor[1] = new Array(1, .01, 1000, .3048, .0254, .000001, .001, 1852, 1853.184, 1852, 1609.344, .9144);

property[2] = "Mass";
unit[2] = new Array("Kilogram", "Gram", "Milligram", "Pound", "Ounce", "Ton (metric)", "Tonne");
factor[2] = new Array(1, .001, 1e-6, 50.80235, .02834952, 1000, 1000);

property[3] = "Volume & Capacity";
unit[3] = new Array("Cubic Metre", "Cubic centimetre", "Cubic millimetre", "Cup", "Fluid ounce (US)", "Cubic foot", "Gallon (UK)", "Gallon (US,dry)", "Gallon (US,liq)", "Cubic inch (in^3)", "Liter (new)", "Liter (old)", "Ounce (UK,fluid)", "Ounce (US,fluid)", "Quart (US,dry)", "Quart (US,liq)");
factor[3] = new Array(1, .000001, .000000001, .0002365882, .00002957353, .02831685, .004546087, .004404884, .003785412, .00001638706, .001, .001000028, .00002841305, .00002957353, .001101221, 9.46353E-04);

// *** Functions *************************************************************

function UpdateUnitMenu(propMenu, unitMenu){
	// Updates the units displayed in the unitMenu according to the selection of
	// property in the propMenu.
	var i;

	i = propMenu.selectedIndex;
	FillMenuWithArray(unitMenu, unit[i]);
}

function FillMenuWithArray(myMenu, myArray){
	// Fills the options of myMenu with the elements of myArray.
	// !CAUTION!: It replaces the elements, so old ones will be deleted.
	var i;

	myMenu.length = myArray.length;
	for(i = 0; i < myArray.length; i++){
		myMenu.options[i].text = myArray[i];
	}
}

function CalculateUnit(sourceForm, targetForm){
	// A simple wrapper function to validate input before making the conversion
	var sourceValue = sourceForm.unit_input.value;

	// First check if the user has given numbers or anything that can be made to
	// one...
	sourceValue = parseFloat(sourceValue);
	if ( !isNaN(sourceValue) || sourceValue == 0){
		// If we can make a valid floating-point number, put it in the
		// text box and convert!
		sourceForm.unit_input.value = sourceValue;
		ConvertFromTo(sourceForm, targetForm);
		} else {
		alert("What you gave me cannot be converted or is zero!");
	}
}

function ConvertFromTo(sourceForm, targetForm){
	// Converts the contents of the sourceForm input box to the units specified in
	// the targetForm unit menu and puts the result in the targetForm input box.
	// In other words, this is the heart of the whole script...
	var propIndex;
	var sourceIndex;
	var sourceFactor;
	var targetIndex;
	var targetFactor;
	var result;

	// Start by checking which property we are working in...
	propIndex = document.property_form.the_menu.selectedIndex;

	// Let's determine what unit are we converting FROM (i.e. source) and the
	// factor needed to convert that unit to the base unit.
	sourceIndex = sourceForm.unit_menu.selectedIndex;
	sourceFactor = factor[propIndex][sourceIndex];

	// Cool! Let's do the same thing for the target unit --the units we are
	// converting TO:
	targetIndex = targetForm.unit_menu.selectedIndex;
	targetFactor = factor[propIndex][targetIndex];

	// Simple, huh? let's do the math: a) convert the source TO the base unit:
	// (The input has been checked by the CalculateUnit function).
	result = sourceForm.unit_input.value;
	// Handle Temperature increments!
	if (property[propIndex] == "Temperature"){
		result = parseFloat(result) + tempIncrement[sourceIndex];
	}
	result = result * sourceFactor;

	// not done yet... now, b) use the targetFactor to convert FROM the base unit
	// to the target unit...
	result = result / targetFactor;
	// Again, handle Temperature increments!
	if (property[propIndex] == "Temperature"){
		result = parseFloat(result) - tempIncrement[targetIndex];
	}

	// Ta-da! All that's left is to update the target input box:
	targetForm.unit_input.value = result;
}

function ClearForm(){
	// Clears the input boxes...
	document.form_A.unit_input.value = "";
	document.form_B.unit_input.value = "";
}

// *** End of Main Script ****************************************************

// end hide -->
