// Functions used for imperial / metric conversions

// in the BMI calculator



function poundsToKilos(pounds)

{

	var kilos = pounds / 2.2046;

	return Math.round(kilos);		

}



function kilosToPounds(kilos)

{

	var pounds = kilos * 2.2046;

	return Math.round(pounds);

}



function to2DecPlaces(value)

{

	value = value * 100;

	value = Math.round(value);

	value = value / 100;

	return Math.round(value);

}



function convertKilos()

{

	var kilos = document.getElementById('ctl00_Content_kgs').value -0;

	var totalPounds = kilosToPounds(kilos);

	var pounds = totalPounds % 14;

	var stones = (totalPounds - pounds) / 14;

	

	// round the pounds because we don't seem to be dealing with ounces

	pounds = Math.round(pounds);

	

	document.getElementById('ctl00_Content_stones').value = Math.round(stones);

	document.getElementById('ctl00_Content_pounds').value = pounds;	

}



function convertPounds()

{

	var stones = document.getElementById('ctl00_Content_stones').value -0;

	var pounds = document.getElementById('ctl00_Content_pounds').value -0;

	var totalPounds = (stones * 14) + pounds;

	

	var kilos = poundsToKilos(totalPounds);	

	kilos = to2DecPlaces(kilos);

	document.getElementById('ctl00_Content_kgs').value = Math.round(kilos);

}



function inchesToCM(inches)

{

	var CM = inches * 2.54;

	return Math.round(CM);

}



function CMToInches(CM)

{

	var inches = CM / 2.54;

	return Math.round(inches);

}



function convertMetres()

{

	var CM = (document.getElementById('ctl00_Content_metres').value -0) * 100;

	var totalInches = Math.round(CMToInches(CM));

	var inches = totalInches % 12;

	var feet = (totalInches - inches) / 12;

	

	document.getElementById('ctl00_Content_feet').value = Math.round(feet);

	document.getElementById('ctl00_Content_inches').value = Math.round(inches);

}



function convertInches()

{

	var inches = document.getElementById('ctl00_Content_inches').value-0;

	var feet = document.getElementById('ctl00_Content_feet').value-0;

	var totalInches = (feet * 12) + inches;

	

	document.getElementById('ctl00_Content_metres').value = Math.round(to2DecPlaces(inchesToCM(totalInches) / 100));

}

function custRound(x,places) {
  return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}

function validateInput(n)

{

	var CM = (document.getElementById('ctl00_Content_metres').value-0) * 100;

	var kilos = document.getElementById('ctl00_Content_kgs').value-0;

	if ((!isNaN(CM)) && (!isNaN(kilos)) && (CM > 0) && (kilos > 0)) {
if(n){

	var height2 = CM / 100;
  var BMI = kilos  / (height2 * height2);
  var res=custRound(BMI,1);
  //if(res>300-10){res=300-10;}if(res<-10){res=-10;}
	
	var m="";
	if(res<18){m= "Results from the BMI calculator show that you have a low estimated BMI, suggesting that you are below optimum weight for your height.";}
if(res>=18 && res<=25){m= "Results from the body analyser show that you have an estimated BMI between 18 and 25, making you an optimum weight for your height.";}
if(res>25){m= "Results from the BMI calculator show that you have an estimated BMI greater than 25(overweight)";}

	alert("Your BMI is: "+res+"\n"+m);
	
	return false;
	
	}else{
		return true;
}
		

	} 

	return false;

}