// 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();
	var daydigit=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()];
	var mondigit = d.getMonth()+1;
	$('searchbox_checkout_div').innerHTML=days[d.getDay()]+' '+day+' '+mon;
	
	//set the 2 hidden variables for checkout day and month to be passed to white label search
	$('lmnCheckOutDay').value=daydigit;
	$('lmnCheckOutMonth').value=mondigit;
	
}



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);
			}			
		}
	}
}

if(document.getElementById("searchbox_country_select")){
var droppy = document.getElementById("searchbox_country_select");
}

var lmnArea = document.getElementById("lmnLocation");
//if using the dropdown use this setlocation function
function setLocation(){
	var droppyindex = droppy.selectedIndex;
	var droppyvalue = droppy.value;
	
	var droppytext = droppy[droppyindex].text;
	var droppysplit = droppytext.split(">");
	var droppyCountryOnly = droppysplit[0];
	var droppyAreaOnly = droppysplit[1];
	
	
	if (droppy[droppyindex].getAttribute('title') =='') {
	lmnArea.setAttribute('value',droppyCountryOnly);
	} else {
	lmnArea.setAttribute('value',(droppy[droppyindex].getAttribute('title')));
	}
	//Save into cookie using prototype - save the dropdown selectbox index for 10 days
	Cookie.set('countryAndDestinationIndex',droppy.selectedIndex,86400);
}

//if using the freetext search use this function
function setLocation2(){
	var countryindex = $('country').selectedIndex;
	var countryvalue = $('country').value;
	
	var countrytext = $('country')[countryindex].text;

	//Save into cookie using prototype - save the dropdown selectbox index for 10 days
	Cookie.set('countryTemp',$('country').selectedIndex,86400);
}

function setDropForLocationPages() {
			
	//if we have a breadcrumb trail then set the dropdown to match it
if (typeof(droppyloc2)!=null) {

		for (var i=1; i < droppy.length ;i++){
			var areaFullName = droppy[i].text.split(">");
			//trim country
			var countryName = areaFullName[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			//trim area
			
			if (areaFullName[1]){
			var areaName = areaFullName[1].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			}
			
			if (typeof(droppyloc3)=='undefined'){
				if (droppyloc2 == countryName.toLowerCase()) {
						droppy.selectedIndex = i;
						setLocation();
						break;
				}
				}else {
				if (droppyloc3 == areaName.toLowerCase()) {
							droppy.selectedIndex = i;
							setLocation();
							break;
							}
			} 		
		} //end for loop
	} else {	
	alert('no country name in variable droppyloc2');
	}
}

function setDropForLocationPages2() {
			
	//if we have a breadcrumb trail then set the dropdown to match it
	if (typeof(droppyloc2)!=null) {
		
		for (var i=1; i < $('country').length ;i++){
			
			var countryName = $('country')[i].text;
			if (typeof(droppyloc3)!='undefined'){
				$('city').value = droppyloc3;
			}
			if (droppyloc2 == countryName.toLowerCase()) {
					$('country').selectedIndex = i;
					Cookie.set('countryTemp',i,86400);
					break;
			}  else {
			$('country').selectedIndex = Cookie.get('countryTemp');
			}
		}
	}
}

//
// Initialisation goes here!
//

  function destinationValidation() {
  	
  	if(droppy){
	  	if (droppy.selectedIndex=='0') {
	  	alert('please enter a destination');
	  	return false;
	  	}
  	}
  	
  	
  	if(document.getElementById("city").value==''){
  		alert('please enter a destination');
  		document.getElementById("city").focus();
  		return false;
  	}
  }
	Event.observe('search', 'click', function(event){destinationValidation;});



// Regster the autocomplete here
//hotel autocomplete disabled for whitelabel booking
//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();
