﻿function sccgovValidateHeightInches(inputvalue) {
    var heightInchesNumberPattern = /^([0-9]|[1][0-1])$/;
    return heightInchesNumberPattern.test(inputvalue);
}

function sccgovValidateHeightFeet(inputvalue) {
    var heightFeetNumberPattern = /^[0-9]$/;
    return heightFeetNumberPattern.test(inputvalue);
}

function sccgovValidateWeightPounds(inputvalue) {
    var weightNumberPattern = /^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$/;
    return weightNumberPattern.test(inputvalue);
}

function calculateBMI() {
    var bmiErrorCheck = "";
    var bmiHeightFeet = parseInt($("#bmi_input_height_feet").val()) * 12;
    var bmiHeightInches = parseInt($("#bmi_input_height_inches").val());
    var bmiWeightPounds = parseInt($("#bmi_input_weight_pounds").val());
    var bmiHeightInchesTotal = bmiHeightFeet + bmiHeightInches;
    var bmiHeightInchesTotalSquared = bmiHeightInchesTotal * bmiHeightInchesTotal;
    var bmiWeightPoundsTwo = bmiWeightPounds * 703;
    var bmi = bmiWeightPoundsTwo / bmiHeightInchesTotalSquared;
    var bmiRounded = Math.round(bmi * 100) / 100;
    if (sccgovValidateHeightFeet($("#bmi_input_height_feet").val()) == false) {
        bmiErrorCheck += "<li>Height (feet)</li>";
    }
    if (sccgovValidateHeightInches($("#bmi_input_height_inches").val()) == false) {
        bmiErrorCheck += "<li>Height (inches)</li>";
    }

    if (sccgovValidateWeightPounds($("#bmi_input_weight_pounds").val()) == false) {
        bmiErrorCheck += "<li>Weight (pounds)</li>";
    }

    if (bmiErrorCheck == "") {
        if (bmiRounded <= 18.5) {
            sccgovModalPopup("Body Mass Index (BMI) Calculator", "<div class=\"sccgov_events_container\">", "<p>Your BMI is <strong>" + bmiRounded + "</strong>. A BMI of <strong>18.5 or less</strong> is categorized as <strong>underweight</strong>.</p>", "<div id=\"sccgov_calendar_popup_bottom\"></div>", "</div>");
        }
        else if (bmiRounded >= 18.5 && bmiRounded <= 24.9) {
            sccgovModalPopup("BMI Calculator", "<div class=\"sccgov_events_container\">", "<p>Your body mass index is <strong>" + bmiRounded + "</strong>. A BMI <strong>between 18.5 and 24.9</strong> is categorized as <strong>normal weight</strong>.</p>", "<div id=\"sccgov_calendar_popup_bottom\"></div>", "</div>");
        }
        else if (bmiRounded >= 25 && bmiRounded <= 29.9) {
            sccgovModalPopup("BMI Calculator", "<div class=\"sccgov_events_container\">", "<p>Your body mass index is <strong>" + bmiRounded + "</strong>. A BMI <strong>between 25 and 29.9</strong> is categorized as <strong>overweight</strong>.</p>", "<div id=\"sccgov_calendar_popup_bottom\"></div>", "</div>");
        }
        else if (bmiRounded >= 30) {
            sccgovModalPopup("BMI Calculator", "<div class=\"sccgov_events_container\">", "<p>Your body mass index is <strong>" + bmiRounded + "</strong>. A BMI of <strong>30 or more</strong> is categorized as <strong>obese</strong>.</p>", "<div id=\"sccgov_calendar_popup_bottom\"></div>", "</div>");
        }
    }
    else {
        sccgovModalPopup("Please try again!", "<div class=\"sccgov_events_container\">", "<p>You entered an invalid value for:</p><ul>" + bmiErrorCheck + "</ul>", "<div id=\"sccgov_calendar_popup_bottom\"></div>", "</div>");
    }
}
