// Sets the value of the days pulldown to be the correct number for that month
function updateDays()
{
	// Get the inDays select box now to save DOM searching in the loop later
	var inDays=$('inDays');

	// Make the date object of a month in the future
	var days=Calendar.getDaysInMonth($('inMonths').value-1, $('inYears').value);

	// We're going to need to know the current Day
	var currentDay=inDays.value;

	// Re-populate the days drop down, preserving the selected day
	inDays.length=0;
	for (var n=0;n< days;)
		inDays[n]=new Option(++n,n);

	// Restore the old value, if it's not now out of bounds
	if (currentDay<=days)
		inDays.value=currentDay;
}

// When you change the starting date, we set the checkout date to be startingDate+numNights
function changeIn()
{
	// First, validate the date - if it's invalid, set it to the correct one (i.e. 29th Feb == 1st Mar)
	var d=new Date($('inYears').value,$('inMonths').value-1,$('inDays').value);
	$('inDays').value=d.getDate();
	$('inMonths').value=d.getMonth()+1;
	$('inYears').value=d.getFullYear();

	// Set the checkout date to be the checkin + the number of nights
	updateCheckoutDate();
}

// When you change the number of nights, we change the ending date
function changeNights()
{
	updateCheckoutDate();
}

function updateCheckoutDate()
{
	var days=['Sun','Mon','Tues','Wed','Thurs','Fri','Sat'];
	var months=Array('Jan','Feb','Mar','Apr','May','June','July','Aug','Sep','Oct','Nov','Dec');	

	// Get the checkin date + the number of nights.
	var sd=new Date($('inYears').value,$('inMonths').value-1,$('inDays').value);

	var d=new Date(sd.getTime()+$('searchbox_noofnight_select').value*24*60*60*1000);

	// E.g. 3rd March 2007
	var day=d.getDate();
	day+=(1==day||21==day||31==day)?'st':(2==day||22==day)?'nd':(3==day||23==day)?'rd':'th';
	var mon=months[d.getMonth()];
	$('searchbox_checkout_div').innerHTML=days[d.getDay()]+' '+day+' '+mon;
}



function checkinClick(owner,callback)
{
	var id=$('inDays');
	var im=$('inMonths');
	var iy=$('inYears');

	new Calendar(
		owner,
		new Date(iy.value, im.value-1, id.value),
		function(d,m,y)
		{
			id.value=d;
			im.value=m+1;
			iy.value=y;
			changeIn();
			
			if (callback) callback();
		} );
}




function formSubmit(form)
{
	// The things that can go wrong.
	var errorInDate=false;
	var errorInDateEarly=false;
	var errorCountry=false;

	// Some elements are required
	ci=new Date($('inYears').value,$('inMonths').value-1,$('inDays').value);
	if (NaN==ci)
		// The date was not valid
		errorInDate=true;
	else
		// The date was too early - must be later than yesterday
		if (Calendar.getDateDiff(new Date(), ci)<0) errorInDateEarly=true;

	// There must be a valid country
	var c = $('searchbox_country_select');
	var h = $('searchbox_hotelname_text');
	if (0==c.value&&""==h.value) errorCountry=true;

	// OK, if we found any errors, display them all in a single alert and then don't submit the form
	if (errorInDate||errorCountry||errorInDateEarly)
	{
		// Display the alert
		var errorString="";
		if (errorCountry) errorString+="Please choose a country\n";
		if (errorInDate) errorString+="Please choose a checkin date\n";
		if (errorInDateEarly) errorString+="The checkin date must be today or later\n";
		alert(errorString);

		// Focus on an incorrect element
		if (errorCountry) { flashObject('searchbox_country_select'); }
		if (errorInDate||errorInDateEarly) { flashObject('inDays'); flashObject('inMonths'); flashObject('inYears'); }

		// Don't submit the form
		return false;
	}

	return true;
}





/*
** Populates the areas select depending on the country selected
*/
function addArea(cid)
{
	var sb=$('searchbox_area_select');
	sb.length=0;
	sb[0]=new Option("All","0");
	
	// Get the country from the areas array
	for (var n=0;n< countries.length;++n)
	{
		var c=countries[n];
		if (c.cid==cid)
		{
			// Add each area to the select box
			for (var m=0;m< c.areas.length;++m)
			{
				var a=c.areas[m];
				sb[m+1]=new Option(a.name, a.aid);
			}			
		}
	}
}




//
// Initialisation goes here!
//



// Regster the autocomplete here
new Autocomplete('searchbox_hotelname_text',['searchbox_country_select']);

// Set the area and resort.
//addArea($('searchbox_country_select').value);

// Initialise the checkout date
updateCheckoutDate();

// Initialise the number of days in the search box
updateDays();
