﻿/*
	Functions related to the search
*/

var C_DATE_SEPARATOR = "/";
var bDuringUpdate = false;


/*
Sets the check out vaue according to the nnumber of nights selected
*/
function SetCheckOutDate(daysTotal, checkInControlId, checkOutControlId)
{
	if (bDuringUpdate == true)
		return;

	try
	{
		bDuringUpdate = true;

		// Get Check in date as date in the correct format
		var checkInValue = document.getElementById(checkInControlId).value;
		
		var day = parseInt(checkInValue.substring(0, 2), 10);
		var month = parseInt(checkInValue.substring(3, 5), 10);
		var year = parseInt(checkInValue.substring(6, 10), 10);

		var CheckInDate = new Date();
		CheckInDate.setDate(day);
		CheckInDate.setMonth(month - 1);
		CheckInDate.setFullYear(year);


		var checkOutDate = new Date();
		checkOutDate = CheckInDate;
		checkOutDate.setDate(CheckInDate.getDate() + parseInt(daysTotal, 10));

		var checkOutValue = (padZeros(checkOutDate.getDate(), 2) + C_DATE_SEPARATOR + padZeros(checkOutDate.getMonth() + 1, 2) + C_DATE_SEPARATOR + checkOutDate.getFullYear());
		document.getElementById(checkOutControlId).value = checkOutValue;

		bDuringUpdate = false;

	}
	catch (e)
	{
		bDuringUpdate = false;
	}
}


/*
Set the number of nights drop down list to the number of nights calculated from
the difference bweteen check in and check out
*/
function SetNumberOfNights(checkInDateControlId, checkOutDateControlId, NumberOfNightsControlId)
{

	if (bDuringUpdate == true)
		return;

	try
	{
		bDuringUpdate = true;

		// Get Check in date as date in the correct format
		var checkInControl = document.getElementById(checkInDateControlId);
		var checkOutControl = document.getElementById(checkOutDateControlId);

		var checkInValue = checkInControl.value;
		var checkOutValue = checkOutControl.value;
		

		var checkInDay = parseInt(checkInValue.substring(0, 2), 10);
		var checkInMonth = parseInt(checkInValue.substring(3, 5), 10);
		var checkInYear = parseInt(checkInValue.substring(6, 10), 10);


		var CheckInDate = new Date();
		CheckInDate.setDate(checkInDay);
		CheckInDate.setMonth(checkInMonth -1);
		CheckInDate.setFullYear(checkInYear);


		var checkOutDay = parseInt(checkOutValue.substring(0, 2), 10);
		var checkOutMonth = parseInt(checkOutValue.substring(3, 5), 10);
		var checkOutYear = parseInt(checkOutValue.substring(6, 10), 10);

		var CheckOutDate = new Date();
		CheckOutDate.setDate(checkOutDay);
		CheckOutDate.setMonth(checkOutMonth -1);
		CheckOutDate.setFullYear(checkOutYear);


		// Calculate difference
		var diff = new Date();
		diff.setTime(Math.abs(CheckOutDate.getTime() - CheckInDate.getTime()));

		var timeDifference = diff.getTime();
		var Totaldays = Math.floor(timeDifference / (1000 * 60 * 60 * 24));

		// If user selected the same check in and check out dates, we set the check out date to chdck in date + 1 day
		if (Totaldays == 0)
		{
			Totaldays = 1;
			var newCheckOutValue = (padZeros(parseInt(CheckOutDate.getDate(), 10) + 1, 2) + C_DATE_SEPARATOR + padZeros(CheckOutDate.getMonth(), 2) + C_DATE_SEPARATOR + CheckOutDate.getFullYear());
			
			checkOutControl.value = newCheckOutValue;
		}
		
		// Set the dropdown value
		var ddlNights = document.getElementById(NumberOfNightsControlId);
		ddlNights.selectedIndex = Totaldays - 1;


		bDuringUpdate = false;

	}
	catch (e)
	{
		alert(e);
		bDuringUpdate = false;
	}
}




