
function calcBEE(p) {

	var obj= document.formulaForm;
	 var Ht     = obj.Ht.value;
   var Wt     = obj.Wt.value;
   var Age    = obj.Age.value;
   var Activity = 1.0;
   var Factor   = 0;
   var TotCR, TotCrNRC;
   var lbKg=1.0;
   var WeightFactorNRC, WeightFactorHB, WeightFactorAve;
   
 if(isNaN(obj.lbsToLose.value)){obj.lbsToLose.value=0;}
 
	if (!obj.gender[0].checked && !obj.gender[1].checked) {
      alert("Click either male or female");
      return; }
 
   if (isNaN(obj.Ht.value) ) {alert("Height invalid");
      return; }

      
  if ( eval(obj.Ht.value) < 50) {  alert("Height too low");
      return; }
         
      
   if (isNaN(obj.Wt.value)) { alert("Weight invalid");
      return; }
      
  if (eval(obj.Wt.value)<40) { alert("Weight too low");
      return; }
      
   if ( isNaN(obj.Age.value) ) {alert("Age invalid");
      return; }
      
   if ( eval(obj.Age.value) < 18) {
      alert("Age to young");
      return; }
      

   // Conversions
   if (obj.cmin[1].checked) {
      Ht = Math.round(Ht * 2.54 * 100)/100;}    

   if (obj.kglb[1].checked) {
      Wt = Math.round(Wt*100 / 2.2)/100;
      lbKg = 2.2;}

   

   // Stress and Activity factors
   // Default activity factor of 1.0 set at declaration
   
  if (obj.gender[0].checked) { // male
   		if (obj.Activity[1].checked) {Activity = 1.3; }
   		if (obj.Activity[2].checked) {Activity = 1.55;}
   		if (obj.Activity[3].checked) {Activity = 1.8;}
   		if (obj.Activity[4].checked) {Activity = 2.1;}
   }
   else { // female  
   		if (obj.Activity[1].checked) { Activity = 1.3; }
   		if (obj.Activity[2].checked) {Activity = 1.6;}
   		if (obj.Activity[3].checked) {Activity = 1.7;}
   		if (obj.Activity[4].checked) {Activity = 1.9;}
   }
   
   Factor = parseFloat(Activity);

   // Calculation using BH equation
   
   // Male
   if (obj.gender[0].checked) {
      REE = 66.5 + 13.75 * Wt + 5.003 * Ht - 6.775 * Age;
      WeightFactHB = 13.75/lbKg;
      
      if (obj.Age.value > 60) {     ReeNRC =  13.5 * Wt + 487 ; WeightFactNRC = 13.5/lbKg; }
      else if (obj.Age.value > 30){ ReeNRC = 11.6 * Wt + 879; WeightFactNRC = 13.5/lbKg;}
      else                    {ReeNRC = 15.3 * Wt + 679; WeightFactNRC = 15.3/lbKg;}
   }

   else { // female
   	
   	  REE = 655.1 + 9.563 * Wt + 1.85 * Ht - 4.676 * Age;
   	   WeightFactHB = 9.563/lbKg;
   
      if (obj.Age.value > 60) {     ReeNRC = 10.5 * Wt + 596; WeightFactNRC = 10.5/lbKg;}
      else if (obj.Age.value > 30) {ReeNRC = 8.7 * Wt + 829; WeightFactNRC = 8.7/lbKg;}
      else {                       ReeNRC = 14.7 * Wt + 496; WeightFactNRC = 14.7/lbKg; }
         
  }
  
  // This is the cunningham formula for both men and women or any age
  var BodyFat = 0;
  if(!isNaN(obj.bodyFatP.value)){BodyFat = eval(obj.bodyFatP.value);}
  var ReeC= false;
 
  if(!isNaN(obj.bodyFatP.value) && (1 < BodyFat) ){
  	ReeC = 370+21.6*Wt*(1-obj.bodyFatP.value/100);
  }
  
// find upper and lower bounds etc.

if(isNaN(obj.hours.value)){obj.hours.value=0;} else {var hours=eval(obj.hours.value)}
if(isNaN(obj.minutes.value)){obj.minutes.value=0;}
if(isNaN(obj.times.value)){obj.times.value=0;}
if(isNaN(obj.extra.value)){obj.extra.value=0;}

    var timeExercising = (eval(obj.hours.value)+ eval(obj.minutes.value)/60 )*eval(obj.times.value)/7;
    var calExercising = eval(obj.extra.value)*eval(obj.times.value)/7;
    
      
   // They can't burn LESS exercising than they do resting around.
    
   
   var minTotCR = REE*Factor;
   var minTotCrNRC = ReeNRC*Factor;
   
      
   TotCR 	= REE	 * Factor*(24-timeExercising)/24 + calExercising ;  // harris-benedict.
   TotCrNRC	=ReeNRC * Factor*(24-timeExercising)/24 + calExercising ; // NRC
    TotCrC	=ReeC 	*Factor*(24-timeExercising)/24 + calExercising ; // cunningham
    
   
   TotCR=Math.max(minTotCR,TotCR);
   TotCrNRC=Math.max(minTotCrNRC, TotCrNRC);
   TotCrC = Math.max(ReeC*Factor, TotCrC);
   
  
   
    // average, upper and lower
   var three;  // take care of possibility that ReeC is not defined.

   
   if(ReeC){
   	
   		three=3;
   }
	else {
		
   		ReeC=0;
   		three = 2;
   		TotCrC=0;
	}
  
   
   
  
// avearage, upper and lower

 var aveCal = (TotCR+TotCrNRC+TotCrC)/three;
  
 
  // protein needs.
  // The RDA of 0.8 grams/ Kg is ok for sedentary people. 
  // endurance athletes need at least 65% more.
  // The american heart association recommends 15%.
  // Most articles recommend 12%
  
  // The NAS recommends 10% - 35%

  recDaily = new recommendations ( aveCal,Wt,calExercising,0,0,0);
  
  	var minProt= recDaily.minProt;
    var maxProt = recDaily.maxProt;
    var minProtCal=recDaily.minProtCal;
    var maxProtCal= recDaily.maxProtCal; 
  
 	var maxFatCal = recDaily.maxFatCal;
  	var minFatCal = recDaily.minFatCal;
  
  var maxFat = recDaily.maxFat
  var minFat = recDaily.minFat
  
  var maxCarbCal = recDaily.maxCarbCal;
  var minCarbCal = recDaily.minCarbCal;



// Diet recommendations:=========================================================
	// find diet recommendations
  if(isNaN(obj.lbsToLose.value)){obj.lbsToLose.value=0;} // set to 0 if it's not a number. 
  
  else { // don't let them lose more than 5 lbs/ month under any circumstances.
   obj.lbsToLose.value = Math.max(eval(obj.lbsToLose.value),0);
   obj.lbsToLose.value = Math.round(10*Math.min(eval(obj.lbsToLose.value), 5 * (lbKg/2.2) ))/10; 
  }
  
  var lbLose_Week =eval(obj.lbsToLose.value) * (2.2/ lbKg) /4 ; // in lbs
  
  if(obj.metabolism[0].checked){aveCal = aveCal*0.8};
  if(obj.metabolism[2].checked){aveCal = aveCal*1.2};
  
  obj.calToMaintain.value = Math.round(aveCal);
   
  var CalDiet = Math.max(aveCal -  lbLose_Week * 3500/7, 1200) ;
  
  obj.CalDiet.value = Math.round(CalDiet);
  var CalDef = aveCal-CalDiet;
  
  // determine real loss, and if it's smaller than the value in the box, replace.
  var realLoss= ( (CalDef * 28)/3500) * ( lbKg/2.2);  
  
  if(realLoss <  eval(obj.lbsToLose.value) ) {obj.lbsToLose.value = Math.round(10*realLoss)/10;}
  
 //  call with minimum fat and target fat % for diet.
 var percentFat, proteinLevel;
 
 if (obj.dietType[0].checked) { percentFat = 0.20; }
 else if (obj.dietType[1].checked)  { percentFat = 0.275; }
 else {percentFat = 0.35;} 
 
 if (obj.proteinType[0].checked) { proteinLevel = 0; }
 else if (obj.proteinType[1].checked)  {  proteinLevel = 1; }
 else { proteinLevel = 2;} 
 
 dietDaily = new recommendations( CalDiet ,Wt, calExercising,recDaily.absMinFat,percentFat,proteinLevel);

 obj.CalDiet2.value = Math.round(CalDiet);  

// set out output  
  obj.ProteinDietCal.value = Math.round(dietDaily.dietProtCal);   
  obj.ProteinDiet.value= Math.round(dietDaily.dietProtCal/4);
  
  obj.ProteinDietP.value = Math.round(1000*dietDaily.dietProtCal/CalDiet)/10;
  
 	// fat 
  obj.FatDietCal.value = Math.round(dietDaily.dietFatCal)  
  obj.FatDiet.value = Math.round(dietDaily.dietFatCal/9);
  obj.FatDietP.value = Math.round(1000*dietDaily.dietFatCal/CalDiet)/10;
  
  
  // carbohydrate-- remaining stuff.
  obj.CarbDietCal.value =  Math.round( dietDaily.dietCarbCal);  
  obj.CarbDiet.value = Math.round(dietDaily.dietCarbCal/4);
  obj.CarbDietP.value = Math.round(1000*dietDaily.dietCarbCal/CalDiet)/10;
  
  /* Fiber. Once fiber was just a way to stay "regular." Now there's a
recommended dietary allowance for fiber to help lower the risk of heart disease. 
Men ages 50 and under should take in 38 grams a day; 
women the same age need 25 grams a day.

 For those 50 and older, the recommended intake is 
 30 grams for men,
  21 for women. This may sound intimidating, but a diet high in grains, beans, fruit
and  */
    if (obj.gender[0].checked) {//men
    	if(Age<50.99){  obj.Fiber.value = 38}
    	else{	obj.Fiber.value = 30}
    }
    else {//women
    	if(Age<50.99){obj.Fiber.value = 25}
    	else{obj.Fiber.value = 21}
    }
}

// isNum function

function IsNum(str) {
      for (var i = 0; i < str.length; i++) {
         var chr = str.substring(i, i+1);
         if ((chr < "0" || "9" < chr) && chr != ".")
            return false; 
      }
      if (eval(str)==0)
         return false;
      return true;
   }
 //---------------------------------------------------------  
   
function recommendations(aveCal,Wt,exCal, absMinFat, fracFat,protein) {

	// fracFat should be the fraction for the diet type.
	// absMinFat should be the minimum for the person-- calculated previously.
	
	// protein = 0 is low protein, = 1 is medium protein and =2 is high protein.

	this.minProtCal = Math.max(0.8*Wt, 0.10*aveCal);
	this.maxProtCal =  Math.max(this.minProtCal,0.35*aveCal);
	
 	 	
 	if(protein == 0){		this.dietProtCal =this.minProtCal;}
 	else if(protein == 1){	this.dietProtCal = (this.minProtCal + this.maxProtCal)/2;} // average
 	else {					this.dietProtCal =this.maxProtCal;}
	
	this.minProt=this.minProtCal/4;
 	this.maxProt= this.maxProtCal/4; 

 	
	this.maxFatCal = aveCal*0.35;
	this.minFatCal = Math.max(aveCal*0.20,absMinFat);// limit
	this.absMinFatCal= aveCal*0.06;
	
	
	this.dietFatCal = Math.max(absMinFat*9,fracFat*aveCal); //
		

	this.maxFat = this.maxFatCal/9;
	this.minFat =this. minFatCal/9;
	this.absMinFat = this.absMinFatCal/9;
  
	this.maxCarbCal = aveCal-(this.minProtCal+this.minFatCal);
 	this.minCarbCal = aveCal-(this.maxProtCal+this.maxFatCal);
 	this.absMaxCarbCal = aveCal-(this.minProtCal+this.absMinFatCal);
 	
 	this.dietCarbCal = aveCal-(this.dietProtCal+this.dietFatCal);
 	
 	// correct for too few carbs
 	var absMinCarb = 0.45*aveCal; // this never falls below 520-- the other minimum recommended
 	var absMaxCarb = 0.65*aveCal;
 // the only time the carb cals will be too low is if the person picks high protein and 
 // high fat. If the carbs end up too low, cut equally from protein and fat.
 	
 	var x = this.dietProtCal/(this.dietProtCal+this.dietFatCal);
 	
 	if(this.dietCarbCal < absMinCarb){
 		var makeUpCal = absMinCarb-this.dietCarbCal;
 		
 		this.dietCarbCal= absMinCarb;
 		this.dietProtCal = this.dietProtCal - x* makeUpCal;
 		this.dietFatCal = this.dietFatCal- (1-x) * makeUpCal;
 		
 		this.dietProt= this.dietProtCal/4;
 		this.dietCarb = absMinCarb/4;
 		this.dietFat= this.dietFatCal/9;
 	}
 	
 	if(this.dietCarbCal > absMaxCarb){
 		var AddCal = this.dietCarbCal-absMaxCarb;
 		
 		this.dietCarbCal= absMaxCarb;
 		this.dietProtCal = this.dietProtCal + x* AddCal;
 		this.dietFatCal = this.dietFatCal+ (1-x)* AddCal;
 		
 		this.dietProt= this.dietProtCal/4;
 		this.dietCarb = absMinCarb/4;
 		this.dietFat= this.dietFatCal/9;
 	}


}


