﻿
function calcola(frm) {
    if(!checkDati(frm.h, "Inserire l'altezza in modo corretto!"))
        return false;

    if (!checkDati(frm.p, "Inserire il peso in modo corretto!"))
        return false;

    if (!checkDati(frm.anni, "Inserire l'età in modo corretto!"))
        return false;    

    var h = frm.h.value;
    var p = frm.p.value;
    var a = frm.anni.value;
    var s = frm.sesso.options[frm.sesso.selectedIndex].value

    var imc = arrotonda(indiceMassaCorporea(p, h), 1);
    var pi = arrotonda(pesoIdeale(h, s), 2);
    var asc = arrotonda(areaSuperficieCorporea(p, h), 2);
    var gc = arrotonda(grassoCorporeo(imc, a, s), 0);
    var fmb = arrotonda(fabbisognoMetabolicoBasale(p, h, s, a), 2);

    messaggio(esito(imc));
    frm.IMC.value = imc;
    frm.pi.value = pi;
    frm.asc.value = asc;
    frm.gc.value = gc;
    frm.fmb.value = fmb;   
}

function checkDati(ctrl, msg) {
    var val = Number(ctrl.value);
    if ((val == null) || (isNaN(val)) || (val == "") || (val <= 0)) {
        alert(msg);
        ctrl.select();
	    ctrl.focus();
        return false;
    }
    return true;
}

// Indice di massa corporea
// peso / (altezza*altezza)   N.B.: altezza in metri
function indiceMassaCorporea(p, h) {
    var hm = h / 100;
    return (p / (hm * hm));
}

// Peso Ideale : Formula di Lorenz
// Uomini: -> altezza in cm - 100 - (altezza - 150)/4 
// Donne:  -> altezza in cm - 100 - (altezza - 150)/2 
function pesoIdeale(h, s) {
    var x = 2;
    if (s == "m") x = 4;
    return (h - 100 - (h - 150) / x);
}

// Area Superficie Corporea
//[(Altezza * Peso / 3600)] elevato a 1/2
function areaSuperficieCorporea(p, h){
    return (Math.pow((h*p/3600), 0.5));
}

// Grasso corporeo
// (1.20 * IMC) + (0.23 * età) - (10.8 * sesso) - 5.4
function grassoCorporeo(imc, a, s) {
    var ss=0;
    if (s=="m") ss=1;
    return ((1.20 * imc) + (0.23 * a) - (10.8 * ss) - 5.4 );
}

// Fabbisogno metabolico basale
// Uomini: -> 66,473 + (13,7516 * Peso) + (5.0033 * Altezza) - (6,775 * Età)
// Donne:  -> 655.095 + (9,5634 * Peso) + (1,8496 * Altezza) - (4,6756 * Età)
// Bambini: -> 22.1 + (31.05 * Peso) + (1.165 * Altezza)
function fabbisognoMetabolicoBasale(p, h, s, a) {
    if (a < 13)
        return (22.1 + (31.05 * p) + (1.165 * h));
    if (s == "m")
        return (66.473 + (13.7516 * p) + (5.0033 * h) - (6.775 * a));
    else
        return (655.095 + (9.5634 * p) + (1.8496 * h) - (4.6756 * a));
}

function arrotonda(num, dec) {
    var div = Math.pow(10, dec);
    return Math.round(num * div) / div;
}

function esito(imc) {
    if (imc < 18.5) { return ("situazione di sottopeso"); } 
    if (imc < 25.0) { return ("situazione di peso ottimale"); }	        	
    if (imc < 30.0) { return ("situazione di sovrappeso"); }
    return ("situazione di obesit&agrave;");
}

function messaggio(msg) {
    document.getElementById("esito").innerHTML=msg;
}

function delMessaggio() {
    document.getElementById("esito").innerHTML = "&nbsp;";
    return true;
}
