// set the checkout date form the checkin date at start
// to reflect a stay of 1 day by default
function setCheckoutFromCheckin(f)
{
		//alert("setCheckoutFromCheckin");
		// use short names
		// values
  		var inyear = f.arrivalDateYear.options[f.arrivalDateYear.selectedIndex].value;
  		var inmonth = f.arrivalDateMonth.options[f.arrivalDateMonth.selectedIndex].value;
  		var inday = f.arrivalDateDay.options[f.arrivalDateDay.selectedIndex].value;

        var checkinFilled = ( inyear != '' && inmonth != '' && inday != '');
			//alert("checkinFilled " + checkinFilled);
		if( !checkinFilled)
			return;
			//alert("passed return checkin");

 	 	var outyear = f.departureDateYear.options[f.departureDateYear.selectedIndex].value;
  		var outmonth = f.departureDateMonth.options[f.departureDateMonth.selectedIndex].value;
  		var outday = f.departureDateDay.options[f.departureDateDay.selectedIndex].value;

        var checkoutFilled = ( outyear != '' && outmonth != '' && outday != '');
		//alert("checkoutFilled " + checkoutFilled);
		// if chout is filled just return

		/* ############# Changes from checkoutFilled to !checkoutFilled############# */
		if(!checkoutFilled)
			return;
			//alert("passed return checkout");



		// we have checkin but not checkout. set checkout = checkin + 1 day
		var checkin = new Date(inyear, inmonth -1, inday);
		var checkout = new Date(outyear, outmonth -1, outday);

		//alert("Checkin:"+checkin.getTime()+" Checkout:"+checkout.getTime());

		if (checkin.getTime() >= checkout.getTime())	{

			oneDayInMillis =  24 * 3600 * 1000;
			checkoutMillis = checkin.getTime() + oneDayInMillis;
			chout = new Date( checkoutMillis );

			f.departureDateDay.selectedIndex = parseInt( chout.getDate(), 10 ) - 1;
			f.departureDateMonth.selectedIndex = parseInt( chout.getMonth(), 10 );
			offset = parseInt(f.arrivalDateYear.options[1].value.toString().slice(2),10)-1;
			if(typeof pastDates == 'undefined' || pastDates == null)
				f.departureDateYear.selectedIndex = parseInt( chout.getFullYear().toString().slice(2), 10 )-offset;
			else
			{
				// SPG specific.
				// we can have dates in the past in that particular form, e.g when asking for missing
				// credits for a stay in a hotel
				// the problem is that the above code will give us an index of 0 for year 2000 .
				// we'll just do a +1 on the index

				debug('SPG specific');
				f.departureDateYear.selectedIndex = parseInt( chout.getFullYear().toString().slice(2), 10 ) - offset;
			}
		}

}

//------------------------

// set the checkout date form the checkin date at start
// to reflect a stay of 1 day by default
function setCheckinFromCheckout(f)
{
		//alert("setCheckoutFromCheckin");
		// use short names
		// values
  		var inyear = f.arrivalDateYear.options[f.arrivalDateYear.selectedIndex].value;
  		var inmonth = f.arrivalDateMonth.options[f.arrivalDateMonth.selectedIndex].value;
  		var inday = f.arrivalDateDay.options[f.arrivalDateDay.selectedIndex].value;

        var checkinFilled = ( inyear != '' && inmonth != '' && inday != '');
			//alert("checkinFilled " + checkinFilled);
		if( !checkinFilled)
			return;
			//alert("passed return checkin");

 	 	var outyear = f.departureDateYear.options[f.departureDateYear.selectedIndex].value;
  		var outmonth = f.departureDateMonth.options[f.departureDateMonth.selectedIndex].value;
  		var outday = f.departureDateDay.options[f.departureDateDay.selectedIndex].value;

        var checkoutFilled = ( outyear != '' && outmonth != '' && outday != '');
		//alert("checkoutFilled " + checkoutFilled);
		// if chout is filled just return

		/* ############# Changes from checkoutFilled to !checkoutFilled############# */
		if(!checkoutFilled)
			return;
			//alert("passed return checkout");



		// we have checkin but not checkout. set checkout = checkin + 1 day
		var checkin = new Date(inyear, inmonth -1, inday);
		var checkout = new Date(outyear, outmonth -1, outday);

		//alert("Checkin:"+checkin.getTime()+" Checkout:"+checkout.getTime());

		if (checkin.getTime() >= checkout.getTime())	{

		//alert("mew function");

			oneDayInMillis =  24 * 3600 * 1000;
			checkinMillis = checkout.getTime() - oneDayInMillis;
			chin = new Date( checkinMillis );



			f.arrivalDateDay.selectedIndex = parseInt( chin.getDate(), 10 ) - 1;
			f.arrivalDateMonth.selectedIndex = parseInt( chin.getMonth(), 10 );
			offset = parseInt(f.departureDateYear.options[1].value.toString().slice(2),10)-1;
			if(typeof pastDates == 'undefined' || pastDates == null)
				f.arrivalDateYear.selectedIndex = parseInt( chin.getFullYear().toString().slice(2), 10 )-offset;
			else
			{
				// SPG specific.
				// we can have dates in the past in that particular form, e.g when asking for missing
				// credits for a stay in a hotel
				// the problem is that the above code will give us an index of 0 for year 2000 .
				// we'll just do a +1 on the index

				debug('SPG specific');
				f.arrivalDateYear.selectedIndex = parseInt( chin.getFullYear().toString().slice(2), 10 ) - offset;
			}
		}

}


function goToPage(f, page) {
	l_arrivalDateYear = f.arrivalDateYear.options[f.arrivalDateYear.selectedIndex].value;
	l_arrivalDateMonth = f.arrivalDateMonth.options[f.arrivalDateMonth.selectedIndex].value;
	l_arrivalDateDay = f.arrivalDateDay.options[f.arrivalDateDay.selectedIndex].value;
	l_departureDateYear = f.departureDateYear.options[f.departureDateYear.selectedIndex].value;
	l_departureDateMonth = f.departureDateMonth.options[f.departureDateMonth.selectedIndex].value;
	l_departureDateDay = f.departureDateDay.options[f.departureDateDay.selectedIndex].value;

	if ((l_arrivalDateYear == null || l_arrivalDateMonth == null || l_arrivalDateDay == null) ||
		(l_arrivalDateYear == ""   || l_arrivalDateMonth == ""   || l_arrivalDateDay == "")) {
		alert("Please select check in and check out dates");
	} else {
		f.arrivalDate.value = l_arrivalDateYear + '-' + paddZero(l_arrivalDateMonth) + '-' + paddZero(l_arrivalDateDay);
		f.departureDate.value = l_departureDateYear + '-' + paddZero(l_departureDateMonth) + '-' + paddZero(l_departureDateDay);
		var indate = new Date(l_arrivalDateYear, l_arrivalDateMonth - 1, l_arrivalDateDay);
		var outdate = new Date(l_departureDateYear, l_departureDateMonth - 1, l_departureDateDay);
		if (f.lengthOfStay.type != 'select-one') f.lengthOfStay.value = getDiffDaysNumber(indate,outdate);
		else {
			if (f.lengthOfStay != null) f.lengthOfStay.disabled = false;
			if (f.departureDateMonth != null) f.departureDateMonth.disabled = false;
			if (f.departureDateDay != null) f.departureDateDay.disabled = false;
			if (f.departureDateYear != null) f.departureDateYear.disabled = false;
		}
		//f.roomOccupancyTotal.value = f.numberOfRooms.value * f.numberOfAdults.value;
		f.roomOccupancyTotal.value = f.numberOfAdults.value;
		document.SearchForm.action = page;
		document.SearchForm.submit();
		return true;
	}
}

// diff in days between two dates
function getDiffDaysNumber(indate,outdate)
{
  		// this will give us a float i.e something like 7.9583333.. for 8 days
  		var df =  ( outdate.getTime()-indate.getTime() ) / (1000 * 60 * 60 * 24);

  		// convert to a whole number
  		return Math.round(df);

}
//--------------------------------------------------------





