// JavaScript Document
var xmlhttp;

function showItemDetails(str)
{
	if(str) {
		xmlhttp=GetXmlHttpObject();
		
		if (xmlhttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		
		var url="nutritional_data.php";
		url=url+"?item_details="+str;
		url=url+"&sid="+Math.random();
		xmlhttp.onreadystatechange=stateChanged;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("item_details_div").innerHTML=xmlhttp.responseText;
document.getElementById('item_details_div').style.display = 'inline';

}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}



/****************************************************** Selects *****************************************************/



var ajax = new Array();

/********** Section Select ******************/

function getSection(sel)
{
	document.getElementById('item_details_div').style.display = 'none';
	var menu_id = sel.options[sel.selectedIndex].value;
	document.getElementById('select_section').options.length = 0;	// Empty city select box
	if(menu_id.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'nutritional_data.php?menu_id='+menu_id;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createSections(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	} else {
		var obj = document.getElementById('select_section');
		obj.options[obj.options.length] = new Option('Select Menu Section','');
		document.getElementById('select_section').disabled = true;
		document.getElementById('select_item').disabled = true;
	}
}

function createSections(index)
{
	var obj = document.getElementById('select_section');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code
	document.getElementById('select_section').disabled = false;
	document.getElementById('select_item').disabled = true;
}

/********** Item Select ******************/

function getItem(sel)
{
	document.getElementById('item_details_div').style.display = 'none';
	var section_id = sel.options[sel.selectedIndex].value;
	document.getElementById('select_item').options.length = 0;	// Empty city select box
	if(section_id.length>0){
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'nutritional_data.php?section_id='+section_id;	// Specifying which file to get
		ajax[index].onCompletion = function(){ createItems(index) };	// Specify function that will be executed after file has been found
		ajax[index].runAJAX();		// Execute AJAX function
	} else {
		var obj = document.getElementById('select_item');
		obj.options[obj.options.length] = new Option('Select Menu Item','');
		
		document.getElementById('select_item').disabled = true;
	}
}

function createItems(index)
{
	var obj = document.getElementById('select_item');
	eval(ajax[index].response);	// Executing the response from Ajax as Javascript code
	document.getElementById('select_item').disabled = false;
}
