dim FoodValues(100), ExpireFuture, ExpirePast, NumServings

ExpireFuture = "expires=Wednesday, 09-Nov-2010 23:12:40 GMT"
ExpirePast = "expires=Wednesday, 09-Nov-2000 23:12:40 GMT"

Public function DisplayFood(VAR)
	dim vals

	vals = split(VAR, "!")

	document.foodsearch.form.value = vals(0)
	document.foodsearch.multiplier.value = vals(1)
	document.foodsearch.units.value = vals(2)
	document.foodsearch.submit
end function

Public Sub DeleteAllFoods()
	for x = 1 to 15
		Document.Cookie = "food" & x & "='';" & ExpirePast
	next
	Document.Cookie = "NumServings='';" & ExpirePast	
	SubmitCookiesToBrowser()
End Sub

public function DeleteFood(NAME)
	GetAllCookies()
	
	'start at the index of the current value.  To remove a cookie from the middle of the list, overwrite each cookie value with the next value
	idx = right(NAME, 1)
	do while FoodValues(idx) <> ""
		Document.Cookie = "food" & idx & "=" & FoodValues(idx + 1) & ";" & ExpireFuture
		idx = idx + 1
	loop
	
	'ExpirePast the last cookie of the list since we have decreased the total number by one
	Document.Cookie = "food" & idx - 1 & "='';" & ExpirePast 
	
	if (idx - 1) = 1 then
		Document.Cookie = "NumServings='';" & ExpirePast
	end if

	GetAllCookies()
	SubmitCookiesToBrowser()
end function

sub SetNumServings()
	NumServings = Document.NumServings.Servings.value
	Document.Cookie = "NumServings=" & NumServings & ";" & ExpireFuture
	GetAllCookies()
	SubmitCookiesToBrowser()
end sub

sub SetRecipeValue(VALUE)
	GetAllCookies()

	'get the last index
	x = 1	
	do while FoodValues(x) <> ""
		x = x + 1
	loop
	Document.Cookie = "food" & x & "=" & VALUE & ";" & ExpireFuture
	GetAllCookies()
	SubmitCookiesToBrowser()
end sub

function GetCookie(NAME)
	txt = Document.Cookie
	pos = instr(txt, NAME)
	if pos = 0 then
		GetCookie = ""
		exit function
	else
		txt = right(txt, len(txt) - pos + 1)
	end if
	
	pos = instr(txt, ";")
	if pos > 0 then txt = left(txt, pos - 1)
	
	pos = instr(txt, "=")
	txt = right(txt, len(txt) - pos)
	
	GetCookie = txt
end function

sub GetAllCookies()	
	for x = 1 to 15
		FoodValues(x) = ""
	next

	'parse cookie values
	for x = 1 to 15
		txt = GetCookie("food" & x)
		if txt <> "" then
			FoodValues(x) = txt
		end if
	next
	NumServings = GetCookie("NumServings")
end sub

Sub SubmitCookiesToBrowser()
	GetAllCookies()

	if NumServings = "0" then 
		NumServings = "1"
	end if	
	Document.recipebuilder.NumServings.value = NumServings
	Document.recipebuilder.food1.value = FoodValues(1)
	Document.recipebuilder.food2.value = FoodValues(2)
	Document.recipebuilder.food3.value = FoodValues(3)
	Document.recipebuilder.food4.value = FoodValues(4)
	Document.recipebuilder.food5.value = FoodValues(5)
	Document.recipebuilder.food6.value = FoodValues(6)
	Document.recipebuilder.food7.value = FoodValues(7)
	Document.recipebuilder.food8.value = FoodValues(8)
	Document.recipebuilder.food9.value = FoodValues(9)
	Document.recipebuilder.food10.value = FoodValues(10)
	Document.recipebuilder.food11.value = FoodValues(11)
	Document.recipebuilder.food12.value = FoodValues(12)
	Document.recipebuilder.food13.value = FoodValues(13)
	Document.recipebuilder.food14.value = FoodValues(14)
	Document.recipebuilder.food15.value = FoodValues(15)
	
	Document.recipebuilder.submit
End Sub