/*******************************************************************
Nutrition Calculator
(calculator.js)

Primary Nutritional Calculations


 ----------  CHANGE INSTRUCTIONS ----------

 Customization Will need to be made to this file to correspond to your school's nutritional guidelines.
 Corresponding changes will also need to be made to the select list (name="SchoolDist") on calculator.html
 
 The following functions on this file will need to be edited for a specific school...
     SetGuidelines(...)


 If there need to add or change to the Snack Types, 
 such changes only need to be made to the select list (name="SnackType") on calculator.html
 (the values of the SnackType indicate the portion size in ounces)

 ---------- END CHANGE INSTRUCTIONS ----------

HISTORY---------------------------------------
This Nutrition Calculator was Created for Seattle Public Schools, 
Nutrition Services by
Amelia MacLeod 
12/16/2005

Additional code comments added for distribution 
by Daniel J. Moore, Seattle Public Schools.
4/5/2006

© 2006 Seattle Public Schools - All Rights Reserved
*******************************************************************/

var maxCaloriesPerPackage;
var maxServingSizeOz;
var maxPctFatCalories;
var maxPctSatFatCalories;
var maxSugarPerPackage;
var maxPctSugarWeight;
var maxSodium;
var minFiber;

var isCompliant;

//-- used with the onChange event for select "SchoolDist" on Calculator.html
function SetGuidelines(schoolDistrict)
{
//	alert("setting guidelines for: " + schoolDistrict);


	document.getElementById("divSnackType").style.visibility="hidden";
	document.getElementById("divEnterGuide").style.visibility="hidden";
	document.getElementById("divNotes").style.visibility="hidden";

	switch(schoolDistrict)
	{
		case "Choose":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			maxTotalFat              = 0;
			break;	
		
		case "Texas":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			maxTotalFat              = 0;
			document.getElementById("divSnackType").style.visibility="visible"; //USED TO DISPLAY THE SNACK TYPE SELECT LIST
			SetSnackTypeGuidelines(document.NutrientCalc.SnackType.value);      //set default current selection from the 'SnackType' dropdown lost.
			break;
		
		case "Other":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			maxTotalFat              = 0;
			document.getElementById("divEnterGuide").style.visibility="visible";  //USED TO DISPLAY PROMPT FOR VISITOR TO ENTER NUTRITION GUIDELINE VALUES
			break;	
/* --------------------------------------------------------------------
ADD YOUR SCHOOL TO THIS CASE LIST AS IN THIS EXAMPLE...
A CORRESPONDING ENTRY MUST BE ADDED TO CALENDAR.HTML IN THE SELECT LIST "SchoolDist"

("SchoolDist" list can also be altered to default to your school district.)

YOU MAY ALSO WISH TO REMOVE ENTRIES FROM THIS CASE LIST THAT DO NOT APPLY TO YOUR SITUATION
(AGAIN, BE SURE TO MAKE THE CORRESPONDING CHANGES TO CALENDAR.HTML IN THE SELECT LIST "SchoolDist"

YOU MAY WISH TO LEAVE THE "Other" CASE ITEM 
AND SELECT ITEM TO ALLOW VISITORS TO ENTER THEIR OWN CRITERIA AND EXPERIMENT WITH THE CALCULATOR
   --------------------------------------------------------------------

		case "Valhalla":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 1.50;
			maxPctFatCalories		= 30;
			maxPctSatFatCalories	= 10;
			maxSugarPerPackage		= 15;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			maxTotalFat              = 0;
			document.getElementById("divSnackType").style.visibility="visible";
			SetSnackTypeGuidelines(document.NutrientCalc.SnackType.value);
			break;			
----------------------------------------------------------------------- */
	}

	populateGuidelineForm();
}

function populateGuidelineForm()
{
	


	document.NutrientCalc.txtmaxCaloriesPerPackage.value		= maxCaloriesPerPackage;
	document.NutrientCalc.txtmaxServingSizeOz.value			= maxServingSizeOz;
	document.NutrientCalc.txtmaxPctFatCalories.value		= maxPctFatCalories;
	document.NutrientCalc.txtmaxPctSatFatCalories.value		= maxPctSatFatCalories;
	document.NutrientCalc.txtmaxSugarPerPackage.value		= maxSugarPerPackage;
	document.NutrientCalc.txtmaxPctSugarWeight.value		= maxPctSugarWeight;
	document.NutrientCalc.txtmaxSodium.value			= maxSodium;
	document.NutrientCalc.txtminFiber.value				= minFiber;
	document.NutrientCalc.txtmaxTotalFat.value			= maxTotalFat;
	
}

function LoadGuidelinesFromForm()
{
	maxCaloriesPerPackage		= parseFloat(document.NutrientCalc.txtmaxCaloriesPerPackage.value);
	maxServingSizeOz		= parseFloat(document.NutrientCalc.txtmaxServingSizeOz.value);
	maxPctFatCalories		= parseFloat(document.NutrientCalc.txtmaxPctFatCalories.value);
	maxPctSatFatCalories		= parseFloat(document.NutrientCalc.txtmaxPctSatFatCalories.value);
	maxSugarPerPackage		= parseFloat(document.NutrientCalc.txtmaxSugarPerPackage.value);
	maxPctSugarWeight		= parseFloat(document.NutrientCalc.txtmaxPctSugarWeight.value);
	maxSodium			= parseFloat(document.NutrientCalc.txtmaxSodium.value);
	minFiber			= parseFloat(document.NutrientCalc.txtminFiber.value);
	maxTotalFat			= parseFloat(document.NutrientCalc.txtmaxTotalFat.value);

}

function SetSnackTypeGuidelines(snackTypeGuideline)
{
//	maxServingSizeOz		= parseFloat(snackTypeGuideline);
//	alert("maxServingSizeOz: " + maxServingSizeOz);
document.getElementById("divNotes").style.visibility="hidden";
	
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			maxTotalFat              = 0;


	switch (snackTypeGuideline){
			
			case "1":
			maxServingSizeOz=3;
			maxPctFatCalories=30;
			maxPctSatFatCalories=10;
			maxSugarPerPackage=30;
			document.getElementById("divNotes").style.visibility="visible";
			document.getElementById("divNotes").innerHTML = "<span style='font-weight:bold;'>**</span> Sugar must not exceed more <br />&nbsp;&nbsp;&nbsp;&nbsp;than 10 grams per ounce."
			break;
			
			case "2":
			maxServingSizeOz=12;
			maxSugarPerPackage=45;
			document.getElementById("divNotes").style.visibility="visible";
			document.getElementById("divNotes").innerHTML = "<span style='font-weight:bold;'>**</span> Must not contain more than <br />&nbsp;&nbsp;&nbsp;&nbsp;30 grams of sugar per 8 fl.oz."

			break;
			case "3":
			maxServingSizeOz=1.5;
			maxTotalFat=7.5;
			break;
			
			case "4":
			maxServingSizeOz=2;
			maxPctFatCalories=30;
			maxPctSatFatCalories=10;
			maxSugarPerPackage=20;
			document.getElementById("divNotes").style.visibility="visible";
			document.getElementById("divNotes").innerHTML = "<span style='font-weight:bold;'>**</span> Sugar must not exceed more <br />&nbsp;&nbsp;&nbsp;&nbsp;than 10 grams per ounce."
			break;
			
			case "7":
			maxServingSizeOz=6;
			maxSugarPerPackage=30;
			document.getElementById("divNotes").style.visibility="visible";
			document.getElementById("divNotes").innerHTML = "<span style='font-weight:bold;'>**</span> Must not contain more than <br />&nbsp;&nbsp;&nbsp;&nbsp;30 grams total sugar per 6 fl.oz."


			break;
			case "5":
			maxServingSizeOz=1.5;
			break;
			case "6":
			maxServingSizeOz=4;
			break;
			case "8":
			maxServingSizeOz=8;
			break;
			
			
			
	}

populateGuidelineForm();



}

function CalculateNutrients()
{
	
 if(document.NutrientCalc.SchoolDist.value != "Choose"){

	var grams			= parseFloat(document.NutrientCalc.grams.value);
	var servings			= parseFloat(document.NutrientCalc.servingsPerContainer.value);
	var calories 			= parseFloat(document.NutrientCalc.calories.value);
	var fatCalories			= parseFloat(document.NutrientCalc.CaloriesFat.value);
	var fat				= parseFloat(document.NutrientCalc.fat.value);
	var saturatedFat		= parseFloat(document.NutrientCalc.saturatedFat.value);
	var sodium 			= parseFloat(document.NutrientCalc.sodium.value);
	var fiber			= parseFloat(document.NutrientCalc.fiber.value);
	var sugar			= parseFloat(document.NutrientCalc.sugar.value);

	var totalOzPerPackage	= 0;
	if(document.NutrientCalc.ServeSizeUnit.value == "g"){
		totalOzPerPackage	= (grams * servings) / 28.3;
	}
	else if(document.NutrientCalc.ServeSizeUnit.value == "oz"){
		totalOzPerPackage	=  grams * servings;
	}
	var totalCalPerPackage	= calories * servings;
	var totalSodium			= sodium * servings;
	var totalFiber			= fiber * servings;
	var totalSugar			= sugar * servings;
	
	var pctCaloriesFat		= maxPctFatCalories;
	var pctCaloriesSatFat		= maxPctSatFatCalories;

//	pctCaloriesFat		= (fatCalories / calories) * 100;    //not correct 
	pctCaloriesFat		= (fat * 9 / calories) * 100;
	pctCaloriesSatFat	= ((saturatedFat * 9) / calories) * 100;
	


//	var fat					= fatCalories / 9;
	var totalfat = fat * servings; 
	var pctSugar			= (sugar / grams) * 100;

	var error = "";

	//Populate Entire Package Nutrition Facts
	document.getElementById("ozPerPackage").innerHTML = FormatNumber(totalOzPerPackage, 2,'');
	document.getElementById("totalcalories").innerHTML = FormatNumber(totalCalPerPackage, 0,'');
	document.getElementById("percentCaloriesFat").innerHTML = FormatNumber(pctCaloriesFat, 0, '') + "%";
	document.getElementById("percentCaloriesSatFat").innerHTML = FormatNumber(pctCaloriesSatFat, 0, '') + "%";
	document.getElementById("totalSodium").innerHTML = FormatNumber(totalSodium, 0,'');
	document.getElementById("totalDietaryFiber").innerHTML = FormatNumber(totalFiber, 0,'');
	document.getElementById("totalSugar").innerHTML = FormatNumber(totalSugar, 0,'');
if(!isNaN(pctSugar))
	document.getElementById("percentSugarWeight").innerHTML = FormatNumber(pctSugar,0,'') + "%";
else
	document.getElementById("percentSugarWeight").innerHTML = FormatNumber(0,0,'') + "%";
	document.getElementById("totalfat").innerHTML = FormatNumber(totalfat, 0,'');
	document.getElementById("ozPerPackage").style.fontWeight="bold";
	document.getElementById("totalcalories").style.fontWeight="bold";
	document.getElementById("percentCaloriesFat").style.fontWeight="bold";
	document.getElementById("percentCaloriesSatFat").style.fontWeight="bold";
	document.getElementById("totalSodium").style.fontWeight="bold";
	document.getElementById("totalDietaryFiber").style.fontWeight="bold";
	document.getElementById("totalSugar").style.fontWeight="bold";
	document.getElementById("percentSugarWeight").style.fontWeight="bold";
	document.getElementById("totalfat").style.fontWeight="bold";
//	document.getElementById("totalfat").style.color="red";

	//Compares calculated values with guidelines
	isCompliant = true;
	//correction to the total sugar based on the input
	var correctedMaxSugarPerPackage = maxSugarPerPackage*(totalOzPerPackage/maxServingSizeOz);
	LoadGuidelinesFromForm();
	CompareToMax(totalOzPerPackage, maxServingSizeOz, "guideOzPerPackage");
	CompareToMax(totalCalPerPackage, maxCaloriesPerPackage, "guidetotalcalories");
	CompareToMax(pctCaloriesFat, maxPctFatCalories, "guidepercentCaloriesFat");
	CompareToMax(pctCaloriesSatFat, maxPctSatFatCalories, "guidepercentCaloriesSatFat");
	CompareToMax(totalSodium, maxSodium, "guidetotalSodium");
	CompareToMin(totalFiber, minFiber, "guidetotalDietaryFiber");
//	CompareToMax(totalSugar, maxSugarPerPackage, "guidetotalSugar");
	CompareToMax(totalSugar, correctedMaxSugarPerPackage, "guidetotalSugar");
	CompareToMax(pctSugar, maxPctSugarWeight, "guidepercentSugarWeight");
	CompareToMax(totalfat, maxTotalFat, "guideTotalFat");
//display result column title  	
document.getElementById("resultCol").innerHTML="Result";
	
 /*
	if (isCompliant)
	{
		var snackItem=document.NutrientCalc.snackItem.options[document.NutrientCalc.snackItem.selectedIndex].text;
		if(document.NutrientCalc.SchoolDist.value != "Other")
			document.getElementById("productCompliant").innerHTML ="Your "  + snackItem + " Meets Standards";
		else
			document.getElementById("productCompliant").innerHTML ="Your Food Meets Standards";
		document.getElementById("productCompliant").style.color="#A2B543";
		document.getElementById("productCompliant").style.fontSize="14px";
		document.getElementById("productCompliant").style.fontFamily="arial";
		document.getElementById("productCompliant").style.fontWeight="bold";
	}
	else
	{
		var snackItem=document.NutrientCalc.snackItem.options[document.NutrientCalc.snackItem.selectedIndex].text;
		if(document.NutrientCalc.SchoolDist.value != "Other")		
			document.getElementById("productCompliant").innerHTML ="Your " + snackItem + " Does Not Comply";
		else
			document.getElementById("productCompliant").innerHTML ="Your Food Does Not Comply";	
		document.getElementById("productCompliant").style.color="red";
		document.getElementById("productCompliant").style.fontSize="14px";
		document.getElementById("productCompliant").style.fontFamily="arial";
		document.getElementById("productCompliant").style.fontWeight="bold";
	}
*/
  }
  // not choose Nutrition Standards from drop down
  else{
	alert("Please choose your Nutrition Standards from drop down menu.");

  }
}

function CompareToMax(value, maxAmount, domElement)
{
	elem = document.getElementById(domElement);

	if (maxAmount == 0 || isNaN(maxAmount) || isNaN(value)) 
	{
		elem.innerHTML = "N/A";
		elem.style.color ="#B59EB4";
	} 
	else
	{
		if (value <= maxAmount)
		{
			elem.innerHTML = "OK";
			elem.style.color ="green";
		}
		else
		{
			elem.innerHTML = "Too High";
			elem.style.color ="red";
			isCompliant = false;
		}
	}
}

function CompareToMin(value, minAmount, domElement)
{
	elem = document.getElementById(domElement);


	if (minAmount == 0 || isNaN(minAmount) || isNaN(value))
	{
		elem.innerHTML = "N/A";
		elem.style.color ="#B59EB4";
	}
	else
	{
		if (value >= minAmount)
		{
			elem.innerHTML = "OK";
			elem.style.color ="green";
		}
		else
		{
			elem.innerHTML= "Too Low";
			elem.style.color ="red";
			isCompliant = false;
		}
	}
}

function clearZero(form)
{
	if (form.value == 0)
	{
		form.value = "";
	}
	else
	{
		form.select();
	}
}

function clearBlank(form)
{
	if (form.value == "")
	{
		form.value = "0";
	}
}

function limitNumbers()
{
	if (event.keyCode < 45 || event.keyCode > 57)
	{
		event.returnValue = false;
	}
}

