/*
© Dr Ian Sidwell, Somerset Obesity Service, 2003
Modified and updated for cross browser use by Richard Wilson 2005
*/

var theForm = document.forms['aspnetForm'];
if (!theForm) {
	theForm = document.aspnetForm;
}

function changestlb (obj)
{
	with(theForm)
	{
		ctl00$cphContent$tbxWeightKg.value=Math.round((Number(ctl00$cphContent$tbxWeightSt.value * 14) + Number(ctl00$cphContent$tbxWeightLb.value)) / 2.2);
	}
}

function changekg (obj)
{
	with(theForm)
	{
		lbs = Math.round(ctl00$cphContent$tbxWeightKg.value * 2.2);
		ctl00$cphContent$tbxWeightSt.value = Math.floor(lbs / 14);
		ctl00$cphContent$tbxWeightLb.value = Math.round(lbs % 14);
	}
}

function kgtostlb(kgs)
{
	return Math.floor((kgs * 2.2) / 14) + "st " + Math.round((kgs * 2.2) % 14) + "lbs";
}



function changeftin (obj)
{
	with(theForm)
	{
		ctl00$cphContent$tbxHeightM.value=Math.round((Number(ctl00$cphContent$tbxHeightFt.value * 12) + Number(ctl00$cphContent$tbxHeightIn.value)) * 2.54)  /100;
	}
}
function changem (obj)
{
	with(theForm)
	{
		vinch = ctl00$cphContent$tbxHeightM.value/0.0254
		ctl00$cphContent$tbxHeightFt.value = Math.floor(vinch / 12)
		ctl00$cphContent$tbxHeightIn.value = Math.round(vinch % 12)
	}
}

function WhatsBMI ()
{
	with(theForm)
	{
		BMIVal = Math.round(ctl00$cphContent$tbxWeightKg.value / (ctl00$cphContent$tbxHeightM.value * ctl00$cphContent$tbxHeightM.value) * 10) /10;
		bestkg = (Math.round(ctl00$cphContent$tbxHeightM.value * ctl00$cphContent$tbxHeightM.value * 250))/10
		bestlbs= bestkg * 2.2;
		currentWeight = (Math.round(ctl00$cphContent$tbxWeightKg.value*10))/10;
		currentHeight = ctl00$cphContent$tbxHeightM.value;
		currentExcess = Math.round((currentWeight - bestkg)*10)/10;
		//50% excess lost
		fiftyWeight = Math.round((currentWeight - (0.5 * currentExcess))*10)/10;
		seventyWeight = Math.round((currentWeight - (0.7 * currentExcess))*10)/10;
		
		fiftyBMI = Math.round(fiftyWeight / (ctl00$cphContent$tbxHeightM.value * ctl00$cphContent$tbxHeightM.value) * 10) /10; 
		seventyBMI = Math.round(seventyWeight / (ctl00$cphContent$tbxHeightM.value * ctl00$cphContent$tbxHeightM.value) * 10) /10; 
		
		
	}
	
		
	
	strResults = "<p>For your height, the upper limit of your ideal weight range is:<br><span class=\"BMIResults\">" + bestkg + "kg (" + kgtostlb(bestkg) + "), BMI = 25 kg/m<sup>2</sup></span></p>";
	if(BMIVal > 25)
	{
		strResults += "<p>You are currently carrying <span class=\"BMIResults\">" + currentExcess + "kg (" + kgtostlb(currentExcess) + ")</span> of excess weight.</p>";
		
		strResults += "<p>Losing 50% of your excess weight will have the following effect: <br><span class=\"BMIResults\">" + fiftyWeight + "kg (" + kgtostlb(fiftyWeight) + "), BMI = " + fiftyBMI + " kg/m<sup>2</sup></span></p>";
		
		strResults += "<p>Losing 70% of your excess weight will have the following effect: <br><span class=\"BMIResults\">" + seventyWeight + "kg (" + kgtostlb(seventyWeight) + "), BMI = " + seventyBMI + " kg/m<sup>2</sup></span></p>";
	}
	else if(BMIVal <=25 && BMIVal >18)
	{
		strResults = "<p>You are in the normal range</p>";
	}
	else if(BMIVal <18)
	{
		strResults = "<p>You are underweight.</p>";
	}
	//Display results
	document.getElementById('TheBMI').innerHTML = BMIVal;
	document.getElementById('Results').innerHTML = strResults;

	//reset colours in table
	for (i=1; i<7; i++) 
	{
		eval("document.getElementById('I" + i + "').bgColor='#FFFFFF'");
		eval("document.getElementById('I" + i + "').style.color='#000000'");
	}
	
	//set new colours
	if (BMIVal < 18)
	{
		 document.getElementById('I1').bgColor='#96C2D6';
		 document.getElementById('I1').style.color='#ffffff';
	}
	if (BMIVal >= 18 && BMIVal <25) 
	{
		document.getElementById('I2').bgColor='#5BA2BF';
		document.getElementById('I2').style.color='#ffffff';
	}
	if (BMIVal >= 25 && BMIVal <30) 
	{
		document.getElementById('I3').bgColor='#5A95B4';
		document.getElementById('I3').style.color='#ffffff';
	}
	if (BMIVal >= 30 && BMIVal <35)
	{
		document.getElementById('I4').bgColor='#31799B';
		document.getElementById('I4').style.color='#ffffff';
	}
	if (BMIVal >= 35 && BMIVal <40)
	{
		document.getElementById('I5').bgColor='#3F648D';
		document.getElementById('I5').style.color='#ffffff';
	}
	if (BMIVal >= 40)
	{
		document.getElementById('I6').bgColor='#53597C';
		document.getElementById('I6').style.color='#ffffff'; 
	}
}
