
function calcSG(brixBox, sgSpan, convBox) {
	if(null != brixBox && null != sgSpan && null != brixBox.value)
	{
		if(isNaN(brixBox.value))
		{
			//alert('<p>Please enter a number in the Brix text box.</p>');
			sgSpan.innerHTML = "";
		}
		else
		{
			if('' != brixBox.value)
			{
				var brix = brixBox.value;
				var sg = (0.524484*Math.exp(Math.pow((parseFloat(brix)+330.872),2)/170435.0))/0.99823
				sgSpan.innerHTML = sg.toFixed(2);
				if(!isNaN(convBox.value))
				{
					convBox.onchange();
				}
			}
		}
	}
	else
	{
		//alert('Invalid parameters!!!');
	}
}

function convertKgToLUSG(convBox, sgSpan, spnKgL, spnKgUSG) {
	if(null != convBox && null != convBox.value && null != sgSpan && null != spnKgL && null != spnKgUSG)
	{
		if(isNaN(convBox.value))
		{
			//alert('<p>Please enter a number in the Kg text box.</p>');
		}
		else if(isNaN(sgSpan.innerHTML) || "" == sgSpan.innerHTML)
		{
			//alert('<p>Please enter a number in the Brix text box.</p>');
		}
		else
		{
			if('' != convBox.value)
			{
				var kg = convBox.value;
				var sg = sgSpan.innerHTML;
				var l = kg / sg;
				var usg = l * .264172052;
				spnKgL.innerHTML = l.toFixed(1) + "";
				spnKgUSG.innerHTML = usg.toFixed(2) + "";
			}
		}
	}
	else
	{
		//alert('Invalid parameters!!!');
	}
}

function convertLToKg(convBox, sgSpan, spnLKg) {
	if(null != convBox && null != convBox.value && null != sgSpan && null != spnLKg)
	{
		if(isNaN(convBox.value))
		{
			//alert('<p>Please enter a number in the L text box.</p>');
		}
		else if(isNaN(sgSpan.innerHTML) || "" == sgSpan.innerHTML)
		{
			//alert('<p>Please enter a number in the Brix text box.</p>');
		}
		else
		{
			if('' != convBox.value)
			{
				var l = convBox.value;
				var sg = sgSpan.innerHTML;
				var kg = l * sg;
				spnLKg.innerHTML = kg.toFixed(1) + "";
			}
		}
	}
	else
	{
		//alert('Invalid parameters!!!');
	}
}

function convertMTToLUSG(convBox, sgSpan, spnKgL, spnKgUSG) {
	if(null != convBox && null != convBox.value && null != sgSpan && null != spnKgL && null != spnKgUSG)
	{
		if(isNaN(convBox.value))
		{
			//alert('<p>Please enter a number in the $/MT text box.</p>');
		}
		else if(isNaN(sgSpan.innerHTML) || "" == sgSpan.innerHTML)
		{
			//alert('<p>Please enter a number in the Brix text box.</p>');
		}
		else
		{
			if('' != convBox.value)
			{
				var mt = convBox.value;
				var sg = sgSpan.innerHTML;
				var l = mt * sg / 1000;
				var usg = l / .264172052;
				spnKgL.innerHTML = l.toFixed(2) + "";
				spnKgUSG.innerHTML = usg.toFixed(2) + "";
			}
		}
	}
	else
	{
		alert('Invalid parameters!!!');
	}
}
function gotoNext(nextIndex, e)
{
	//console.log(e);
	if(e.keyCode == 13)
	{
		if(e.stopPropagation)
			e.stopPropagation();
		e.cancelBubble = true;
		if (e.preventDefault)
			e.preventDefault();
		e.returnValue = false;
		elements[nextIndex].focus();
		return false;
	}
}
var elements;
function converterLoad() {
	elements = new Array();
	elements[0] = document.getElementById('txtBrix1');
	elements[1] = document.getElementById('txtBrix2');
	elements[2] = document.getElementById('txtBrix3');
	elements[3] = document.getElementById('txtKg');
	elements[4] = document.getElementById('txtL');
	elements[5] = document.getElementById('txtMT');
}
attachEventListener(window, 'load', converterLoad);