//** freetext or area id logic **/
// triggered by clicking either the freetext field or the area dropdown
// sets the appropriate radio button to checked
function setAreaType(obj) {
	if (obj.name == "Area") {
		document.getElementById("specific").value = "";
	}else{
		document.getElementById("areaDropDown").value = "";
	}
}

/** number of rooms to show logic  **/
// triggered by changing the number of rooms
// will show the normal how many people dropdown for 1 room or individual room dropdowns where its more than 1
function showRooms(obj) {
	var numRooms = obj.value;

	var               roomCopy = document.getElementById('tooManyRooms');
	var              partyCopy = document.getElementById('bigParty');
	var  selfCateringPartyCopy = document.getElementById('selfCateringParty');

	var howManyPeople = document.getElementById('howManyPeople');

	var numberOfPeople = document.getElementById('numberOfPeople');

	if (numRooms == 1) {
		
		setDisplay(roomCopy,      'none');
		setDisplay(howManyPeople, 'block');

		for (var i = 1; i < 4; i++) {
			setDisplay(document.getElementById('room' + i), 'none');
		}

		if (isSet(numberOfPeople.options) && numberOfPeople.options.length == 20) {
			if (numberOfPeople.selectedIndex == 19) {
				setDisplay(selfCateringPartyCopy, 'block');
			} else {
				setDisplay(selfCateringPartyCopy, 'none');
			}
		} else {
			setDisplay(selfCateringPartyCopy, 'none');

			if (((numberOfPeople.value)*1) > 2) {
				setDisplay(partyCopy, 'block');
			} else {
				setDisplay(partyCopy, 'none');
			}
		}

	} else if(numRooms == '4+') {
		setDisplay(roomCopy,      'block');
		setDisplay(partyCopy,     'none');
		setDisplay(howManyPeople, 'none');
		for (var i=1; i<4; i++) {
			setDisplay(document.getElementById('room' + i), 'none');
		}
	} else {
		setDisplay(roomCopy,      'none');
		setDisplay(partyCopy,     'none');
		setDisplay(howManyPeople, 'none');
		for (var i=1; i<4; i++) {
			setDisplay(document.getElementById('room' + i), (i <= numRooms ? 'block' : 'none'));
		}
	}
}

/** date dropdown logic **/
// triggered by changing a month dropdown
// 1) ensures the check out month isn't before the check in
// 2) fixes any day issues where the number of days is now less than whats shown
function validateMonth() {
	var checkIn = document.getElementById("checkInMonth");
	var checkOut = document.getElementById("checkOutMonth");

	if ((checkOut.value*1) < (checkIn.value*1)) {
		checkOut.value = checkIn.value;
	}

//  these are no longer reguired as we are not using menus from checkout date

//	showCorrectDays(document.getElementById("checkInDate"), daysInMonth(checkIn.value));
//	showCorrectDays(document.getElementById("checkOutDate"), daysInMonth(checkOut.value));

	validateDate();
}

function validateMonth2() {
	var checkIn = document.getElementById("checkInMonth2");
	var checkOut = document.getElementById("checkOutMonth2");

	if ((checkOut.value*1) < (checkIn.value*1)) {
		checkOut.value = checkIn.value;
	}

	showCorrectDays(document.getElementById("checkInDate2"), daysInMonth(checkIn.value));
	showCorrectDays(document.getElementById("checkOutDate2"), daysInMonth(checkOut.value));

	validateDate2();
}

// triggered by changing a date dropdown (also called by validateMonth)
// 1) tests the check in date against the current date to ensure its not in the past
// 2) ensures the check out date isn't before the check in when they are on the same month
// 3) rejigs the calendar to display the correct month and highlighted date
// 4) sets the min date for the check out calendar

// this function changed (a couple of times) by Paul as we dont need second calendar for check out
// but we would like to return values of check out date based on the number of days 

function validateDate() {
	var checkIn = document.getElementById("checkInDate");
	var checkOut = document.getElementById("checkOutDate");
	var checkOutNew = document.getElementById("checkOutDateNew");

	var checkInMonth = document.getElementById("checkInMonth");
	var checkOutMonth = document.getElementById("checkOutMonth");
	var checkOutMonthNew = document.getElementById("checkOutMonthNew");

	var numDays = document.getElementById("numDays");
	
	var date = new Date();
	date.setDate(checkIn.value);
	date.setMonth(checkInMonth.value);
	if (date < today) {
		date = today;
		checkIn.value = date.getDate();
		checkInMonth.value = date.getMonth();
	}

// I have hacked this a bit as check out date now set by the number of days (not just if less than check in date)

//	if ((checkInMonth.value*1) == (checkOutMonth.value*1)) {
//		if ((checkOut.value*1) <= (checkIn.value*1)) {

			date.setDate(date.getDate() + (numDays.value*1) );

			//showCorrectDays(checkOut, daysInMonth(date.getMonth()));

			//checkOutMonth.value = (date.getYear() > today.getYear() ? (date.getMonth()+12) : date.getMonth());

			// array for values of month and year
			var monthYear = new Array();
			monthYear[0] = "Jan   "  + today.getFullYear();
			monthYear[1] = "Feb   " + today.getFullYear();
			monthYear[2] = "Mar   " + today.getFullYear();
			monthYear[3] = "Apr   " + today.getFullYear();
			monthYear[4] = "May   " + today.getFullYear();
			monthYear[5] = "Jun   " + today.getFullYear();
			monthYear[6] = "Jul   " + today.getFullYear();
			monthYear[7] = "Aug   " + today.getFullYear();
			monthYear[8] = "Sep   " + today.getFullYear();
			monthYear[9] = "Oct   " + today.getFullYear();
			monthYear[10] = "Nov   " + today.getFullYear();
			monthYear[11] = "Dec   " + today.getFullYear();
			monthYear[12] = "Jan   " + (today.getFullYear()+1);
			monthYear[13] = "Feb   " + (today.getFullYear()+1);
			monthYear[14] = "Mar   " + (today.getFullYear()+1);
			monthYear[15] = "Apr   " + (today.getFullYear()+1);
			monthYear[16] = "May   " + (today.getFullYear()+1);
			monthYear[17] = "Jun   " + (today.getFullYear()+1);
			monthYear[18] = "Jul   " + (today.getFullYear()+1);
			monthYear[19] = "Aug   " + (today.getFullYear()+1);
			monthYear[20] = "Sep   " + (today.getFullYear()+1);
			monthYear[21] = "Oct   " + (today.getFullYear()+1);
			monthYear[22] = "Nov   " + (today.getFullYear()+1);
			monthYear[23] = "Dec   " + (today.getFullYear()+1);

			checkOutMonthNew.value = monthYear[(date.getFullYear() > today.getFullYear() ? (date.getMonth()+12) : date.getMonth())];

			//checkOut.value = date.getDate();
			checkOutNew.value = date.getDate();

			date.setDate( date.getDate() -1 );
//		}
//	}

	//checkOutCalendar.minDate = date;

	redoCalendar(checkInCalendar, checkIn.value, checkInMonth.value);
	redoCalendar(checkOutCalendar, checkOut.value, checkOutMonth.value);
}

function validateDate2() {
	var checkIn = document.getElementById("checkInDate2");
	var checkOut = document.getElementById("checkOutDate2");

	var checkInMonth = document.getElementById("checkInMonth2");
	var checkOutMonth = document.getElementById("checkOutMonth2");

	var date = new Date();
	date.setDate(checkIn.value);
	date.setMonth(checkInMonth.value);
	if (date < today) {
		date = today;
		checkIn.value = date.getDate();
		checkInMonth.value = date.getMonth();
	}

	if ((checkInMonth.value*1) == (checkOutMonth.value*1)) {
		if ((checkOut.value*1) <= (checkIn.value*1)) {

			date.setDate(date.getDate() +1);

			showCorrectDays(checkOut, daysInMonth(date.getMonth()));

			checkOutMonth.value = (date.getYear() > today.getYear() ? (date.getMonth()+12) : date.getMonth());
			checkOut.value = date.getDate();

			date.setDate( date.getDate() -1 );
		}
	}

	checkOutCalendar2.minDate = date;

	redoCalendar(checkInCalendar2, checkIn.value, checkInMonth.value);
	redoCalendar(checkOutCalendar2, checkOut.value, checkOutMonth.value);
}



// util function to return number of days in a month
function daysInMonth(month) {
	var date = new Date();

	date.setDate(1);
	date.setMonth(month);
	var month = date.getMonth();
	var day = date.getDate();

	if (month < date.getMonth()) {
		while (month < date.getMonth()) {
			date.setDate(day--);
		}
	}else{
		date.setMonth(month+1);
		date.setDate(0);
	}
	return date.getDate();
}

// util function to limit the number of options in the date dropdown
function showCorrectDays(obj, numDays) {

	var indx = obj.options.selectedIndex;
	while (obj.options.length > 0) {
		removeElement(obj.options[0]);
	}

	for (var i=1; i<=numDays; i++) {
		var option = createElement("option");
		option.value = i;
		option.innerHTML = i;
		appendChild(obj, option)
	}

	obj.options.selectedIndex = (indx < obj.options.length ? indx : 0);
}

/** calendar logic **/
// util function to redraw a calendar
function redoCalendar(calendar, day, month) {
	var date = new Date();
	date.setDate(day);
	date.setMonth(month);
	calendar.setDate(date);
}

// triggered by clicking a date on the check in calendar
// 1) updates the check in date dropdown with new value
// 2) hides the calendar again
// 4) changes the check out date if its before the check in
function setCheckInDate(date, hide) {
	
	var checkIn = document.getElementById("checkInDate");
	var checkOut = document.getElementById("checkOutDate");
	var checkInMonth = document.getElementById("checkInMonth");
	checkIn.value = date.getDate();
	
	var yearDiff = date.getYear() - today.getYear();
	var month = yearDiff == 0 ? date.getMonth() : (date.getMonth() + (yearDiff*12)) ;
	
	
	checkInMonth.value = month;

	checkOutCalendar.minDate = date;
	validateMonth();

	if (checkOutCalendar.date < checkInCalendar.date) {
		checkOutCalendar.setDate( date );
	}else{
		checkOutCalendar.setDate(checkOutCalendar.date);
	}


	if (hide) {
		showHide("checkInPicker");
		switchVisibility("checkOutDate", "checkOutMonth");
		setVisibility(document.getElementById("room"),  "visible");
		setVisibility(document.getElementById("checkInDate"),  "visible");
		setVisibility(document.getElementById("checkInMonth"), "visible");
		setVisibility(document.getElementById("checkOutDate"),  "visible");
		setVisibility(document.getElementById("checkOutMonth"), "visible");
	} else {
		//ie fix to ensure the check out date stays hidden after showing the correct number of days
		setVisibility(checkOut, "hidden");
	}

}

function setCheckInDate2(date, hide) {
	var checkIn = document.getElementById("checkInDate2");
	var checkOut = document.getElementById("checkOutDate2");
	var checkInMonth = document.getElementById("checkInMonth2");
	checkIn.value = date.getDate();

	var yearDiff = date.getYear() - today.getYear();
	var month = yearDiff == 0 ? date.getMonth() : (date.getMonth() + (yearDiff*12)) ;

	checkInMonth.value = month;

	checkOutCalendar2.minDate = date;
	validateMonth2();

	if (checkOutCalendar2.date < checkInCalendar2.date) {
		checkOutCalendar2.setDate( date );
	}else{
		checkOutCalendar2.setDate(checkOutCalendar2.date);
	}


	if (hide) {
		showHide("checkInPicker2");
		/*switchVisibility("checkOutDate2", "checkOutMonth2");
		setVisibility(document.getElementById("checkInDate2"),  "visible");
		setVisibility(document.getElementById("checkInMonth2"), "visible");
		setVisibility(document.getElementById("checkOutDate2"),  "visible");
		setVisibility(document.getElementById("checkOutMonth2"), "visible"); */
	} else {
		//ie fix to ensure the check out date stays hidden after showing the correct number of days
		setVisibility(checkOut, "hidden");
	}

}

// triggered by clicking a date on the check out calendar
// 1) updates the check out date dropdown with new value
// 2) hides the calendar again
function setCheckOutDate(date, hide) {
	var checkOut = document.getElementById("checkOutDate");
	var checkOutMonth = document.getElementById("checkOutMonth");
	checkOut.value = date.getDate();

	var yearDiff = date.getYear() - today.getYear();
	var month = yearDiff == 0 ? date.getMonth() : (date.getMonth() + (yearDiff*12)) ;
	checkOutMonth.value = month;
	validateMonth();

	if (hide) {
		showHide("checkOutPicker");
		setVisibility(document.getElementById("room"),  "visible");
		setVisibility(document.getElementById("checkInDate"),  "visible");
		setVisibility(document.getElementById("checkInMonth"), "visible");
		setVisibility(document.getElementById("checkOutDate"),  "visible");
		setVisibility(document.getElementById("checkOutMonth"), "visible");
	}
}

// triggered by clicking a date on the check out calendar
// 1) updates the check out date dropdown with new value
// 2) hides the calendar again
function setCheckOutDate2(date, hide) {
		
	var checkOut = document.getElementById("checkOutDate2");
	var checkOutMonth = document.getElementById("checkOutMonth2");
	checkOut.value = date.getDate();

	var yearDiff = date.getYear() - today.getYear();
	var month = yearDiff == 0 ? date.getMonth() : (date.getMonth() + (yearDiff*12)) ;
	checkOutMonth.value = month;
	validateMonth2();

	if (hide) {
		showHide("checkOutPicker2");
		setVisibility(document.getElementById("checkInDate2"),  "visible");
		setVisibility(document.getElementById("checkInMonth2"), "visible");
		setVisibility(document.getElementById("checkOutDate2"),  "visible");
		setVisibility(document.getElementById("checkOutMonth2"), "visible");
	}
}

// triggered by page load
// 1) initializes the calendar displays
function initCalendars() {
	var startDate = new Date();
	
	var checkInAttachPoint  = document.getElementById("checkInPicker");
	var checkInTable = document.createElement("table");
	
	checkInTable.id  = "checkInCalendar";
	checkInTable.className = "dynamicCalendar";

	checkInAttachPoint.appendChild(checkInTable);

	var checkOutAttachPoint = document.getElementById("checkOutPicker");
	var checkOutTable = document.createElement("table");

	checkOutTable.id  = "checkOutCalendar";
	checkOutTable.className = "dynamicCalendar"

	checkOutAttachPoint.appendChild(checkOutTable);
	checkInCalendar = new objCalendar(startDate, "checkInCalendar", "checkInDate", "checkInMonth");
	checkInCalendar.onClick = setCheckInDate;
	checkInCalendar.minDate = today;

	checkOutCalendar = new objCalendar(startDate, "checkOutCalendar", "checkOutDate", "checkOutMonth");
	checkOutCalendar.onClick = setCheckOutDate;

	validateDate();

	var numberOfRooms = document.getElementById("numberOfRooms");
	if (isSet(numberOfRooms)) {
		showRooms(numberOfRooms);
	}

	addEvent(document, "click", function(e) {
		var src = ((ie) ? e.srcElement : e.target);

		if (src.isCalendar) {
			return;
		}

		if (src.id != "checkInIcon") {
			setDisplay(document.getElementById("checkInPicker"), "none");
		}

		if (src.id != "checkOutIcon") {
			setDisplay(document.getElementById("checkOutPicker"), "none");
		}

		if (src.id != "checkInIcon" && src.id != "checkOutIcon") {
			setVisibility(document.getElementById("checkInDate"), "visible");
			setVisibility(document.getElementById("checkInMonth"), "visible");
			setVisibility(document.getElementById("checkOutDate"), "visible");
			setVisibility(document.getElementById("checkOutMonth"), "visible");
			setVisibility(document.getElementById("numberOfRooms"), "visible");
			setVisibility(document.getElementById("numberOfPeople"), "visible");
			setVisibility(document.getElementById("room"), "visible");
		}
	});
}

// triggered by page load
// 1) initializes the calendar displays
function initCalendars2() {
	
	
	var startDate = new Date();
	
	var checkInAttachPoint  = document.getElementById("checkInPicker2");
	if (! checkInAttachPoint )
		return;
		
	var checkInTable = document.createElement("table");
	
	
	
	checkInTable.id  = "checkInCalendar2";
	checkInTable.className = "dynamicCalendar";

	checkInAttachPoint.appendChild(checkInTable);

	var checkOutAttachPoint = document.getElementById("checkOutPicker2");
	var checkOutTable = document.createElement("table");

	checkOutTable.id  = "checkOutCalendar2";
	checkOutTable.className = "dynamicCalendar"

	checkOutAttachPoint.appendChild(checkOutTable);
	checkInCalendar2 = new objCalendar(startDate, "checkInCalendar2", "checkInDate2", "checkInMonth2");
	checkInCalendar2.onClick = setCheckInDate2;
	checkInCalendar2.minDate = today;

	checkOutCalendar2 = new objCalendar(startDate, "checkOutCalendar2", "checkOutDate2", "checkOutMonth2");
	checkOutCalendar2.onClick = setCheckOutDate2;

	validateDate2();

	addEvent(document, "click", function(e) {
		var src = ((ie) ? e.srcElement : e.target);

		if (src.isCalendar) {
			return;
		}

		if (src.id != "checkInIcon2") {
			setDisplay(document.getElementById("checkInPicker2"), "none");
		}

		if (src.id != "checkOutIcon2") {
			setDisplay(document.getElementById("checkOutPicker2"), "none");
		}

		if (src.id != "checkInIcon2" && src.id != "checkOutIcon2") {
			setVisibility(document.getElementById("checkInDate2"), "visible");
			setVisibility(document.getElementById("checkInMonth2"), "visible");
			setVisibility(document.getElementById("checkOutDate2"), "visible");
			setVisibility(document.getElementById("checkOutMonth2"), "visible");
		}
	});
}

function showCalendar(calendarId, hideMany) {
	var   picker = document.getElementById(calendarId);
		
	var visibility = "";

	if (picker.style.display == "none" || picker.style.display == "") {
		picker.style.display = "block";
		visibility = "hidden";
	} else {
		picker.style.display = "none";
		visibility = "visible";
	}
	
	

	if (isSet(hideMany)) {
		
//		setVisibility(document.getElementById("checkInDate"),  visibility);
//		setVisibility(document.getElementById("checkInMonth"), visibility);
//		setVisibility(document.getElementById("checkOutDate"),  visibility);
//		setVisibility(document.getElementById("checkOutMonth"), visibility);

	}


	//if (picker.id == "checkInPicker") {
	//	setVisibility(document.getElementById("checkOutDate"),  visibility);
	//	setVisibility(document.getElementById("checkOutMonth"), visibility);
	//}
}

function showCalendar2(calendarId, hideMany) {
	var   picker = document.getElementById(calendarId);
		
	var visibility = "";

	if (picker.style.display == "none" || picker.style.display == "") {
		picker.style.display = "block";
		visibility = "hidden";
	} else {
		picker.style.display = "none";
		visibility = "visible";
	}
	
	

	/*if (isSet(hideMany)) {
		
		setVisibility(document.getElementById("checkInDate2"),  visibility);
		setVisibility(document.getElementById("checkInMonth2"), visibility);
		setVisibility(document.getElementById("checkOutDate2"),  visibility);
		setVisibility(document.getElementById("checkOutMonth2"), visibility);

	}
*/

	//if (picker.id == "checkInPicker") {
	//	setVisibility(document.getElementById("checkOutDate"),  visibility);
	//	setVisibility(document.getElementById("checkOutMonth"), visibility);
	//}
}




function setVisibility(element, visibility) {
	if (isSet(element)) {
		element.style.visibility = visibility;
	}
}

function setDisplay(element, display) {
	if (isSet(element)) {
		element.style.display = display;
	}
}

/** global variables **/
var checkInCalendar, checkOutCalendar, checkInCalendar2, checkOutCalendar2;
var today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);