`;
}
function setSectionEnabled(key, checked) {
const card = document.getElementById(key + 'Card');
card.classList.toggle('disabled-section', !checked);
card.querySelectorAll('.field input, .field select, .toggle-btn').forEach(el => {
el.disabled = !checked;
});
if (key === 'bmi') {
checked ? calcBMI() : showPlaceholder('bmiResult', 'Tick the box above to enable the BMI calculator.');
} else if (key === 'nutrition') {
checked ? calcNutrition() : showPlaceholder('nutritionResult', 'Tick the box above to enable the nutrition calculator.');
} else if (key === 'fitness') {
checked ? calcFitness() : showPlaceholder('fitnessResult', 'Tick the box above to enable the fitness calculator.');
}
}
/* ---------- BMI ---------- */
function setBmiUnit(u) {
bmiUnit = u;
document.getElementById('bmiMetricBtn').classList.toggle('active', u === 'metric');
document.getElementById('bmiImperialBtn').classList.toggle('active', u === 'imperial');
document.getElementById('bmiHeightLabel').textContent = u === 'metric' ? 'Height (cm)' : 'Height (in)';
document.getElementById('bmiWeightLabel').textContent = u === 'metric' ? 'Weight (kg)' : 'Weight (lb)';
document.getElementById('bmiHeight').placeholder = u === 'metric' ? 'e.g. 170' : 'e.g. 67';
document.getElementById('bmiWeight').placeholder = u === 'metric' ? 'e.g. 65' : 'e.g. 143';
calcBMI();
}
function calcBMI() {
const h = parseFloat(document.getElementById('bmiHeight').value) || 0;
const w = parseFloat(document.getElementById('bmiWeight').value) || 0;
if (h <= 0 || w <= 0) { showPlaceholder('bmiResult', 'Enter your height and weight to see your BMI.'); return; }
const heightM = bmiUnit === 'metric' ? h / 100 : h * 0.0254;
const weightKg = bmiUnit === 'metric' ? w : w * 0.453592;
const bmi = weightKg / (heightM * heightM);
let category, cls;
if (bmi < 18.5) { category = 'Underweight'; cls = 'warn'; }
else if (bmi < 25) { category = 'Normal weight'; cls = 'up'; }
else if (bmi < 30) { category = 'Overweight'; cls = 'warn'; }
else { category = 'Obese'; cls = 'down'; }
const minKg = 18.5 * heightM * heightM;
const maxKg = 24.9 * heightM * heightM;
const fmtW = (kg) => bmiUnit === 'metric' ? kg.toFixed(1) + ' kg' : (kg / 0.453592).toFixed(1) + ' lb';
const fmtH = bmiUnit === 'metric' ? h + ' cm' : h + ' in';
const fmtWeightIn = bmiUnit === 'metric' ? w + ' kg' : w + ' lb';
document.getElementById('bmiResult').innerHTML = `
Your BMI
${bmi.toFixed(1)}
${category} — a healthy weight for your height is ${fmtW(minKg)} to ${fmtW(maxKg)}.
Height${fmtH}
Weight${fmtWeightIn}
BMI${bmi.toFixed(1)}
Category${category}
Healthy weight range${fmtW(minKg)} – ${fmtW(maxKg)}
`;
}
function resetBmi() {
document.getElementById('bmiHeight').value = '';
document.getElementById('bmiWeight').value = '';
calcBMI();
}
/* ---------- Nutrition ---------- */
function setNutGender(g) {
nutGender = g;
document.getElementById('nutMaleBtn').classList.toggle('active', g === 'male');
document.getElementById('nutFemaleBtn').classList.toggle('active', g === 'female');
calcNutrition();
}
function calcNutrition() {
const age = parseFloat(document.getElementById('nutAge').value) || 0;
const height = parseFloat(document.getElementById('nutHeight').value) || 0;
const weight = parseFloat(document.getElementById('nutWeight').value) || 0;
const activity = parseFloat(document.getElementById('nutActivity').value) || 0;
if (age <= 0 || height <= 0 || weight <= 0 || !activity) {
showPlaceholder('nutritionResult', 'Enter your age, height, weight and activity level to see your daily calorie needs.');
return;
}
const bmr = nutGender === 'male'
? (10 * weight + 6.25 * height - 5 * age + 5)
: (10 * weight + 6.25 * height - 5 * age - 161);
const tdee = bmr * activity;
const lose = tdee * 0.85;
const gain = tdee * 1.15;
const proteinKcal = tdee * 0.30, carbKcal = tdee * 0.40, fatKcal = tdee * 0.30;
const proteinG = proteinKcal / 4, carbG = carbKcal / 4, fatG = fatKcal / 9;
document.getElementById('nutritionResult').innerHTML = `
Daily maintenance calories
${Math.round(tdee).toLocaleString('en-IN')} kcal
Base metabolic rate (BMR): ${Math.round(bmr).toLocaleString('en-IN')} kcal/day at rest.
Protein — ${proteinG.toFixed(0)}g30%
Carbs — ${carbG.toFixed(0)}g40%
Fat — ${fatG.toFixed(0)}g30%
BMR${Math.round(bmr).toLocaleString('en-IN')} kcal/day
Maintenance (TDEE)${Math.round(tdee).toLocaleString('en-IN')} kcal/day
Mild weight loss${Math.round(lose).toLocaleString('en-IN')} kcal/day
Mild weight gain${Math.round(gain).toLocaleString('en-IN')} kcal/day
`;
}
function resetNutrition() {
document.getElementById('nutAge').value = '';
document.getElementById('nutHeight').value = '';
document.getElementById('nutWeight').value = '';
document.getElementById('nutActivity').value = '';
calcNutrition();
}
/* ---------- Fitness ---------- */
function calcFitness() {
const age = parseFloat(document.getElementById('fitAge').value) || 0;
const resting = parseFloat(document.getElementById('fitResting').value) || 0;
if (age <= 0 || resting <= 0) {
showPlaceholder('fitnessResult', 'Enter your age and resting heart rate to see your target heart rate zones.');
return;
}
const maxHR = 220 - age;
const reserve = maxHR - resting;
const zone = (lo, hi) => Math.round(reserve * lo + resting) + '–' + Math.round(reserve * hi + resting) + ' bpm';
document.getElementById('fitnessResult').innerHTML = `
Maximum heart rate
${Math.round(maxHR)} bpm
Training zones below are based on your heart rate reserve (Karvonen method).
Max heart rate${Math.round(maxHR)} bpm
Resting heart rate${resting} bpm
Light (50–60%)${zone(0.5, 0.6)}
Fat burn (60–70%)${zone(0.6, 0.7)}
Cardio (70–80%)${zone(0.7, 0.8)}
Peak (80–90%)${zone(0.8, 0.9)}
`;
}
function resetFitness() {
document.getElementById('fitAge').value = '';
document.getElementById('fitResting').value = '';
calcFitness();
}