﻿function CheckDigitsAfterDecimal(objControl, DigitsAfterDecimal) {

    var val = objControl.value;
    if (val.indexOf(".") > -1) {
        if (val.length - (val.indexOf(".") + 1) > DigitsAfterDecimal) {
            return false;
        }
        else {
            if (val.length - (val.indexOf(".")) > DigitsAfterDecimal) {
                return false;
            }
        }
    }
}

//function NumericOnly(evt){
//    var e = event || evt;     // for trans-browser compatibility
//    var charCode = e.which || e.keyCode;
//    if (charCode > 31 && (charCode < 48 || charCode > 57))
//        return false;
//}

function ButtonTextChange() {
    if (document.getElementById("RadioBMR").checked) {
        //Change the Text of Submit button
        document.getElementById("Button1").value = "Calculate BMR";
    }
    else if (document.getElementById("RadioBMI").checked) {
        //Change the Text of Submit button
        document.getElementById("Button1").value = "Calculate BMI";
    }
}

function PageValidations() {
    if (document.getElementById("TextAge").value == "") {
        alert("Please enter Age");
        return false;
    }

    if (document.getElementById("TextAge").value != "") {
        if (isNaN(document.getElementById("TextAge").value)) {
            alert("Please enter correct Age");
            document.getElementById("TextAge").value = "";
            document.getElementById("TextAge").focus();
            return false;
        }
    }

    if (document.getElementById("TextHeight").value == "") {
        alert("Please enter Height");
        return false;
    }

    if (document.getElementById("TextHeight").value != "") {
        if (isNaN(document.getElementById("TextHeight").value)) {
            alert("Please enter Correct Height");
            document.getElementById("TextHeight").value = "";
            document.getElementById("TextHeight").focus();
            return false;
        }
    }

    if (document.getElementById("TextWeight").value == "") {
        alert("Please enter Weight");
        return false;
    }

    if (document.getElementById("TextWeight").value != "") {
        if (isNaN(document.getElementById("TextWeight").value)) {
            alert("Please enter Correct Weight");
            document.getElementById("TextWeight").value = "";
            document.getElementById("TextWeight").focus();
            return false;
        }
    }
    Calculate();
}

function Calculate() {
    var CalcType = document.getElementById("RadioImperal");
    var CalculationFor = document.getElementById("RadioBMR");
    var RdGender = document.getElementById("RadioMale");
    var DivResult = document.getElementById("divDisplayResult");
    var BMRCal = 0;
    var BMICal = 0;

    document.getElementById("divDisplayResult").style.display = "block";
    if (CalcType.checked) {
        BMICal = CalculateBMI("Imperal");
        if (RdGender.checked) {
            BMRCal = CalculateBMR("Imperal", "Male");
        }
        else {
            BMRCal = CalculateBMR("Imperal", "Women");
        }
    }
    else {
        BMICal = CalculateBMI("Metric");
        if (RdGender.checked) {

            BMRCal = CalculateBMR("Metric", "Male");
        }
        else {
            BMRCal = CalculateBMR("Metric", "Women");
        }
    }

    //Store the BMR value for further use 
    document.getElementById("HiddenBMRValue").value = BMRCal;

    //Show result in div
    if (CalculationFor.checked) {
        DivResult.innerHTML = "BMR = " + BMRCal.toFixed(2);

    }
    else {
        if (BMICal <= 18.5) {
            DivResult.innerHTML = "BMI = " + BMICal.toFixed(2);
            DivResult.innerHTML += "\n A lean BMI can indicate that your weight maybe too low.";
            DivResult.innerHTML += "You should consult your physician to determine if you should gain weight,";
            DivResult.innerHTML += "as low body mass can decrease your body's immune system, which could lead to illness.";
        }
        else if (BMICal > 18.5 && BMICal <= 24.9) {
            DivResult.innerHTML = "BMI = " + BMICal.toFixed(2);
            DivResult.innerHTML += "\n People whose BMI is within 18.5 to 24.9 possess the ideal amount of body weight, ";
            DivResult.innerHTML += " associated with living longest, the lowest incidence of serious illness, ";
            DivResult.innerHTML += " as well as being perceived as more physically attractive people than persons with BMI in higher or lower ranges.";
        }
        else if (BMICal > 25 && BMICal <= 30) {
            DivResult.innerHTML = "BMI = " + BMICal.toFixed(2);
            DivResult.innerHTML += "\n Persons falling in this BMI range are considered overweight and would benefit from finding healthyways to lower their weight, ";
            DivResult.innerHTML += "  such as diet and exercise. Individuals who fall in this range are at increased risk for a variety of illnesses. ";
        }
        else if (BMICal > 30) {
            DivResult.innerHTML = "BMI = " + BMICal.toFixed(2);
            DivResult.innerHTML += "\n Individuals with a BMI over 30 are in a physically unhealthy condition, "
            DivResult.innerHTML += "which puts them at risk for serious illnesses such as heart disease, diabetes, high blood pressure, "
            DivResult.innerHTML += "gall bladder disease, and some cancers. These persons would benefit greatly by modifying their lifestyle."
        }

    }
}

function CalculateBMI(calctype) {
    var Age = document.getElementById("TextAge").value;
    var Weight = document.getElementById("TextWeight").value;
    var Height = document.getElementById("TextHeight").value;
    var BMI = 0;

    if (calctype == "Metric") {
        // 1 meter = 100cm
        BMI = Weight / ((Height / 100) * (Height / 100));
    }
    else {
        // 1 Kg = 2.2 pounds
        // 1 inches = 2.54cms
        BMI = ((Weight * 2.2) / ((Height / 2.54) * (Height / 2.54))) * 703;
    }
    return BMI;
}

function CalculateBMR(calctype, gender) {
    var Age = document.getElementById("TextAge").value;
    var Weight = document.getElementById("TextWeight").value;
    var Height = document.getElementById("TextHeight").value;
    var BMR = 0;

    if (calctype == "Metric") {
        // 1 meter = 100cm
        if (gender == "Women") {
            BMR = 655 + (9.6 * Weight) + (Height / 100) - Age;
        }
        else {
            BMR = 66 + (13.7 * Weight) + (5 * (Height / 100)) - (6.8 * Age);
        }
    }
    else {
        // 1 Kg = 2.2 pounds
        // 1 inches = 2.54cms
        if (gender == "Women") {
            BMR = 655 + (4.35 * (Weight * 2.2)) + (4.7 * (Height / 2.54)) - (4.7 * Age);
        }
        else {
            BMR = 66 + (6.23 * (Weight * 2.2)) + (12.7 * (Height / 2.54)) - (6.8 * Age);
        }
    }

    return BMR;
}


function CalculateDailyCalorficNeed() {


    var Displaystyle = document.getElementById("divDailyCal");

    var BMRValue = parseFloat(document.getElementById("ctl00$ContentPlaceHolder1$HiddenFieldBMR").value);
    var CaloryResult = 0;
    if (document.getElementById("radio1").checked) {
        CaloryResult = (BMRValue * 1.2);
    }
    else if (document.getElementById("radio2").checked) {
        CaloryResult = (BMRValue * 1.375);
    }
    else if (document.getElementById("radio3").checked) {
        CaloryResult = (BMRValue * 1.55);
    }
    else if (document.getElementById("radio4").checked) {
        CaloryResult = (BMRValue * 1.725);
    }
    else if (document.getElementById("radio5").checked) {
        CaloryResult = (BMRValue * 1.9);
    }

    if (CaloryResult > 0) {


        Displaystyle.innerHTML = "Daily Calories Required = " + roundVal(CaloryResult,0) + " Kcal";
    }
    return false;
}


function CalculateBMIForHealth() {

    var TextBMI = document.getElementById("ctl00_ContentPlaceHolder1_TextBoxCalBmi");
    var Weight = document.getElementById("ctl00_ContentPlaceHolder1_TextBoxWeight").value;
    var Height = document.getElementById("ctl00_ContentPlaceHolder1_TextBoxHeight").value;
    var BMI = 0;


    if (Weight != "" && Height != "") {

        // 1 meter = 100cm
        BMI = Weight / ((Height / 100) * (Height / 100));

        TextBMI.value = '' + roundVal(BMI, 1);
    }


}


function roundVal(val, dec) {

    var result = Math.round(val * Math.pow(10, dec)) / Math.pow(10, dec);

    return result.toFixed(dec);

}


function CalculateWaist() {
    var TextBMI = document.getElementById("DivRatioResult");
    var TextConculsion = document.getElementById("DivConculion");
    var Waist = document.getElementById("TextboxWaistCircum").value;
    var Hips = document.getElementById("TextboxHipCircum").value;

    var Gender = document.getElementById("ctl00$ContentPlaceHolder1$HiddenFieldGender").value;
    var BMI = 0;

    if (Waist == "") {
        alert("Please enter Waist Circumference.");
        return false;

    }
    if (Hips == "") {
        alert("Please enter Hip Circumference.");
        return false;
    }


    if (Waist != "" && Hips != "") {

        // 1 meter = 100cm
        BMI = Waist / Hips;

        if (Gender == "M") {

            if (BMI <= 0.95)
                TextConculsion.innerHTML = "Health Risk Based Solely on WHR : Low Risk . ";
            else if (BMI > 0.95 && BMI <= 1.0)
                TextConculsion.innerHTML = "Health Risk Based Solely on WHR : Moderate Risk . ";
            else
                TextConculsion.innerHTML = "Health Risk Based Solely on WHR : High Risk . ";
        }
        else {


            if (BMI <= 0.80)
                TextConculsion.innerHTML = "Health Risk Based Solely on WHR : Low Risk . ";
            else if (BMI > 0.80 && BMI <= 0.85)
                TextConculsion.innerHTML = "Health Risk Based Solely on WHR : Moderate Risk . ";
            else
                TextConculsion.innerHTML = "Health Risk Based Solely on WHR : High Risk . ";
        }

        TextBMI.innerHTML = 'Waist to Hip Ratio = ' + roundVal(BMI, 2);
    }
    return false;
}

function IsNumeric(sNewText) {
    var sText = sNewText.value;


    var ValidChars = ".0123456789";
    var IsNumber = true;
    var Char;
    for (var i = 0; i < sText.length && IsNumber == true; i++) {
        if (sText.charAt(0) == "0") {
            IsNumber = false;
            sNewText.value = '';
            sNewText.focus();
            alert("Number cannot start with 0!");
        }
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {

            IsNumber = false;
            sNewText.value = '';
            alert("Enter number only");

        }
    }

    return IsNumber;
}

