// JavaScript Document

//global var for quick pick meal id
var qpmealid = 0;

function getmealoptions(date) {
//gets meal options for a date via ajax call
//date is expected to be in text format: m/d/y

	document.getElementById("calendardate").innerHTML = date;
	document.getElementById("ResDate").value = date;

	//construct URL to be called
	var url = "include/getinfo.asp?info=mealoptions&date=" + date;

	//for testing only
	//alert(url);
	//window.location = url;
	
	//make ajax call with YUI connection manager
	YAHOO.util.Connect.asyncRequest("GET", url, {success: mealoptionshandler });
	
}

function mealoptionshandler(o) {
//callback for getmealoptions

	//remember index of currently selected meal button
	var button = getcurrentmealbuttonindex();
	
	//redraw meal option buttons
	document.getElementById("mealoptions").innerHTML = o.responseText;

	//check to see if button was undefined (i.e. when page first loads);
	//if so, get selection post-redraw
	if (button == undefined)
		button = getcurrentmealbuttonindex();

	//if there is only one button, select the first (and only) one
	if (document.getElementsByName("Meal").length == 1)
		button = 0;
	
	//check if function was called because of quick pick selection; if so, get index of desired qp meal id
	if (qpmealid > 0) {

		//get index
		button = getmealbuttonindex(qpmealid);

		//if return value was -1, meal id wasn't found, so set to 0
		if (button == -1)
			button = 0;
			
		//clear qpmealid
		qpmealid = 0;
	}

	//simulate a click on the previously selected meal button to force meal info box update
	//only do if there buttons
	if (o.responseText.search("radio") != -1)
		document.getElementsByName("Meal")[button].click();
	
	
}
	
function getmealinfo(mealid) {
//gets meal info via ajax call, but only if mealinfo div is present

	if (document.getElementById("mealinfo") != null) {

		//construct URL to be called
		var url = "include/getinfo.asp?info=mealinfo&mealid=" + mealid;
	
		//make ajax call with YUI connection manager
		YAHOO.util.Connect.asyncRequest("GET", url, {success: mealinfohandler });
	}
}

function mealinfohandler(o) {
//callback for getmealinfo

	document.getElementById("mealinfo").innerHTML = o.responseText;

}

function getmealidfromform() {
//gets the currently selected meal ID (i.e. value)

	var mealbuttons = document.getElementsByName("Meal");
	
	for (i=0; i<mealbuttons.length; i++){
		
		if (mealbuttons[i].checked==true)
		return mealbuttons[i].value;
	}
}

function getcurrentmealbuttonindex() {
//gets the index of the currently selected meal

	var mealbuttons = document.getElementsByName("Meal");
	
	for (i=0; i<mealbuttons.length; i++){
		
		if (mealbuttons[i].checked==true)
		return i;
	}
}

function getmealbuttonindex(mealid) {
//greturns the index of the button with the given meal id
//if not found, returns -1

	var mealbuttons = document.getElementsByName("Meal");
	
	for (i=0; i<mealbuttons.length; i++){
		
		if (mealbuttons[i].value == mealid) 
			return i;
	}
	
	return -1;
}

function qpOnClick(date, mealid) {
//handler for when a quick pick link is clicked

	qpmealid = mealid;
	
	$('#datepicker').datepicker('setDate', date);
	
	getmealoptions(date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear());
	getmealinfo(mealid);

}