// JavaScript Document

//This file contains all of the JavaScript for the 
//fitness calculators

//Body Mass Index Calculator
function calcBMI()
	{
	var form = document.BMI;
	// convert lbs to kg
	wt_kg = (form.lbs.value * 0.4536);  
	// convert inches to meters
	ht_mtrs = (((form.feet.value * 12) + (1 * form.inches.value)) * 0.0254); 
	// calculate bmi
	form.bmi.value = parseInt(wt_kg/Math.pow((ht_mtrs),2));
}

//Calorie Burning Calculator
function calcCalories() {
	var form = document.calcForm;
	//grab the form variables
	var calcWeight = form.weight.value;
	var calcUnit = form.weightUnit.options[document.calcForm.weightUnit.selectedIndex].value;
	var calcDistance = form.distance.value;
	var calcDistUnit = form.distUnit.options[document.calcForm.distUnit.selectedIndex].value;
	var calcCoefficient = form.sport.options[document.calcForm.sport.selectedIndex].value;
	//convert kg to lbs if kg selected
	if (calcUnit == "kg") {
		calcWeight = calcWeight * 2.2046;
	}
	//calculate for yds 
	if (calcDistUnit == "yd") {
		calcDistance = calcDistance / 1760;
	}
	//calculate for metres
	if (calcDistUnit == "meter") {
		calcDistance = calcDistance / 1609.344;
	}
	//calculate for kilometres
	if (calcDistUnit == "km") {
		calcDistance = calcDistance / 1.609344;
	}
	//multiply and round off the result
	form.calories.value = Math.round(calcCoefficient * calcWeight * calcDistance);
}

//Target Heartrate Calculator
function calcTarget(){
	var form = document.calcTHR;
	var target = 220;
	//grab the form variables
	var age = form.age.value;	
	//grab selected radio button
	for (var i=0; i < form.rout.length; i++)
		{
		if (form.rout[i].checked)
			{
			var routine = form.rout[i].value;
			}
		}		
	//set some useful percentages
	var per1 = .50;
	var per2 = .70;
	//begin the math for target heart rate
	var thr = (target - age);	
	//get users routine type and multiply accordingly
	if (routine == "b")	{
		thr = thr * per1;
	}
	if (routine == "i")	{
		thr = thr * per2;
	}
	//output results
	form.thr.value = thr; 
}
	
//Water Requirements Calculator
function calcWater(){
	var form = document.calcWRC;
	//grab the form variables
	var weight = form.weight.value;
	//do the math
	var waterR = (weight * .50);
	//output results
	form.required.value = Math.round(waterR); 
}

//Lose A Pound Calculator
function calcLBS() {
	var form = document.calcLP;
	//grab the form variables
	var lbsToLose = form.weight.value;
	var timeToLose = form.time.value;
	//do the math
	var totCalLose = (lbsToLose * 3500);
	var totTimeLose = (totCalLose / timeToLose);
	//final calculation	
	var totNumCal = (totTimeLose / 7);
	//output results
	form.calories.value = (totNumCal); 
}

//Ideal Bodyweight Calculator
function FigureIBW(form, feet, inches, gender)
	{
	TotalInches = eval(feet*12) + eval(inches)
	if (gender == "f") {	
		if (TotalInches == 60) {
			form.calcval.value = 100;
			}
			if (TotalInches > 60 < 72) {
			form.calcval.value = 100 + (5 * inches);
			}
			if (TotalInches == 72) {
			form.calcval.value = 160;
			}
			if (TotalInches > 72) {
			form.calcval.value = 160 + (5 * inches);
			}
			if (TotalInches == 48) {
			form.calcval.value = 70;
			}
			if (TotalInches < 60) {
			form.calcval.value = 70 + (2.5 * inches);
		}
	}
	else
	{
		if (TotalInches == 60) {
		form.calcval.value = 106;
		}
		if (TotalInches > 60) {
		form.calcval.value = 106 + (6 * inches);
		}
		if (TotalInches == 72) {
		form.calcval.value = 178;
		}
		if (TotalInches > 72) {
		form.calcval.value = 178 + (6 * inches);
		}
		if (TotalInches == 48) {
		form.calcval.value = 76;
		}
		if (TotalInches < 60) {
		form.calcval.value = 76 + (2.5 * inches);
		}
	}
}

//Dynamic function for pop up windows
function launchPop(theWindow,windowName,parameters)
	{
		var winOpen 
		winOpen = window.open(theWindow,windowName,parameters);
	}



