function calculateBodyFat(thisform) {

	form = thisform;
	weight = form.Weight.value;
	waist = form.Waist.value;
	wrist = form.Wrist.value;
	hips = form.Hips.value;
	forearm = form.Forearm.value;
	isFemale = 0;
	bodyfat = 0;
	
	if(form.Gender[1].checked) {
		isFemale = 1;
	}

	formInvalid = 0;

	field1 = '';
	field2 = '';
	field3 = '';
	field4 = '';
	field5 = '';

	if(!(checkValue(weight))) {
		field1 = 'weight in pounds, ';
		formInvalid = 1;
	}

	if(!(checkValue(waist))) {
		field2 = 'waist size, ';
		formInvalid = 1;
	}

	if(!(checkValue(wrist)) && isFemale) {
		field3 = 'wrist circumference, ';
		formInvalid = 1;
	}

	if(!(checkValue(hips)) && isFemale) {
		field4 = 'hips circumference, ';
		formInvalid = 1;
	}

	if(!(checkValue(forearm)) && isFemale) {
		field5 = 'forearm circumference, ';
		formInvalid = 1;
	}


	if(formInvalid) {
	
		message = 'Please enter the following required information: ' + field1 + field2 + field3 + field4 + field5;

		message = message.substring(0, (message.length - 2)) + '.';
	
		alert(message);
	
	} else {

		if(isFemale) {
		
			bodyfat = ( (weight - ( ( ( ((weight * 0.732) + 8.987) + (wrist / 3.14) ) - (waist * 0.157) ) - (hips * 0.249) ) + (forearm * 0.434) ) * 100) / weight;
		
		} else {
		
 	 		bodyfat = ((weight - (((weight * 1.082) + 94.42) - (waist * 4.15))) * 100) / weight;
		}
		
		if(!(isFemale)) {
		
			form.Wrist.value = '';
			form.Hips.value = '';
			form.Forearm.value = '';
		
		}
		
		form.BodyFat.value = Math.floor(bodyfat);
	 
	}	
}





// BMI calculator part

function checkNum(thisChar) {

	if((thisChar != '0') &&
		(thisChar != '1') &&
		(thisChar != '2') &&
		(thisChar != '3') &&
		(thisChar != '4') &&
		(thisChar != '5') &&
		(thisChar != '6') &&
		(thisChar != '7') &&
		(thisChar != '8') &&
		(thisChar != '9')) {
		
		return false;
		
	} else {
	
		return true;
		
	}
}
		

function checkValue(thisString) {

	var isNum = true;
	var stringLen = thisString.length;
	
	if(stringLen == 0) {
	
		isNum = false;
		
	} else {
	
		for(count = 0; count < stringLen; count++) {
	
			if(!(checkNum(thisString.charAt(count)))) {
		
				isNum = false;
				break;
		
			}	
		}
	}
	
	return isNum;
	
}


function FigureBMI(thisform) {

form = thisform;
feetIndex = form.Feet.selectedIndex;
feet = form.Feet.options[feetIndex].value;
inchesIndex = form.Inches.selectedIndex;
inches = form.Inches.options[inchesIndex].value;
pounds = form.Weight.value;

if(!(checkValue(pounds))) {
	
	alert('Please enter a number for your weight.');
	
} else {

 	 TotalInches = eval(feet*12) + eval(inches)
	 Meters = TotalInches/39.36
	 Kilos = pounds/2.2
	 Square = Meters * Meters
	 
 	 form.BMI.value = Math.ceil(Kilos/Square);
	 
}
	
}
// -------------------------------------------------------

function checkNum(thisChar) {

	if((thisChar != '0') &&
		(thisChar != '1') &&
		(thisChar != '2') &&
		(thisChar != '3') &&
		(thisChar != '4') &&
		(thisChar != '5') &&
		(thisChar != '6') &&
		(thisChar != '7') &&
		(thisChar != '8') &&
		(thisChar != '9')) {
		
		return false;
		
	} else {
	
		return true;
		
	}
}


function Figurecalneeds(thisform) {

form = thisform;
feetIndex = form.Feet.selectedIndex;
feet = form.Feet.options[feetIndex].value;
inchesIndex = form.Inches.selectedIndex;
inches = form.Inches.options[inchesIndex].value;
pounds = form.Weight.value;
years = form.Age.value;
activityIndex = form.activity.selectedIndex;
activity = form.activity.options[activityIndex].value;

if((!(checkValue(pounds))) ||
	(!(checkValue(years)))) {
	
	alert('Please enter numbers for weight and age.');
	
} else {

TotalInches = eval(feet*12) + eval(inches);
Centis      = TotalInches * 2.54;
Kilos       = pounds/2.2;
Age         = years;

	if(form.Gender[0].checked) {

	<!--Male calculations-->
	Weight      = 66 + (13.7 * Kilos)
	Height      = 5 * Centis
	Ages        = 6.8 * Age

	} else {

	<!--Female calculations-->
	Weight      = 655 + (9.6 * Kilos)
	Height      = 1.7 * Centis
	Ages        = 4.7 * Age

	}

form.Calories.value = Math.ceil((Weight + Height - Ages) * activity)

}

}
 

function openNutrition() {

	caloricNeeds = document.mod_calc.Calories.value;
	URL = 'nutritionalneeds_calc.cfm?calories=' + caloricNeeds;
	popup=window.open(URL, '', 'width=456,height=293');

}

// -----------------------------------------------

function calcIdealWeight(thisform) {

form = thisform;
feetIndex = form.Feet.selectedIndex;
feet = form.Feet.options[feetIndex].value;
inchesIndex = form.Inches.selectedIndex;
inches = form.Inches.options[inchesIndex].value;

TotalInches = eval(feet*12) + eval(inches);
idealWeight = 0;

if(form.Gender[0].checked){

	<!-- Male calculations -->

	if(TotalInches==60){
		idealWeight=106;
	}

	if((TotalInches>60) & (TotalInches < 72)){
		idealWeight=106+(6*inches);
	}
	
	if(TotalInches==72){
		idealWeight=178;
	}

	if(TotalInches>72){
		idealWeight=178+(6*inches);
	}
	
} else {

	<!-- Female calculations -->
	
	if(TotalInches==60){
		idealWeight=100;
	}

	if((TotalInches>60) & (TotalInches < 72)){
		idealWeight=100+(5*inches);
	}
	
	if(TotalInches==72){
		idealWeight=160;
	}
	
	if(TotalInches>72){
		idealWeight=160+(5*inches);
	}
}

	form.Weight.value = idealWeight;

}
//-------------------------------------------------------

function calcNutrition (thisform) {

var form = thisform;
var totcal = form.TotalCalories.value;

if(!(checkValue(totcal))) {

	alert('Please enter a number for your daily caloric needs.');
	
} else {

var carbs = (parseFloat(totcal) * .40);
var cgrams = Math.ceil(parseFloat(carbs) / 4); 
var prots = (parseFloat(totcal) * .30);
var pgrams = Math.ceil(parseFloat(prots) / 4); 
var fatts = (parseFloat(totcal) * .30);
var fgrams = Math.ceil(parseFloat(fatts) / 9); 

form.Carbohydrates.value = Math.ceil(parseFloat(carbs));
form.gramsCarbs.value = parseFloat(cgrams); 
form.Proteins.value = Math.ceil(parseFloat(prots));
form.gramsProts.value = parseFloat(pgrams);
form.Fats.value = Math.ceil(parseFloat(fatts));
form.gramsFats.value = fgrams;

}

}
//------------------------------------------

function calculate(thisform){

	form = thisform;
	age = form.Age.value;
	
	if(!(checkValue(age))) {
	
		alert('Please enter a number for your age.');
		
	} else {
	
	intensityIndex = form.Intensity.selectedIndex;
	intensity = form.Intensity.options[intensityIndex].value;
	percentIntensity = (intensity / 100)
	
	lowRate = parseInt((220 - age) * .55);
	highRate = parseInt((220 - age) * .85);

	targetRate = 220;
	targetRate -= age;
	targetRate *= percentIntensity;
	targetRate = parseInt(targetRate);
	
	form.LowRate.value = lowRate;
	form.HighRate.value = highRate;
	form.TargetRate.value = targetRate;
	
	}
}
