<!--
// Begin Hiding Script


function calcNutrition(obj) {

// calculate calories from grain-
/*
Grain	Whole	111.8	3.5		23.5	2.3	7.6	83%	7%	0.3		
Grain	Refined	98.4	3.3		20.2	3.0	9.6	81%	11%	0.3	

I'm going to add 1 grams of fat (9 calories) to account for preparation. 
Some things like bread or tortilla's are already prepared.  But rice isn't.
Rice and other boiled grains usually have oil added when prepared.
*/

var typWGrainCal 	= 111.8+9; // calories
var typWGrainProt	= 3.5;  // grams
var typWGrainCarb	= 23.5; // grams
var typWGrainFiber 	= 2.3; // grams
var typWGrainFat	= 7.6+9; // cal


var typRGrainCal	= 98.4+9; // calories
var typRGrainProt 	= 3.3;  // grams
var typRGrainFat 	= 20.2; // cal
var typRGrainFiber	= 3.0;// grams
var typRGrainCarb 	= 9.6+9; // grams

wholeGrain = new nutritionCont(typWGrainCal,typWGrainFat,typWGrainProt,typWGrainCarb,
typWGrainFiber,obj.wholeGrain.value)

refGrain = 
  new nutritionCont(typRGrainCal,typRGrainFat,typRGrainProt,typRGrainCarb,typRGrainFiber,obj.refGrain.value)

var totGrainCal = refGrain.cal + wholeGrain.cal;
var totGrainFat =refGrain.fat + wholeGrain.fat;
var totGrainProt = refGrain.prot + wholeGrain.prot;
var totGrainFiber = refGrain.fiber+wholeGrain.fiber;
var totGrainCarb = refGrain.carb+wholeGrain.carb;

obj.GrainCal.value = Math.round(totGrainCal);
obj.GrainFat.value = Math.round( totGrainFat);
obj.GrainFatG.value = Math.round( totGrainFat/9);

obj.GrainCarb.value = Math.round(totGrainCarb);
obj.GrainProtein.value = Math.round(totGrainProt);
obj.GrainFiber.value = Math.round(totGrainFiber);


if(totGrainCal >0) {obj.GrainFatP.value= Math.round(100 * totGrainFat/totGrainCal )}
else { obj.GrainFatP.value = "??";};

// calculate calories from dairy---
// I averaged the milk and cheese avarages, otherwise the light and whole
// milk categories came out too low in calories. (Because 1 oz of cheese is higher
// fat but lower calorie.
/*
		Part		Cal		prot	carb	fiber	fat		%cab	%fat		ROUND	
Dairy	skim		86.9	7.0		13.6	0.0		7.3	62%	8%	0.3		
Dairy	light (2%)	123.5	8.4		11.3	0.0		43.3	36%	35%	1.5		
Dairy	whole		162.0	10.3	8.2		0.0		89.7	21%	55%	3.0		
Dairy	tip			189.8	3.0		8.0		0.0		145.8	18%	75%	4.8		*/	

var typDairyCal = 86.9; // calories
var typDairyProt = 7;  // grams
var typDairyCarb = 13.6; // grams
var typDairyFiber= 0;
var typDairyFat = 7.3; // cal

// Lite
var typLiteDairyCal   = 123.5; // grams
var typLiteDairyProt  = 8.4;  // grams
var typLiteDairyCarb  = 11.3; // grams
var typLiteDairyFiber =  0;
var typLiteDairyFat   = 43.3; // cal


// Whole
var typWholeDairyCal   = 162.0; // grams
var typWholeDairyProt  =  10.3;  // grams
var typWholeDairyCarb  = 8.2; // grams
var typWholeDairyFiber =  0.0;
var typWholeDairyFat   = 89.7; // cal


Dairy = 
  new nutritionCont(typDairyCal,typDairyFat,typDairyProt,
                    typDairyCarb,typDairyFiber,obj.dairy.value);


DairyLite =new nutritionCont(typLiteDairyCal,typLiteDairyFat,typLiteDairyProt,
                             typLiteDairyCarb,typLiteDairyFiber,obj.dairyLite.value);
                    
DairyWhole =new nutritionCont(typWholeDairyCal,typWholeDairyFat,typWholeDairyProt,
                               typWholeDairyCarb,typWholeDairyFiber,obj.dairyWhole.value);

var totDairyCal = Dairy.cal + DairyLite.cal+DairyWhole.cal;
var totDairyFat = Dairy.fat + DairyLite.fat+DairyWhole.fat;
var totDairyProt = Dairy.prot + DairyLite.prot+DairyWhole.prot;
var totDairyFiber = Dairy.fiber+DairyLite.fiber+DairyWhole.fiber;
var totDairyCarb = Dairy.carb+DairyLite.carb+DairyWhole.carb;

obj.DairyCal.value = Math.round(totDairyCal)
obj.DairyFat.value = Math.round(totDairyFat);
obj.DairyFatG.value = Math.round(totDairyFat/9);

obj.DairyCarb.value = Math.round(totDairyCarb);
obj.DairyProtein.value = Math.round(totDairyProt);
obj.DairyFiber.value = Math.round(totDairyFiber);

if(totDairyCal >0) {obj.DairyFatP.value = Math.round(100* totDairyFat/totDairyCal);}
else { obj.DairyFatP.value = "??";};

//===================Veg
// calculate calories from vegetables---------------------
/*
Vegetable	LowCal		11.5	0.8		2.4		1.0		2.0		81%	17%	0.0		
Vegetable	Standard	28.6	1.0		6.6		1.4		2.4		93%	8%	0.0		
Vegetable	Starchy		67.5	1.6		13.7	2.3		2.7		82%	4%	0.0	
Vegetable	Fat			114.5	1.4		5.3		2.9		98.0	18%	86%	3.3		
	
*/
var typLeafCal = 11.5; // calories
var typLeafProt = 0.8;  // grams
var typLeafCarb = 2.4; // grams
var typLeafFiber= 1.0
var typLeafFat = 2.0;

var typStandCal = 28.6; // calories
var typStandProt = 1.0;  // grams
var typStandCarb = 6.6; // grams
var typStandFiber= 1.4;
var typStandFat = 2.4; // grams

var typStarchCal = 67.5; // calories
var typStarchProt = 1.6;  // grams
var typStarchCarb = 13.7; // grams
var typStarchFiber= 2.3;
var typStarchFat = 2.7; // grams

var typFatCal = 114.5; // calories
var typFatProt = 1.4;  // grams
var typFatCarb = 5.3; // grams
var typFatFiber= 2.9;
var typFatFat = 98; // grams


VegLeaf = 
  new nutritionCont(typLeafCal,typLeafFat,typLeafProt,
                    typLeafCarb,typLeafFiber,obj.vegLeaf.value);
        
VegStand = 
  new nutritionCont(typStandCal,typStandFat,typStandProt,
                    typStandCarb,typStandFiber,obj.vegStand.value);
VegStarch = 
  new nutritionCont(typStarchCal,typStarchFat,typStarchProt,
                    typStarchCarb,typStarchFiber,obj.vegStarch.value);
                    
VegFat = 
  new nutritionCont(typFatCal,typFatFat,typFatProt,
                    typFatCarb,typFatFiber,obj.vegFat.value);

var totVegCal   =  VegLeaf.cal + VegStand.cal+ VegStarch.cal+VegFat.cal;
var totVegFat   =  VegLeaf.fat + VegStand.fat + VegStarch.fat+VegFat.fat;
var totVegProt  =  VegLeaf.prot + VegStand.prot+ VegStarch.prot+VegFat.prot;
var totVegFiber =  VegLeaf.fiber + VegStand.fiber+ VegStarch.fiber+VegFat.fiber;
var totVegCarb  =  VegLeaf.carb + VegStand.carb+ VegStarch.carb+VegFat.carb;


//==========FRUIT================================

/*
Part			Cal		prot	carb	fiber	fat		%cab	%fat		ROUND	
Fruit	Fruit	55.5	0.9		14.0	1.6		2.6		101%	5%		0.0		
Fruit	Juice	64.3	0.3		15.7	0.0		0.5		98%		1%		0.0		
Fruit	Coconut	32.3	0.3		1.3		0.9		28.0	16%		87%		0.9		
*/
var typJuiceCal = 64.3; // calories
var typJuiceProt = 0.3;  // grams
var typJuiceCarb = 15.7; // grams
var typJuiceFiber= 0;
var typJuiceFat = 0.5; // grams

var typFruitCal = 55.5; // calories
var typFruitProt = 0.9;  // grams
var typFruitCarb = 14.0; // grams
var typFruitFiber= 1.6
var typFruitFat = 2.6; 
 



FrWhole = 
  new nutritionCont(typFruitCal,typFruitFat,typFruitProt,
                    typFruitCarb,typFruitFiber,obj.fruit.value);
        
FrJuice = 
  new nutritionCont(typFruitCal,typFruitFat,typFruitProt,
                    typFruitCarb,typFruitFiber,obj.juice.value);

var totFrCal   =  FrJuice.cal   + FrWhole.cal;
var totFrFat   =  FrJuice.fat   + FrWhole.fat;
var totFrProt  =  FrJuice.prot  + FrWhole.prot;
var totFrFiber =  FrJuice.fiber + FrWhole.fiber;
var totFrCarb  =  FrJuice.carb  + FrWhole.carb;


obj.FrVegCal.value     = Math.round(totVegCal+totFrCal)
obj.FrVegFat.value     = Math.round(totVegFat+totFrFat);
obj.FrVegFatG.value     = Math.round((totVegFat+totFrFat)/9);
obj.FrVegCarb.value    = Math.round(totVegCarb+totFrCarb);
obj.FrVegProtein.value = Math.round(totVegProt+totFrProt);
obj.FrVegFiber.value   = Math.round(totVegFiber+totVegFiber);

if((totVegCal+totFrCal) >0) {
      obj.FrVegFatP.value = Math.round( 100* (totVegFat+totFrFat)/(totVegCal+totFrCal)  );}
else { obj.FrVegFatP.value = "??";}

//==============Protein ==========================
// calculate calories from protein-------------------
var calProt, calCarbProt, calProtFruit, calFatFruit;

/*
Meat	Lean -- cut off		72.5	14.9	0.2		0.0		7.2		0%	9%	0.3		
Meat	Standard -- cut off	97.7	14.3	0.9		0.1		31.4	4%	32%	0.9		
Meat	Fatty- cut off		116.6	13.5	0.1		0.0		58.5	0%	49%	2.1		
*/
	
var typMeatCal = 72.5; // calories
var typMeatProt = 14.9;  // grams
var typMeatCarb = 0.2; // grams
var typMeatFiber= 0;
var typMeatFat = 7.2; // cal

var typSMeatCal = 97.7; // calories
var typSMeatProt = 14.3;  // grams
var typSMeatCarb = 0.9; // grams
var typSMeatFiber= 0.1;
var typSMeatFat = 31.4; // cal

var typFMeatCal = 116.6; // calories
var typFMeatProt = 13.5;  // grams
var typFMeatCarb = 0.1; // grams
var typFMeatFiber= 0.0;
var typFMeatFat = 58.5; // cal

/*
Meat	Bean		112.9	8.0		20.3	7.5		3.1		72%	3%	0.0
Meat	soybeans	149.0	14.5	8.5		5.0		69.3	23%	47%	2.4
Meat	nuts		163.0	9.4		8.1		3.6		99.6	21%	59%	3.3
*/


var typBeanCal = 112.9; // calories
var typBeanProt = 8.;  // grams
var typBeanCarb = 20.3; // grams
var typBeanFiber= 7.5
var typBeanFat = 3.1; // grams

var typSoyCal = 149.0; // calories
var typSoyProt = 14.5;  // grams
var typSoyCarb = 8.5; // grams
var typSoyFiber= 5.0
var typSoyFat = 69.3; 

var typNutCal = 163.
var typNutProt= 14.5
var typNutCarb= 8.1
var typNutFiber= 3.6
var typNutFat=99.6

lean = 
  new nutritionCont(typMeatCal,typMeatFat,typMeatProt,
                    typMeatCarb,typMeatFiber,obj.lean.value);
        
standard = 
 new nutritionCont(typSMeatCal,typSMeatFat,typSMeatProt,
                    typSMeatCarb,typSMeatFiber,obj.stand.value);
                   
fatty = 
  new nutritionCont(typFMeatCal,typFMeatFat,typFMeatProt,
                    typFMeatCarb,typFMeatFiber,obj.fat.value);
                    
bean = 
  new nutritionCont(typBeanCal,typBeanFat,typBeanProt,
                    typBeanCarb,typBeanFiber,obj.bean.value);
                    
soy =new nutritionCont(typSoyCal,typSoyFat,typSoyProt,
                    typSoyCarb,typSoyFiber,obj.soy.value);
                    
nuts =new nutritionCont(typNutCal,typNutFat,typNutProt,
                    typNutCarb,typNutFiber,obj.nut.value);
                    
var totProtCal   =   bean.cal    + lean.cal   + standard.cal   + fatty.cal   + nuts.cal   + soy.cal;                    
var totProtFat   =   bean.fat    + lean.fat   + standard.fat   + fatty.fat   + nuts.fat   + soy.fat;
var totProtProt  =   bean.prot   + lean.prot  + standard.prot  + fatty.prot  + nuts.prot  + soy.prot;
var totProtFiber =   bean.fiber  + lean.fiber + standard.fiber + fatty.fiber + nuts.fiber + soy.fiber;
var totProtCarb  =   bean.carb   + lean.carb  + standard.carb  + fatty.carb  + nuts.carb  + soy.carb;

obj.ProteinCal.value       = Math.round(totProtCal);
obj.ProteinFat.value       = Math.round(totProtFat);
obj.ProteinFatG.value       = Math.round(totProtFat/9);
obj.ProteinCarb.value      = Math.round(totProtCarb);
obj.ProteinProtein.value   = Math.round(totProtProt);
obj.ProteinFiber.value     = Math.round(totProtFiber);

if( totProtCal >0) {
       obj.ProteinFatP.value = Math.round( 100* totProtFat/totProtCal  );}
else { obj.ProteinFatP.value = "??";}

// ============== TIPS=================================

	
var typFatCal = 35; // calories
var typFatCarb = 0; // grams
var typFatFiber= 0
var typFatProt = 0;  // grams
var typFatFat = 35; // grams

var typSugarCal = 35; // calories
var typSugarCarb = 9; // grams
var typSugarFiber= 0
var typSugarProt = 0;  // grams
var typSugarFat = 0; // grams

var typAlcoholCal = 35; // calories
var typAlcoholCarb = 2.6; // grams
var typAlcoholFiber= 0
var typAlcoholProt = 0.1;  // grams
var typAlcoholFat = 0; // grams

var typMixedCal = 35; // calories
var typMixedProt = 0.7;  // grams
var typMixedCarb = 4.6; // grams
var typMixedFiber= 0.5;
var typMixedFat = 10.5; // grams


fatTip = 
  new nutritionCont(35,35,0, 0,0,obj.fatTip.value);
        
sugarTip= 
  new nutritionCont(35,0,0,8.75,0,obj.sugarTip.value);
                   
mixedTip = 
  new nutritionCont(35,typMixedFat,typMixedProt,
                    typMixedCarb,typMixedFiber,obj.mixedTip.value);
                    
alcoholTip = 
  new nutritionCont(35,0,0,typAlcoholCarb,0,obj.alcoholTip.value);
                    
var totTipCal   =   fatTip.cal     + sugarTip.cal    + mixedTip.cal  + alcoholTip.cal;                    
var totTipFat   =   fatTip.fat     + sugarTip.fat    + mixedTip.fat   + alcoholTip.fat;   
var totTipProt  =   fatTip.prot    + sugarTip.prot   + mixedTip.prot  + alcoholTip.prot ;   
var totTipFiber =   fatTip.fiber   + sugarTip.fiber  + mixedTip.fiber + alcoholTip.fiber;   
var totTipCarb  =   fatTip.carb    + sugarTip.carb   + mixedTip.carb  + alcoholTip.carb;   

obj.TipCal.value       = Math.round(totTipCal);
obj.TipFat.value       = Math.round(totTipFat);
obj.TipFatG.value       = Math.round(totTipFat/9);
obj.TipCarb.value      = Math.round(totTipCarb);
obj.TipProtein.value   = Math.round(totTipProt);
obj.TipFiber.value     = Math.round(totTipFiber);

if( totTipCal >0) {
       obj.TipFatP.value = Math.round(  100* totTipFat/totTipCal  );}
else { obj.TipFatP.value = "??";}

// Totals=================================

obj.TotCal.value = 
       Math.round(totTipCal+totProtCal+totGrainCal
                  +totDairyCal+totVegCal+totFrCal);

obj.TotFat.value = 
     Math.round(totTipFat+totProtFat+totGrainFat
                +totDairyFat+totVegFat+totFrFat);
                
obj.TotFatG.value = 
     Math.round((totTipFat+totProtFat+totGrainFat
                +totDairyFat+totVegFat+totFrFat)/9);

obj.TotProtein.value = 
  Math.round(totTipProt+totProtProt+totGrainProt
             +totDairyProt+totVegProt+totFrProt);

obj.TotCarb.value = 
   Math.round(totTipCarb+totProtCarb+totGrainCarb
             +totDairyCarb+totVegCarb+totFrCarb);
             
             
obj.TotFiber.value = 
   Math.round(totTipFiber+totProtFiber+totGrainFiber
             +totDairyFiber+totVegFiber+totFrFiber);
  
if( eval(obj.TotCal.value) >0) {
       obj.TotFatP.value = Math.round(  100* eval(obj.TotFat.value)/eval(obj.TotCal.value)  );}
else { obj.TotFatP.value = "??";}

// Calculate the number of recommended servings.

var tar=eval(obj.targetCalorie.value)

MinPor = new minPlan();
MaxPor = new maxPlan();


var two=5;

obj.recGrain.value = 
   Math.round(two*interpolate(MinPor.tar, MaxPor.tar,
               tar,MinPor.totGrain,MaxPor.totGrain))/two;
               
obj.recVeg.value = 
   Math.round(two*interpolate(MinPor.tar, MaxPor.tar,
               tar,MinPor.totVeg,MaxPor.totVeg))/two;
               
obj.recFruit.value = 
   Math.round(two*interpolate(MinPor.tar, MaxPor.tar,
               tar,MinPor.totFruit,MaxPor.totFruit))/two;
               
obj.recDairy.value = 
   Math.round(two*interpolate(MinPor.tar, MaxPor.tar,
               tar,MinPor.LiteDairy,MaxPor.LiteDairy))/two;

obj.recMeat.value = 
   Math.round(two*interpolate(MinPor.tar, MaxPor.tar,
               tar,MinPor.StandMeat, MaxPor.StandMeat))/two;

var extTip = 
          interpolate(MinPor.tar, MaxPor.tar,tar,MinPor.totalTip,MaxPor.totalTip);

var actTip = Math.min(extTip, 0.20*tar/35);  // no matter what, keep tips below 20%
                                        
obj.recTotalTip.value =   Math.round(two*actTip)/two;

// Calculate the number of recommended tips.


 }
 // MINOR FUNCTIONS=====================================
 
 function interpolate(x1,x2, x,y1,y2){
 var y = (x-x1)/(x2-x1) *(y2-y1) + y1;
 return y;
 }
 //--------------------------
 
 function nutritionCont( cal, fat, protein, carb, fiber,quantity){
 
 if(isNaN(quantity)) quantity=0;
 this.cal=  quantity*cal;
 this.carb=  quantity*carb;
 this.prot=  quantity*protein;
 this.fiber= quantity*fiber;
 this.fat=   quantity* fat;

 }
 
 function minPlan( ){

 this.WGrain= 3;
 this.RGrain=3;
 this.totGrain=6;
 
 this.LCVeg=1;
 this.StanVeg=1;
 this.StarchyVeg=1;
 this.totVeg=3;
 
 this.Juice=1;
 this.WholeFr=1;
 this.totFruit=2;
 
 this.StandMeat=2;
 this.LiteDairy=2;
 
 this.leanCal= 1292;
 this.tar= this.leanCal/0.90;
 this.totalTip= -(this.leanCal -this.tar)/35;

 }
 
 function maxPlan( ){
 this.WGrain= 6;
 this.RGrain=5;
 this.totGrain=11;
 
 this.LCVeg=1;
 this.StanVeg=2; 
 this.StarchyVeg=2;
 this.totVeg=5;
 
 this.Juice=2;
 this.WholeFr=2;
 this.totFruit=4
 
 this.StandMeat=4;
 this.LiteDairy=3;
 
 // I wqant the total number of tips to make up the calorie difference.
 
 this.leanCal= 2350;
 this.tar=this.leanCal/0.85;
 this.totalTip= -(this.leanCal-this.tar)/35;

 }