function log10(value) {
  return Math.log(value)/Math.LN10;
}

function bmr(s, a, w, h, f) {
  var result = 0;
  if (f>0) {
    result = Math.round(370 + 21.6 * (w-w*f/100));
  } else if (h>0 && s=="m") {
     result = Math.round(66.473 + 13.751*w + 5.0033*h - 6.755*a);
  } else if (h>0 && s=="f") {
     result = Math.round(655.0955 + 9.463*w + 1.8496*h - 4.6756*a);
  }
  return result;
}

function bmi(s, w, h) {
  result = new Array();
  var bmi, avg, verb;
  //
  bmi = Math.round(w/Math.pow(h,2)*10)/10;
  if (s == "m") {
    avg= 25.9;
  } else {
    avg= 24.9;
  }
  //
  if (bmi >= 40) {
     verb ="Grade 3 (morbid obesity)";
  } else if (bmi < 40 && bmi >= 35) {
     verb ="Grade 2 obesity";
  } else if (bmi < 35 && bmi >= 30) {
     verb ="Grade 1 obesity";
  } else if (bmi < 30 && bmi >= 25) {
     verb ="Overweight";
  } else if (bmi < 25 && bmi >= 18.5) {
     verb ="Healthy";
  } else if (bmi < 18.5) {
     verb ="Underweight";
  }
  //
  result["bmi"] = bmi;
  result["avg"] = avg;
  result["verb"] = verb;
  return result;
}

function onerepmax(reps, weight) {
  var result;
  if (reps < 10) {
     result = Math.round(weight/(1.0278-0.0278*reps)*100)/100;
  } else if (reps == 10){
     result = Math.round(weight/0.75*100)/100;
  } else {
    result = 0; 
  }
  return result;
}

// weight in kg, length in cm
function fat(sex,age,weight,height,neck,forearm,waist,abdomen,hips,thigh,calf,wrist) {
  result = new Array();
  var f1 = fat1(sex,height,neck,waist,abdomen,hips);
  var f2 = fat2(sex,weight,height,neck,forearm,hips,wrist);
  var f3 = fat3(sex,age,forearm,abdomen,hips,thigh,calf,wrist);
  //
  var avg = 0;
  var n = 0;
  if (f1>0) {
    avg += f1;
    n++;
  }
  if (f2>0) {
    avg += f2;
    n++;
  }
  if (f3>0) {
    avg += f3;
    n++;
  }
  if (n>0) {
    avg = Math.round(avg/n*10)/10;
  } else {
    avg = 0;
  }
  //
  result[0] = avg;
  result[1] = f1;
  result[2] = f2;
  result[3] = f3;
  return result;
}

// U.S. Navy Circumference Method #1 - men and women
function fat1(sex,height,neck,waist,abdomen,hips) {
  var result = 0;
  //
  if (sex=="m") {
    // men: abdomen, neck, height (cm)
    if (abdomen>0 && neck>0 && height>0)
      result = 495.0/(1.0324-0.19077*(log10(abdomen-neck))+0.15456*(log10(height)))-450.0;
  } else {
    // women: waist, hips, neck, height (cm)
    if (waist>0 && hips>0 && neck>0 && height>0)
      result = 495.0/(1.29579-0.35004*(log10(waist+hips-neck))+0.22100*(log10(height)))-450.0;
  }
  if (result < 0) result = 0;
  if (result > 100) result = 100;
  return Math.round(result*100.0)/100.0;
}

// U.S. Navy Circumference Method #2 - women only
function fat2(sex,weight,height,neck,forearm,hips,wrist) {
  var result = 0;
  if (sex=="f") {
    // women
    if (weight>0 && hips>0 && wrist>0 && forearm>0 && neck>0 && height>0)
      result = -71.938 + 105.42*log10(weight*2.2) + 0.4396/2.54*hips - 0.5086/2.54*wrist -3.997/2.54*forearm -1.3085/2.54*height -1.354/2.54*neck;
  }
  if (result < 0) result = 0;
  if (result > 100) result = 100;
  return Math.round(result*100.0)/100.0;
}

// book by Covert Bailey "Fit or Fat"
function fat3(sex,age,forearm,abdomen,hips,thigh,calf,wrist) {
  var result = 0;
  var k;
  //
  if (sex=="m") {
    // men
    if (abdomen>0 && hips>0 && forearm>0 && wrist>0) {
      if (age<=30) {
        k = 3;
      } else {
        k = 2.7;
      }
      result = abdomen/2.54 + hips/2.54/2.0 - k*forearm/2.54 - wrist/2.54;
    }
  } else {
    // women
    if (hips>0 && thigh>0 && calf>0 && wrist>0) {
      if (age<=30) {
        k = 0.8;
      } else {
        k = 1;
      }
      result = hips/2.54 + k*thigh/2.54 - calf/2.54*2.0 - wrist/2.54;
    }
  }
  if (result < 0) result = 0;
  if (result > 100) result = 100;
  return Math.round(result*100.0)/100.0;
}

