'Script (c) Dr. Don Hall
'Last updated: 09 FEB 2001 by Lauren Smith

Sub cmdCompute_onclick()
    
    'Data Validation: Height and Weight
    'Height must be >= 48 and <= 96
    'Weight must be >= 40 and <= 600
    If ValidateInput(txtHt.Value, 48, 96) Then
        If ValidateInput(txtWt.Value, 40, 600) Then
            'Calculate and display the BMI
            BMI = (txtWt.Value / 2.2) / (txtHt.Value * 2.54 / 100) ^ 2
            txtBMI.Value = FormatNumber(BMI, 1)
        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 "Height is a required field and must be between 48 and 96 inches.", vbOKOnly, "Data Entry Error"
        Exit Sub
    End If
    
    'Data Validation:
    'Desired BMI must be between 17 and 27
    'Min must be smaller then max
    If ValidateInput(txtDesiredBMI1.Value, 17, 27) Then
        If ValidateInput(txtDesiredBMI2.Value, 17, 27) Then
            If txtDesiredBMI1.Value < txtDesiredBMI2.Value Then
                'Calculate and Display the Weight Goals
                txtWtGoal1.Value = CInt((txtHt.Value * 2.54 / 100) ^ 2 * txtDesiredBMI1.Value * 2.2)
                txtWtGoal2.Value = CInt((txtHt.Value * 2.54 / 100) ^ 2 * txtDesiredBMI2.Value * 2.2)
            Else
                MsgBox "Maximum desired BMI Must be larger than minimum desired BMI.", vbOKOnly, "Data Entry Error"
                Exit Sub
            End If
        Else
            MsgBox "Maximum desired BMI should be below 27.", vbOKOnly, "Data Entry Error"
            Exit Sub
        End If
    Else
        MsgBox "Minimum desired BMI should be at least 17.", vbOKOnly, "Data Entry Error"
        Exit Sub
    End If
End Sub

Sub radioMen_onclick()
'txtName.value = radioMen.value
    If radioMen.Value = 1 Then
        txtDesiredBMI1.Value = 20
        txtDesiredBMI2.Value = 24.9
    End If
End Sub

Sub radioWomen_onclick()
'txtName.value=radioWomen.value
    If radioWomen.Value = 2 Then
        txtDesiredBMI1.Value = 20
        txtDesiredBMI2.Value = 23
    End If
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

Sub CmdClear_onclick()
  txtName.value=""
  txtHt.value=""
  txtWt.value=""
  txtBMI.value=""
  txtWtGoal1.Value=""
  txtWtGoal2.Value="" 
  radioMen.click
  
End Sub

