function doSubmitRoomRates() 
{
	var form_action = document.forms[0].action;
	var form_target = document.forms[0].target;
	document.forms[0].action = "http://www.choicehotels.no/hotels/roomRates";
	document.forms[0].target = "_blank";
	checkReservation();
	document.forms[0].action = form_action;
	document.forms[0].target = form_target;
}

function doSubmitSearchHotels() 
{
	var form_action = document.forms[0].action;
	var form_target = document.forms[0].target;
	document.forms[0].action = "http://www.choicehotels.no/hotels/searchHotels";
	document.forms[0].target = "_blank";
	checkReservation();
	document.forms[0].action = form_action;
	document.forms[0].target = form_target;
}

function openCalendar(str_ctrl, str_month){
	var vWinCal = window.open("/wp-content/plugins/datepicker-i18n.php?ctrl="+str_ctrl+"&month="+str_month, "Calendar", "width=355,height=260,status=yes,resizable=yes,top=200,left=200,toolbar=yes,menubar=yes");
	vWinCal.focus();
}

function setDates() 
{
	var new_date = new Date();
	document.getElementById('day').options.selectedIndex = new_date.getDate();
	document.getElementById('month').options.selectedIndex = new_date.getMonth() + 1;
	document.getElementById('depart_day').options.selectedIndex = new_date.getDate() + 1;
	document.getElementById('depart_month').options.selectedIndex = new_date.getMonth() + 1;
}

function checkDates(ctrl)
{
	//get the values of these dates so we can validate
	var ONE_DAY = 1000 * 60 * 60 * 24;
	var arrivalDate = new Date();
	if(document.getElementById('month').options.selectedIndex == 2
	|| document.getElementById('month').options.selectedIndex == 4
	|| document.getElementById('month').options.selectedIndex == 6
	|| document.getElementById('month').options.selectedIndex == 9
	|| document.getElementById('month').options.selectedIndex == 11) 
	{
		arrivalDate.setDate(document.getElementById('day').options.selectedIndex);
		arrivalDate.setMonth(eval(document.getElementById('month').options.selectedIndex)-1);
	}
	else
	{
		arrivalDate.setMonth(eval(document.getElementById('month').options.selectedIndex)-1);
		arrivalDate.setDate(document.getElementById('day').options.selectedIndex);
	}
	var departDate = new Date();
	if(document.getElementById('depart_month').options.selectedIndex == 2
	|| document.getElementById('depart_month').options.selectedIndex == 4
	|| document.getElementById('depart_month').options.selectedIndex == 6
	|| document.getElementById('depart_month').options.selectedIndex == 9
	|| document.getElementById('depart_month').options.selectedIndex == 11) 
	{
		departDate.setDate(document.getElementById('depart_day').options.selectedIndex);
		departDate.setMonth(eval(document.getElementById('depart_month').options.selectedIndex)-1);
	}
	else
	{
		departDate.setMonth(eval(document.getElementById('depart_month').options.selectedIndex)-1);
		departDate.setDate(document.getElementById('depart_day').options.selectedIndex);
	}
	if ((arrivalDate.getMonth() == departDate.getMonth() && arrivalDate.getDate() >= departDate.getDate())
	|| arrivalDate.getMonth() > departDate.getMonth())
	{
		if (ctrl == 'arrival') 
		{
			if( arrivalDate.getMonth() == 11 && departDate.getMonth() == 0) 
			{
				//december-january
				return; // don't do anything
			}
			//if arrival then the departure date cannot be before (less than or equal to) the arrival
			departDate.setTime(arrivalDate.getTime()+ONE_DAY);
			document.getElementById('depart_day').options.selectedIndex=eval(departDate.getDate());
			document.getElementById('depart_month').options.selectedIndex=eval(departDate.getMonth()+1);
			document.getElementById('day').options.selectedIndex=eval(arrivalDate.getDate());
			document.getElementById('month').options.selectedIndex=eval(arrivalDate.getMonth()+1);
		}
		else
		{
			if( arrivalDate.getMonth() == 11 && departDate.getMonth() == 0) 
			{
				//december-january
				return; // don't do anything
			}
			//if departure then the arrival date cannot be greater than or equal departure
			arrivalDate.setTime(departDate.getTime()-ONE_DAY-1);
			document.getElementById('day').options.selectedIndex=eval(arrivalDate.getDate());
			document.getElementById('month').options.selectedIndex=eval(arrivalDate.getMonth()+1);
			document.getElementById('depart_day').options.selectedIndex=eval(departDate.getDate());
			document.getElementById('depart_month').options.selectedIndex=eval(departDate.getMonth()+1);
		}
	}
	else
	{
		document.getElementById('day').options.selectedIndex=eval(arrivalDate.getDate());
		document.getElementById('month').options.selectedIndex=eval(arrivalDate.getMonth()+1);
		document.getElementById('depart_day').options.selectedIndex=eval(departDate.getDate());
		document.getElementById('depart_month').options.selectedIndex=eval(departDate.getMonth()+1);
	}
}
        
function validateDates(formName)
{
	var placeFinder = document.getElementById(formName);
	//set the arrival day/month
	var arr_date = new Date();
	arr_date.setMonth(eval(placeFinder.month.selectedIndex)-1);
	arr_date.setDate(placeFinder.day.selectedIndex);
	//set the departure day/month
	var dep_date = new Date();
	dep_date.setMonth(eval(placeFinder.depart_month.selectedIndex)-1);
	dep_date.setDate(placeFinder.depart_day.selectedIndex);
	//get todays date
	var today_date = new Date();
	//if selected arrival date is earlier than the current date, add 1 year to the arrival date
	if (arr_date < today_date)
	{
		arr_date.setFullYear(eval(arr_date.getFullYear()) + 1);
	}
	//if selected departure date is earlier than the current month, add 1 year to the departure date
	if (dep_date < today_date)
	{
		dep_date.setFullYear(eval(dep_date.getFullYear()) + 1);
	}
	if (arr_date > dep_date)
	{
		return 1;
	}
	//if arrival departure booking is more than 51 weeks(357 days) in advance inform the
	//user this isn't possible to book
	// Call the weeks_between function
	var days_left = daysBetween(today_date, arr_date)
	if (days_left > 357)
	{
		return 2;
	}

	// Call the weeks_between function
	days_left = daysBetween(today_date, dep_date)
	if (days_left > 357)
	{
		return 5;
	}
}

function daysBetween(date1, date2) 
{
	// The number of milliseconds in one day
	var ONE_DAY = 1000 * 60 * 60 * 24
	// Convert both dates to milliseconds
	var date1_ms = date1.getTime()
	var date2_ms = date2.getTime()
	// Calculate the difference in milliseconds
	var difference_ms = Math.abs(date1_ms - date2_ms)
	// Convert back to days and return
	return Math.round(difference_ms/ONE_DAY)
}

function checkReservation() 
{
	var placeFinder = document.forms[0];
	var someBool = true
	if (placeFinder.destination.selectedIndex == 0) 
	{
        	alert("Please select a location.");
	        someBool = false;
	}
	if (placeFinder.month.selectedIndex == 0) 
	{
        	alert("Please select a check-in month.");
	        someBool = false;
	}
	if (placeFinder.day.selectedIndex == 0) 
	{
        	alert("Please select a check-in day.");
	        someBool = false;
	}
	if (placeFinder.depart_month.selectedIndex == 0) 
	{
        	alert("Please select a check-out month.");
	        someBool = false;
	}
	if (placeFinder.depart_day.selectedIndex == 0) 
	{
	        alert("Please select a check-out day.");
	        someBool = false;
	}
	var result = validateDates(document.forms[0].name);
	if (result == 1) 
	{
        	alert("The Check-in date must be before the Check-out date. A date earlier than today is considered next year.");
	        someBool = false;
	}
	if (result == 2) 
	{
        	alert("You cannot book a room more than 51 weeks in the future. A date earlier than today is considered next year.");
	        someBool = false;
	}
	if(someBool)
	{
		document.forms[0].submit()
	}
}

window.onload = setDates;


