
// Body Fat Calculator
// copyright 23rd April 2006, by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
function valButton(btn)
{
   var cnt = - 1;
   for (var i = btn.length - 1; i > - 1; i -- )
   {
      if (btn[i].checked)
      {
         cnt = i;
         i = - 1;
      }
   }
   if (cnt > - 1) return btn[cnt].value;
   else return null;
}
function stripBlanks(fld)
{
   var result = "";
   var c = 0;
   for (i = 0; i < fld.length; i ++ )
   {
      if (fld.charAt(i) != " " || c > 0)
      {
         result += fld.charAt(i);
         if (fld.charAt(i) != " ") c = result.length;
      }
   }
   return result.substr(0, c);
}

function maleFat(h, n, w)
{
   return thou(495 / (1.0324 - 0.19077 * (Math.log(w - n) / Math.LN10) + 0.15456 * (Math.log(h) / Math.LN10)) - 450);
}

function femaleFat(h, n, w, r)
{
   return thou(495 / (1.29579 - 0.35004 * (Math.log(w + r - n) / Math.LN10) + 0.22100 * (Math.log(h) / Math.LN10)) - 450);
}

function thou(n)
{
   return Math.round(n * 10) / 10 + '%';
}

function sex(thisform)
{
   var s = valButton(thisform.s); thisform.r.disabled = (s != 'f');
}

function calc(thisform)
{
   var s = valButton(thisform.s);
   if (s == null)
   {
      alert("You must enter your sex");
      thisform.s.focus();
      return false;
   }
   var d = valButton(thisform.d);
   if (d == null)
   {
      alert("You must choose centimetres or inches");
      thisform.d.focus();
      return false;
   }
   d = Number(d);
   var h = stripBlanks(thisform.h.value);
   if (h == '')
   {
      alert("You must enter your height");
      thisform.h.focus();
      return false;
   }
   if (h != Number(h) || (h = Number(h * d)) < 100 || h > 250)
   {
      alert("Invalid height entered");
      thisform.h.focus();
      return false;
   }
   var n = stripBlanks(thisform.n.value);
   if (n == '')
   {
      alert("You must enter your neck size");
      thisform.n.focus();
      return false;
   }
   if (n != Number(n) || (n = Number(n * d)) < 20 || n > 60)
   {
      alert("Invalid neck size entered");
      thisform.n.focus();
      return false;
   }

   var w = stripBlanks(thisform.w.value);
   if (w == '')
   {
      alert("You must enter your waist size");
      thisform.w.focus();
      return false;
   }

   if (w != Number(w) || (w = Number(w * d)) < 30 || w > h)
   {
      alert("Invalid waist size entered");
      thisform.w.focus();
      return false;
   }
   var r = 0;
   if (s == 'f')
   {
      r = stripBlanks(thisform.r.value);
      if (r == '')
      {
         alert("You must enter your hip size");
         thisform.r.focus();
         return false;
      }
      if (r != Number(r) || (r = Number(r * d)) < 40 || h > 200)
      {
         alert("Invalid hip size entered");
         thisform.r.focus();
         return false;
      }
   }
   switch(s)
   {
      case 'm' :
         thisform.f.value = maleFat(h, n, w);
         break;
      case 'f' :
         thisform.f.value = femaleFat(h, n, w, r);
   }
}

