/*This code is copyright Andrew Wilkes
http://www.adscube.com
You may reuse it as long as you leave this credit in place.
*/
function runningCals(form)
{
distance = Number(form.distance.value);
weight = Number(form.weight.value);
milesOrKm = form.distanceType.selectedIndex;
poundOrKilos = form.weightType.selectedIndex;

// check for invalid values 
if ((weight < 20) || (weight > 750))
{
alert("Entered invalid weight!");
return;
}
if ((distance <= 0) || (distance > 500))
{
alert("Entered invalid distance!");
return;
}

if (poundOrKilos == 0)
{
//convert to kilos
weight = weight / 2.2;
}

if (milesOrKm == 0)
{
//convert to kilometers
distance = distance * 1.609;
}
// compute calories
calories = Math.round(weight * distance * 1.036);
// display results
alert("You would consume about " + calories + " calories.");
}

function calcBMI(form){
if (isNaN(form.height1.value)){
	alert("Please enter a number for your height!")
	return false}
if (isNaN(form.height2.value)){
	alert("Please enter a number for your height!")
	return false}
if (isNaN(form.weight.value)){
	alert("Please enter a number for your weight!")
	return false}

//calc height in meters
height = Number(form.height1.value);

if (form.hunit1.value == 1){ //units are feet
	height *= 0.3048;
}

if (form.hunit2.value == 1){ //inches
	height += form.height2.value * 0.0254;
} else {
	height += form.height2.value * 0.01;
}

// calc weight in kilos
weight = Number(form.weight.value);
if (form.wunit.value == 1){ //units are pounds
	weight *= 0.454;
}

// calc BMI
bmi = Math.round(weight / height / height);

upperidealkg = Math.round(25 * height * height);

upperideallb = Math.round(upperidealkg * 2.2046);



loweridealkg = Math.round(20 * height * height);

lowerideallb = Math.round(loweridealkg * 2.2046);

result = "Your Body Mass Index is " + bmi + "\n\n";

w = 0; //overweight

if (bmi > 49) {
	result += "You are super obese.";
} else if (bmi > 39) {
	result += "You are morbidly obese.";
} else if (bmi >29) {
	result += "You are obese.";
} else if (bmi > 25) {
	result += "You are overweight.";
} else if (bmi > 19) {
	result += "You have a healthy body weight.";
	w = 1;
} else {
	result += "You are underweight.";
	w = 2;
}
result += "\n\nYour ideal weight range is ";

if (form.wunit.value == 1) {

	result += lowerideallb + " - " + upperideallb + " pounds.\n\n";

	switch (w){
	case 0:
		x = form.weight.value - upperideallb;
		result += "You need to lose " + x + " pounds.";
		break;

	case 2:
		x = lowerideallb - form.weight.value;
		result += "You need to gain " + x + " pounds.";
		break;

	default:
		break;
	}

} else {
	result += loweridealkg + " - " + upperidealkg + " kilos.\n\n";

	switch (w){
	case 0:
		x = form.weight.value - upperidealkg;
		result += "You need to lose " + x + " kilos.";
		break;

	case 2:
		x = loweridealkg - form.weight.value;
		result += "You need to gain " + x + " kilos.";
		break;

	default:
		break;
	}
}
alert(result);
}