/*** jump to buttons **/
function jumpto(url){
window.location=url
}


// WALKING DISTANCE CALCULATOR //

function isPosNumber(val) {
  var str = val + "";
  if (str != "") {
    oneDecimal = false;
    for (var i = 0; i < str.length; i++) {
      var oneChar = str.charAt(i);
      if (oneChar == "." && !oneDecimal) {
        oneDecimal = true;
        continue;
      }
      if (oneChar < "0" || oneChar > "9") {
        return false;
      }
    }
  }
  return true;
}		
		
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 format(expr, decplaces) {
  var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));
  while (str.length <= decplaces) str = "0" + str;
  var decpoint = str.length - decplaces;
  var right = str.substring(decpoint, str.length);
  if (right == "00") right = ""; else right = "." + right;
  return str.substring(0, decpoint) + right;
}

function checkForm(form) {

var formValid = true;
var fieldsUsed = 0;

var distance = form.dist.value;

var timeHours = form.TimeHours.value;
var timeMins = form.TimeMins.value;
var timeSecs = form.TimeSecs.value;

var paceHours = form.PaceHours.value;
var paceMins = form.PaceMins.value;
var paceSecs = form.PaceSecs.value;

var timestr = timeHours.toString() + timeMins.toString() + timeSecs.toString();
var pacestr = paceHours.toString() + paceMins.toString() + paceSecs.toString();

	if(distance != '') {
		fieldsUsed++
	}

	if(timestr != '') {
		fieldsUsed++
	}
	
	if(pacestr != '') {
		fieldsUsed++
	}
	
	
	if(fieldsUsed < 2) {
	
		alert('Please include at least two variables.');
		
		formValid = false;
		
	} else {
	
		if(fieldsUsed == 3) {
		
			alert('Please include only two variables.')
			
			formValid = false;
			
		} else {
		
			validNumbers = true;
			
			if (((timeHours != '') && (!(checkValue(timeHours)))) ||
				((timeMins != '') && (!(checkValue(timeMins)))) ||
				((timeSecs != '') && (!(checkValue(timeSecs))))) {
			
				validNumbers = false;
			
			}
			
			if (((paceHours != '') && (!(checkValue(paceHours)))) ||
				((paceMins != '') && (!(checkValue(paceMins)))) ||
				((paceSecs != '') && (!(checkValue(paceSecs))))) {
			
				validNumbers = false;
			
			}
			
			if ((distance != '') && (!(isPosNumber(distance)))) {
			
				validNumbers = false;
			
			}			
			if (!(validNumbers)) {			
				alert('Please enter only numbers for the field values.');				
				formValid = false;			
			}
		}
	} 
	
	return formValid;
}

function calculateWalk() {

var form = document.pacer;
if(checkForm(form)) {

var dist;
var empty="";
var timestr = form.TimeHours.value.toString() + form.TimeMins.value.toString() + form.TimeSecs.value.toString();
var timeHours = form.TimeHours.value;
var timeMins = form.TimeMins.value;
var timeSecs = form.TimeSecs.value;
var paceHours = form.PaceHours.value;
var paceMins = form.PaceMins.value;
var paceSecs = form.PaceSecs.value;

if(timeHours == '') { timeHours = 0 }
if(timeMins == '') { timeMins = 0 }
if(timeSecs == '') { timeSecs = 0 }
if(paceHours == '') { paceHours = 0 }
if(paceMins == '') { paceMins = 0 }
if(paceSecs == '') { paceSecs = 0 }

timeHours = parseInt(timeHours, 10);
timeMins = parseInt(timeMins, 10);
timeSecs = parseInt(timeSecs, 10);
paceHours = parseInt(paceHours, 10);
paceMins = parseInt(paceMins, 10);
paceSecs = parseInt(paceSecs, 10);

var time1 = (timeHours* 60) + timeMins + (timeSecs / 60);
var time2 = (paceHours* 60) + paceMins + (paceSecs / 60);
if (form.dist.value != "") {
  if (form.disttype.selectedIndex == 0) dist = form.dist.value * 1000;
  else if (form.disttype.selectedIndex == 1) dist = form.dist.value * 1609.34;
  else if (form.disttype.selectedIndex == 2) dist = form.dist.value;
  else if (form.disttype.selectedIndex == 3) dist = form.dist.value / 1.093616;
  if (timestr == "") empty = "time"; else empty = "pace";  
} else {
	empty = "dist";
}
var h, m, s;
var hstr, mstr, sstr;
var thepace = 0;
if (empty == "pace") {
  //time1 / dist --> minutes / meters
  if (form.pacetype.selectedIndex == 0) thepace = (time1 / dist) * 1000; //per kilometer
  else thepace = (time1 / dist) * 1609.34; //per mile
  h = parseInt(thepace / 60, 10);
  m = parseInt(thepace % 60, 10);
  s = parseInt((thepace - parseInt(thepace)) * 60, 10);
  if (h < 10) hstr = "0" + h; else hstr = h;
  if (m < 10) mstr = "0" + m; else mstr = m;
  if (s < 10) sstr = "0" + s; else sstr = s;
  if (isNaN(hstr)) hstr = "00";
  if (isNaN(mstr)) mstr = "00";
  if (isNaN(sstr)) sstr = "00";
  form.PaceHours.value = hstr;
  form.PaceMins.value = mstr;
  form.PaceSecs.value = sstr; 
}
else if (empty == "dist") {
  if (form.pacetype.selectedIndex == 0) { //per kilometer
    var kilometers = time1 / time2;
    if (form.disttype.selectedIndex == 0) form.dist.value = format(kilometers, 2); //kilometers
    else if (form.disttype.selectedIndex == 1) form.dist.value = format(kilometers * .62137274, 2); //miles
    else if (form.disttype.selectedIndex == 2) form.dist.value = format(kilometers * 1000, 2); //meters
    else form.dist.value = format(kilometers * 1093.6160165, 2); //yards 
  }  
  else { //per mile
    var miles = time1 / time2; 
    if (form.disttype.selectedIndex == 0) form.dist.value = format(miles * 1.60934, 2);
    else if (form.disttype.selectedIndex == 1) form.dist.value = format(miles, 2);
    else if (form.disttype.selectedIndex == 2) form.dist.value = format(miles * 1609.34, 2);
    else form.dist.value = format(miles * 1760, 2);
  }
}
else if (empty == "time") {
  var tempdist = form.dist.value;
  if (form.pacetype.selectedIndex == 0) { //per kilometer
    if (form.disttype.selectedIndex == 1) tempdist *= 1.60934; //miles
    else if (form.disttype.selectedIndex == 2) tempdist /= 1000; //meters
    else if (form.disttype.selectedIndex == 3) tempdist /= 1093.6160165; //yards
  }
  else { //per mile
    if (form.disttype.selectedIndex == 0) tempdist *= .62137274; //k
    else if (form.disttype.selectedIndex == 2) tempdist /= 1609.34;
    else if (form.disttype.selectedIndex == 3) tempdist /= 1760;
  }
  thepace = tempdist * time2; 
  h = parseInt(thepace / 60, 10);
  m = parseInt(thepace % 60, 10);
  s = parseInt((thepace - parseInt(thepace)) * 60, 10);
  if (h < 10) hstr = "0" + h; else hstr = h;
  if (m < 10) mstr = "0" + m; else mstr = m;
  if (s < 10) sstr = "0" + s; else sstr = s;
  if (isNaN(hstr)) hstr = "00";
  if (isNaN(mstr)) mstr = "00";
  if (isNaN(sstr)) sstr = "00";
  form.TimeHours.value = hstr;
  form.TimeMins.value = mstr;
  form.TimeSecs.value = sstr; 
}
}
}

function setDistance() {
	var form = document.pacer;
	distIndex = form.disttype.selectedIndex;
	if(distIndex == 5) {
		form.disttype.selectedIndex = 1;
		form.dist.value = '26.2';		
	}	
	if(distIndex == 6) {	
		form.disttype.selectedIndex = 1;
		form.dist.value = '13.1';		
	}	
	if(distIndex == 7) {	
		form.disttype.selectedIndex = 0;
		form.dist.value = '10';
	}
	if(distIndex == 8) {	
		form.disttype.selectedIndex = 0;
		form.dist.value = '5';
	}
}

	//TARGET HEART RATE CALCULATOR//
	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 calculate(){

	form = document.thisTarget;
	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);
	
	document.thisTarget.LowRate.value = lowRate;
	document.thisTarget.HighRate.value = highRate;
	document.thisTarget.TargetRate.value = targetRate;
	
	}
}
//IDEA WEIGHT//
function ClearFormkgs (form) {
form.MaxWt.value="";
form.MinWt.value="";
form.height.value="";
}

function Optimumkgs(form) {

// Holds whether or not the form is correct
var Correct = true;

// Check for missing fields
	if (form.height.value == null || form.height.value.length == 0) {
	alert ("\nPlease fill in all of the form elements");
	Correct = false;
	}

// Check that only numbers are in the weight and height fields
	else {

// Holds the floats representing the height input
	var Testheight;

// Parse the weight and height values and put in the variables

	Testheight = parseFloat(form.height.value);

// Check to see that Testheight is a number
		if (isNaN (Testheight)) {
		alert("\nMake sure to enter the height \nas a number.");
		Correct = false;
		form.height.value="";
		}

// Check that the Testheight is greater than 0
		if (Testheight <= 0) {
		alert("\nCome on.  Enter a real height size.");
		Correct = false;
		form.height.value="";	
		}

	}
// If the inputs are correct, calculate the bmi

	if (Correct) {

// Hold the calculation numbers
	var MaxWt;
	var MinWt;
	Height2 = (Testheight/100) * (Testheight/100);
	MaxWt = (Math.round(23 * Height2));
	MinWt = (Math.round(19 * Height2));

// Send this number to the screen
	form.MaxWt.value = MaxWt;
	form.MinWt.value = MinWt;


	}

}
// CALORIES value//
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 calcNutrition () {

var form = document.thisCalories;
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 setValue() {

	document.thisCalories.TotalCalories.value = '#Calories#';
	
}

// CALORIC NEEDS CALCULATOR //


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 Figurecalneeds() {

form = document.calneeds;
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.calneeds.Calories.value;
	URL = 'nutritionalneeds_calc.cfm?calories=' + caloricNeeds;
	popup=window.open(URL, '', 'width=456,height=293');

}
// BODY FAT //
function ClearFormkg (form) {

	form.weight.value="";

	form.waist.value="";

	form.bodyfat.value="";

}


function VerifyFormkg(form) {

	

	// Holds whether or not the form is correct

	var Correct = true;



	// Check for missing fields

	if (form.weight.value == null || form.weight.value.length == 0 ||

	form.waist.value == null || form.waist.value.length == 0) {

		alert ("\nPlease fill in all of the form elements");

		Correct = false;

	}

	

	// Check that only numbers are in the weight and waist fields

	else {

		// Holds the floats representing the weight and waist

		var TestWeight;

		var TestWaist;



		// Parse the weight and waist values and put in the variables

		TestWeight = parseFloat(form.weight.value);

		TestWaist = parseFloat(form.waist.value);



		// Check to see that TestWeight is a number

		if (isNaN (TestWeight)) {

			alert("\nMake sure to enter the weight\nas a number.");

			Correct = false;

			form.weight.value="";

		}



		// Check to see that TestWaist is a number

		if (isNaN (TestWaist)) {

			alert("\nMake sure to enter the waist size\nas a number.");

			Correct = false;

			form.waist.value="";

		}

	

		// Check that the TestWeight is greater than 0

		if (TestWeight <= 0) {

			alert("\nCome on.  Enter a real weight.");

			Correct = false;

			form.weight.value="";	

		}



		// Check that the TestWaist is greater than 0

		if (TestWaist <= 0) {

			alert("\nCome on.  Enter a real waist size.");

			Correct = false;

			form.waist.value="";	

		}



	}



	// If the inputs are correct, calculate the bodyfat percent

	if (Correct) {

		

		// Holds the bodyfat number and percent

		var BF;

		var BFPercent;


			TestWaist=TestWaist/2.54;

			TestWeight=TestWeight*2.2;

		// Make the male calculations	

		if (form.gender[0].checked) {

			BF = -98.42 + (4.15*TestWaist) - (.082*TestWeight);

		}



		// Make the female calculations
		else {

			BF = -76.76 + (4.15*TestWaist) - (.082*TestWeight);

		}

		

		// Calculate the bodyfat percentage

		BFPercent = BF / TestWeight;

		BFPercent = BFPercent * 100;

		BFPercent = Math.round(BFPercent);



		// Send this number to the screen

		form.bodyfat.value = BFPercent + "%";

	}

}



// BMR AMR CALCULATOR //

function computeBMR(form){
form.BMRMale.value = 66.473 + ((form.weight.value) * 13.751) + (5.0033 * form.height.value) - (6.55 * form.age.value);
form.BMRFemale.value = 665.51 + ((form.weight.value) * 9.463) + (1.8496 * form.height.value) - (4.6756 * form.age.value);
return;
}
function computeTotal(form){
if (form.vlight.value.length == 0){
form.vlight.value = "0";
}
if (form.light.value.length == 0){
form.light.value = "0";
}
if (form.moderate.value.length == 0){
form.moderate.value = "0";
}
if (form.heavy.value.length == 0){
form.heavy.value = "0";
}
if (form.vheavy.value.length == 0){
form.vheavy.value = "0";
}
form.TotMale.value = (1.0 * form.BMRMale.value) + (1.4 * form.vlight.value) + (2.5 * form.light.value) + (4.2 *form.moderate.value) + (8.2 * form.heavy.value) + (12 * form.vheavy.value);
form.TotFemale.value = (1.0 * form.BMRFemale.value) + (1.4 * form.vlight.value) + (2.5 * form.light.value) + (4.2 *form.moderate.value) + (8.2 * form.heavy.value) + (12 * form.vheavy.value);
return;
}
function clearBMR(form){
form.age.value = "";
form.weight.value = "";
form.height.value = "";
form.BMRMale.value = "";
form.BMRFemale.value = "";
return;
}
function clearTotal(form){
form.vlight.value = "0";
form.light.value = "0";
form.moderate.value = "0";
form.heavy.value = "0";
form.vheavy.value = "0";
form.TotMale.value = "0";
form.TotFemale.value = "0";
return;
}




//BMI//
		

function calcMetric(form, meters, kilograms) {
   form.calcval.value = Math.round(kilograms * 10 / meters / meters) / 10
}
var activity=new Array();
var weight;
var minutes;

function HowMany(ActivityBurn) {
var burn;
var kilograms;
var weight_entered;
var minutes_entered;

burn_value=ActivityBurn.activity.options[ActivityBurn.activity.selectedIndex].value;
weight_entered=ActivityBurn.curweight.value;
minutes_entered=ActivityBurn.duration.value;

if (burn_value=="" || weight_entered=="" || minutes_entered=="") {
alert("Please enter all required data!");
}
else {

kilograms=curweight/2.2;
hours=duration/60.0;
burn=Math.round(burn_value*kilograms*hours);
 }

ActivityBurn.Fdiff.value = burn;
ActivityBurn.wkg.value = eval(kilograms);
}

function SetWeight(weight) {
curweight = weight.value;
}
function SetDuration(minutes) {
duration = minutes.value;
}

//metric
var mactivity=new Array();
var mweight;
var mminutes;

function MHowMany(MActivityBurn) {
var mburn;
var mkilograms;
var mpounds;
var mweight_entered;
var mminutes_entered;

mburn_value=MActivityBurn.mactivity.options[MActivityBurn.mactivity.selectedIndex].value;
mweight_entered=MActivityBurn.mcurweight.value;
mminutes_entered=MActivityBurn.mduration.value;

if (mburn_value=="" || mweight_entered=="" || mminutes_entered=="") {
alert("Please enter all required data!");
}
else {

mkilograms=mcurweight;
mpounds=mkilograms * 2.2;
mhours=mduration/60.0;
mburn=Math.round(mburn_value*mkilograms*mhours);
 }

MActivityBurn.MFdiff.value = mburn;
MActivityBurn.mpounds.value = eval(mpounds);
}

function MSetWeight(mweight) {
mcurweight = mweight.value;
}
function MSetDuration(mminutes) {
mduration = mminutes.value;
}
