// dem 05/24/04

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 bmi = ((w * 703) / (h * h));
	floor_bmi = Math.floor(bmi);
	diff  = Math.round( (bmi - floor_bmi) * 10 );
	if (diff == 10){
		// Need to bump up the whole thing instead
		floor_bmi += 1;
		diff = 0;
	}
	displaybmi = floor_bmi + "." + diff;

	var rvalue = true;

	if ( (w <= 35) || (w >= 500)  || (h <= 48) || (h >= 120) ) {
		alert ("Please enter your weight!");
		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;
		}
		if (displaybmi < 18.5)  {
			document.bmi.comment.value = "Underweight";
		}
		if (displaybmi >= 18.5 && displaybmi < 25) {
			document.bmi.comment.value = "Desirable";
		}
		if (displaybmi >= 25 && displaybmi < 30) {
			document.bmi.comment.value = "Overweight";
		}
		if (displaybmi >= 30) {
			document.bmi.comment.value = "Severe overweight";
		}
		document.bmi.answer.value = displaybmi;

		if (document.bmi.sex[0].checked == "1") {
			display_ibw = 106 + (6 * (h - 60));
		}
		if (document.bmi.sex[1].checked == "1") {
			display_ibw = 100 + (5* (h - 60));
		}
		document.bmi.ibw.value = display_ibw;

		display_ibw_percent = Math.round( (w / display_ibw) * 100 );
		document.bmi.ibw_percent.value = display_ibw_percent;

	 }

	return rvalue;
}

