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 compute(f) {

    if (!isNumber(f.Weight)) return false;

    weight = parseFloat(f.Weight.value, 1);
    sexe = f.Sexe.options[f.Sexe.selectedIndex].value;
    weight  = weight * sexe;

    sqt1 = weight * 2.10; sdt1 = weight * 2.63; dc1 = weight * 1.58; dm1 = weight * 1.05;
    sqt20 = weight * 1.58; sdt15 = weight * 2.03; sdtjt10 = weight * 1.58;

    dm6 = weight * 0.88; dc6 = weight * 1.34; dips6 = dc6 * 1.25;
    di6 = ( dc6 + dm6 ) / 2; dd6 = ( dc6 + dips6 ) / 2;
    dch6 = dc6 / 2 * 0.85; dh6 = dm6 / 2 * 0.85;

    dcs6 = di6 * 1.1; dn6 = dcs6 * 0.66;
    tractions6 = dc6; rowing6 = di6;
    curlp6 = dcs6 * 0.46; 
    curl6 = curlp6 * 1.125; curlh6 = curl6 / 2; curli6 = dcs6 * 0.35; bf6 = curl6 * 0.9; curlp6 = curlp6 * 0.95; curlhi6 = curlp6 / 2;

    cp = sqt1 * 0.77; snatch = cp * 0.8; frtsqt = cp * 1.11;
    pclean = cp * 0.82; psnt = snatch * 0.82;
    pjerk = cp * 0.95; ppull = cp * 1.35;

    var coeff = new Array(0.5, 0.67, 0.83, 1, 1.125, 1.25, 1.5);

    for ( i = 0 ; i <= 6 ; i++ ) {

        f["SQT1_" + i].value = round(sqt1 * coeff[i], 0);
        f["SDT1_" + i].value = round(sdt1 * coeff[i], 0);
        f["DC1_" + i].value = round(dc1 * coeff[i], 0);
        f["DM1_" + i].value = round(dm1 * coeff[i], 0);
        f["FRTSQT_" + i].value = round(frtsqt * coeff[i], 0);

        f["SQT20_" + i].value = round(sqt20 * coeff[i], 0);
        f["SDT15_" + i].value = round(sdt15 * coeff[i], 0);
        f["SDTJT10_" + i].value = round(sdtjt10 * coeff[i], 0);

        f["DM6_" + i].value = round(dm6 * coeff[i], 0);
        f["DI6_" + i].value = round(di6 * coeff[i], 0);
        f["DC6_" + i].value = round(dc6 * coeff[i], 0);
        f["DD6_" + i].value = round(dd6 * coeff[i], 0);
        f["DIPS6_" + i].value = round(dips6 * coeff[i] - weight, 0);
        f["DCS6_" + i].value = round(dcs6 * coeff[i], 0);
        f["DCH6_" + i].value = round(dch6 * coeff[i], 0);
        f["DN6_" + i].value = round(dn6 * coeff[i], 0);
        f["DH6_" + i].value = round(dh6 * coeff[i], 0);

        f["TRACTIONS6_" + i].value = round(tractions6 * coeff[i] - weight, 0);
        f["ROWING6_" + i].value = round(rowing6 * coeff[i], 0);
        f["CURL6_" + i].value = round(curl6 * coeff[i], 0);
        f["CURLH6_" + i].value = round(curlh6 * coeff[i], 0);
        f["CURLHI6_" + i].value = round(curlhi6 * coeff[i], 0);
        f["CURLP6_" + i].value = round(curlp6 * coeff[i], 0);
        f["CURLI6_" + i].value = round(curli6 * coeff[i], 0);
        f["BARREFRONT6_" + i].value = round(bf6 * coeff[i], 0);

        f["CP_" + i].value = round(cp * coeff[i], 0);
        f["SNT_" + i].value = round(snatch * coeff[i], 0);
        f["PCLEAN_" + i].value = round(pclean * coeff[i], 0);
        f["PSNT_" + i].value = round(psnt * coeff[i], 0);
        f["PJERK_" + i].value = round(pjerk * coeff[i], 0);
        f["PPULL_" + i].value = round(ppull * coeff[i], 0);

    }
}