
function ValidChars(sText,ValidChars) {
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1)  {
			IsNumber = false;
		}
	}
	return IsNumber;
}

function checkform (form) {

	if (form.age.value == "") {
		alert( "Please enter your age" );
		chd.age.focus();
		return false;
	}
	if (!ValidChars(form.age.value,"0123456789")) {
		alert( "Please enter only numbers for your age" );
		form.age.focus();
		return false;
	}
	
	
	if (form.weight.value == "") {
		alert( "Please enter your weight" );
		form.weight.focus();
		return false;
	}
	if (!ValidChars(form.weight.value,"0123456789")) {
		alert( "Please enter only numbers for your weight" );
		form.weight.focus();
		return false;
	}
	
}

function calculateCalories(form) {
	
	if(checkform(form) != false) {
		var w = 0;
		if(form.wu.value == 'lbs')
			w = form.weight.value * 0.45359237;
		else
			w = form.weight.value;
		var bmr = 0;
		if(form.sex[0].checked) {
			if (form.age.value >= 0 && form.age.value <= 17) {
				bmr = 17.7 * w + 657;
			} else if (form.age.value > 17 && form.age.value <= 29) {
				bmr = 15.1 * w  + 692;
			} else if (form.age.value > 29 && form.age.value <= 99) {
				bmr = 11.5 * w + 873;
			}
		} else {
			if (form.age.value >= 0 && form.age.value <= 17) {
				bmr = 13.4 * w + 692;
			} else if (form.age.value > 17 && form.age.value <= 29) {
				bmr = 14.8 * w  + 487;
			} else if (form.age.value > 29 && form.age.value <= 99) {
				bmr = 8.3 * w + 846;
			}
		}
		bmr = Math.round(bmr);
		form.calorie.value = bmr;
		document.getElementById('expl').innerHTML = "This means that you need roughly "+bmr+" calories a day to maintain your current weight";
	}
}
