<!-- // Copyright 2004, 2008 - Antonio Zamora
// These functions are used in DIET.HTML and DIETA.HTML

function diet_calc(lang){
   // lang parameter is language code or null ("es")
   var f = document.forms[0]; // "bmi_input" is 1st form
   var bm, i, fi, ii, i1, kg, htc, minbm, maxbm, m, bmix, j, o1,o2, m1;
   var calmin, calmax, metricsw, gpd;

  // assume metric
  kg = f.wtk.value;
  htc = parseFloat(f.htc.value);
  nkc = parseFloat(f.neckc.value);
  wac = parseFloat(f.waistc.value);
  hic = parseFloat(f.hipc.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) ){
     m1 = "Please enter your height and weight.";
     if (lang=="es") {
       m1 = "Por favor, introduzca su altura y peso.";
     }
     alert(m1);
     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

    nk = parseFloat(f.neck.value);
    nkc = nk*2.54;
    wa = parseFloat(f.waist.value);
    wac = wa*2.54;
    hi = parseFloat(f.hip.value);    
    hic = hi*2.54;
   }  // not metric
   
    if (htc < 100 || htc > 250) {     
     m1 = "Error in height.";
     if (lang=="es") {
       m1 = "Error en la altura.";
     }
     alert(m1);
     return;
    }
    if (kg < 25 || kg > 250) {     
     m1 = "Error in weight.";
     if (lang=="es") {
       m1 = "Error en el peso.";
     }
     alert(m1);
     return;
    }   
   m = htc/100;  // meters
   h2 = m * m;
   bm = kg/h2;
   bmix = rounder(bm);  // bmi rounded to tenths
   // f.bmi.value = bmix;   // display on form    
   document.getElementById('bmi').innerHTML=bmix+"&nbsp;&nbsp;kg/m<sup>2</sup>";
       
    if  (!chkw(wac))  {     // waist 
     m1 = "Please enter your waist size.";
     if (lang=="es") {
       m1 = "Por favor, introduzca el tamaño de su cintura.";
     }
     alert(m1);
     return;
    }        
    if (  wac < 45 || wac > htc) {     // waist should be smaller than height
     m1 = "Error in waist size.";
     if (lang=="es") {
       m1 = "Error en el tamaño de la cintura.";
     }
     alert(m1);
     return;
    }     
    
   w2h = (wac/htc) + 0.005;  // round to hundredths
   ii = w2h.toString();
   //f.wthr.value = ii.substring(0,4); //w2h.toFixed(2);  // display waist-to-height ratio  
   document.getElementById('wthr').innerHTML=ii.substring(0,4);  
   
    if (!chkw(nkc)) {     
     m1 = "Please enter your neck size.";
     if (lang=="es") {
       m1 = "Por favor, introduzca el tamaño de su cuello.";
     }
     alert(m1);
     return;
    }    
    if ( nkc < 20 || nkc > 60) {     
     m1 = "Error in neck size.";
     if (lang=="es") {
       m1 = "Error en el tamaño del cuello.";
     }
     alert(m1);
     return;
    }    

   // check for radio buttons
   sex = " ";
   if (f.sex[0].checked) {
     sex = "m";
   }
   if (f.sex[1].checked) {
     sex = "f";
   }
   if (sex == " ") { 
     m1 = "Please specify Male or Female";
     if (lang=="es") {
       m1 = "Por favor, indique Hombre o Mujer.";
     }
     alert(m1);
    return;
   }
  if (f.sex[1].checked) {
    if  (!chkw(hic)) {     
     m1 = "Please enter your hip size.";
     if (lang=="es") {
       m1 = "Por favor, introduzca el tamaño de su cadera.";
     }
     alert(m1);
     return;
    }   
    if ( hic < 45 || hic > 200) {     
     m1 = "Error in hip size.";
     if (lang=="es") {
       m1 = "Error en el tamaño de la cadera.";
     }
     alert(m1);
     return;
    }  
  }    

  logcon = Math.LN10;  // ln(10) = 2.302585093;  //  log10(x) = ln(x)/ln(10)
  if (sex == "m") { // male
    calmin = Math.floor(1842 + (htc-150)*15.4 + .5);
    calmax = Math.floor(2488 + (htc-150)*23.6 + .5);
    // compute % body fat
    i = 495/(1.0324 - 0.19077*(Math.log(wac-nkc)/logcon) + 0.15456*(Math.log(htc)/logcon) ) - 450;
  }
  else {  // female
    calmin = Math.floor(1622 + (htc-150)*13.2 + .5);
    calmax = Math.floor(2194 + (htc-150)*19.3 + .5);
    // compute % body fat
    i = 495/(1.29579 - 0.35004*(Math.log(wac+hic-nkc)/logcon) + 0.22100*(Math.log(htc)/logcon) ) - 450;  
  }
  ii = rounder(i); 
  //f.pctfat.value = ii+"%";
  document.getElementById('pctfat').innerHTML=ii+"%";
  
  // Lean body mass: lbm = wac * (100 - pctfat);
  ii = kg*((100 - i)/100);
  j = " Kg";
  if (metricsw == 0) {
    ii = ii*2.2;
    j = " Lb";
  }
  //f.leanbm.value = rounder(ii) + j;
  document.getElementById('leanbm').innerHTML=rounder(ii) + j;
  
   act = " ";
   gpd = 0.8;    // grams of protein per day
   if (f.act[0].checked) {
     act = "0";
     gpd = 0.8;
	 }
   if (f.act[1].checked) {
     act = "1";
     gpd = 1.1;
	 }
   if (f.act[2].checked) {
     act = "2";
     gpd = 1.4;
   }
  if (act == " ") { 
     m1 = "Please specify Level of Activity";
     if (lang=="es") {
       m1 = "Por favor, indique su nivel de actividad.";
     }
     alert(m1);
    return;
  }
   
   minbm = 18.5;
   maxbm = 24.9;
   ii = Math.floor(gpd * (maxbm * h2));   // (ideal high weight in Kg) * gpd = grams of protein per day
    
   o1=""; o2 = "";
   if ((bmix >= minbm) && (bmix  <= maxbm) ) {  // normal weight
    o1="Your weight is in the normal range.";
    if (lang=="es") {
      o1 = "Su peso está en el rango normal.";
    }
    if (w2h < 0.5) {
      o2 = "You do not need to lose weight.<br/>\n";
      if (lang=="es") {
       o2 = "Usted no necesita perder peso.<br/>\n";
      }
    }
    else {
      o2 = "You may need to lose some abdominal fat.<br/>\n";
      if (lang=="es") {
       o2 = "Es posible que tenga que perder grasa abdominal.<br/>\n";
      }
    }
    if (act == "0"){
      if (lang=="es") {
       o2 = o2 + "Start an exercise progam and become more active.<br/>\n";
      }
      else { // English
       o2 = o2 + "Inicie un programa de ejercicio y aumente su actividad física.<br/>\n";
      }
    }
    if (lang=="es") {
     o2 = o2 + "Calorías mínimas necesarias: " + calmin + " Calorías diarias<br/>\n";
    }
    else { // English
     o2 = o2 + "Minimum caloric requirements: " + calmin + " Calories per day<br/>\n";
    }
   }  // normal weight
   else {  // not normal weight
    if (bmix < minbm) {  // underweight
     i = rounder(h2*minbm - kg);
     i1 = i*2.2;
     i1 = rounder(i1);
     o1 = "You are underweight by "+i+" kilograms ("+i1+" pounds).<br/>\n";
     if (lang=="es") {
      o1 = "Su peso esta "+i+" kilogramos ("+i1+" libras) bajo lo normal.<br/>\n";
     }
     o2 = "You may need to increase your caloric intake.<br/>\n";
     o2 = o2 + "Consume at least " + calmin + " calories per day.<br/>\n";
     if (lang=="es") {
      o2 = "Es posible que tenga que aumentar su ingestión de calorías.<br/>\n";
      o2 = o2 + "Consuma cuando menos " + calmin + " calorías diarias.<br/>\n";
     }
    } // underweight
    if (bmix > maxbm) {  // overweight
      if (w2h < 0.5) { // BMI > 24.9 && waist-to-height < 0.5 ==> bodybuilder
        o1 = "Your BMI is greater than normal, but your waist-to-height ratio is normal.";
        o2 = "You may be overweight or have unusual mass distribution.<br/>\n";
        if (lang=="es") {
         o1 = "Su Índice de Masa Corporal (IMC) excede lo normal, pero su proporcion de cintura a altura es normal.";
         o2 = "Usted puede tener exceso de peso o una distribución peculiar de peso.<br/>\n";
        }
      }
      else { // w2h > .5
       i = rounder(kg - h2*maxbm);
       i1 = i*2.2;
       i1 = rounder(i1);
       o1 = "You are overweight by "+i+" kilograms ("+i1+" pounds)";
       if (lang=="es") {
        o1 = "Usted tiene "+i+" kilogramos ("+i1+" libras) de sobrepeso."; 
       }
       if (act == "0"){
        o2 = "You need to exercise at least 30 minutes every day.<br/>\n";
        if (lang=="es") {
         o2 = "Usted necesita hacer ejercicio cuando menos 30 minutos al día.<br/>\n"; 
        }
       }
       if (lang=="es") {
        o2 = o2 + "Calorías mínimas necesarias: " + calmin + " Calorías diarias<br/>\n";
       }
       else { // English
        o2 = o2 + "Minimum caloric requirements: " + calmin + " Calories per day<br/>\n";
       }
       i = calmin * 0.15;
       v = Math.floor(calmin - i);
       if (lang=="es") {
        o2 = o2 + "Limite su consumo alimenticio a " + v + " calorías diarias<br/>\n";
       }
       else { // English
        o2 = o2 + "Limit your food intake to " + v + " calories per day<br/>\n";
       }
       if (metricsw == 0) {
        u = rounder( (i/4086) * 30)
        if (lang=="es") {
          o2 = o2 + " para perder " + u + " libras por mes.<br/>\n";
        }
        else { // English
         o2 = o2 + " to lose " + u + " pounds per month.<br/>\n";
        }
       }
       else {
        u = rounder( ((i/4086) * 30)/2.2)
        if (lang=="es") {
          o2 = o2 + " para perder " + u + " Kg por mes.<br/>\n";
        }
        else { // English
         o2 = o2 + " to lose " + u + " Kg per month.<br/>\n";
        }     
       }
      } // w2h > .5
     }  // overweight
    } // not normal weight
    if (lang=="es") {
     o2 = o2 + "Su dieta debe contener cuando menos " + ii + " gramos de proteína por día.<br/>\n"; 
    }
    else { // English
     o2 = o2 + "Your diet should contain at least " + ii + " grams of protein per day.<br/>\n"; 
    }  
   document.getElementById('out').innerHTML=o1;
   document.getElementById('out2').innerHTML=o2;
}  // diet_calc(lang)

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.neckc.value = ""; f.waistc.value = ""; f.hipc.value = "";
     f.wtk.value = "";
   }
   if (x == 2){  // Metric units, clear English units
     f.htf.value = ""; f.hti.value = "";  f.neck.value = ""; f.waist.value = ""; f.hip.value = "";
     f.wt.value = "";
   }   
   if (x == 3){  // male
     f.hip.value = ""; f.hipc.value = "";
   }      

   document.getElementById('bmi').innerHTML="";
   document.getElementById('wthr').innerHTML="";
   document.getElementById('pctfat').innerHTML="";
   document.getElementById('leanbm').innerHTML="";
   document.getElementById('out').innerHTML="";
   document.getElementById('out2').innerHTML="";
}

//-->
