//*******************************************************************
// Name:  dailycalc.js
//
// IMPORTANT:  
//	is.js is essential to the operation of some of the functions in 
//	this file.  is.js must be included in html if this file is to be 
//	used.
//
// Description:
//	The JavaScript file that runs the daily energy calculator.  This
//	file must be included for the calculator to work.
//
// Author:  Kevin Mueller  mule@thinice.com
//
//	Initials		Date			Description
// ------------------------------------------------------------------
//	KRM				8-may-2001		Initial programming and design
//
//*******************************************************************


// Function to check the form1 fields and change weight and height to SI units.
function processForm1(){
	// Variables for the form values
	var weight, height, age, gender, form1List;

	// Checking to see that all of the fields have been entered
	if (document.form1.weight.value == ""){
		alert("You need to enter your weight.");
		return;
	}
	if (document.form1.height.value == ""){
		alert("You need to enter your height.");
		return;
	}
	if (document.form1.age.value == ""){
		alert("You need to enter your age.");
		return;
	}
	if (!document.form1.gender[0].checked && !document.form1.gender[1].checked){
		alert("You need to enter your gender.");
		return;
	}

	// Pull values form the form
	weight = document.form1.weight.value;
	height = document.form1.height.value;
	age = document.form1.age.value;
	if (document.form1.gender[0].checked){
		gender = document.form1.gender[0].value;
	}else if (document.form1.gender[1].checked){
		gender = document.form1.gender[1].value;
	}

	// Converting weight from lbs. to kg.
	if (document.form1.weight_units[1].checked){
		weight = weight/2.2;
	}
	// Converting height from in. to cm.
	if (document.form1.weight_units[1].checked){
		height = height*2.54;
	}

	// Returning it all in a list
	form1List = [weight, height, age, gender];
	return form1List;
}


// Function to drop all but the first decimal place.  
// Use only when displaying a result.
function dropDecimalPlaces(theNumber){
	var theString, decimalIndex
	theString = theNumber.toString();
	decimalIndex = theString.indexOf('.');
	if (decimalIndex != -1){
		theString = theString.slice(0, decimalIndex+2);
		return theString;
	}else{
		return theString;
	}
}


var dailyResultCalculated = 0;   // Global set to 1 if result has been calculated and 0 if not


// Function to reshow the calculate button/link
function clearResult(){
	if (!dailyResultCalculated)
		return;
	if (is_nav3 || is_nav4){   // For older Netscapes
		dailyDisplay("<a class=\"nsresultlink\" href=\"javascript:dailyCalculate();\">Calculate Calories</a>");
	}else{   // For other browsers
		dailyDisplay("<span class=\"biggreen\">&nbsp;</span><input type=\"button\" name=\"calculate\" onMouseUp=\"javascript:dailyCalculate();\" value=\"Calculate Calories\"><span class=\"biggreen\">&nbsp;</span>");
	}
	dailyResultCalculated = 0;
}


// Function to display calculated calories
function dailyDisplay(text){
	var lyr;   // the whole layer path for old NS browsers
	if (is_nav3 || is_nav4){   // For older Netscapes
		lyr = document.layers["nsbs"].document.layers["nsresult"].document;
		lyr.open();
		lyr.write(text);
		lyr.close();
	}else if (is_nav6up || is_ie5up){   // For standards compliant browsers
		document.getElementById("result").innerHTML = text;
	}else{   // For old IEs
		document.all["result"].innerHTML = text;
	}
	dailyResultCalculated = 1;	
}


// Function to calculate calories for the daily calculator
function dailyCalculate(){
	// Fomr1 list and values
	var form1List, weight, height, age, gender
	// Form2 values
	var restingTime, inactiveTime, vlaTime, lightTime, moderateTime, heavyTime
	// Calculation variables
	var bmrPerDay, bmrPerHour, resultString, totalBurned, totalTime, resting, inactive, vla, light, moderate, heavy
	
	// If there was something wrong with the form1 values, we stop the script.
	// Otherwise we use the entered information.
	form1List = processForm1();
	if (!form1List){
		return;
	}else{
		weight = form1List[0];
		height = form1List[1];
		age = form1List[2];
		gender = form1List[3];
	}	
	
	// Determining the user's BMR 
	if (gender == 'm'){
		// If the user is male
		bmrPerDay = 66 + (13.7 * weight) + (5 * height) - (6.8 * age);
	}else if (gender == 'f'){
		// If the user is female
		bmrPerDay = 655 + (9.6 * weight) + (1.7 * height) - (4.7 * age);
	}

	// Determining total burned calories
	bmrPerHour = bmrPerDay / 24;
	totalBurned = totalTime = 0;
	restingTime = document.form2.resting.value;
	inactiveTime = document.form2.inactive.value;
	vlaTime = document.form2.vla.value;
	lightTime = document.form2.light.value;
	moderateTime = document.form2.moderate.value;
	heavyTime = document.form2.heavy.value;
	//Setting time to 0 if nothing is entered or finding kcal/kg
	if (restingTime == ""){
		resting = 0;
	}else{
		resting = 0;
		totalTime = totalTime + parseFloat(restingTime);
	}
	if (inactiveTime == ""){
		inactive = 0;
	}else if(inactiveTime != "" && gender == 'f'){
		inactive = 0.24 * inactiveTime;
		totalTime = totalTime + parseFloat(inactiveTime);
	}else if(inactiveTime != "" && gender == 'm'){
		inactive = 0.45 * inactiveTime;
		totalTime = totalTime + parseFloat(inactiveTime);
	}
	if (vlaTime == ""){
		vla = 0;
	}else if(vlaTime != "" && gender == 'f'){
		vla = 0.75 * vlaTime;
		totalTime = totalTime + parseFloat(vlaTime);
	}else if(vlaTime != "" && gender == 'm'){
		vla = 1.08 * vlaTime;
		totalTime = totalTime + parseFloat(vlaTime);
	}
	if (lightTime == ""){
		light = 0;
	}else if(lightTime != "" && gender == 'f'){
		light = 1.59 * lightTime;
		totalTime = totalTime + parseFloat(lightTime);
	}else if(lightTime != "" && gender == 'm'){
		light = 1.89 * lightTime;
		totalTime = totalTime + parseFloat(lightTime);
	}
	if (moderateTime == ""){
		moderate = 0;
	}else if(moderateTime != "" && gender == 'f'){
		moderate = 2.76 * moderateTime;
		totalTime = totalTime + parseFloat(moderateTime);
	}else if(moderateTime != "" && gender == 'm'){
		moderate = 3.06 * moderateTime;
		totalTime = totalTime + parseFloat(moderateTime);
	}
	if (heavyTime == ""){
		heavy = 0;
	}else if(heavyTime != "" && gender == 'f'){
		heavy = 3.30 * heavyTime;
		totalTime = totalTime + parseFloat(heavyTime);
	}else if(heavyTime != "" && gender == 'm'){
		heavy = 3.72 * heavyTime;
		totalTime = totalTime + parseFloat(heavyTime);
	}
	totalBurned = (bmrPerHour * totalTime) + (weight * (resting + inactive + vla + light + moderate + heavy));

	// Displaying the result
	totalBurned = dropDecimalPlaces(totalBurned);
	resultString = "<span class=\"biggreen\">" + totalBurned + " kcal</span>";
	dailyDisplay(resultString);
}
