/*------------------------------------------------------------------
!!	MyGymCoach.com / GymToolz.com Websites
!!	Copyright Structured Fitness Solutions, Inc.
!!	By Michael Burford 
!!----------------------------------------------------------------*/

var DEBUG = true;

function D(xxx) {
   //if (DEBUG) return xxx;
   return "";
}

function doOnLoaded() {
  return true;
} 

function doOnError() {
   return true;
}
//window.onerror = doOnError;

//Javascript queries
function createHttpRequester() { 
   var req; 
   if (window.XMLHttpRequest){ 
      // Firefox, Safari, Opera... 
      req = new XMLHttpRequest(); 
   } else if (window.ActiveXObject) { 
      // Internet Explorer 5+ 
      req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
      alert("Error creating XMLHttpRequest"); 
   } 
   return req;  
} 
var http = createHttpRequester(); 
var http2 = createHttpRequester(); 

var WORKINGANIMATION = "<center><br><img src='/images/wait1.gif' height=32 width=32><br><br></center>";
var SMALLWORKINGANIMATION = "<center><img src='/images/loading-bar1.gif' height=19 width=220></center>";
var SMALLWORKINGANIMATIONINLINE = "<img src='/images/wait1.gif' height=16 width=16 border=0 align='absmiddle'>";
//var WORKINGANIMATION2 = "<center><br><br><br><img src='/images/wait-bar3.gif' width=440 height=38><br><br></center>";
var WORKINGANIMATION2 = "&nbsp;";//<img src='/images/wait-bar5.gif' class='workingbaroverlay'>"; 

var BackStack = new Array(); 

//---------------------------------------------------------
function addslashes(str) {
    str = str.replace(/\'/g, '\\\'');
    str = str.replace(/\r/g, '\\r');
    str = str.replace(/\n/g, '\\n');
    str = str.replace('#', '%23');    //# messes up Firefox (IE, strangely, is OK with it.)
    str = str.replace('+', '%2B');    //A real +, not plus-to-space as in URLs
    //str = str.replace('&amp;', '+'); 
    //str = str.replace('&', '+'); 
    //str = str.replace(/#/g, '%23');     //# messes up Firefox (IE, strangely, is OK with it.)
    str = str.replace(/\+/g, '%2B');    //A real +, not plus-to-space as in URLs
    str = str.replace(/&amp;/g, "%26"); 
    str = str.replace(/&/g, "%26"); 
    //str = str.replace('\r', '\\r');
    //str = str.replace('\n', '\\n');
    //str = str.replace(/\"/g, '\\"');
    //str = str.replace(/\\/g, '\\\\');
    //str = str.replace(/\0/g, '\\0');
    return str;
}
function endsWith(str, ends) {
    var elen = ends.length;
    var slen = str.length;
    return (ends==str.substring(slen-elen));
}

function initialCaps(objID) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	
	frmObj = document.getElementById(objID);
	if (!frmObj) return;
	
	tmpStr = frmObj.value.toLowerCase();
	strLen = tmpStr.length;
	if (strLen>0) {
		for (index=0; index<strLen; index++) {
			if (index==0) {
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			} else {
				tmpChar = tmpStr.substring(index, index+1);
				if ((tmpChar==" " || tmpChar=="(" || tmpChar=="," || tmpChar==")" || tmpChar=="/") && index<(strLen-1)) {
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
                }
            }
        }
	}
	frmObj.value = tmpStr;
   	return false;
}


//---------------------------------------------------------
//For calendar-month.php
var jsd=0, jsm=0, jsy=0, jsuser="", jsmealid=0, jsrecipeid=0;
var jsreturnpage="";
function refreshCalendar(thepage, m, y, thisd, thism, thisy, bigcalendar, viewuserid, extra) {
    jsm = m;
	jsy = y;
	var ran_no=(Math.round((Math.random()*9999))); 
	http2.open('get', '/calendar-month.php?m='+m+'&y='+y+'&ran='+ran_no+'&viewuserid='+viewuserid+'&bigcalendar='+bigcalendar+'&refresh=1'+extra);
   	http2.onreadystatechange = function() {
		if (http2.readyState == 4 && http2.status == 200) { 
      		var response = http2.responseText;
      		if (response) { 
				document.getElementById("CalendarMonth").innerHTML = response; 
      		} 
   		} 
	} 
   	http2.send(null); 
   	return false;
}
function displayCalendar(thepage, m, y, thisd, thism, thisy, bigcalendar, viewuserid, extra) {
    jsm = m;
	jsy = y;
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/calendar-month.php?thepage='+thepage+'&m='+m+'&y='+y+'&ran='+ran_no+'&viewuserid='+viewuserid+'&bigcalendar='+bigcalendar+extra);
   	http.onreadystatechange = function() {
		if (http.readyState == 4 && http.status == 200) { 
      		var response = http.responseText;
      		if(response) { 
				document.getElementById("CalendarMonth").innerHTML = http.responseText; 
				if (document.getElementById("CalendarDay")) document.getElementById("CalendarDay").innerHTML = "";
            	if (document.getElementById("AddEdit")) document.getElementById("AddEdit").innerHTML = "";
				if (jsm==thism && jsy==thisy) displayDayItems(thepage, thisd, thism, thisy, viewuserid);
      		} 
   		} 
	} 
   	http.send(null); 
   	return false;
}

var lastselday = null;
var lastseldayclass = "";
function displayDayItems(thepage, d, m, y, viewuserid) {	
    displayDayItems2(thepage, d, m, y, 'calories', viewuserid);
   	return false;
}
function displayDayItems2(thepage, d, m, y, detailsfor, viewuserid) {	
	if (d=="") return false;	
	var elem = document.getElementById("day_"+d+"_"+m+"_"+y);
	if (!elem) return false;
	
	if (document.getElementById("AddEdit")) {   //!!Extra check prevents looping before page fully loaded!
	    if (!document.getElementById("CalendarDay") && !document.getElementById("TrainerSchedule")) {
	        window.location.href = '/user-main.php?d='+d+'&m='+m+'&y='+y+'&ran='+ran_no+'&viewuserid='+viewuserid+'&detailsfor='+detailsfor;
	        return false;
	    }
	}
	
	if (thepage=="/scheduler.php" || thepage=="/scheduler-embed.php") {
	    ajaxpage = "/schedule-day.php";
	} else if (thepage=="/calendars.php") {
	    ajaxpage = "/calendars-data.php";
	} else {
	    ajaxpage = "/calendar-day.php";
	}

    jsd = d;
    jsm = m;
    jsy = y;
    jsuser = viewuserid;
	
	if (document.getElementById("CalendarDay")) document.getElementById("CalendarDay").innerHTML = WORKINGANIMATION;
	if (document.getElementById("TrainerSchedule")) document.getElementById("TrainerSchedule").innerHTML = WORKINGANIMATION;
    if (document.getElementById("AddEdit")) document.getElementById("AddEdit").innerHTML = "";
		
	if (lastselday!=null) lastselday.className = lastseldayClassName;
	lastseldayClassName = elem.className;
	//Today can already be set right...
	if (lastseldayClassName=="seltoday") {
	    lastseldayClassName = "today";
	} else if (lastseldayClassName=="selfulltoday") {
	    lastseldayClassName = "fulltoday";
	} else {
	    elem.className = "sel" + elem.className; 
	}
    //elem.className = "sel" + elem.className; 
    lastselday = elem;
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', ajaxpage+'?d='+d+'&m='+m+'&y='+y+'&ran='+ran_no+'&viewuserid='+viewuserid+'&detailsfor='+detailsfor);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	
   	return false;
   	
	//http2.open('get', '/food-label.php?d='+d+'&m='+m+'&y='+y+'&ran='+ran_no+'&label=day&viewuserid='+viewuserid);
   	//http2.onreadystatechange = ResultToDailyFoodLabelPanel;
   	//http2.send(null);    	
}
function RefreshFoodLabel() {
	var ran_no=(Math.round((Math.random()*9999))); 
	http2.open('get', '/food-label.php?d='+jsd+'&m='+jsm+'&y='+jsy+'&ran='+ran_no+'&label=day' + (jsuser.length>0 ? '&viewuserid='+jsuser : ''));
   	http2.onreadystatechange = ResultToDailyFoodLabelPanel;
   	http2.send(null);    	
}
function RefreshFoodLabelMeal() {
	var ran_no=(Math.round((Math.random()*9999))); 
	http2.open('get', '/food-label.php?mealid='+jsmealid+'&label=meal' + (jsuser.length>0 ? '&viewuserid='+jsuser : ''));
   	http2.onreadystatechange = ResultToDailyFoodLabelPanel;
   	http2.send(null);    	
}
function RefreshFoodLabelRecipe() {
	var ran_no=(Math.round((Math.random()*9999))); 
	http2.open('get', '/food-label.php?recipeid='+jsrecipeid+'&label=recipe' + (jsuser.length>0 ? '&viewuserid='+jsuser : ''));
   	http2.onreadystatechange = ResultToDailyFoodLabelPanel;
   	http2.send(null);    	
}
//-------------------------------------------------------------------
//Scrolling
var jsXPos = -1;
function OnWinScroll() {
    jsXPos = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
}
window.onscroll = OnWinScroll;

//-------------------------------------------------------------------
//Both immediately, and on a short timer, it wasn't working on ajax innerHTML things in IE.
function DoFocus() {
    DoFocus2();
	setTimeout("DoFocus2();", 500);
}
function DoFocus2() {
	//Set focus to something...
	if (document.getElementById("subject")) document.getElementById("subject").focus();
	else if (document.getElementById("text")) document.getElementById("text").focus();
	else if (document.getElementById("quantity")) document.getElementById("quantity").focus();
	else if (document.getElementById("string")) document.getElementById("string").focus();
}

//-------------------------------------------------------------------
//Results to various DIV panels in pages.
function ResultToAddEditPanel() {
	if (http.readyState == 4 && http.status == 200) { 
  		var response = http.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else if (response=="Close") {
  		        CloseAddEditPanel();
  		        window.location.reload(true);
  		    } else {
			    document.getElementById("AddEdit").innerHTML = response; 
    			DoFocus();
  		        window.location.href = "#top"+D("-a");
			}
  		} 
	} 
} 
//-------------------------------------------------------------------
function ResultToMessagePanel() {
	if (http.readyState == 4 && http.status == 200) { 
  		var response = http.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else {
			    if (document.getElementById("ResultMessageHtml")) {
			        document.getElementById("ResultMessageHtml").innerHTML = http.responseText; 
			    }
			}
		    CloseAddEditPanel();
        }
    }
}
function ResultToMessagePanel2() {
	if (http.readyState == 4 && http.status == 200) { 
  		var response = http.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else {
			    if (document.getElementById("ResultMessageHtml2")) {
			        document.getElementById("ResultMessageHtml2").innerHTML = http.responseText; 
			    }
			}
        }
    }
}
function ResultToMainPanelQ() {
    ResultToMainPanel();
}
function ResultToMainPanel() {
	if (http.readyState == 4 && http.status == 200) { 
  		var response = http.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else {
			    if (document.getElementById("CalendarDay")) {
			        document.getElementById("CalendarDay").innerHTML = http.responseText; 
    			    RefreshFoodLabel();
    			    if (document.getElementById("GraphImage")) {
                    	var ran_no=(Math.round((Math.random()*9999))); 
    			        document.getElementById("GraphImage").src = "/graph-meals.php?small&rand="+ran_no+(jsuser.length>0 ? '&viewuserid='+jsuser : ''); 
    			    }
			    } else if (document.getElementById("MealBuilder")) {
			        document.getElementById("MealBuilder").innerHTML = http.responseText; 
			        RefreshFoodLabelMeal();
			    } else if (document.getElementById("RecipeBuilder")) {
			        document.getElementById("RecipeBuilder").innerHTML = http.responseText; 
			        RefreshFoodLabelRecipe();
			    } else if (document.getElementById("TrainerSchedule")) {
			        document.getElementById("TrainerSchedule").innerHTML = http.responseText; 
			    }
			    
			    CloseAddEditPanel();
			}
  		} 
	} else if (http.readyState == 4 && http.status > 200) { 
	    if (DEBUG) alert("http error code "+http.status);
	} 
} 
//-------------------------------------------------------------------
function ShowWorkingMainPanel() {
    if (document.getElementById("CalendarDay")) {
        document.getElementById("CalendarDay").innerHTML = WORKINGANIMATION; 
    } else if (document.getElementById("MealBuilder")) {
        document.getElementById("MealBuilder").innerHTML = WORKINGANIMATION; 
    } else if (document.getElementById("RecipeBuilder")) {
        document.getElementById("RecipeBuilder").innerHTML = WORKINGANIMATION; 
    } else if (document.getElementById("TrainerSchedule")) {
        document.getElementById("TrainerSchedule").innerHTML = WORKINGANIMATION; 
    }
}
//-------------------------------------------------------------------
function ResultToFoodLabelPanel() {
	if (http.readyState == 4 && http.status == 200) { 
  		var response = http.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else if (document.getElementById("FoodLabel")) {
    			document.getElementById("FoodLabel").innerHTML = http.responseText; 
            }
  		} 
	} 
} 
//2 since timing issue, if http request for food label on Add wasn't finished before they OK,
//caused an "object in use" sort of error.  So food label for that is on http2 and handled here.
function ResultToFoodLabelPanel2() {
	if (http2.readyState == 4 && http2.status == 200) { 
  		var response = http2.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else if (document.getElementById("FoodLabel")) {
    			document.getElementById("FoodLabel").innerHTML = http2.responseText; 
            }
  		} 
	} 
} 

function ResultToDailyFoodLabelPanel() {
	if (http2.readyState == 4 && http2.status == 200) { 
  		var response = http2.responseText;
  		if (response) { 
  		    if (response=="Login") {
  		        window.location.href = "/login.php";
  		    } else if (document.getElementById("DailyFoodLabel")) {
    			document.getElementById("DailyFoodLabel").innerHTML = http2.responseText; 
    		}
  		} 
	} 
} 
//---------------------------------------------------------
//For calendar-day.php
/*function displayAddMeal(returnpage, day, mon, year, mealid, meal, viewuserid) {
	var ran_no=(Math.round((Math.random()*9999))); 
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	scrollTo(jsXPos,0);
		
    jsreturnpage = returnpage;
    if (returnpage=="/meal-builder-data.php") {
        jsmealid = mealid;
    	url = '/add-meal.php?search=pantry&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
    } else if (returnpage=="/recipe-builder-data.php") {
        jsrecipeid = mealid;
    	url = '/add-meal.php?search=pantry&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&recipeid='+mealid+'&viewuserid='+viewuserid;
    } else {
        jsmealid = mealid;
    	url = '/add-meal.php?search=pantry&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
    }
    
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null);   	
   	BackStack.push(url);
}*/
function displayThisInPanel(pageurl) {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top"+D("-b");
	//if (jsXPos>-1) window.scrollTo(jsXPos,1);

	http.open('get', pageurl);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null);   	
   	BackStack.push(pageurl);
   	return false;
}
/*function displayEditServing(returnpage, foodid, servingid, day, mon, year, mealid, meal, viewuserid) {
	var ran_no=(Math.round((Math.random()*9999))); 
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	scrollTo(jsXPos,0);
	
    jsreturnpage = returnpage;
    if (returnpage=="/meal-builder-data.php") {
        jsmealid = mealid;
    	url = '/add-meal.php?foodid='+foodid+'&servingid='+servingid+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
    } else if (returnpage=="/recipe-builder-data.php") {
        jsrecipeid = mealid;
    	url = '/add-meal.php?foodid='+foodid+'&servingid='+servingid+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&recipeid='+mealid+'&viewuserid='+viewuserid;
    } else {
        jsmealid = mealid;
    	url = '/add-meal.php?foodid='+foodid+'&servingid='+servingid+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
    }
    
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	BackStack.push(url);
   	return false;
}*/
function displaySaveMeal(day, mon, year, mealid, meal, viewuserid) {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top"+D("-c");
    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/save-meal.php?ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	return false;
}
/*function AddMealAddQuickMeal(mealid, day, mon, year, meal, viewuserid) {
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/calendar-day.php?operation=addmymeal&mealid='+mealid+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&viewuserid='+viewuserid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}*/
//---------------------------------------------------------
function FractionToDec(textIn) {
    text = textIn;
	if (text.indexOf("/")>0) {
		plus = 0;
		//They entered as a fraction!
		if (text.indexOf(" ")>0) {
			//Entered as a WHOLE and a fraction (1 1/2)
			work = text.split(" ");
			plus = parseInt(work[0]);
			text = work[1];
		}
		work = text.split("/");
		if (parseInt(work[1])==0) return textIn;
		return plus + (parseInt(work[0])/parseInt(work[1]));
	}
	return textIn;
}
//---------------------------------------------------------
//Used from either calendar-day or meal-builder
function AddMealEatFood(foodid, servingunit, detailid, quantity1, quantity2, day, mon, year, mealid, meal, addpantry, viewuserid) {
    if (quantity1=="") quantity = parseFloat(quantity2);
    else quantity = parseFloat(FractionToDec(quantity1)) + parseFloat(quantity2);
    
    if (quantity==0.0) return false;
    
    ShowWorkingOverlay();
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http = createHttpRequester(); 
	http.open('get', jsreturnpage+'?operation=add&foodid='+foodid+'&servingunit='+addslashes(servingunit)+'&detailid='+detailid+'&quantity='+quantity+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&addpantry='+addpantry+'&mealid='+mealid+'&viewuserid='+viewuserid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
function AddMealDeleteEntry(returnpage, servingid, day, mon, year, mealid, meal, viewuserid) {
    jsmealid = mealid;
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', returnpage+'?operation=delete&servingid='+servingid+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	
   	if (document.getElementById("imgSID"+servingid)) {
   	    document.getElementById("imgSID"+servingid).src = "/images/wait-small.gif";
   	}
   	
   	return false;
}
function AddMealEditEntry(servingid, servingunit, detailid, quantity1, quantity2, day, mon, year, mealid, meal, viewuserid) {
    if (quantity1=="") quantity = parseFloat(quantity2);
    else quantity = parseFloat(FractionToDec(quantity1)) + parseFloat(quantity2);
    
    ShowWorkingOverlay();
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', jsreturnpage+'?operation=edit&servingid='+servingid+'&detailid='+detailid+'&quantity='+quantity+'&servingunit='+addslashes(servingunit)+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
function AddMealCopyMeal(day, mon, year, meal, datecopy, mealcopy, viewuserid) {
    if (jsreturnpage.length==0) jsreturnpage = "/calendar-day.php";
    
    ShowWorkingOverlay();
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', jsreturnpage+'?operation=copy&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&datecopy='+datecopy+'&meal='+meal+'&mealcopy='+mealcopy+'&viewuserid='+viewuserid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
function ShowWorkingOverlay() {
    CloseAddEditPanel();
	if (document.getElementById("WorkingGray")) document.getElementById("WorkingGray").className = "workingoverlay";
	if (document.getElementById("WorkingHtml")) document.getElementById("WorkingHtml").innerHTML = WORKINGANIMATION2;
}
function ShowSmallWorkingOverlay() {
    CloseAddEditPanel();
	if (document.getElementById("ResultMessageHtml")) document.getElementById("ResultMessageHtml").innerHTML = SMALLWORKINGANIMATION;
}
//---------------------------------------------------------
//For meal-builder.php
function MealBuilderRename(returnpage, renameto, mealid) {
    if (returnpage=="/meal-builder-data.php") {
        jsmealid = mealid;
    } else if (returnpage=="/recipe-builder-data.php") {
        jsrecipeid = mealid;
    } else {
        jsmealid = mealid;
    }
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', returnpage+'?operation=rename&renameto='+addslashes(renameto)+'&ran='+ran_no+'&mealid='+mealid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
function displayCreateMeal() {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/save-meal.php?ran='+ran_no;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	return false;
}
function displayEditMeal(mealid) {
    jsmealid = mealid;
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/save-meal.php?mealid='+mealid+'&ran='+ran_no;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	return false;
}
function CreateMeal(mealname, meal) {
	mealname = mealname.replace("&", "%26");
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/meal-builder-data.php?operation=create&mealname='+addslashes(mealname)+'&meal='+meal);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
function EditMeal(mealid, mealname, meal) {
	mealname = mealname.replace("&", "%26");
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/meal-builder-data.php?operation=properties&mealid='+mealid+'&renameto='+addslashes(mealname)+'&meal='+meal);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
//---------------------------------------------------------
//For recipe-builder.php
function displayCreateRecipe() {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/save-recipe.php?ran='+ran_no;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	return false;
}
function displayEditRecipe(recipeid) {
    jsrecipeid = recipeid;
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/save-recipe.php?recipeid='+recipeid+'&ran='+ran_no;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	return false;
}
function displayRecipeDirections(recipeid) {
    jsrecipeid = recipeid;
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/add-notes.php?recipeid='+recipeid+'&ran='+ran_no+'&returnpage=recipe-builder-data.php';
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	return false;
}
function CreateRecipe(recipename, meal, servings) {
	recipename = recipename.replace("&", "%26");
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/recipe-builder-data.php?operation=create&recipename='+addslashes(recipename)+'&meal='+meal+'&servings='+servings);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
function EditRecipe(recipeid, recipename, meal, servings) {
	recipename = recipename.replace("&", "%26");
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/recipe-builder-data.php?operation=properties&recipeid='+recipeid+'&renameto='+addslashes(recipename)+'&meal='+meal+'&servings='+servings);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}
/*function RecipeBuilderSetServings(returnpage, servings, mealid) {
    jsrecipeid = mealid;
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', returnpage+'?operation=setservings&servings='+servings+'&ran='+ran_no+'&mealid='+mealid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}*/
function RecipeSaveDirections(recipeid, directions) {
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/recipe-builder-data.php?operation=notes&recipeid='+recipeid+'&directions='+addslashes(directions));
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}

//---------------------------------------------------------
//For add-notes.php
function displayNotes(day, mon, year, notes, userid) {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/add-notes.php?day='+day+'&mon='+mon+'&year='+year+'&notes='+addslashes(notes)+'&viewuserid='+userid+'&returnpage=calendar-day.php');
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
}
function sendEmail(returnpage, userid) {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/add-notes.php?notes=email&viewuserid='+userid+'&returnpage='+returnpage);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
}
function sendEmailSMS(to, message) {
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/-mailsender.php?operation=emailsms&message='+addslashes(message)+'&mailto='+to);
   	http.onreadystatechange = ResultToMessagePanel;
   	http.send(null); 
}
function editLeadNotes(returnpage, leadid) {
    BackStack.length = 0;
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	document.getElementById("PopoverWrap").className = "overlay";
	window.location.href = "#top";
    
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/add-notes.php?operation=lead&viewleadid='+leadid+'&returnpage='+returnpage);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
}
function AddNotesSave(day, mon, year, notes, text, userid) {
	text = text.replace("&", "%26");
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/calendar-day.php?operation=notes&day='+day+'&mon='+mon+'&year='+year+'&notes='+addslashes(notes)+'&text='+addslashes(text)+'&viewuserid='+userid);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}

//---------------------------------------------------------
//For save-meal.php
function SaveMealSave(mealname, day, mon, year, meal) {
	mealname = mealname.replace("&", "%26");
	CloseAddEditPanel();
	var ran_no=(Math.round((Math.random()*9999))); 
	http.open('get', '/calendar-day.php?operation=savemeal&mealname='+addslashes(mealname)+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal);
   	http.onreadystatechange = ResultToMainPanel;
   	http.send(null); 
   	return false;
}

//---------------------------------------------------------
//For add-meal.php
function AddMealPerformSeach(string, day, mon, year, mealid, meal, viewuserid) {
	string = string.replace("&", "%26");
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	window.location.href = "#top";
	    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/add-meal.php?string='+string+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	BackStack.push(url);
   	return false;
}
function AddMealPerformSeach2(string, day, mon, year, mealid, meal, viewuserid) {
	string = string.replace("&", "%26");
	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
	window.location.href = "#top";
	    
	var ran_no=(Math.round((Math.random()*9999))); 
	url = '/add-meal.php?txtstring='+string+'&ran='+ran_no+'&day='+day+'&mon='+mon+'&year='+year+'&meal='+meal+'&mealid='+mealid+'&viewuserid='+viewuserid;
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	BackStack.push(url);
   	return false;
}
//---------------------------------------------------------
function OnChangeAddMealValues() {
	var		selUnit = document.getElementById("servingunit"); 
	var		quantity = document.getElementById("quantity"); 
	var		quantityplus = document.getElementById("quantityplus"); 

	su = document.getElementById("servingunit"); 
	detailid = su[su.selectedIndex].value;
	if (detailid.indexOf("::")>0) {
	    detailid = detailid.substring(0, detailid.indexOf("::"));
	}
	servingunit = su[su.selectedIndex].text;
	calories = document.getElementById("cal"+detailid).value; 
	servingname = document.getElementById("sn"+detailid).value; 	
	servingsize = document.getElementById("sz"+detailid).value; 	
    equalserving = document.getElementById("ez"+detailid).value;
    equalservingunit = document.getElementById("eq"+detailid).value;
	if (quantity.value.length==0 || isNaN(parseFloat(quantity.value))) {
        servingamount = parseFloat(quantityplus.value);
	} else {
        servingamount = parseFloat(FractionToDec(quantity.value)) + parseFloat(quantityplus.value);
    }
	
	http2 = createHttpRequester(); 
	http2.open('get', '/food-label.php?detailid='+detailid+'&label=show&servingunit='+addslashes(servingunit)+'&servingamount='+servingamount);
   	http2.onreadystatechange = ResultToFoodLabelPanel2;
   	http2.send(null); 
}
//---------------------------------------------------------
function ShowMoreResults(extraid) {
	document.getElementById("showmore"+extraid).className = "hide";
	document.getElementById("moreresults"+extraid).className = "show";
   	return false;
}
//---------------------------------------------------------
//---------------------------------------------------------
function NavBack() {
    if (BackStack.length<=1) {
        CloseAddEditPanel();
        return false;
    }
   	junk = BackStack.pop(); //Current one.
   	url = BackStack.pop();
	http.open('get', url);
   	http.onreadystatechange = ResultToAddEditPanel;
   	http.send(null); 
   	BackStack.push(url);   	
   	return false;
}
//---------------------------------------------------------
function CloseAddEditPanel() {
	if (document.getElementById("AddEdit")) document.getElementById("AddEdit").innerHTML = "";
	if (document.getElementById("PopoverWrap")) document.getElementById("PopoverWrap").className = "hiddenoverlay";
	if (document.getElementById("FoodLabel")) document.getElementById("FoodLabel").innerHTML = "";
	
	if (document.getElementById("WorkingGray")) document.getElementById("WorkingGray").className = "hiddenoverlay";
	if (document.getElementById("WorkingHtml")) document.getElementById("WorkingHtml").innerHTML = "";
	return false;
}

//---------------------------------------------------------
//---------------------------------------------------------
function displayAsync(ReturnPage, ResultPanel, ThePage, TheURL) {
//alert("dAsync:"+TheURL);
	jsVarsFromURL(TheURL);
    if (jsXPos==-1) jsXPos = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
	
    BackStack.length = 0;
    if (ResultPanel==ResultToMainPanelQ) {
        ShowWorkingMainPanel();
	} else if (ResultPanel==ResultToMainPanel) {
        ShowWorkingOverlay();
    } else if (ResultPanel==ResultToMessagePanel || ResultPanel==ResultToMessagePanel2) {
    } else {
    	if (document.getElementById("AddEdit")) document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
    	if (document.getElementById("PopoverWrap")) document.getElementById("PopoverWrap").className = "overlay";
    }	
	jsreturnpage = ReturnPage;
	
	http.open('get', TheURL, true);
   	http.onreadystatechange = ResultPanel;
   	http.send(null); 
   	BackStack.push(TheURL);
   	return false;
}
//---------------------------------------------------------
function Async(ResultPanel, ThePage, TheURL) {
//alert("Async:"+TheURL);
	jsVarsFromURL(TheURL);
	
	if (ResultPanel==ResultToMainPanelQ) {
        ShowWorkingMainPanel();
	} else if (ResultPanel==ResultToMainPanel) {
        ShowWorkingOverlay();
    } else if (ResultPanel==ResultToMessagePanel || ResultPanel==ResultToMessagePanel2) {
    } else {
    	if (document.getElementById("AddEdit")) document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
    	if (document.getElementById("PopoverWrap")) document.getElementById("PopoverWrap").className = "overlay";
    }
	
	http = createHttpRequester(); 
	http.open('get', TheURL, true);
   	http.onreadystatechange = ResultPanel;
   	http.send(null); 
   	BackStack.push(TheURL);
   	return false;
}

//---------------------------------------------------------
function AsyncResults(ResultID, TheURL) {
   	if (document.getElementById(ResultID)) document.getElementById(ResultID).innerHTML = SMALLWORKINGANIMATIONINLINE;
   		
	http = createHttpRequester(); 
	http.open('get', TheURL, true);
   	http.onreadystatechange = function() {
    	    if (http.readyState==4 && http.status==200) { 
		        if (document.getElementById(ResultID)) {
		            document.getElementById(ResultID).innerHTML = http.responseText; 
		        }
            }
        }

   	http.send(null); 
   	return false;
}

function AsyncResultsForm(ResultID, FormName, TheURL) {
    TheForm = document.getElementById(FormName);
    
    if (!endsWith(TheURL, "?") && !endsWith(TheURL, "&")) {
        TheURL += "?";
    }

    for (i=0; i<TheForm.elements.length; i++) {
        if (TheForm.elements[i].type=="checkbox") {
            TheURL += TheForm.elements[i].name + "=" + (TheForm.elements[i].checked ? TheForm.elements[i].value : "" ) + "&";
        } else if (TheForm.elements[i].type=="submit") {
            if (TheForm.elements[i].value==jsSubmitValue) {
                TheURL += TheForm.elements[i].name + "=" + TheForm.elements[i].value + "&";
            }
        } else if (TheForm.elements[i].type=="radio") {
            if (TheForm.elements[i].checked) {
                TheURL += TheForm.elements[i].name + "=" + TheForm.elements[i].value + "&";
            }
        } else if (TheForm.elements[i].type=="textarea") {
            TheURL += TheForm.elements[i].name + "=" + addslashes(TheForm.elements[i].value) + "&";
        } else {
            TheURL += TheForm.elements[i].name + "=" + addslashes(TheForm.elements[i].value) + "&";
        }
        TheURL = TheURL.replace(/\s+$/, '');
    }
    TheURL += "ajax=" + (Math.round((Math.random()*9999)));
    
    CloseAddEditPanel();
    
    return AsyncResults(ResultID, TheURL);
}

//---------------------------------------------------------
//Lets the Submit button set which one got clicked...
var jsSubmitValue="";
function formAsyncSubmit(SubmitValue) {
    jsSubmitValue = SubmitValue;
}
function formAsync(ResultPanel, FormName, TheURL) {
//alert(TheURL);
    TheForm = document.getElementById(FormName);
    
    if (!endsWith(TheURL, "?") && !endsWith(TheURL, "&")) {
        TheURL += "?";
    }

    for (i=0; i<TheForm.elements.length; i++) {
        if (TheForm.elements[i].type=="checkbox") {
            TheURL += TheForm.elements[i].name + "=" + (TheForm.elements[i].checked ? TheForm.elements[i].value : "" ) + "&";
        } else if (TheForm.elements[i].type=="submit") {
            if (TheForm.elements[i].value==jsSubmitValue) {
                TheURL += TheForm.elements[i].name + "=" + TheForm.elements[i].value + "&";
            }
        } else if (TheForm.elements[i].type=="radio") {
            if (TheForm.elements[i].checked) {
                TheURL += TheForm.elements[i].name + "=" + TheForm.elements[i].value + "&";
            }
        } else if (TheForm.elements[i].type=="textarea") {
            TheURL += TheForm.elements[i].name + "=" + addslashes(TheForm.elements[i].value) + "&";
        } else {
            TheURL += TheForm.elements[i].name + "=" + addslashes(TheForm.elements[i].value) + "&";
        }
        TheURL = TheURL.replace(/\s+$/, '');
    }
    TheURL += "ajax=" + (Math.round((Math.random()*9999)));
    
//if (DEBUG) alert(TheURL);
    
	if (ResultPanel==ResultToMainPanel) {
        ShowWorkingOverlay();
    } else if (ResultPanel=="") {
        window.location.href = TheURL;
        return false;
    } else if (ResultPanel==ResultToMessagePanel2) {
    } else if (ResultPanel==ResultToMessagePanel) {
        ShowSmallWorkingOverlay();
    } else {
    	document.getElementById("AddEdit").innerHTML = WORKINGANIMATION;
    }
	
//alert("Form:"+TheURL);
	return Async(ResultPanel, '', TheURL);
}
//---------------------------------------------------------
//---------------------------------------------------------
function jsVarsFromURL(TheURL) {
    //Meal/Recipe/User variables...
    if (TheURL.indexOf("recipeid=")>0) {
        jsrecipeid = TheURL.substring(TheURL.indexOf("recipeid=")+9);
        if (jsrecipeid.indexOf("&")>0) {
            jsrecipeid = jsrecipeid.substring(0, jsrecipeid.indexOf("&"));
        }
    }
    if (TheURL.indexOf("mealid=")>0) {
        jsmealid = TheURL.substring(TheURL.indexOf("mealid=")+7);
        if (jsmealid.indexOf("&")>0) {
            jsmealid = jsmealid.substring(0, jsmealid.indexOf("&"));
        }
    }
    if (TheURL.indexOf("viewuserid=")>0) {
        jsuser = TheURL.substring(TheURL.indexOf("viewuserid=")+11);
        if (jsuser.indexOf("&")>0) {
            jsuser = jsuser.substring(0, jsuser.indexOf("&"));
        }
    }
    //Day
    if (TheURL.indexOf("&day=")>0) {
        jsd = TheURL.substring(TheURL.indexOf("&day=")+5);
        if (jsd.indexOf("&")>0) {
            jsd = jsd.substring(0, jsd.indexOf("&"));
        }
    }
    if (TheURL.indexOf("&d=")>0) {
        jsd = TheURL.substring(TheURL.indexOf("&d=")+3);
        if (jsd.indexOf("&")>0) {
            jsd = jsd.substring(0, jsd.indexOf("&"));
        }
    }
    //Month
    if (TheURL.indexOf("&mon=")>0) {
        jsm = TheURL.substring(TheURL.indexOf("&mon=")+5);
        if (jsm.indexOf("&")>0) {
            jsm = jsm.substring(0, jsm.indexOf("&"));
        }
    }
    if (TheURL.indexOf("&m=")>0) {
        jsm = TheURL.substring(TheURL.indexOf("&m=")+3);
        if (jsm.indexOf("&")>0) {
            jsm = jsm.substring(0, jsm.indexOf("&"));
        }
    }
    //Year
    if (TheURL.indexOf("&year=")>0) {
        jsy = TheURL.substring(TheURL.indexOf("&year=")+6);
        if (jsy.indexOf("&")>0) {
            jsy = jsy.substring(0, jsy.indexOf("&"));
        }
    }
    if (TheURL.indexOf("&y=")>0) {
        jsy = TheURL.substring(TheURL.indexOf("&y=")+3);
        if (jsy.indexOf("&")>0) {
            jsy = jsy.substring(0, jsy.indexOf("&"));
        }
    }
}
function SetIDtoValue(theid, thevalue) {
	if (!document.getElementById(theid)) return;	
	document.getElementById(theid).value = thevalue;
}

function OnChangeMealCheckOperation() {
	if (!document.getElementById("MealCheckOperation")) return;	
	if (!document.getElementById("SelectDate")) return;	
	if (!document.getElementById("SelectMeal")) return;	
	if (!document.getElementById("SubmitButton")) return;	
	if (!document.getElementById("TheSubmitBtn")) return;	
	
	var current = document.getElementById("MealCheckOperation").value;
	
	if (current=="copyto") document.getElementById("TheSubmitBtn").value = "Copy";
	if (current=="moveto") document.getElementById("TheSubmitBtn").value = "Move";
	if (current=="shoplist") document.getElementById("TheSubmitBtn").value = "Add";
	if (current=="delete") document.getElementById("TheSubmitBtn").value = "Delete";
	
	if (current=="copyto" || current=="moveto") {
	    setTimeout('document.getElementById("SubmitButton").className = "showformitem";', 100);
	    setTimeout('document.getElementById("SelectMeal").className = "showformitem";', 100);
	    setTimeout('document.getElementById("SelectDate").className = "showformitem";', 100);
	    //WTF, IE is very screwy, show them twice!
	    document.getElementById("SubmitButton").className = "showformitem";
	    document.getElementById("SelectMeal").className = "showformitem";
	    document.getElementById("SelectDate").className = "showformitem";
	} else if (current!="") {
	    document.getElementById("SubmitButton").className = "showformitem";
	    document.getElementById("SelectMeal").className = "hideformitem";
	    document.getElementById("SelectDate").className = "hideformitem";
	} else {
	    document.getElementById("SelectDate").className = "hideformitem";
	    document.getElementById("SelectMeal").className = "hideformitem";
	    document.getElementById("SubmitButton").className = "hideformitem";
	}	
}


//---------------------------------------------------------
//  Functions for the buy page to select the current row.
//---------------------------------------------------------

function clickRadio(theItem) {
	if (document.getElementById("rowTechOnly")) document.getElementById("rowTechOnly").className = "";
	if (document.getElementById("row1month")) document.getElementById("row1month").className = "";
	if (document.getElementById("row3month")) document.getElementById("row3month").className = "";
	if (document.getElementById("row6month")) document.getElementById("row6month").className = "";
	if (document.getElementById("row12month")) document.getElementById("row12month").className = "";

	if (document.getElementById(theItem)) document.getElementById(theItem).className = "selected";
	
	/*if (theItem=="row1month") {
		document.getElementById("row1month").className= "selected";
	} else if (theItem=="row3month") {
		document.getElementById("row3month").className = "selected";
	} else if (theItem=="row12month") {
		document.getElementById("row12month").className = "selected";
	} else if (theItem=="rowTechOnly") {
		document.getElementById("rowTechOnly").className = "selected";
	}*/
}



