
var bmiRanges = [
    {min_bmi:0.0, max_bmi:35.0, min_weight: null, element_id: {qc: 'bmiLT35', other: 'bmiLT35'}},
    {min_bmi:35.0, max_bmi:55.0, min_weight: null, element_id: {qc: 'bmiLT55', other: 'bmiLT55NFQ'}},
    {min_bmi:55.0, max_bmi:null, min_weight: 350.0, element_id: {qc: 'bmiGT55', other: 'bmiGT55NFQ'}}  
];


function calculateBMI(p_nHeight, p_nWeight)
{
    var fWeightKg = 0.45359237 * parseFloat(p_nWeight);
    var fHeightM = parseFloat(p_nHeight) * 2.54 / 100.0;
    return Math.round((fWeightKg / (fHeightM * fHeightM)) * 10) / 10;
}

function calculateIdealWeight(p_nHeight, p_bFemale)
{
    var nIdealWeight = 0;
    
    if (p_bFemale == true) {
        nIdealWeight = Math.round(100.0 + 5.0 * (parseFloat(p_nHeight) - 60.0));
    } else {
        nIdealWeight = Math.round(106.0 + 6.0 * (parseFloat(p_nHeight) - 60.0));
    }
    
    return nIdealWeight;
}

function getAssessment(p_fBMI)
{
    var sAssessment = 'Healthy';
    
    if (p_fBMI < 20.0) {
        sAssessment = 'Under Weight';
    } else if (p_fBMI >= 20.0 && p_fBMI < 25.0) {
        sAssessment = 'Healthy';
    } else if (p_fBMI >= 25.0 && p_fBMI < 30.0) {
        sAssessment = 'Slightly Overweight';
    } else if (p_fBMI >= 30.0 && p_fBMI < 35.0) {
        sAssessment = 'Obese';
    } else if (p_fBMI >= 35.0 && p_fBMI < 40.0) {
        sAssessment = 'Severely Obese';
    } else if (p_fBMI >= 40.0) {
        sAssessment = 'Morbidly Obese';
    }
    
    return sAssessment;
}


function hideAllDecisionTree()
{
    for (var i=0;i < bmiRanges.length; i++) {
        for (var key in bmiRanges[i].element_id) {
            var element = document.getElementById(bmiRanges[i].element_id[key]);
            if (element) {
                element.style.display = 'none';
            }
        }
    }
}



function expandBMIBlurb(fBMI, nWeight)
{
    hideAllDecisionTree();
    var range = null;
    var qc = document.getElementById('fromQuebec');
    
    for (var i=0;i < bmiRanges.length;i++) {
        with(bmiRanges[i]) {
            // if (fBMI > min_bmi && (min_weight == null || nWeight > min_weight) && (max_bmi == null || fBMI <= max_bmi)) {
            if (fBMI > min_bmi && (max_bmi == null || fBMI <= max_bmi)) {
                range = bmiRanges[i];
                break;
            }
        }
    }    
    
    if (range == null) {
        for (var i=0;i < bmiRanges.length;i++) {
            with(bmiRanges[i]) {
                if (min_weight != null && nWeight > min_weight) {
                    range = bmiRanges[i];
                    break;
                }
            }
        }
    }
    
    if (range != null) {
        var element_id;
        if (qc && qc.checked) {
            elementId = range.element_id.qc;
        } else {
            elementId = range.element_id.other;
        }
        var element = document.getElementById(elementId);        
        if (element) {
            element.style.display = 'block';
        } 
        
    } 
}

var submitPressed = false;

function updateBMIResults(submit)
{

    var nFeet = parseInt(document.bmiForm.heightFeet.options[document.bmiForm.heightFeet.selectedIndex].value);
    var nInches = parseInt(document.bmiForm.heightInches.options[document.bmiForm.heightInches.selectedIndex].value);
    var nWeight = parseInt(document.bmiForm.weight.value);
    var nCms = parseInt(document.bmiForm.heightCms.value);
    var nKgs = parseInt(document.bmiForm.weightKgs.value);
    var sGender = document.bmiForm.gender.options[document.bmiForm.gender.selectedIndex].value;
    var sMeasure = document.bmiForm.measurement.value;
    
    if ( !isNaN(nFeet) && !isNaN(nInches) && !isNaN(nWeight) && sGender != '' && sMeasure == "Imperial") {
        if (submit || submitPressed) {
            var nHeight = nFeet * 12 + nInches;		 		 
            var bFemale = sGender === 'Female';
            var nIdealWeight = calculateIdealWeight(nHeight, bFemale);
            var ideallow = Math.round(parseFloat(nIdealWeight) * 0.9);
            var idealhigh = Math.round(parseFloat(nIdealWeight) * 1.1);
            var idealmeasure = " lbs";
            var fBMI = calculateBMI(nHeight, nWeight);

            document.getElementById('bmi-div').style.display = 'block';
            
            document.getElementById('bmi-result').innerHTML = fBMI;
            document.getElementById('ideal-result').innerHTML = ideallow + " - " + idealhigh + idealmeasure;
            document.getElementById('assessment-result').innerHTML = getAssessment(fBMI);
            submitPressed = true;
            document.bmiForm.Button.disabled = false;
            window.setTimeout(function() {window.location.hash = 'bmiresult';}, 100);
        } else {
            document.bmiForm.Button.disabled = false;
        }
    } else {
        if ( !isNaN(nCms) && !isNaN(nKgs) && sGender != '' && (sMeasure == "Metric") ) {
            if (submit || submitPressed) {
                nHeight = nCms * 0.3937;
                nWeight = nKgs * 2.20462262;       
                var bFemale = sGender === 'Female';
                var nIdealWeight = calculateIdealWeight(nHeight, bFemale);
                var ideallow = Math.round(parseFloat(nIdealWeight) * 0.9 * 0.45359237 );
                var idealhigh = Math.round(parseFloat(nIdealWeight) * 1.1 * 0.45359237 );
                var idealmeasure = " kgs";
                var fBMI = calculateBMI(nHeight, nWeight);
                
                document.getElementById('bmi-div').style.display = 'block';
                
                document.getElementById('bmi-result').innerHTML = fBMI;
                document.getElementById('ideal-result').innerHTML = ideallow + " - " + idealhigh + idealmeasure;
                document.getElementById('assessment-result').innerHTML = getAssessment(fBMI);
                submitPressed = true;
                document.bmiForm.Button.disabled = false;            
                window.setTimeout(function() {window.location.hash = 'bmiresult';}, 100);                
            } else {            
                document.bmiForm.Button.disabled = false;            
            }
        } else {
            document.getElementById('bmi-result').innerHTML = '';
            document.getElementById('ideal-result').innerHTML = '';
            document.getElementById('assessment-result').innerHTML = '';
            hideAllDecisionTree();
            document.bmiForm.Button.disabled = true;            
        }
        
    }
    if (document.getElementById('bmi-result').innerHTML != '') {
        expandBMIBlurb(fBMI, nWeight);
    }
    
}

function resetBMIForm() {
    document.getElementById('bmi-results-container').style.display = 'none';
    hideAllDecisionTree();
//    document.bmiForm.Button.disabled = true;       
    submitPressed = false;
    $('#bmi-form :input').each(function() {
        $(this).val('');
    });
    return false;
}