var dddlb_fromMetric;
var dddlb_toMetric;

window.onload = convert_onload;
function fromMetric_onchange()
{
	dddlb_fromMetric.onChange();
	document.frmConvert.Button1.disabled=false;
	document.getElementById('calcValue').innerHTML='####';
}

function toMetric_onchange()
{
	dddlb_toMetric.onChange();
	document.frmConvert.Button1.disabled=false;
	document.getElementById('calcValue').innerHTML='####';
}

function convert_onload()
{
	dddlb_fromMetric = new dddlb(document.frmConvert.fromMetric);
	dddlb_fromMetric.addOption("US Tons","eton");
	dddlb_fromMetric.addOption("Metric Tons","mton");
	dddlb_fromMetric.addOption("Pounds","lbs");
	dddlb_fromMetric.addOption("Kilograms","kg");
	dddlb_fromMetric.addOption("Fahrenheit","f");
	dddlb_fromMetric.addOption("Celsius","c");
	dddlb_fromMetric.addOption("Inches","in");
	dddlb_fromMetric.addOption("Millemeters","mm");
	
	dddlb_fromMetric.populate();
	document.frmConvert.fromMetric.onchange = fromMetric_onchange;
	
	dddlb_toMetric = new dddlb(document.frmConvert.toMetric);
	dddlb_toMetric.setParent(dddlb_fromMetric);
	dddlb_toMetric.addOption("eton","Metric Tons","mton");
	dddlb_toMetric.addOption("eton","Pounds","lbs");
	dddlb_toMetric.addOption("eton","Kilograms","kg");
	dddlb_toMetric.addOption("mton","US Tons","eton");
	dddlb_toMetric.addOption("mton","Pounds","lbs");
	dddlb_toMetric.addOption("mton","Kilograms","kg");
	dddlb_toMetric.addOption("lbs","Metric Tons","mton");
	dddlb_toMetric.addOption("lbs","US Tons","eton");
	dddlb_toMetric.addOption("lbs","Kilograms","kg");
	dddlb_toMetric.addOption("kg","Metric Tons","mton");
	dddlb_toMetric.addOption("kg","US Tons","eton");
	dddlb_toMetric.addOption("kg","Pounds","lbs");
	dddlb_toMetric.addOption("f","Celsius","c");
	dddlb_toMetric.addOption("c","Fahrenheit","f");
	dddlb_toMetric.addOption("in","Millemeters","mm");
	dddlb_toMetric.addOption("mm","Inches","in");
	
	dddlb_toMetric.populate();
	document.frmConvert.toMetric.onchange = toMetric_onchange;
}

function checkNum(str)
{
  for (var i=0; i<str.length; i++)
  {
    var ch = str.substring(i, i + 1)
    
    if (ch!="." && ch!="+" && ch!="-" && ch!="e" && ch!="E" && (ch < "0" || ch > "9"))
    {
      alert("Please enter a valid number.");
      return false;
    }
  }
  return true
}

function Calculate()
{
	var fromValue;
	var toValue = "&nbsp;";
	
	fromValue = document.frmConvert.fromValue.value;
	
	if (checkNum(fromValue))
	{
		switch(document.frmConvert.fromMetric.value)
		{
			case "eton" :
				switch(document.frmConvert.toMetric.value)
				{
					case "mton" :
						toValue = Math.round((fromValue * 0.907185)*100)/100;
						toValue += " Metric Tons";
						break;
					case "lbs" :
						toValue = Math.round((fromValue * 2000)*100)/100;
						toValue += " lbs.";
						break;
					case "kg" :
						toValue = Math.round((fromValue * 907.185)*100)/100;
						toValue += " kg";
						break;
				}
				break;
			case "mton" :
				switch(document.frmConvert.toMetric.value)
				{
					case "eton" :
						toValue = Math.round((fromValue * 1.10231)*100)/100;
						toValue += " US Tons";
						break;
					case "lbs" :
						toValue = Math.round((fromValue * 2204.62)*100)/100;
						toValue += " lbs.";
						break;
					case "kg" :
						toValue = Math.round((fromValue * 1000)*100)/100;
						toValue += " kg";
						break;
				}
				break;
			case "lbs" :
				switch(document.frmConvert.toMetric.value)
				{
					case "mton" :
						toValue = Math.round((fromValue * 0.000453592)*100)/100;
						toValue += " Metric Tons";
						break;
					case "eton" :
						toValue = Math.round((fromValue * 0.0005)*100)/100;
						toValue += " US Tons";
						break;
					case "kg" :
						toValue = Math.round((fromValue * 0.453592)*100)/100;
						toValue += " kg";
						break;
				}
				break;
			case "kg" :
				switch(document.frmConvert.toMetric.value)
				{
					case "mton" :
						toValue = Math.round((fromValue * 0.001)*100)/100;
						toValue += " Metric Tons";
						break;
					case "eton" :
						toValue = Math.round((fromValue * 0.001102)*100)/100;
						toValue += " US Tons";
						break;
					case "lbs" :
						toValue = Math.round((fromValue * 2.20462)*100)/100;
						toValue += " lbs.";
						break;
				}
				break;
			case "f" :
				switch(document.frmConvert.toMetric.value)
				{
					case "c" :
						toValue = Math.round(((5/9)*(fromValue - 32))*100)/100;
						toValue += " C";
						break;
				}
				break;
			case "c" :
				switch(document.frmConvert.toMetric.value)
				{
					case "f" :
						toValue = Math.round(((9/5)*(fromValue) + 32)*100)/100;
						toValue += " F";
						break;
				}
				break;
			case "in" :
				switch(document.frmConvert.toMetric.value)
				{
					case "mm" :
						toValue = Math.round((fromValue * 25.4)*100)/100;
						toValue += " mm";
						break;
				}
				break;
			case "mm" :
				switch(document.frmConvert.toMetric.value)
				{
					case "in" :
						toValue = Math.round((fromValue * 0.03937)*100)/100 ;
						toValue += " in.";
						break;
				}
				break;
		}
	}

	document.getElementById("calcValue").innerHTML = toValue;
}
