function parseIntegers(val) {
	var output = parseInt(val);
	if(!(output > 0)){
		output = '';
	}
	
	return output;
}
function calculateBMI(){
	var ids = new Array('bmi_1range', 'bmi_1a', 'bmi_1b', 'bmi_2range', 'bmi_2a', 'bmi_2b', 'bmi_3range', 'bmi_3a', 'bmi_3b', 'bmi_4range', 'bmi_4a', 'bmi_4b');
	var ids_ct = ids.length;
	for(i=0; i < ids_ct; i++){
		$(ids[i]).removeClassName('highlight')
	}
	
	var form = document.forms.bmi;

	var height = parseIntegers(form.height.value);
	var weight = parseIntegers(form.weight.value);
	
	var error = false;
	var emessage = "The Following errors were found in this form.\n";
	
	emessage += "Please correct them before trying to calculate again.\n";
	emessage += "-------------------------------------\n";
	if(weight=="") {
		emessage += "Weight - please enter your weight in pounds\n";
		error = true;
	}
	if(height=="") {
		emessage += "Height - please enter your height in inches\n";
		error = true;
	}
	if(error) {
		alert(emessage);
		return !error;
	}

	// Calculates, and rounds to 2 decimals
	var bmicalc = Math.round(( (weight/( height * height ))  * 703 ) * 100)/100;
	
	// Calculate Height in feet and inches
	var feetHeight =((height%12) <= 11) ? Math.floor(height/12) : Math.round(height/12);
	var houtputStr = feetHeight + "'" + (height%12) + '" ';
	document.getElementById('bmi_height').innerHTML=houtputStr;
	
	// Output message about BMI and height into table
	var outputStr = "At " + houtputStr + " tall and " + weight + "lbs your BMI is: <strong>" + bmicalc + "</strong>";
	document.getElementById('BMIoutput').innerHTML=outputStr;
	document.getElementById('bmi_height').innerHTML=houtputStr;
	
	// Calculate and output BMI:weight ranges
	document.getElementById('bmi_1range').innerHTML = calcTargetBMI(height, 18.4) + 'lbs or less';
	document.getElementById('bmi_2range').innerHTML = calcTargetBMI(height, 18.5) + 'lbs to ' + calcTargetBMI(height, 24.9) + 'lbs';
	document.getElementById('bmi_3range').innerHTML = calcTargetBMI(height, 25) + 'lbs to ' + calcTargetBMI(height, 29.9) + 'lbs';
	document.getElementById('bmi_4range').innerHTML = calcTargetBMI(height, 30.0) + 'lbs or more';
	
	// Highlight the correct row
	if(bmicalc < 18.5){
		$('bmi_1a').addClassName('highlight')
		$('bmi_1b').addClassName('highlight')
		$('bmi_1range').addClassName('highlight')
	} else if (bmicalc < 24.9){
		$('bmi_2a').addClassName('highlight')
		$('bmi_2b').addClassName('highlight')
		$('bmi_2range').addClassName('highlight')
	} else if (bmicalc < 29.9){
		$('bmi_3a').addClassName('highlight')
		$('bmi_3b').addClassName('highlight')
		$('bmi_3range').addClassName('highlight')
	} else {
		$('bmi_4a').addClassName('highlight')
		$('bmi_4b').addClassName('highlight')
		$('bmi_4range').addClassName('highlight')
	}
	
}

function calcTargetBMI(height, target){
	// Calculates target weights using the target BMI# and height
	var tWeight = Math.round((target/703) * (height * height));
	return tWeight;
}