<!-- // Copyright 2007 - Antonio Zamora
// These functions are used in how-to-count-calories.html

function calcpctcr(){
   var f = document.forms[0]; 
   var bm, i, fi, ii, i1, kg, htc, minbm, maxbm, bmix, m, j;
   var calmin, calmax, metricsw, gpd;

  // assume metric units
  kgbef = f.wtkbefore.value;
  htc = parseFloat(f.htc.value);
 
  metricsw = 1;
  if ((!chkw(kgbef)) ||  (!chkw(htc))) {  // not metric
   metricsw = 0;
   wbef = f.wtbefore.value;
   v = f.htf.value;
   u = f.hti.value;

    // Validate fields to check for existence of values
    if (!chkw(u) || !chkw(v) || !chkw(wbef) ){
     alert("Please enter your height and weight.");
     return;
    }
    // Convert feet to inches
    ii = parseFloat(f.hti.value);
    fi = parseFloat(f.htf.value * 12);
    i = fi + ii;

    kgbef = wbef/2.2;    // convert pounds to kg  
    htc = i*2.54;      // convert inches to cm
  }  // not metric
   
    if (htc < 100 || htc > 250) {     
	  	alert("Error in height.");
     	return;
    }
    if (kgbef < 25 || kgbef > 250 ) {     
	  	alert("Error in weight.");
     	return;
    }

   m = htc/100;  // meters
   h2 = m * m;
   bmix = kgbef/h2;
   bmix = rounder(bmix);  // bmi rounded to tenths 
   /* // f.bmi.value = bmix;   // display on form     */
   document.getElementById('bmi').innerHTML=bmix;  
       
    agebef = parseFloat(f.agebefore.value);
    if  (!chkw(agebef) )  {     // age
	    alert("Please enter your age.");
     	return;
    }        
    if (  agebef < 12 || agebef > 115 ) {     
	    alert("Error in age.");
     	return;
    }     
 
   // check for radio buttons
   sex = " ";
   if (f.sex[0].checked) {
     sex = "m";
	}
	if (f.sex[1].checked) {
     sex = "f";
	}
	if (sex == " ") { 
    alert("Please specify Male or Female");
    return;
  }

   // check  activity level
   actbef = parseFloat(f.actbefore.value);
   // alert ("actbef="+actbef+" actaft="+actaft);
   
  if (sex == "m") { //male
    // Mifflin-St Jeor equation:
    // Male: BEE = 10 x weight + 6.25 x height - 5 x age + 5 
    beebef = 10*kgbef + 6.25*htc - 5*agebef + 5;
  }
  else {  //female
    // Mifflin-St Jeor equation:
    // Female: BEE = 10 x weight + 6.25 x height - 5 x age - 161
    beebef = 10*kgbef + 6.25*htc - 5*agebef - 161;
  }
  // Multiply times activity level
  beeactbef = Math.floor(beebef*actbef + 0.05);

  //  f.beebefore.value = rounder(beebef);
  document.getElementById('beebefore').innerHTML=rounder(beebef); 
  // f.beeactbefore.value = beeactbef;
  document.getElementById('beeactbefore').innerHTML=beeactbef; 

   return;   
   
}  // compute()

function chkw(w){
   if (isNaN(parseFloat(w))){
      return false;
   } else if (w < 0){
    return false;
   }
   else{
    return true;
   } 
}

function rounder(x) {
  var x1;
   x = x + 0.05;  // round to tenths
   f_bmi = Math.floor(x);
   diff  = Math.floor((x - f_bmi)*10);
   x1 = f_bmi + "." + diff;
  return(x1);
}

function vclear(x) {
   var f = document.forms[0]; 
   if (x == 1){ // English units, clear metric
     f.htc.value = "";
     f.wtkbefore.value = "";
   }
   if (x == 2){  // Metric units, clear English units
     f.htf.value = ""; f.hti.value = "";  
     f.wtbefore.value = "";
   }   
   // f.beebefore.value = "";
   // f.beeactbefore.value = "";
  document.getElementById('bmi').innerHTML=""; 
  document.getElementById('beebefore').innerHTML="";
  document.getElementById('beeactbefore').innerHTML="";
}

//-->
