/* =============================================================================================================
 * FileName:     CBORD_NN.js
 * Created On:   04/11/2006
 * $Revision: #2 $
 * $Date: 2008/01/18 $
 * See report for revision history.
 * =============================================================================================================
 */// NetNutrition navigation and client-side functions

// make this element visible; hide the previous element
// newElementID: the actual element to be made visible
// oldElementIdID: the id of the hidden input element that holds the ID of the element to be made hidden
function swapElements(newElementID, oldElementIdID)
{
	var newElem = document.getElementById(newElementID);
	var oldElemIdID = document.getElementById(oldElementIdID);
	if (oldElemIdID != null && oldElemIdID.value.length > 0)
	{
		var oldElem = document.getElementById(oldElemIdID.value);
		if (oldElem != null)
		{
			oldElem.style.visibility = "hidden";
		}
	}
	if (newElem != null)
	{
		newElem.style.visibility = "visible";
		if (oldElemIdID != null)
		{
			oldElemIdID.value = newElementID;
		}
	}
}

// positioning percent consumed drop-downs
// enabling/disabling buttons based on checks
function buttonState(cbElem, selectID)
{
	var form = GetCBORDForm();
	var detail1 = form.elements['detail1'];
	var add1 = form.elements['add1'];
	var detail2 = form.elements['detail2'];
	var add2 = form.elements['add2'];
	var mealSummary = form.elements['mealSummary'];
	var mealDetail = form.elements['mealDetail'];
	var selectMenuElement=form.elements['pcm'+selectID];
	var selectSummaryElement=form.elements['pcs'+selectID];
	var cbmElem = form.elements['cbm'+selectID];
	var cbsElem = form.elements['cbs'+selectID];
	var controlType = cbElem.name.substr(0,3);
	if (controlType=='cbs')
	{
		if (cbsElem.checked == true)
		{
			checkMyMealCount++;
			selectSummaryElement.disabled = false;
		}
		else
		{
			checkMyMealCount--;
			selectSummaryElement.disabled = true;
		}
	}
	else
	{
		if (cbmElem.checked == true)
		{
			checkCount++;
			selectMenuElement.disabled = false;
		}
		else
		{
			checkCount--;
			selectMenuElement.disabled = true;
		}
	}
	if (detail1 != null)
	{
		detail1.disabled=(checkCount<=0);
	}
	if (detail2 != null)
	{
		detail2.disabled=(checkCount<=0);
	}
	if (add1 != null)
	{
		add1.disabled=(checkCount<=0);
	}
	if (add2 != null)
	{
		add2.disabled=(checkCount<=0);
	}
	if (mealSummary != null)
	{
		mealSummary.disabled=(checkMyMealCount<=0);
	}
	if (mealDetail != null)
	{
		mealDetail.disabled=(checkMyMealCount<=0);
	}
}
// NetNutrition navigation away from the item selection page for synchronization of checkboxes and amounts.
// generates list checked items and amounts for argument passing on command line
// only send list if changes to checkboxes by user.
function gotoNNtopicWithSync(topicID)
{
	// Be aware that this was broken out into multiple += to deal with an odd bug in Netscape 4.7x handling concatenation during parsing
	var form = GetCBORDForm();
	var isDirty = false;
	// generate list of check items
	var checkedItems = '';
	var amounts = '';
	// generate list of checked items for the 'my meal list'
	var myMealItems = '';
	var myMealAmounts = '';
	// generate list of checked items for the traits checkboxes
	var traits = '';
	
	for (i=0; i<form.length; i++)
	{
		var elem = form.elements[i];
		var elemPrefix = elem.name.substr(0,3);
		var elemSuffix = elem.name.substr(3);
		var selector = null;
		if (elem.type == 'radio')
		{
			if (elem.checked)
			{
				topicID = topicID+"&goal="+elem.value;
			}
		}
		if(elem.type == 'checkbox')
		{
			if((elem.defaultChecked != elem.checked) || (elem.checked && topicID.search('update=') != -1 && elemPrefix !='cbs') || (elem.checked && elemPrefix == 'cbm' && topicID.search('update=') == -1) || (elemPrefix == 'cbs' && topicID.search('update=') != -1 && checkMyMealCount == -1) )
			{
				isDirty=true;
				var isCheckedStatus = elem.checked ? '+' : '-';

				// this prefix is for Trait (Allergen/DietRestriction) checkboxes
				if (elemPrefix == 'trt')
				{
					if (traits.length > 0)
					{
						traits += ',';
					}
					traits += isCheckedStatus;
					traits += elemSuffix;
				}
				
				// this prefix is for "My Meal" checkboxes
				if (elemPrefix == 'cbs')
				{
					selector = form.elements['pcs'+elemSuffix];
					var val = selector.options[selector.selectedIndex].value;
					if (checkMyMealCount == -1)
					{
						isCheckedStatus = '-';
					}
					myMealItems = AddOptionToNNList(myMealItems, isCheckedStatus, elemSuffix);
					myMealAmounts = AddAmountToNNList(myMealAmounts, val);
				}
				// this prefix is for menu detail selector checkboxes
				if (elemPrefix == 'cbm')
				{
					selector = form.elements['pcm'+elemSuffix];
					var val = selector.options[selector.selectedIndex].value;
					checkedItems = AddOptionToNNList(checkedItems, isCheckedStatus, elemSuffix);					amounts = AddAmountToNNList(amounts, val);				}
			}
		}
		if (elem.type=='select-one')
		{
			for (j=0; j<elem.length; j++)
			{
				var myOption=elem.options[j];
				if(myOption.defaultSelected != myOption.selected)
				{
					// this option list is for the My Meal menu builder
					if (elemPrefix == 'pcs')
					{
						var alreadyExists = OptionExistsInNNList(myMealItems, elemSuffix);
						if (alreadyExists == false)
						{
							selector = form.elements['pcs'+elemSuffix];
							var val = selector.options[selector.selectedIndex].value;
							myMealItems = AddOptionToNNList(myMealItems, '+', elemSuffix);
							myMealAmounts = AddAmountToNNList(myMealAmounts, val);
							isDirty=true;
						}
					}
					// this option list is for the menu detail selector
					else if (elemPrefix == 'pcm')
					{
						var alreadyExists = OptionExistsInNNList(checkedItems, elemSuffix);
						if (alreadyExists == false)
						{
							selector = form.elements['pcm'+elemSuffix];
							var val = selector.options[selector.selectedIndex].value;
							checkedItems = AddOptionToNNList(checkedItems, '+', elemSuffix);							amounts = AddAmountToNNList(amounts, val);							isDirty=true;
						}
					}
				} // selector option != default
			} // for each option in the select
		} // end select
	}
	
	if (isDirty)
	{
		// Be aware that this was broken out into multiple += to deal with an odd bug in Netscape 4.7x handling concatenation during parsing
		var destination=pageName+'?topicID='+topicID;		destination+="&";		destination+='oidList=';		destination+=checkedItems;		destination+="&";		destination+='pcList='+amounts;		destination+="&";		destination+='myMealOidList=';		destination+=myMealItems;		destination+="&";		destination+='myMealPcList='+myMealAmounts;		destination+="&";		destination+='traits=';		destination+=traits;		processRedirect(destination);
	}
	else
	{
		// no change, so it is important that the oidList be missing
		gotoTopic(topicID);
	}
}

// add an entry to the end of NN checkbox status list
function AddOptionToNNList(curList, isCheckedPrefix, elemSuffix)
{
	if (curList.length > 0)
	{
		curList += ',';
	}
	curList += isCheckedPrefix;
	curList += elemSuffix;
	return curList;
}

// add an amount to the end of an NN percent consumed list
function AddAmountToNNList(curList, selectedAmount)
{
	if (curList.length > 0)
	{
		curList += ',';
	}
	curList += selectedAmount;
	return curList;
}

// return true or false depeding on whether the current option value is already in the current list
// the elemSuffix is really the OID
function OptionExistsInNNList(curList, elemSuffix)
{
	// Be aware that this was broken out into multiple += to deal with an odd bug in Netscape 4.7x handling concatenation during parsing
	var alreadyExists = false;
	var checkval = '+';
	checkval += elemSuffix;		// only care about entries that are 'checked'
	var checkExists = curList.indexOf(checkval);
	if (checkExists != -1) 
	{
		if (curList.length == checkval.length)
		{
			alreadyExists=true;
		}
		if (curList.indexOf(",") != -1)
		{
			if (curList.indexOf(checkval+",") != -1) // double check to avoid false positives
			{
				alreadyExists=true;
			}						
		}
	}
	return alreadyExists;
}

// NetNutrition Administration Validation
function confirmNNParams(id)
{
	var form = GetCBORDForm();
	if (id == "StartDay")
	{
		var startDay = form.elements['StartDay'];
		var dateIsOk = false;
		if (startDay != null)
		{
			var startDayValue = startDay.value;
			if (startDayValue == 'today')
			{
				dateIsOk = true;
			}
			else if (startDayValue.substring(0,5) == "date:")
			{
				try
				{
					var year = parseInt(startDayValue.substr(5,4),10);
					var month = parseInt(startDayValue.substr(10,2),10);
					var day = parseInt(startDayValue.substr(14,2),10);
					var startDate = new Date (year, month, day);
					if (! isNaN(startDate))
					{
						dateIsOk = true;
					}
				}
				catch(e)
				{
					dateIsOk = false;
				}
			}
			else
			{
				try
				{
					var dTD = new Number (startDayValue);
					if (dTD>0 && dTD< 8)
					{
						dateIsOk = true;
					}
				}
				catch(e)
				{
					dateIsOk = false;
				}
			}
		}
		if (dateIsOk == false)
		{
			alert ("Valid values are either 'date:YYYY-MM-DD, 'today', or 1-7 where 1=the first day of the week.\n Please re-enter.");
			startDay.value = startDay.defaultValue;
			startDay.focus();
		}
	}
	else if (id == "DaysToDisplay")
	{
		var daysIsOk = false;
		var daysToDisplay = form.elements['DaysToDisplay'];
		try
		{
			var dTD = new Number (daysToDisplay.value);
			if (dTD>0 && dTD< 15)
			{
				daysIsOk = true;
			}
		}
		catch(e)
		{
			daysIsOk = false;
		}
		if (daysIsOk == false)
		{
			alert ("Valid values are 1-14. Please re-enter.");
			daysToDisplay.value = daysToDisplay.defaultValue;
			daysToDisplay.focus();
		}
	}
}
