function twoDecimalPlaces(aValue) {
    var numberPart;
    var decimalPart;
    var decimalPartDigitOne;
    var decimalPartDigitTwo;
    var decimalPartDigitThree;

    offset = aValue.toString().indexOf(".");


    if (parseInt(offset) > 0) {
        decimalPart = aValue.toString().substring(aValue.toString().indexOf(".") + 1, aValue.toString().length);
        if (decimalPart.length < 3) {
            if (decimalPart.length < 2) {
                decimalPart = decimalPart + "00";
            }
            else {
                decimalPart = decimalPart + "0";
            }
        }
        numberPart = aValue.toString().substr(0, aValue.toString().indexOf("."));
    }
    else {
        numberPart = aValue.toString();
        decimalPart = "000";
    }

    decimalPartDigitOne = decimalPart.toString().substr(0, 1);
    decimalPartDigitTwo = decimalPart.toString().substr(1, 1);
    decimalPartDigitThree = decimalPart.toString().substr(2, 1);

    if (parseInt(decimalPartDigitThree) >= 5) {
        decimalPartDigitTwo = parseInt(decimalPart.toString().substr(1, 1)) + 1;
        if (parseInt(decimalPartDigitTwo) >= 10) {
            decimalPartDigitTwo = parseInt(decimalPartDigitTwo) - 10;
            decimalPartDigitOne = parseInt(decimalPartDigitOne) + 1;
            if (parseInt(decimalPartDigitOne) >= 10) {
                decimalPartDigitOne = parsInt(decimalPartDigitOne) - 10;
                numberPart = parseInt(numberPart) + 1;
            }
        }
    }

    aValue = numberPart + "." + decimalPartDigitOne + decimalPartDigitTwo;
    return aValue;
}

var waistWristWeightArray = new Array();

waistWristWeightArray = [
[120, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 50, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[125, 4, 6, 7, 9, 11, 13, 15, 17, 19, 20, 22, 24, 26, 28, 30, 32, 33, 35, 37, 39, 41, 43, 45, 46, 48, 50, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[130, 3, 5, 7, 9, 11, 12, 14, 16, 18, 20, 21, 23, 25, 27, 28, 30, 32, 34, 36, 37, 39, 41, 43, 44, 46, 48, 50, 52, 53, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[135, 3, 5, 7, 8, 10, 12, 13, 15, 17, 19, 20, 22, 24, 26, 27, 29, 31, 32, 34, 36, 38, 39, 41, 43, 44, 46, 48, 50, 51, 53, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[140, 3, 5, 6, 8, 10, 11, 13, 15, 16, 18, 19, 21, 23, 24, 26, 28, 29, 31, 33, 34, 36, 38, 39, 41, 43, 44, 46, 48, 49, 51, 53, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[145, 0, 4, 6, 7, 9, 11, 12, 14, 15, 17, 19, 20, 22, 23, 25, 27, 28, 30, 31, 33, 35, 36, 38, 39, 41, 43, 44, 46, 47, 49, 51, 52, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[150, 0, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[155, 0, 4, 5, 6, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[160, 0, 4, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 48, 50, 51, 53, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[165, 0, 3, 5, 6, 8, 9, 10, 12, 13, 15, 16, 17, 19, 20, 22, 23, 24, 26, 27, 29, 30, 31, 33, 34, 36, 37, 38, 40, 41, 43, 44, 45, 47, 48, 50, 51, 52, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[170, 0, 3, 4, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33, 34, 36, 37, 39, 40, 41, 43, 44, 45, 47, 48, 49, 51, 52, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[175, 0, 0, 4, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 19, 20, 21, 23, 24, 25, 27, 28, 29, 31, 32, 33, 35, 36, 37, 39, 40, 41, 43, 44, 45, 47, 48, 49, 51, 52, 53, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[180, 0, 0, 4, 5, 7, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 28, 30, 31, 32, 34, 35, 36, 37, 39, 40, 41, 43, 44, 45, 47, 48, 49, 50, 52, 53, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[185, 0, 0, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 23, 24, 25, 26, 28, 29, 30, 31, 33, 34, 35, 36, 38, 39, 40, 41, 43, 44, 45, 46, 48, 49, 50, 51, 53, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[190, 0, 0, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 30, 32, 33, 34, 35, 37, 38, 39, 40, 41, 43, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[195, 0, 0, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[200, 0, 0, 3, 4, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 48, 50, 51, 52, 53, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[205, 0, 0, 0, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0],
[210, 0, 0, 0, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 0, 0, 0, 0, 0, 0, 0],
[215, 0, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 0, 0, 0, 0, 0],
[220, 0, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 0, 0, 0, 0],
[225, 0, 0, 0, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 0, 0, 0],
[230, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 0, 0],
[235, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 51, 52, 53, 54, 55],
[240, 0, 0, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54],
[245, 0, 0, 0, 0, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53],
[250, 0, 0, 0, 0, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 44, 45, 46, 47, 48, 49, 50, 51, 52],
[255, 0, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 24, 25, 27, 27, 28, 29, 30, 31, 32, 33, 34, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 44, 45, 46, 47, 48, 49, 50, 51],
[260, 0, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, 27, 28, 29, 30, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47, 48, 49, 50],
[265, 0, 0, 0, 0, 0, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 18, 19, 20, 21, 22, 22, 23, 24, 25, 26, 27, 28, 29, 29, 30, 31, 32, 33, 34, 35, 36, 36, 37, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47, 48, 49],
[270, 0, 0, 0, 0, 0, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 13, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47, 48],
[275, 0, 0, 0, 0, 0, 4, 5, 5, 6, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 16, 16, 17, 18, 19, 20, 21, 22, 22, 23, 24, 25, 26, 27, 27, 28, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46, 47],
[280, 0, 0, 0, 0, 0, 4, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 24, 25, 26, 27, 28, 29, 29, 30, 31, 32, 33, 33, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 43, 44, 45, 46],
[285, 0, 0, 0, 0, 0, 4, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 26, 26, 27, 28, 29, 30, 30, 31, 32, 33, 34, 34, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44, 45],
[290, 0, 0, 0, 0, 0, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11, 11, 12, 13, 14, 15, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 23, 24, 25, 26, 27, 27, 28, 29, 30, 31, 31, 32, 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43, 44],
[295, 0, 0, 0, 0, 0, 3, 4, 5, 6, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 14, 15, 16, 17, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 25, 26, 27, 28, 28, 29, 30, 31, 32, 32, 44, 34, 35, 36, 36, 37, 38, 39, 39, 40, 41, 42, 43, 43],
[300, 0, 0, 0, 0, 0, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, 29, 30, 31, 32, 44, 33, 34, 35, 36, 36, 37, 38, 39, 39, 40, 41, 42, 43]
		      ];

var waistMinusWristIndex = new Array();
waistMinusWristIndex =
[22,
22.5,
23,
23.5,
24,
24.5,
25,
25.5,
26,
26.5,
27,
27.5,
28,
28.5,
29,
29.5,
30,
30.5,
31,
31.5,
32,
32.5,
33,
33.5,
34,
34.5,
35,
35.5,
36,
36.5,
37,
37.5,
38,
38.5,
39,
39.5,
40,
40.5,
41,
41.5,
42,
42.5,
43,
43.5,
44,
44.5,
45,
45.5,
46,
46.5,
47,
47.5,
48,
48.5,
49,
49.5,
50]

function displayArrayValue() {
    alert("Value at:" + waistMinusWristIndex[56])
}

function calcBodyFatValue(emailaddress) {
    var weightValue = parseFloat(document.getElementById("txtWeight").value);
    weightValueDiv = weightValue / 5;
    weightValueDivInt = parseInt(weightValueDiv);
    weightValueDivResult = weightValueDiv - weightValueDivInt;

    if (weightValue < 120) {
        weightValue = 120;
    }
    else if (weightValue > 300) {
        weightValue = 300;
    }

    else if (weightValueDivResult != 0) {
        if (weightValueDivResult >= 0.5) {
            weightValue = (weightValueDivInt * 5) + 5;
        }
        else {
            weightValue = (weightValueDivInt * 5);
        }
    }

    document.getElementById("txtWeight").value = weightValue;


    var waistMinusWristValue = parseFloat(document.getElementById("txtHips").value) - parseFloat(document.getElementById("txtAbdomen").value);


    waistMinusWristValueDiv = waistMinusWristValue / 0.5;
    waistMinusWristValueDivInt = parseInt(waistMinusWristValueDiv);
    waistMinusWristValueDivResult = waistMinusWristValueDiv - waistMinusWristValueDivInt;

    if (waistMinusWristValue < 22) {
        waistMinusWristValue = 22;
    }
    else if (waistMinusWristValue > 50) {
        waistMinusWristValue = 50;
    }

    else

        if (waistMinusWristValueDivResult != 0) {
        if (waistMinusWristValueDivResult >= 0.5) {
            waistMinusWristValue = (waistMinusWristValueDivInt * 0.5) + 0.5;
        }
        else {
            waistMinusWristValue = (waistMinusWristValueDivInt * 0.5);
        }
    }

    //  document.getElementById("Waist").value=waistValue;




    var waistMinusWristIndexNumber;
    var waistWristWeightArrayIndex;
    var bodyFatPercent;
    var bodyFatWeight;
    var leanBodyMass;
    var activityFactorValue = document.getElementById("selFactor").value;
    var proteinRequirements;

    for (i = 0; i < waistMinusWristIndex.length; i++) {
        if (waistMinusWristIndex[i] == waistMinusWristValue) {
            waistMinusWristIndexNumber = i + 1;
            break;
        }
    }

    for (y = 0; y < waistWristWeightArray.length; y++) {
        if (waistWristWeightArray[y][0] == weightValue) {
            waistWristWeightArrayIndex = y;
            break
        }
    }

    bodyFatPercent = waistWristWeightArray[waistWristWeightArrayIndex][waistMinusWristIndexNumber];
    if (bodyFatPercent > 50) {
        bodyFatPercent = 50;
    }

    if (weightValue > 300) {
        activityFactorValue = 0.6;
        document.getElementById("selFactor").value = 0.6;
    }

    bodyFatWeight = weightValue * (bodyFatPercent / 100);
    leanBodyMass = weightValue - bodyFatWeight;
    proteinRequirements = leanBodyMass * activityFactorValue;
    if (proteinRequirements < 77) {
        proteinRequirements = 77;
    }
    if (twoDecimalPlaces(bodyFatPercent) == '0.00')
        alert('Your measurements fall outside the scope of this calculator')
    else {
        params = 'Fatpercent=' + twoDecimalPlaces(bodyFatPercent) + '&FatWeight=' + twoDecimalPlaces(bodyFatWeight) + '&BodyMass=' + twoDecimalPlaces(leanBodyMass) + '&protein=' + twoDecimalPlaces(proteinRequirements) + '&carbs=' + Math.round(proteinRequirements / 7) + '&sex=M&email=' + emailaddress;
        location.href = 'FatCalculator.aspx?' + params;
    }
    //  document.getElementById("m_lblbodyFatPercent").innerHTML ='Body Fat %:' ;
    //  document.getElementById("m_lblbodyFatWeight").innerHTML ='Body Fat Weight (lbs):' ;
    //  document.getElementById("m_lblleanBodyMass").innerHTML ='Lean Body Mass (lbs):' ;
    //  document.getElementById("m_lblproteinRequirements").innerHTML = 'Daily Protein Requirement (g):' ;
    //  document.getElementById("m_lblnumberOfBlocks").innerHTML ='or # of Blocks of Protein:' ;
    //  document.getElementById("m_lblnumberOfBlocksc").innerHTML ='# of Blocks of Carbohydrate:' ;
    //  document.getElementById("m_lblnumberOfBlocksf").innerHTML ='# of Blocks of Fat:' ;
    //  document.getElementById("m_bodyFatPercent").innerHTML =twoDecimalPlaces(bodyFatPercent) ;
    //  document.getElementById("m_bodyFatWeight").innerHTML =twoDecimalPlaces(bodyFatWeight) ;
    //  document.getElementById("m_leanBodyMass").innerHTML =twoDecimalPlaces(leanBodyMass) ;
    //  document.getElementById("m_proteinRequirements").innerHTML =twoDecimalPlaces(proteinRequirements) ;
    //  document.getElementById("m_numberOfBlocks").innerHTML =Math.round(proteinRequirements/7) ;
    //  document.getElementById("m_numberOfBlocksc").innerHTML =Math.round(proteinRequirements/7) ;
    //  document.getElementById("m_numberOfBlocksf").innerHTML =Math.round(proteinRequirements/7) ;
}

//function clearForm(){

//  document.getElementById("m_Weight").value ='';
//  document.getElementById("m_Height").value ='';
//  document.getElementById("m_Waist").value ='';
//  document.getElementById("m_Wrist").value ='';
//  document.getElementById("m_bodyFatPercent").innerHTML ='';
//  document.getElementById("m_bodyFatWeight").innerHTML ='' ;
//  document.getElementById("m_leanBodyMass").innerHTML ='' ;
//  document.getElementById("m_proteinRequirements").innerHTML ='' ;
//  document.getElementById("m_numberOfBlocks").innerHTML ='' ;
//  document.getElementById("m_numberOfBlocksc").innerHTML ='' ;
//  document.getElementById("m_numberOfBlocksf").innerHTML ='' ;
//  
//  document.getElementById("m_lblbodyFatPercent").innerHTML ='' ;
//  document.getElementById("m_lblbodyFatWeight").innerHTML ='' ;
//  document.getElementById("m_lblleanBodyMass").innerHTML ='' ;
//  document.getElementById("m_lblproteinRequirements").innerHTML = '' ;
//  document.getElementById("m_lblnumberOfBlocks").innerHTML ='' ;
//  document.getElementById("m_lblnumberOfBlocksc").innerHTML ='' ;
//  document.getElementById("m_lblnumberOfBlocksf").innerHTML ='' ;

//}



