
// This function accepts sex inputs via radio buttons on this form, and five numeric inputs from the text boxes there. The inputs are used to calculate the daily caloric needs of someone meeting those parameters. 
function Calc() {
var height_feet = document.BMR.Feet_Height.value;
var height_inches = document.BMR.Inches_Height.value;
var weight_lbs = document.BMR.WeightLbs.value;
var years_of_age = document.BMR.Years_Age.value;
var lbs_loss = document.BMR.LbsLoss.value;
var activity_level = document.BMR.ActivityLevel.value;

if (document.BMR.Calories.value != ""){
	reset();
	}

if((!(CheckValues(weight_lbs))) || (!(CheckValues(years_of_age)))) {
	alert("Please complete all required fields.");
} else {
	var total_inches = parseInt(height_feet * 12) + parseInt(height_inches);
  var pounds_loss = lbs_loss * 500
	if(document.BMR.sex[0].checked) {  // Male calculation values
       var adjust_weight = 66 + (6.23 * weight_lbs);
		var adjust_height = 12.7 * total_inches;
		var adjust_age = 6.8 * years_of_age;
	} else {  // Female calculation values
		var adjust_weight = 655 + (4.35 * weight_lbs);
		var adjust_height = 4.7 * total_inches;
		var adjust_age = 4.7 * years_of_age;
		}
	document.BMR.Calories.value = Math.ceil((adjust_weight + adjust_height - adjust_age) * activity_level - pounds_loss);
	}
}

// This function accepts a string value and determines if it is a zero-length or null string, then checks for positive integer values. It returns a value of true or false. 
function CheckValues(this_string) {
	var is_number = true;
	var string_length = this_string.length;
	if(string_length == 0) {
		is_number = false;
	} else {
		for(count = 0; count < string_length; count++) {
			if((this_string.charAt(count) < "0") || (this_string.charAt(count) > "9")) {
				is_number = false;
				break;
			}	
		}
	}
	return is_number;
}

//  Clear and Restart  
function reset() {
	location.reload()
}
