// mets.js

// Calculate METs from displayed workloads on commonly used exercise equipment
// Copyright, 2003 - Frank J. Fedel, C.E.S. - frank@fedel.com - http://www.fedel.com/bio


// function to set cursor at first form input box - from irt.org
function setFocus() {
    document.forms[0].elements[0].focus();
}

// standard conversions
onemet=3.5;
mph_to_mmin=26.8;
vo2tomet=3.5;

// setup converstion factor values
// from ACSM formula
tmfactor=1.8;
tmhoriz=0.1;
armfactor=3;

// "Guesstimates" from other formulae; not validated
adworkfactor=2;
c2factor=3;
adfactor=300;
c2dist=6;
adwattsPerLoadSetting=50;

// information from equipment manufacturers
armdist=3;

// conversion information from regression equation on RPM to Load on Schwinn Airdyne
rpmSquaredFactor = 0.0034454;
rpmFactor = -0.159723;
rpmConstant = 2.37055;
rpmToWattsSquaredFactor = 0.172274;
rpmToWattsFactor = -7.98617;
rpmToWattsConstant = 118.527;

// factors for NuStep 4000 at Low (less than 115 watts) or High (115 watts or greater) work rates
nsFirstFactorLow=-171.8;
nsSecondFactorLow=6.61;
nsThirdFactorLow=6.725;
nsFirstFactorHigh=-8.67;
nsSecondFactorHigh=7.76;
nsThirdFactorHigh=0.0384;

// factors for NuStep 3000
nsFirstFactor=0.105;
nsSecondFactor=7.672;


// initialize variables
var adload=0;
var nustepWatts=0;
var nustepmet=0;


// do the work here...

function results() {
	// convert all information first
	convert();
	// do treadmill calculations
	treadmill();
	// do Airdyne calculations
	airdyne();
	// do arm ergometer calculations
	arm();
	// do NuStep calculations
	nustep();
}



// basic information conversions
function convert() {
	// get weight from form
	kg=document.exerciseForm.weight.value;
	// convert from lbs. to kg
	kg=kg/2.204;
}


function treadmill() {
// get speed and grade readings
spd=document.exerciseForm.treadmillSpeed.options[document.exerciseForm.treadmillSpeed.selectedIndex].value;
grade=document.exerciseForm.treadmillGrade.options[document.exerciseForm.treadmillGrade.selectedIndex].value;
grade/=100;

// perform calculations of work, vo2 and mets
mmin=spd*mph_to_mmin;
tmh=tmhoriz*mmin;
tmv=tmfactor*grade*mmin;
tmr=onemet;
tmvo2=tmv+tmh+tmr;
tmmet=tmvo2/vo2tomet;
tmmetOutput=parseInt(tmmet*10)/10;

// display output
document.exerciseForm.treadmillResult.value=tmmetOutput;
}



function airdyne() {
// get Airdyne Workload Setting from somewhere

rpm=document.exerciseForm.airdyneRPM.options[document.exerciseForm.airdyneRPM.selectedIndex].value;

watts=document.exerciseForm.airdyneWatts.options[document.exerciseForm.airdyneWatts.selectedIndex].value;

load=document.exerciseForm.airdyneLoad.options[document.exerciseForm.airdyneLoad.selectedIndex].value;


if (load) {
	adload=load;
	}


if (rpm) {
	adload=(rpmSquaredFactor * (rpm*rpm)) + (rpmFactor * rpm) + rpmConstant;
	}

if (watts) {
	adload = watts/adwattsPerLoadSetting;
	}

// perform calculations of work, vo2 and mets
adkgmmin=adload*adfactor;
adwork=adworkfactor*adkgmmin;
adr=vo2tomet*kg;
advo2=adwork+adr;
advo2=advo2/kg;
admet=advo2/vo2tomet;
admetOutput=parseInt(admet*10)/10;

// display output
document.exerciseForm.adResult.value=admetOutput;
}







function arm() {

// get load reading and RPM value
armset=document.exerciseForm.armErgometerSetting.options[document.exerciseForm.armErgometerSetting.selectedIndex].value;
armrpm=document.exerciseForm.armErgometerRPM.options[document.exerciseForm.armErgometerRPM.selectedIndex].value;

// perform calculations of work, vo2 and mets
armv=armset*armrpm*armdist*armfactor;
armr=onemet*kg;
armvo2=armv+armr;
armmet=armvo2/kg/vo2tomet;
armmetOutput=parseInt(armmet*10)/10;

// display output
document.exerciseForm.armResult.value=armmetOutput;
}






function nustep() {
// get Model and Watt values


for (var i=0; i < document.exerciseForm.nustepModel.length; i++)
   {
   if (document. exerciseForm.nustepModel[i].checked)
      {
      var nustepModel = document. exerciseForm.nustepModel[i].value;
      }
   }

nustepWatts=document.exerciseForm.nustepWatts.options[document.exerciseForm.nustepWatts.selectedIndex].value;

// perform calculations of METs
// Use appropriate formula for NuStep Model (NuStep 3000 or NuStep 4000)
// if neither model was selected, let user know
if (((nustepModel!=3)&&(nustepModel!=4))&& nustepWatts) {
	alert ("Please select a NuStep Model and Try Again");
	nustepmet=0;
}
if (nustepModel=="3") {
nustepmet=(((nustepWatts*nsFirstFactor)+ nsSecondFactor))/3.5;
}
if (nustepModel=="4") {
	if (!kg) {
		alert ("You must enter a body weight first");
	}
nustepmet=((((nsFirstFactorLow+(nsSecondFactorLow*kg))+(nsThirdFactorLow*nustepWatts))/kg/3.5)*(nustepWatts<115)) + ((((nsFirstFactorHigh+(nsSecondFactorHigh*kg))+(nsThirdFactorHigh*(nustepWatts*nustepWatts)))/kg/3.5)*(nustepWatts>=115));
}
nustepmetOutput=parseInt(nustepmet*10)/10;

// display output
document.exerciseForm.nustepResult.value=nustepmetOutput;
}