/* ******************************************************
nutrition javascript (foodprocessor.js)
by Kevin Kelleher
http://www.midlandstech.edu/science/kelleherk/
copyright 2004 Kevin Kelleher. All rights reserved.
******************************************************** */
function setMode(modeval) {
	var doswap = true;
	if (modeval == 1 && theMode[0] == 2 && altDay != "") {
		doswap = confirm("Do you really want to clear all your entered food data for day " + (3-theMode[1]) + "?");
		if (doswap) {
			theMode[0] = 1;
			theMode[1] = 1;
			altDay = "";
		}
		else {
			document.form.modemenu.selectedIndex = 1;
		}
	}
	else {
		theMode[0] = modeval;
		if (modeval == 1) {
			theMode[1] = 1;
		}
	}
	if (doswap) {
		getFoods();
	}
}

function swapDay() {
	var dayList = (selIDs + "/" + servIngs + "/" + stats);
	var altSplit = ["","",""];
	if (altDay.indexOf("/") != 0) {
		var altSplit = altDay.split("/");
	}
	selIDs = altSplit[0];
	servIngs = altSplit[1];
	if (stats.indexOf("|") == 0) stats = altSplit[2];
	altDay = dayList;
	theMode[1] = 3-theMode[1];
	getFoods();
}

function setbtnState(btnNum) {
	if (btnNum == 0) {
		if (addActive == 0) {
			document.form.addfood.disabled = false;
			document.form.foodinfo.disabled = false;
			addActive = 1;
		}
	}
	else {
		showSizes();
		if (remActive == 0) {
			document.form.remfood.disabled = false;
			document.form.setsize.disabled = false;
			remActive = 1;
		}
	}
}

function addFood() {
	var dvdr = "|";
	if (selIDs == "") dvdr = "";
	selIDs += dvdr + idList[document.form.menu.selectedIndex];
	servIngs += dvdr + "100";
	setData();
	if (curSearch != "") document.form.foodsearch.value = curSearch;
	document.form.target = "";
	document.form.action = "nutrition.asp";
	document.form.submit();
}

function remFood() {
	var remID = document.form.foodcart.options[document.form.foodcart.selectedIndex].value;
	var modIDs = "";
	var modServs = "";
	var dvdr = "";
	var isrem = 0;
	for(i=0;i<selList.length;i++) {
		if (selList[i] != remID || isrem == 1) {
			modIDs += dvdr + selList[i];
			modServs += dvdr + servsList[i];
			dvdr = "|";
		}
		else {
			isrem = 1;
		}
	}
	selIDs = modIDs;
	servIngs = modServs;
	setData();
	if (curSearch != "") document.form.foodsearch.value = curSearch;
	document.form.target = "";
	document.form.action = "nutrition.asp";
	document.form.submit();
}

function showSizes() {
	var ol = 1;
	document.form.setsize.options.length = ol;
	var itemNum = document.form.foodcart.selectedIndex;
	/*var wtOne = szarrayOne[itemNum].split(":");
	var optionOne = new Option();
	optionOne.text = wtOne[0];
	optionOne.value = Number(wtOne[1]);
	if (wtOne[0] != "") {
		document.form.setsize.options[ol] = optionOne;
		ol = (ol + 1)
	}
	var wtTwo = szarrayTwo[itemNum].split(":");
	var optionTwo = new Option();
	optionTwo.text = wtTwo[0];
	optionTwo.value = Number(wtTwo[1]);
	if (wtTwo[0] != "") {
		document.form.setsize.options[ol] = optionTwo;
		ol = (ol + 1)
	}*/
	var theWts = [];
	if (szarray[itemNum].indexOf("^") != -1) {
		theWts = szarray[itemNum].split("^");
	}
	else {
		theWts[0] = szarray[itemNum];
	}
	for(sz=0;sz<theWts.length;sz++) {
		var gmWt = theWts[sz].split(":");
		var wtOption = new Option();
		wtOption.text = gmWt[0];
		wtOption.value = Number(gmWt[1]);
		if (gmWt[0] != "") {
		document.form.setsize.options[ol] = wtOption;
		ol = (ol + 1)
		}
	}
	var wtChoose = new Option();
	wtChoose.text = "Enter Size...";
	wtChoose.value = "";
	document.form.setsize.options[ol] = wtChoose;
}

function setSize(sizeVal) {
	var itemNum = document.form.foodcart.selectedIndex;
	var custSize = sizeVal;
	if (custSize == "" ) {
		custSize = prompt("Please enter the new serving size in grams...\n(1 ounce = 28.35 grams)",servsList[itemNum]);
	}
	var num=/(^\d+$)|(^\d+\.\d+$)/
	if (custSize != null && num.test(custSize)) {
		custSize = Math.round(custSize*100)/100;
		servsList[itemNum] = custSize;
		servIngs = servsList.join("|");
		if (curSearch != "") document.form.foodsearch.value = curSearch;
		getFoods();
	}
	else {
		document.form.setsize.selectedIndex = 0;
	}
}

function getInfo () {
	window.open("","NutrientInformation","height=400,width=600,resizable,scrollbars");
	document.form.selectedfood.value = idList[document.form.menu.selectedIndex];
	document.form.target = "NutrientInformation";
	document.form.action = "nutrientinfo.asp";
	document.form.submit();
}

function getSummary() {
	setData();
	document.form.target = "_blank";
	document.form.action = "nutritionalsummary.asp";
	document.form.submit();
	// return false;
}

function resetForm() {
	//document.form.reset();
	//document.form.foodsearch.value = "";
	//document.form.foodsearch.focus();
	document.form.category.selectedIndex = currentCat;
	document.form.modemenu.selectedIndex = (theMode[0]-1);
	document.form.action = "javascript:void(null);"
	if (stats != "") {
		var statsList = stats.split("|");
		document.form1.sex[Number(statsList[0])].checked = true;
		document.form1.age.value = statsList[1];
		document.form1.heightfeet.value = (Math.floor(statsList[2]/12));
		document.form1.heightinches.value = ((statsList[2]*1) - ((Math.floor(statsList[2]/12))*12));
		document.form1.weight.value = statsList[3];
		document.form1.activity.selectedIndex = statsList[4];
	}
}

function noSubmit() {
	if (window.event.keyCode == 13) {
		getFoods();
		//return !(window.event && window.event.keyCode == 13);
	}
	else {
		return !(window.event && window.event.keyCode == 13);
	}
}

function getFoods() {
	setData();
	document.form.target = "";
	document.form.action = "nutrition.asp";
	document.form.submit();
	// return false;
}

function setData() {
	document.form.thebuggy.value = selIDs;
	document.form.portions.value = servIngs;
	document.form.stats.value = stats;
	document.form.otherday.value = altDay;
	document.form.mode.value = theMode.join("|");
	document.form.catsel.value = document.form.category.selectedIndex;
}

function setDisplay() {
	var scrpos = 0;
	if (dtype == 0) {
		dtype = 1;
		scrpos = 1000;
	}
	else {
		dtype = 0;
	}
	var dStatus = (dtype == 0) ? 'none':''; 
	if (document.layers) {
	document.layers['pinfo'].display = dStatus;
	}
	else {
	document.getElementById('pinfo').style.display = dStatus;
	}
	window.scrollTo(0,scrpos);
}

function setpInfo(issex,val) {
	if (issex == 1) {
		sex = val;
	}
	else {
		stats = "";
		stats += sex + "|";
		stats += document.form1.age.value + "|";
		var htinch = (((document.form1.heightfeet.value)*12) + ((document.form1.heightinches.value)*1));
		if (htinch == 0) htinch = "";
		stats += htinch + "|";
		stats += document.form1.weight.value + "|";
		stats += document.form1.activity.selectedIndex;
		var num=/(^\d+$)|(^\d+\.\d+$)/
		var paramList = ["gender","age","height","weight"];
		var statsList = stats.split("|");
		var goodStats = 1;
		for(v=1;v<(statsList.length-1);v++) {
			if (statsList[v] == "" || !num.test(statsList[v])) {
				var goodStats = 0;
				alert("Please enter a valid value for your " + paramList[v] + "...");
				break;
			}
		}
		if (goodStats == 1) {
			document.form.stats.value = stats;
			setDisplay();
		}
		else {
			stats = "";
		}
	}
}

function getTDEE() {
	 var statsList = stats.split("|");
	 sex = statsList[0];
	 var age = statsList[1];
	 var height = statsList[2]*2.54;
	 var weight = statsList[3]/2.2;
	 var activity = document.form1.activity.options[statsList[4]].value
	 var wtval = 66 + (13.7 * weight);
	 var htval = 5 * height;
	 var ageval = 6.8 * age;
	 var sqmtrs = ((height/100) * (height/100));
	 if (sex == 1) { /*female*/
	 	wtval = 655 + (9.6 * weight);
	 	htval = 1.8 * height;
	 	ageval = 4.7 * age;
	 }
	rci = Math.round((wtval + htval - ageval) * activity);
	bmi = Math.round(weight/sqmtrs);
}
