<!-- Author: Brian Mackenzie -->
<!-- Date: 11th March 2004 -->
<!-- Version: 1.0 -->

<!-- Function:  Calculate the results from body fat measurements -->

<!-- Required by: fatcent.htm -->

<!-- Copyright Brian Mackenzie 2004 -->


function fatcalculate()

{

female = new Array(0,14.1,17.0,19.8,21.4,16.8,19.4,22.2,24.0,19.5,21.8,24.5,26.6,21.5,23.7,26.4,28.5,
23.4,25.5,28.2,30.3,25.0,26.9,29.6,31.9,26.5,28.2,31.0,33.4,27.8,29.4,32.1,34.6,29.1,30.6,33.2,35.7,
30.2,31.6,34.1,36.7,31.2,32.5,35.0,37.7,32.2,33.4,35.9,38.7,33.1,34.3,36.7,39.6,34.0,35.1,37.5,40.4,
34.8,35.8,38.3,41.2,35.6,36.5,39.0,41.9,36.4,37.2,39.7,42.6);

male = new Array(0,8.1,12.2,12.2,12.6,10.5,14.2,15.0,15.6,12.9,16.2,17.7,18.6,14.7,17.7,19.6,20.8,
16.4,19.2,21.4,22.9,17.7,20.4,23.0,24.7,19.0,21.5,24.6,26.5,20.1,22.5,25.9,27.9,21.2,23.5,27.1,29.2,
22.2,24.3,28.2,30.4,23.1,25.1,29.3,31.6,24.0,25.9,30.3,32.7,24.8,26.6,31.2,33.8,25.5,27.2,32.1,34.8,
26.2,27.8,33.0,35.8,26.9,28.4,33.7,36.6,27.6,29.0,34.4,37.4);

var age = document.fat.age.options[document.fat.age.selectedIndex].value;
var fold = document.fat.fold.value;
var gender = document.fat.gender.options[document.fat.gender.selectedIndex].value;
var wtunits = document.fat.wtunits.options[document.fat.wtunits.selectedIndex].value;
var wt = document.fat.wt.value;

  if (wtunits == "kilos")
    {
        wt *= 2.205;
    }

  if (gender == "male")
    {
      if (age == '17')
        {
            x=1;
        }

      if (age == '30')
        {
            x=2;
        }

      if (age == '40')
        {
             x=3;
        }

      if (age == 50)
        {
             x=4;
        }

      if (fold<20)
	{
	  fold=0;
	}

      if (fold>100)
        {
          fold=0;
	}

      y=0;
      if (fold>0)
        {
	  nfold = Math.round(fold/10)*10;

	  if (Math.abs(nfold-fold)>2.5) 
	    {
	      if (nfold<fold)
		{
		  nfold=nfold+5;
		}

	      else {nfold=nfold-5}
			  }
	      b = Math.floor(((nfold-20)/5)+1);
	      y = ((b-1)*4) + x;
	   }


           tfat = male [y];
          document.fat.bodyfat.value = male[y];
        }

    if (gender == "female")
    {
        if (age == '17')
        {
            x=1;
        }

      if (age == '30')
        {
            x=2;
        }

      if (age == '40')
        {
             x=3;
        }

      if (age == 50)
        {
             x=4;
        }

      if (fold<20)
	{
	  fold=0;
	}

      if (fold>100)
	{
	  fold=0;
	}

     y=0;
     if (fold>0)
       {
         nfold = Math.round(fold/10)*10;
	 if (Math.abs(nfold-fold)>2.5) 
	   {
	     if (nfold<fold)
	       { 
                 nfold=nfold+5;
	       }

	     else {nfold=nfold-5}
			  }
	     b = Math.floor(((nfold-20)/5)+1);
	     y = ((b-1)*4) + x;
	   }


   tfat = female [y];
   document.fat.bodyfat.value = female[y];

      }


  // determine weight of fat and lean body weight

  wf = wt *tfat / 100;
  wl = wt - wf;	 
  document.fat.wfat.value = nt2dp(wf,2);
  document.fat.wlean.value = nt2dp(wl,2);
  document.fat.wfatkg.value = nt2dp(wf / 2.205,2);
  document.fat.wleankg.value = nt2dp(wl /2.205,2);
}


function vclear()
{
  document.fat.bodyfat.value = "";
  document.fat.wfat.value = "";
  document.fat.wlean.value = "";
  document.fat.wfatkg.value = "";
  document.fat.wleankg.value = "";
}



function bodyfat1calculate()


{
  var heightunits = document.bodyfat1.heightunits.options[document.bodyfat1.heightunits.selectedIndex].value;
  var height = document.bodyfat1.height.value;
  var weightunits = document.bodyfat1.weightunits.options[document.bodyfat1.weightunits.selectedIndex].value;
  var weight = document.bodyfat1.weight.value;


  if (heightunits == "centimeters")
    {
        height /= 2.54;
    }

  if (weightunits == "kilos")
    {
        weight *= 2.2;
    }
  var fatweight = height / Math.pow(weight,1 / 3 );
  var bodyfat = 265.21364 + (fatweight * -18.17273) + (-0.090909 * fatweight * fatweight) ;
       
  document.bodyfat1.bodyfat.value = nt2dp(bodyfat,1);

  // determine weight of fat and lean body mass

  wf = weight * Math.round(bodyfat) / 100;
  wl = weight - wf;	 

  document.bodyfat1.wfat.value = nt2dp(wf,2);
  document.bodyfat1.wlean.value = nt2dp(wl,2);
  document.bodyfat1.wfatkg.value = nt2dp(wf / 2.205,2);
  document.bodyfat1.wleankg.value = nt2dp(wl /2.205,2);
}

function valclear()

{
  document.bodyfat1.bodyfat.value = "";
  document.bodyfat1.wfat.value = "";
  document.bodyfat1.wlean.value = "";
  document.bodyfat1.wfatkg.value = "";
  document.bodyfat1.wleankg.value = "";
}

function nt2dp(num,dp)
{
<!-- rounds num to dp decimal places -->
  num=num*1+(0.55/Math.pow(10,dp));
  if (dp>0) dp=dp+1;
  b=Math.floor(num).toString().length+dp;
  return num.toString().substr(0,b);
}