//OUN so that new windows are opened each time
var bmicount = 0;

//Convert to metres
function getMetricHeight(doc)
{
	var h1 = doc.getElementById("height1").value;
	var h2 = doc.getElementById("height2").value;

	if (doc.getElementById("height3").selectedIndex == 1)
	{
		h1 = h1 * 0.3048; //feet to metres
		h2 = h2 * 2.54;   //inches to cms
	}
	return (h1 / 1) + (h2 / 100);
}

//Convert lbs to kgs if necessary
function getMetricWeight(doc)
{
	var w = doc.getElementById("weight1").value;

	if (doc.getElementById("weight2").selectedIndex == 1)
	{
		w = w / 2.2;
	}
	return w;
}

//Work out what sort of BMI value this is
function getDescription(r)
{
	if (r < 20)
		return "This falls within the 'low' BMI category (under 20)";
	if (r <= 25)
		return "This falls within the 'desirable' BMI category (20-25)";
	if (r <= 30)
		return "This falls within the 'overweight' category (25-30)";
	return "This falls within the 'obese' category (over 30)";
}

//Calculate BMI and dynamically update page using 'innerHTML'
function calc(doc)
{
	var height = getMetricHeight(doc);
	var weight = getMetricWeight(doc);
	var result = (weight / 1) / (height * height);
	var description = getDescription(result);
	result = result.toString().substring(0,4);
	var resulttag = doc.getElementById("result");
	var desctag = doc.getElementById("describe_result");
	if ((height > 0) && (weight > 0))
	{
		resulttag.innerHTML = "Your BMI is <B>" + result + "</B>";
		desctag.innerHTML = description;
	}
	return false;
}

//Called from HTML to open new calculator window
function launchcalc()
{
	var win = window.open("bmicalc.html","bmi"+bmicount++,"resizable=no,width=320,height=240");
	var doc = win.document;
}
