var parser;
var domDoc;
var docRoot;
var foodDoc = null;
var listXSL = null;
var recipeXSL = null;
var foodsXSL = null;
var recipeDoc = null;
var foodListDoc = null;
var nutritionXSL = null;
var nutritionDoc = null;
var foodRowXSL = null;
var foodRowDoc = null;
var foodIndex = 0;
var aXML = new Array();
var xslReq;
var loadin;
var listContainer;
var recipeContainer;
var xmlContainer;
var aTotals;
var initLoggedIn = false;
var userBowl = "";
var foodCount = new Array();
var foodLimit = new Array();
var tipIndex = new Array();
var cabAlertBox;
var cabAlertButton;
var cabAlertMsg;

logging = false;
logging__ = false;
xsltdebug = false;
xpathdebug = false;

function initXML(sLogged) {
	if (sLogged == "yup") {
		initLoggedIn = true;	
	}
	
	foodCount["Meats"] = 0;
	foodCount["Vegetables"] = 0;
	foodCount["Sauces"] = 0;
	foodCount["Starches"] = 0;
	foodLimit["Meats"] = 3;
	foodLimit["Vegetables"] = 10;
	foodLimit["Sauces"] = 6;
	foodLimit["Starches"] = 10;
	
	aTotals = new Array("tc","tf","sf","carb","df","prot");
	
	initXmlRequest();

	// This is definitely going into Bosco's badass library o' tricks
//	addFile("foodDoc","/experience/ingredients.xml");
	addFile("foodDoc","/experience/ingredients.php");

	addFile("foodsXSL","/experience/transforms/foodList.xsl");
	addFile("recipeXSL","/experience/transforms/recipeCard.xsl");
	addFile("nutritionXSL","/experience/transforms/recipeNutrition.xsl");
	addFile("foodRowXSL","/experience/transforms/foodRow.xsl");
		
	loadXML();
}

function addFile(sVarName,sFileName) {
	obj = new Object();
	obj.varName = sVarName;
	obj.fileName = sFileName;
	
	aXML.push(obj);
}

function loadXML() {
	if(aXML.length > 0) {
		loadin = aXML.pop();
	
		xslReq.open("GET", loadin.fileName, true);
		xslReq.onreadystatechange = gotXML;
		xslReq.send(null);
	}
}

function initDoc() {
	domDoc = xmlParse(foodDoc);
	foodListDoc = xmlParse(foodsXSL);
	recipeDoc = xmlParse(recipeXSL);
	nutritionDoc = xmlParse(nutritionXSL);
	foodRowDoc = xmlParse(foodRowXSL);
	
	foodSlider = document.getElementById("foodList");
	menuContainer = document.getElementById("menuContainer");
	cabAlertBox = document.getElementById("alertBox");
	cabAlertButton = document.getElementById("closeBoxButton");
	cabAlertMsg = document.getElementById("alertMsg");

	//now set main menu contents for if they're logged in or not
	document.getElementById("mmContents").innerHTML = (initLoggedIn) ? document.getElementById("mm_loggedin").innerHTML : document.getElementById("mm_loggedout").innerHTML;
	drawDisplay();
}

function drawDisplay() {
	document.getElementById("recipeContents").innerHTML = xsltProcess(domDoc, recipeDoc);
	document.getElementById("bowlNutrition").innerHTML = xsltProcess(domDoc, nutritionDoc);
//	document.getElementById("recipeContents").innerHTML = xsltProcess(bowlDoc, recipeDoc);
	
}

function gotXML() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			eval(loadin.varName + ' = xslReq.responseText');
			if(aXML.length > 0) {
				loadin = aXML.pop();

				// if I don't re-init the request object it won't work...
				initXmlRequest();

				xslReq.open("GET", loadin.fileName, true);
				xslReq.onreadystatechange = gotXML;
				xslReq.send(null);
			} else {
				initDoc();	
			}
		} else {
			alert("Couldn't load a required resource");
		}
	}
}

function initXmlRequest() {
	xslReq = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xslReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			xslReq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xslReq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!xslReq) {
		alert('Your web browser is not compatible with our Create a Bowl interface.');
		return false;
	} else {
		return true;	
	}
}


function addFood(id,sCat) {
	
	if (foodCount[sCat] >= foodLimit[sCat] ) {
		switch(sCat) {
			case "Meats":
				popMessage("<span style=\"font-size:18px\">That's a lot of meat!</span><p>As a general rule, try to limit the meat and seafood in your bowl to about 9 ounces or less <br/> (about 3 full tongs).</p>");
				break;
			case "Vegetables":
				popMessage("<span style=\"font-size:18px\">Veggie overload!</span><p>We love vegetables too, but you'll want to limit your vegetables to around 10 ounces worth <br>(our bowls aren't <em>that</em> gigantic)</p>");
				break;
			case "Sauces":
				popMessage("<span style=\"font-size:18px\">Your sauce cup runneth over!</span><p>Our sauce cups hold about 3 ounces, so up to 3 ounces of your favorite sauces should do it</p>");
				break;
		}
	} else {
		foodCount[sCat]++;
	
		var expr = xpathParse("//food[@id=" + id + "]");
		var nodez = expr.evaluate(new ExprContext(domDoc));
		var aFoods = nodez.nodeSetValue();
	
	
		var cnt = aFoods[0].getAttribute("num");
		cnt++;
		aFoods[0].setAttribute("num",cnt);
		
		aFoods[0].setAttribute("sorty",foodIndex);
		foodIndex++;
		
		var aTotal = domDoc.getElementsByTagName("totals");
		
		for(x in aTotals) {
			fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2));
			rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
			
			tot = fTotal + rTotal;
	
			aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
		}
	
	
		document.getElementById("food_" + id).innerHTML = xsltProcess(xmlParse(xmlText(aFoods[0])), foodRowDoc);
	//	updateFoodRow(id,xmlText(aFoods[0]));
	//	drawFoods(sCat);
		drawDisplay();
	}
}

function removeFood(id,sCat) {
	foodCount[sCat]--;

	var expr = xpathParse("//food[@id=" + id + "]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();
	
	var cnt = aFoods[0].getAttribute("num");
	cnt--;
	if(cnt < 0) {
		cnt = 0;
		aFoods[0].setAttribute("sorty",0);
	}
	
	aFoods[0].setAttribute("num",cnt);

	var aTotal = domDoc.getElementsByTagName("totals");
	
	for(x in aTotals) {
		fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2));
		rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
		
		tot = rTotal - fTotal;

		aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
	}

	if(dv = document.getElementById("food_" + id)) {
		dv.innerHTML = xsltProcess(xmlParse(xmlText(aFoods[0])), foodRowDoc);
	}


//	drawFoods(sCat);
	drawDisplay();
}

function updateFoodRow(id,fNode) {
	if(dv = document.getElementById("food_" + id)) {
		dv.innerHTML = xsltProcess(domDoc, foodRowDoc);
	}
}

function xmlDisplay() {
	rLT = /</g;	
	rGT = />/g;
	
	docString = new String(xmlText(domDoc));
	
	xmlContainer.innerHTML = "<pre>" + docString.replace(rLT,"&lt;") + "</pre>";
}

function foodCat(sCat) {
	document.getElementById("tipContent").innerHTML = document.getElementById("tips_" + sCat).innerHTML;
	drawFoods(sCat);
	slideMainMenu("foods");
}

function drawFoods(sCat) {
	var expr = xpathParse("//food[@cat='" + sCat + "']");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();

//	alert("string: " + nodez.stringValue());
	
	var sXML = "<root><cat>" + sCat + "</cat>";
	for(i=0;i<aFoods.length;i++) {
		sXML += xmlText(aFoods[i]);
	}

	var xml = xmlParse(sXML + "</root>");

	var html = xsltProcess(xml, foodListDoc);
	
//	alert(html);

	document.getElementById("foodList").innerHTML = html;
}


function changeTips(dest,sDiv) {
	if(dest < 0) {
		tipIndex.push(sDiv);
	} else {
		tipIndex.pop();
		if (tipIndex.length <= 0) {
			sDiv = (userBowl == "") ? "welcome" : "main";
		} else {
			sDiv = tipIndex[(tipIndex.length - 1)];
		}
/*		
		
		sTmp = tipIndex.pop();
		if(sTmp == sDiv) {
			sDiv =  ? "welcome" : tipIndex.pop();
		} else {
			sDiv = sTmp;
		}
*/
	}

	var tip = null;

	switch(sDiv) {
		case "foods_Meats":
		case "foods_Vegetables":
		case "foods_Sauces":
			tip = "tips_" + String(sDiv).substr(6);	
			break;
		
		default:
			if(document.getElementById("tips_" + sDiv)) {
				tip = "tips_" + sDiv;	
			} else {
				alert("div: " + sDiv + "\nlength: " + tipIndex.length);	
			}
			
			break;
			
	}
	if(tip != null) {
		document.getElementById("tipContent").innerHTML = document.getElementById(tip).innerHTML;
	}
}

////////////////////////////////////////////
//  User Accounts

function showErrorMsg(sDiv,sMsg) {
	errBox = document.getElementById(sDiv)
	errBox.innerHTML = sMsg;
	errBox.style.visibility = "visible";	
}

function closeErrorMsg(sDiv) {
	errBox = document.getElementById(sDiv)
	if(errBox.style.visibility == "visible") {
		errBox.innerHTML = "";
		errBox.style.visibility = "hidden";
	}
}

function createAccount() {

	var caFlds = new Array("userNameCreate","userEmailCreate","userPassCreate","userPassConfirm");
	for (x in caFlds) {
		if (document.getElementById(caFlds[x]).value == "") {
			showErrorMsg("err_createAccount","You must fill out all 4 fields to create your account");
			return;
		}
	}

	if (document.getElementById("userPassCreate").value != document.getElementById("userPassConfirm").value) {
		showErrorMsg("err_createAccount","Your password entries do not match");
		return;
	}
	
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doCabAccountCreate.php", true);
	xslReq.onreadystatechange = gotAccountCreate;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	var pdiddyInfo = "username=" + document.getElementById("userNameCreate").value + "&userpass=" + document.getElementById("userPassCreate").value + "&useremail=" + document.getElementById("userEmailCreate").value;
	xslReq.send(pdiddyInfo);
	
//	closeErrorMsg("err_createAccount");
//	slideMainMenu("createAccount");
}

function gotAccountCreate() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			sReturno = String(xslReq.responseText);
			
			if (sReturno.substr(0,7) == "WELCOME") {
				document.getElementById("loginName").innerHTML = sReturno + ' | <a class="redLink" href="javascript:logout()">LOG OUT</a>';
	
				if ((foodCount["Meats"] + foodCount["Vegetables"] + foodCount["Sauces"]) > 0) {
					document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin_bowl").innerHTML;
				} else {
					document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin").innerHTML;
				}
				
				closeErrorMsg("err_createAccount");
				slideMainMenu("createAccount");	
			} else {
				showErrorMsg("err_createAccount",sReturno);
			}
		} else {
			alert("Couldn't load a required resource");
		}
	}
}

function sendLogin() {
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doCabLogin.php", true);
	xslReq.onreadystatechange = gotLogin;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	var pdiddyInfo = "username=" + document.getElementById("userName").value + "&userpass=" + document.getElementById("userPass").value;
	xslReq.send(pdiddyInfo);
}

function gotLogin() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			sReturno = String(xslReq.responseText);
			
			if (sReturno.substr(0,7) == "WELCOME") {
				document.getElementById("loginName").innerHTML = sReturno + ' | <a class="redLink" href="javascript:logout()">LOG OUT</a>';

				if ((foodCount["Meats"] + foodCount["Vegetables"] + foodCount["Sauces"]) > 0) {
					document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin_bowl").innerHTML;
				} else {
					document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin").innerHTML;
				}
				
				initLoggedIn = true;

				closeErrorMsg("err_login");
				slideMainMenu("loginMenu");
			} else {
				showErrorMsg("err_login",sReturno);
			}
		} else {
			alert("Couldn't load a required resource");
		}
	}
	document.getElementById("userPass").value = "";
}


function forgotPass() {
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doForgotPassword.php", true);
	xslReq.onreadystatechange = gotForgotPass;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	var pdiddyInfo = "username=" + document.getElementById("forgotPassEmail").value;
	xslReq.send(pdiddyInfo);
}

function gotForgotPass() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			if(xslReq.responseText == "sent") {
				closeErrorMsg("err_forgotpass");
				
				popMessage("You password has been sent to your email account");

				slideMainMenu("forgotPass");
			} else {
				showErrorMsg("err_forgotpass",xslReq.responseText);
			}
		} else {
			alert("Couldn't load a required resource");
		}
	}
	document.getElementById("forgotPassEmail").value = "";
}

function logout() {
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doCabLogout.php", true);
	xslReq.send(null);

	initLoggedIn = false;

	if ((foodCount["Meats"] + foodCount["Vegetables"] + foodCount["Sauces"]) > 0) {
		document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedout_bowl").innerHTML;
	} else {
		document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedout").innerHTML;
	}
	document.getElementById("loginName").innerHTML = "YOU ARE NOT LOGGED IN";
}

//////////////////////////////////////
// Loading, Saving, and Fabulous Printing of Recipes

function nameBowl() {
	userBowl = document.getElementById("bowlName2").value;
	document.getElementById("recipeName").innerHTML = userBowl;
	document.getElementById("bowlName").value = userBowl;
	
	document.getElementById("foods_Meats").style.left = "276px";
	tipIndex.pop();
	document.getElementById("foods_Vegetables").style.left = "276px";
	tipIndex.pop();
	document.getElementById("foods_Sauces").style.left = "276px";
	tipIndex.pop();
	
	if (initLoggedIn) {
		document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin_bowl").innerHTML
	} else {
		document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedout_bowl").innerHTML
	}
	
//	popMessage("Presenting... <p style=\"font-size:18px\">" + userBowl + "</p>Fantastic!");
	slideMainMenu('nameMenu');

}

function newBowl() {
	resetBowl();	
	drawDisplay();
	document.getElementById("recipeName").innerHTML = ":: Original bd's Bowl Creation ::";
	document.getElementById("bowlName").value = "";
	document.getElementById("bowlName2").value = "";
	userBowl = "";
	slideMainMenu('foods_Meats');
}

function userBowlXML() {
	var expr = xpathParse("//food[@num>0]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();

	var sXML = "<root><bowlname>" + document.getElementById("bowlName").value + "</bowlname>";
	for(i=0;i<aFoods.length;i++) {
		sXML += xmlText(aFoods[i]);
	}

	var expr = xpathParse("//totals");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aTots = nodez.nodeSetValue();
	sXML += xmlText(aTots[0]);
	
	sXML += "</root>";

	return sXML;
}

function printBowl() {
	sXML = userBowlXML();

	// send the string to server
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doPrintBowl.php", true);
	xslReq.onreadystatechange = gotBowlPrint;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	userBowl = document.getElementById("bowlName").value;
	
	var pdiddyInfo = "bname=" + document.getElementById("bowlName").value + "&bowl=" + sXML;
	xslReq.send(pdiddyInfo);
	
}

function printBowl() {
	sXML = userBowlXML();

	// send the string to server
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doPrintBowl.php", true);
	xslReq.onreadystatechange = gotBowlPrint;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	userBowl = document.getElementById("bowlName").value;
	
	var pdiddyInfo = "bname=" + document.getElementById("bowlName").value + "&bowl=" + sXML;
	xslReq.send(pdiddyInfo);
	
}

function gotBowlPrint() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			winRecipe = window.open("recipe.html","printRecipe","width=620,height=750,scrollbars=auto");
		} else {
			alert("Couldn't load a required resource");
		}
	}
}

function sendEmail() {
	document.getElementById("sendinButton").disabled = true;

	sXML = userBowlXML();

	// send the string to server
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doEmailBowl.php", true);
	xslReq.onreadystatechange = gotSendEmail;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	userBowl = document.getElementById("bowlName").value;
	
	var pdiddyInfo = "toaddy=" + document.getElementById("sendToEmail").value + "&bname=" + document.getElementById("bowlName").value + "&emailMsg=" + document.getElementById("emailMsg").value + "&bowl=" + sXML;
	xslReq.send(pdiddyInfo);
	
}

function gotSendEmail() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
//			winRecipe = window.open("recipe.html","printRecipe","width=620,height=750,scrollbars=auto");
			popMessage(xslReq.responseText);
//			alert(xslReq.responseText);

			//this condition added due to camron's constant nagging
			if(xslReq.responseText != "You must be logged in to send an email") {
				slideMainMenu('sendEmail');
				document.getElementById("sendToEmail").value = "";
				document.getElementById("emailMsg").value = "";
			}
		} else {
			alert("Couldn't load a required resource");
		}
	}
	document.getElementById("sendinButton").disabled = false;
}

function saveBowl() {
	popStatus("saving");
	// dump only the food nodes that are used into a string
	var expr = xpathParse("//food[@num>0]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();

	var sXML = "<root><bowlname>" + document.getElementById("bowlName").value + "</bowlname>";
	for(i=0;i<aFoods.length;i++) {
		sXML += xmlText(aFoods[i]);
	}
	
	sXML += "</root>";

	// send the string to server
	initXmlRequest();
	
	xslReq.open("POST", "/experience/doSaveBowl.php", true);
	xslReq.onreadystatechange = gotBowlSave;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
//	if (userBowl == document.getElementById("bowlName").value) {
//		savin = "update";			
//	} else {
//		userBowl = document.getElementById("bowlName").value;
//		savin = "new";
//	}
	
	var pdiddyInfo = "bname=" + document.getElementById("bowlName").value + "&bowl=" + sXML;
	xslReq.send(pdiddyInfo);

}

function gotBowlSave() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
//			alert(xslReq.responseText);
			if (xslReq.responseText == "toomanyrecipes") {
				closeStatus();
				popMessage("You have reached your limit of 10 saved recipes.");	
			} else {
				document.getElementById("recipeName").innerHTML = ":: " + userBowl + " ::";
				closeStatus();
				slideMainMenu("saveMenu");
			}
		} else {
			closeStatus();
			alert("Couldn't load a required resource");
		}
	}
}

function listSavedBowls() {
	initXmlRequest();
	
	xslReq.open("POST", "/experience/bowlLoader.php", true);
	xslReq.onreadystatechange = gotSavedBowlList;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xslReq.send("accion=list");
	popStatus("loading");
}

function gotSavedBowlList() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			//check to make sure the session didn't expire.  If so, present login screen
			if (xslReq.responseText == "timedout") {
				closeStatus();
				slideMainMenu("loginMenu");
				logout();
			} else {
				document.getElementById("recipeList").innerHTML = xslReq.responseText;
				closeStatus();
				slideMainMenu("savedList");
			}
		} else {
			closeStatus();
			alert("Couldn't load a required resource");
		}
	}
}

function loadRecipe(iKey) {
	initXmlRequest();
	
	xslReq.open("POST", "/experience/bowlLoader.php", true);
	xslReq.onreadystatechange = gotRecipe;
	xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xslReq.send("accion=load&key=" + iKey);
	popStatus("loading");
}

function gotRecipe() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			//check to make sure the session didn't expire.  If so, present login screen
			if (xslReq.responseText == "timedout") {
				slideMainMenu("loginMenu");
				logout();
			} else {
				resetBowl("ih8oil");
				
				var svBowl = xmlParse(xslReq.responseText);
				var aIngs = svBowl.getElementsByTagName("food");
				var aTotal = domDoc.getElementsByTagName("totals");
				
				for (n in aIngs) {
					fId = aIngs[n].getAttribute("id");

					var expr = xpathParse("//food[@id='" + fId + "']");
					var nodez = expr.evaluate(new ExprContext(domDoc));
					var aFoods = nodez.nodeSetValue();
					aFoods[0].setAttribute("num",aIngs[n].getAttribute("num"));

					for(x in aTotals) {
						fTotal = parseFloat(Number(aFoods[0].getAttribute(aTotals[x])).toFixed(2));
						rTotal = parseFloat(Number(aTotal[0].getAttribute(aTotals[x])).toFixed(2));
						
						tot = fTotal + rTotal;
				
						aTotal[0].setAttribute(aTotals[x],parseFloat(tot.toFixed(2)));
					}

					foodCount[aIngs[n].getAttribute("cat")]++;
					document.getElementById("food_" + fId).innerHTML = xsltProcess(xmlParse(xmlText(aIngs[n])), foodRowDoc);
				}
				
				drawDisplay();

				//now handle the damn bowl name
				aIngs = svBowl.getElementsByTagName("bowlname");
				bName = xmlValue(aIngs[0]);
				userBowl = bName;
				document.getElementById("bowlName").value = bName;
				document.getElementById("bowlName2").value = bName;
				document.getElementById("recipeName").innerHTML = ":: " + bName + " ::";
				document.getElementById("mmContents").innerHTML = document.getElementById("mm_loggedin_bowl").innerHTML;
				closeStatus();
				slideMainMenu("savedList");
			}
		} else {
			closeStatus();
			alert("Couldn't load a required resource");
		}
	}
}

function resetBowl(bDefault) {
	var aTotal = domDoc.getElementsByTagName("totals");
	for(x in aTotals) {
		aTotal[0].setAttribute(aTotals[x],0);
	}

	var expr = xpathParse("//food[@num>0]");
	var nodez = expr.evaluate(new ExprContext(domDoc));
	var aFoods = nodez.nodeSetValue();
	for (x in aFoods) {
		aFoods[x].setAttribute("num",0);
		document.getElementById("food_" + aFoods[x].getAttribute("id")).innerHTML = xsltProcess(xmlParse(xmlText(aFoods[x])), foodRowDoc);

	}

	//Last minute hack to support Sesame Vegtable Oil as a default ingredient
	if (bDefault == null) {
		var expr = xpathParse("//food[@id=51]");
		var nodez = expr.evaluate(new ExprContext(domDoc));
		var aFoods = nodez.nodeSetValue();
		aFoods[0].setAttribute("num",1);
		document.getElementById("food_51").innerHTML = xsltProcess(xmlParse(xmlText(aFoods[0])), foodRowDoc);
		//now adjust totals
		for(x in aTotals) {
			aTotal[0].setAttribute(aTotals[x],aFoods[0].getAttribute(aTotals[x]));
		}
	}
	
	foodCount["Meats"] = 0;
	foodCount["Vegetables"] = 0;
	foodCount["Sauces"] = 0;
	foodCount["Starches"] = 0;

}

function popMessage(sMsg) {
//	alert("poppin");
	cabAlertMsg.innerHTML = sMsg;
	cabAlertBox.style.display = "inline";
}

function closeBox() {
	cabAlertBox.style.display = "none";
	cabAlertMsg.innerHTML = "";
}

function popStatus(sStatus) {
	switch (sStatus) {
		case "loading":
			cabAlertMsg.innerHTML = "<div class=\"cabLoading\"></div>";
			break;
		case "saving":
			cabAlertMsg.innerHTML = "<div class=\"cabSaving\"></div>";
			break;
	}
	
	cabAlertButton.style.display = "none";
	cabAlertBox.style.display = "inline";
}

function closeStatus() {
	cabAlertBox.style.display = "none";
	cabAlertButton.style.display = "inline";
	cabAlertMsg.innerHTML = "";
}

function deleteRecipe(iKey) {
	if (confirm("Are you sure you want to delete this recipe?")) {
		initXmlRequest();
		
		xslReq.open("POST", "/experience/doDeleteRecipe.php", true);
		xslReq.onreadystatechange = gotDelete;
		xslReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		xslReq.send("key=" + iKey);
	}
}

function gotDelete() {
	if (xslReq.readyState==4) {
		if (xslReq.status==200) {
			popMessage(xslReq.responseText);
			slideMainMenu("savedList");
		} else {
			alert("Couldn't load a required resource");
		}
	}	
}


/////////////////////////////////
// debugging
function reportXML() {
	alert(xmlText(domDoc));	
}

////////////////////////////////////////////////////
// rollovers

function foodOver(el) {
	el.className = "foodRowOver";	
}

function foodOut(el) {
	el.className = "foodRow";	
}

function foodSelectedOut(el) {
	el.className = "foodRowSelected";	
}

////////////////////////////////////////////////////
//animation functions
var a = null;
var b = null;
var c = null;
var slider = null;
var infoSlider = null;
var infoCat = null;
var openSlider = null;
var foodSlider = null;
var menuContainer = null;
var tmpMenu = null;

function slideMainMenu(sDiv) {
	if (a != null) {
		return;
//		a.stop();
//		a = null;
	}

	if(parseInt(menuContainer.style.height) > 540) {
		slideMenuUp(sDiv);
		return;
	}

	slider = document.getElementById(sDiv);

	var dest = parseInt(slider.style.left) > -3 ? -3 : 276;

	a = new Accelimation(slider.style, "left", dest, 600, .8, "px");
	a.onend = mmEnd;
	a.onframe = mmSlide;
	a.start();
	
	changeTips(dest,sDiv);
}

function mmSlide(x) {
	slider.style.left = x + "px";
}

function mmEnd() {
	a.stop();
	a = null;
	slider = null;
	openSlider = null;
}

function slideNutriton(sDiv,sCat) {
	if (b != null) {
		return;
//		a.stop();
//		a = null;
	}

	infoSlider = document.getElementById(sDiv);
	infoCat = document.getElementById(sCat + "_guts");

//	var dest = parseInt(infoSlider.style.height) > 0 ? 0 : 120;
	var dest = parseInt(infoSlider.style.height) > 0 ? 0 : infoSlider.scrollHeight + 10;

	b = new Accelimation(infoSlider.style, "height", dest, 400, .8, "px");
	b.onend = (dest > 0) ? infoEndDown : infoEndUp;
	b.onframe = infoSlide;
	b.start();
}

function infoSlide(x) {
	infoSlider.style.height = x + "px";
	infoHeight = infoCat.offsetHeight;
	if(infoHeight > 540) {
		menuContainer.style.height = infoHeight + "px";	
	}
}

function infoEndDown() {
//	alert("div: " + infoCat.id + "\noffsetHeight: " + infoCat.offsetHeight)
	b.stop();
	b = null;
	infoSlider = null;
}

function infoEndUp() {
	b.stop();
	b = null;
	infoSlider = null;
	
	if(parseInt(menuContainer.style.height) > 540 && infoCat.offsetHeight <= 540) {
		slideMenuUp(null);	
	}
}

function slideMenuUp(sDiv) {
	if (c != null) {
		return;
//		a.stop();
//		a = null;
	}
	
	if(sDiv != null) {
		tmpMenu = sDiv;	
	}

	c = new Accelimation(menuContainer.style, "height", 540, 200, .5, "px");
	c.onend = mcEnd;
	c.onframe = mcSlide;
	c.start();

}

function mcSlide(x) {
	menuContainer.style.height = x + "px";
}

function mcEnd() {
	c.stop();
	c = null;

	if(tmpMenu != null) {
		slideMainMenu(tmpMenu);
		tmpMenu = null;
	}
}
