// Your web browser is too old.<br>
<!-- // hide javascript when no scripting.

function CalcIt(form) {
	var Age = 35;

	var weight =  Number(form.wt.value);

	if (!checkWeight(weight)) return false;
	if (form.wu.selectedIndex == 0) {	// 0 = weight in lbs
		kg = weight * 0.45359237;
	} else {								// 1 = weight in kg.
		kg = weight;
	}

	var height =  Number(form.ht.value);

	if ((isNaN(height)) || (height == null)  || (height == "") || (height < 0)) {
		feetAndInches(form);
		height =  Number(form.ht.value);
	}

	if (form.hu.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) {
		alert("Heights should be taller than 33 centimeters (31.5 inches).");
		return false;
	}
	if (heightMeters > 2.41) {
		alert("Heights should be shorter than 241 centimeters ( 7 feet, 11 inches).");
		return false;
	}
	setFeetAndInches(form,heightInches);
 
	// Calculate BMI
	bmi = kg / Math.pow(heightMeters,2);
 	form.bmi.value = rounding(bmi,1);
 
// Calculate IBW using Devine formulas.
	if (form.Gender.selectedIndex == 1)  {  // sex is female.
	
		idPivot = 48.67; //45.5
		idSlope = 1.65; //1.65
	
	
	} else {								// sex is male.
		
		idPivot = 51.65; //50
		idSlope = 1.85; //2.3

	}
	
	var idKg = idPivot + idSlope * (heightInches - 60);

	if (form.wu.selectedIndex == 0) {  // 0 = weight in lbs
		form.idealRange.value = Math.round( (19 * (heightMeters*heightMeters)) * 2.2046 ) + " - " + Math.round( (25 * (heightMeters*heightMeters)) * 2.2046 ) + " lbs";
		form.idealWeight.value = Math.round( (25 * (heightMeters*heightMeters)) * 2.2046 ) + " lbs";
		form.goalWeightLoss.value = ( Number(form.wt.value) - Math.round( (25 * (heightMeters*heightMeters)) * 2.2046 ) ) + " lbs";
	} else {  // 1 = weight in kg.
		form.idealRange.value = Math.round( (19 * (heightMeters*heightMeters)) ) + " - " + Math.round( (25 * (heightMeters*heightMeters)) ) + " kgs";
		form.idealWeight.value = Math.round( (25 * (heightMeters*heightMeters)) ) + " kgs";		
		form.goalWeightLoss.value = ( Number(form.wt.value) - Math.round( (25 * (heightMeters*heightMeters)) ) ) + " kgs";		
	}

// Calculate IBW using Met Life tables, using linear formulas actually.
	var MetInches = heightInches + 1;   // Met Life tables use height in 1 inch heel shoes.
		if (form.Gender.selectedIndex == 1) {  // sex is female.
			if (Age > 18 && MetInches > 57 && MetInches < 73) {
				if (form.frameSize.selectedIndex == 0) {  // Medium Frame, default
					form.idealMet.value = Math.round(0.032*MetInches*MetInches - 1.2954*MetInches + 75.813) + " - " + Math.round(0.0074*MetInches*MetInches + 2.0191*MetInches - 21.379) + " lbs";
				} else { if ( form.frameSize.selectedIndex == 1) {  // Large frame. 
					form.idealMet.value = Math.round( 0.0189*MetInches*MetInches + 0.4706*MetInches + 26.374) + " - " + Math.round((3.5714*MetInches) - 77.143) + " lbs";
					} else {  // small frame.
					form.idealMet.value = Math.round( 0.0633*MetInches*MetInches - 5.521*MetInches + 207.9) + " - " + Math.round(0.0189*MetInches*MetInches + 0.4706*MetInches + 19.374) + " lbs";
					}
				} 
			} else {  // age < 18 or too short or too tall.
				form.idealMet.value = "N/A";
			}
		} else {										// sex is male.
			if (Age > 18 && MetInches > 61 && MetInches < 77 ) {
				if ( form.frameSize.selectedIndex == 0) { // Medium frame size
					form.idealMet.value = Math.round(0.0649*MetInches*MetInches - 6.0789*MetInches + 258.26) + " - " + Math.round(0.0808*MetInches*MetInches - 7.893*MetInches + 319.71) + " lbs";
				} else { if ( form.frameSize.selectedIndex == 1) { // Large frame size
					form.idealMet.value = Math.round( 0.0962*MetInches*MetInches - 10.255*MetInches + 404.05) + " - " + Math.round( 0.0494*MetInches*MetInches - 2.7666*MetInches + 131.46 ) + " lbs";
					} else { // small frame size
					form.idealMet.value = Math.round( 0.0619*MetInches*MetInches - 6.2128*MetInches + 275.92) + " - " + Math.round( 0.0889*MetInches*MetInches - 9.2657*MetInches + 366.84 ) + " lbs";
					}
				}
			} else {  // age < 18 or too short or too tall.
				form.idealMet.value = "N/A";
			}
		}
		
return null;
}

function setFeetAndInches(form,inchies) {
	var feet = Math.min( Math.max( Math.floor( inchies / 12 ), 1), 7);
	form.htf.selectedIndex = feet - 1;

	inchies = rounding( inchies - feet*12,0);
	form.hti.selectedIndex = Math.min( Math.max( inchies,0), 11 );
	return true;
}

function feetAndInches(form) {
	var inchies = 0;
	inchies = ((form.htf.selectedIndex+1) * 12) + form.hti.selectedIndex;

	if (form.hu.selectedIndex == 0) form.ht.value = inchies;
	if (form.hu.selectedIndex == 1) form.ht.value = rounding( inchies * 2.54,0);
	return true;
}


function SetPercentile( pc ) {
	if (pc > 98) pc = "> 98th percentile";
	else  {  if (pc < 2) pc = "< 2nd percentile";
		else { 
			if ( (pc > 10) && (pc < 14) ) { pc = pc + "th percentile"; }
			else { if ( rightDigit(pc) == 1) { pc = pc + "st percentile"; }
			else { if ( rightDigit(pc) == 2) { pc = pc + "nd percentile"; }
			else { if ( rightDigit(pc) == 3) { pc = pc + "rd percentile"; }
			else pc = pc + "th percentile";
					}
				}
			}
		}
	}
	return pc;
}

function poundsAndKilos(form) {
	var weight = Number(form.wt.value);
	if ( weight > 0) {
		if (form.wu.selectedIndex == 0) {	// 0 = pounds.
			form.wt.value = rounding( weight / 0.45359237,0);
		} else {								// 1 = kilograms.
			if (weight > 219) {
				form.wt.value = rounding( weight * 0.45359237,0);
			} else {
				form.wt.value = rounding( weight * 0.45359237,1);
			}
		}
		form.wt.select()
		form.wt.focus()
	}
	return true;
}

function inchesCm(form) {
	var height = Number(form.ht.value);
	if (height > 0) {
		if (form.hu.selectedIndex == 0) { // is now inches, was cm.
			setFeetAndInches(form, height / 2.54); 
			form.ht.value = rounding( height / 2.54,1) ; 
		} else {								// is now cm, was inches.
			setFeetAndInches(form, height);  // Always pass inches in height to this function.
			form.ht.value = rounding( height * 2.54,0);  }  
		form.ht.select()
		form.ht.focus()
	}
	return true;
}

function rightDigit(num) {
	num = num - (Math.floor(num/10)*10);
	return num;
}

function rounding(number,decimal) {
	multi = Math.pow(10,decimal);
	number = Math.round(number * multi) / multi;
	return number;
}


function checkWeight(val) {
	if ((isNaN(val)) || (val == null)  || (val == "") || (val < 0)) {
		alert( "Please enter a value for Weight.");
		return false;
	}
	return true;
}

function microsoftKeyPress() {
    if (window.event.keyCode == 13) {
	CalcIt(document.forms[0]);
    }
    return true;
}

function cal_bmi(lbs, ins){
   h2 = ins * ins;
   bmi = lbs/h2 * 703
   f_bmi = Math.floor(bmi);
   diff  = bmi - f_bmi;
   diff = diff * 10;
   diff = Math.round(diff);
   if (diff == 10){
      // Need to bump up the whole thing instead
      f_bmi += 1;
      diff = 0;
   }
   bmi = f_bmi + "." + diff;
   return bmi;
}
function compute(){
   var f = self.document.forms[0];

   w = f.wt.value;
   v = f.htf.value;
   u = f.hti.value;

   // Format values for the BMI calculation

   if (!chkw(u)){
     var ii = 0;
     f.hti.value = 0;
   } else {
     var ii = parseInt(f.hti.value);
   }

   var fi = parseInt(f.htf.value * 12);
   var i = fi + ii;

  // Do validation of remaining fields to check for existence of values

   if (!chkw(v)){
     alert("Please enter a number for your height.");
     f.htf.focus();
     return;
   }
   if (!chkw(w)){
     alert("Please enter a number for your weight.");
     f.wt.focus();
     return;
   }

   // Perform the calculation

   f.bmi.value = cal_bmi(w, i);
   f.bmi.focus();
}

function chkw(w){
   if (isNaN(parseInt(w))){
      return false;
   } else if (w < 0){
  return false;
  }
  else{
  return true;
  }
}

//setTimeout("document.forms[0].wt.focus();",1);
