

	var bookings = new BookingList();
	var roomTypes = new Array();
	var currentMonth;
	var currentYear;

	var daysArray = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	var monthsArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

	function BookingList()
	{
		this.rooms = new Array();

		this.addBooking = function(type, adults, children)
		{
			room = new Object();
			room.id = zeroPad(Math.floor(Math.random()*9999), 4);
			room.type = type;
			room.adults = adults;
			room.children = children;
			room.dates = new Array();

			var keys = new Array();
			for(key in testcal.selectedDates)
			{
				 room.dates[key] = testcal.selectedDates[key];
			}

			this.rooms.push(room);
			this.updateDisplay();
		}

		this.updateBooking = function(id, thing)
		{
			for (roomLoop = 0; roomLoop < this.rooms.length; roomLoop++)
			{
				if (this.rooms[roomLoop].id == id)
				{
					var currentType = thing.getAttribute("name");

					if (currentType == "adults")
					{
						this.rooms[roomLoop].adults = thing.value;
					}
					else if (currentType == "children")
					{
						this.rooms[roomLoop].children = thing.value;
					}

					this.updateDisplay();
					break;
				}
			}
		}

		this.deleteBooking = function(id)
		{
			for (roomLoop = 0; roomLoop < this.rooms.length; roomLoop++)
			{
				if (this.rooms[roomLoop].id == id)
				{
					this.rooms.splice(roomLoop, 1);
					this.updateDisplay();
					break;
				}
			}
		}

		this.updateDisplay = function()
		{
			var lowerContentList = document.getElementById("lowerContent");

			this.clearDisplay(lowerContentList);

			if (this.rooms.length > 0)
			{
				newRow = document.createElement("tr");

					newCell = document.createElement("th");
					newCell.innerHTML = "Room Type";
					newRow.appendChild(newCell);

					newCell = document.createElement("th");
					newCell.innerHTML = "Dates";
					newRow.appendChild(newCell);

					newCell = document.createElement("th");
					newCell.innerHTML = "Adults";
					newRow.appendChild(newCell);

					newCell = document.createElement("th");
					newCell.innerHTML = "Children";
					newRow.appendChild(newCell);

					newCell = document.createElement("th");
					newCell.innerHTML = "Price";
					newRow.appendChild(newCell);

					newCell = document.createElement("th");
					newCell.innerHTML = "";
					newRow.appendChild(newCell);

				lowerContentList.appendChild(newRow);

				grandTotal = 0;
				
				for (lineLoop = 0; lineLoop <= this.rooms.length; lineLoop++)
				{
					newRow = document.createElement("tr");

					if (lineLoop == this.rooms.length)
					{
						newCell = document.createElement("td");
						newCell.setAttribute("colspan", "3");
						newCell.innerHTML = "&nbsp;";
						newRow.appendChild(newCell);

						newCell = document.createElement("td");
						newCell.style.textAlign = "right";
						newCell.innerHTML = "Total";
						newRow.appendChild(newCell);
						
						newCell = document.createElement("td");
						newCell.innerHTML = "&pound;" + grandTotal;
						newRow.appendChild(newCell);

						newCell = document.createElement("td");
						newCell.innerHTML = "&nbsp;";
						newRow.appendChild(newCell);

						lowerContentList.appendChild(newRow);
						newRow = document.createElement("tr");

						newCell = document.createElement("td");
						newCell.setAttribute("colspan", "6");
						newCell.style.textAlign = "right";
						newCell.innerHTML = "<a href='#' alt''><img src='/skins/boscomesparresorthotels/resources/booknow.png' alt='' /></a>";
						newRow.appendChild(newCell);
					}
					else
					{
						newCell = document.createElement("td");
						newCell.innerHTML = this.rooms[lineLoop].type;
						newRow.appendChild(newCell);

						newCell = document.createElement("td");
						newCell.innerHTML = generateDateString(this.rooms[lineLoop].dates);
						newRow.appendChild(newCell);

						newCell = document.createElement("td");
						
						temp = "<select name=\"adults\" onchange=\"bookings.updateBooking('" + this.rooms[lineLoop].id + "', this)\">";

						for (optionLoop = 0; optionLoop <= 5; optionLoop++)
						{
							temp += "<option value='" + optionLoop + "'";
							if (this.rooms[lineLoop].adults == optionLoop)
							{
								temp += " selected";
							}
							temp += ">" + optionLoop + "</option>";
						}

						temp += "</select>";
						newCell.innerHTML = temp;

						newRow.appendChild(newCell);

						newCell = document.createElement("td");
						temp = "<select name=\"children\" onchange=\"bookings.updateBooking('" + this.rooms[lineLoop].id + "', this)\">";

						for (optionLoop = 0; optionLoop <= 5; optionLoop++)
						{
							temp += "<option value='" + optionLoop + "'";
							if (this.rooms[lineLoop].children == optionLoop)
							{
								temp += " selected";
							}
							temp += ">" + optionLoop + "</option>";
						}

						temp += "</select>";
						newCell.innerHTML = temp;
						newRow.appendChild(newCell);

						newCell = document.createElement("td");

						for (roomLoop = 0; roomLoop < roomTypes.length; roomLoop++)
						{
							if (roomTypes[roomLoop].type.toLowerCase() == this.rooms[lineLoop].type.toLowerCase())
							{
								currentPrices = roomTypes[roomLoop].prices;
								break;
							}
						}

						subTotal = (this.rooms[lineLoop].adults * currentPrices["Adult"] + this.rooms[lineLoop].children * currentPrices["Child"]);
						grandTotal += subTotal;

						newCell.innerHTML = "&pound;" + subTotal;
						newRow.appendChild(newCell);

						newCell = document.createElement("td");
						newCell.innerHTML = "<a href='javascript: bookings.deleteBooking(" + this.rooms[lineLoop].id + ")'>Remove</a>";
						newRow.appendChild(newCell);
					}

					lowerContentList.appendChild(newRow);
				}
			}
		}

		this.clearDisplay = function(listLocation)
		{
			while (listLocation.childNodes[0])
			{
				listLocation.removeChild(listLocation.childNodes[0]);
			}
		}
	}

	function importRooms(ref)
	{
		url = "dynamic/roomTypes.php?ref=" + ref;

		AjaxRequest.get(
		{
			"url": url, "onSuccess" : function(xmlhttp)
			{ 
				xmlDoc = xmlhttp.responseXML;
				var loadedRooms = xmlDoc.getElementsByTagName("room");
				var roomTypeList = document.getElementById("roomTypeList");
				clearChildNodes(roomTypeList);

				for (roomLoop = 0; roomLoop < loadedRooms.length; roomLoop++)
				{
					var room = new Object();
					room.type = loadedRooms[roomLoop].getElementsByTagName("roomtype")[0].childNodes[0].nodeValue;
					room.description = loadedRooms[roomLoop].getElementsByTagName("description")[0].childNodes[0].nodeValue;
					
					currentPrices = loadedRooms[roomLoop].getElementsByTagName("price");
					room.prices = new Array();

					for (priceLoop = 0; priceLoop < currentPrices.length; priceLoop++)
					{
						var priceType = currentPrices[priceLoop].getElementsByTagName("pricetype")[0].childNodes[0].nodeValue;
						var priceValue = currentPrices[priceLoop].getElementsByTagName("value")[0].childNodes[0].nodeValue;
						room.prices[priceType] = priceValue;
					}

					roomTypes.push(room);

					newItem = document.createElement("li");

					newContent = "<div style=\"float: left; width: 66%;\">" + room.type + " Room</div>";
					newContent += "<div style=\"float: left; width: 34%; text-align: right;\"><a href=\"javascript: bookings.addBooking('" + room.type.toLowerCase() + "', 0, 0);\" alt=\"\">Add Room</a></div>";
					newContent += "<div style=\"clear: both;\"></div><hr />";
					newContent += "<div style=\"float: left; width: 66%;\">" + room.description + "</div>";
					newContent += "<div style=\"float: left; width: 34%; text-align: right;\" class=\"small\">&pound;" + room.prices["Adult"] + " (Adult)<br />&pound;" + room.prices["Child"] + " (Child) </div>";
					
					newItem.innerHTML = newContent;
					roomTypeList.appendChild(newItem);
				}
			}
		});
	}
	
	function updateBookings()
	{
		var lowerContentList = document.getElementById("lowerContent");

		while (lowerContentList.childNodes[0])
		{
			lowerContentList.removeChild(lowerContentList.childNodes[0]);
		}

		for (lineLoop = 0; lineLoop < bookings.rooms.length; lineLoop++)
		{
			newRow = document.createElement("li");
			newRow.setAttribute("id", "bookingRow" + lowerContentList.childNodes.length);
			newRow.innerHTML = "(" + (lowerContentList.childNodes.length - 1) + ") Room Type: " + bookings.rooms[lineLoop].type + " Dates: " + generateDateString();

			lowerContentList.appendChild(newRow);
		}
	}

	function addTag(tag, content, attributes)
	{
		if (attributes == undefined)
		{
			attributes = "";
		}
		return "<" + tag + attributes + ">" + content + "</" + tag + ">";
	}

	function daysInMonth(iMonth, iYear)
	{
		return 32 - new Date(iYear, iMonth, 32).getDate();
	}

	function roundUp(num, acc)
	{
		return Math.ceil(Math.round(num)/acc)*acc;
	}

	function zeroPad(num, count)
	{
		var numZeropad = num + '';
		while(numZeropad.length < count) 
		{
			numZeropad = "0" + numZeropad;
		}
		return numZeropad;
	}

	function generateCellId(year, month, day)
	{
		return zeroPad(year, 2) + zeroPad(month, 2) + zeroPad(day, 2);
	}



	function updateRoomTypes(ref)
	{
		url = "dynamic/roomTypes.php?ref=" + ref;

		AjaxRequest.get(
		{
			"url": url, "onSuccess" : function(xmlhttp)
			{ 
				xmlDoc = xmlhttp.responseXML;
				var loadedRooms = xmlDoc.getElementsByTagName("room");
				var roomTypeList = document.getElementById("roomTypeList");
				clearChildNodes(roomTypeList);

				for (roomLoop = 0; roomLoop < loadedRooms.length; roomLoop++)
				{
					var room = new Object();
					room.type = loadedRooms[roomLoop].getElementsByTagName("roomtype")[0].childNodes[0].nodeValue;
					room.description = loadedRooms[roomLoop].getElementsByTagName("description")[0].childNodes[0].nodeValue;
					
					currentPrices = loadedRooms[roomLoop].getElementsByTagName("price");
					room.prices = new Array();

					for (priceLoop = 0; priceLoop < currentPrices.length; priceLoop++)
					{
						var priceType = currentPrices[priceLoop].getElementsByTagName("pricetype")[0].childNodes[0].nodeValue;
						var priceValue = currentPrices[priceLoop].getElementsByTagName("value")[0].childNodes[0].nodeValue;
						room.prices[priceType] = priceValue;
					}

					newItem = document.createElement("li");

					newContent = "<div style=\"float: left; width: 66%;\">" + room.type + " Room</div>";
					newContent += "<div style=\"float: left; width: 34%; text-align: right;\"><a href=\"javascript: bookings.addBooking('" + room.type.toLowerCase() + "', 0, 0);\" alt=\"\">Add Room</a></div>";
					newContent += "<div style=\"clear: both;\"></div><hr />";
					newContent += "<div>" + room.description + "</div>";
					
					newItem.innerHTML = newContent;
					roomTypeList.appendChild(newItem);
				}
			}
		});
	}

	function clearChildNodes(element)
	{
		if(element == null)
			return;

		while (element.childNodes[0])
		{
			element.removeChild(element.childNodes[0]);
		}
	}

	function sortNumber(a, b)
	{
		return ((a > b) - (a < b));
	}

	function sortArrayByKeys(array)
	{
		var keys = new Array();
		for(k in array)
		{
			 keys.push(k);
		}

		keys.sort(sortNumber);

		result = new Array();

		for (var i = 0; i < keys.length; i++)
		{
			 result[keys[i]] = array[keys[i]];
		}

		return result;			
	}

	function getDateFromId(id)
	{
		result = new Object()
		result.year = id.substr(0, 4);
		result.month = monthsArray[parseInt(id.substr(4, 2) - 1)].substr(0, 3);
		result.mon = parseInt(id.substr(4, 2));
		result.day = id.substr(6, 2);
		result.maxdays = daysInMonth(result.mon - 1, result.year);

		return result;
	}

	function addDaySuffix(day)
	{
		switch(day)
		{
			case "01":
				suffix = "st";
				break;
			case "21":
				suffix = "st";
				break;
			case "31":
				suffix = "st";
				break;
			case "02":
				suffix = "nd";
				break;
			case "22":
				suffix = "nd";
				break;
			case "03":
				suffix = "rd";
				break;
			case "23":
				suffix = "rd";
				break;
			default:
				suffix = "th";
		}

		if (day[0] == "0")
		{
			day = day[1];
		}

		return day + suffix;
	}

	function generateDateString(datesArray)
	{
		var htmlInsert = "";

		datesArray = sortArrayByKeys(datesArray);
		blockStart = "";
		blockEnd = "";

		blocks = new Array();

		lastDate = 0;

		for (date in datesArray)
		{
			specialCase = false;
			if (datesArray[date] != null)
			{
				dateObject = getDateFromId(date);

				if (lastDate != 0)
				{
					lastDateObject = getDateFromId(lastDate);

					nextmon = lastDateObject.mon + 1 == 13 ? 1 : lastDateObject.mon + 1;

					if (lastDateObject.day == lastDateObject.maxdays && nextmon == dateObject.mon && dateObject.day == "01")
					{ // if the block is over a month join flag it as a special link
						specialCase = true;
					}
				}

				if ((date - lastDate == 1) || specialCase == true)
				{
					blockEnd = date;
				}
				else
				{
					if (blockStart != "" && blockEnd != "")
					{
						block = new Object();
						block.start = blockStart;
						block.end = blockEnd;

						blocks.push(block);
					}

					blockStart = date;
					blockEnd = date;
				}

				lastDate = date;
			}	
		}

		block = new Object();
		block.start = blockStart;
		block.end = blockEnd;

		blocks.push(block);

		if (blocks[0].start != "")
		{
			for (blockLoop = 0; blockLoop < blocks.length; blockLoop++)
			{
				if (blocks[blockLoop].start == blocks[blockLoop].end)
				{// single date
					startDate = getDateFromId(blocks[blockLoop].start);
					htmlInsert += ", " + addDaySuffix(startDate.day) + " of " + startDate.month;
				}
				else
				{// block of dates
					startDate = getDateFromId(blocks[blockLoop].start);
					endDate = getDateFromId(blocks[blockLoop].end);

					if (startDate.month != endDate.month)
					{
						htmlInsert += ", " + addDaySuffix(startDate.day) + " of " + startDate.month + " - " + addDaySuffix(endDate.day) + " of " + endDate.month;
					}
					else
					{
						htmlInsert += ", " + addDaySuffix(startDate.day) + " - " + addDaySuffix(endDate.day) + " of " + endDate.month;
					}
				}
			}
		}

		return htmlInsert.substr(2, htmlInsert.length);
	}
