function limpia(ctrl){
	document.getElementById(ctrl).checked = false;
	}


function diet_calc(lang){
   // lang parameter is language code or null ("es")
  

  var bm, i, fi, ii, i1, kg, htc, minbm, maxbm, m, bmix, j, o1,o2, m1;
   var calmin, calmax, metricsw, gpd;

  // assume metric
  kg = document.getElementById('wtk').value;
  htc = parseFloat(document.getElementById('htc').value);
  nkc = parseFloat(document.getElementById('neckc').value);
  wac = parseFloat(document.getElementById('waistc').value);
  hic = parseFloat(document.getElementById('hipc').value);
//  alert(hic +"-"+ wac +"-"+ nkc +"-"+htc+"-"+kg);

metricsw = 1;
 
   
    if (htc < 100 || htc > 250) {     
          m1 = "La altura ingresada es invalida.";
         alert(m1);
     return;
    }
    if (kg < 25 || kg > 250) {     
      m1 = "El peso ingresado es invalido.";
       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').value=bmix;
 
	
    if  (!chkw(wac))  {     // waist 
       m1 = "Por favor, introduzca el tamano de su cintura.";
        alert(m1);
     return;
    }        
    if (  wac < 45 || wac > htc) {     // waist should be smaller than height
      m1 = "El tamano de la cintura es invalido.";
         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').value=ii.substring(0,4);  
   
    if (!chkw(nkc)) {     
          m1 = "Por favor, introduzca el tamano de su cuello.";
     alert(m1);
     return;
    }    
    if ( nkc < 20 || nkc > 60) {     
           m1 = "El tamano del cuello es invalido.";
    alert(m1);
     return;
    }    

// check for radio buttons
   sex = " ";
   if (document.getElementById('sexm').checked)
	   sex = document.getElementById('sexm').value;
   if (document.getElementById('sexf').checked)
   	   sex = document.getElementById('sexf').value;
   if (sex == " ") { 
      m1 = "Por favor, indique si es Hombre o Mujer.";
       alert(m1);
    return;
   }
  if (sex=="f") {
    if  (!chkw(hic)) {     
    m1 = "Por favor, introduzca el tamano de su cadera.";
    alert(m1);
     return;
    }   
    if ( hic < 45 || hic > 200) {     
     m1 = "El tamano de la cadera es invalido.";
     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); 
  //alert(ii+"-"+i+"-"+wac+"-"+hic+"-"+nkc+"-"+htc);
  //f.pctfat.value = ii+"%";
  document.getElementById('pctfat').value=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').value=rounder(ii) + j;
  
  
  
  actividad = document.getElementById('act').value;

   act = " ";
   gpd = 0.8;    // grams of protein per day
   if (actividad==1) {
     act = "0";
     gpd = 0.8;
	 }
   if (actividad==2) {
     act = "1";
     gpd = 1.1;
	 }
   if (actividad==3) {
     act = "2";
     gpd = 1.4;
   }
  if (act == " ") { 
     m1 = "Please specify Level of Activity";
     if (lang=="es") {
       m1 = "Por favor, indique su nivel de actividad diaria.";
     }
     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 estai 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 fisica.<br/>\n";
      }
    }
    if (lang=="es") {
     o2 = o2 + "Calorias minimas necesarias: " + calmin + " Calorias 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 calorias.<br/>\n";
      o2 = o2 + "Consuma cuando menos " + calmin + " calorias 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 indice 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 distribucion 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 dia.<br/>\n"; 
        }
       }
       if (lang=="es") {
        o2 = o2 + "Calorias minimas necesarias: " + calmin + " Calorias 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 + " calorias 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 proteiana por dia.<br/>\n"; 
    }
    else { // English
     o2 = o2 + "Your diet should contain at least " + ii + " grams of protein per day.<br/>\n"; 
    }  
}  // diet_calc(lang)

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


function getRadioButtonSelectedValue(ctrl)
{
    for(i=0;i<ctrl.length;i++)
        if(ctrl[i].checked) return ctrl[i].value;
}


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) {
   document.getElementById('bmi').value="";
   document.getElementById('wthr').value="";
   document.getElementById('pctfat').value="";
   document.getElementById('leanbm').value="";
   document.getElementById('out').value="";
   document.getElementById('out2').value="";
}

//-->

