<!-- // Copyright 2005 - Antonio Zamora
// These functions are used in CRON1.HTML

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

  // assume metric
  kg = f.wtk.value;
  htc = parseFloat(f.htc.value);
 
  metricsw = 1;
  if ((!chkw(kg)) || (!chkw(htc))) {  // not metric
   metricsw = 0;
   w = f.wt.value;
   v = f.htf.value;
   u = f.hti.value;

    // Validate fields to check for existence of values
    if (!chkw(u) || !chkw(v) || !chkw(w) ){
     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;

    kg = w/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 (kg < 25 || kg > 250) {     
	  	alert("Error in weight.");
     	return;
    }
    
   m = htc/100;  // meters
   h2 = m * m;
   bmi = kg/h2;
   bmix = rounder(bmi);  // bmi rounded to tenths
   f.bmi.value = bmix;   // display on form    
       
    age = parseFloat(f.age.value);
    if  (!chkw(age))  {     // age
	    alert("Please enter your age.");
     	return;
    }        
    if (  age < 12 || age > 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 for radio buttons for activity level
   act = 0;
   if (f.act[0].checked) {
     act = 1.200;
	 }
   if (f.act[1].checked) {
     act = 1.375;
	 }
   if (f.act[2].checked) {
     act = 1.550;
	 }
   if (f.act[3].checked) {
     act = 1.725;
	 }
   if (f.act[4].checked) {
     act = 1.900;
	 }
	if (act == 0) { 
    alert("Please specify Level of Activity.");
    return;
  }   

  // Weight corresponding to BMI 22.0
  controlwt = 22*m*m;  //kg

  if (sex == "m") { //male
    // Harris-Benedict Equation:
    // Male: BEE = 66.47 + (13.75 X weight in kilos) + (5 X height in cm) - (6.76 X age in years)
    // bee1 = 66.47 + (13.75*kg) + (5*htc) - (6.76*age);
    // beecont1 = 66.47 + (13.75*controlwt) + (5*htc) - (6.76*age);
    
    // Mifflin-St Jeor equation:
    // Male: BEE = 10 x weight + 6.25 x height - 5 x age + 5 
    bee1 = 10*kg + 6.25*htc - 5*age + 5;
    beecont1 = 10*controlwt + 6.25*htc - 5*age + 5;  
  }
  else {  //female
    // Harris-Benedict Equation:
    // Female: BEE = 655.1 + (9.56 X weight in kilos) + (1.85 X height in cm) - (4.68 X age in years)
    // bee1 = 655.1 + (9.56*kg) + (1.85*htc) - (4.68*age);
    // beecont1 = 655.1 + (9.56*controlwt) + (1.85*htc) - (4.68*age);
    
    // Mifflin-St Jeor equation:
    // Female: BEE = 10 x weight + 6.25 x height - 5 x age - 161
    bee1 = 10*kg + 6.25*htc - 5*age - 161;
    beecont1 = 10*controlwt + 6.25*htc - 5*age - 161;  
  }
  bee = Math.floor(bee1 + 0.05);
  beecont = Math.floor(beecont1 + 0.05);
  beeact = Math.floor(bee1*act + 0.05);
  beeactcont = Math.floor(beecont1*act + 0.05);

   f.contwtlb.value  = rounder(controlwt*2.2);
   f.contwtkg.value  = rounder(controlwt);
   f.bee.value = bee;
   f.beeact.value = beeact;
   f.beecont.value = beecont;
   f.beeactcont.value = beeactcont;

   cal = parseFloat(f.cal.value);
   if  (!chkw(cal))  {     // calories 
	  alert("Please enter the Calories that you consume per day.");
    return;
   }        
   if (  cal < 400 || cal > 7000) {     
	  alert("Error in Calories.");
    return;
   }  
   caldiff = beeactcont - cal;
   pctcr = rounder(caldiff*100/beeactcont);

   f.caldiff.value = caldiff;
   f.pctcr.value = pctcr;

   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.wtk.value = "";
   }
   if (x == 2){  // Metric units, clear English units
     f.htf.value = ""; f.hti.value = "";  
     f.wt.value = "";
   }   
   f.contwtlb.value  = "";
   f.contwtkg.value  = "";
   f.bmi.value = "";
   f.bee.value = "";
   f.beeact.value = "";
   f.beecont.value = "";
   f.beeactcont.value = "";
   f.caldiff.value = "";
   f.pctcr.value = "";
}

//-->
