$(document).ready(function() {

	$('#runBmiCalc').click(function() {
		weight = $('#bmiWeight').val();
		feet = $('#bmiHeightFeet').val();
		inches = $('#bmiHeightInches').val();
		
		if (bmiVerifyValues(weight, feet, inches)) {
			var bmi = bmiFormula(weight, feet, inches);
			if (bmi < 19.0) {
				alert("This BMI falls below the range for most people.  You may want to recheck your entries, or, for an individualized assessment, see your physician.");
			}
			$('#bmiCalcResult').val(bmi);
		}
		
	});

});

function bmiFormula(weight,feet,inches){
	weight = parseFloat(weight);
	feet = parseFloat(feet);
	inches = parseFloat(inches);
	width=weight*.45359237;
	height=feet*12;
	height=height-(-inches);
	height=height*.0254;
	height=height*height;
	var i = width/height;
	return i;
}

function bmiVerifyValues(weight, height_feet, height_inches) {	
	if (weight==null || weight.length==0 || height_feet==null || height_feet.length==0 || height_inches==null) {
		alert("You must enter a number.");
		return false;
	} else if (parseFloat(height_feet)<=0 || parseFloat(height_feet)>=9 || parseFloat(height_inches)<0 || parseFloat(height_inches)>12 || parseFloat(weight)<=0 || parseFloat(weight)>=801) {
		alert("Are you sure? Please check your values.");
		return false;
	}
	return true;
}