

var maxCaloriesPerPackage;
var maxServingSizeOz;
var maxPctFatCalories;
var maxPctSatFatCalories;
var maxSugarPerPackage;
var maxPctSugarWeight;
var maxSodium;
var minFiber;

var isCompliant;

function SetGuidelines(schoolDistrict)
{
	//alert("setting guidelines for: " + schoolDistrict);


	document.getElementById("divSnackType").style.visibility="hidden";
	document.getElementById("divEnterGuide").style.visibility="hidden";

	switch(schoolDistrict)
	{
		case "Choose":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			break;	
		case "Seattle":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 1.50;
			maxPctFatCalories		= 30;
			maxPctSatFatCalories	= 10;
			maxSugarPerPackage		= 15;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			document.getElementById("divSnackType").style.visibility="visible";
			SetSnackTypeGuidelines(document.NutrientCalc.SnackType.value);
			break;
		case "Highline":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			document.getElementById("divEnterGuide").style.visibility="visible";
			break;
		case "Tukwila":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			document.getElementById("divEnterGuide").style.visibility="visible";
			break;
		case "Other":
			maxCaloriesPerPackage	= 0;
			maxServingSizeOz		= 0;
			maxPctFatCalories		= 0;
			maxPctSatFatCalories	= 0;
			maxSugarPerPackage		= 0;
			maxPctSugarWeight		= 0;
			maxSodium				= 0;
			minFiber				= 0;
			document.getElementById("divEnterGuide").style.visibility="visible";
			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;
}

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);

}

function SetSnackTypeGuidelines(snackTypeGuideline)
{
	maxServingSizeOz		= parseFloat(snackTypeGuideline);
	//alert("maxServingSizeOz: " + maxServingSizeOz);
	populateGuidelineForm();
}

function CalculateNutrients()
{
	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 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	= (grams * servings) / 28.3;
	var totalCalPerPackage	= calories * servings;
	var totalSodium			= sodium * servings;
	var totalFiber			= fiber * servings;
	var totalSugar			= sugar * servings;
	var pctCaloriesFat		= (fatCalories / calories) * 100;
	var pctCaloriesSatFat	= ((saturatedFat * 9) / calories) * 100;
	var fat					= fatCalories / 9;
	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,'');
	document.getElementById("percentSugarWeight").innerHTML = FormatNumber(pctSugar,0,'') + "%";
	document.getElementById("fat").innerHTML = FormatNumber(fat, 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("fat").style.fontWeight="bold";
	document.getElementById("fat").style.color="red";

	//Compares calculated values with guidelines
	isCompliant = true;
	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(pctSugar, maxPctSugarWeight, "guidepercentSugarWeight");

	if (isCompliant)
	{
		var snackItem=document.NutrientCalc.snackItem.options[document.NutrientCalc.snackItem.selectedIndex].text;
		document.getElementById("productCompliant").innerHTML ="Your "  + snackItem + " Meets Guidelines";
		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;
		document.getElementById("productCompliant").innerHTML ="Your " + snackItem + " 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";
	}
}

function CompareToMax(value, maxAmount, domElement)
{
	elem = document.getElementById(domElement);

	if (maxAmount == 0)
	{
		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)
	{
		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;
	}
}

