﻿function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// length
var arrLength = new Array(9);
arrLength[0] = 1;			// kilometre
arrLength[1] = 0.001;		// metre
arrLength[2] = 0.00001;		// centimetre
arrLength[3] = 0.000001;	// millimetre
arrLength[4] = 1.609344;	// miles
arrLength[5] = 0.0009144;	// yards
arrLength[6] = 0.0003048;	// feet
arrLength[7] = 0.0000254;	// inches
arrLength[8] = 0.201168;	// furlongs

// weight
var arrWeight = new Array(6);
arrWeight[0] = 1; 			// tonne
arrWeight[1] = 0.001; 		// kilogram
arrWeight[2] = 0.000001; 	// gram
arrWeight[3] = 0.000000001; // milligram
arrWeight[4] = 0.000453592; // pound
arrWeight[5] =  0.00002835 	// ounce

// fuel Economy
var arrFuel = new Array(4);
arrFuel[0] = 1;				// miles per gallon
arrFuel[1] = 0.746235992;		// kilometres per gallon
arrFuel[2] = 4.54609;		// miles per litre
arrFuel[3] = 2.8248094;		// kilometres per litre

// speed
var arrSpeed = new Array(10);
arrSpeed[0] = 1;			// miles per hour
arrSpeed[1] = 0.621371192;	// kilometres per hour
arrSpeed[2] = 0.022369363;	// centimetres per second
arrSpeed[3] = 0.681818182;	// foot per second
arrSpeed[4] = 0.056818182;	// inches per second
arrSpeed[5] = 2.236936292; 	// metres per second
arrSpeed[6] = 761.207050823;// mach
arrSpeed[7] = 1.150779448;	//knots
arrSpeed[8] = 761.207051;// speed of sound
arrSpeed[9] = 670611130.994989; // speed of light

// volume
var arrVolume = new Array(18);
arrVolume[0] = 1;				// cubic centimetre
arrVolume[1] = 28316.846711688;	// cubic feet
arrVolume[2] = 16.387064069;	// cubic inche
arrVolume[3] = 1000000000000000;// cubic kilometre
arrVolume[4] = 1000000;			// cubic metre
arrVolume[5] = 4168181843058454;// cubic mile
arrVolume[6] = 0.001;			// cubic millimetre
arrVolume[7] = 764554.861215585;// cubic yard
arrVolume[8] = 4546.09;			// gallon
arrVolume[9] = 1136.5225;		// quart
arrVolume[10]= 1000;			// litre

arrVolume[11]= 1;               // millilitre
arrVolume[12]= 10;              // centiliter

arrVolume[13]= 200000;			// drum
arrVolume[14]= 18184.36;		// bucket
arrVolume[15]= 250;				// cup
arrVolume[16]= 17.7581714;		// tablespoon
arrVolume[17]= 5.91939047;		// teaspoon
arrVolume[18]= 28.4130742;		// ounce
arrVolume[19]= 568.261485;		// pint

// area
var arrArea = new Array(10);
arrArea[0] = 1;					// acre
arrArea[1] = 2.471053815;		// hectares
arrArea[2] = 0.00000002471053815;// square centimetre
arrArea[3] = 0.000022957;		// square foot
arrArea[4] = 0.000000159422508;	// square inch
arrArea[5] = 247.105381467;	// square kilometer
arrArea[6] = 0.000247105;		// square metre
arrArea[7] = 0.000206612;		// square yard
arrArea[8] = 0.0000000002471054;// square millimeter
arrArea[9] = 640;				// square mile


function convert() {
	//get value to be converted
	var unitVal = findObj('unitValue').value;
	//get type
	var unitType = findObj('unitType').options[findObj('unitType').selectedIndex].value;
	//get 'from' unit 
	var unitFrom = findObj('unitFrom').selectedIndex;
	//get 'to' unit
	var unitTo = findObj('unitTo').selectedIndex;
	var unitResult = 0;
	
	if (unitVal != "") {
		if (unitType == "length" ) {
			unitResult = unitVal / (1/arrLength[unitFrom]);
			unitResult = unitResult * (1/arrLength[unitTo]);
		}
		else if (unitType == "weight") {
			unitResult = unitVal / (1/arrWeight[unitFrom]);
			unitResult = unitResult * (1/arrWeight[unitTo]);
		}
		else if (unitType == "fuelEconomy") {
			unitResult = unitVal / (1/arrFuel[unitFrom]);
			unitResult = unitResult * (1/arrFuel[unitTo]);
		}
		else if (unitType == "speed") {
			unitResult = unitVal / (1/arrSpeed[unitFrom]);
			unitResult = unitResult * (1/arrSpeed[unitTo]);
		}
		else if ( unitType == "volume" ) {
			unitResult = unitVal / (1/arrVolume[unitFrom]);
			unitResult = unitResult * (1/arrVolume[unitTo]);
		}
		else if ( unitType == "area" ) {
			unitResult = unitVal / (1/arrArea[unitFrom]);
			unitResult = unitResult * (1/arrArea[unitTo]);
		}
		else if (unitType == "temperature") {
			// convert everything to celsius first
			var valCelsius = 0;
			if (unitFrom == 0) { // from Celsius
				valCelsius = unitVal;
			}
			else if (unitFrom == 1) { // from Fahrenheit
				valCelsius = (unitVal - 32) * (5/9);
			}
			else if (unitFrom == 2) { // from Kelvin
				valCelsius = (unitVal - 273.15);
			}
			else if (unitFrom == 3) { // from Gas Mark
				if (unitVal == 1)
					valCelsius = 140;
				else if (unitVal == 2)
					valCelsius = 150;
				else if (unitVal == 3)
					valCelsius = 170;
				else if (unitVal == 4)
					valCelsius = 177;
				else if (unitVal == 5)
					valCelsius = 190;
				else if (unitVal == 6)
					valCelsius = 200;
				else if (unitVal == 7)
					valCelsius = 220;
				else if (unitVal == 8)
					valCelsius = 230;	
				else if (unitVal == 9)
					valCelsius = 245;
				else if (unitVal == 10)
					valCelsius = 260;
				else
					valCelsius = "nan";
			}
			
			if (unitTo == 0) { // to Celsius
				if (valCelsius == "nan"){
					unitResult = "nan";
				} else {
					unitResult = valCelsius;
				}
			}
			else if (unitTo == 1) { // to Fahrenheit
				if (valCelsius == "nan"){
					unitResult = "nan";
				} else {
					unitResult = (valCelsius * 1.8) +32;
				}
			}
			else if (unitTo == 2) { // to Kelvin
				if (valCelsius == "nan"){
					unitResult = "nan";
				} else {
					unitResult = valCelsius + 273.15;
				}
			}
			else if (unitTo == 3) { // to Gas Mark
				if (valCelsius == "nan"){
					unitResult = "nan";
				} else {
					if (valCelsius >= 130 && valCelsius <=135) {
						unitResult = 0.5;
					}
					else if (valCelsius >= 136 && valCelsius <= 145){
							unitResult = 1;
					}
					else if (valCelsius >= 146 && valCelsius <= 165){
							unitResult = 2;
					}
					else if (valCelsius >= 166 && valCelsius <= 173){
							unitResult = 3;
					}
					else if (valCelsius >= 174 && valCelsius <= 185){
							unitResult = 4;
					}
					else if (valCelsius >= 186 && valCelsius <= 195){
							unitResult = 5;
					}
					else if (valCelsius >= 196 && valCelsius <= 215){
							unitResult = 6;
					}
					else if (valCelsius >= 216 && valCelsius <=225){
							unitResult = 7;
					}
					else if (valCelsius >= 226 && valCelsius <= 240){
							unitResult = 8;
					}
					else if (valCelsius >= 241 && valCelsius <= 255){
							unitResult = 9;
					}
					else if (valCelsius >= 256 && valCelsius <= 265){
							unitResult = 10;
					}
					else{
							unitResult = "nan";
					}
				}
			}
			
		}
		if (unitResult == "nan") {
			unitResult = "Out of range";
		} else {
			unitResult = parseFloat(unitResult).toFixed(4);
		}
	}
	else {
		unitResult = "";
	}
	
	
	findObj('unitResult').value = "= " + unitResult;

}

function updateType(){
	var vUnitType = findObj('unitType');
	var vUnitFrom = findObj('unitFrom');
	var vUnitTo = findObj('unitTo');
	// clear the list
	vUnitFrom.options.length = 0;
	vUnitTo.options.length = 0;
	
	var selectedType = vUnitType.options[vUnitType.selectedIndex].value;

	if ( selectedType == "length" ) {
		
		
		vUnitFrom.options[0] = new Option('Kilometre','kilometre');
		vUnitTo.options[0] = new Option('Kilometre','kilometre');
		
		vUnitFrom.options[1] = new Option('Metre','metre');
		vUnitTo.options[1] = new Option('Metre','metre');
		
		vUnitFrom.options[2] = new Option('Centimetre','centimetre');
		vUnitTo.options[2] = new Option('Centimetre','centimetre');

		vUnitTo.options[3] = new Option('Millimetre','millimetre');
		vUnitFrom.options[3] = new Option('Millimetre','millimetre');
		
		vUnitTo.options[4] = new Option('Mile', 'mile');
		vUnitFrom.options[4] = new Option('Mile', 'mile');
		
		vUnitTo.options[5] = new Option('Yard', 'yard');
		vUnitFrom.options[5] = new Option('Yard', 'yard');
		
		vUnitTo.options[6] = new Option('Feet', 'feet');
		vUnitFrom.options[6] = new Option('Feet', 'feet');
		
		vUnitTo.options[7] = new Option('Inch', 'inch');
		vUnitFrom.options[7] = new Option('Inch', 'inch');
		
		vUnitTo.options[8] = new Option('Furlong', 'furlong');
		vUnitFrom.options[8] = new Option('Furlong', 'furlong');
	}
	else if ( selectedType == "weight" ) {
		vUnitFrom.options[0] = new Option('Tonne','tonne');
		vUnitTo.options[0] = new Option('Tonne');
		
		vUnitFrom.options[1] = new Option('Kilogram');
		vUnitTo.options[1] = new Option('Kilogram');
		
		vUnitFrom.options[2] = new Option('Gram');
		vUnitTo.options[2] = new Option('Gram');
		
		vUnitFrom.options[3] = new Option('Milligram');
		vUnitTo.options[3] = new Option('Milligram');
		
		vUnitFrom.options[4] = new Option('Pound');
		vUnitTo.options[4] = new Option('Pound');
		
		vUnitFrom.options[5] = new Option('Ounce');
		vUnitTo.options[5] = new Option('Ounce');
	} 
	else if ( selectedType == "fuelEconomy" ) {
		vUnitFrom.options[0] = new Option('Miles per Gallon');
		vUnitTo.options[0] = new Option('Miles per Gallon');
		
		vUnitFrom.options[1] = new Option('Kilometres per Gallon');
		vUnitTo.options[1] = new Option('Kilometres per Gallon');
		
		vUnitFrom.options[2] = new Option('Miles per Litre');
		vUnitTo.options[2] = new Option('Miles per Litre');
		
		vUnitFrom.options[3] = new Option('Kilometres per Litre');
		vUnitTo.options[3] = new Option('Kilometres per Litre');
	}
	else if ( selectedType == "speed") {
		vUnitFrom.options[0] = new Option('Miles p/h');
		vUnitTo.options[0] = new Option('Miles p/h');
		
		vUnitFrom.options[1] = new Option('Kilometres p/h');
		vUnitTo.options[1] = new Option('Kilometres p/h');
		
		vUnitFrom.options[2] = new Option('Centimetres p/s');
		vUnitTo.options[2] = new Option('Centimetres p/s');
		
		vUnitFrom.options[3] = new Option('Feet p/s');
		vUnitTo.options[3] = new Option('Feet p/s');
		
		vUnitFrom.options[4] = new Option('Inches p/s');
		vUnitTo.options[4] = new Option('Inches p/s');
		
		vUnitFrom.options[5] = new Option('Metres p/s');
		vUnitTo.options[5] = new Option('Metres p/s');
		
		vUnitFrom.options[6] = new Option('Mach');
		vUnitTo.options[6] = new Option('Mach');
		
		vUnitFrom.options[7] = new Option('Knot');
		vUnitTo.options[7] = new Option('Knot');
		
		vUnitFrom.options[8] = new Option('Speed of Sound');
		vUnitTo.options[8] = new Option('Speed of Sound');
		
		vUnitFrom.options[9] = new Option('Speed of Light');
		vUnitTo.options[9] = new Option('Speed of Light');
		
	}
	else if ( selectedType == "volume" ) {
		vUnitFrom.options[0] = new Option('Cubic Centimetres');
		vUnitTo.options[0] = new Option('Cubic Centimetres');
		
		vUnitFrom.options[1] = new Option('Cubic Feet');
		vUnitTo.options[1] = new Option('Cubic Feet');
			
		vUnitFrom.options[2] = new Option('Cubic Inches');
		vUnitTo.options[2] = new Option('Cubic Inches');
		
		vUnitFrom.options[3] = new Option('Cubic Kilometres');
		vUnitTo.options[3] = new Option('Cubic Kilometres');
		
		vUnitFrom.options[4] = new Option('Cubic Metres');
		vUnitTo.options[4] = new Option('Cubic Metres');
		
		vUnitFrom.options[5] = new Option('Cubic Miles');
		vUnitTo.options[5] = new Option('Cubic Miles');
		
		vUnitFrom.options[6] = new Option('Cubic Millimetres');
		vUnitTo.options[6] = new Option('Cubic Millimetres');
		
		vUnitFrom.options[7] = new Option('Cubic Yards');
		vUnitTo.options[7] = new Option('Cubic Yards');
		
		vUnitFrom.options[8] = new Option('Gallons');
		vUnitTo.options[8] = new Option('Gallons');
		
		vUnitFrom.options[9] = new Option('Quarts');
		vUnitTo.options[9] = new Option('Quarts');
		
		vUnitFrom.options[10] = new Option('Litres');
		vUnitTo.options[10] = new Option('Litres');
		
		vUnitFrom.options[11] = new Option('Millilitres');
		vUnitTo.options[11] = new Option('Millilitres');
		
		vUnitFrom.options[12] = new Option('Centilitres');
		vUnitTo.options[12] = new Option('Centilitres');
		
		vUnitFrom.options[13] = new Option('Drums');
		vUnitTo.options[13] = new Option('Drums');
		
		vUnitFrom.options[14] = new Option('Buckets');
		vUnitTo.options[14] = new Option('Buckets');
		
		vUnitFrom.options[15] = new Option('Cups');
		vUnitTo.options[15] = new Option('Cups');
		
		vUnitFrom.options[16] = new Option('Tablespoons');
		vUnitTo.options[16] = new Option('Tablespoons');
		
		vUnitFrom.options[17] = new Option('Teaspoons');
		vUnitTo.options[17] = new Option('Teaspoons');
		
		vUnitFrom.options[18] = new Option('Fluid Ounces');
		vUnitTo.options[18] = new Option('Fluid Ounces');
		
		vUnitFrom.options[19] = new Option('Pints');
		vUnitTo.options[19] = new Option('Pints');
				
	}
	else if ( selectedType == "area" ) {
		vUnitFrom.options[0] = new Option('Acres');
		vUnitTo.options[0] = new Option('Acres');
		
		vUnitFrom.options[1] = new Option('Hectares');
		vUnitTo.options[1] = new Option('Hectares');
		
		vUnitFrom.options[2] = new Option('Square Centimetres');
		vUnitTo.options[2] = new Option('Square Centimetres');
		
		vUnitFrom.options[3] = new Option('Square Feet');
		vUnitTo.options[3] = new Option('Square Feet');
		
		vUnitFrom.options[4] = new Option('Square Inches');
		vUnitTo.options[4] = new Option('Square Inches');
		
		vUnitFrom.options[5] = new Option('Square Kilometres');
		vUnitTo.options[5] = new Option('Square Kilometres');
		
		vUnitFrom.options[6] = new Option('Square Metres');
		vUnitTo.options[6] = new Option('Square Metres');
		
		vUnitFrom.options[7] = new Option('Square Yards');
		vUnitTo.options[7] = new Option('Square Yards');
		
		vUnitFrom.options[8] = new Option('Square Millimetres');
		vUnitTo.options[8] = new Option('Square Millimetres');
		
		vUnitFrom.options[9] = new Option('Square Miles');
		vUnitTo.options[9] = new Option('Square Miles');
		
	
	}
	else if ( selectedType == "temperature" ) {
		vUnitFrom.options[0] = new Option('Celsius');
		vUnitTo.options[0] = new Option('Celsius');
		
		vUnitFrom.options[1] = new Option('Fahrenheit');
		vUnitTo.options[1] = new Option('Fahrenheit');
		
		vUnitFrom.options[2] = new Option('Kelvin');
		vUnitTo.options[2] = new Option('Kelvin');
		
		vUnitFrom.options[3] = new Option('Gas Mark');
		vUnitTo.options[3] = new Option('Gas Mark');
		
	} 
	convert();
}
