// JavaScript Document

//This page contains all of the JavaScript for the 
//health calculators

//Body Fat Index Calculator
function clearForm (form) {
	form.weight.value="";
	form.waist.value="";
	form.bodyfat.value="";
	}

function validateForm(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("\nEnter the weight\nas a number.");
	Correct = false;
	form.weight.value="";
	}
	
	// Check to see that TestWaist is a number
	if (isNaN (TestWaist))
	{
	alert("\nEnter the waist size\nas a number.");
	Correct = false;
	form.waist.value="";
	}
	
	// Check that the TestWeight is greater than 0
	if (TestWeight <= 0)
	{
	alert("\nEnter a real weight.");
	Correct = false;
	form.weight.value=""; 
	}
	
	// Check that the TestWaist is greater than 0
	if (TestWaist <= 0)
	{
	alert("\nEnter 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;
	// 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 + "%";
	}
}

//Body Mass Index Calculator
function bmi_sol()
	{
	var form = document.calcBMI;
	// 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 Calculator
function calcCalories() {
	//grab the form variables
	var calcWeight = document.calcForm.weight.value;
	var calcUnit = document.calcForm.weightUnit.options[document.calcForm.weightUnit.selectedIndex].value;
	var calcDistance = document.calcForm.distance.value;
	var calcDistUnit = document.calcForm.distUnit.options[document.calcForm.distUnit.selectedIndex].value;
	var calcCoefficient = document.calcForm.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
	document.calcForm.calories.value = Math.round(calcCoefficient * calcWeight * calcDistance);

}

//Ideal Bodweight 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);
		}
	}
}

//Smoking Calculator
function calCigarettes() 
	{
	//grab the form variables
	var cigPerDay =  document.smoCalc.perDay.value;
	var cigPerPack =  document.smoCalc.inPack.value;
	var costPerPack =  document.smoCalc.perPack.value;
	
	//calculate cost per of smoking per year based on users entries
	//first divide number per pack by number smoked a day
	if (cigPerDay != 0) {
	cigPerPack = cigPerPack / cigPerDay;
	}
	//compare results to arbitrary number
	//then divide days per year by number of days a pack lasts
	if (cigPerPack != 5000) {
	cigPerPack = 365 / cigPerPack;
	}
	//compare results to 0
	//then multiply number of packs a year by cost per pack
	if (cigPerPack != 0) {
	cigPerPack = cigPerPack * costPerPack;
	}
//output results
document.smoCalc.total.value = (cigPerPack.toFixed(2));
}

//Dynamic function for pop up windows
function launchPop(theWindow,windowName,parameters)
	{
		var winOpen 
		winOpen = window.open(theWindow,windowName,parameters);
	}


