// --- JAVASCRIPT UNIT CONVERTER (JSUC) ----------------------------------------
// Based on the converter made by
// Costas Malamas
// Thessaloniki, Greece
// http://www.nyx.net/~cmalamas
//
// Customized by Heikki for tofufortwo.net

// *** Global Variable & Data Definitions **************************************
var property = new Array();
var unit = new Array();
var factor = new Array();

property[0] = "Choose a property";

property[1] = "Mass";
unit[1] = new Array("Kilogram (kg)", "Gram (gr)", "Milligram (mg)", "Pound (lb)", "Ounce (oz)" );
factor[1] = new Array(1, .001, 1e-6, .4535924, .02834952 );

// !!! Caution: Temperature requires an increment as well as a multiplying factor
// !!! and that's why it's handled differently
// !!! Be VERY careful in how you change this behavior
property[2] = "Temperature";
unit[2] = new Array("Degrees Celsius ('C)", "Degrees Fahrenheit ('F)" );
factor[2] = new Array(1,  0.555555555555 );
tempIncrement = new Array(0, -32 );

property[3] = "Volume";
unit[3] = new Array("Decilitre (dl)", "Cup", "Litre (l)" );
factor[3] = new Array(1, 2.4, 10);


// *** 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 ****************************************************


