BookingControl = Class.create();
BookingControl.serviceURL = "/resource/application/service/booking.php";
BookingControl.seatingBookingURL = "/resource/page-component/seating-booking.php?Submit=New";
BookingControl.seatingBookingStage2URL = "/resource/page-component/seating-booking-stage-2.php";
BookingControl.seatingBookingStage3URL = "/resource/page-component/seating-booking-stage-3.php";
BookingControl.seatingBookingStageFinalURL = "/resource/page-component/seating-booking-stage-final.php";

BookingControl.prototype = {
	initialize: function(element) {
		this.element = $(element);
		this.bookingDate = new Date();
	},

	loadBookingInclude: function(seatingAreaId, bookingDate) {
		this.bookingDate = bookingDate;
		this.seatingAreaId = seatingAreaId
		if ($('background-screen')) {
			$('background-screen').onclick = null;
		}
		new Ajax.Request(BookingControl.seatingBookingURL, {
			method: "get",
			parameters: {
				SeatingAreaId: this.seatingAreaId,
				BookingDate: this.bookingDate.getFullYear() + "-" + (this.bookingDate.getMonth() + 1) + "-" + this.bookingDate.getDate()
			},
			onSuccess: (function(response) {
				this.displayBookingForm(response.responseText);
			}).bind(this)
		});		
		return true;
	},

	loadBookingStage2Include: function() {
		new Ajax.Request(BookingControl.seatingBookingStage2URL, {
			method: "get",
			parameters: {
				SeatingAreaId: this.seatingAreaId,
				BookingDate: this.bookingDate.getTime()
			},
			onSuccess: (function(response) {
				this.displayBookingFormStage2(response.responseText);
			}).bind(this)
		});		
		return true;
	},

	loadBookingStage3Include: function(foodString) {
		this.foodString = foodString;
		this.bookingFormElementStage2.hide();	
		new Ajax.Request(BookingControl.seatingBookingStage3URL, {
			method: "get",
			parameters: {
				SeatingAreaId: this.seatingAreaId,
				BookingDate: this.bookingDate.getTime()
			},
			onSuccess: (function(response) {
				this.displayBookingFormStage2(response.responseText);
			}).bind(this)
		});
		return true;
	},

	loadBookingStageFinalInclude: function(drinkString) {
		this.drinkString = drinkString;
		this.bookingFormElementStage2.hide();	
		new Ajax.Request(BookingControl.seatingBookingStageFinalURL, {
			method: "get",
			parameters: {
				SeatingAreaId: this.seatingAreaId,
				BookingDate: this.bookingDate.getTime(),
				DrinkOrder: this.drinkString,
				FoodOrder: this.foodString,
				FormData: this.serializedForm				
			},
			onSuccess: (function(response) {
				this.displayBookingFormStage2(response.responseText);
			}).bind(this)
		});
		return true;
	},

	displayBookingForm: function(seatingAreaHtml) {		
		this.bookingBackground = $("background-screen");
		if (!this.bookingBackground) {
			this.bookingBackground = document.createElement("div");
			this.bookingBackground.id = "background-screen";
			this.bookingBackground.className = "booking-background";
			Element.extend(this.bookingBackground);
			document.body.appendChild(this.bookingBackground);
		}
		this.bookingFormElement = document.createElement("div");
		Element.extend(this.bookingFormElement);
		document.body.appendChild(this.bookingFormElement);				
		this.bookingFormElement.style.zIndex = 1001;
		this.bookingFormElement.id = "full-seating-area";
		this.bookingFormElement.className = "above-content";
		this.bookingFormElement.innerHTML = seatingAreaHtml;
	},

	validateBooking: function(clickElement) {
		var bookingFormElement = $("booking-form");
		this.serializedForm = bookingFormElement.serialize();		
		new Ajax.Request(BookingControl.serviceURL, {
			method: "get",
			parameters: {
				Action: "Validate",
				SeatingAreaId: this.seatingAreaId,
				BookingDate: this.bookingDate,
				FormData: this.serializedForm
			},
			onSuccess: (function(response) {
				var response = response.responseText.evalJSON();
				if (response.success) {
					this.bookingFormElement.hide();
					this.loadBookingStage2Include();
				} else {
					var errors = response.errors.split(",");	
					var bookingErrorsElement = $("booking-errors");
					var returnedErrorElement = $("returned-errors");
					returnedErrorElement.innerHTML = "";
					errorList = document.createElement("ul");
					returnedErrorElement.appendChild(errorList);
					errors.each(function(error) {
						var errorListItem = document.createElement("li");
						errorListItem.innerHTML = error;
						errorList.appendChild(errorListItem);
					});
					bookingErrorsElement.show();			
				}
			}).bind(this)
		});
	},

	displayBookingFormStage2: function(seatingAreaHtml) {
		this.bookingBackground = $("background-screen");

		if (!this.bookingBackground) {
			this.bookingBackground = document.createElement("div");
			this.bookingBackground.id = "background-screen";
			this.bookingBackground.className = "booking-background";

			Element.extend(this.bookingBackground);

			document.body.appendChild(this.bookingBackground);
		}

		this.bookingFormElementStage2 = document.createElement("div");
		Element.extend(this.bookingFormElementStage2);
		document.body.appendChild(this.bookingFormElementStage2);				
		this.bookingFormElementStage2.style.zIndex = 1001;

		this.bookingFormElementStage2.id = "full-seating-area";
		this.bookingFormElementStage2.className = "above-content";
		this.bookingFormElementStage2.innerHTML = seatingAreaHtml;

		$A(this.bookingFormElementStage2.getElementsByTagName("script")).each(function(s) {
			eval(s.text);
		});
	},

	makeBooking: function() {
		new Ajax.Request(BookingControl.serviceURL, {
			method: "get",
			parameters: {
				Action: "Book",
				SeatingAreaId: this.seatingAreaId,
				BookingDate: this.bookingDate,
				FormData: this.serializedForm,
				DrinkOrder: this.drinkString,
				FoodOrder: this.foodString
			},
			onSuccess: (function(response) {
				var response = response.responseText.evalJSON();
				if (response.success) {		
					window.location = "/booking/thanks.php";
				} else {
					alert("Sorry, there has been an error with your form. Please try again later.");			
				}
			}).bind(this)
		});		
	},

	cancel: function() {
		if (this.bookingBackground != undefined) {
			this.bookingBackground.remove();
		}

		if (this.bookingFormElement != undefined) {
			this.bookingFormElement.remove();
		}
		
		if (this.bookingFormElementStage2 != undefined) {
			this.bookingFormElementStage2.hide();
		}
	}	
}