// BMR Calculator
// Copyright - Property of DietandFitnessToday.com 2006
// No part of this document may be reproduced in any form

function calculateBMR(){

  var frm    = document.mainForm;
  var KG     = frm.inputKG.value;
  var age    = frm.inputAge.value;
  var pounds = frm.inputPounds.value;
  var stone  = frm.inputStone.value;
  var pnd    = frm.inputPnd.value;
  var metre  = frm.inputMetre.value;
  var inches = frm.inputInches.value;
  var feet   = frm.inputFeet.value;
  var inch   = frm.inputInch.value;

  var anum=/(^\d+$)|(^\d+\.\d+$)/;
  var wtNo = 0;
  var htNo = 0;

  if (!age){
    alert('Please type a non-zero valid age');
    frm.inputAge.focus();
    return;
  }else if (!anum.test(age)){
    frm.inputAge.focus();
    alert('Please type a valid age');
    return;
  }

  // verify wt input 
  if (KG){
    wtNo++;
  }	
  if (pounds){
    wtNo++;
  }
  if (stone || pnd){
    wtNo++;
  }

  if (wtNo > 1 || wtNo == 0){
    alert('Please enter a single weight in one of three rows');
    frm.inputKG.focus();
    return;
  }

  if (KG){
    if (!anum.test(KG)){
      frm.inputKG.focus();
      alert('Please type a valid weight');
      return;
    }
  }else if(pounds){
    if (!anum.test(pounds)){
      frm.inputPounds.focus();
      alert('Please type a valid weight');
      return;
    }else{
      KG += pounds * 0.45359237;
    }
  }else if(stone||pnd){
    if (stone){
      if (!anum.test(stone)){
	frm.inputStone.focus();
	alert('Please type a valid weight');
	return;
      }else{
	KG = (+KG) + (stone * 6.35029318);
      }
    }
    if (pnd){
      if (!anum.test(pnd)){
	frm.inputPnd.focus();
	alert('Please type a valid weight');
	return;
      }else{
	KG = (+KG) + (pnd * 0.45359237);
      }
    }
  }else{
    alert('Please type a valid weight');
    return;
  }

  if (metre){
    htNo++;
  }	
  if (inches){
    htNo++;
  }
  if (feet || inch){
    htNo++;
  }

  if (htNo > 1 || htNo == 0){
    alert('Please enter a single height in one of three rows');
    frm.inputMetre.focus();
    return;
  }

  if (metre){
    if (!anum.test(metre)){
      frm.inputMetre.focus();
      alert('Please type a valid height');
      return;
    }
  }else if(inches){
    if (!anum.test(inches)){
      frm.inputInches.focus();
      alert('Please type a valid height');
      return;
    }else{
      metre = (+metre) + (inches * 0.0254);
    }
  }else if(feet||inch){
    if (feet){
      if (!anum.test(feet)){
	frm.inputFeet.focus();
	alert('Please type a valid height');
	return;
      }else{
	metre = (+metre) + (feet * 0.3048);
      }
    }
    if (inch){
      if (!anum.test(inch)){
	frm.inputInch.focus();
	alert('Please type a valid hight');
	return;
      }else{
	metre = metre + (inch * 0.0254);
	window.status = metre;
      }
    }
  }else{
    alert('Please type a valid height');
    return;
  }

  var bmrM =  (13.751 * KG) + (500.33 * metre) - (6.76 * age) + 66.473;
  var bmrF =  (9.463 * KG) + (184.96 * metre) - (4.68 * age) + 655.51;
  frm.bmrMale.value=sprintf(bmrM,2);
  frm.bmrFemale.value=sprintf(bmrF,2);
}

function clearAllFields(){
  var frm = document.mainForm;
  frm.inputAge.value='';
  frm.inputKG.value='';
  frm.inputPounds.value='';
  frm.inputStone.value='';
  frm.inputPnd.value='';
  frm.inputMetre.value='';
  frm.inputInches.value='';
  frm.inputFeet.value='';
  frm.inputInch.value='';

  frm.outputBMI.value='';
  frm.outputDescription.value='';
}

function sprintf(num, decimalNum){
  var tmpNum = num;

  // Return the right number of decimal places
  tmpNum *= Math.pow(10,decimalNum);
  tmpNum = Math.floor(tmpNum);
  tmpNum /= Math.pow(10,decimalNum);
  var tmpStr = new String(tmpNum);

  return tmpStr;
}

