function updateCalendar(val)
	{
		if (bookingArr == null) {
			return false;
		}
		if (val == null) {
			// try to get id from select box
			var selectBoxVal = getSelectedRoomId();
			if (selectBoxVal != null) {
				val = selectBoxVal;
			}
			if (val == null) {
				// try to get id from url
				var params = window.location.search.substr(1).split('&');
				for (var i = 0; i < params.length; i++) {
					var param = params[i].split('=');
					if (param[0] == 'selected') {
						val = param[1];	
					}
				}
				
				if (val == null) return;
			}
		}
		
		if (val == 0) {
			// hide calendar
			document.getElementById('calendars').style.display = 'none';
			document.getElementById('calendarArea').style.display = 'none';
			document.getElementById('roomTitle').innerHTML = '';
			return false;
		} else {
			// Show calendar, ensure select box has the right option selected
			document.getElementById('calendars').style.display = 'block';
			document.getElementById('calendarArea').style.display = 'block';

			
			var selectBox = document.getElementById('roomSelect');
			var roomTitle = '';
			for (var i = 0; i < selectBox.options.length; i++) {
				if (selectBox.options[i].value == val) {
					roomTitle = selectBox.options[i].text;
				}
			}
			document.getElementById('roomTitle').innerHTML = roomTitle;
			selectBox.value = val;

			// show the surcharge descriptions
			var descBox = document.getElementById('surchargeDescBox');
			descBox.innerHTML = '';
			for (var i = 0; i < surchargeDescArr.length; i++) {
				if (val == surchargeDescArr[i]["roomId"]) {
					//var newDescStr = '<div>'+surchargeDescArr[i]["dateStr"]+': '+surchargeDescArr[i]["amount"]+'% surcharge applies</div>';
					var newDescStr = '<div>'+surchargeDescArr[i]["dateStr"]+': special period tariffs apply</div>';
					descBox.innerHTML += newDescStr;
				}
            }
		
			// Make sure the viewer can actually see the calendar	
			window.location.hash = 'calendars';
		}

		clearHighlights();
		highlightSurcharges(surchargeArr[val]);
		highlightBookings(bookingArr[val]);
		
		return true;
	}
	
	function highlightBookings(bookings)
	{
		
		for (bookingDate in bookings) {
			var dayTd = document.getElementById(bookings[bookingDate]);
			if (dayTd) {
				dayTd.className = 'dayBooked';
			}
		}

	}

	function highlightSurcharges(surcharges)
    {
		
        for (surchargeDate in surcharges) {
            var dayTd = document.getElementById(surcharges[surchargeDate]);
            if (dayTd) {
                dayTd.className = 'daySurcharged';
            }
        }
    }

	function clearHighlights()
	{
		var days = document.getElementsByTagName('td');
		for (var i = 0; i < days.length; i++) {
			var day = days[i];
			if (day.className == 'daySelected') {
				day.className = 'day';
			}
			if (day.className == 'dayBooked') {
				day.className = 'day';
			}
			if (day.className == 'daySurcharged') {
				day.className = 'day';
			}
		}
		startId = null;
		endId = null;
	}
	
	var startId = null;
	var endId = null;
	function dateClick(elId)
	{
		var success = setDates(elId);
		var roomId = getSelectedRoomId();
		if (success) {
			if (endId != null) {
				var sdVals = startId.split('-');
				var edVals = endId.split('-');
				var startDate = new Date(sdVals[0], sdVals[1]-1, sdVals[2]);
				var endDate = new Date(edVals[0], edVals[1]-1, edVals[2]);
				var startDateStr = getFriendlyDateString(startDate);
				var endDateStr = getFriendlyDateString(endDate);
				var numNights = getTotalNights(startDate, endDate);
				var proceed = true;
                var errorPos = null;
                for(var i = 0; (i < restrictions.length) && (proceed == true); i++) {
                    if (checkRestrictions(restrictions[i], startDate, endDate) == false) {
                        proceed = false;
                        errorPos = i;
                    }
                }
				if (!proceed) {
					// The attempted booking violates a restriction
                    alert(restrictions[errorPos][3]);
                    var roomId = document.getElementById('roomSelect').value;
                    updateCalendar(roomId);

				} else {
					var confirmMsg = 'Would You Like To Create A Booking? \n\n';
					confirmMsg += 'First Night:  '+startDateStr+'\n';
					confirmMsg += ' Last Night:  '+endDateStr+'\n';
					confirmMsg += '        Total:   '+numNights+' nights';
				
					if (confirm(confirmMsg,' ' ,'Create A Booking?')) {
						// set form vars then redirect to booking page
						document.getElementById('roomId').value = roomId;
						document.getElementById('startDate').value = startDateStr;
						document.getElementById('endDate').value = endDateStr;
						document.getElementById('numNights').value = numNights;

					 	// clear selection in case the user hits the back button
						updateCalendar();	
						document.getElementById('bookingForm').submit();
					} else {
						var roomId = document.getElementById('roomSelect').value;
						updateCalendar(roomId);
					}
				}
				
				// clear temp pointers			
				startId = null;
				endId = null;
			}
		} else {
			
			updateCalendar(roomId);
			alert("You cannot create a booking for those dates as the room is already booked out");
		}
	}

	function checkRestrictions(restriction, startDate, endDate) {
        var result = false;

        switch(restriction[0]) {
            case 'weekend':
                // do the booking dates fall on a weekend?
                var tempDate = new Date();
                tempDate.setTime(startDate.getTime());
                var bLength = 0;
                var hasWeekend = false;
                while (tempDate <= endDate) {
                    if (tempDate.getDay() > 4) { hasWeekend = true; }
                    bLength++;
                    tempDate.setDate(tempDate.getDate()+1);
                }
                if (hasWeekend) {
                    if (restriction[1] == 'min') {
                        if ((hasWeekend == true) && (bLength >= restriction[2])) {
                            result = true;
                        }
                    } else if (restriction[1] == 'max') {
                        if ((hasWeekend == true) && (bLength <= restriction[2])) {
                            result = true;
                        }
                    }
                } else {
                    result = true;
                }
                break;

            case 'weeknight':
                // do the booking dates fall on  weeknights
                var tempDate = new Date();
                tempDate.setTime(startDate.getTime());
                var bLength = 0;
                var hasWeeknight = false;
                while (tempDate <= endDate) {
                    if (tempDate.getDay() <= 4) { hasWeeknight = true; }
                    bLength++;
                    tempDate.setDate(tempDate.getDate()+1);
                }
                if (hasWeeknight) {
                    if (restriction[1] == 'min') {
                        if ((hasWeeknight == true) && (bLength >= restriction[2])) {
                            result = true;
                        }
                    } else if (restriction[1] == 'max') {
                        if ((hasWeekend == true) && (bLength <= restriction[2])) {
                            result = true;
                        }
                    }
                } else {
                    result = true;
                }
                break;
        }

        return result;
    }
	
	function setDates(elId) {
		if (startId == null) {
			startId = elId;
			var td = document.getElementById(startId);
			if (td.className == 'dayBooked') {
				return false;
			}
			td.className = 'daySelected';
			

		} else {
			endId = elId;
			var td = document.getElementById(endId);
			if (td.className == 'dayBooked') {
				return false;
			}
			// Ensure start date is before end date
			var sdVals = startId.split('-');
			var edVals = endId.split('-');
			var startDate = new Date();
			var endDate = new Date();
			startDate.setFullYear(sdVals[0],(sdVals[1]-1),sdVals[2]);
			endDate.setFullYear(edVals[0],(edVals[1]-1),edVals[2]);
			if (startDate.getTime() > endDate.getTime()) {
				tempId = startId;
				startId = endId;
				endId = tempId;	
			}
			
			var sdVals = startId.split('-');
			var edVals = endId.split('-');
			
			while (sdVals[0] <= edVals[0]) {
				
				if (sdVals[0] == edVals[0]) {
					while (sdVals[1] <= edVals[1]) {
						if (sdVals[1] == edVals[1]) {
							while (sdVals[2] <= edVals[2]){
								var currId = sdVals[0]+'-'+sdVals[1]+'-'+sdVals[2];
								var curr = document.getElementById(currId);
								if (curr) {
									if (curr.className == 'dayBooked') {
										return false;
									}
									curr.className = 'daySelected';
								}
								sdVals[2]++;
								if (sdVals[2] < 10) { sdVals[2] = '0'+sdVals[2]; }
							}
							sdVals[1]++;
						} else {
							// Calculate num days in month
							var tempDate = new Date();
							tempDate.setFullYear(sdVals[0], sdVals[1], 0);
							var daysInMonth = tempDate.getDate();
							while (sdVals[2] <= daysInMonth){
								var currId = sdVals[0]+'-'+sdVals[1]+'-'+sdVals[2];
								var curr = document.getElementById(currId);
								if (curr) {
									if (curr.className == 'dayBooked') {
										return false;
									}
									curr.className = 'daySelected';
								}
								sdVals[2]++;
								if (sdVals[2] < 10) { sdVals[2] = '0'+sdVals[2]; }
							}
							sdVals[2] = 1;
							sdVals[1]++;
							if (sdVals[1] < 10) { sdVals[1] = '0'+sdVals[1]; }
						}
					}
					sdVals[2] = 1;
					sdVals[1] = 1;
					sdVals[0]++;
				} else {
					if (sdVals[1] == edVals[1]) {
						while (sdVals[2] <= edVals[2]){
							var currId = sdVals[0]+'-'+sdVals[1]+'-'+sdVals[2];
							var curr = document.getElementById(currId);
							if (curr) {
								if (curr.className == 'dayBooked') {
									return false;
								}
								curr.className = 'daySelected';
							}
							sdVals[2]++;
						}
						sdVals[1]++;
					} else {
						// Calculate num days in month
						var tempDate = new Date();
						tempDate.setFullYear(sdVals[0], sdVals[1], 0);
						var daysInMonth = tempDate.getDate();
						while (sdVals[2] <= daysInMonth){
							var currId = sdVals[0]+'-'+sdVals[1]+'-'+sdVals[2];
							var curr = document.getElementById(currId);
							if (curr) {
								if (curr.className == 'dayBooked') {
									return false;
								}
								curr.className = 'daySelected';
							}
							sdVals[2]++;
						}
						sdVals[2] = 1;
						sdVals[1]++;
					}
				}
				
				sdVals[2] = 1;
				sdVals[1] = 1;
				sdVals[0]++;
				
			}
				
		
		}
		return true;
	}
	
	function getFriendlyDateString(dateObj) 
	{
		var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 
			'Thursday', 'Friday', 'Saturday'];
		var monthsOfYear = ['January', 'February', 'March', 'April', 'May', 
			'June', 'July', 'August', 'September', 'October', 
			'November', 'December'];
		
		var dateStr = daysOfWeek[dateObj.getDay()];
		dateStr += ' ' + monthsOfYear[dateObj.getMonth()];
		dateStr += ' ' + dateObj.getDate();
		dateStr += ', ' + dateObj.getFullYear();
		return dateStr;
	}
	
	function getTotalNights(startDate, endDate)
	{
		var tempDate = endDate.getTime() - startDate.getTime();
		return Math.floor((tempDate/86400000)+1);
	}
	
	function getSelectedRoomId()
	{
		selectBox = document.getElementById('roomSelect');
		if (selectBox) return selectBox.value;
		return 0;
	}
