var months = new Array("January","February","March","April","May","Jun","July","August","September","October","November","December");
var days = new Array("Mo","Tu","We","Th","Fr","Sa","Su");
var days_long = new Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
var previous_button_text = "prev";
var next_button_text = "next";

var sText = "";
var container = "calendar";

function drawMonthSelectTool(calendar,minimum_date_mil_secs,maximum_date_mil_secs,date_mil_secs){
	var date = new Date(0);
	date.setTime(date_mil_secs);

	var min = new Date(0);
	min.setTime(minimum_date_mil_secs);
	var min_month = min.getMonth();
	var today = new Date();

	var previousmonth = new Date(date.getFullYear(),date.getMonth()-1,1);//date.getDate()
	var nextmonth = Date.parse(new Date(date.getFullYear(),date.getMonth()+1,1));//date.getDate()

	var maximum_date_mil_secs_human = new Date(maximum_date_mil_secs);
	var nextmonth_human = new Date(nextmonth);
//	alert("nextmonth_human:"+nextmonth_human);

	//the 'previous month' button
	sText=sText+"<div class='month'>";
	//the button should do nothing when going before today:
	if (((previousmonth.getMonth()>=today.getMonth())&&(previousmonth.getFullYear()==today.getFullYear()))||(previousmonth.getFullYear()>today.getFullYear())){
		href = "Javascript:DrawPopUpCalendar_small("+Date.parse(previousmonth)+",\""+calendar+"\","+minimum_date_mil_secs+","+maximum_date_mil_secs+")";
	}
	else{
		href="#";
	}
	sText = sText+"<a href='"+href+"' class='c-prev'>"+previous_button_text+"</a>";

	//the select box for the month:
	sText=sText+"<select id='my_pop_upcalendar' onchange='Javascript:Changepopupcalendar(this,\""+calendar+"\","+maximum_date_mil_secs+","+minimum_date_mil_secs+");'>";

	for(var i=0;i<=11;i++){
		//populate selectbox with months/years
		option_date = new Date(today.getFullYear(),today.getMonth()+i,1);
		//alert(option_date.getMonth());
		option_text = months[option_date.getMonth()]+", "+option_date.getFullYear();
		//
		sText=sText+"<option value='"+option_date.getMonth()+"-"+option_date.getFullYear()+"'";

		//the required month is default selected.

		if (i>11){
			j=i-12;
		}
		else{
			j=i;
		}
		//alert (date.getMonth()+"---"+j+"   "+date.getFullYear()+"---"+option_date.getFullYear());
		if((date.getMonth()==option_date.getMonth())&&(date.getFullYear()==option_date.getFullYear())){
			//alert("selected");
			sText=sText+" selected";
		}
		sText=sText+">"+option_text+"</option>";
	}
	sText=sText+"</select>";
	/**************************************************************/

	//the 'next month' button
	//alert(nextmonth_human+"<"+maximum_date_mil_secs);
	if(nextmonth<maximum_date_mil_secs){
		href="Javascript:DrawPopUpCalendar_small("+nextmonth+",\""+calendar+"\","+minimum_date_mil_secs+","+maximum_date_mil_secs+")";
	}
	else{
		href="#";
	}
	sText=sText+"<a href='"+href+"' class='c-next'>"+next_button_text+"</a>";
	sText=sText+"</div>";
	return sText;
}

function drawDays(){
	sText = "<ul class='d-week'>";
	for(var i=0;i<days.length;i++){
		sText=sText+"<li>"+days[i]+"</li>";
	}
	sText = sText+"</ul>";
	return sText;
}

function DrawPopUpCalendar_small(date_mil_secs,calendar,minimum_date_mil_secs,maximum_date_mil_secs){
	//this functions draws a calendar for the month the date is in and some days around it
	/*************************************************************************************
	*                              some global variables:                                *
	*************************************************************************************/
	if(date_mil_secs==0){
		//date_mil_secs = GetDateFromDropDown();
		date_mil_secs = Date.parse(new Date());
	}
	var date = new Date(0);
	date.setTime(date_mil_secs);	//the date that function draws a calendar around
	//alert(date);
	var month = date.getMonth();
	var year = date.getFullYear();
	var today = new Date();


	//after the maximum date a date should not be selectable anymore.
	//If not given the max date is the given date + 2 years
	if(maximum_date_mil_secs==0){
		maximum_date_mil_secs = Date.parse(new Date(year,month+13,1));
	}
	//before the minimumdate a date should not be selectable anymore.
	//if no minimum date is given the minimum date will be set to yesterday
	if(minimum_date_mil_secs==0){
		minimum_date_mil_secs = Date.parse(new Date(today.getFullYear(),today.getMonth(),today.getDate()-1));
	}
	var startdate = 1;//startdatum van de kalender, 0 is zondag, 1 is maandag, etc.
	var firstDayOfTheMonth = new Date(year,month,1);
	var weekday = firstDayOfTheMonth.getDay()-startdate;//0 is sunday.
	var numberOfDaysThisMonth = GetNumberOfDays(month, year);
	var today_midnight = new Date(today.getFullYear(),today.getMonth(),today.getDate());
	var counter = 0;
	if (calendar=="arr"){
		othercalendar="dep";
	}
	else{
		othercalendar="arr";
	}



	/*************************************************************************************
	*                   the rest of the script, actual functions                         *
	*************************************************************************************/


	//first draw the Month select tool (dropdown+buttons)
	sText = drawMonthSelectTool(calendar,minimum_date_mil_secs,maximum_date_mil_secs,date_mil_secs);
	// than draw the daynames in the calendar
	sText = sText + drawDays();

	/***************************************************************************************/
	//than write the last days of the month before the selected date on the first row until we reach the weekday where the new month starts:
	sText=sText+"<div class='days'><ul>";
	var dateToWrite = new Date(0);

	for(i=weekday;i>0;i--){
		dateToWrite = new Date(year,month,1-i)
		if ((dateToWrite.getTime()<today.getTime())||(dateToWrite.getTime()>=maximum_date_mil_secs)||(dateToWrite.getTime()<=minimum_date_mil_secs)){
			//the date should not be selectable:
			complete_link = dateToWrite.getDate();
			class_name = "";
		}
		else{
			//it should be selectable:
			class_name = "";
			complete_link = "<a href=\"Javascript:ChangeDropDownCalendar("+Date.parse(dateToWrite)+",'"+calendar+"');UpdateDropdown('"+othercalendar+"');HideCalendar();\">"+dateToWrite.getDate()+"</a>";//
		}
		sText = sText + "<li class='"+class_name+"'>"+complete_link+"</li>";
		counter++;

	}
	/***************************************************************************************/
	//then we draw this month:
	for(i=1;i<=numberOfDaysThisMonth;i++){
		dateToWrite = new Date(year,month,i);
		//
		if((Date.parse(dateToWrite)<Date.parse(today_midnight))||(Date.parse(dateToWrite)<=minimum_date_mil_secs)||(Date.parse(dateToWrite)>=maximum_date_mil_secs)){
			//not selectable:
			class_name = "";
			complete_link = "<a href=\"#\">"+i+"</a>";
		}else{
			//selectable:
			class_name = "active";
			complete_link = "<a href=\"Javascript:ChangeDropDownCalendar("+Date.parse(dateToWrite)+",'"+calendar+"');UpdateDropdown('"+othercalendar+"');HideCalendar();\">"+dateToWrite.getDate()+"</a>";//
		}
		sText = sText+"<li class='"+class_name+"'>"+complete_link+"</li>";
		//alert (sText);
		counter++;
	}
	/***************************************************************************************/
	//and last draw the first days of the next month if the end of the last row is not reached yet:
	if(counter%7>0){
		j=0;
		for(i=counter%7;i<7;i++){
			dateToWrite = new Date(year,month+1,1+j);
			if (((Date.parse(dateToWrite)<Date.parse(today))||(dateToWrite.getMonth()<today.getMonth())&&(dateToWrite.getFullYear()<=today.getFullYear()))||(dateToWrite.getTime()<=minimum_date_mil_secs)||(dateToWrite.getTime()>=maximum_date_mil_secs)){
				//not selectable
				class_name = "";
				complete_link = dateToWrite.getDate();
			}
			else{
				class_name = "active.nextmonth";
				complete_link = "<a href=\"Javascript:ChangeDropDownCalendar("+Date.parse(dateToWrite)+",'"+calendar+"');UpdateDropdown('"+othercalendar+"');HideCalendar();\">"+dateToWrite.getDate()+"</a>";//
			}
			sText=sText+"<li class='"+class_name+"'>"+complete_link+"</li>";
			j++;
		}
	}
	/***************************************************************************************/
	sText=sText+"</ul></div>";
	sText=sText+"<a href=\"#\" onclick=\"this.parentNode.style.display = ''; return false;\" class=\"close-box\">"+closetext+"</a>";
	//alert (sText);

	eval("var div = document.getElementById('"+container+"')");
	div.innerHTML=sText;
	sText="";
}

function ChangeNumberOfDays(calendar){
	//this function updates the number of days in a dropdownlist on a given calendar
	eval("selectbox_d = document.getElementById('"+calendar+"_day');");
	eval("var selectbox_my = document.getElementById('"+calendar+"_month_year');");

	var month_year = selectbox_my.options[selectbox_my.selectedIndex].value;
	var selected_day_index = selectbox_d.selectedIndex;
	var month_year = month_year.split("-");
	var month = month_year[0]-1;
	var year = month_year[1];
	var max_days = GetNumberOfDays(month, year);

	var date = new Date();
	var day = "";
	selectbox_d.options.length=0;
	for(i=1;i<=max_days;i++){
		date = new Date(year,month,i);
		var index = date.getDay()-1;
		if(index<0){
			index=index+7;
		}
		selectbox_d.options[i-1] = new Option(i+" "+days_long[index],i);
	}
	//aantal dagen van de nieuwe maand opvragen en bekijken of selected_day_index niet groter is dan het aantal:

	if(selected_day_index>=max_days){
		selected_day_index = max_days-1;
	}
	selectbox_d.selectedIndex = selected_day_index;

}
function ChangeNumberOfDays2(calendar){
	//this function updates the number of days in a dropdownlist on a given calendar
	eval("selectbox_d = document.getElementById('"+calendar+"_day');");
	eval("var selectbox_my = document.getElementById('"+calendar+"_month');");

	var month_year = selectbox_my.options[selectbox_my.selectedIndex].value;
	var selected_day_index = selectbox_d.selectedIndex;
	var month_year = month_year.split("-");
	var month = month_year[0]-1;
	var year = month_year[1];
	var max_days = GetNumberOfDays(month, year);

	var date = new Date();
	var day = "";
	selectbox_d.options.length=0;
	for(i=1;i<=max_days;i++){
		date = new Date(year,month,i);
		var index = date.getDay()-1;
		if(index<0){
			index=index+7;
		}
		selectbox_d.options[i-1] = new Option(i+" "+days_long[index],i);
	}
	//aantal dagen van de nieuwe maand opvragen en bekijken of selected_day_index niet groter is dan het aantal:

	if(selected_day_index>=max_days){
		selected_day_index = max_days-1;
	}
	selectbox_d.selectedIndex = selected_day_index;

}
function GetNumberOfDays(month, year){
	return 32 - new Date(year, month, 32).getDate();

}
function ShowCalendar(){
	eval ("var div = document.getElementById('"+container+"')");
	div.style.display="block";
}
function HideCalendar(){
	eval ("var div = document.getElementById('"+container+"')");
	div.style.display="none";
}

function ChangeDropDownCalendar(date_mil_secs,calendar){
	//get the date from the popup calendar and put it in the dropdown calendar
	var date = new Date();
	date.setTime(date_mil_secs);
	var day = date.getDate();
	var month = date.getMonth();
	var year = date.getFullYear();

	//calculate the numbers of months in between the selected date and now in order to find which index of the select box should be selected.
	var today = new Date();
	var today_year = today.getFullYear();
	var today_month = today.getMonth();
	var difference = ((year-today_year)*12)+month-today_month;

	eval("var selectbox_d = document.getElementById('"+calendar+"_day');");
	eval("var selectbox_my = document.getElementById('"+calendar+"_month_year');");

	if (difference>=0){
		selectbox_my.selectedIndex = difference;
	}
	ChangeNumberOfDays(calendar);
	selectbox_d.selectedIndex = day-1;
}
function ChangeDropDownCalendar2(date_mil_secs,calendar){
	//get the date from the popup calendar and put it in the dropdown calendar
	var date = new Date();
	date.setTime(date_mil_secs);
	var day = date.getDate();
	var month = date.getMonth();
	var year = date.getFullYear();

	//calculate the numbers of months in between the selected date and now in order to find which index of the select box should be selected.
	var today = new Date();
	var today_year = today.getFullYear();
	var today_month = today.getMonth();
	var difference = ((year-today_year)*12)+month-today_month;

	eval("var selectbox_d = document.getElementById('"+calendar+"_day');");
	eval("var selectbox_my = document.getElementById('"+calendar+"_month');");
	if (difference>=0){
		selectbox_my.selectedIndex = difference;
	}
	ChangeNumberOfDays2(calendar);
	selectbox_d.selectedIndex = day-1;
}
function OpenPopUpCalendar_small(calendar){
	//read the date from the dropdowncalendar and open the popup calendar on this date:
/*	eval("var selectbox_d = document.getElementById('"+calendar+"_day');");
	eval("var selectbox_my = document.getElementById('"+calendar+"_month_year');");
	var monthyear = selectbox_my[selectbox_my.selectedIndex].value;
	monthyear = monthyear.split("-");
	var month = monthyear[0];
	var year = monthyear[1];
	var day = selectbox_d.selectedIndex+1;
	date_mil_secs = Date.parse(new Date(year,month-1,day));
*/
	date_mil_secs = GetDateFrom(calendar);
	DrawPopUpCalendar_small(date_mil_secs,calendar,0,0);
}

function UpdateDropdown(calendar){

	//updates the given dropdown calendar in case of dep_date<arr_date or arr_date>dep_date
	var arr_date = GetDateFrom('arr');
	var dep_date = GetDateFrom('dep');
	//alert(arr_date+" - "+dep_date);
	if((calendar=='arr')&&(arr_date>=dep_date)){
		//de arr date wordt 1 kleiner dan de dep date
		arr_date = dep_date-24*60*60*1000;
		ChangeDropDownCalendar(arr_date,calendar);
	}else if((calendar=="dep")&&(dep_date<=arr_date)){
		//de dep_date wordt 1 groter dan de arr date
		dep_date = arr_date+24*60*60*1000;
		ChangeDropDownCalendar(dep_date,calendar);
	}
}

function GetDateFrom(calendar){
	eval("var selectbox_d = document.getElementById('"+calendar+"_day');");
	eval("var selectbox_my = document.getElementById('"+calendar+"_month_year');");
	var monthyear = selectbox_my[selectbox_my.selectedIndex].value;
	monthyear = monthyear.split("-");
	var month = monthyear[0];
	var year = monthyear[1];
	var day = selectbox_d.selectedIndex+1;
	date_mil_secs = Date.parse(new Date(year,month-1,day));
	return date_mil_secs;
}

function InitCalendar(arrival, departure){
	if (arrival==''){
		arrival = Date.parse(new Date());
		departure = arrival+24*60*60*1000;
	}
	else{
		arrival=arrival.split("-");
		arrival=Date.parse(new Date(arrival[0],arrival[1]-1,arrival[2]));
		departure=departure.split("-");
		departure=Date.parse(new Date(departure[0],departure[1]-1,departure[2]));
	}
	ChangeDropDownCalendar(arrival,'arr');
	ChangeDropDownCalendar(departure,'dep');
	if(document.getElementById("arr_no_avail_day")){
		//voor de kalenders op het niet available stuk van de hotelpagina:
		ChangeDropDownCalendar(arrival,'arr_no_avail');
		ChangeDropDownCalendar(departure,'dep_no_avail');
	}
}
function InitCalendar2(arrival, departure){
	if (arrival==''){
		arrival = Date.parse(new Date());
		departure = arrival+24*60*60*1000;
	}
	else{
		arrival=arrival.split("-");
		arrival=Date.parse(new Date(arrival[0],arrival[1]-1,arrival[2]));
		departure=departure.split("-");
		departure=Date.parse(new Date(departure[0],departure[1]-1,departure[2]));
	}
	ChangeDropDownCalendar2(arrival,'b_checkin');
	ChangeDropDownCalendar2(departure,'b_checkout');
}

function Changepopupcalendar(element,calendar,maximum_date_mil_secs,minimum_date_mil_secs){
	//get the selected month and year:
	var monthyear = element[element.selectedIndex].value;
	monthyear = monthyear.split("-");
	var month = monthyear[0];
	var year = monthyear[1];
	date_mil_secs = Date.parse(new Date(year,month,2));
	DrawPopUpCalendar_small(date_mil_secs,calendar,minimum_date_mil_secs,maximum_date_mil_secs);
}

