﻿// JScript File

var divError
var divResults

var age
var gender
var activity

// height
var feetinches
var inches
var centims

// weight
var pounds
var kilos

var bmi

function doCalcs()
{  
    setAllVars()
    
    divResults = document.getElementById? document.getElementById("divResults") : document.all.divResults;
    divError = document.getElementById? document.getElementById("divError") : document.all.divError;

    divError.innerHTML = ""
    divResults.innerHTML = ""
        
    // check if the weight is a good value
    if (checkNumber(pounds) == true && checkNumber(age) == true){
    
        calcBMI()
        calcIdealWeight()
        calcCaloricNeeds()
        calcLean()
        
        divResults.innerHTML += "<small>Please note these calculations are based on averages.</small>"
    
    } else {
        divError.innerHTML += "Please enter a value in both the 'Your Weight' and 'Your Age' fields.";
    }   
}

function setAllVars()
{
    // set age
    var txtAge = document.getElementById? document.getElementById("txtAge") : document.all.txtAge;
    age = txtAge.value
    
    // set gender
    var radGenderMale = document.getElementById? document.getElementById("radGenderMale") : document.all.radGenderMale;   
    if (radGenderMale.checked == true){
        gender = 'male';
    } else {
        gender = 'female';
    }
    
    //set activity level
    var ddlActivity = document.getElementById? document.getElementById("ddlActivity") : document.all.ddlActivity;
    activity = eval(ddlActivity.options[ddlActivity.selectedIndex].value);
    
    // set height
    var ddlFeet = document.getElementById? document.getElementById("ddlFeet") : document.all.ddlFeet;
    var ddlInches = document.getElementById? document.getElementById("ddlInches") : document.all.ddlInches;
    
    inches = eval(ddlInches.options[ddlInches.selectedIndex].value);
    feetinches = eval((ddlFeet.options[ddlFeet.selectedIndex].value) * 12) + eval(inches);
    centims = feetinches * 2.54;
    
    // set weight
    var txtWeight = document.getElementById? document.getElementById("txtWeight") : document.all.txtWeight;
    pounds = txtWeight.value;
    if (checkNumber(pounds) == true) kilos = eval(pounds) / 2.2;
}

function checkNumber(intCheck)
{
    var blnCheck = false;
    
    if (isNaN(intCheck) || intCheck == null || intCheck == '') {
        blnCheck = false;
    } else {
        blnCheck = true;
    }
    return blnCheck;
}

function calcBMI()
{
    var heighSq = feetinches*feetinches

    bmi = eval(Math.ceil(703 * (pounds / heighSq)));
    var bmiCategory = '';
    
    if (bmi <= 16.5) bmiCategory = 'Starvation';
    if (bmi > 16.5 && bmi <= 18.5) bmiCategory = 'Underweight'; 
    if (bmi > 18.5 && bmi <= 25) bmiCategory = 'Normal'; 
    if (bmi > 25 && bmi <= 30) bmiCategory = 'Overweight'; 
    if (bmi > 30 && bmi <=35) bmiCategory = 'Obese'; 
    if (bmi > 35 && bmi <= 40) bmiCategory = 'Clinically Obese'; 
    if (bmi > 40) bmiCategory = 'Morbidly Obese'; 
    
    divResults.innerHTML += "<p>Your Body Mass Index is <strong>" + bmi + "</strong> which is categorized as <strong>" + bmiCategory + "</strong>.</p>"
}

function calcIdealWeight()
{
    var idealWeight;
    
    if (gender == 'male'){
        // boys 
        if(feetinches == 60){
            idealWeight = 106;
        }
        if((feetinches > 60) & (feetinches < 72)){
            idealWeight = 106 + (6*inches);
        }
        if(feetinches==72){
            idealWeight = 178;
        }
        if(feetinches>72){
            idealWeight = 178 + (6*inches);
        }
    } else {
        // girls
        if(feetinches == 60){
            idealWeight = 100;
        }
        if((feetinches > 60) & (feetinches < 72)){
            idealWeight = 100 + (5*inches);
        }
        if(feetinches == 72){
            idealWeight = 160;
        }
        if(feetinches > 72){
            idealWeight = 160 + (5*inches);
        }
    }
    
    divResults.innerHTML += "<p>Your Ideal Weight is about <strong>" + idealWeight + "lbs</strong>.</p>";
}

function calcCaloricNeeds()
{
    var rmr = (10 * kilos) + (6.25 * centims) - (5 * age);
    
    if  (gender == 'male'){
        starter = 66;
        thisWeight = (13.75 * kilos);
        thisHeight = 5.0 * centims;
        thisAge = 6.76 * age;
        rmr = rmr + 5;
    } else {
        starter = 655
        thisWeight = 9.56 * kilos;
        thisHeight = 1.85 * centims;
        thisAge = 4.68 * age;
        rmr = rmr - 161;
    }
    
    var bmr = (starter + thisWeight + thisHeight - thisAge);
        
    strHTML = "<p>Your Basal Metabolic Rate is <strong>" + Math.ceil(bmr) + "</strong>. "
    strHTML += "Your Resting Metabolic Rate is <strong>" + Math.ceil(rmr) + "</strong>. "
    strHTML += "Which means you need <strong>" + Math.ceil(bmr * activity) + "</strong>(bmr) <strong>" + Math.ceil(rmr * activity) + "</strong>(rmr) calories per day to maintain your current weight.</p>"
    
    divResults.innerHTML += strHTML
}

function calcLean()
{
    if (gender == 'male') {
        if (feetinches <= 64) {
            m = 1.6758;
            b = 75.188;
        }
        else if (feetinches > 64 && feetinches <= 68) {
            m = 1.9066;
            b = 85.026;
        }
        else if (feetinches > 68 && feetinches <= 72) {
            m = 2.1618;
            b = 94.934;
        }
        else if (feetinches > 72) {
            m = 2.4316;
            b = 105.71;
        } 
    } else {
        if (feetinches <= 60) {
            m = 1.3596;
            b = 53.456;
        }
        else if (feetinches > 60 && feetinches <= 64) {
            m = 1.6886;
            b = 60.149;
        }
        else if (feetinches > 64 && feetinches <= 68) {
            m = 1.9133;
            b = 68.811;
        }
        else if (feetinches > 68) {
            m = 2.1734;
            b = 76.674;
        }
    }
    
    var pf = m * bmi + b;
        
    if (pf<=99) {
        mealplan="A";
    }
    else if (pf>99 && pf<=124) {
        mealplan="B";
    }
    else if (pf>124 && pf<=149) {
        mealplan="C";
    }
    else if (pf>149) {
        mealplan="D";
    }
    
    divResults.innerHTML += "<p>Your Protein Factor is <strong>" + Math.round(pf) + "</strong> which means you need ShapeWorks Meal Plan <strong>" + mealplan + "</strong></p>"
    
    divResults.innerHTML += "<img src='../images/resources/meal_plan_" + mealplan + ".gif' alt='ShapeWorks Mealplan " + mealplan + "' /><br /><br />"
}