﻿<!-- // Copyright 2007 - Antonio Zamora
// These functions are used in EXERCISE.HTML
// Copyright 2007 - Antonio Zamora

function calc(){
  var f = document.forms[0]; // "heartrate" is first form
  var age, rhr, intensity, mhr, hrr, thr;
  age = parseFloat(f.age.value);
    if  (!chkw(age))  {     // age
	    alert("Please enter your age.");
     	return;
    }        
    if (  age < 12 || age > 115) {     
	    alert("Error in age.");
     	return;
    }  
  rhr = parseFloat(f.rhr.value);
    if  (!chkw(rhr))  {     // Resting Heart Rate
	    alert("Please enter your Resting Heart Rate.");
     	return;
    }        
    if (  rhr < 30 || rhr > 220) {     
	    alert("Error in Resting Heart Rate.");
     	return;
    }  
  intensity = parseFloat(f.intensity.value);

  //Karvonen formula.
  mhr = 220 - age; //  Maximum Heart Rate = 220 - Age
  hrr = mhr - rhr; //  Heart Rate Reserve = Maximum Heart Rate - Resting Heart Rate
  thr = intensity*hrr + rhr; //  Training Heart Rate = Intensity × Heart Rate Reserve + Resting Heart Rate 
  mhr = Math.floor(mhr+ 0.5);
  hrr = Math.floor(hrr+ 0.5);
  thr = Math.floor(thr+ 0.5);

  document.getElementById('mhr').innerHTML=mhr;
  document.getElementById('hrr').innerHTML=hrr;
  document.getElementById('thr').innerHTML=thr;

  return;
}

function clr(){
  var f = document.forms[0]; // "heartrate" is first form
  var age, rhr, intensity, mhr, hrr, thr;

  mhr = ""; //  Maximum Heart Rate = 220 - Age
  hrr = ""; //  Heart Rate Reserve = Maximum Heart Rate - Resting Heart Rate
  thr = ""; //  Training Heart Rate = Intensity × Heart Rate Reserve + Resting Heart Rate 

  document.getElementById('mhr').innerHTML=mhr;
  document.getElementById('hrr').innerHTML=hrr;
  document.getElementById('thr').innerHTML=thr;

  return;
}

// rounder(x) - provides precision of one decimal place.
function rounder(x) {
  var x1;
   x = x + 0.05;  // round to tenths
   f_bmi = Math.floor(x);
   diff  = Math.floor((x - f_bmi)*10);
   x1 = f_bmi + "." + diff;
  return(x1);
}

// chkw(w) - check input argument w to see if it is a numeric
function chkw(w){
  if (isNaN(parseFloat(w))){
    return false;
  } 
  if (isNaN(w)){
    return false;
  }  
  if (w < 0){
    return false;
  }
  return true;
}
