/**
 * @author jnewman
 */

function convert(inputValue, inputUnit, outputUnit, precision)
{
	var val = parseFloat(inputValue);
	
	var units = {
		"AU": 6.684587124055793e-12,
		"Bolt": 0.027340332458442695,
		"Cable": 0.004556722076407116,
		"Caliber": 3937.0078740157483,
		"Centimeter": 100,
		"Chain": 0.049709695378986715,
		"Cicero": 221.66666666666666,
		"Cubit": 2.1872265966754156,
		"Didot": 2660,
		"DidotPoint": 2660,
		"Ell": 0.8748906386701663,
		"Fathom": 0.5468066491688539,
		"Fermi": 1e15,
		"Foot": 3.2808398950131235,
		"Furlong": 0.004970969537898671,
		"Hand": 9.84251968503937,
		"Inch": 39.37007874015748,
		"League": 0.000207123730745778,
		"LightYear": 1.0570265842185932e-16,
		"Link": 4.9709695378986725,
		"Meter": 1,
		"Micron": 1000000,
		"Mil": 39370.07874015748,
		"Mile": 0.0006213711922373339,
		"Millimeter": 1000,
		"NauticalMile": 0.0005399568034557236,
		"Parsec": 3.2407557442395566e-17,
		"Perch": 0.1988387815159469,
		"Pica": 236.2204724409449,
		"Point": 2834.6456692913384,
		"Pole": 0.1988387815159469,
		"Rod": 0.1988387815159469,
		"Rope": 0.16404199475065617,
		"Skein": 0.009113444152814232,
		"Span": 4.374453193350831,
		"Stadion": 0.005274662210632031,
		"Stadium": 0.005413927219493603,
		"StatuteMile": 0.0006213711922373339,
		"SurveyMile": 0.0006213711922373339,
		"XUnit": 9.980039920159682e12,
		"Yard": 1.0936132983377078
	};
	return (val * units[outputUnit] / units[inputUnit]).toFixed(precision);
}

function updateDiameter(form, dropDown)
{
	var selectedIndex = dropDown.selectedIndex;
	form.diameter_met.selectedIndex = selectedIndex;
	form.diameter_eng.selectedIndex = selectedIndex;
}

function convertTextInput(inputTextBox, inputUnit, outputTextBox, outputUnit)
{
	var inputValue = inputTextBox.value;
	var result = convert(inputValue, inputUnit, outputUnit, 3);
	if(isNaN(result)) result = 0;
	outputTextBox.value = result;
	inputTextBox.value = inputValue;
}

function setTabOrder(focusField) {
	
	var focusFieldNameParts = focusField.name.split('_');
	
	if(focusFieldNameParts.length == 1) return;
	
	var form = focusField.form;
	var fieldCount = form.elements.length;
	var tabIndex = 1;

	for(var index = 0; index < fieldCount; index++)
	{
		var formField = form.elements[index];
		var fieldNameParts = formField.name.split('_');
		
		if ((fieldNameParts[1] == focusFieldNameParts[1]) || (fieldNameParts.length == 1)) {
			formField.tabIndex = tabIndex;
			tabIndex++;
		}
		else
		{
			formField.tabIndex = 0;
		}
	}
}

