var startedZero = true;
var units = 0;
function loadjscssfile(filename, filetype){
if (filetype==”js”){ //if filename is a external JavaScript file
var fileref=document.createElement(‘script’)
fileref.setAttribute(“type”,”text/javascript”)
fileref.setAttribute(“src”, filename)
}
else if (filetype==”css”){ //if filename is an external CSS file
var fileref=document.createElement(“link”)
fileref.setAttribute(“rel”, “stylesheet”)
fileref.setAttribute(“type”, “text/css”)
fileref.setAttribute(“href”, filename)
}
if (typeof fileref!=”undefined”)
document.getElementsByTagName(“head”)[0].appendChild(fileref)
}
function Calc()
{
var genderFemale = document.getElementById(“genderFemale”).checked;
var genderMale = document.getElementById(“genderMale”).checked;
var weightStr = document.getElementById(“weight”).value;
var heightft = document.getElementById(“heightft”).value;
var heightin = document.getElementById(“heightin”).value;
var ageStr = document.getElementById(“age”).value;
var age = parseFloat(ageStr);
var BMR = 0;
var minCalories = ‘1200’;
var weightkg = 0;
var heightcc = 0;
if (units == 0)
{
var weight = parseFloat(weightStr);
var height = (parseFloat(heightft) * 12) + parseFloat(heightin);
weightkg = Math.round(weight / 2.2,2);
heightcc = Math.round(height * 2.54,2);
}
else
{
weightkg = parseFloat(weightStr);
heightcc = parseFloat(heightft);
}
if (genderFemale)
{
BMR = (10 * weightkg) + (6.25 * heightcc) – (5 * age) – 161;
}
else
{
BMR = (10 * weightkg) + (6.25 * heightcc) – (5 * age) + 5;
minCalories = ‘1500’;
}
var sedentary = document.getElementById(“sedentary”).checked;
var light = document.getElementById(“light”).checked;
var moderately = document.getElementById(“moderately”).checked;
var veryactive = document.getElementById(“veryactive”).checked;
var extraactive = document.getElementById(“extraactive”).checked;
var TDEE = 0;
if (sedentary)
{
TDEE = BMR * 1.2;
}
if (light)
{
TDEE = BMR * 1.375;
}
if (moderately)
{
TDEE = BMR * 1.55;
}
if (veryactive)
{
TDEE = BMR * 1.725;
}
if (extraactive)
{
TDEE = BMR * 1.9;
}
var tdeeInt = Math.floor(TDEE);
var tdeeMaintain = tdeeInt;
var tdeeLoseOnePound = tdeeInt – 500;
var tdeeLoseOneHalfPound = tdeeInt – 750;
var tdeeGainHalfPound = tdeeInt + 250;
var caloriesDesired = document.getElementById(“caloriegoal”).value;
var caloriesDesiredInt = parseFloat(caloriesDesired);
var proteinPerc35 = document.getElementById(“35PercProtein”).checked;
var proteinPerc30 = document.getElementById(“30PercProtein”).checked;
var proteinPerc25 = document.getElementById(“25PercProtein”).checked;
var fatPerc20 = document.getElementById(“20PercFat”).checked;
var fatPerc25 = document.getElementById(“25PercFat”).checked;
var fatPerc30 = document.getElementById(“30PercFat”).checked;
var fatPerc20Int = parseFloat(fatPerc20);
var fatPerc25Int = parseFloat(fatPerc25);
var fatPerc30Int = parseFloat(fatPerc30);
var proteinGramsPerDay = 0;
var fatGramsPerDay = 0;
var proteinPerc = 0;
var fatPerc = 0;
if ( proteinPerc35)
{
proteinPerc = .35;
}
if ( proteinPerc30)
{
proteinPerc = .30;
}
if ( proteinPerc25)
{
proteinPerc = .25;
}
if ( fatPerc20)
{
fatPerc = .20;
}
if ( fatPerc25)
{
fatPerc = .25;
}
if ( fatPerc30)
{
fatPerc = .30;
}
proteinGramsPerDay = Math.round((caloriesDesiredInt * proteinPerc) / 4);
fatGramsPerDay = Math.round((caloriesDesiredInt * fatPerc) / 9);
var carbsGramsPerDay = Math.round((1 – (proteinPerc + fatPerc)) * caloriesDesiredInt);
carbsGramsPerDay = Math.round(carbsGramsPerDay / 4);
if ((weight > 0) && (height > 0) && (age > 0) & (startedZero))
{
caloriesDesiredInt = tdeeLoseOnePound;
if ((genderFemale) && (caloriesDesired < 1200))
caloriesDesiredInt = 1200;
if ((genderMale) && (caloriesDesired < 1500))
caloriesDesiredInt = 1500;
var control = document.getElementById(“caloriegoal”);
control.value = tdeeLoseOnePound;
}
document.getElementById(“maintainweight”).textContent=tdeeMaintain + ‘ calories’;
document.getElementById(“loseonepound”).textContent=tdeeLoseOnePound + ‘ calories’;
document.getElementById(“loseonehalfpound”).textContent=tdeeLoseOneHalfPound + ‘ calories’;
document.getElementById(“gainonehalfpound”).textContent=tdeeGainHalfPound + ‘ calories’;
document.getElementById(“proteingramsperday”).textContent=proteinGramsPerDay + ‘ grams’;
document.getElementById(“fatgramsperday”).textContent=fatGramsPerDay + ‘ grams’;
document.getElementById(“carbsgramsperday”).textContent=carbsGramsPerDay + ‘ grams’;
document.getElementById(“caloriemin”).textContent=’Do not go under ‘ + minCalories + ‘ calories’;
}
function Load()
{
loadjscssfile(“https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css”, “css”);
loadjscssfile(“https://code.jquery.com/jquery-3.3.1.slim.min.js”,”js”);
loadjscssfile(“https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js”,”js”);
var text = ‘
Macro Calculator
‘;
var control = document.getElementById(“macrocalc”);
control.innerHTML = text;
}
function StopAuto()
{
startedZero = false;
}
function ChangeUnits(unit)
{
switch (unit)
{
case 0:
var control = document.getElementById(“weightunits”);
control.innerHTML = ‘lbs.’;
control = document.getElementById(“heightunits”);
control.innerHTML = ‘feet’;
control = document.getElementById(“heightin”);
control.style.visibility = “visible”;
control = document.getElementById(“heightindiv”);
control.style.visibility = “visible”;
if (units == 1)
{
var weightInKg = document.getElementById(“weight”).value;
var weightInLbs = Math.round(parseFloat(weightInKg * 2.20462),1);
document.getElementById(“weight”).value = weightInLbs;
var heightInCM = parseInt(document.getElementById(“heightft”).value);
var heightinIn = Math.round(heightInCM / 2.54);
var heightinFt = Math.round(parseInt(heightinIn /12));
var heightinIn = heightinIn % 12;
document.getElementById(“heightft”).value = heightinFt;
document.getElementById(“heightin”).value = heightinIn;
}
break;
case 1:
var control = document.getElementById(“weightunits”);
control.innerHTML = ‘kg.’;
control = document.getElementById(“heightunits”);
control.innerHTML = ‘cm.’;
control = document.getElementById(“heightin”);
control.style.visibility = “hidden”;
control = document.getElementById(“heightindiv”);
control.style.visibility = “hidden”;
if (units == 0)
{
var weightInLbs = document.getElementById(“weight”).value;
var weightInKg = Math.round(parseFloat(weightInLbs * .453592),2);
document.getElementById(“weight”).value = weightInKg;
var heightInFt = parseInt(document.getElementById(“heightft”).value);
var heightInIn = parseInt(document.getElementById(“heightin”).value);
var totalInches = (heightInFt * 12) + heightInIn;
var heightinFt = Math.round(parseFloat(totalInches * 2.54));
document.getElementById(“heightft”).value = heightinFt;
}
break;
}
units = unit;
}
Load();