// BMI Calculator using jquery
// Created by Sean Gates on Feb. 20, 2009
// Usage: ochsner.org/programs/bariatric_surgery_bmi/

$(document).ready(function(){
	$('#bmi-error, #calculated-bmi').hide();
	$('#bmi-calculator form').submit(function(){
		$('#bmi-error, #calculated-bmi').slideUp('fast');
		
		var height = $('#bmi-height').val();
		var weight = $('#bmi-weight').val();
		
		if(weight == ''){
			$('#bmi-error').text('Please enter a weight.').slideDown('fast');
		}
		else if(height == ''){
			$('#bmi-error').text('Please enter a height.').slideDown('fast');
		}
		else{
			var bmi = new Number( ( weight / ( height * height ) ) * 703 );
			var calculated_bmi = bmi.toFixed(2);
			var bmi_display = '';
			if(calculated_bmi < 18.5){
				// underweight
				bmi_display = '<span style="color: yellow;">'+calculated_bmi+' (underweight)</span>';
			}
			else if(calculated_bmi <= 24.9){
				// normal
				bmi_display = '<span style="color: green;">'+calculated_bmi+' (normal)</span>';
			}
			else if(calculated_bmi <= 29.9){
				// overweight
				bmi_display = '<span style="color: orange;">'+calculated_bmi+' (overweight)</span>';
			}
			else{
				// obese
				bmi_display = '<span style="color: red;">'+calculated_bmi+' (obese)</span>';
			}
			
			$('#calculated-bmi').html('Your calculated BMI: ' + bmi_display ).slideDown('fast');
		}
										
		return false;
	});
});
