
var taxRate = 0.095;

//Date methods
Date.prototype.getMonthName = function(){
	var months = ['January', 'February','March','April','May','June','July','August','September','October','November','December'];
	return months[this.getMonth()];
}
Date.prototype.getShortMonthName = function(){
	var shortMonths = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
	return shortMonths[this.getMonth()];
}
Date.prototype.getDateName = function(){
	var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
	return days[this.getDay()];
}
Date.prototype.getShortDateName = function(){
	var shortDays = ['Sun','Mon','Tues','Weds','Thurs','Fri','Sat'];
	return shortDays[this.getDay()];	
}
Date.prototype.addDay = function(n){
	this.setDate( this.getDate() + parseInt(n) );	
}
Date.prototype.getNoon = function(){
	if(12==this.getHours() && 0==this.getMinutes() && 0==this.getSeconds())
	{
		return 'Noon';				
	}	
	else
	{
		return '';
	}
}
Date.prototype.getAMPM = function(){
	if(12<=this.getHours())
	{
		return 'pm';
	}			
	else
	{
		return 'am';			
	}
}
Date.prototype.getAMPMHours = function(){
	var h = this.getHours(); //0-23
	if(0==h)
	{
		return 12;
	}
	else if(12==h)
	{
		return 12;
	}
	else if(12 < h)
	{
		return h-12;
	}
	else
	{
		return h;
	}
}
Date.prototype.newDateFromMySQLDate = function(mysqlDate){
	var d = mysqlDate.split(' ');
	var y = d[0].split('-');
	var t;
	if(d[1]){
		t = d[1].split(':');
	}
	else
	{
		t = ['00'];	
	}
	//hours can have leading zeroes, look for that
	if(0==t[0].indexOf('0')){
		t[0] = t[0].substr(1,1);	
	}
	
	return new Date(y[0],y[1]-1,y[2], parseInt(t[0]) );
	
}
Date.prototype.getMySQLFormatYMD = function(){
	return this.getFullYear() + '-' + (this.getMonth() +1) + '-' +this.getDate();	
}
Date.prototype.getAMPMNoonHours = function(){
	return this.getAMPMHours() + this.getAMPM() + ' ' + this.getNoon();	
}

Array.prototype.sortMySQLDate = function(){
	return this.sort(
			  function(a,b){
				var aD = new Date().newDateFromMySQLDate(a);
				var bD = new Date().newDateFromMySQLDate(b);
				return aD.getTime() - bD.getTime();
			  }
		);	
}


function menu() {

	var items={};
	var that = this;
	
	this.getSizeTypes = function(){
		return sizeTypes;
	}

	this.getItem = function (id){
		return items[id];
	};
	
	this.getItems = function(){
		return items;
	};
	
	this.addItem = function(oItem){
		items[oItem.itemID] = oItem; //add to the menu
	};

	this.remItem = function(id){
		delete( items[id] );	
	};
	
	this.listItems = function(){
		var a=[];
		for(var i in items){if(items.hasOwnProperty(i)){
			a.push(items[i].desc);
		}}
		
		return a.join("\n");
	};

	this.showHTML = function(entryItem, entryCount){
		/**
		*	list out the available items and unit prices as appropriate
		*/
		var s,t, sNum;
		
		if('menuItem'==entryItem.type) //just show the item //called from a default listing
		{
			
			s = renderMenuItemToHTML(entryItem.menuItem, entryCount);
			
		}
		else if('reservationMenuItem'==entryItem.type)
		{
			s = renderReservationMenuItemToHTML(entryItem.itemGroup, entryItem.menuItemID, entryCount);
		}
		else if('itemGroup'==entryItem.type) //show the items of the group //called from a substitutable itemgroup
		{
			//pick
			s = '<div>';
			s += renderDiscountSubstituteItemGroupToHTML(entryItem.itemGroup, entryItem.menuItemID, entryCount);
			s += renderMenuEntryReservationsToHTML(entryItem.itemGroup.menuEntryID);
			s += '</div>';
			
		}
		else if('menuEntry'==entryItem.type) //the whole entry! //called from a dinner
		{
			// how many over all
			
			//pick from what you can pick
			s = '<div>';
			s += renderMenuEntryToHTML(entryItem.menuEntry, entryCount);
			s += renderMenuEntryReservationsToHTML(entryItem.menuEntry.menuEntryID);
			s += '</div>';
		}
		
	
		
		return s;		
	
	};
	
	function renderMenuEntryReservationsToHTML(menuEntryID)
	{
		/**
			* 	Builds the reservation block for a Menu Entry 
			*		(at the Entry level, if Reservation Type is set to Allow Day|Allow Day and Hour|Require Day|require Day and Hour)
			*
			*	Displays options for users to choose when they would like to pick up or drop off (or whatever else) their item
			*
			*	Types:
			*		range - Repeats the timeBegin-timeEnd slots for each day from dateBegin to dateEnd
			*		daily - NOT IMPLEMENTED
			*		weekly - Repeats the timeBegin-timeEnd slots for each weekday equal to weekday(dateBegin) (such as 'Monday'), up until dateEnd
			*		monthly - Repeats the timeBegin-timeEnd slots for each calendar day equal to dateBegin (such as 25th), up until dateEnd
			*		yearly - NOT IMPLEMENTED --Repeats the timeBegin-timeEnd slots for each dateBegin (e.g. Christmas, Easter) every year. dateEnd is ignored			
			*
			*	Time slots are only available from now into the future, even if a dateBegin is in the past
			*
			*	This method composites all reservation types to create a single offering
			*	Ranges are first considered, then overridden by weekly, then monthly
			*		In other words, with
			*			range(2009-12-01,2009-12-31,8,16) //every day in December, 2009, 8am-4pm
			*			weekly(2009-12-02,209-12-31,10,14) //10am-2pm Wednesdays until December 31, 2009	
			*		the slots will be 8am-4pm MT,RFSS, and 10am-2pm W
			*
		**/
		var menuEntry = oMenu.getMenuEntry(menuEntryID);
		var dBegin, dEnd, dOpen;
		var tBegin, tEnd;

		var slots = {}, slotTimes=[];
		var i, igrl, igr, igp,st, rd, opt;
		var s='';
		var wcounter=0;
	
		//build our day options
	
		var res, resCount = menuEntry.reservations.length;
		for(i=0;i<resCount;i++) //each reservation entry, in range<weekly<monthly, dateBegin asc, timeBegin asc
		{
			res = menuEntry.reservations[i];
			dBegin = new Date().newDateFromMySQLDate(res.dateBegin);
			dEnd =  new Date().newDateFromMySQLDate(res.dateEnd);
	
			//loop through the timeslots, calc how many are available
			igp = parseInt(res.period);
			tBegin = parseFloat(res.timeBegin);
			tEnd = parseFloat(res.timeEnd);
			slotTimes = [];
			
			while(tBegin <=tEnd)
			{
				slotTimes.push( {items:parseInt(res.itemsPerPeriod)
								,time:tBegin}				
							); //slots.day.time[i] has n available
				tBegin += (igp/60);
			}
	
			//don't offer days in the past
			if(dBegin.getTime() < new Date().getTime())
			{
				dBegin = new Date();	
			}
	
			//advance dBegin by advanceNotice days
			dBegin.addDay( res.advanceNotice );
	
			while( dBegin <= dEnd )
			{
				slotDay = dBegin.getMySQLFormatYMD();	
				slots[slotDay] = {opt:''
								,tBegin:res.timeBegin
								,title:res.title			
								,dayTitle:res.dayTitle
								,timeTitle:res.timeTitle
								,times:slotTimes
								}; //this will overwrite a previous entry, e.g. if a weekly overlaps a range
				
				opt = '<option value="' + slotDay + '"'
				if(0==wcounter) //first time, select!
				{
					opt += ' selected="true" ';
					wcounter++;
				}
				opt += '>' + dBegin.getDateName() + ', ' + dBegin.getMonthName() + ' ' + dBegin.getDate() + '</option>';
				
				slots[slotDay].opt = opt;
			
				//now advance the 'counter	'
				dBegin.addDay(1);			
			}
			
		}

		//sort out our actual calendar days
		var calDays=[];
		for(var calDay in slots){if(slots.hasOwnProperty(calDay)){
			calDays.push(calDay);
		}}
		calDays.sortMySQLDate();
		
		//build slot grids
		var slot, theSlot, h, reservationDayTableDivs=[];
		var pickDays = [];
		var calDaysCount = calDays.length;
			
		for(i=0;i<calDaysCount;i++) //each reservation day, in ascending-day order
		{
			slotDay = calDays[i];	
			theSlot = slots[ slotDay ];
			pickDays.push( theSlot.opt ); //each option item
			
			h = '<div id="slots' + slotDay + '" ';
			if(0<i)
			{
				h += 'style="display:none;"';
			}

			h += '><table>'; // id="slots2009-10-20"			
			h += '<tr><td colspan="2">' + theSlot.timeTitle + '</td></tr>';
			
			for(var j=0;j<theSlot.times.length;j++) //each slot-time; this will have the same number of items as while(tBegin<tEnd){}
			{
			
				h += '<tr><td align="left">';
				h += '<input type="radio" name="slot' + slotDay + '" id="slot' + slotDay + '_' + j + '" '; //name="slot2009-10-20" id="slot2009-10-20_0"
				if(0==j)
				{
					h += ' checked ';	
				}
				h += '/>';
				h += new Date().newDateFromMySQLDate(slotDay + ' ' + theSlot.times[j].time).getAMPMNoonHours();				
				h += '</td>';
				
				h += '<td>';
				/*
				//not worrying about number of open slots for this one.
				if(theSlot.times[j].items){
					h += theSlot.times[j].items;	
				}
				*/
				h += '</td>';
			
				h += '<td>';
				h += '</td></tr>';	
			}
			
			h += '</table></div>';
			
			reservationDayTableDivs.push(h);
			
		}	
		
		//now build the html
		
		//show the day selector
		s = '<div style="width:99%">';
		s += '<table style="width:100%">';
		s += '<tr><td>' + menuEntry.reservations[0].dayTitle + '<select id="pickDay" onclick="showSlots()">';
		s += pickDays.join('');
		s += '</select></td></tr>';
		
		s += '<tr><td>' + reservationDayTableDivs.join('') + '</td></tr>';
		s += '</table>';
	
		s += '</div>';
		
		
		return s;
		
	}
	
	function renderMenuItemToHTML(menuItem, entryCount)
	{
		var s ='<div id="orderItem_' + entryCount + '">';//s='<div id="menuItem_' + li + '">';
		s += '<table style="width:99%"><tr><td />';	// column title row. nothing in the first cell
		/**
		*	loop through the available units and prices
		*/
		var uc = menuItem.units.length;
		for(var i=0;i<uc;i++){
			s += '<td align="left">' + menuItem.units[i].title + '</td>';				
		}
		s += '</tr>';
		
		//	make a select list from 1 to 100 -- if they want more than 100, they can add a second entry
		
		s += '<tr><td><select id="item_' + entryCount + '_count">'; //s += '<tr><td><select id="item_' + li + '_count">';
		var p=10, q;
		while(p--){
			s += ['<option value="','">','</option>'].join( Math.abs(p-10) );
		}
		
		s += '</select>&nbsp;';
		s += menuItem.title + '</td>';
		
		//	loop through the available units and add prices
		for(var i=0;i<uc;i++){
			s += '<td align="left"><input name="item_' + entryCount + '_price" id="item_' + entryCount + '_price" type="radio" value="' + menuItem.units[i].price + '" '
			if(1==(uc-i)){ //last one
				s += ' checked="true"';
			}
			s += '>$' + menuItem.units[i].price + '</td>';				
		}
		
		
		s += '</tr>';
		s += '<tr><td colspan="' + uc + 1 + '"><textarea id="item_' + entryCount + '_comments" class="entryComment" style="width:100%;">Add comments here</textarea>';
		s += '<td/></tr>';	
	
		s += '</table>';
	
		s += '</div>';
		
		return s;
	}
		
	function renderReservationMenuItemToHTML(itemGroup, menuItemID, entryCount)
	{
		var s ='<div id="orderItem_' + entryCount + '">';//s='<div id="menuItem_' + li + '">';
		s += '<table style="width:98%">';
	
		var menuItem = oMenu.getMenuItem(itemGroup.menuEntryID,itemGroup.itemGroupID, menuItemID);
		
		var uc = menuItem.units.length;
		
		s += '<tr><td colspan="2">' + menuItem.title + '</td></tr>';
	
		s += '<tr>';
		for(var i=0;i<uc;i++){
			if(0<i && 0==i%2)
			{
				s += '</tr>';
				s += '<tr>';
			}
			s += '<td align="left"><input name="item_' + entryCount + '_price" id="item_' + entryCount + '_price"';
			if(0==i)
			{
				s += ' checked="true" ';	
			}
			s += 'type="radio" value="' + menuItem.units[i].price + '" />' + menuItem.units[i].title + '</td>';	

		}
		s += '</tr>';
		
	
		// list the days available
		
		var dBegin = new Date().newDateFromMySQLDate(itemGroup.dateBegin);
		var dEnd =  new Date().newDateFromMySQLDate(itemGroup.dateEnd);
	
		//advance notice required?
		dBegin.addDay( parseInt(itemGroup.advanceNotice) );
		
		var tBegin, tEnd;
		var mm, yyyy, dd;

		var pickDays = [];
		var slots = {};
		var i, igrl, igr, igp,st, rd, opt;
		var wcounter=0;
		while( dBegin <= dEnd )
		{
			yyyy = dBegin.getFullYear(); //4-digit year
			mm = dBegin.getMonth() +1; //1-12 month
			dd = dBegin.getDate(); //the 1-31 day of the month 
		
			slotDay = dBegin.getMySQLFormatYMD();	
			opt = '<option value="' + slotDay + '"'
			if(0==wcounter) //first time, select!
			{
				opt += ' selected="true" ';
				wcounter++;
			}
			opt += '>' + dBegin.getDateName() + ', ' + dBegin.getMonthName() + ' ' + dBegin.getDate() + '</option>';
			pickDays.push( opt );
		
			igrl = itemGroup.res.length;
			igp = parseInt(itemGroup.period);
			tBegin = parseFloat(itemGroup.timeBegin);
			tEnd = parseFloat(itemGroup.timeEnd);
			
			//loop through the timeslots, calc how many are available

			slots[slotDay] = {times:[]};

			while(tBegin <=tEnd)
			{
				slotTime = tBegin + ':00:00';
				st = slots[slotDay].times.length;
				slots[slotDay].times.push(parseInt(itemGroup.itemsPerPeriod)); //slots.day.time[i] has n available

				for(i=0;i<igrl;i++) //are there any existing reservations? if so we must reduce the ammount we say is available
				{
					igr = itemGroup.res[i];
					rd = new Date().newDateFromMySQLDate(igr.reservation);
					
					if(slotDay == rd.getMySQLFormatYMD() && tBegin==rd.getHours())
					{
						slots[slotDay].times[ st ] -= parseInt(igr.itemSlotsTaken);	
					}					
				}		
			
				tBegin += (igp/60);
			}
			
		
			//now advance the 'counter	'
			dBegin.addDay(1);			
		}
		
		//show the day selector
		s += '<tr><td colspan="2">' + itemGroup.dayTitle + ': <select id="pickDay" onchange="showSlots()">';
		s += pickDays.join('');
		s += '</select></td></tr>';
		
		//build slot grids
		var slot, theSlot, h, ash=[];
		var checkedOne=0;
		wcounter=0;
		
		for(slot in slots){if(slots.hasOwnProperty(slot)){
			theSlot = slots[slot];
			h = '<div id="slots' + slot + '" ';
			if(0<wcounter)
			{
				h += 'style="display:none;"';
			}

			h += '><table>'; // id="slots2009-10-20"
			wcounter++; //a little counter for the display
			checkedOne=0; //one timeslot should be checked by default
			h += '<tr><td>' + itemGroup.timeTitle + '</td><td>Quantity</td></tr>';
			tBegin = parseFloat(itemGroup.timeBegin);
			for(i=0;i<theSlot.times.length;i++) //each slot-time; this will have the same number of items as while(tBegin<tEnd){}
			{
				tAvail = theSlot.times[i];
				
			
				h += '<tr><td align="left">';
				h += '<input type="radio" name="slot' + slot + '" id="slot' + slot + '_' + i + '" '; //name="slot2009-10-20" id="slot2009-10-20_0"
				if(0<tAvail)
				{
					if(0==checkedOne)
					{
						h += ' checked="true" ';	
						checkedOne = 1;
					}
				}
				else
				{
					h += ' disabled ';	
				}
				h += '/>';
				if(12<tBegin)
				{
					h += (tBegin%12) + 'pm';
				}
				else if(12==tBegin)
				{
					h += '12pm Noon';
				}
				else
				{
					h += tBegin + 'am';					
				}
				h += '</td><td>';
				if(0<tAvail)
				{
					h += '<select id="slot' + slot + '_' + i + 'count" onclick="document.getElementById(\'slot' + slot + '_' + i +'\').checked = true">';
					for(var j=0;j<=tAvail;j++)
					{
						h += '<option value="' + j + '">' + j + '</option>';
					}
					h += '</select>';
				}
				h += '</td></tr>';
			
				tBegin += (igp/60);
			}
			
			h += '</table></div>';
			
			ash.push(h);
			
		}}
		
		s += '<tr><td colspan="2">' + ash.join('') + '</td></tr>';
		
		s += '<tr><td colspan="2"><textarea id="item_' + entryCount + '_comments" class="entryComment" style="width:100%;">Add comments here</textarea>';
		s += '</tr>';	
	
		s += '</table>';
	
		s += '</div>';
		
		return s;
	}
	
	function renderDiscountSubstituteItemGroupToHTML(itemGroup, menuItemID, entryCount)
	{
		//subsitutable menuEntry
		//pick unlimited pay itemGroup
		//no units! - use the itemGroup.defaultPrice and .discountPrice
		//show each item with a counter
		
		var s ='<div id="orderItem_' + entryCount + '">';//s='<div id="menuItem_' + li + '">';
		s += '<table width="100%">';
		
		if(0<parseInt(itemGroup.discountThresh))
		{
			s += '<tr><td colspan="2">';
			s += 'First ' + itemGroup.discountThresh + ' ' + itemGroup.title;
			if(1< itemGroup.discountThresh)
			{
				s += 's';
			}
			s += ' for $' + itemGroup.defaultPrice + ', each additional ' + itemGroup.discountThresh + ' $' + itemGroup.discountPrice + '.';
			s += '</td></tr>';
		}
		else
		{
			s += '<tr><td colspan="2">';
			s += itemGroup.title;
			s += ' for $' + itemGroup.defaultPrice;
			s += '</td></tr>';			
		}
		
		//s += '<tr><td />';	// column title row. nothing in the first cell
		
		//	loop through the available units and prices
		
		var i, j,k,p;
		var mic, mi;
		//each item in the itemGroup
		mic = itemGroup.items.length;
		for(i=0;i<mic;i++)
		{
			mi = itemGroup.items[i];
			s += '<tr>';
			s += '<td><select id="item_' + i + '_count">';
			
			for(j=0;j<11;j++)
			{
				s += '<option value="' + j + '"';
				if(1==j && mi.menuItemID==menuItemID)
				{
					s += ' selected="true" ';	
				}
				s +='>' + j + '</option>';
			}			
			s += '</select></td>';
			
			s += '<td align="left">' + mi.title + '</td>';
			s += '</tr>';
		}

		s += '<tr><td colspan="2"><textarea id="item_' + entryCount + '_comments" class="entryComment" style="width:100%;">Add comments here</textarea>';
		s += '<td/></tr>';	
		s += '</table>';
		
		

		s += '</div>';
		
		return s;
	}
	
	function renderMenuEntryToHTML(menuEntry, entryCount){
		var i, j,k,p;
		var s ='<div id="orderItem_' + entryCount + '">';//s='<div id="menuItem_' + li + '">';
		s += '<table style="width:99%">';
		s += '<tr><td colspan="4">';
		s += menuEntry.title;
		s += '</td></tr>';
	
			//each itemGroup
		var igc, ig, mic, mi;
		igc = menuEntry.itemGroups.length;
		for(i=0;i<igc;i++)
		{
			ig = menuEntry.itemGroups[i];
			//fork on itemGroup.groupType
			if('included'==ig.groupType){			
				//nothing to pick!			
			}			   
			else if('pickFree'==ig.groupType){
				//only pick
				s += '<tr><td colspan="4" align="left">Pick ' + ig.pick + ':</td></tr>';
				mic = ig.items.length;
				for(j=0;j<mic;j++)
				{
					mi = ig.items[j];
					if(!(j%2))
					{
						if(0<j)
						{
							s += '</tr>';	
						}
						s += '<tr>';
					}
					
					s += '<td><input onclick="pick(this,' + ig.pick + ',' + ig.itemGroupID + ')" type="checkbox" id="group' + ig.itemGroupID + 'item_' + j + '" value="' + mi.menuItemID + '"></td>';
					s += '<td align="left">' + mi.title + '</td>'; 					
				}	
				if(0<mic) //odd number of items, close it out
				{
					if(j%2)
					{
						s += '<td/>';
					}
					s += '</tr>';
				}

			}					
			else if('pickCharge'==ig.groupType){
				s += '<tr><td colspan="4" align="left">Pick up to ' + ig.pick + ':</td></tr>';
				mic = ig.items.length;
				for(j=0;j<mic;j++)
				{
					mi = ig.items[j];
					if(!(j%2))
					{
						if(0<j)
						{
							s += '</tr>';	
						}
						s += '<tr>';
					}
					
					s += '<td><input onclick="pick(this,' + ig.pick + ',' + ig.itemGroupID + ')" type="checkbox" id="group' + ig.itemGroupID + 'item_' + j + '" value="' + mi.menuItemID + '"></td>';
					s += '<td>' + mi.title + ' $' + mi.units[0].price + '</td>'; 
				}	
				if(0<mic) //odd number of items, close it out
				{
					if(j%2)
					{
						s += '<td/>';
					}
					s += '</tr>';
				}
			}					
			else if('pickChargeUnlimited'==ig.groupType){
				s += '<tr><td colspan="4" align="left">Any of these:</td></tr>';
				mic = ig.items.length;
				for(j=0;j<mic;j++)
				{
					mi = ig.items[j];
					if(!(j%2))
					{
						if(0<j)
						{
							s += '</tr>';	
						}
						s += '<tr>';
					}
					
					s += '<td><input type="checkbox" id="group' + ig.itemGroupID + 'item_' + j + '" value="' + mi.menuItemID + '"></td>';
					s += '<td>' + mi.title + '</td>'; 
					
					if(!(j%2))
					{
						s += '</tr>';
					}					
				}	
				if(0<mic) //odd number of items, close it out
				{
					if(j%2)
					{
						s += '<td/>';
					}
					s += '</tr>';
				}
			}
			
		}
		//	loop through the available units and prices
		
		
		s += '<tr><td colspan="4"><textarea id="item_' + entryCount + '_comments" class="entryComment" style="width:100%;">Add comments here</textarea>';
		s += '<td/></tr>';	
		s += '</table>';
	
		s += '</div>';
		
		return s;
	};
	
	this.getMenuItem = function(menuEntryID, itemGroupID, menuItemID ){
		var e, ec, igc, ig, mic, mi, miu, miuc;
		var i,j,k;
		
		ec = that.entries.length;
		outerLoop:
		for(i=0;i<ec;i++)
		{
			e = that.entries[i];
			if(e.menuEntryID == menuEntryID)
			{
				igc = e.itemGroups.length;
				for(j=0;j<igc;j++)
				{
					ig = e.itemGroups[j];
					if(ig.itemGroupID == itemGroupID)
					{
						mic = ig.items.length;
						for(k=0;k<mic;k++)
						{
							if(ig.items[k].menuItemID == menuItemID)
							{
								return ig.items[k];
								//break outerLoop;
							}
						}
					}
				}
			}
		}
		
		return 0;
	}
	
	this.getItemGroup = function( menuEntryID, itemGroupID ){
		var e, ec, igc, ig, mic, mi, miu, miuc;
		var i,j,k;
		
		ec = that.entries.length;
		outerLoop:
		for(i=0;i<ec;i++)
		{
			e = that.entries[i];
			if(e.menuEntryID == menuEntryID)
			{
				igc = e.itemGroups.length;
				for(j=0;j<igc;j++)
				{
					if(itemGroupID == e.itemGroups[j].itemGroupID)
					{
						return e.itemGroups[j];
					}			
				}
			}
		}
		return 0;	
	}
	
	this.getMenuEntry = function (menuEntryID){
		var e, ec, igc, ig, mic, mi, miu, miuc;
		var i,j,k;
		
		ec = oMenu.entries.length;
		outerLoop:
		for(i=0;i<ec;i++)
		{
			if(menuEntryID == that.entries[i].menuEntryID)
			{
				return that.entries[i];							
			}
		}	
		return 0;		
	}
	
	this.loadJSON = function(oJsn){
		that.entries = oJsn.entries;
	};

};


function showSlots()
{
	//loop to hide all slots
	var pd = document.getElementById('pickDay');
	for(var i=0;i<pd.length;i++)
	{
		if(pd[i] && pd[i].value)
		{
			document.getElementById('slots' + pd[i].value).style.display = 'none';	
		}
	}
	
	document.getElementById('slots' + pd[pd.selectedIndex].value).style.display = 'inline';	

}


var _order = {entryItem:{itemID:0}, entries:[], entryCount:0 };

function orderMenuItem(menuEntryID, itemGroupID, menuItemID){
	//get the item
	
	_order.entryItem = {menuItem: oMenu.getMenuItem(menuEntryID, itemGroupID, menuItemID)
					,menuEntryID:menuEntryID
					,itemGroupID:itemGroupID
					,'type':'menuItem'
					};
		//build a display of the entry chooser and put it inside the display dialog
	addEntryRow();
	
	//show the dialog
	showDialog('dlgOrderItems');



}
function orderReservationMenuItem(menuEntryID, itemGroupID, menuItemID){
	//get the item
	
	_order.entryItem = {itemGroup: oMenu.getItemGroup(menuEntryID, itemGroupID)
					,menuEntryID:menuEntryID
					,itemGroupID:itemGroupID
					, menuItemID:menuItemID
					,'type':'reservationMenuItem'
					};
		//build a display of the entry chooser and put it inside the display dialog
	addEntryRow();
	
	//show the dialog
	showDialog('dlgOrderItems');



}

function orderItemGroupEntry(menuEntryID, itemGroupID, menuItemID){
	//get the item
		
	_order.entryItem = {itemGroup: oMenu.getItemGroup(menuEntryID, itemGroupID)
					,menuEntryID:menuEntryID
					,'type':'itemGroup'
					,menuItemID:menuItemID
					};
		//build a display of the entry chooser and put it inside the display dialog
	addEntryRow();
	
	//show the dialog
	showDialog('dlgOrderItems');

}

function orderMenuEntry(menuEntryID){
	
	_order.entryItem = {menuEntry: oMenu.getMenuEntry( menuEntryID )
					,'type':'menuEntry'							
					};
		//build a display of the entry chooser and put it inside the display dialog
	addEntryRow();
	
	//show the dialog
	showDialog('dlgOrderItems');

}

var _picks={};
function pick(obj, thresh, groupID){
	
	if(_picks[groupID])
	{
		if(_picks[groupID].count < thresh) //add the item, unless it was already checked
		{
	
			if(_picks[groupID][obj.id]) //was checked, uncheck and remove
			{
				obj.checked = false;
				delete _picks[groupID][obj.id];
				_picks[groupID].count--;
			}
			else //wasn't checked; add
			{
				obj.checked = true;
				_picks[groupID][obj.id] = 1;
				_picks[groupID].count++;
			}
	
		}
		else
		{
			
			if(_picks[groupID][obj.id]) //was checked, uncheck and remove
			{
				obj.checked = false;
				delete _picks[groupID][obj.id];
				_picks[groupID].count--;
			}
			else
			{
				alert("You may only pick " + thresh + " of these.");
				obj.checked = false;				
			}
		}
		

	}
	else
	{
		_picks[groupID] = {count:0};
		
		if(_picks[groupID].count < thresh) //add the item, unless it was already checked
		{
	
			if(_picks[groupID][obj.id]) //was checked, uncheck and remove
			{
				obj.checked = false;
				delete _picks[groupID][obj.id];
				_picks[groupID].count--;
			}
			else //wasn't checked; add
			{
				obj.checked = true;
				_picks[groupID][obj.id] = 1;
				_picks[groupID].count++;
			}
	
		}
		else
		{
			if(_picks[groupID][obj.id]) //was checked, uncheck and remove
			{
				obj.checked = false;
				delete _picks[groupID][obj.id];
				_picks[groupID].count--;
			}
			else
			{
				alert("You may only pick " + thresh + " of these.");
				obj.checked = false;				
			}
				
		}
		
	}
	
}

function addEntryRow(){
	//build a display row
	
	var itemID = _order.entryItem.itemID;
	var entryIndex = _order.entryCount;
	_order.entryCount++;
	
	var d = document.createElement("div");
	d.innerHTML = oMenu.showHTML( _order.entryItem , entryIndex); // getItem(itemID).showHTML(itemID, entryIndex, oMenu.getSizeTypes());
	document.getElementById("itemEntries").appendChild(d.lastChild);
	
}

function entry(){
	this.menuEntryID=0;
	this.itemGroupID=0;
	this.menuItemID=0;
	this.count=0;
	this.price=0;
	this.unit='';
	this.title='';
	this.comments='';
	this.additional=[];
	this.type='';
	this.reservation='';
}

function addToOrder(){
//var _order = {entryItem:{itemID:0}, entries:[], entryCount:0 };
	var entryCount = _order.entryCount;
	var entryItem = _order.entryItem;
	var f, el, itemID, menuItem;

	if('menuItem'==entryItem.type) //just show the item //called from a default listing
	{
		var menuEntryID = entryItem.menuEntryID;
		var itemGroupID = entryItem.itemGroupID;
		var menuItemID = entryItem.menuItem.menuItemID;
		
		var menuItem = entryItem.menuItem;// oMenu.getMenuItem(menuEntryID, itemGroupID, menuItemID); //it will be easier to call properties than do lots of loops. clearer, anyway...

		for(var i=0; i<entryCount;i++){ //loop each entry, then push it onto _order.entries[]
			f = new entry(); 
			f.menuEntryID = menuEntryID;
			f.itemGroupID = itemGroupID;
			f.menuItemID = menuItem.menuItemID;
			f.title = menuItem.title;
			
			//get the count
			el = document.getElementById('item_' + i + '_count');
			f.count = el[el.selectedIndex].value; //how many with these preferences?
	
			//get the price
			p = document.getElementsByName('item_' + i + '_price'); //does byName solve the problem?
	
			for(var j=0;j<p.length;j++){
				if(p[j].checked){
					f.price = menuItem.units[j].price; //p[j].value;
					f.unit = menuItem.units[j].title;
					break; //only one radio can be checked...
				}
			}
			
			f.comments = document.getElementById('item_0_comments').value;
			if('Add comments here'==f.comments)
			{
				f.comments = '';	
			}
			f.type = entryItem.type;
			
			_order.entries.push( f );
		}

	}
	else if('reservationMenuItem'==entryItem.type)
	{
		var menuEntryID = entryItem.menuEntryID;
		var itemGroupID = entryItem.itemGroup.itemGroupID;
		var menuItemID = entryItem.menuItemID;
		var itemGroup = entryItem.itemGroup;
		
		var menuItem = oMenu.getMenuItem(menuEntryID, itemGroupID, menuItemID); //it will be easier to call properties than do lots of loops. clearer, anyway...

		for(var i=0; i<entryCount;i++){ //loop each entry, then push it onto _order.entries[]
			f = new entry(); 
			f.menuEntryID = menuEntryID;
			f.itemGroupID = itemGroupID;
			f.menuItemID = menuItemID;
			f.title = menuItem.title;
		
			//which unit did they choose?
			var p = document.getElementsByName('item_' + i + '_price'); //does byName solve the problem?
	
			for(var j=0;j<p.length;j++){
				if(p[j].checked){
					f.comments = menuItem.units[j].title;
					f.title += ': ' + menuItem.units[j].title;
					break; //only one radio can be checked...
				}
			}
		
			//which reservation day is chosen?
			var slotDay = document.getElementById('pickDay')[document.getElementById('pickDay').selectedIndex].value;
			
			//which timeslot?
			var tBegin = parseFloat(itemGroup.timeBegin);
			var period = parseInt(itemGroup.period);
			var ts = document.getElementsByName('slot' + slotDay );
			for(var j=0;j<ts.length;j++)
			{
				if(ts[j].checked)
				{
					if(document.getElementById('slot' + slotDay + '_' + j + 'count').selectedIndex)
					{
						f.count = document.getElementById('slot' + slotDay + '_' + j + 'count')[document.getElementById('slot' + slotDay + '_' + j + 'count').selectedIndex].value;				
						f.reservation = slotDay + ' ' + tBegin;
						break;
					}
					else
					{
						alert("Please choose how many items you want for this time.");
						return;
					}
				}
				tBegin += (period/60);				
			}
			
		
			f.price = menuItem.units[0].price; //p[j].value;
			f.unit = menuItem.units[0].title;
			
			var com = document.getElementById('item_0_comments').value;
			if('Add comments here'!=com)
			{
				f.comments += com;	
			}
			f.type = entryItem.type;
			
			_order.entries.push( f );
		}	
	}
	else if('itemGroup'==entryItem.type) //show the items of the group //called from a substitutable itemgroup
	{
		//substitutable, discount
		var menuEntryID = entryItem.menuEntryID;
		var itemGroupID = entryItem.itemGroup.itemGroupID;
		var menuEntry = oMenu.getMenuEntry( menuEntryID );
		var itemGroup = entryItem.itemGroup; //oMenu.getItemGroup(menuEntryID, itemGroupID );

		var i, j,k,p;
		var mic, mi, totalItemCount=0, itemCount;
		var oec, oe;
		//check _order.entries to see if theis itemGroup has been ordered already -- will affect discount
		oec = _order.entries.length;
		for(i=0;i<oec;i++){
			oe = _order.entries[i];
			if(menuEntryID==oe.menuEntryID && itemGroupID==oe.itemGroupID)
			{
				totalItemCount += parseInt( oe.count );	
			}
		}
		
		//each item in the itemGroup
		mic = itemGroup.items.length;
		for(i=0; i<mic;i++){ //loop each entry, then push it onto _order.entries[]
			
			menuItem = itemGroup.items[i];
			
			el = document.getElementById('item_' + i + '_count');
			itemCount = el[el.selectedIndex].value; //how many with these preferences?
			
			//discount calculation
			f = new entry();
			while( (itemGroup.discountThresh>totalItemCount) && (0<itemCount)) //all items up to discountThresh get defaultPrice
			{
				f.count++;
				f.price = itemGroup.defaultPrice;
				totalItemCount++;
				itemCount--;
				
			}
			if(f.count){
				f.unit = menuItem.title;
				f.menuEntryID = menuEntryID;
				f.itemGroupID = itemGroupID;
				f.menuItemID = menuItem.menuItemID;
				f.title = menuItem.title;
				
				f.comments = document.getElementById('item_0_comments').value;
				if('Add comments here'==f.comments)
				{
					f.comments = '';	
				}
				
				//reservations on the menuEntry?
				if( 'allow|allowDay|allowDayHour|requireDay|requireDayHour'.indexOf( menuEntry.reservationType ) > -1)
				{
					var slotDay = document.getElementById('pickDay')[document.getElementById('pickDay').selectedIndex].value;
				
				
					for(var j=0;j<menuEntry.reservations.length;j++)
					{
					//which timeslot?
						var tBegin = parseFloat(menuEntry.reservations[j].timeBegin);
						var period = parseInt(menuEntry.reservations[j].period);
						var ts = document.getElementsByName('slot' + slotDay );
						for(var k=0;k<ts.length;k++)
						{
							if(ts[k].checked)
							{
								f.reservation = slotDay + ' ' + tBegin;
								break;
							}
							tBegin += (period/60);				
						}
					}
				}
				
				f.type = entryItem.type;
				_order.entries.push( f );	
			}
			//remaining items will be discounted
			f = new entry();
			while( (itemGroup.discountThresh<=totalItemCount) && (0<itemCount)) //all items up to discountThresh get defaultPrice
			{
				f.count++;
				f.price = itemGroup.discountPrice;
				totalItemCount++;
				itemCount--;
				
			}
			if(f.count){
				f.unit = menuItem.title;
				f.menuEntryID = menuEntryID;
				f.itemGroupID = itemGroupID;
				f.menuItemID = menuItem.menuItemID;				
				f.title = menuItem.title;
				
				f.comments = document.getElementById('item_0_comments').value;
				if('Add comments here'==f.comments)
				{
					f.comments = '';	
				}
				//reservations on the menuEntry?
				if( 'allow|allowDay|allowDayHour|requireDay|requireDayHour'.indexOf( menuEntry.reservationType ) > -1)
				{
					var slotDay = document.getElementById('pickDay')[document.getElementById('pickDay').selectedIndex].value;
				
				
					for(var j=0;j<menuEntry.reservations.length;j++)
					{
					//which timeslot?
						var tBegin = parseFloat(menuEntry.reservations[j].timeBegin);
						var period = parseInt(menuEntry.reservations[j].period);
						var ts = document.getElementsByName('slot' + slotDay );
						for(var k=0;k<ts.length;k++)
						{
							if(ts[k].checked)
							{
								f.reservation = slotDay + ' ' + tBegin;
								break;
							}
							tBegin += (period/60);				
						}
					}
				}
				f.type = entryItem.type;
				_order.entries.push( f );	
			}
		}
	}
	else if('menuEntry'==entryItem.type) //the whole entry! //called from a dinner
	{
		var menuEntry = entryItem.menuEntry;// oMenu.getMenuEntry(menuEntryID);

		var i, j,k,p;
		var igc, ig, mic, mi, totalItemCount=0, itemCount;
		var oec, oe;
		var f = new entry();

		f.menuEntryID = menuEntry.menuEntryID;		
		f.comments = document.getElementById('item_0_comments').value;
		if('Add comments here'==f.comments)
		{
			f.comments = '';	
		}
		f.title = menuEntry.title;
		f.price = menuEntry.defaultPrice;
		
		//reservations on the menuEntry?
		if( 'allow|allowDay|allowDayHour|requireDay|requireDayHour'.indexOf( menuEntry.reservationType ) > -1)
		{
			var slotDay = document.getElementById('pickDay')[document.getElementById('pickDay').selectedIndex].value;
			for(var j=0;j<menuEntry.reservations.length;j++)
			{
			//which timeslot?
				var tBegin = parseFloat(menuEntry.reservations[j].timeBegin);
				var period = parseInt(menuEntry.reservations[j].period);
				var ts = document.getElementsByName('slot' + slotDay );
				for(var k=0;k<ts.length;k++)
				{
					if(ts[k].checked)
					{
						f.reservation = slotDay + ' ' + tBegin;
						break;
					}
					tBegin += (period/60);				
				}
			}
		}
		
		
		
		igc = menuEntry.itemGroups.length;
		for(i=0;i<igc;i++)
		{
			ig = menuEntry.itemGroups[i];
			
			if('included'==ig.groupType){			
				//nothing to pick!			
			}			   
			else if('pickFree'==ig.groupType){
				//only pick
				mic = ig.items.length;
				for(j=0;j<mic;j++)
				{
					mi = ig.items[j];
					
					el = document.getElementById('group' + ig.itemGroupID + 'item_' + j);
					if(el.checked)
					{
						f.additional.push( 
								{ itemGroupID:ig.itemGroupID
									,menuItemID:mi.menuItemID
									,title:mi.title
									,price:0								
								}
							);
					}					
				}
			}					
			else if('pickCharge'==ig.groupType){
				mic = ig.items.length;
				for(j=0;j<mic;j++)
				{
					mi = ig.items[j];
					
					el = document.getElementById('group' + ig.itemGroupID + 'item_' + j);
					if(el.checked)
					{
						f.additional.push( 
								{ itemGroupID:ig.itemGroupID
									,menuItemID:mi.menuItemID
									,title:mi.title
									,price:mi.units[0].price							
								}
							);
					}					
				}
			}					
			else if('pickChargeUnlimited'==ig.groupType){
				mic = ig.items.length;
				for(j=0;j<mic;j++)
				{
					mi = ig.items[j];
					
					el = document.getElementById('group' + ig.itemGroupID + 'item_' + j);
					if(el.checked)
					{
						f.additional.push( 
								{ itemGroupID:ig.itemGroupID
									,menuItemID:mi.menuItemID
									,title:mi.title
									,price:mi.units[0].price									
								}
							);
					}					
				}
			}
		}
		f.type = entryItem.type;
		f.count = 1;
		_order.entries.push( f );
	
	}

	
//	var _order = {entryItem:{itemID:0}, entries:[], entryCount:0 };


	//clean up and hide the dialog box
	hideDialog('dlgOrderItems');
	_order.entryCount =0;
	_order.entryItem = {};
	document.getElementById('itemEntries').innerHTML = '';

	displayOrder();	

}

function cancelOrderItems(){
	hideDialog('dlgOrderItems');
	
	_order.entryCount =0;
	_order.entryItem = {};

	document.getElementById('itemEntries').innerHTML = '';
}



function removeEntry(i){
	_order.entries.splice(i,1);
	displayOrder();
}

function displayOrder(){

	var iEntriesCount = _order.entries.length;

	var billTotal=0;
	var menuItem;
	var mainSubTotal=0, extraSubTotal=0;
	
	var s='<table>';
	for(var i=0;i<iEntriesCount;i++)
	{ //each entry
		oEntry = _order.entries[i];
		
		
		if('menuItem'==oEntry.type || 'itemGroup'==oEntry.type) //just show the item 
		{
			mainSubTotal = oEntry.count * oEntry.price;
			extraSubTotal =0;		
			
			s += '<tr>';
			s += '<td><a href="javascript:removeEntry(' + i + ');" style="text-decoration:none;">(-)</a></td>';
			s += '<td class="entryCount">' + oEntry.count + '</td>';
			s += '<td class="entryName">' + oEntry.title + '<br/>';
			if('' != oEntry.reservation)
			{
				var resDate = new Date().newDateFromMySQLDate( oEntry.reservation );
				s += resDate.getShortDateName() + ', ' + resDate.getShortMonthName() + ' ' + resDate.getDate() + ' at ' + resDate.getAMPMHours() + resDate.getAMPM() + ' ' + resDate.getNoon();  
			}
			s += '</td>'; //\n2 burger
			s += '<td class="entryPrice">@ $' + oEntry.price + '</td>';
			if(''!=oEntry.comments){
				s += '<td/></tr>';	
				s += '<tr>';
				s += '<td colspan="4" class="entryComment">' + oEntry.comments + '</td>';
				s += '<td />';
				s += '</tr>';
				s += '<tr><td colspan="4" /><td class="entrySubTotal">=$' + (parseFloat(mainSubTotal) + parseFloat(extraSubTotal)) + '</td></tr>';
			}
			else{
				s += '<td class="entrySubTotal">=$' + (parseFloat(mainSubTotal) + parseFloat(extraSubTotal)) + '</td></tr>';
			}
		}
		else if('reservationMenuItem'==oEntry.type)
		{
			mainSubTotal = oEntry.count * oEntry.price;
			extraSubTotal =0;		
			
			s += '<tr>';
			s += '<td><a href="javascript:removeEntry(' + i + ');" style="text-decoration:none;">(-)</a></td>';
			s += '<td class="entryCount">' + oEntry.count + '</td>';
			s += '<td class="entryName">' + oEntry.title + '<br/>';
			var resDate = new Date().newDateFromMySQLDate( oEntry.reservation );
			s += resDate.getShortDateName() + ', ' + resDate.getShortMonthName() + ' ' + resDate.getDate() + ' at ' + resDate.getAMPMHours() + resDate.getAMPM() + ' ' + resDate.getNoon();
			
			s += '</td>'; //\n2 burger
			s += '<td class="entryPrice">@ $' + oEntry.price + '</td>';
			
			if(''!=oEntry.comments){
				s += '<td/></tr>';	
				s += '<tr>';
				s += '<td colspan="4" class="entryComment">' + oEntry.comments + '</td>';
				s += '<td />';
				s += '</tr>';
				s += '<tr><td colspan="4" /><td class="entrySubTotal">=$' + (parseFloat(mainSubTotal) + parseFloat(extraSubTotal)) + '</td></tr>';
			}
			else{
				s += '<td class="entrySubTotal">=$' + (parseFloat(mainSubTotal) + parseFloat(extraSubTotal)) + '</td></tr>';
			}
		}
		else if('menuEntry'==oEntry.type)
		{
			var me = oMenu.getMenuEntry( oEntry.menuEntryID );
	
			s += '<tr>';
			s += '<td><a href="javascript:removeEntry(' + i + ');" style="text-decoration:none;">(-)</a></td>';
			s += '<td class="entryCount">' + oEntry.count + '</td>';
			s += '<td class="entryName">' + oEntry.title ;
			if('' != oEntry.reservation)
			{
				var resDate = new Date().newDateFromMySQLDate( oEntry.reservation );
				s += '<br/>' + resDate.getShortDateName() + ', ' + resDate.getShortMonthName() + ' ' + resDate.getDate() + ' at ' + resDate.getAMPMHours() + resDate.getAMPM() + ' ' + resDate.getNoon();  
			}
			s += '</td>';
			
			s += '<td class="entryPrice">@ $' + oEntry.price + '</td>';
			s += '<td />';
			s += '</tr>';	
			
			mainSubTotal = oEntry.count * oEntry.price;
			extraSubTotal =0;		

			//itemgroups are in additional!
			var add, addc = oEntry.additional.length;
			var inc = [];
			var chg = [];
			var h='';
			for(j=0;j<addc;j++)
			{
				add = oEntry.additional[j];
				
				if(0==add.price)
				{
					inc.push( add.title );	
				}
				else
				{
					h = '<tr>';
					h += '<td/>';
					h += '<td />'; //h += '<td class="entryCount">' + oEntry.count + '</td>';
					h += '<td class="entryName">' + add.title + '</td>'; //\n2 burger
					h += '<td class="entryPrice">@ $' + add.price + '</td>';
					h += '<td />';
					h += '</tr>';
					chg.push( h );
					extraSubTotal += parseFloat(add.price);
				}
				
			}
			
			s += '<tr>';
			s += '<td/>';
			s += '<td />'; //h += '<td class="entryCount">' + oEntry.count + '</td>';
			s += '<td class="entryName">' + inc.join(', ') + '</td>';
			s += '<td class="entryPrice">included</td>';
			s += '<td />';
			s += '</tr>';
			
			s += chg.join('');

			if(''!=oEntry.comments){
				s += '<tr>';
				s += '<td colspan="4" class="entryComment">' + oEntry.comments + '</td>';
				s += '<td />';
				s += '</tr>';
			}
	
	//		extraSubTotal *= oEntry.count;
	
			s += '<tr><td colspan="4" /><td class="entrySubTotal">=$' + (parseFloat(mainSubTotal) + parseFloat(extraSubTotal)) + '</td></tr>';
		}
	
		billTotal += (mainSubTotal + extraSubTotal);

	}
	//var tax = 0.095 * billTotal;
	var tax = taxRate * billTotal;
	var grandTotal = parseFloat(billTotal) + parseFloat(tax);
	s += '<tr><td colspan="3" class="billTotal" align="right">SUBTOTAL: </td><td colspan="2" class="billTotal" align="left">$' + billTotal.toFixed(2) + '</td></tr>';
	s += '<tr><td colspan="3" class="billTotal" align="right">TAX @ ' + taxRate*100 + '%: </td><td colspan="2" class="billTotal" align="left">$' + tax.toFixed(2) + '</td></tr>';
	s += '<tr><td colspan="3" class="billTotal" align="right">TOTAL: </td><td colspan="2" class="billTotal" align="left">$' + grandTotal.toFixed(2) + '</td></tr>';
	s += '</table>';

	document.getElementById('orderEntries').innerHTML = s;

	showDialog('dlgOrder'); //show the Order box
	doScrolls(); // position the Order box
}

function placeOrder(){
	//build the reservation
	
	
	showDialog('dlgCustInfo');
	
}

function doOrder(){
	var o={};
	o.mainMenuID = oMenu.entries[0].menuID; //any of the entries has the menuID
	o.name = document.getElementById('custName').value;	
	o.phone = document.getElementById('custPhone').value;
	o.email = document.getElementById('custEmail').value;
	o.address = document.getElementById('custAddress').value;
	
	var pm = document.getElementsByName('paymentMethod');

	for(var i=0;i<pm.length;i++)
	{
		if(pm[i].checked)
		{
			o.payMethod = pm[i].value;	
		}
	}

	for(var k in o){if(o.hasOwnProperty(k)){
		if(''==o[k]){
			alert("Please tell us your " + k + " information.");
			document.getElementById('cust' + k.charAt(0).toUpperCase() + k.substr(1) ).focus();
			return;
		}	
	}}

	o.orderEntries = _order.entries;
	o.action = 'placeOrder';

    var oX = new oXML();
	oX.xmlhttp.onreadystatechange = function(){
	  if( (oX.xmlhttp.readyState == 4) && (oX.xmlhttp.status == 200) ){ // do something with the results
	  
	    _sOrderID = oX.xmlhttp.responseText;

		if('paypal'==o.payMethod)
		{
			alert("Your order has been placed.\nYour Order Number is:\n\n" + _sOrderID + "\n\nAn email with your order details has been sent to your account.\n\nPlease note: Orders are not filled until payment has been made.\n\nClick OK to continue to PayPal to make your payment.");
			
			buildPayPalOrder();
			showPayPalRedirect();
		}
		else
		{
			document.getElementById('dlgChoosePayMethod_OrderNum').innerHTML = _sOrderID;
			showDialog('dlgChoosePayMethod');
		}
	  }
    }
    oX.request("POST","processorder.php", "jsn="+ o.toJSONString() );

	hideDialog('dlgCustInfo');

//	document.getElementById('jsn').value = o.toJSONString();
//	document.forms[0].submit();

}

function cancelOrder(){
	if(window.confirm("Click OK to return to your order.") ){

		hideDialog('dlgCustInfo');
		//clear the cust info
		document.getElementById('custName').value = '';
		document.getElementById('custAddress').value = '';
		document.getElementById('custPhone').value = '';
		document.getElementById('custEmail').value = '';
		
		displayOrder();
	}
}

function clearPlate(){
	if(window.confirm("Click OK to remove all entries from your list and start over.") ){
		clearOrder();
		
		hideDialog('dlgOrder');
		
	}
}

function clearOrder(){
	_order.entries = [];
	location.href = _clearOrderHref;
}

//PAYPAL
var _sOrderID='';
function buildPayPalOrder(){

	var iEntriesCount = _order.entries.length;
	
	var billTotal=0;
	var menuItem;
	var mainSubTotal=0, extraSubTotal=0;
	
	for(var i=0;i<iEntriesCount;i++)
	{ //each entry
		oEntry = _order.entries[i];
		
		
		if('menuItem'==oEntry.type || 'itemGroup'==oEntry.type || 'reservationMenuItem'==oEntry.type) //just show the item 
		{
			mainSubTotal = oEntry.count * oEntry.price;
		}
		else if('menuEntry'==oEntry.type)
		{
			extraSubTotal =0;		

			//itemgroups are in additional!
			var add, addc = oEntry.additional.length;
			for(j=0;j<addc;j++)
			{
				add = oEntry.additional[j];
				if(0!=add.price)
				{
					extraSubTotal += parseFloat(add.price);
				}
			}
			
			extraSubTotal *= oEntry.count;
			mainSubTotal = oEntry.count * oEntry.price;
		}
	
		billTotal += (mainSubTotal + extraSubTotal);
	}
	
	var	inp = document.createElement("input");
	var frm = document.getElementById("postf");
	
	//item name
	inp.setAttribute("name","item_name_1");
	inp.setAttribute("value","Yats Online Order " + _sOrderID);
	inp.setAttribute("type","hidden");
	frm.appendChild(inp);

	//price
	inp = document.createElement("input");
	inp.setAttribute("name","amount_1");
	inp.setAttribute("value", billTotal.toFixed(2) );
	inp.setAttribute("type","hidden");
	frm.appendChild(inp);

	//tax amount
	//var tax = 0.095 * billTotal;
	var tax = taxRate * billTotal;
	inp = document.createElement("input");
	inp.setAttribute("name","tax_1");
	inp.setAttribute("value", tax.toFixed(2) );
	inp.setAttribute("type","hidden");
	frm.appendChild(inp);

	frm.submit();
}

function showPayPalRedirect(){
	var ph =document.getElementById('pageholder');
	ph.style.textalign='center';
	ph.style.width='25%';

	ph.innerHTML='<br /><br /><br /><br />Redirecting to PayPal.com...';
}


function showDialog(dl){
//	document.getElementById(dl).style.display = "inline";
	var oDlg = document.getElementById(dl);
	
	oDlg.style.display = "inline";
	
	objh = parseInt(oDlg.style.height)/2;
	objw = parseInt(oDlg.style.width)/2;

//	objT = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
	objT = Math.floor(Math.round(250+document.documentElement.scrollTop)-objh)+'px';

	objL = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';
	
	oDlg.style.top = objT;

	oDlg.style.left = objL;

	
}

function hideDialog(dl){
	document.getElementById(dl).style.display = "none";
}

function doScrolls(){
/*
	var viewTop = Math.round(200+document.documentElement.scrollTop);
	var viewLeft = document.documentElement.scrollLeft;
	var screenHorizMiddle = Math.round(document.documentElement.offsetWidth/2) + viewLeft;
*/
	scrollOrder();

}

function scrollOrder(){
	var viewTop = document.documentElement.scrollTop;
	var viewLeft = document.documentElement.scrollLeft;
	var leftEdge = document.documentElement.offsetWidth;
	var screenHorizMiddle = Math.round(document.documentElement.offsetWidth/2) + viewLeft;

	var d = document.getElementById('dlgOrder');
	d.style.top = (viewTop + 100) + 'px';
	d.style.left = (leftEdge - (20 + parseInt(d.style.width))) + 'px';

}

window.onscroll = doScrolls;
window.onresize = doScrolls;
function afterOnload(){

	scrollOrder();

}

