function replaceSubString(originalString, searchForString, replaceWithString) {
    var objRegExp = eval("/" + searchForString + "/g");
    return (originalString.replace(objRegExp, replaceWithString));
}

function isNumber(item, min, max) {

    item.value = replaceSubString(item.value, ",", ".");
    item.value = replaceSubString(item.value, " ", "");

    if (item.value.length == 0) {
        alert("Calcul interrompu : vous devez renseigner ce champ !");
        item.focus();
        return false;
    }

    if (!isNaN(item.value)) {
        if (min != null) if (parseFloat(item.value) < min) item.value = min;
        if (max != null) if (parseFloat(item.value) > max) item.value = max;
        return true;
    }

    alert("Calcul interrompu :\n\n\"" + item.value + "\" n'est pas un nombre valide !");
    item.focus();
    return false;
}

function round(number, decimal) {
    var factor = Math.pow(10, decimal);
    return (Math.round(number * factor) / factor);
}

function log(x) { // La fonction log en javascript equivaut en fait a ln.
    return (Math.log(x) / Math.log(10));
}

function compute1(f) {
    var weight = 0.0;
    var waist = 0.0;

    if (!isNumber(f.Weight, 1)) return false;
    if (!isNumber(f.Waist, 1)) return false;

    var weight = parseFloat(f.Weight.value);
    var waist = parseFloat(f.Waist.value);

    if (f.WaistUnit[0].checked) waist = waist / 2.54;

    if (f.WeightUnit[0].checked) weight = weight / 0.453592;

    if (f.Sexe[0].checked) f.BodyFat.value = round((-98.42 + 4.15 * waist - 0.082 * weight) / weight * 100, 0) + " %";
    else f.BodyFat.value = round((-76.76 + 4.15 * waist - 0.082 * weight) / weight * 100, 0) + " %";
}

function swithHeightUnit(f) {
    var unit = "";
    if (f.HeightUnit[1].checked) unit = f.HeightUnit[1].value;
    else unit = f.HeightUnit[0].value;

    f.HeightUnit1.value = unit;
    f.HeightUnit2.value = unit;
    f.HeightUnit3.value = unit;
    f.HeightUnit4.value = unit;
    f.HeightUnit5.value = unit;
}

function compute2(f) {
    var HEIGHT = 0.0;
    var NECK = 0.0;
    var ABD1 = 0.0;
    var ABD2 = 0.0;
    var HIP = 0.0;
    var bodyFat = 0.0;
    var tmp = 0.0;

    if (!isNumber(f.HEIGHT, 1)) return false;
    if (!isNumber(f.NECK, 1)) return false;

    NECK = parseFloat(f.NECK.value);
    HEIGHT = parseFloat(f.HEIGHT.value);

    if (f.Sexe[0].checked) { // Homme
        if (!isNumber(f.ABD2, 1)) return false;
        ABD2 = parseFloat(f.ABD2.value);
    } else { // Femme
        if (!isNumber(f.ABD1, 1)) return false;
        if (!isNumber(f.HIP, 1)) return false;
        ABD1 = parseFloat(f.ABD1.value);
        HIP = parseFloat(f.HIP.value);
    }

    if (f.HeightUnit[1].checked) {
        ABD1 = ABD1 * 2.54;
        ABD2 = ABD2 * 2.54;
        HIP = HIP * 2.54;
        NECK = NECK * 2.54;
        HEIGHT = HEIGHT * 2.54;
    }

    if (f.Sexe[0].checked) {
        tmp = ABD2 - NECK;
        bodyfat = (495 / (1.0324 - 0.19077 * (log(tmp)) + 0.15456 * (log(HEIGHT))) - 450);
    } else {
        tmp = ABD1 + HIP - NECK;
        bodyfat = (495 / (1.29579 - 0.35004 * (log(tmp)) + 0.22100 * (log(HEIGHT))) - 450);
    }
    f.BodyFat.value = round(bodyfat, 0) + " %";
}


function compute3(f) {
    var height = 0.0;
    var weight = 0.0;

    if (!isNumber(f.Height, 1)) return false;
    if (!isNumber(f.Weight, 1)) return false;

    var height = parseFloat(f.Height.value);
    var weight = parseFloat(f.Weight.value);

    if (f.WeightUnit[1].checked) weight = weight * 0.453592;

    f.IMC.value = round(weight / ((height / 100) * (height / 100)), 1);
}
