// JavaScript Document

var moreIngredients = new Array();
var recipeIngredients = new Array();
//For reference:
//[0] = name
//[1] = id
//[2] = amount
//[3] = measure
//[4] = costpermeasure
//[5] = type (solid / liquid)

var storedValues = new Array();
var recipesArray = new Array();

var customIngID = 999;

var tips;
var tipNo;

//set variables to 0*0 so JS knows they are numbers
var ingTotal = 0*0;
var oz_ingTotal = 0*0;
var scratchTotal = 0*0;
var oz_scratchTotal = 0*0;
var prepTotal = 0*0;
var oz_prepTotal = 0*0;
var profPerOzScr = 0*0;
var profPerOzHeinz = 0*0;

var menuPrice = 0*0;
var laborCost = 0*0;
var prepHr = 0*0;
var prepMin = 0*0;
var recipeYield = 0*0;

var listPrice = 0*0;
var milkCost = 0*0;
var heinzYield = 0*0;

var diffPerOz = 0*0;

var soupServed = 0*0;
var soupServedPer = 0*0;


 $(document).ready(function()
{
	$('#txtMenuPrice').attr('value', '2');
	$('#txtLaborCost').attr('value', '10');
	$('#txtPrepHr').attr('value', '1');
	$('#txtPrepMin').attr('value', '30');
	$('#txtRecipeYield').attr('value', '3');
	
	$('#txtCustomerName').attr('value', '');
	$('#txtHeinzSalesRep').attr('value', '');
	
	$('#txtListPrice').attr('value', '10');
	$('#txtMilkCost').attr('value', '6');
	$('#ddlHeinzYield').attr('value', '3');
	
	$('#txtSoupServed').attr('value', '20');
    $('#ddlSoupServedPer').attr('value', '52');
	
	$('#displayoz').hide();
	$('#display8oz').hide();
	$('#display16oz').hide();
	$('#display32oz').hide();
	$('#displayGal').show();
	$('#displayAnnoz').hide();
	$('#displayAnn8oz').hide();
	$('#displayAnn16oz').hide();
	$('#displayAnn32oz').hide();
	$('#displayAnnGal').show();
	$('#ddlDisplayResults').attr('value', 'gal');
	
	$('#ddlRecipe').attr('value', '');
	
	$('#addCustom').hide();
	$('#customWindow').hide();

	loadRecipes();
	loadTips();
	
	
	$('#ddlRecipe').change(function(){
			loadXMLRecipe();
		});
	
	$('#ddlDisplayResults').change(function(){
			changeDisplayResults();
		});
	
	$('input, select').change(function(){
			calculateTotal();
		});
	
	$('input, #btnPDF').keypress(function(keyPressed){
			if(keyPressed.which==13){
				calculateTotal();
				return false;
			}
		});
	
	$('tr.tableHeader a').click(function(){
			displayHeaderDefinition($(this));
		});
	
	$('input').focus(function(){
			$(this).select();		
		});
	
	$('#tips #more').click(function(){
			displayTips();
		});
	
	$('#addCustom a').click(function(){
			$('#txtAdd').attr('value', '');				   
			$('#customWindow').slideDown('normal');
		});
	
	$('#customWindow a#close').click(function(){
			$('#customWindow').slideUp('normal');
		});
	
	$('#customWindow a#add').click(function(){
			var customIng = new Array();
			customIng.push($('#txtAdd').attr('value'));
			customIng.push(customIngID++);
			customIng.push('1');
			customIng.push($('#ddlAdd').attr('value'));
			customIng.push('1.00');
			($('#ddlAdd').attr('value') == 'gal' ? customIng.push('') : customIng.push('solid'));
			recipeIngredients.push(customIng);
		
			refreshIngredients();
	
			$('#customWindow').fadeOut('normal');
		});
});

function displayTips()
{
	if ($('#more').html() == "More"){
		IE_DropDownDisplay('fadeOut');
		$('#more').html('Less');
		$('#tips div').fadeIn();
	}
	else
	{
		IE_DropDownDisplay('fadeIn');
		$('#more').html('More');	
		$('#tips div').fadeOut();
	}
}

function loadTips()
{
   tips = new Array();
   
   $.get('xml/Tips.xml', function(xml) {

		 $(xml).find('tip').each(function(index, recipeTag){
				var tipHead = $(this).find('head').text();
				var tipBody = $(this).find('body').text();
				
				var tip = new Array();
				tip.push(tipHead);
				tip.push(tipBody);

				tips.push(tip);
			    
		 }); //close each
		 
		 tipNo = Math.floor(Math.random()*tips.length);
		 
		 if(tipNo < 1)	$('#tips #prev').css('display', 'none');
		 else if(tipNo > tips.length-2) $('#tips #next').css('display', 'none');
		 
		$(tips).each(function(tipIndex, tipTag){
			$('#tips div').append('<p id="tip' + tipIndex + '">' + tipTag [1] + '</p>');
		});
		
		$('#tips p').filter('[id!=tip' + tipNo + ']').css('display', 'none');
		$('#tips h3').html(tips[tipNo][0]);
		$('#tipNumber').html((tipNo*1+1) + ' of ' + (tips.length*1));
			
   }); //close $.get 

	$('#tips #prev').click(function(){
		prevTip();
	});
		
	$('#tips #next').click(function(){
		nextTip();
	});
}

function prevTip()
{
	if(tipNo > 0){
		tipNo--;
		refreshTips();
	}
	
	if(tipNo > tips.length-2){
		$('#tips #next').css('display', 'none');	
	}
	else
	{
		$('#tips #next').css('display', 'block');
	}
	
	if(tipNo < 1){
		$('#tips #prev').css('display', 'none');	
	}
	else
	{
		$('#tips #prev').css('display', 'block');
	}
}

function nextTip()
{
	if(tipNo < tips.length-1){
		tipNo++;
		refreshTips();
	}
	
	if(tipNo > tips.length-2){
		$('#tips #next').css('display', 'none');	
	}
	else
	{
		$('#tips #next').css('display', 'block');
	}
	
	if(tipNo < 1){
		$('#tips #prev').css('display', 'none');	
	}
	else
	{
		$('#tips #prev').css('display', 'block');
	}
}

function refreshTips()
{
	$('#tips p')
		.filter('[id!=tip' + tipNo + ']')
		.fadeOut('slow');
	
	$('#tips p')
		.filter('[id=tip' + tipNo + ']')
		.fadeIn('slow');
		
	$('#tips h3')
		.html(tips[tipNo][0])
		.fadeIn('slow');
		
	$('#tipNumber').html((tipNo*1+1) + ' of ' + (tips.length*1));

}

function loadRecipes()
{
   $.get('xml/Recipes.xml', function(xml) {
		
		 $(xml).find('recipe').each(function(index, recipeTag){
				var name = $(this).find('name').eq(0).text();
				recipesArray.push(name);
				//$('#ddlRecipe').append('<option>' + name + '</option>');
			
		 }); //close each
		
		recipesArray.sort();
		
		$.each(recipesArray, function(index, value){		  
				$('#ddlRecipe').append('<option>' + recipesArray[index] + '</option>'); 		    
    		});
		
   }); //close $.get 
   
}

function loadXMLRecipe()
{
	//this function is very resource-intensive due to all of the loops.
	//However, this function is only called when the user changes the recipe dropdown.
	//It is not a function that is re-used throughout the page.
	var recipeName = $('#ddlRecipe option:selected').text();					  
	$('h2#recipeName').text(recipeName);	
	
	if(recipeName == 'Custom Recipe')
	{
		recipeIngredients = new Array();
		loadIngredients();
	}
	else
	{
		loadIngredients();
		recipeIngredients = new Array();
				
		$.get('xml/Recipes.xml', function(xml) {
						 					
			 $(xml).find('recipe').each(function(recipeIndex, recipeTag){
					var name = $(this).find('name').eq(0).text();
					
					if (name == recipeName)
					{
						$('#txtRecipeYield').attr('value', $(this).find('recipeyield').text());
					
						$(this).find('ingredient').each(function (recipeIngredientIndex, recipeIngredientTag){
							
							var ingredientName = $(this).find('name').text();
							
							$.get('xml/Ingredients.xml', function(xml) {
									  
									  $(xml).find('ingredient').each(function(ingredientIndex, ingredientTag){
															  
											$(this).find('name').each(function(){
																									
												if($(ingredientTag).find('name').text() == ingredientName){
													
													var ingID = $(ingredientTag).find('id').text()*1;
													var ingName = $(ingredientTag).find('name').text();
													var ingAmount = $(recipeIngredientTag).find('amount').text();
													var ingMeasure = $(recipeIngredientTag).find('measure').text();
													var ingCostpermeasure = $(ingredientTag).find('costpermeasure').text();
													var ingType = $(ingredientTag).find('type').text();
													
													switch(ingMeasure){
														case "gal":
															ingCostpermeasure = ingCostpermeasure*128;
															break;
														case "qt":
															ingCostpermeasure = ingCostpermeasure*32;
															break;
														case "lb":
															ingCostpermeasure = ingCostpermeasure*16;
															break;
														case "cup":
															ingCostpermeasure = ingCostpermeasure*8;
															break;
													}
													
													//alert(ingMeasure);
													var arrIndex = GetIndexByIngID(ingID, moreIngredients);
													
													var newIng = new Array();
													newIng.push(ingName);
													newIng.push(ingID);
													newIng.push(ingAmount);
													newIng.push(ingMeasure);
													newIng.push(ingCostpermeasure);
													newIng.push(ingType);
													
													recipeIngredients.push(newIng);
													moreIngredients.splice(arrIndex,1);
												}
						
											});
											
										});//close ingredient each()
									  
									  	refreshIngredients();	
									  									  
							});//close $.get
							
						});// close recipeIngredients each()
						
					}// end if
					
			 }); //close recipe each()
						
	   }); //close $.get 
						
	}// end else
		
}

function loadIngredients()
{
   $('#addCustom').show();
	
   $.get('xml/Ingredients.xml', function(xml) {
		  
		 $(xml).find('ingredient').each(function(index){
			var id = $(this).find('id').text()*1;
			var name = $(this).find('name').text();
			var measure = $(this).find('measure').text();
			var costpermeasure = $(this).find('costpermeasure').text()*1;
			var type = $(this).find('type').text();
			
			moreIngredients[index] = new Array();
			moreIngredients[index].push(name, id, 1, measure, costpermeasure, type);
    
		 }); //close each
		 
		 displayIngredients();
		 
   }); //close $.ajax 
}

function displayIngredients()
{
	moreIngredients.sort();
	
	$('table#moreIng tr').filter('[class!=tableHeader]').filter('[id!=addCustom]').remove();
	$('table#recipeIng tr').filter('[class!=tableHeader]').remove();

	
	$.each(moreIngredients, function(index, value){
			 
		$('<tr id="id'+ moreIngredients[index][1] +'"></tr>')
		    .html('<td><a class="add">' + moreIngredients[index][0] + '</a></td>')
		    .appendTo('table#moreIng');
		    
		    });
	
	var rowNum = 1; 
	
	$.each(recipeIngredients, function(index, value){
		
			var htmlCopy;
		
			htmlCopy = '<td><span id="pdf' + rowNum + 'A">' + recipeIngredients[index][0] + '</span></td>';
			htmlCopy += '<td><input id="pdf' + rowNum + 'B" type="text" value="' + recipeIngredients[index][2] + '" class="short recipeInput" runat="server" /></td>';
			htmlCopy += '<td><select id="pdf' + rowNum + 'C" class="short">';
			//htmlCopy += '<td><select id="select'+ recipeIngredients[index][1] +'" class="short">';
			
			if(recipeIngredients[index][5] == 'solid')
			{
				htmlCopy += '<option value="oz"';
					if(recipeIngredients[index][3] == 'oz') htmlCopy += ' selected="selected"';
				htmlCopy += '>oz</option>';
				
				htmlCopy += '<option value="lb"';
					if(recipeIngredients[index][3] == 'lb') htmlCopy += ' selected="selected"';
				htmlCopy += '>lb</option>';
			}
			else
			{
				htmlCopy += '<option value="oz"';
					if(recipeIngredients[index][3] == 'oz') htmlCopy += ' selected="selected"';
				htmlCopy += '>oz</option>';
				
				htmlCopy += '<option value="cup"';
					if(recipeIngredients[index][3] == 'cup') htmlCopy += ' selected="selected"';
				htmlCopy += '>cup</option>';
				
				htmlCopy += '<option value="qt"';
					if(recipeIngredients[index][3] == 'qt') htmlCopy += ' selected="selected"';
				htmlCopy += '>qt</option>';
				
				htmlCopy += '<option value="gal"'; 
					if(recipeIngredients[index][3] == 'gal') htmlCopy += ' selected="selected"';
				htmlCopy += '>gal</option>';
			}
				
			htmlCopy += '</select>';
			htmlCopy += '<td>$<input id="pdf' + rowNum + 'D" type="text" value="' + dollarFormat(recipeIngredients[index][4]) + '" class="short recipeInput" /></td>';
			htmlCopy += '<td>$<input id="pdf' + rowNum + 'E" type="text" readonly="readonly" value="" class="short" /></td>';
			htmlCopy += '<td><a class="remove">Remove</a></td>';
	
			$('<tr id="id'+ recipeIngredients[index][1] +'"></tr>')
			    .html(htmlCopy)
			    .appendTo('table#recipeIng');
			
			rowNum ++;
		
			});
	
	$('table#moreIng tr:odd').addClass('row0');
	$('table#recipeIng tr:odd').addClass('row0');
	
	$('a.add').click(function(){
		addIngredient(this);
	});
	
	$('a.remove').click(function(){
		removeIngredient(this);
	});
	
	$('table#recipeIng input, table#recipeIng select').change(function(){
			calculateTotal();
		});
	
	$('table#recipeIng input').keypress(function(keyPressed){
			if(keyPressed.which==13){
				calculateTotal();
				return false;
			}
		});
	
	$('table#recipeIng input').focus(function(){
			$(this).select();		
		});
		
}

function addIngredient(thisIng)
{
	
	if(recipeIngredients.length == 49)
	{
		alert('There is a maximum of 50 ingredients per recipe. Please remove an ingredient before adding more.');	
	}
	else
	{	
		var ingID = thisIng.parentNode.parentNode.id.substr(2)*1;
		var arrIndex = GetIndexByIngID(ingID, moreIngredients);
		
		var newIng = new Array();
		newIng.push(moreIngredients[arrIndex][0]);
		newIng.push(moreIngredients[arrIndex][1]);
		newIng.push(moreIngredients[arrIndex][2]);
		newIng.push(moreIngredients[arrIndex][3]);
		newIng.push(moreIngredients[arrIndex][4]);
		newIng.push(moreIngredients[arrIndex][5]);
				
		recipeIngredients.push(newIng);
		moreIngredients.splice(arrIndex,1);
		
		refreshIngredients();
	}
}

function refreshIngredients(deletedIndex){
	
	//this stores the users variables for when the values are removed from the table.
	var storedValues = new Array();
	var storedSelect = new Array();
	
	if(deletedIndex != null)
	{
		var deletedIndexLow;
		var deletedIndexHigh;
	
		if(deletedIndex == 0)
		{
			deletedIndexLow = 0;
			deletedIndexHigh = 2;
		}
		else
		{
			deletedIndexLow = (deletedIndex*3);
			deletedIndexHigh = (deletedIndex*3)+2;
		}
	}
	
	$('table#recipeIng input').each(function(inputIndex, inputTag){
		
			if(deletedIndex != null)
			{
				if(inputIndex < deletedIndexLow || inputIndex > deletedIndexHigh)
				{
					storedValues.push(inputTag.value);	
				}
			}
			else
			{
				storedValues.push(inputTag.value);	
			}
		});
	
	$('table#recipeIng select').each(function(selectIndex, selectTag){
		
			if(deletedIndex != null)
			{
				if(selectIndex != deletedIndex)
				{
					storedSelect.push(selectTag.value);	
				}
			}
			else
			{
				storedSelect.push(selectTag.value);	
			}
		});
	
	displayIngredients();


	//this re-adds the users variables from before.
	$('table#recipeIng input').each(function(index, inputTag){
								
		if(storedValues[index]) inputTag.value = storedValues[index];	
		
		});
	
	$('table#recipeIng select').each(function(index, selectTag){
								
		if(storedSelect[index]) selectTag.value = storedSelect[index];	
		
		});
	
	calculateTotal();
}

function removeIngredient(thisIng)
{
	var ingID = thisIng.parentNode.parentNode.id.substr(2)*1;	
	var arrIndex = GetIndexByIngID(ingID, recipeIngredients);
	
	var newIng = new Array();
	newIng.push(recipeIngredients[arrIndex][0]);
	newIng.push(recipeIngredients[arrIndex][1]);
	newIng.push(recipeIngredients[arrIndex][2]);
	newIng.push(recipeIngredients[arrIndex][3]);
	newIng.push(recipeIngredients[arrIndex][4]);
	newIng.push(recipeIngredients[arrIndex][5])
	
	moreIngredients.push(newIng);
	recipeIngredients.splice(arrIndex,1);
	
	refreshIngredients(arrIndex);
}

function GetIndexByIngID(ingID, ingArray)
{
	var returnVal;
	
	$.each(ingArray, function(index, value){
		if(value[1] == ingID) {
			returnVal = index;	
		}	
	});
	
	return returnVal;
}

function calculateTotal()
{
	var allAreNumbers = true;
	var errorList = new Array();
	var errorMsg = "";
	
	$('input')
		.filter(function(){
			return $(this).attr("id") != "txtCustomerName" && $(this).attr("id") != "txtHeinzSalesRep" && $(this).attr("id") != "txtAdd" && $(this).attr("type") != "hidden";
			})
		.each(function(index, inputTag){
		if(isNaN(inputTag.value)){
			allAreNumbers = false;
			errorList.push(inputTag.value);
		}
	});
	
	if(allAreNumbers==false)
	{
		errorMsg += 'Calculation Failed.  All inputs must contain numbers.\nThe following inputs do not contain numbers:\n\n';
		$(errorList).each(function(errIndex, errVal){
			errorMsg += '- '+errVal+'\n';
		});
		errorMsg += '\n\n';
		alert(errorMsg);
	}
	else if($('#txtMenuPrice').val() == "0")
	{
	    alert('The Heinz Menu Price must be greater than zero.');
	}
	else
	{
		laborCost = $('#txtLaborCost').attr('value')*1;
		prepHr = $('#txtPrepHr').attr('value')*1;
		prepMin = $('#txtPrepMin').attr('value')*1;
		menuPrice = $('#txtMenuPrice').attr('value')*1;
		recipeYield = $('#txtRecipeYield').attr('value')*1;

		listPrice = $('#txtListPrice').attr('value')*1;
		milkCost = $('#txtMilkCost').attr('value')*1;
		heinzYield = $('#ddlHeinzYield').attr('value')*1;
		
		soupServed = $('#txtSoupServed').attr('value')*1;
		soupServedPer = $('#ddlSoupServedPer').attr('value')*1;
		
		ingTotal = 0;
		oz_ingTotal = 0;
		scratchTotal = 0;
		oz_scratchTotal = 0;
		prepTotal = 0;
		profPerOzScr = 0;
		profPerOzHeinz = 0;
		diffPerOz = 0;
	
		calculateEachRow();
		calculateScratchTotal();
		calculateHeinzTotal();
		calculateDifference();
		
		calculateAnnualTotal();
		
		updateHiddenPDFFields();
	}
}

function calculateEachRow()
{	
	$('table#recipeIng tr').filter('[class!=tableHeader]').each(function(trIndex, trTag){
	   			
		var calc = 1*1;
		
		$(trTag).find('input').each(function(inputIndex, inputTag){
			    
			    if (inputIndex == 0)
			    {
					calc = calc * inputTag.value;
			    }
			    else if(inputIndex == 1)
			    {
					if(dollarFormat(recipeIngredients[trIndex][4]) == inputTag.value){
						calc = calc * recipeIngredients[trIndex][4];
					}
					else
					{
						calc = calc * inputTag.value;
					}
			    }
			    else if(inputIndex == 2)
			    {
					inputTag.value = dollarFormat(calc);    
			    }
			      
		});
		
	});
}



function calculateScratchTotal()
{				
		$('table#recipeIng tr').each(function(index, trTag){
	   					
			$(trTag).find('input').each(function(index, inputTag){
				    
				    if (index == 2)
				    {
						ingTotal = (ingTotal + (inputTag.value*1));
				    }
			      
			});
		});
		
		ingTotal = (ingTotal*1)/(recipeYield*1);
		oz_ingTotal = ingTotal/128;
		
		$('#gal_ingTotal').html(dollarFormat(ingTotal));
		$('#_32oz_ingTotal').html(dollarFormat(oz_ingTotal*32));
		$('#_16oz_ingTotal').html(dollarFormat(oz_ingTotal*16));
		$('#_8oz_ingTotal').html(dollarFormat(oz_ingTotal*8));
		$('#_1oz_ingTotal').html(dollarFormat(oz_ingTotal));

		laborCost = laborCost * ((prepHr*1) + (prepMin/60));
		scratchTotal = ((laborCost*1/recipeYield) + (ingTotal*1));
		oz_scratchTotal = scratchTotal/128;
		
		$('#gal_scrTotal').html(dollarFormat(oz_scratchTotal*128));
		$('#_32oz_scrTotal').html(dollarFormat(oz_scratchTotal*32));
		$('#_16oz_scrTotal').html(dollarFormat(oz_scratchTotal*16));
		$('#_8oz_scrTotal').html(dollarFormat(oz_scratchTotal*8));
		$('#_1oz_scrTotal').html(dollarFormat(oz_scratchTotal));
		$('#oz_scrTotal').html(dollarFormat(oz_scratchTotal));
		
		profPerOzScr = ((menuPrice-(oz_scratchTotal*8))/8);
		$('#oz_scrProfit').html(dollarFormat(profPerOzScr));
		$('#_1oz_scrProfit').html(dollarFormat(profPerOzScr));
		$('#_8oz_scrProfit').html(dollarFormat(profPerOzScr*8));
		$('#_16oz_scrProfit').html(dollarFormat(profPerOzScr*16));
		$('#_32oz_scrProfit').html(dollarFormat(profPerOzScr*32));
		$('#gal_scrProfit').html(dollarFormat(profPerOzScr*128));
}

function calculateHeinzTotal()
{	
	prepTotal = ((listPrice/heinzYield)+(milkCost/heinzYield));
	oz_prepTotal = prepTotal/128;
	
	$('#gal_prepTotal').html(dollarFormat(oz_prepTotal*128));
	$('#_32oz_prepTotal').html(dollarFormat(oz_prepTotal*32));
	$('#_16oz_prepTotal').html(dollarFormat(oz_prepTotal*16));
	$('#_8oz_prepTotal').html(dollarFormat(oz_prepTotal*8));
	$('#_1oz_prepTotal').html(dollarFormat(oz_prepTotal));
	$('#oz_prepTotal').html(dollarFormat(oz_prepTotal));

	profPerOzHeinz = ((menuPrice*16)-(oz_prepTotal*128))/128;
	$('#oz_prepProfit').html(dollarFormat(profPerOzHeinz));
	$('#_1oz_prepProfit').html(dollarFormat(profPerOzHeinz));
	$('#_8oz_prepProfit').html(dollarFormat(profPerOzHeinz*8));
	$('#_16oz_prepProfit').html(dollarFormat(profPerOzHeinz*16));
	$('#_32oz_prepProfit').html(dollarFormat(profPerOzHeinz*32));
	$('#gal_prepProfit').html(dollarFormat(profPerOzHeinz*128));
	
}

function calculateDifference()
{
	diffPerOz = profPerOzHeinz-profPerOzScr;
	
	$('#_1oz_difference').html(dollarFormat(diffPerOz));
	$('#_8oz_difference').html(dollarFormat(diffPerOz*8));
	$('#_16oz_difference').html(dollarFormat(diffPerOz*16));
	$('#_32oz_difference').html(dollarFormat(diffPerOz*32));
	$('#gal_difference').html(dollarFormat(diffPerOz*128));
}

function calculateAnnualTotal()
{
	soupServed = soupServed * soupServedPer;
	
	$('#gal_annIngTotal').html(dollarFormat(oz_ingTotal*soupServed*128));
	$('#_32oz_annIngTotal').html(dollarFormat(oz_ingTotal*soupServed*32));
	$('#_16oz_annIngTotal').html(dollarFormat(oz_ingTotal*soupServed*16));
	$('#_8oz_annIngTotal').html(dollarFormat(oz_ingTotal*soupServed*8));
	$('#_1oz_annIngTotal').html(dollarFormat(oz_ingTotal*soupServed));

	$('#gal_annScrTotal').html(dollarFormat(oz_scratchTotal*soupServed*128));
	$('#_32oz_annScrTotal').html(dollarFormat(oz_scratchTotal*soupServed*32));
	$('#_16oz_annScrTotal').html(dollarFormat(oz_scratchTotal*soupServed*16));
	$('#_8oz_annScrTotal').html(dollarFormat(oz_scratchTotal*soupServed*8));
	$('#_1oz_annScrTotal').html(dollarFormat(oz_scratchTotal*soupServed));
		
	$('#_1oz_annScrProfit').html(dollarFormat(profPerOzScr*soupServed));
	$('#_8oz_annScrProfit').html(dollarFormat(profPerOzScr*soupServed*8));
	$('#_16oz_annScrProfit').html(dollarFormat(profPerOzScr*soupServed*16));
	$('#_32oz_annScrProfit').html(dollarFormat(profPerOzScr*soupServed*32));
	$('#gal_annScrProfit').html(dollarFormat(profPerOzScr*soupServed*128));
		
	$('#gal_annPrepTotal').html(dollarFormat(oz_prepTotal*soupServed*128));
	$('#_32oz_annPrepTotal').html(dollarFormat(oz_prepTotal*soupServed*32));
	$('#_16oz_annPrepTotal').html(dollarFormat(oz_prepTotal*soupServed*16));
	$('#_8oz_annPrepTotal').html(dollarFormat(oz_prepTotal*soupServed*8));
	$('#_1oz_annPrepTotal').html(dollarFormat(oz_prepTotal*soupServed));
	
	$('#_1oz_annPrepProfit').html(dollarFormat(profPerOzHeinz*soupServed));
	$('#_8oz_annPrepProfit').html(dollarFormat(profPerOzHeinz*soupServed*8));
	$('#_16oz_annPrepProfit').html(dollarFormat(profPerOzHeinz*soupServed*16));
	$('#_32oz_annPrepProfit').html(dollarFormat(profPerOzHeinz*soupServed*32));
	$('#gal_annPrepProfit').html(dollarFormat(profPerOzHeinz*soupServed*128));
	
	$('#_1oz_annDifference').html(dollarFormat(diffPerOz*soupServed));
	$('#_8oz_annDifference').html(dollarFormat(diffPerOz*soupServed*8));
	$('#_16oz_annDifference').html(dollarFormat(diffPerOz*soupServed*16));
	$('#_32oz_annDifference').html(dollarFormat(diffPerOz*soupServed*32));
	$('#gal_annDifference').html(dollarFormat(diffPerOz*soupServed*128));
}

function updateHiddenPDFFields()
{
	//set all of the ASP inputs to the static HTML input values
	
	//ingredients
	for(i = 1; i < 50; i++){
		$('#hdnPDFVal' + i + 'A').attr('value', $('#pdf' + i + 'A').html());
		$('#hdnPDFVal' + i + 'B').attr('value', $('#pdf' + i + 'B').attr('value'));
		$('#hdnPDFVal' + i + 'C').attr('value', $('#pdf' + i + 'C').attr('value'));
		$('#hdnPDFVal' + i + 'D').attr('value', $('#pdf' + i + 'D').attr('value'));
		$('#hdnPDFVal' + i + 'E').attr('value', $('#pdf' + i + 'E').attr('value'));
	}
	
	//Customer Name and Sales Rep
	$('#hdnPDF_txtCustomerName').attr('value', $('#txtCustomerName').attr('value'));
	$('#hdnPDF_txtHeinzSalesRep').attr('value', $('#txtHeinzSalesRep').attr('value'));
	
	//Scratch and Profit per Ounce
	$('#hdnPDF_ScratchCostPerOunce').attr('value', $('#oz_scrTotal').html());
	$('#hdnPDF_PrepCostPerOunce').attr('value', $('#oz_prepTotal').html());
	$('#hdnPDF_ScratchProfitPerOunce').attr('value', $('#oz_scrProfit').html());
	$('#hdnPDF_PrepProfitPerOunce').attr('value', $('#oz_prepProfit').html());
	
	//generated numbers
	$('#hdnPDF_gal_ingTotal').attr('value', $('#gal_ingTotal').html());
	$('#hdnPDF_32oz_ingTotal').attr('value', $('#_32oz_ingTotal').html());
	$('#hdnPDF_16oz_ingTotal').attr('value', $('#_16oz_ingTotal').html());
	$('#hdnPDF_8oz_ingTotal').attr('value', $('#_8oz_ingTotal').html());
	$('#hdnPDF_1oz_ingTotal').attr('value', $('#_1oz_ingTotal').html());

	$('#hdnPDF_gal_scrTotal').attr('value', $('#gal_scrTotal').html());
	$('#hdnPDF_32oz_scrTotal').attr('value', $('#_32oz_scrTotal').html());
	$('#hdnPDF_16oz_scrTotal').attr('value', $('#_16oz_scrTotal').html());
	$('#hdnPDF_8oz_scrTotal').attr('value', $('#_8oz_scrTotal').html());
	$('#hdnPDF_1oz_scrTotal').attr('value', $('#_1oz_scrTotal').html());
		
	$('#hdnPDF_gal_scrProfit').attr('value', $('#gal_scrProfit').html());
	$('#hdnPDF_32oz_scrProfit').attr('value', $('#_32oz_scrProfit').html());
	$('#hdnPDF_16oz_scrProfit').attr('value', $('#_16oz_scrProfit').html());
	$('#hdnPDF_8oz_scrProfit').attr('value', $('#_8oz_scrProfit').html());
	$('#hdnPDF_1oz_scrProfit').attr('value', $('#_1oz_scrProfit').html());
		
	$('#hdnPDF_gal_prepTotal').attr('value', $('#gal_prepTotal').html());
	$('#hdnPDF_32oz_prepTotal').attr('value', $('#_32oz_prepTotal').html());
	$('#hdnPDF_16oz_prepTotal').attr('value', $('#_16oz_prepTotal').html());
	$('#hdnPDF_8oz_prepTotal').attr('value', $('#_8oz_prepTotal').html());
	$('#hdnPDF_1oz_prepTotal').attr('value', $('#_1oz_prepTotal').html());
	
	$('#hdnPDF_gal_prepProfit').attr('value', $('#gal_prepProfit').html());
	$('#hdnPDF_32oz_prepProfit').attr('value', $('#_32oz_prepProfit').html());
	$('#hdnPDF_16oz_prepProfit').attr('value', $('#_16oz_prepProfit').html());
	$('#hdnPDF_8oz_prepProfit').attr('value', $('#_8oz_prepProfit').html());
	$('#hdnPDF_gal_prepProfit').attr('value', $('#_1oz_prepProfit').html());
	
	$('#hdnPDF_gal_difference').attr('value', $('#gal_difference').html());
	$('#hdnPDF_32oz_difference').attr('value', $('#_32oz_difference').html());
	$('#hdnPDF_16oz_difference').attr('value', $('#_16oz_difference').html());
	$('#hdnPDF_8oz_difference').attr('value', $('#_8oz_difference').html());
	$('#hdnPDF_1oz_difference').attr('value', $('#_1oz_difference').html());
	
	//generated annual numbers
	$('#hdnPDF_gal_annIngTotal').attr('value', $('#gal_annIngTotal').html());
	$('#hdnPDF_32oz_annIngTotal').attr('value', $('#_32oz_annIngTotal').html());
	$('#hdnPDF_16oz_annIngTotal').attr('value', $('#_16oz_annIngTotal').html());
	$('#hdnPDF_8oz_annIngTotal').attr('value', $('#_8oz_annIngTotal').html());
	$('#hdnPDF_1oz_annIngTotal').attr('value', $('#_1oz_annIngTotal').html());

	$('#hdnPDF_gal_annScrTotal').attr('value', $('#gal_annScrTotal').html());
	$('#hdnPDF_32oz_annScrTotal').attr('value', $('#_32oz_annScrTotal').html());
	$('#hdnPDF_16oz_annScrTotal').attr('value', $('#_16oz_annScrTotal').html());
	$('#hdnPDF_8oz_annScrTotal').attr('value', $('#_8oz_annScrTotal').html());
	$('#hdnPDF_1oz_annScrTotal').attr('value', $('#_1oz_annScrTotal').html());
		
	$('#hdnPDF_gal_annScrProfit').attr('value', $('#gal_annScrProfit').html());
	$('#hdnPDF_32oz_annScrProfit').attr('value', $('#_32oz_annScrProfit').html());
	$('#hdnPDF_16oz_annScrProfit').attr('value', $('#_16oz_annScrProfit').html());
	$('#hdnPDF_8oz_annScrProfit').attr('value', $('#_8oz_annScrProfit').html());
	$('#hdnPDF_1oz_annScrProfit').attr('value', $('#_1oz_annScrProfit').html());
		
	$('#hdnPDF_gal_annPrepTotal').attr('value', $('#gal_annPrepTotal').html());
	$('#hdnPDF_32oz_annPrepTotal').attr('value', $('#_32oz_annPrepTotal').html());
	$('#hdnPDF_16oz_annPrepTotal').attr('value', $('#_16oz_annPrepTotal').html());
	$('#hdnPDF_8oz_annPrepTotal').attr('value', $('#_8oz_annPrepTotal').html());
	$('#hdnPDF_1oz_annPrepTotal').attr('value', $('#_1oz_annPrepTotal').html());
	
	$('#hdnPDF_gal_annPrepProfit').attr('value', $('#gal_annPrepProfit').html());
	$('#hdnPDF_32oz_annPrepProfit').attr('value', $('#_32oz_annPrepProfit').html());
	$('#hdnPDF_16oz_annPrepProfit').attr('value', $('#_16oz_annPrepProfit').html());
	$('#hdnPDF_8oz_annPrepProfit').attr('value', $('#_8oz_annPrepProfit').html());
	$('#hdnPDF_gal_annPrepProfit').attr('value', $('#_1oz_annPrepProfit').html());
	
	$('#hdnPDF_gal_annDifference').attr('value', $('#gal_annDifference').html());
	$('#hdnPDF_32oz_annDifference').attr('value', $('#_32oz_annDifference').html());
	$('#hdnPDF_16oz_annDifference').attr('value', $('#_16oz_annDifference').html());
	$('#hdnPDF_8oz_annDifference').attr('value', $('#_8oz_annDifference').html());
	$('#hdnPDF_1oz_annDifference').attr('value', $('#_1oz_annDifference').html());	
}

function clearAllHiddenPDFFields()
{
	//set all of the ASP inputs to the static HTML input values
	
	//ingredients
	for(i = 1; i < 50; i++){
		$('#hdnPDFVal' + i + 'A').attr('value', '');
		$('#hdnPDFVal' + i + 'B').attr('value', '');
		$('#hdnPDFVal' + i + 'C').attr('value', '');
		$('#hdnPDFVal' + i + 'D').attr('value', '');
		$('#hdnPDFVal' + i + 'E').attr('value', '');
	}

	//Customer Name and Sales Rep
	$('#hdnPDF_txtCustomerName').attr('value','');
	$('#hdnPDF_txtHeinzSalesRep').attr('value', '');
	
	//Scratch and Profit per Ounce
	$('#hdnPDF_ScratchCostPerOunce').attr('value','');
	$('#hdnPDF_PrepCostPerOunce').attr('value', '');
	$('#hdnPDF_ScratchProfitPerOunce').attr('value', '');
	$('#hdnPDF_PrepProfitPerOunce').attr('value', '');
	
	//generated numbers
	$('#hdnPDF_gal_ingTotal').attr('value', '');
	$('#hdnPDF_32oz_ingTotal').attr('value','');
	$('#hdnPDF_16oz_ingTotal').attr('value', '');
	$('#hdnPDF_8oz_ingTotal').attr('value', '');
	$('#hdnPDF_1oz_ingTotal').attr('value', '');

	$('#hdnPDF_gal_scrTotal').attr('value', '');
	$('#hdnPDF_32oz_scrTotal').attr('value', '');
	$('#hdnPDF_16oz_scrTotal').attr('value', '');
	$('#hdnPDF_8oz_scrTotal').attr('value', '');
	$('#hdnPDF_1oz_scrTotal').attr('value', '');
		
	$('#hdnPDF_gal_scrProfit').attr('value', '');
	$('#hdnPDF_32oz_scrProfit').attr('value', '');
	$('#hdnPDF_16oz_scrProfit').attr('value', '');
	$('#hdnPDF_8oz_scrProfit').attr('value', '');
	$('#hdnPDF_1oz_scrProfit').attr('value', '');
		
	$('#hdnPDF_gal_prepTotal').attr('value', '');
	$('#hdnPDF_32oz_prepTotal').attr('value', '');
	$('#hdnPDF_16oz_prepTotal').attr('value', '');
	$('#hdnPDF_8oz_prepTotal').attr('value', '');
	$('#hdnPDF_1oz_prepTotal').attr('value', '');
	
	$('#hdnPDF_gal_prepProfit').attr('value', '');
	$('#hdnPDF_32oz_prepProfit').attr('value', '');
	$('#hdnPDF_16oz_prepProfit').attr('value', '');
	$('#hdnPDF_8oz_prepProfit').attr('value', '');
	$('#hdnPDF_gal_prepProfit').attr('value', '');
	
	$('#hdnPDF_gal_difference').attr('value', '');
	$('#hdnPDF_32oz_difference').attr('value', '');
	$('#hdnPDF_16oz_difference').attr('value', '');
	$('#hdnPDF_8oz_difference').attr('value', '');
	$('#hdnPDF_1oz_difference').attr('value', '');
	
	//generated annual numbers
	$('#hdnPDF_gal_annIngTotal').attr('value', '');
	$('#hdnPDF_32oz_annIngTotal').attr('value', '');
	$('#hdnPDF_16oz_annIngTotal').attr('value', '');
	$('#hdnPDF_8oz_annIngTotal').attr('value', '');
	$('#hdnPDF_1oz_annIngTotal').attr('value', '');

	$('#hdnPDF_gal_annScrTotal').attr('value', '');
	$('#hdnPDF_32oz_annScrTotal').attr('value', '');
	$('#hdnPDF_16oz_annScrTotal').attr('value', '');
	$('#hdnPDF_8oz_annScrTotal').attr('value', '');
	$('#hdnPDF_1oz_annScrTotal').attr('value', '');
		
	$('#hdnPDF_gal_annScrProfit').attr('value', '');
	$('#hdnPDF_32oz_annScrProfit').attr('value', '');
	$('#hdnPDF_16oz_annScrProfit').attr('value', '');
	$('#hdnPDF_8oz_annScrProfit').attr('value', '');
	$('#hdnPDF_1oz_annScrProfit').attr('value', '');
		
	$('#hdnPDF_gal_annPrepTotal').attr('value', '');
	$('#hdnPDF_32oz_annPrepTotal').attr('value', '');
	$('#hdnPDF_16oz_annPrepTotal').attr('value', '');
	$('#hdnPDF_8oz_annPrepTotal').attr('value', '');
	$('#hdnPDF_1oz_annPrepTotal').attr('value', '');
	
	$('#hdnPDF_gal_annPrepProfit').attr('value', '');
	$('#hdnPDF_32oz_annPrepProfit').attr('value', '');
	$('#hdnPDF_16oz_annPrepProfit').attr('value', '');
	$('#hdnPDF_8oz_annPrepProfit').attr('value', '');
	$('#hdnPDF_gal_annPrepProfit').attr('value', '');
	
	$('#hdnPDF_gal_annDifference').attr('value', '');
	$('#hdnPDF_32oz_annDifference').attr('value', '');
	$('#hdnPDF_16oz_annDifference').attr('value', '');
	$('#hdnPDF_8oz_annDifference').attr('value', '');
	$('#hdnPDF_1oz_annDifference').attr('value', '');	
}

function changeDisplayResults()
{
	$('#displayoz').hide();
	$('#display8oz').hide();
	$('#display16oz').hide();
	$('#display32oz').hide();
	$('#displayGal').hide();
	$('#displayAnnoz').hide();
	$('#displayAnn8oz').hide();
	$('#displayAnn16oz').hide();
	$('#displayAnn32oz').hide();
	$('#displayAnnGal').hide();
	
	switch($('#ddlDisplayResults').attr('value'))
	{
		case 'gal':
			$('#displayGal').show();
			$('#displayAnnGal').show();
			break;
		case '32oz':
			$('#display32oz').show();
			$('#displayAnn32oz').show();
			break;
		case '16oz':
			$('#display16oz').show();
			$('#displayAnn16oz').show();
			break;
		case '8oz':
			$('#display8oz').show();
			$('#displayAnn8oz').show();
			break;
		case 'oz':
			$('#displayoz').show();
			$('#displayAnnoz').show();
			break;
		default:
			$('#displayGal').show();
			break;
	}
}

function displayHeaderDefinition(header)
{
	var textHeader = '';
	var textBody = '';
	var parentID = $(header).parent().parent().parent().parent().attr('id');
	
	textHeader += $(header).html();
	
	if(parentID == 'annualTable') textHeader += ' (Annual)';
	
	switch($(header).html())
	{
		case 'Cost of Scratch Ingredients':
			textBody += 'Scratch Recipe Ingredient Cost. ';
			if(parentID == 'annualTable') textBody += 'This value is an annual amount.';
			textBody += '\n\n';
			textBody += '(Sum of all the ingredients listed in the scratch recipe)';
			if(parentID == 'annualTable') textBody += '\nMultiplied by Soup Servings per Year.';
			break;
		case 'Cost of Scratch Ingredients + Labor':
			textBody += 'Scratch Recipe Ingredient Cost, including Labor. ';
			if(parentID == 'annualTable') textBody += 'This value is an annual amount.';
			textBody += '\n\n';
			textBody += '(Cost of the Ingredients + (Prep Time X Labor Cost))';
			if(parentID == 'annualTable') textBody += '\nMultiplied by Soup Servings per Year.';
			break;
		case 'Heinz Prepared Soup Cost':
			textBody += 'Cost of Heinz Prepared Soup. ';
			if(parentID == 'annualTable') textBody += 'This value is an annual amount.';
			textBody += '\n\n';
			textBody += '(Heinz Prepared Soup Price + Milk Cost)';
			if(parentID == 'annualTable') textBody += '\nMultiplied by Soup Servings per Year.';
			break;
		case 'Scratch Profit':
			textBody += 'Profit from Making a Scratch Recipe. ';
			if(parentID == 'annualTable') textBody += 'This value is an annual amount.';
			textBody += '\n\n';
			textBody += '(Menu Price – Scratch Total Cost)';
			if(parentID == 'annualTable') textBody += '\nMultiplied by Soup Servings per Year.';
			break;
		case 'Heinz Prepared Soup Profit':
			textBody += 'Profit from Buying Soup from Heinz. ';
			if(parentID == 'annualTable') textBody += 'This value is an annual amount.';
			textBody += '\n\n';
			textBody += '(Menu Price – (Heinz Prepared Soup Price + Milk Cost))';
			if(parentID == 'annualTable') textBody += '\nMultiplied by Soup Servings per Year.';
			break;
		case 'Difference':
			textBody += 'The difference in profit from using Heinz Prepared Soup versus the Scratch Recipe selected. ';
			if(parentID == 'annualTable') textBody += 'This value is an annual amount.';
			textBody += '\n\n';
			textBody += '(Heinz Prepared Soup Profit - Scratch Profit)';
			if(parentID == 'annualTable') textBody += '\nMultiplied by Soup Servings per Year.';
			break;
	}	
	
	alert(textHeader + '\n' + textBody);
}

function displayPDFWarning()
{
	var msg = "Caution\n\n";
	
	msg += "All of the information displayed on the current page is about to be compiled into a PDF document.  After this information is compiled, you will no longer be able to edit it.\n\n";
	msg += "Are you sure you would like to create a PDF of the current page?\n\n";
	
	return confirm(msg);	
}

function dollarFormat(number)
{
	//this function simply takes all of the information on the page 
	//and formats it so it looks like dollar amounts. i.e. 4.10 rather than 4.1
    var value;
			
	if(number == "") number = "0";
	value = Math.round(number*100)/100;
	value = (parseFloat(value)).toFixed(2);
	value = dollarCommaFormat(value);

	return value;

}

function dollarCommaFormat(amount) {
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}

function IE_DropDownDisplay(fadeType) 
{
	//this is a fix for an IE z-index bug.  Apparently <select> elements have no z-index in IE-6.
	if(navigator.appName == 'Microsoft Internet Explorer' && getIEVersionNumber() < 7 ){
	
		if(fadeType == 'fadeOut'){
			$('select').fadeOut('fast');
		}
		else if(fadeType == 'fadeIn'){
			$('select').fadeIn('fast');
		}
	}
} 

function getIEVersionNumber() 
{
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}
