cmToIn = 0.3937
inToCm = 2.54
kgToLb = 2.24
function fatCalc(){
	isMale =(document.selectForm.gender[0].checked);
	//isCm = document.selectForm.heightUnit[1].checked
	//isKg = document.selectForm.WtUnit[1].checked
	isCm = false;
	isKg = false;
	weight = parseFloat(document.selectForm.weight.value);
	if (isKg) {
		mult = kgToLb;
		massUnit= " Kgs.";
	} else {
		mult = 1;
		massUnit = " lbs.";
	}
	lower = 75 / mult;
	upper = 500 / mult;
	isGood = validateNum(weight,lower,upper,massUnit,"Weight.");
	if (!isGood) {
		document.selectForm.weight.focus();
		return false;
	}
	weight= weight * mult;
	// calculate various lengths
	if (isCm) {
		mult = cmToIn;
		mult2 = 1;
		distUnit = " cm.";
	} else {
		mult = 1;
		mult2 = inToCm;
		distUnit = " in.";
	}
	
	height = eval(eval(document.selectForm.heightFeet.value) * 12) + eval(document.selectForm.heightInches.value);
	document.selectForm.height.value = height;
	lower = 44 / mult;
	upper = 84 / mult;

	abd1 = parseFloat(document.selectForm.abd1.value);
	lower = 20 / mult;
	upper = 52/ mult;

	abd2 = parseFloat(document.selectForm.abd2.value);
	lower = 20 / mult;
	upper = 52 / mult;

	neck = parseFloat(document.selectForm.neck.value);
	lower = 10 / mult;
	upper = 22 / mult;

	heightIn = height * mult;
	heightCm = height * mult2;
	abd1In = abd1 * mult;
	abd1Cm = abd1 * mult2;
	abd2In = abd2 * mult;
	abd2Cm = abd2 * mult2;
	neckIn = neck * mult;
	neckCm = neck * mult2;
	
	if (isMale) {
		fat1 = 495/(1.0324-.19077*((Math.log(abd2Cm - neckCm))/Math.LN10) +0.15456 * ((Math.log(heightCm))/Math.LN10)) - 450;
		fat2 = 100 * (-98.42 + 4.15*abd1In - 0.082 * weight)/weight;
		avgFat = (fat1 + fat2)/2;
	} else {
		fat1 = 495/(1.29579-.35004*((Math.log(abd1Cm + abd2Cm - neckCm))/Math.LN10) +0.221 * ((Math.log(heightCm))/Math.LN10)) - 450;
		fat2 = 100*(-76.76 + 4.15*abd1In - 0.082 * weight)/weight;
		avgFat = (fat1 + fat2)/2;
	}
	return Math.round(avgFat*10)/10;	
}
function validateNum(num, lowerLim, upperLim, unit, label){
	if(isNaN(num)){
		alert("Please enter a value between "+ Math.round(lowerLim) + " and " + Math.round(upperLim) + unit + " for " + label);
		return false;
	} else if ((num<lowerLim)||(num>upperLim)){
		alert("Please enter a value between "+ Math.round(lowerLim) + " and " + Math.round(upperLim) + unit + " for " + label);
		return false;
	} else {
		return true;
	}
}
function GetAnalysis(mode) {
	if ((document.selectForm.loseGainType[1].checked && !validateDate(document.selectForm.weightDate, "Weight Goal Date"))) { 
		return; 
	} else if (document.selectForm.weight.value == "0" || document.selectForm.weight.value.length == 0) {
		alert("Please enter a valid weight.");
		document.selectForm.weight.focus();
		return;
	} else if (document.selectForm.targetWeight.value == "0" || document.selectForm.targetWeight.value.length == 0) {
		alert("Please enter a valid target weight.");
		document.selectForm.targetWeight.focus();
		return;
	} else if (document.selectForm.weight.value != document.selectForm.targetWeight.value) {
		if (document.selectForm.loseGainType[0].checked && eval(document.selectForm.loseGainRate.value) <= 0) {
			if (document.selectForm.weight.value > document.selectForm.targetWeight.value) {
				alert("Please enter a valid pound loss rate per week.");
				document.selectForm.loseGainRate.focus();
				return;
			} else {
				alert("Please enter a valid pound gain rate per week.");
				document.selectForm.targetWeight.focus();
				return;
			}
		} else if (document.selectForm.loseGainType[0].checked && eval(document.selectForm.loseGainRate.value) > 2) {
			var lossGainText = "gain";
			if (document.selectForm.weight.value > document.selectForm.targetWeight.value) {
				lossGainText = "loss";
			}
			if (!confirm("It is not recommended to have a weight " + lossGainText + " of more than 2 pounds per week. If you choose to continue, please consult your doctor or a qualified wellness professional before you alter your diet or fitness activities. Do you wish to continue?")) {
				document.selectForm.loseGainRate.value="2";
				return;
			}
		} else if (document.selectForm.loseGainType[1].checked && document.selectForm.weightDate.value.length == 0) {
			alert("Please enter a valid date to reach your target weight by.");
			document.selectForm.weightDate.focus();
			return;
		} else if (document.selectForm.loseGainType[1].checked && new Date() > new Date(document.selectForm.weightDate.value)) {
			alert("Please select a target date which is in the future.");
			document.selectForm.weightDate.focus();
			return;
		}
	}
	document.selectForm.bodyFat.value = fatCalc();
	if (!eval(document.selectForm.bodyFat.value)) { return; }

	//weight information
	var rate;
	var targetWeightDate;
	var weightGoal;
	if (document.selectForm.weight.value == document.selectForm.targetWeight.value) { //maintain weight
		weightGoal = "maintainWeight";
		rate = 0;
		var today = new Date();
		targetWeightDate = eval(today.getMonth()+1)+"/"+today.getDate()+"/"+today.getFullYear();
	} else if (document.selectForm.weight.value > document.selectForm.targetWeight.value) { //lose weight
		weightGoal = "loseWeight";
		if (document.selectForm.loseGainType[0].checked) {
			rate = document.selectForm.loseGainRate.value;
			targetWeightDate = addWeeks(new Date(), eval(eval(document.selectForm.weight.value - document.selectForm.targetWeight.value) / rate));
			targetWeightDate = targetWeightDate.getMonth()+1 + "/" + targetWeightDate.getDate() + "/"+ targetWeightDate.getFullYear();
		} else {
			targetWeightDate = document.selectForm.weightDate.value;
			var daysToTargetWeight = daysElapsed(new Date(document.selectForm.weightDate.value), new Date());
			rate = Math.round(((eval(document.selectForm.weight.value) - eval(document.selectForm.targetWeight.value)) / (daysToTargetWeight / 7))*100)/100;
		}
	} else { //gain weight
		weightGoal = "gainWeight";
		if (document.selectForm.loseGainType[0].checked) {
			rate = document.selectForm.loseGainRate.value;
			targetWeightDate = addWeeks(new Date(), eval(eval(document.selectForm.targetWeight.value - document.selectForm.weight.value) / rate));
			targetWeightDate = targetWeightDate.getMonth()+1 + "/" + targetWeightDate.getDate() + "/"+ targetWeightDate.getFullYear();
		} else {
			targetWeightDate = document.selectForm.weightDate.value;
			var daysToTargetWeight = daysElapsed(new Date(document.selectForm.weightDate.value), new Date());
			rate = Math.round(((eval(document.selectForm.targetWeight.value) - eval(document.selectForm.weight.value)) / (daysToTargetWeight / 7))*100)/100;
		}
	}
	document.selectForm.loseGainRate.value = rate;
	document.selectForm.weightDate.value = targetWeightDate;			

	//bmi information
	var height = document.selectForm.height.value;
	var weight = document.selectForm.weight.value;
	height = height * 2.54;
	height = height / 100;
	weight = weight / 2.2;
	var bmi = weight / (height*height);
	bmi = Math.round(bmi*10)/10;

	//bmr
	var bmr;
	var age = document.selectForm.age.value;
	height = height * 100;
	if (document.selectForm.gender[0].checked) {
	  bmr = 66.473 + (13.751 * weight) + (5.0033 * height) - (6.755 * age);
	} else {
	  bmr = 655.0955 + (9.563 * weight) + (1.8496 * height) - (4.6756 * age);
	}
	document.selectForm.bmr.value = Math.round(bmr);
	
	var exerciseActivityTime = eval(eval(document.selectForm.exerciseActivityHr.value) + eval(document.selectForm.exerciseActivityMin.value / 60))
	if (exerciseActivityTime == 0) {
		if (!confirm("You have entered zero exercise time. Is this correct?")) {
			return;
		}
	}
	var avgCaloriesWeighted = 0;
	var avgCaloriesWeightedWeekly = 0;
	var avgCaloriesWeightedDaily = 0;
	if (mode == 1 && exerciseActivityTime > 0) { //detailed
		//exercise information
		var activityType = 1;
		var activityID = 0;
		var activityHrs = 0;
		var activityMin = 0;
		var calorieCoefficient = 0;
		var timeActivityPerformed = 0;
		weight = document.selectForm.weight.value;
				
		var totalTimeCommonActivity = 0;
		var commonActivityCaloriesTotal = 0;
		var totalTimeOccassionalActivity = 0;
		var occassionalActivityCaloriesTotal = 0;
		var totalTimeRareActivity = 0;
		var rareActivityCaloriesTotal = 0;
		var myUserGoalActivities = "";
							
		for (i=0; i<myActivityTable.rows.length; i++) {
			activityType = myActivityTable.rows(i).cells(0).children(0).value;
			activityID = myActivityTable.rows(i).cells(0).children(1).value;
			activityHrs = myActivityTable.rows(i).cells(0).children(3).value;
			activityMin = myActivityTable.rows(i).cells(0).children(4).value;
			calorieCoefficient = myActivityTable.rows(i).cells(0).children(2).value;
			timeActivityPerformed = eval(activityHrs) + eval(activityMin / 60);
			if (activityType == 1) {
				totalTimeCommonActivity = totalTimeCommonActivity + timeActivityPerformed;
				commonActivityCaloriesTotal = commonActivityCaloriesTotal + calorieCoefficient * bmr * timeActivityPerformed / 24;
			} else if (activityType == 2) {
				totalTimeOccassionalActivity = totalTimeOccassionalActivity + timeActivityPerformed;
				occassionalActivityCaloriesTotal = occassionalActivityCaloriesTotal + calorieCoefficient * bmr * timeActivityPerformed / 24;
			} else if (activityType == 3) {
				totalTimeRareActivity = totalTimeRareActivity + timeActivityPerformed;
				rareActivityCaloriesTotal = rareActivityCaloriesTotal + calorieCoefficient * bmr * timeActivityPerformed / 24;
			}
			myUserGoalActivities = myUserGoalActivities + activityType + "|" + activityID + "|" + activityHrs + "|" + activityMin + "|";
		}
		document.selectForm.userGoalActivities.value = myUserGoalActivities;
				
		if (exerciseActivityTime > 0 && myUserGoalActivities.length == 0) {
			alert("Please select some activities you plan on participating in.");
			return;
		}
				
		var commonActivityWeight = 7;
		var occassionalActivityWeight = 3;
		var rareActivityWeight = 1;
	
		if (myActivityTable.rows.length > 0) {
			avgCaloriesWeighted = eval(commonActivityCaloriesTotal * commonActivityWeight + occassionalActivityCaloriesTotal * occassionalActivityWeight + rareActivityCaloriesTotal * rareActivityWeight) / eval(totalTimeCommonActivity * commonActivityWeight + totalTimeOccassionalActivity * occassionalActivityWeight + totalTimeRareActivity * rareActivityWeight);
			avgCaloriesWeightedWeekly = avgCaloriesWeighted * exerciseActivityTime;
			avgCaloriesWeightedDaily = avgCaloriesWeightedWeekly / 7;
		}
	} else if (mode == 2 && exerciseActivityTime > 0) {
		var commonActivityWeight = 7;
		var activityCaloriesTotal = eval(document.selectForm.exerciseActivityLevel.value) * bmr * exerciseActivityTime / 24;
		avgCaloriesWeighted = eval(activityCaloriesTotal * commonActivityWeight) / eval(exerciseActivityTime * commonActivityWeight);
		avgCaloriesWeightedWeekly = avgCaloriesWeighted * exerciseActivityTime;
		avgCaloriesWeightedDaily = avgCaloriesWeightedWeekly / 7;
	}			
	document.selectForm.exerciseCalories.value = Math.round(avgCaloriesWeightedWeekly);
	//total calorie intake
	var activityLevel = document.selectForm.activityLevel.value;
	var calorieIntake = 0;
	if (document.selectForm.gender[0].checked) {
		if (activityLevel == "1") {
			calorieIntake = bmr * 1.15;
		} else if (activityLevel == "2") {
			calorieIntake = bmr * 1.4;
		} else if (activityLevel == "3") {
			calorieIntake = bmr * 1.5;
		} else if (activityLevel == "4") {
			calorieIntake = bmr * 1.85;
		} else if (activityLevel == "5") {
			calorieIntake = bmr * 2.1;
		}
	} else {
		if (activityLevel == "1") {
			calorieIntake = bmr * 1.15;
		} else if (activityLevel == "2") {
			calorieIntake = bmr * 1.35;
		} else if (activityLevel == "3") {
			calorieIntake = bmr * 1.45;
		} else if (activityLevel == "4") {
			calorieIntake = bmr * 1.7;
		} else if (activityLevel == "5") {
			calorieIntake = bmr * 2;
		}
	}
	calorieIntake = calorieIntake + avgCaloriesWeightedDaily;
	//now take into account user's target weight goal
	if (weightGoal == "loseWeight") {
		calorieIntake = calorieIntake - eval(500 * rate);
	} else if (weightGoal == "gainWeight") {
		calorieIntake = calorieIntake + eval(500 * rate);
	}

	document.selectForm.calorieIntake.value = calorieIntake;

	if (document.selectForm.gender[1].checked) {
		document.selectForm.userGender.value = "F";
	}
	if (document.selectForm.IAgree.checked) {
		document.selectForm.IAgree.value = "on";
	} else {
		document.selectForm.IAgree.value = "off";
	}	
	if (document.selectForm.name.value.length == 0) {
		alert("Please enter your name.");
		document.selectForm.name.focus();
	} else if (document.selectForm.email.value.length == 0) {
		alert("Please enter your email address.");
		document.selectForm.email.focus();
	} else if (check_email(document.selectForm.email.value)==false) {
		document.selectForm.email.focus(); 
	} else if (!document.selectForm.IAgree.checked) {
		alert("You are required to be at least 18 years old and have read the Terms & Conditions. Please review this option in Get Your Results.");
		document.selectForm.IAgree.focus();
		return;
	} else {
		document.selectForm.submit();
	}
}	
function addWeeks(myDate, weeks) {
	return new Date(myDate.getTime() + weeks*7*24*60*60*1000);
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function daysElapsed(date1,date2) {
    var difference = Date.UTC(y2k(date1.getFullYear()),date1.getMonth(),date1.getDate(),0,0,0)- Date.UTC(y2k(date2.getFullYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}
function clearData() {
	if (confirm("Are you sure you wish to clear the activities table?")) {
		activityTableDIV.innerHTML = "<table id='myActivityTable' width='87%'	border='1' cellspacing='1' cellpadding='2'></table>";
	}
}	
function CheckNumeric(myfield, fieldName) {
	for (i=0; i<myfield.value.length; i++) {
		if ( ((myfield.value.charAt(i) < '0') || (myfield.value.charAt(i) > '9')) && myfield.value.charAt(i) !=	'.') {
			alert (fieldName + " must be numeric.");
      myfield.value="0";
			myfield.focus();
			return false;
		}
	}
	if (myfield.value.length==0) {
		myfield.value="0";
	}
	return true;
}
function CheckNumeric2(myfield, fieldName, min, max) {
	for (i=0; i<myfield.value.length; i++) {
		if ( ((myfield.value.charAt(i) < '0') || (myfield.value.charAt(i) > '9')) && myfield.value.charAt(i) !=	'.') {
			alert (fieldName + " must be numeric.");
      myfield.value="0";
			myfield.focus();
			return false;
		}
	}
	if (myfield.value.length==0) {
		myfield.value="0";
	}
	if (myfield.value > max) {
		alert("Please enter a value less than " + eval(max+1) + " for " + fieldName + ".");
		myfield.value = max;
		myfield.focus();
		return false;
	}
	if (myfield.value < min) {
		alert("Please enter a value greater than " + eval(min-1) + " for " + fieldName + ".");
		myfield.value = min;
		myfield.focus();
		return false;
	}
	return true;
}
function setTargetWeight() {
	if (document.selectForm.targetWeight.value.length == 0 || document.selectForm.targetWeight.value == "0") {
		document.selectForm.targetWeight.value = document.selectForm.weight.value;
	}
}
function setLoseGainWeight() {
	if (document.selectForm.weight.value > document.selectForm.targetWeight.value) {
		loseGainText.innerHTML = "If you are interested in losing weight, your goal is to lose:";
	} else if (document.selectForm.weight.value < document.selectForm.targetWeight.value) {
		loseGainText.innerHTML = "If you are interested in gaining weight, your goal is to gain:";
	} else {
		loseGainText.innerHTML = "If you are interested in only maintaining weight, you don't have to fill in the following:";
	}
}
var selectedActivityID = 0;
function setActivityID(selectedID) {
	selectedActivityID = selectedID;
}
function addActivity() {
	var activity = document.selectForm.activity.value;
	var activityID = activity.substring(0, activity.indexOf("-"));
	var	calorieCoefficient = activity.substring(activity.indexOf("-")+1, activity.length);
	for (i=0; i<myActivityTable.rows.length; i++) {
		if ((activityID == myActivityTable.rows(i).cells(0).children(1).value)) {
			alert("You have already selected this activity.");
			return;
		}	
	}
	if (document.selectForm.activityHr.options[document.selectForm.activityHr.selectedIndex].text == "0" &&
			document.selectForm.activityMin.options[document.selectForm.activityMin.selectedIndex].text == "00") {
		alert("Please select a time for the selected activity.");
		return;		
	}
	var newActivityTable = activityTableDIV.innerHTML;
	newActivityTable = newActivityTable.substring(0, newActivityTable.length-16);
	newActivityTable = newActivityTable + "<tr border='1'><td align='left' width='20%' class='black10Caption'>";
	newActivityTable = newActivityTable + "<input type='hidden' value='" + document.selectForm.activityType.value + "'>";
	newActivityTable = newActivityTable + "<input type='hidden' value='" + activityID + "'>";
	newActivityTable = newActivityTable + "<input type='hidden' value='" + calorieCoefficient + "'>";
	newActivityTable = newActivityTable + "<input type='hidden' value='" + document.selectForm.activityHr.value + "'>";
	newActivityTable = newActivityTable + "<input type='hidden' value='" + document.selectForm.activityMin.value + "'>";
	newActivityTable = newActivityTable + document.selectForm.activityType.options[document.selectForm.activityType.selectedIndex].text + "</td>";
	newActivityTable = newActivityTable + "<td align='left' width='62%' class='black10Caption'><a href='#' class='blue10TextHL' onMouseover='showmenu(event,linkset[1]);setActivityID(" + activityID + ");' onMouseout='delayhidemenu()'>" + document.selectForm.activity.options[document.selectForm.activity.selectedIndex].text + "</a></td>";
	newActivityTable = newActivityTable + "<td align='left' width='9%' class='black10Caption'>" + document.selectForm.activityHr.options[document.selectForm.activityHr.selectedIndex].text + " Hrs</td>";
	newActivityTable = newActivityTable + "<td align='left' width='9%' class='black10Caption'>" + document.selectForm.activityMin.options[document.selectForm.activityMin.selectedIndex].text + " Min</td></tr></tbody></table>";
	activityTableDIV.innerHTML = newActivityTable;
}
function removeActivity() {
	var activityTable = activityTableDIV.innerHTML;
	var newActivityTable = "";
	var newRowID=0;
	newActivityTable = newActivityTable + activityTable.substring(0, activityTable.indexOf("<TR"));
	activityTable = activityTable.substring(activityTable.indexOf("<TR")+3, activityTable.length);
	if (myActivityTable.rows.length == 1) {
		clearData();
	} else {
		for (i=0; i<myActivityTable.rows.length; i++) {
			if (myActivityTable.rows(i).cells(0).children(1).value != selectedActivityID) {
				if (activityTable.indexOf("<TR") > 0) {
					newActivityTable = newActivityTable + "<TR" + activityTable.substring(0, activityTable.indexOf("<TR"));
				} else {
					newActivityTable = newActivityTable + "<TR" + activityTable.substring(0, activityTable.length);
				}
			}
			activityTable = activityTable.substring(activityTable.indexOf("<TR")+3, activityTable.length);
		}
		activityTableDIV.innerHTML = newActivityTable;
	}
}