/*
Conversion formulae from http://en.wikipedia.org/wiki/Conversion_of_units
Converts input unit to standard metric unit of measure then converts
standard unit to output unit
*/

function calcValues() {
	var fromType = document.getElementById('FromType').value; 	// From Unit
	var toType = document.getElementById('ToType').value;		// To Unit
	var cVal = document.getElementById('curVal').value; 		// Current Value
	cVal = cVal.replace(/,/g,"");
	
	if (cVal == '-' || cVal == '.' || cVal == '' || cVal == '0.' || parseFloat(cVal) == 0) return;
	
	var cValue = parseFloat(cVal);
	
	switch (fromType) {											// Temperature: Convert to Kelvin
		case 'F' :
			fVal = (cValue + 459.67) / 1.8;
			fType = '&deg; Fahrenheit';
			break;
		case 'C' :
			fVal = cValue + 273.15;
			fType = '&deg; Celcius';
			break;
		case 'K' :
			fVal = cValue;
			fType = '&deg; Kelvin';
			break;
		default:
			fVal = 0;
			fType = '';
		case 'inches':											// Length/Distance : Convert to Meters
			fVal = cValue * 0.0254;
			fType = 'inches';
			break;
		case 'feet':
			fVal = cValue * 0.3048;
			fType = 'feet';
			break;
		case 'yards':
			fVal = cValue * 0.9144;
			fType = 'yards';
			break;
		case 'miles':
			fVal = cValue * 1609.344;
			fType = 'miles';
			break;
		case 'mm':
			fVal = cValue / 1000;
			fType = 'millimeters';
			break;
		case 'cm':
			fVal = cValue / 100;
			fType = 'centimeters';
			break;
		case 'm':
			fVal = cValue;
			fType = 'meters';
			break;
		case 'km':
			fVal = cValue * 1000;
			fType = 'kilometers';
			break;
		case 'tsp':												// Volume : Convert to litres
			fVal = cValue * 0.004928921595;
			fType = 'teaspoons';
			break;
		case 'tbl':
			fVal = cValue * 0.0147867647825;
			fType = 'tablespoons';
			break;
		case 'floz':
			fVal = cValue * 0.0295735295625;
			fType = 'fluid ounces';
			break;
		case 'cus':
			fVal = cValue * 0.2365882365;
			fType = 'cups (US)';
			break;
		case 'pt':
			fVal = cValue * 0.473176473;
			fType = 'pints';
			break;
		case 'qt':
			fVal = cValue * 0.946352946;
			fType = 'quarts';
			break;
		case 'gal':
			fVal = cValue * 3.785411784;
			fType = 'gallons';
			break;
		case 'ml':
			fVal = cValue / 1000;
			fType = 'milliliters';
			break;
		case 'cmet':
			fVal = cValue / 4;
			fType = 'cups (metric)';
			break;
		case 'l':
			fVal = cValue;
			fType = 'liters';
			break;
		case 'cum':
			fVal = cValue * 1000;
			fType = 'cubic meters';
			break;

		case 'ozav':												// Weight : Convert to Kilograms
			fVal = cValue * 0.028349523125;
			fType = 'ounces';
			break;
		case 'lb':
			fVal = cValue * 0.45359237
			fType = 'pounds';
			break;
		case 'st':
			fVal = cValue * 6.35029318;
			fType = 'stones';
			break;
		case 'ton':
			fVal = cValue * 907.18474;
			fType = 'tons';
			break;
		case 'mg':
			fVal = cValue / 1000000;
			fType = 'milligrams';
			break;
		case 'g':
			fVal = cValue / 1000;
			fType = 'grams';
			break;
		case 'kg':
			fVal = cValue;
			fType = 'kilograms';
			break;
		case 't':
			fVal = cValue * 1000;
			fType = 'metric tonnes';
			break;
}


	switch (toType) {											// Temperature : Convert from Kelvin
		case 'F':
			result = (fVal * 9/5)-459.67;
			tType = '&deg; Fahrenheit';
			break
		case 'C':
			result = fVal - 273.15;
			tType = '&deg; Celcius';
			break
		case 'K':
			result = fVal;
			tType = '&deg; Kelvin';
			break
		default:
			result = 0;
			tType = '';
		case 'inches':											// Length/Distance : Convert from Meters
			result = fVal / 0.0254;
			tType = 'inches';
			break;
		case 'feet':
			result = fVal / 0.3048;
			tType = 'feet';
			break;
		case 'yards':
			result = fVal / 0.9144;
			tType = 'yards';
			break;
		case 'miles':
			result = fVal / 1609.344;
			tType = 'miles';
			break;
		case 'mm':
			result = fVal * 1000;
			tType = 'millimeters';
			break;
		case 'cm':
			result = fVal * 100 ;
			tType = 'centimeters';
			break;
		case 'm':
			result = fVal;
			tType = 'meters';
			break;
		case 'km':
			result = fVal / 1000;
			tType = 'kilometers';
			break;
		case 'tsp':												// Volume : Convert from litres
			result = fVal / 0.004928921595;
			tType = 'teaspoons';
			break;
		case 'tbl':
			result = fVal / 0.0147867647825;
			tType = 'tablespoons';
			break;
		case 'floz':
			result = fVal / 0.0295735295625;
			tType = 'fluid ounces';
			break;
		case 'cus':
			result = fVal / 0.2365882365;
			tType = 'cups (US)';
			break;
		case 'pt':
			result = fVal / 0.473176473;
			tType = 'pints';
			break;
		case 'qt':
			result = fVal / 0.946352946;
			tType = 'quarts';
			break;
		case 'gal':
			result = fVal / 3.785411784;
			tType = 'gallons';
			break;
		case 'ml':
			result = fVal * 1000;
			tType = 'milliliters';
			break;
		case 'cmet':
			result = fVal * 4;
			tType = 'cups (metric)';
			break;
		case 'l':
			result = fVal;
			tType = 'liters';
			break;
		case 'cum':
			result = fVal / 1000;
			tType = 'cubic meters';
			break;
		case 'ozav':												// Weight : Convert from Kilograms
			result = fVal / 0.028349523125;
			tType = 'ounces';
			break;
		case 'lb':
			result = fVal / 0.45359237
			tType = 'pounds';
			break;
		case 'st':
			result = fVal / 6.35029318;
			tType = 'stones';
			break;
		case 'ton':
			result = fVal / 907.18474;
			tType = 'tons';
			break;
		case 'mg':
			result = fVal * 1000000;
			tType = 'milligrams';
			break;
		case 'g':
			result = fVal * 1000;
			tType = 'grams';
			break;
		case 'kg':
			result = fVal;
			tType = 'kilograms';
			break;
		case 't':
			result = fVal / 1000;
			tType = 'metric tonnes';
			break;
	}

	result = Deci(result);
	cValue = addCommas(cValue);
	document.getElementById('curVal').value = cValue;

	HandleOutput(cValue,result,fVal,fType,tType);	
}

	function Deci(num) {
		x = Math.round(num * 100)/100;
		x = addCommas(x);
		return x;
	}

// Nice function taken from http://www.mredkj.com/javascript/nfbasic.html (public domain)
function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function HandleOutput(cValue,result,fVal,fType,tType) {
		document.getElementById('BigNums').innerHTML = '<table class="Centered" style="width:100%"><tr><td style="font-size: 20pt;width:45%;" class="Centered">'+cValue+' <span class="NormalFont">'+fType+'</span></td><td style="width:10%;">=</td><td style="font-size: 20pt;width:45%;" class="Centered">'+result+' <span class="NormalFont">'+tType+'</span></td></tr></table>';
	}

