'Script (c) Dr. Don Hall
'Last updated 09 FEB 2001 by Lauren Smith

Sub cmdCompute_onclick()
'--- Compute percent body fat based on 3 site skinfold test ---

    'Validate user input:
    'Age between 18, 110
    'Sum of skinfolds between 6, 300
    'Weight between 40, 600
    If ValidateInput(txtAge.Value, 18, 110) Then
        If ValidateInput(txtSumSF.Value, 6, 300) Then
            If ValidateInput(txtWt.Value, 40, 600) Then
                'Do the calculations for Men and Women
                If (radioMale.checked) Then   'Men:
                    Density = 1.10938 - (0.0008267 * txtSumSF.Value) + (0.0000016 * txtSumSF.Value ^ 2) - (0.0002574 * txtAge.Value)
                    PercentFat = ((4.95 / Density) - 4.5) * 100
                Else                    'Women:
                    Density = 1.0994921 - (0.0009929 * txtSumSF.Value) + (0.0000023 * txtSumSF.Value ^ 2) - (0.0001392 * txtAge.Value)
                    PercentFat = ((5.01 / Density) - 4.57) * 100
                End If
            Else
                MsgBox "Weight is a required field and must be between 40 and 600 pounds.", vbOKOnly, "Data Entry Error"
                Exit Sub
            End If
        Else
            MsgBox "Sum of skinfolds is a required field and must be between 6 and 300.", vbOKOnly, "Data Entry Error"
            Exit Sub
        End If
    Else
        MsgBox "Age is a required field and must be between 18 and 110.", vbOKOnly, "Data Entry Error"
        Exit Sub
    End If
    
    'Display the results:
    txtPercentFat.Value = FormatNumber(PercentFat, 1)
    txtFatWt.Value = FormatNumber(txtWt.Value * (PercentFat / 100), 1)
    txtLeanWt.Value = FormatNumber(txtWt.Value - txtFatWt.Value, 1)

    'Verify user input:
    'Desired percent fat between 4 and 25
    'min < max
    If ValidateInput(txtDesiredPF1.Value, 4, 25) Then
        If ValidateInput(txtDesiredPF2.Value, 4, 25) Then
            If CInt(txtDesiredPF1.Value) < CInt(txtDesiredPF2.Value) Then
                txtWtGoal1.Value = FormatNumber(txtLeanWt.Value / ((100 - txtDesiredPF1.Value) / 100), 0)
                txtWtGoal2.Value = FormatNumber(txtLeanWt.Value / ((100 - txtDesiredPF2.Value) / 100), 0)
            Else
                MsgBox "Minimum desired percent fat must be lower than maximum desired percent fat.", vbOKOnly, "Data Entry Error"
                Exit Sub
            End If
        Else
            MsgBox "Maximum desired percent fat is a required field and must be between 4 and 25.", vbOKOnly, "Data Entry Error"
            Exit Sub
        End If
    Else
        MsgBox "Minimum desired percent fat is a required field and must be between 4 and 25.", vbOKOnly, "Data Entry Error"
        Exit Sub
    End If
End Sub

Sub radioMale_onclick()
'--- Set default desired percent body fat for men ---
    If ValidateInput(txtAge.Value, 18, 110) Then
        If txtAge.Value < 20 Then
            txtDesiredPF1.Value = 6
            txtDesiredPF2.Value = 14
        ElseIf txtAge.Value < 40 Then
            txtDesiredPF1.Value = 8
            txtDesiredPF2.Value = 15
        Else
            txtDesiredPF1.Value = 10
            txtDesiredPF2.Value = 19
        End If
    End If
    
    'Then recalculate, if all fields are filled in
    'Otherwise just exit silently
    If ValidateInput(txtAge.Value, 18, 110) Then
        If ValidateInput(txtSumSF.Value, 6, 300) Then
            If ValidateInput(txtWt.Value, 40, 600) Then
                cmdCompute_onclick
            End If
        End If
    End If
    
End Sub

Sub radioFemale_onclick()
'--- Set default desired percent body fat for women ---

    If ValidateInput(txtAge.Value, 18, 110) Then
        If txtAge.Value < 20 Then
            txtDesiredPF1.Value = 12
            txtDesiredPF2.Value = 20
        ElseIf txtAge.Value < 40 Then
            txtDesiredPF1.Value = 13
            txtDesiredPF2.Value = 23
        Else
            txtDesiredPF1.Value = 16
            txtDesiredPF2.Value = 25
        End If
    End If
    
    'Then recalculate, if all fields are filled in
    'Otherwise just exit silently
    If ValidateInput(txtAge.Value, 18, 110) Then
        If ValidateInput(txtSumSF.Value, 6, 300) Then
            If ValidateInput(txtWt.Value, 40, 600) Then
                cmdCompute_onclick
            End If
        End If
    End If

End Sub

Sub cmdClear_onclick()
'--- Clear all fields ---
    txtAge.Value = ""
    txtSumSF.Value = ""
    txtPercentFat.Value = ""
    txtFatWt.Value = ""
    txtLeanWt.Value = ""
    txtWtGoal1.Value = ""
    txtWtGoal2.Value = ""
    txtWt.Value = ""
    txtName.Value = ""
    txtDesiredPF1.Value = "10"
    txtDesiredPF2.Value = "15"
End Sub

Function ValidateInput(strInputData, dMin, dMax)
'Validate user input data:
'Returns True if it is a number and is equal to or between min and max
'Otherwise it returns false.

    If IsNumeric(strInputData) Then
        If CDbl(strInputData) <= dMax And CDbl(strInputData) >= dMin Then
                ValidateInput = True
                Exit Function
        End If
    End If
    
    'Otherwise...
    ValidateInput = False
End Function

