/*=========================================================
  Calcuate Body Mass Index = the ratio Mass[kg]/Height[m]^2
  =========================================================*/
//The data input is actually weight in lbs and height in feet & inches,
//so the units must be converted thusly:
//First, compute the height in inches = 12*Ht[ft] + dHt[in]
//Next, BMI[kg/m^2] =BMI[lb/in^2] * 39.37^2[in^2/m^2] / 2.205[lb/kg] = 703*BMI[lb/in^2]                       
  function BMI_Calculate() {

    var n, TheBMIForm;
    var NumForms = document.forms.length;
    for (n=1; n<=NumForms; n++) {
      if (document.forms[n-1].name == "BMIForm") {
        TheBMIForm = document.forms[n-1];
        break;
      }
    }

    if (!TheBMIForm) {
      alert('BMI form not found');
      return;
    }

    if (TheBMIForm.Wt_lb.value == "" || parseInt(TheBMIForm.Wt_lb.value) == 0) {
      alert("Please enter your weight");
      return;
    }

    var TotalInches = parseInt(TheBMIForm.Ht_ft.value)*12 + parseInt(TheBMIForm.Ht_in.value);
    var Ht_m   = TotalInches/39.37;
    var Ht_sqr = Ht_m * Ht_m;
    var Mass_kg   = parseInt(TheBMIForm.Wt_lb.value)/2.205;
    if (Ht_sqr > 0) {
      var BMIValue = parseInt(Mass_kg/Ht_sqr);
      TheBMIForm.BMIValueLabel.value = BMIValue;
    }else{
      alert("You must enter a height");
    }
  }





//####################################
//Restrict a textbox field to numerals
//####################################
  function BMI_RestrictToNumeralKey(EventObj) {

// ---------------------------------------------------------------------------------
//| To be called via onKeyDown="BMI_RestrictToNumeralKey(event);" in a textbox.
//|
//| This function cancels keys other than:
//|    0     - no key, happens when the textbox gets the focus
//|    8     - backspace
//|   46     - delete
//|   48- 57 - numerals 0-9
//|   96-105 - keypad numerals
//|
//| This will disallow pasting with ctrl-v, but not pasting from the edit menu.
//| To allow pasting with ctrl-v, allow keyCodes 17 and 86.
//|
//| This function also cancels any keys pressed while the Shift key is down.
//| Without this cancelling action, the shifted versions of the numeral keys
//| (!, @, etc) would be displayed.
// ---------------------------------------------------------------------------------

    var KeysAllowed = 
       "0,8,46,48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105";

    if (EventObj.shiftKey == true) {EventObj.returnValue = false; return;}

    var keyCode = (EventObj.keyCode) ? EventObj.keyCode : EventObj.which;
    if (KeysAllowed.indexOf(keyCode.toString()) == -1) {EventObj.returnValue = false;}
  }


  function BMI_RemoveNonNumerals(Textbox) {
// ---------------------------------------------------------------------------------------
//| To be called via onBlur="BMI_RemoveNonNumerals(this);"
// ---------------------------------------------------------------------------------------
    Textbox.value = Textbox.value.replace(/[^0-9]/g,'');
  }
