//
// BMI calculator 
// author: mcn 1/7/2009
// copyright 2009, Page1Solutions, LLC
//

/*
The general formula for Body Mass Index is BMI (kg/m^2) = (weight_in_pounds * 703) / height_in_inches^2
For kilograms, the formula is  BMI (kg/m^2) = (weight_in_kg)/(height_in_meters)^2
http://www.bmi-calculator.net/bmi-formula.php
http://www.whathealth.com/bmi/formula.html

formula verified by: 
http://www.bmi-calculator.net/bmi-chart.php
*/
 // CONFIG
 
 var errtxt       = "Please correct the fields";  // general message	
 var feet_error   = 'Feet is not a number.';
 var inches_error = 'Inches is not a number.';
 var weight_error = 'Weight is not a number.';
 var meters_error = 'Height is not a number.';
 
 var lb_weight, kg_weight, i_height, m_height, feet, inches, bmi;
 
 var bmi_modes    = ['round', 'floor', 'ceiling', 'float_1', 'float_2'];  // starts at 0, not 1.
 var bmi_mode     = bmi_modes[4];
 
 var formulaTypes = ['Imperial', 'Metric'];
 var formulaType  = formulaTypes[0];		// Set as Imperial (feet/inches)
 
 // END CONFIG
 
 //
 // Needs variables set as global.
 //
 function calculate_bmi () {
 
	var errflag = false;
	 
	feet      = parseInt(jQuery("#feet").val()); 
 	inches    = parseInt(jQuery("#inches").val());
 	lb_weight = parseInt(jQuery('#lb_weight').val());
	
 	//kg_weight = parseInt(jQuery('#kg_weight').val());
 	//m_height  = parseInt(jQuery('#m_height').val()); 
	
 	var valid = {
	 	'feet'     : {'vtype':'number', 'wb_tag':'#feet', 'wb':0, 'value':feet, 'error': '#feet_error', 'msg': feet_error, 'error_class': 'err' }, 
	 	'inches'   : {'vtype':'number', 'wb_tag':'#inches', 'wb':0, 'value':inches, 'error': '#inches_error', 'msg': inches_error, 'error_class': 'err' },
	 	'lb_weight': {'vtype':'number', 'wb_tag':'#lb_weight', 'wb':0, 'value':lb_weight, 'error': '#weight_error', 'msg': weight_error, 'error_class': 'err' }
	};
	
	//
	// Validate fields
	//
	for (i in valid) {
		
		jQuery(valid[i].error).text("").removeClass(valid[i].error_class);    // reset error msgs
		jQuery(valid[i].wb_tag).removeClass(valid[i].error_class);
		
		//
		// check that it's not 0, bugfix: disregard inches
		//
		if ( valid[i].value == 0 && (i != 'inches') ) {
			
			errflag = true;
			
			//
			// write back a value, add error class
			//
			jQuery(valid[i].wb_tag).addClass(valid[i].error_class);
		}
		//
		// Check that it's a number and not a string
		//
		else if ( isNaN( valid[i].value ) ) {
			
			errflag = true;
			
			//
			// show per field error messages
			//
			jQuery(valid[i].error).text(valid[i].msg).addClass(valid[i].error_class);
			jQuery(valid[i].wb_tag).addClass(valid[i].error_class); 
		}
	} //for	
	
	//
	// On ANY error, return general error message
	//
	if (errflag) {
		return errtxt; 
	};
	
	//console.log("feet: " + feet);
	//console.log("inches : " + inches);
	//console.log("lb_weight: " + lb_weight );
	
	//
	// If the inches are > 12, add to feet and put the remainder as inches.
	//
	if (inches > 12) {
 		
		extra_feet = Math.floor(inches/12);
 		feet       = feet + extra_feet;
 		inches     = inches % 12;
		
		//
		// Update feet and inches
		//
		jQuery('#feet').val(feet);
		jQuery('#inches').val(inches);
 	}
 	
	i_height = (feet * 12) + inches;
	
	//console.log("i_height: " + i_height);
	
	if (formulaType == 'Imperial') { 
		var bmi = ( lb_weight  / (i_height * i_height) ) * 703;
	}
	else if (formulaType == 'Metric') { 
		var bmi = (kg_weight) / (m_height * m_height);
	}
	 
	 return bmi; 
} 
 
 //
 // Source: http://www.hhs.gov/
 // http://en.wikipedia.org/wiki/Obesity
 //
 function get_weight_status (bmi) {

	 var status;
	 
	 switch (true) {
	 	
		 case (bmi < 18.5) : 
			status = 'Underweight'; 
		  	break; 
		 case (bmi < 24.9) :
		 	status = 'Normal'; 
		 	break; 
		 case (bmi < 29.9) :
		 	status = 'Overweight'; 
		 	break;
		 case (bmi < 35) : 
		 	status = 'Class I Obesity';
		 	break;
		 case (bmi < 40) :
		 	status = 'Class II Obesity'; 
		 	break;
		 case (bmi < 50):
		 	status = 'Class III Obesity'; 
		 	break;
		 case (bmi >=50): 
		 	status = 'Super Obese';
		 	break;		
		 default: 
		 	status = 'error';
		 	break; 	
	 }
	 return status;
}

 function reformat_bmi (bmi, mode) {
 	
	switch (mode) {
		
		case "round"   : 
			bmi = Math.round(bmi);
			break; 
		case "floor"   : 
			bmi = Math.floor(bmi);
			break; 
		case "ceiling" :
			bmi = Math.ceil(bmi);
			break; 
		case "float_1" : 
			bmi = bmi.toFixed(1);
			break; 
		case "float_2" :
			bmi = bmi.toFixed(2);
			break; 
		default: 
			return bmi;
	}
	return bmi;
 }

 //
 // Display the BMI
 //
 function show_bmi () {
		
	var weight_text   = "";
	var bmi_text      = "";
	var weight_status = "";
	
	bmi = calculate_bmi();
	
	if ((typeof(bmi) == 'number') && (bmi > 0)) {
	
		weight_status = get_weight_status(bmi);
		weight_text   = "Weight Status: " + weight_status;
		bmi_text      = "BMI: " + reformat_bmi(bmi, bmi_mode);	
	}
	else {
		bmi_text = bmi;
	}
	
	jQuery('#bmi').text(bmi_text);
	jQuery('#weight_status').text(weight_text);
}

jQuery(function(){
	
	bmi = "";
	
	//
	// initial state
	//
	jQuery("#bmi").text(bmi); 
	jQuery("input").change(function(){show_bmi();});
	jQuery("#calculate").click(function(){show_bmi();});
});
