
function calcBMR() {
	
	var doc = document.bmr
	
	if ( doc.height1.value == "" || isNaN(doc.height1.value) || doc.height1.value <= 0)
	{
		alert("That is not a valid number for height.  Enter a number greater than 0, please."); 
		doc.height1.value = "";
		doc.height1.focus();
		return;
	}
	if (doc.height2.value == "" || isNaN(doc.height2.value) || doc.height2.value < 0)
	{
		alert("That is not a valid number for height.  Enter a number greater than 0, please."); 
		doc.height2.value = "";
		doc.height2.focus();
		return;
		
	}

	if (doc.weight.value == "" || isNaN(doc.weight.value) || doc.weight.value <= 0)
	{
		alert("That is not a valid number for weight.  Enter a number greater than 0, please."); 
		doc.weight.value = "";
		doc.weight.focus();
		return;
	}
	
	weight = doc.weight.value
	var uom = doc.weightuom.selectedIndex
	if ((uom == 0 && weight < 100) || (uom == 1 && weight < 44)){
	  alert("Weight is too low to calculate BMR.  Weight must be 100lbs (45kg) and over."); 
		doc.weight.value = "";
		doc.weight.focus();
		return;
	}
	
	if (doc.age.value == "" || isNaN(doc.age.value) || doc.age.value <= 0 )
	{
		alert("That is not a valid number for age.  Enter a number greater than 0, please"); 
		doc.age.value = "";
		doc.age.focus();
		return;
		
	}

	//weight = doc.weight.value
	
	//var uom = doc.weightuom.selectedIndex
	if (uom == 0) 
		{
			weight = convertlbstokg(weight)
		}
	
	height1uom = doc.height1uom.selectedIndex
	height2uom = doc.height2uom.selectedIndex
	
	if (height1uom == 0 && height2uom == 0)
	{
		feet = parseInt(doc.height1.value);
		inches = parseInt(doc.height2.value);
		htInCm = calculateHeight(feet, inches);
	}
	else if (height1uom == 1 && height2uom == 1)
	{
		metres = parseInt(doc.height1.value);
		cm = parseInt(doc.height2.value);
		convertedCm = convertMetresToCm(metres);
		htInCm = convertedCm + cm;
	}
	else {
		alert("The units of measure need to be the same.  Make both measure of units for your height either feet and inches OR metres and centimetres.");
		return;
	}

	age = doc.age.value;
	if (doc.gender[0].checked == true)
		{
			bmrate = calculateBMRF(weight, htInCm, age);
		}
	else if (doc.gender[1].checked == true)
	{
		bmrate = calculateBMRM(weight, htInCm, age);
		
	}
		
		doc.result.value=Math.round(bmrate);

}

function calculateBMRF(weight, htInCm, age) {
	return (9.5634 * weight) + (1.8496 * htInCm) - (4.6756 * age) + 655.0955	
}

function calculateBMRM(weight, htInCm, age) {
	return (13.7516 * weight) + (5.0033 * htInCm) - (6.7550 * age) + 66.4730
}

function convertlbstokg(pounds) {
	weight = pounds * .45359237
	return weight
}
function convertMetresToCm (metres) {
	return metres * 100
}

function convertFeetToInches(feet) {
	inches = feet * 12;
	return inches
}

function convertInchesToCm (totalInches) {
	totalInches = totalInches * 2.54
	return totalInches
	}

function calculateHeight(feet, inches) {
	
		convertedInches = convertFeetToInches(feet);
		totalInches = convertedInches + inches;
		return convertInchesToCm(totalInches);
}
	
function clearForm() {
	document.forms['bmr'].reset();
}



function mod(div,base) {
return Math.round(div - (Math.floor(div/base)*base));
}
function calcBMI() {
var w = document.bmi.weight.value * 1;
var HeightFeetInt = document.bmi.htf.value * 1;
var HeightInchesInt = document.bmi.hti.value * 1;
HeightFeetConvert = HeightFeetInt * 12;
h = HeightFeetConvert + HeightInchesInt;
displaybmi = (Math.round((w * 703) / (h * h)));
var rvalue = true;
if ( (w <= 35) || (w >= 500)  || (h <= 48) || (h >= 120) ) {
alert ("The figures you just entered doesn't look right. Can you please check again?");
rvalue = false;
}
if (rvalue) {
if (HeightInchesInt > 11) {
reminderinches = mod(HeightInchesInt,12);
document.bmi.hti.value = reminderinches;
document.bmi.htf.value = HeightFeetInt + 
((HeightInchesInt - reminderinches)/12);
document.bmi.answer.value = displaybmi;
}

document.bmi.answer.value = displaybmi; }
return rvalue;
}





