/*
This file contains javascript functions that are used by the hotel search
include file on bestwestern.com.  This code is copied from the original 
javascript on book.bestwestern.com but there are some modifications.  Please
pay attention to comments when making any changes or prior to overwriting
any code with updated versions from book.bestwestern.com.  It is recommended
that a code merge be done as opposed to overwriting.
*/

/**
 ****************************************************************************
 * the main validation methods
 ****************************************************************************
 */

function changeDepartureDate(form)
{
		var arrMY = form.arrivalMonthYear.value;
		var depMY = form.departureMonthYear.value;
		var arrDY = form.arrivalDay.value;
		var depDY = form.departureDay.value;

		arrDate = new Date();
		depDate = new Date();

		arrDate.setYear(arrMY.substring(0,4));
		arrDate.setMonth(arrMY.substring(6,4));
		arrDate.setDate(arrDY);

		depDate.setYear(depMY.substring(0,4));
		depDate.setMonth(depMY.substring(6,4));
		depDate.setDate(depDY);

		if(days_between(depDate, arrDate)< 0){
			//alert('Your Check-out date must occur after your Check-in date.  Please revise your dates and try again.');
			changeDep(form);
			depMY = form.departureMonthYear.value;
			depDY = form.departureDay.value;
			depDate.setYear(depMY.substring(0,4));
			depDate.setMonth(depMY.substring(6,4));
			depDate.setDate(depDY);
			//return false;
		}
		if(days_between(depDate, arrDate)== 0){
			//alert('Check-in date is equal to the Check-out date. Please enter a valid date.');
			changeDep(form);
			depMY = form.departureMonthYear.value;
			depDY = form.departureDay.value;
			depDate.setYear(depMY.substring(0,4));
			depDate.setMonth(depMY.substring(6,4));
			depDate.setDate(depDY);
			//return false;
		}
}

/**
* Changes the departure date equal to increment of
* arrival date to one day.
*/
function changeDep(form)
{
	var arrMY = form.arrivalMonthYear.value;
	var arrDY = form.arrivalDay.value;
	
	arrDate = new Date();
	
	arrDate.setMonth(arrMY.substring(6,4));
	arrDate.setYear(arrMY.substring(0,4));
	arrDate.setDate(arrDY);
	
	temp_date = new Date(arrDate.getTime() +(24*60*60*1000)); //Incrementing the arrival date to one day
	form.departureDay.value = temp_date.getDate();
	
	if(temp_date.getMonth() < 10){
		tempMY= temp_date.getYear() + "0" + temp_date.getMonth();
	
	}else{
		tempMY= temp_date.getYear() + '' + temp_date.getMonth();
	
	}

	form.departureMonthYear.value = tempMY;
}

/**
 * When a Guest selects a month, refresh the ArrivalDay drop-down;
 * also called when a year changes, to account for the possibility
 * that a leap year was selecte/unselected
 */
function changeOfMonth(aSelect) {
	selectNextDate(aSelect)
	var month = (aSelect.value).substring(4,6)
	var day   = aSelect.form.arrivalDay.value;
	var year  = aSelect.value.substring(0,4);
	var numDays = getNumDaysInMonth(month,year);
	var aryDays = new Array();

	aryDays[0] = new Option("", "-1");

	for (var i=1; i <= numDays; i++) {
		aryDays[i] = new Option(i,i);
	}

	setOptions(aSelect.form.arrivalDay, aryDays);

	// restore the date, if one was selected and is valid
	if (day > 0 && day <= numDays) {
		aSelect.form.arrivalDay.options[day].selected = true;
	}

	changeDepartureDate(aSelect.form);
}

function selectNextDate(aSelect){
	var day	  = aSelect.form.arrivalDay.value;
	var month = (aSelect.form.arrivalMonthYear.value).substring(4,6);
	var year  = (aSelect.form.arrivalMonthYear.value).substring(0,4);
	var curDate		=new Date(year,month,day)
	var nextDate	=new Date(curDate)
	nextDate.setDate(nextDate.getDate()+1);
	var nextmonthyear = nextDate.getFullYear() +'' + 
						(nextDate.getMonth() <10 ? '0'+ nextDate.getMonth() :nextDate.getMonth())
	if(	aSelect.form.departureDay.selectedIndex==0 && 
		aSelect.form.departureMonthYear.selectedIndex==0 ){
		aSelect.form.departureDay.value=nextDate.getDate();
		aSelect.form.departureMonthYear.value=nextmonthyear;
	}
}

/**
 * When a Guest selects a month, refresh the ArrivalDay drop-down;
 * also called when a year changes, to account for the possibility
 * that a leap year was selecte/unselected
 */
function changeOfMonthDeparture(aSelect) {
	if(	aSelect.form.arrivalDay.selectedIndex==0 || aSelect.form.arrivalMonthYear.selectedIndex==0 ){
		alert('Du måste välja datum för incheckning!');
		return false;
	}

	var month = (aSelect.value).substring(4,6)
	var day   = aSelect.form.departureDay.value;
	var year  = (aSelect.value).substring(0,4);

	var numDays = getNumDaysInMonth(month,year);
	var aryDays = new Array();

	aryDays[0] = new Option("", "-1");

	for (var i=1; i <= numDays; i++) {
		aryDays[i] = new Option(i,i);
	}
	setOptions(aSelect.form.departureDay, aryDays);

	// restore the date, if one was selected and is valid
	if (day > 0 && day <= numDays) {
		aSelect.form.departureDay.options[day].selected = true;
	}
	changeDepartureDate(aSelect.form);
}


/**
 ****************************************************************************
 * Date functions supporting the main validation methods above
 ****************************************************************************
 */

 function days_between(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 = date1_ms - date2_ms;

    diff=Math.round(difference_ms/ONE_DAY);

   	return diff;
}
 
/**
 ****************************************************************************
 * Generic helper functions not specific to the validation that we are performing
 ****************************************************************************
 */
 
 /**
 * Given a month represented by an integer (jan=0) and a year,  
 * return the number of days in that month; if the year is null, 
 * return 29 days for February
 */
function getNumDaysInMonth(month,year) {
	if (month == 3 || month == 5 || month == 8 || month == 10) {
		return 30;
	}
	else if (month == 1) {
		var leapOrNull = (year < 0) || (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (leapOrNull)
			return 29;
		else
			return 28;
	}
	else {
		return 31;
	}
}
 
function removeAllData(options)
{
	var element = options.firstChild;
	while (element != null)
	{
		var nextChild = element.nextSibling;
		options.removeChild(element);
		element = nextChild;
	}
	options.length = 0;
}

/** 
 * given a select input and an array of Options, 
 * set the options of the select to the Options provided
 */
function setOptions(aSelect, aryOptions) {
	if (aryOptions.length > 0) {
		removeAllData(aSelect);
		for (var i=0; i < aryOptions.length; i++) {
			// if we do not assign a new Option, running this method twice
			// over the same array will yield a pointer error
			aSelect.options[aSelect.length] = new Option(aryOptions[i].text, aryOptions[i].value);
		}
	}
}
