7<!--
// Calculate BMI using English measurements
function calculateBMIUsingEnglish()
{
   var feet;
   var inches;
   var pounds;
   var bmi;
   var bmiStatus;
   
 	// Get data from English measurement fields
	feet = window.document.bmiform.feetMenu.selectedIndex + 4;
	inches = window.document.bmiform.inchesMenu.selectedIndex;
	pounds = window.document.bmiform.poundsTextField.value - 0;
	
   	// If valid data entered, calculate BMI
	if(pounds > 0) {
   		// Calcuate BMI
       inches += (feet * 12.0);
       bmi = pounds / (inches * inches) * 703;

		// Determine BMI status
       bmiStatus = "Healthy, normal range.";
       if(bmi < 18.5) {
       	bmiStatus = "May be underweight.";
       }
       else if(bmi >= 25.0 && bmi < 30.0) {
       	bmiStatus = "May be overweight.";
       }
       else if(bmi >= 30.0) {
       	bmiStatus = "May be obese.";
       }    
       
       // Display BMI result and status
  	 	window.document.bmiform.bmiTextField.value = Math.round(bmi * 100)/100.0;
 	 	window.document.bmiform.statusTextField.value = bmiStatus;
    }           
}
        
// Calculate low, high and max heart target rate based on age
function calculateTHRate()
{
   var age;
   var timeInMinutes;
   var timeInSeconds;
   var lowHeartRate;
   var highHeartRate;
   var maxHeartRate;
   var timeArray = new Array(60,30,15);
   
   // Get information from age text input box
	age = window.document.thrateform.ageTextField.value - 0;

   // If age is in the correct range, calculate target heart rate
   if(age > 0 && age < 121) {
   		timeInSeconds = timeArray[window.document.thrateform.secondsMenu.selectedIndex];   	
     	timeInMinutes = Math.round(60/timeInSeconds);
     	maxHeartRate = Math.round((220 - age)/timeInMinutes);
     	lowHeartRate = Math.round(0.50 * maxHeartRate);
     	highHeartRate = Math.round(0.70 * maxHeartRate);

    	// Format and copy results to text input boxes
	 	window.document.thrateform.lowHRateTextField.value = lowHeartRate;
	 	window.document.thrateform.highHRateTextField.value = highHeartRate;
		window.document.thrateform.maxHRateTextField.value = maxHeartRate;
    }

}

// Calculate distance, time, pace and speed
function calculateDTPS()
{
        var inputBoxText;
        var numEmptyFields = 0;
        var distance = 0.0;
        var timeInSeconds = 0.0;
        var paceInSeconds = 0.0;
        var speed = 0.0;
        var hours = 0.0;
        var minutes = 0.0;   
        var seconds = 0.0;       
        
        // Extract information from distance text input box
        distance = window.document.dtpsform.distanceTextField.value - 0;

        // Determine time in seconds
        hours = window.document.dtpsform.timeHoursTextField.value - 0;
        minutes = window.document.dtpsform.timeMinsTextField.value - 0;
        seconds = window.document.dtpsform.timeSecsTextField.value - 0;
        timeInSeconds = (hours * 3600.0) + (minutes * 60.0) + seconds;
        
        // Determine pace in seconds
        hours = window.document.dtpsform.paceHoursTextField.value - 0;
        minutes = window.document.dtpsform.paceMinsTextField.value - 0;
        seconds = window.document.dtpsform.paceSecsTextField.value - 0;
        paceInSeconds = (hours * 3600.0) + (minutes * 60.0) + seconds;

        // Determine how many fields are empty (contain zero values)
        if(distance == 0) {
            numEmptyFields++;            
        }

        if(timeInSeconds == 0) {
            numEmptyFields++;
        }

        if(paceInSeconds == 0) {
             numEmptyFields++;
        }

        // Check if calculation can be performed
        if(numEmptyFields > 1) {
        	// Notify user data is invalid - Must enter at least two fields
        }

        // Determine the value of the empty field
        else {
            if(distance == 0) {              
                distance = timeInSeconds/paceInSeconds;
            } 
            else if(timeInSeconds == 0) {
                timeInSeconds = paceInSeconds * distance;
            } 
            else {
                paceInSeconds = timeInSeconds/distance;
            } 
            
            // Format and copy result to distance text input box
		     window.document.dtpsform.distanceTextField.value=Math.round(distance * 100.0)/100.0;

            // Format and copy result to speed text input box             
            speed = (3600.0/timeInSeconds) * distance;
            window.document.dtpsform.speedTextField.value=Math.round(speed * 100.0)/100.0;
            
            // Format and copy result to time text input box
            hours = Math.floor(timeInSeconds/3600.0);
            timeInSeconds -= hours * 3600.0;
            minutes = Math.floor(timeInSeconds/60.0);
            timeInSeconds -= minutes * 60;
            seconds = Math.floor(timeInSeconds);
            window.document.dtpsform.timeHoursTextField.value=hours;
            window.document.dtpsform.timeMinsTextField.value=minutes;
            window.document.dtpsform.timeSecsTextField.value=seconds;
            
            // Format and copy result to pace text input box
            hours = Math.floor(paceInSeconds/3600.0);
            paceInSeconds -= hours * 3600.0;
            minutes = Math.floor(paceInSeconds/60.0);
            paceInSeconds -= minutes * 60.0;
            seconds = Math.floor(paceInSeconds);
            window.document.dtpsform.paceHoursTextField.value=hours;
            window.document.dtpsform.paceMinsTextField.value=minutes;
            window.document.dtpsform.paceSecsTextField.value=seconds;
        }
    }    

// Calculate log base 10
function logBase10(x)
{
  	return Math.log(x)/Math.log(10.0);
}

// Calculate percent body fat
function calculatePercentBodyFat()
{
        var gender;
        var height = 0.0;
        var waistAtNavel = 0.0;
        var waistBelowRibs = 0.0;
        var waistAtHips = 0.0;
        var neck = 0.0;
        var percentBodyFat = 0.0;
	var density = 0.0;
        var in2cm = 2.54;
	var mTotal;
	
	// Determine gender
	gender = window.document.bodyfatform.genderMenu.selectedIndex;

        // Extract information measurement boxes
        height = window.document.bodyfatform.heightTextField.value - 0.0;
        neck = window.document.bodyfatform.neckTextField.value - 0.0;
        waistAtNavel = window.document.bodyfatform.waistAtNavelTextField.value - 0.0;
        waistBelowRibs = window.document.bodyfatform.waistBelowRibsTextField.value - 0.0;
        waistAtHips = window.document.bodyfatform.waistAtHipsTextField.value - 0.0;

	//Convert values from inches to centimeters
        height *= in2cm;
        neck *= in2cm;
        waistAtNavel *= in2cm;
        waistBelowRibs *= in2cm;
        waistAtHips *= in2cm;

        // Determine percent body fat for male
	if(gender == 0) {
 		density = -0.191 * logBase10(waistAtNavel - neck) +
			0.155 * logBase10(height) + 1.032;
		percentBodyFat = 100.0 * ((4.95 / density) - 4.5);
	}     

        // Determine percent body fat for female
	else {
		mTotal = waistBelowRibs + waistAtHips + neck;//=193.04
                density = -0.350 * logBase10(mTotal);//= -.35 * log(193.04) = -.79
		density += 0.221 * logBase10(height);//= .221 * log(172.2) = .494
		density += 1.296;//= -.79 + .494 + 1.296 = 1;
		percentBodyFat = 100.0 * ((4.95 / density) - 4.5);
	}

        // Make sure body fat is in valid range
	if(percentBodyFat < 1.0 || percentBodyFat > 200.00) {
		percentBodyFat = 0.0;
	}

        // Copy result to percent body fat text box
	percentBodyFat = Math.round(percentBodyFat * 100.0)/100.0;
        window.document.bodyfatform.bodyFatTextField.value = percentBodyFat;
}    

// -->
