// generates todays date an selects it in the form...
now_today = new Date();
Tag_today = now_today.getDate();
Monat_today = now_today.getMonth();
Jahr_today = now_today.getFullYear();

now = new Date(now_today.getTime() + (14 * 86400000));
Tag = now.getDate();
Monat = now.getMonth();
Jahr = now.getFullYear();

var tomorrow = new Date(now_today.getTime() + (15 * 86400000));
tDay = tomorrow.getDate();
tMonth = tomorrow.getMonth();
tYear = tomorrow.getYear();
if (tYear < 2000) tYear += 1900; // Netscape

if ((Jahr % 4 == 0) && (Jahr % 100 != 0) || (Jahr % 400 == 0)) {
	feb_days = 29;
} else {
	feb_days = 28;
}


day_array = [31, feb_days, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

Monat_days = day_array[Monat];

month_array = ["Januar", "Februar", "Mars", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Desember"];






/*
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.
*/

function showWindow( m_url, winname, winwidth, winheight, wndparams)
{
	if ((winname == null) || (winname == "")) winname = "lvs";
	
	winleft = (screen.availWidth / 2) - (winwidth / 2);
	wintop = (screen.availHeight / 2) - (winheight / 2);
	
	if (wndparams == null) wndparams = "scrollbars=no,resizable=no";
	oWinInfo=(window.open(m_url, winname,"left=" + winleft + ",top=" + wintop + ",width=" + winwidth + ",height=" + winheight + "," + wndparams));
	if (!Browser.ie) {
		if(oWinInfo.self != null)  oWinInfo.focus();
	}
}

/**
 * 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;
	}
}


// checks the current date in the arrivalDate dropdown
// and puts 2 days later's date in the departuredate dropdown
function date_checkout() {

	dy = document.selectHotelForm.arrivalDay.value; // 27 (Tag)
	mt = document.selectHotelForm.arrivalMonthYear.value; // 200608 (Sept. 2006)
	
	// richtiges Datum wird eruiert
	Tag_arr = dy;
	Monat_arr = Number(mt.substr(4,2));
	Jahr_arr = Number(mt.substr(0,4));
	var mont_h = ["Januar", "Februar", "Mars", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Desember"];
	
	// Datum wird formatiert zuzr weiterverwendung mit Date() Object
	var depart = new Date(mont_h[Monat_arr] + " " + Tag_arr + ", " + Jahr_arr + "  00:00:00 GMT");
	
	// Datum in 2 Tagen wird eruiert
	twod = new Date(depart.getTime() + (2 * 86400000));
	twod_Day = twod.getDate();
	twod_Month = twod.getMonth();
	twod_Year = twod.getFullYear();
	
	// window.alert("--> " + twod_Day + " " + twod_Month);
	
	document.selectHotelForm.departureDay.value = twod_Day;	
	if (twod_Month < 10) {
		document.selectHotelForm.departureMonthYear.value = twod_Year+'0'+twod_Month;
	} else {
		document.selectHotelForm.departureMonthYear.value = twod_Year+''+twod_Month;
	}
	
}




function writeDayDropdown (monthDays) {
  for (d=1; d<=Monat_days; d++) {
    document.write("<option value="+d+">"+d+"</option>");
  }
}


function writeMonthDropdown (monthToday, YearToday, month_array) {

  for (i=monthToday; i<12; i++) {
    if (i<10) {
      document.write("<option value="+YearToday+'0'+(i)+">"+month_array[i]+" "+YearToday+"</option>");
    } else {
      document.write("<option value="+YearToday+''+(i)+">"+month_array[i]+" "+YearToday+"</option>");
    }
  }
  for (k=0; k<monthToday+1; k++) {
    if (k<10) {
      document.write("<option value="+(YearToday+1)+'0'+(k)+">"+month_array[k]+" "+(YearToday+1)+"</option>"); 
    } else {
      document.write("<option value="+(YearToday+1)+''+(k)+">"+month_array[k]+" "+(YearToday+1)+"</option>");
    }
  }
}

/**
 * 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('Check-in date is required');
		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;
	}
}

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;
	}
		
}

/**
 * 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;
	}
}

/** 
 * 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);
		}
	}
}
function removeAllData(options)
{
	var element = options.firstChild;
	while (element != null)
	{
		var nextChild = element.nextSibling;
		options.removeChild(element);
		element = nextChild;
	}
	options.length = 0;
}