﻿// JScript File

// Length Converter script- By Bruce Zhang
// For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
// Please keep this notice intact

var factors0 = new Array(1, 0.1, 0.001, 0.000001, 0.000000621, 0.03937, 0.00328, 0.001094);		//Millimetres
var factors1 = new Array(10 ,1, 0.01, 0.00001, 0.00000621, 0.3937, 0.0328, 0.01094);			//Centimetres
var factors2 = new Array(1000 ,100, 1, 0.001, 0.000621, 39.37, 3.28,  1.094);					//Metres
var factors3 = new Array(1000000 ,100000, 1000, 1, 0.621, 39370, 3280, 1094);					//Kilometres
var factors4 = new Array(1609340 ,160934, 1609.34, 1.60934, 1, 63360, 5280,1760);				//miles
var factors5 = new Array(25.4 ,2.54, 0.0254, 0.0000254, 0.0000158, 1, 0.08333, 0.02778);		//inches
var factors6 = new Array(304.8 ,30.48, 0.3048, 0.0003048, 0.0001896, 12, 1, 0.3333);			//feet
var factors7 = new Array(914.4 ,91.44, 0.9144, 0.0009144, 0.0005688, 36, 3, 1);					//yards
var factors = new Array(factors0,factors1,factors2,factors3,factors4,factors5,factors6,factors7);

function convert_unit()
{
ctrfrom_unit=document.getElementById('from_unit');
ctrto_unit=document.getElementById('to_unit');
ctrfrom_value = document.getElementById('from_value');
	from_index = ctrfrom_unit.selectedIndex;
	to_index = ctrto_unit.selectedIndex;
	factor = factors[from_index][to_index];
	document.getElementById("formula").innerHTML = ctrfrom_unit.options[ctrfrom_unit.selectedIndex].text + " = " + factor + " " + ctrto_unit.options[ctrto_unit.selectedIndex].text;
	if(isNaN(ctrfrom_value.value))
		document.getElementById("to_value").innerHTML = "Not a valid number.";
	else
		document.getElementById("to_value").innerHTML = factor * ctrfrom_value.value;
}



//VOLUME FACTORS
//Factors from http://www.onlineconversion.com/volume.htm
var vfactors0 = new Array(1, 0.000001, 0.001, 0.06102374, 0.00003531, 0.03381402, 0.0017597539864, 0.00026417, 0.00000131,1, 0.033814022701, 0.0021133764189, 0.00026417205236);	//cubic centimetres
var vfactors1 = new Array(1000000, 1, 1000, 61023.7441, 35.3146667, 33814.0227, 1759.7539864, 264.172052, 1.30795062, 1000000, 33814.022701, 2113.3764189, 264.17205236);	//cubic metre
var vfactors2 = new Array(1000, 0.001, 1, 61.0237441, 0.03531467, 33.8140227, 1.759753986, 0.2199692483, 0.00130795, 1000, 33.814022701, 2.1133764189, 0.26417205236);		//litre
var vfactors3 = new Array(16.387064, 0.00001639, 0.01638706, 1, 0.00057870, 0.55411255, 0.028837201199, 0.00432900, 0.00002143, 16.387064, 0.5541125541, 0.034632034632, 0.004329004329);		//cubic inch
var vfactors4 = new Array(28316.8466, 0.02831685, 28.3168466, 1728, 1, 957.506494, 49.830683672, 7.48051948, 0.03703704, 28316.846592, 957.50649349, 59.844155844, 7.4805194805);	//cubic foot
var vfactors5 = new Array(29.5735296, 0.00002957, 0.02957353, 1.80468750, 0.00104438, 1, 0.05, 0.00781250, 0.00003868, 28.4130625, 0.96075994039, 0.060047496275, 0.0075059370344);	//fluid ounce
var vfactors6 = new Array(568.26125, 0.00056826125, 0.56826125, 34.677429099, 0.020067956654, 20, 1, 0.125, 0.00074325765387, 568.26125, 19.215198808, 1.2009499255, 0.15011874069);	//pint
var vfactors7 = new Array(3785.41178, 0.00378541, 4.54609, 231.000000, 0.13368056, 128.000000, 8, 1, 0.00495113, 4546.09, 153.72159046, 9.607599404, 1.2009499255);	//gallon
var vfactors8 = new Array(764554.858, 0.76455486, 764.554858, 46656.0000, 27.0000000, 25852.6753, 1345.4284591, 201.974026, 1, 764554.85798, 25852.675324, 1615.7922078, 201.97402597);		//yard
var vfactors9 = new Array(1, 0.000001, 0.001, 0.061023744095, 0.000035314666721, 0.035195079728, 0.0017597539864, 0.0002199692483, 0.0000013079506193, 1, 0.033814022701, 0.0021133764189, 0.00026417205236);	//Milliliter
var vfactors10 = new Array(29.573529563, 0.000029573529563, 0.029573529563, 1.8046875, 0.0010443793403, 1.0408427308, 0.05204213654, 0.0065052670675, 0.000038680716307, 29.573529563, 1, 0.062500000001, 0.0078125000001);	//US Fluid Ounce
var vfactors11 = new Array(473.176473, 0.000473176473, 0.473176473, 28.875, 0.016710069444, 16.653483693, 0.83267418463, 0.10408427308, 0.00061889146091, 473.176473, 16, 1, 0.125);	//US Pint
var vfactors12 = new Array(3785.411784, 0.003785411784, 3.785411784, 231, 0.13368055556, 133.22786954, 6.661393477, 0.83267418463,  0.0049511316873, 3785.411784, 128, 8, 1);	//US Gallon

var vfactors = new Array(vfactors0,vfactors1,vfactors2,vfactors3,vfactors4,vfactors5,vfactors6,vfactors7,vfactors8,vfactors9,vfactors10,vfactors11,vfactors12);

function convert_volume()
{
	ctrfrom_unit=document.getElementById('vfrom_unit');
	ctrto_unit=document.getElementById('vto_unit');
	ctrfrom_value = document.getElementById('vfrom_value');
	from_index = ctrfrom_unit.selectedIndex;
	to_index = ctrto_unit.selectedIndex;
	factor = vfactors[from_index][to_index];
	document.getElementById("vformula").innerHTML = ctrfrom_unit.options[ctrfrom_unit.selectedIndex].text + " = " + factor + " " + ctrto_unit.options[ctrto_unit.selectedIndex].text;
	if(isNaN(ctrfrom_value.value))
		document.getElementById("vto_value").innerHTML = "Not a valid number.";
	else
		document.getElementById("vto_value").innerHTML = factor * ctrfrom_value.value;
}



//WEIGHT FACTORS
//Factors from http://www.onlineconversion.com/volume.htm
var wfactors0 = new Array(1, 0.015432358353, 0.001, 0.00003527396195, 0.000001, 0.0000022046226218, 1.0, 9.842065276, 1.5747304442, 1.9684130552, 9.8420652761, 1);		//milligram 
var wfactors1 = new Array(64798.91, 1, 0.06479891, 0.0022857142857, 0.00006479891, 0.00014285714286, 6.479891, 6.3775510204, 0.000010204081633, 0.0000012755102041, 6.3775510204, 6.479891);		//grain
var wfactors2 = new Array(1000, 15.432358353, 1, 0.03527396195, 0.001, 0.0022046226218, 0.000001, 9.8420652761, 0.00015747304442, 0.000019684130552, 9.8420652761, 0.000001);		//gram 
var wfactors3 = new Array(28349.523125, 437.5, 28.349523125, 1, 0.028349523125, 0.0625, 0.000028349523125, 0.000027901785714, 0.0044642857143, 0.00055803571429, 0.000027901785714, 0.000028349523125);		//ounce 
var wfactors4 = new Array(1000000, 15432.358353, 1000, 35.27396195, 1, 2.2046226218, 0.001, 0.00098420652761, 0.15747304442, 0.019684130552, 0.00098420652761, 0.001);		//kilogram 
var wfactors5 = new Array(453592.37, 7000, 453.59237, 16, 0.45359237, 1, 0.00045359237, 0.00044642857143, 0.071428571429, 0.0089285714286, 0.00044642857143, 0.00045359237);		//lb
var wfactors6 = new Array(1000000000, 15432358.353, 1000000, 35273.96195, 1000, 2204.6226218, 1, 0.98420652761, 157.47304442, 19.684130552, 0.98420652761, 1);		//tonne 
var wfactors7 = new Array(1016046908.8, 15680000, 1016046.9088, 35840, 1016.0469088, 2240, 1.0160469088, 1, 160, 20, 1, 1.0160469088);		//ton 
var wfactors8 = new Array(6350293.18, 98000, 6350.29318, 224, 6.35029318, 14, 0.00635029318, 0.00625, 1, 0.125, 0.00625, 0.00635029318);		//stone  
var wfactors9 = new Array(50802345.44, 784000, 50802.34544, 1792, 50.80234544, 112, 0.05080234544, 0.05, 8, 1, 0.05, 0.05080234544);		//hundredweight  
var wfactors10 = new Array(1016046908.8, 15680000, 1016046.9088, 35840, 1016.0469088, 2240, 1.0160469088, 1, 160, 20, 1, 1.0160469088);		//long ton 
var wfactors11 = new Array(1000000000, 15432358.353, 1000000, 35273.96195, 1000, 2204.6226218, 1, 0.98420652761, 157.47304442, 19.684130552, 0.98420652761, 1);		//ton metric
var wfactors = new Array(wfactors0,wfactors1,wfactors2,wfactors3,wfactors4,wfactors5,wfactors6,wfactors7,wfactors8,wfactors9,wfactors10,wfactors11);

function convert_weight()
{
	ctrfrom_unit=document.getElementById('wfrom_unit');
	ctrto_unit=document.getElementById('wto_unit');
	ctrfrom_value = document.getElementById('wfrom_value');
	from_index = ctrfrom_unit.selectedIndex;
	to_index = ctrto_unit.selectedIndex;
	factor = wfactors[from_index][to_index];
	document.getElementById("wformula").innerHTML = ctrfrom_unit.options[ctrfrom_unit.selectedIndex].text + " = " + factor + " " + ctrto_unit.options[ctrto_unit.selectedIndex].text;
	if(isNaN(ctrfrom_value.value))
		document.getElementById("wto_value").innerHTML = "Not a valid number.";
	else
		document.getElementById("wto_value").innerHTML = factor * ctrfrom_value.value;
}




//AREA FACTORS
//Factors from http://www.onlineconversion.com/area.htm
//Array(mm, cm, inch, meter, yard, hectare, acres, km, mile, foot)
var afactors0 = new Array(1, 0.01, 0.0015500031, 0.000001, 0.0000011959900463, 0.0000000001, 0.00000000024710538147, 0.000000000001, 0.00000000000038610215855, 0.000010763910417);		//mm sq
var afactors1 = new Array(100, 1, 0.15500031, 0.0001,  0.00011959900463, 0.00000001, 0.000000024710538147, 0.0000000001, 0.000000000038610215855, 0.0010763910417);		//cm sq
var afactors2 = new Array(645.16, 6.4516, 1, 0.00064516, 0.00077160493827, 0.000000064516, 0.00000015942250791, 0.00000000064516, 0.00000000024909766861, 0.0069444444444);		//inch sq
var afactors3 = new Array(1000000, 10000, 1550.0031, 1, 1.1959900463, 0.0001, 0.00024710538147, 0.000001, 0.00000038610215855, 10.763910417);		//meter sq
var afactors4 = new Array(836127.36, 8361.2736, 1296, 0.83612736, 1, 0.000083612736, 0.00020661157025, 0.00000083612736, 0.00000032283057852, 9);		//yard sq
var afactors5 = new Array(10000000000, 100000000, 15500031, 10000, 11959.900463, 1, 2.4710538147, 0.01,  0.0038610215855, 107639.10417);		//hectare
var afactors6 = new Array(4046856422.4, 40468564.224, 6272640, 4046.8564224, 4840, 0.40468564224, 1, 0.0040468564224, 0.0015625, 43560);		//acres
var afactors7 = new Array(1000000000000, 10000000000, 1550003100, 1000000, 1195990.0463, 100, 247.10538147, 1, 0.38610215855, 10763910.417);		//km
var afactors8 = new Array(2589988110300, 25899881103, 4014489599.9, 2589988.1103, 3097600, 258.99881103, 639.99999999, 2.5899881103, 1, 27878400);		//mile
var afactors9 = new Array(92903.04, 929.0304, 144, 0.09290304, 0.11111111111, 0.000009290304, 0.000022956841139, 0.00000009290304, 0.00000003587006428, 1);		//foot
var afactors = new Array(afactors0,afactors1,afactors2,afactors3,afactors4,afactors5,afactors6,afactors7,afactors8,afactors9);

function convert_area()
{
	ctrfrom_unit=document.getElementById('afrom_unit');
	ctrto_unit=document.getElementById('ato_unit');
	ctrfrom_value = document.getElementById('afrom_value');
	from_index = ctrfrom_unit.selectedIndex;
	to_index = ctrto_unit.selectedIndex;
	factor = afactors[from_index][to_index];
	document.getElementById("aformula").innerHTML = ctrfrom_unit.options[ctrfrom_unit.selectedIndex].text + " = " + factor + " " + ctrto_unit.options[ctrto_unit.selectedIndex].text;
	if(isNaN(ctrfrom_value.value))
		document.getElementById("ato_value").innerHTML = "Not a valid number.";
	else
		document.getElementById("ato_value").innerHTML = factor * ctrfrom_value.value;
}


//TEMPERATURE FACTORS
//Formulas from http://www.onlineconversion.com/temperature.htm
function convert_temp()
{
	ctrfrom_unit=document.getElementById('tfrom_unit');
	ctrto_unit=document.getElementById('tto_unit');
	ctrfrom_value = document.getElementById('tfrom_value');

	from_index = ctrfrom_unit.selectedIndex;
	to_index = ctrto_unit.selectedIndex;
	
	if (from_index == 0 && to_index == 1)
	{
		//convert celsius to fahrenheit
		document.getElementById("tformula").innerHTML = "Celsius to Fahrenheit = (1.8 * &#176;C) + 32"
		
		if(isNaN(ctrfrom_value.value))
		{
			document.getElementById("tto_value").innerHTML = "Not a valid number.";
		}
		else
		{
			document.getElementById("tto_value").innerHTML = (1.8 * ctrfrom_value.value) + 32;
		}
	}	
	else if (from_index == 1 && to_index == 0)
	{
		//convert fahrenheit to celsius
		document.getElementById("tformula").innerHTML = "Fahrenheit to Celsius = (5/9) * (&#176;F - 32)"
	
		if(isNaN(ctrfrom_value.value))
		{
			document.getElementById("tto_value").innerHTML = "Not a valid number.";
		}
		else
		{
			document.getElementById("tto_value").innerHTML = (5/9) * (ctrfrom_value.value - 32);
		}
	}	
	else
	{
		//dont convert anything
		document.getElementById("tformula").innerHTML = ctrfrom_unit.value + " = 1 " + ctrfrom_unit.value
		
		if(isNaN(ctrfrom_value.value))
		{
			document.getElementById("tto_value").innerHTML = "Not a valid number.";
		}
		else
		{
			document.getElementById("tto_value").innerHTML = ctrfrom_value.value;
		}		
	}
}



//PRESSURE FACTORS
//Factors from http://www.onlineconversion.com/pressure.htm
//Array(Bar, PSI)
var pfactors0 = new Array(1, 14.503773801);		//Bar
var pfactors1 = new Array(0.0689475728, 1);		//PSI
var pfactors = new Array(pfactors0, pfactors1);

function convert_pressure()
{
	ctrfrom_unit=document.getElementById('pfrom_unit');
	ctrto_unit=document.getElementById('pto_unit');
	ctrfrom_value = document.getElementById('pfrom_value');
	from_index = ctrfrom_unit.selectedIndex;
	to_index = ctrto_unit.selectedIndex;
	factor = pfactors[from_index][to_index];
	document.getElementById("pformula").innerHTML = ctrfrom_unit.options[ctrfrom_unit.selectedIndex].text + " = " + factor + " " + ctrto_unit.options[ctrto_unit.selectedIndex].text;
	if(isNaN(ctrfrom_value.value))
		document.getElementById("pto_value").innerHTML = "Not a valid number.";
	else
		document.getElementById("pto_value").innerHTML = factor * ctrfrom_value.value;
}