// JavaScript Document
<!--
function doCalc(f) {
  // weight
  var weight =  Number(f.wt.value);
  if ((isNaN(weight)) || (weight == null) || (weight == "") || (weight < 0)) {
    alert( "Please enter a weight between 22 and 441 pounds (10 and 200 kilograms).");
    return false;
  }

  // pounds or kilos
  if (f.wSystem.selectedIndex == 0) { 
    kg = weight * 0.45359237;
  } 
  else { 
    kg = weight;
  }

  if ((kg < 10) || (kg > 200) ) {
    alert("Weight should be between 22 and 441 pounds (10 and 200 kilograms).");
    return false;
  }
  ////////////////////////////////////
  // height
  var height =  Number(f.ht.value);
  if ((isNaN(height)) || (height == null)  || (height == "") || (height < 0)) {
    feetInches(f);
    height =  Number(f.ht.value);
  }

  if (f.hSystem.selectedIndex == 0) {		//  if height units are "inches"
    heightInches = height;
    heightMeters = height * 2.54 / 100;
  } 
  else {					// else if height units are "cm".
    heightInches = height / 2.54;
    heightMeters = height / 100;
  }
  if ((heightMeters < 0.33) || (heightMeters > 2.41) ){
    alert("Height should be between 31 inches and 7 feet, 11 inches (33 and 241 centimeters).");
    return false;
  }
  setFeetInches(f,heightInches);
  ///////////////////////////////////
  var feet = Math.min( Math.max( Math.floor( heightInches / 12 ), 1), 7);
  f.htf.selectedIndex = feet - 1;

  justInches = rounding( heightInches - feet*12,0);
  f.hti.selectedIndex = Math.min( Math.max( justInches,0), 11 );

  var cm = heightMeters * 100;

  bmi = kg / Math.pow(heightMeters,2);
  f.bmi.value = rounding(bmi,1);
  return true;
}
///////////////////////////////////////////////////////

// pounds or kilograms
function lbsK(f) {
  var weight = Number(f.wt.value);
  if ( weight > 0) {
    if (f.wSystem.selectedIndex == 0) {	// 0 = pounds.
      f.wt.value = rounding( weight / 0.45359237,0);
    } 
    else {	// 1 = kilograms.
      if (weight > 219) {
	f.wt.value = rounding( weight * 0.45359237,0);
      } 
      else {
        f.wt.value = rounding( weight * 0.45359237,1);
      }
    }
    f.wt.select();
    f.wt.focus();
  }
  return true;
}
///////////////////////////////////////////////////////

function inCm(f) {
  var height = Number(f.ht.value);
  if (height > 0) {
    if (f.hSystem.selectedIndex == 0) { // is now inches, was cm.
      setFeetInches(f, height / 2.54); 
      f.ht.value = rounding( height / 2.54,1) ; 
    } 
    else {			// is now cm, was inches.
      setFeetInches(f, height);  // Always pass inches in height to this function.
      f.ht.value = rounding( height * 2.54,0);  
    }  
    f.ht.select();
    f.ht.focus();
  }
  return true;
}
///////////////////////////////////////////////////////

function feetInches(f) {
  var inches = 0;
  inches = ((f.htf.selectedIndex+1) * 12) + f.hti.selectedIndex;

  if (f.hSystem.selectedIndex == 0) {
    f.ht.value = inches;
  }
  if (f.hSystem.selectedIndex == 1) {
    f.ht.value = rounding( inches * 2.54,0);
  }
  return true;
}
///////////////////////////////////////////////////////

function setFeetInches(f,inches) {
  var feet = Math.min( Math.max( Math.floor( inches / 12 ), 1), 7);
  f.htf.selectedIndex = feet - 1;

  inches = rounding( inches - feet*12,0);
  f.hti.selectedIndex = Math.min( Math.max( inches,0), 11 );
  return true;
}
///////////////////////////////////////////////////////

function rounding(number,decimal) {
	multi = Math.pow(10,decimal);
	number = Math.round(number * multi) / multi;
	return number;
}
///////////////////////////////////////////////////////

//-->
