function doPopSwap() {
	showBlackout();
	showBlankNewOpenSaveLayer();
	var url = 'popSwap.php?swapDay=4&swapCourse=8)'; //"?m=meals&a=mealplanner&taction=createAMP_displayBox&alreadyHasMP="+alreadyHasMP+"&gotoMP="+gotoMealPlanPage+"rc="+randChars();
	var xmlHttp;
	  try
	    {
	    // Firefox, Opera 8.0+, Safari
	    xmlHttp=new XMLHttpRequest();
	    }
	  catch (e)
	    {
	    // Internet Explorer
	    try
	      {
	      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	      }
	    catch (e)
	      {
	      try
	        {
	        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	        }
	      catch (e)
	        {
	        alert("Your browser does not support AJAX!");
	        return false;
	        }
	      }
	    }
	    xmlHttp.onreadystatechange=function()
	      {
	      if(xmlHttp.readyState==4)
	        {
	        	//alert('update openSaveOptionsLayer');
			document.getElementById('openSaveOptionsLayer').innerHTML=xmlHttp.responseText;
	        }
	      }
	     // alert(scriptWithParams);
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);

}

function showPopSwap( results ) {
	alert( 'showPopSwap' );
	//showBlackout();
	//showBlankNewOpenSaveLayer();
	document.getElementById('openSaveOptionsLayer').innerHTML = results;
	//javascript:popUp('popSwap.php?swapDay=4&swapCourse=8')
}

function doAjaxRecipeSearch() {
  	//alert('running doAjaxRecipeSearch');
  	//return;
  	var script ="./modules/meals/doRecipeSearch.php";
	var scriptWithParams = script + '?search_term=' + document.getElementById('mp_search').value;
	var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
			//window.frames['search_results_iframe'].document.getElementById('search_results').innerHTML=xmlHttp.responseText;
			document.getElementById('search_results').innerHTML=xmlHttp.responseText;
        }
      }
    xmlHttp.open("GET",scriptWithParams,true);
    xmlHttp.send(null);
  }

function MP_showHiddenRecipe(id)
{
	var w = document.getElementById('mp_recipe_popup');
	iframe = document.getElementById('recipe_popup')
	if (w != null)
	{	
		w.style.display = 'block';
		w.style.visibility = 'visible';
		
		iframe.src = "?m=items&a=recipe_add_edit_view&mode=view&print=1&recipe_edit=" + id;//"./modules/meals/showRecipePopup.php?id=" + id;
	}
}

function mpNav(show_week) {
	selected_date = document.mp_nav_form.selected_date.value;
	document.location = './index.php?m=meals&a=mealplanner&show_week='+show_week+'&selected_date='+selected_date;
}
function dateString() {
	var today = new Date();
	return today.getTime();
}
var lastDay = null;
var lastDayColor = null;
function getSelectedDate() {
    return document.mp_nav_form.selected_date.value;
}
function updateDayNames(day) {
    if (day == null || day == -1) { return ''; }
    
    var dayNames = new Array();
    dayNames[0] = 'Monday';
    dayNames[1] = 'Tuesday';
    dayNames[2] = 'Wednesday';
    dayNames[3] = 'Thursday';
    dayNames[4] = 'Friday';
    dayNames[5] = 'Saturday';
    dayNames[6] = 'Sunday';
    
    var dayName = dayNames[day];
    if (dayName == null || dayName == undefined) { dayName = 'Day'; }
    
    var obj = null;
    for(var i=1; i < 50; i++) {
        obj = document.getElementById('dayName'+i);
        if (obj) {
            obj.innerHTML = dayName;
        } else {
            break;
        }
    }
    
    return;
}
function doAjaxShowSelectedDay(selected_date) {
  	if (selected_date > -1) {
  		  	var theHeight = getHeight('mp_selected_day');
			  	//alert(theHeight);
			  	if (parseInt(theHeight) > 0) {} else { theHeight = 400; /*default*/ }

  		  document.getElementById('mp_selected_day').innerHTML= "<div align=center valign=top style=\"height:"+parseInt(theHeight)+"px;color:#999999;\"><b><big><br>Loading... </big></b></div>";
  		  
		  	var btn = null;
		  	
		  	//This works by setting the backgroundColor to null;
		  	//thats right, lastDayColor doesn't even store a color... it just reverts back to the class color
		  	if (lastDay) {
		  	    //alert('setting ' + lastDay + ' to ' + lastDayColor);
		  		btn = document.getElementById('mp_day_' + lastDay);
		  		btn.style.backgroundColor = lastDayColor;
		  	}
		  	
		  	btn = document.getElementById('mp_day_' + selected_date);
		  	
		  	lastDay = selected_date;
		  	lastDayColor = btn.style.backgroundColor;
		    //if (0 && parseInt(document.mp_nav_form.today.value) == selected_date) {btn.style.backgroundColor = '#99cc66';} else {}
    		btn.style.backgroundColor = '#FE9D32';
    		
    		document.mp_nav_form.selected_date.value = selected_date;
    }
    
    
 	updateDayNames(selected_date);
    	
  	planId = document.mp_nav_form.planId.value;
  	
  //alert('selected_date='+selected_date);
  	// first close recipe_popup
  	MP_hideHiddenRecipe();
  	var script ="?m=meals&a=doShowSelectedDay";
	var scriptWithParams = script + '&selected_date=' + selected_date + '&t=' + dateString() + '&planId='+planId;
	
	var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
			document.getElementById('mp_selected_day').innerHTML=xmlHttp.responseText;
        }
      }
     // alert(scriptWithParams);
    xmlHttp.open("GET",scriptWithParams,true);
    xmlHttp.send(null);
  }


function MP_hideHiddenRecipe()
{
	var w = document.getElementById('mp_recipe_popup');
	if (w != null)
	{
		w.style.display = 'none';
		w.style.visibility = 'hidden';
	}
}

function swapBev(course, day, bevId) {
	var url = "?m=meals&a=mealplanner&taction=selectMeal&isBeverage=1&day="+day+"&course="+course+"&mealId="+bevId+"&aj=t&rc="+randChars();
	//alert(url);
	callAjax(url, false, null, 'showBevSave', null);
}

function showBevSave(results) {
	results = unescape(results);
	//alert(results);
	var saveBevLayer = document.getElementById('bevSave');
	if (getMWAJTagContents(results, 'status') == 'saved') {
		saveBevLayer.innerHTML = getMWAJTagContents(results, 'message');
		doAjaxShowSelectedDay(getMWAJTagContents(results, 'day'));
	} else {
		saveBevLayer.innerHTML = '<B>Failed!</B>';
	}
	
	//saveBevLayer.style.left = 300;
	//saveBevLayer.style.top = 300;
	
	//showOrHideById('bevSave', 'show');
	//setTimeout('showOrHideById("bevSave", "hide")', 5000);
}


//db=displayBox
function renamePlan_db(mpId, goBackTo) {
	var url = "?m=meals&a=mealplanner&taction=renamePlan_displayBox&planId="+mpId+"&goBackTo="+goBackTo+"&rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
}
function newPlan_db() {
	var url = "?m=meals&a=mealplanner&taction=newPlan_displayBox&rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
}
function openPlan_db(currentOpenPlanId) {
	var url = "?m=meals&a=mealplanner&taction=openPlan_displayBox&currentOpenPlanId="+currentOpenPlanId+"&rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
}
function savePlan_db() {
	var url = "?m=meals&a=mealplanner&taction=savePlan_displayBox&rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
}


function renamePlan(mpId, newTitle, goBackTo) {
	var url = "?m=meals&a=mealplanner&taction=renamePlan&rc="+randChars()+"&planId="+mpId+"&goBackTo="+goBackTo+"&newTitle="+escape(newTitle);
	callAjax(url, false, null, 'newOpenSave', null);
}

function setActiveMealPlan(mpId) {
	var url = "?m=meals&a=mealplanner&taction=setActivePlan&rc="+randChars()+"&planId="+mpId;
	callAjax(url, false, null, 'newOpenSave', null);
}

function newPlan(saveTitle) {
	var urlExtra = '';
	var url = "?m=meals&a=mealplanner&taction=newPlan&rc="+randChars()+"&title="+escape(saveTitle);
	callAjax(url, false, null, 'openPlanAjax', null);
}
function openPlanAjax(results) {
	results = unescape(results);
	var mealPlanId = getAjaxReturnTagValue(results, 'mealPlanId');
	var action = getAjaxReturnTagValue(results, 'action');
	if (action == 'openPlan') {
		openPlan(mealPlanId);
	}
}
function openPlan(planId) { //refers to action of Open link
	document.openPlanForm.planId.value = planId;
	document.openPlanForm.submit();
}
function openPlan_setActive(planId) {
	var url = "?m=meals&a=mealplanner&taction=setActivePlan&planId="+mpId+"&rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
}

function deletePlan(planId, goBackTo) {
	if (confirm('This Meal Plan will be permanently deleted. Are you sure you want to delete this Meal Plan?')) {
		var url = "?m=meals&a=mealplanner&taction=deletePlan&planId="+planId+"&goBackTo="+goBackTo+"&rc="+randChars();
		callAjax(url, false, null, 'newOpenSave', null);
	}
}
function copyPlan(planId) {
	if (confirm('Are you sure you want to make a copy of this Meal Plan?')) {
		var url = "?m=meals&a=mealplanner&taction=copyPlan&planId="+planId+"&rc="+randChars();
		callAjax(url, false, null, 'newOpenSave', null);
	}
}
function savePlan(planId, saveTitle) {
	var urlExtra = '&doSave='+doSave+'&title='+escape(saveTitle);
	var url = "?m=meals&a=mealplanner&taction=savePlan&planId="+mpId+"&rc="+randChars()+urlExtra;
	callAjax(url, false, null, 'newOpenSave', null);
}

//displays options for all 3 functions above
function newOpenSave(results) {
	results = unescape(results);
	//alert(results);
	
	var action     = getAjaxReturnTagValue(results, 'action');
	if (action) {
			if (action == 'waiting') {
					var estimatedTime = parseInt(getAjaxReturnTagValue(results, 'estimatedTime'));
					var displayBox = getAjaxReturnTagValue(results, 'displayBox');
					//alert('amp status: ' + estimatedTime + '\n\n' + results);			
					if (estimatedTime > 0) {
							//document.getElementById('openSaveOptionsLayer').innerHTML = displayBox;
							setTimeout("createAMPInProgress()", 3000);
					} else if (estimatedTime < 0) {
							doAjaxShowSelectedDay(document.mp_nav_form.selected_date.value);
							showOrHideById("newMealPlanHasBeenCreated", "show");
							//is finished
							//document.ampForm.taction.value = 'doAlertNewMp';
							//document.ampForm.submit();
					} else {
							//document.getElementById('openSaveOptionsLayer').innerHTML = results;
					}
			} else if (action == 'openPlan') {
					var mealPlanId = getAjaxReturnTagValue(results, 'mealPlanId');
					openPlan(mealPlanId);
			}
	} else {
		//alert(results);
		document.getElementById('openSaveOptionsLayer').innerHTML = "<form name=nosForm>"+results+"</form>";
		showOrHideById('openSaveOptionsLayer', 'show');
		centerLayerInWindow('openSaveOptionsLayer');
	}
}
function showBlankNewOpenSaveLayer() {
		document.getElementById('openSaveOptionsLayer').innerHTML = "&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>";
		showOrHideById('openSaveOptionsLayer', 'show');
		centerLayerInWindow('openSaveOptionsLayer');
}

function closeNOSOptionsBox() {
	showOrHideById('openSaveOptionsLayer', 'hide');
	hideBlackout();
}

function createAMP(alreadyHasMP, gotoMealPlanPage) {
    if (!gotoMealPlanPage || gotoMealPlanPage == null) { gotoMealPlanPage = 0; }
	if (alreadyHasMP == null) { alreadyHasMP = 0; }
	showBlackout();
	showBlankNewOpenSaveLayer();
	var url = "?m=meals&a=mealplanner&taction=createAMP_displayBox&alreadyHasMP="+alreadyHasMP+"&gotoMP="+gotoMealPlanPage+"rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
}

function createAMPInProgress() {
	var url = "?m=meals&a=mealplanner&taction=createAMPInProgress_displayBox&rc="+randChars();
	callAjax(url, false, null, 'newOpenSave', null);
	
	doAjaxShowSelectedDay(-1);
}
function createAMPNow(alreadyHasMP, gotoMealPlanPage) {
	//alert('submitting');
	//document.autoplan.createAutoSmartPlan.value=2;
	//document.autoplan.submit();

    if (!gotoMealPlanPage) {
    	showOrHideById("newMealPlanHasBeenCreated", "hide");
    	
    	doAjaxShowSelectedDay(-1);
    }
    
	closeNOSOptionsBox();
	var url = "?m=meals&a=mealplanner&taction=createAMP_startJob&gotoMP="+gotoMealPlanPage+"&alreadyHasMP="+alreadyHasMP+"&rc="+randChars();
	if (parseInt(gotoMealPlanPage) == 1) {
	    callAjax(url, false, null, 'createAMP_doNext', null); //'newOpenSave'
	} else {
	    callAjax(url, false, null, 'newOpenSave', null); //'newOpenSave'
    }
	
	
	//setTimeout("createAMPInProgress()",1000);
	
	
	/*
		for prefill:
		document.autoplan.ampMpPrefillId.value=0;
		this.form.ampMpPrefillId[this.form.ampMpPrefillId.selectedIndex].value;
	*/
}
function createAMP_doNext(results) {
	results = unescape(results);
	var doAutoGo = getAjaxReturnTagValue(results, 'doAutoGo');
	//alert('HERE: ' + doAutoGo);
	
	if (parseInt(doAutoGo) == 1 && document.gotoMealPlanPage) {
	    
	    document.gotoMealPlanPage.submit();
	}
	//if (getMWAJTagContents(results, 'status') == 'saved') {
}


