// Unit converter tool

// First we'll build a Converter object

function ConvertToBase(value) {
	return value * this.ratio;
}

function ConvertFromBase(value) {
	return value * this.inverse;
}

function Converter(units, ratio) {
	this.units = units;
	this.ratio = ratio;
	this.inverse = 1/this.ratio;
	this.convert = ConvertToBase;
	this.invert = ConvertFromBase;
}

// Also a Category object, that holds lists of Converters

function Build_List(f) {
	f.from_units.options.length = 0;
	for(var i=0; i<this.converters.length; i++) {
		f.from_units.options[i] = new Option(this.converters[i].units,i);
	}
	f.to_units.options.length = 0;
	for(var i=0; i<this.converters.length; i++) {
		f.to_units.options[i] = new Option(this.converters[i].units,i);
	}
}

function Category(desc, base) {
	this.desc = desc;
	this.base = base;
	this.converters = new Array();
	this.build_list = Build_List;
}

// And a Converter_form object to hold them all
function Category_List(select_object) {
	var list_str = '';
	for(var i=0; i<this.categories.length; i++) {
		select_object.options[select_object.options.length] = new Option(this.categories[i].desc, i);
	}
}

function Converter_List(select_object) {
	this.categories[select_object.options[select_object.selectedIndex].value].build_list(select_object.form);
	select_object.form.to_quant.value='';
	select_object.form.from_quant.value='';
}

function Form_Convert(f) {
	var base_unit = this.categories[f.cats.selectedIndex].converters[f.from_units.selectedIndex].convert(f.from_quant.value);
	var converted = this.categories[f.cats.selectedIndex].converters[f.to_units.selectedIndex].invert(base_unit);
	f.to_quant.value = converted.toFixed(4);
}

function Converter_Form() {
	this.categories = new Array();
	this.cat_list = Category_List;
	this.con_list = Converter_List;
	this.convert = Form_Convert;
}

// Now lets create a few instances of these objects
// The container object for the form and all its functions - easy
var con_form   = new Converter_Form();
// The different classes of quantity that we might want to convert
// Give 'em a name as it will appear in the drop-down, and the base unit for conversions
// Pick a base unit in metric that is small but not too small - e.g. cm2 not mm2
var dummy      = new Category('Select Unit Type', 'Unit');
var lengths    = new Category('Lengths',          'mm');
var areas      = new Category('Areas',            'cm2');
var volumes    = new Category('Volumes',          'cm3');
var capacities = new Category('Capacities',       'ml');
var weights    = new Category('Weights',          'kg');
var pressures  = new Category('Pressure',         'kN/m2');
// Having created the categories, add them to the container object
con_form.categories[0] = dummy;
con_form.categories[1] = lengths;
con_form.categories[2] = areas;
con_form.categories[3] = volumes;
con_form.categories[4] = capacities;
con_form.categories[5] = weights;
con_form.categories[6] = pressures;
// Now create the individual converters in each category
// This can be created directly as elements of the category, no need to instantiate separately
// Make sure you set the first converter to the base unit with a value of '1'
// All others are expressed in terms of this base unit
con_form.categories[0].converters[0] = new Converter('Unit', 1);

lengths.converters[0] = new Converter('mm',1);
lengths.converters[1] = new Converter('m',1000);
lengths.converters[2] = new Converter('in',25.4);
lengths.converters[3] = new Converter('ft',304.8);

areas.converters[0] = new Converter('cm2',1);
areas.converters[1] = new Converter('m2',10000);
areas.converters[2] = new Converter('mm2',0.01);
areas.converters[3] = new Converter('ins2',6.4516);
areas.converters[4] = new Converter('ft2',929.03);
areas.converters[5] = new Converter('yd2',8361.27);

volumes.converters[0] = new Converter('cm3',1);
volumes.converters[1] = new Converter('m3',1000000);
volumes.converters[2] = new Converter('l',1000);
volumes.converters[3] = new Converter('mm3',0.001);
volumes.converters[4] = new Converter('ins3',16.387);
volumes.converters[5] = new Converter('ft3',28316.736);

capacities.converters[0] = new Converter('ml',1);
capacities.converters[1] = new Converter('cl',10);
capacities.converters[2] = new Converter('dl',100);
capacities.converters[3] = new Converter('l',1000);
capacities.converters[4] = new Converter('pt',568.2);
capacities.converters[5] = new Converter('gal',4546.4);

weights.converters[0] = new Converter('kg',1);
weights.converters[1] = new Converter('t',1000);
weights.converters[2] = new Converter('g',0.001);
weights.converters[3] = new Converter('oz',0.02835);
weights.converters[4] = new Converter('lb',0.4536);
weights.converters[5] = new Converter('cwt',50.802);
weights.converters[6] = new Converter('ton',1016);

pressures.converters[0] = new Converter('kN/m2',1);
pressures.converters[1] = new Converter('lb/in2',0.145);
