var calendarsClicker = 0;
var outsideCalendarsClicker = true;

var cals_from;
var cals_to;

var cals_from_one_way;

var cals_from_1;
var cals_from_2;
var cals_from_3;
var cals_from_4;
var cals_hotel;
var cals_hotel_dep,cals_hotel_arr;

function clickOutsideCalendars() {

	if (outsideCalendarsClicker)
	{
		hideUnusedCalendars();
	}

	outsideCalendarsClicker = true;
}

function clickOnCalendars()
{
	outsideCalendarsClicker = false;
}

function hideUnusedCalendars(){
	try {	cals_from.hideCalendars();}catch (e){	}
	try {	cals_to.hideCalendars();}catch (e){	}
	try {	cals_from_one_way.hideCalendars();}catch (e){	}
	try {	cals_from_1.hideCalendars();}catch (e){	}
	try {	cals_from_2.hideCalendars();}catch (e){	}
	try {	cals_from_3.hideCalendars();}catch (e){	}
	try {	cals_from_4.hideCalendars();}catch (e){	}
	try {	cals_hotel.hideCalendars();}catch (e){	}	
	
	try {	cals_hotel_arr.hideCalendars();}catch (e){	}
	try {	cals_hotel_dep.hideCalendars();}catch (e){	}		
}

function createCalendars()
{
	//mierzeczas = new Date();
	if (document.getElementById("calendar_from") && document.getElementById("calendar_to"))
	{		
		cals_from = new Calendars(new Date(),null,null,null,"cals_from",document.getElementById("calendar_from"),12,2,"TWOPLACES",Array(
		"w_d",
		"w_m",
		"w_r",
		"date_from"),
		Array(document.getElementById("ile_stu_select"),
		document.getElementById("ile_dor_select"),
		document.getElementById("ile_mlo_select"),
		document.getElementById("ile_dzi_select"),
		document.getElementById("ile_nie_select"),
		document.getElementById("ile_sen_select"),
		document.getElementById("godz_w_select"),
		document.getElementById("godz_p_select"),
		document.getElementById("klasa_select")
		));
	
		cals_to = new Calendars(new Date(),null,null,null,"cals_to",document.getElementById("calendar_to"),12,2,"TWOPLACES",Array(
		"p_d",
		"p_m",
		"p_r",
		"date_to"),
		Array(document.getElementById("godz_p_select"),
		document.getElementById("ile_dzi_select"),
		document.getElementById("ile_mlo_select"),
		document.getElementById("ile_nie_select"),
		document.getElementById("ile_stu_select"))
		);
	
		cals_from.setRightCalendars(cals_to);
		cals_to.setLeftCalendars(cals_from);
	
		cals_from.builtCalendars();
		cals_to.builtCalendars();
	}
	
	
	if (document.getElementById("calendar_from_one_way"))
	{			
		cals_from_one_way = new Calendars(new Date(),null,null,null,"cals_from_one_way",document.getElementById("calendar_from_one_way"),12,2,"TWOPLACES",Array(
		"w_d",
		"w_m",
		"w_r",
		"date_from_one_way"),
		Array(document.getElementById("ile_stu_select"),
		document.getElementById("ile_dor_select"),
		document.getElementById("ile_mlo_select"),
		document.getElementById("ile_dzi_select"),
		document.getElementById("ile_nie_select"),
		document.getElementById("ile_sen_select"),
		document.getElementById("godz_w_select"),
		document.getElementById("godz_p_select"),
		document.getElementById("klasa_select")
		));
		
		cals_from_one_way.builtCalendars();
	}
	
	if (document.getElementById("calendar_from_1") && document.getElementById("calendar_from_2") && document.getElementById("calendar_from_3") && document.getElementById("calendar_from_4"))
	{		
		cals_from_1 = new Calendars(new Date(),null,null,null,"cals_from_1",document.getElementById("calendar_from_1"),12,2,"TWOPLACES",Array(
		"w_d",
		"w_m",
		"w_r",
		"date_from_1"),
		Array(document.getElementById("godz_w_select_2"))
		);

		cals_from_1.builtCalendars();

		cals_from_2 = new Calendars(new Date(),null,null,null,"cals_from_2",document.getElementById("calendar_from_2"),12,2,"TWOPLACES",Array(
		"w_d2",
		"w_m2",
		"w_r2",
		"date_from_2"),
		Array(document.getElementById("godz_w_select_3"))
		);

		cals_from_2.builtCalendars();

		cals_from_3 = new Calendars(new Date(),null,null,null,"cals_from_3",document.getElementById("calendar_from_3"),12,2,"TWOPLACES",Array(
		"w_d3",
		"w_m3",
		"w_r3",
		"date_from_3"),
		Array(document.getElementById("godz_w_select_4"),
		document.getElementById("ile_dor_select"),
		document.getElementById("ile_dzi_select"),
		document.getElementById("ile_nie_select"))
		);

		cals_from_3.builtCalendars();

		cals_from_4 = new Calendars(new Date(),null,null,null,"cals_from_4",document.getElementById("calendar_from_4"),12,2,"TWOPLACES",Array(
		"w_d4",
		"w_m4",
		"w_r4",
		"date_from_4"),
		Array(document.getElementById("ile_stu_select"),
		document.getElementById("ile_dor_select"),
		document.getElementById("ile_mlo_select"),
		document.getElementById("ile_dzi_select"),
		document.getElementById("ile_nie_select"),
		document.getElementById("ile_sen_select"),
		document.getElementById("klasa_select")
		)
		);
		cals_from_4.builtCalendars();
	
		cals_from_1.setRightCalendars(cals_from_2);
		cals_from_2.setRightCalendars(cals_from_3);
		cals_from_3.setRightCalendars(cals_from_4);
	}
	
	if (document.getElementById("calendar_hotel_dep"))
	{

		cals_hotel_dep = new Calendars(new Date(),null,null,null,"cals_hotel_dep",document.getElementById("calendar_hotel_dep"),12,2,"TWOPLACES",Array(
		"hotel_day_dep",
		"hotel_month_dep",
		"hotel_year_dep",
		"hotel_date_dep"),
		Array()
		);
		cals_hotel_dep.builtCalendars();	
	}

	if (document.getElementById("calendar_hotel_arr"))
	{
		cals_hotel_arr = new Calendars(new Date(),null,null,null,"cals_hotel_arr",document.getElementById("calendar_hotel_arr"),12,2,"TWOPLACES",Array(
		"hotel_day_arr",
		"hotel_month_arr",
		"hotel_year_arr",
		"hotel_date_arr"),
		Array()
		);
		cals_hotel_arr.builtCalendars();	
	}
	

	
	//zmierzony = new Date();
	//alert("utworzylem "+(zmierzony.getTime() - mierzeczas.getTime())+"ms");
}

function createCalendarsMiniform1()
{
	cals_from = new Calendars(new Date(),null,null,null,"cals_from",document.getElementById("calendar_from"),12,2,"INONEINPUT",Array("date_from"),Array(document.getElementById("godz_w_select"),document.getElementById("dokl_lub_plus_minus_z_w_jedna")));
	cals_from.builtCalendars();
	if (document.getElementById("promotionMiniform"))
	{
	cals_from_promotion = new Calendars(new Date(),null,null,null,"cals_from_promotion",document.getElementById("calendar_from_promotion"),12,2,"INONEINPUT",Array("date_from_promotion"),Array(document.getElementById("godz_w_select"),
			document.getElementById("godz_p_select"),
			document.getElementById("dokl_lub_plus_minus_z_w_jedna"),
			document.getElementById("klasa_select")));
	cals_from_promotion.builtCalendars();
	}
}

function createCalendarsMiniform2()
{
	
	if (document.getElementById("calendar_from_1"))
	{
		cals_from_1 = new Calendars(new Date(),null,null,null,"cals_from_1",document.getElementById("calendar_from_1"),12,2,"INONEINPUT",Array("date_from_1"),Array(document.getElementById("godz_w_select_1")));
		cals_from_2 = new Calendars(new Date(),null,null,null,"cals_from_2",document.getElementById("calendar_from_2"),12,2,"INONEINPUT",Array("date_from_2"),Array(document.getElementById("godz_w_select_2")));
		cals_from_3 = new Calendars(new Date(),null,null,null,"cals_from_3",document.getElementById("calendar_from_3"),12,2,"INONEINPUT",Array("date_from_3"),Array(document.getElementById("godz_w_select_3")));
		cals_from_4 = new Calendars(new Date(),null,null,null,"cals_from_4",document.getElementById("calendar_from_4"),12,2,"INONEINPUT",Array("date_from_4"),Array(document.getElementById("godz_w_select_4"),document.getElementById("klasa_select")));		
		
		cals_from_1.builtCalendars();
		cals_from_2.builtCalendars();
		cals_from_3.builtCalendars();
		cals_from_4.builtCalendars();
		
		cals_from_1.setRightCalendars(cals_from_2);
		cals_from_2.setRightCalendars(cals_from_3);
		cals_from_3.setRightCalendars(cals_from_4);
	}	
	else
	{
		if (document.getElementById("miniform"))
		{
			cals_from = new Calendars(new Date(),null,null,null,"cals_from",document.getElementById("calendar_from"),12,2,"INONEINPUT",Array("date_from"),Array(document.getElementById("godz_w_select"),
			document.getElementById("godz_p_select"),
			document.getElementById("dokl_lub_plus_minus_z_w_jedna"),
			document.getElementById("klasa_select")));
			
			cals_to = new Calendars(new Date(),null,null,null,"cals_to",document.getElementById("calendar_to"),12,2,"INONEINPUT",Array("date_to"),Array(document.getElementById("godz_p_select"),
			document.getElementById("dokl_lub_plus_minus_z_w_jedna"),
			document.getElementById("klasa_select")));
			
			cals_from.setRightCalendars(cals_to);
			cals_to.setLeftCalendars(cals_from);
			
			cals_from.builtCalendars();
			cals_to.builtCalendars();
		}
		if (document.getElementById("promotionMiniform"))
		{
			cals_from = new Calendars(new Date(),null,null,null,"cals_from",document.getElementById("calendar_from"),12,2,"INONEINPUT",Array("date_from"),Array(document.getElementById("godz_w_select"),
			document.getElementById("godz_p_select"),
			document.getElementById("dokl_lub_plus_minus_z_w_jedna"),
			document.getElementById("klasa_select")));
			
			cals_to = new Calendars(new Date(),null,null,null,"cals_to",document.getElementById("calendar_to"),12,2,"INONEINPUT",Array("date_to"),Array(document.getElementById("godz_p_select"),
			document.getElementById("dokl_lub_plus_minus_z_w_jedna"),
			document.getElementById("klasa_select")));
			
			cals_from.setRightCalendars(cals_to);
			cals_to.setLeftCalendars(cals_from);
			
			cals_from.builtCalendars();
			cals_to.builtCalendars();
		}
	}

	if (document.getElementById("calendar_hotel_dep") && document.getElementById("calendar_hotel_arr"))
	{

		cals_hotel_dep = new Calendars(new Date(),null,null,null,"cals_hotel_dep",document.getElementById("calendar_hotel_dep"),12,2,"TWOPLACES",Array(
		"hotel_day_dep",
		"hotel_month_dep",
		"hotel_year_dep",
		"hotel_date_dep"),
		Array()
		);
		cals_hotel_dep.builtCalendars();	

		cals_hotel_arr = new Calendars(new Date(),null,null,null,"cals_hotel_arr",document.getElementById("calendar_hotel_arr"),12,2,"TWOPLACES",Array(
		"hotel_day_arr",
		"hotel_month_arr",
		"hotel_year_arr",
		"hotel_date_arr"),
		Array()
		);
		cals_hotel_arr.builtCalendars();	

		//cals_hotel_dep.setRightCalendars(cals_hotel_arr);
		
		cals_hotel_arr.setRightCalendars(cals_hotel_dep);

	}

}

//************* kalendarze dla HOTELI ******************

function createHotelsCalendars()
{
	cals_hotel_dep = new Calendars(new Date(),null,null,null,"cals_hotel_dep",document.getElementById("calendar_hotel_dep"),12,2,"TWOPLACES",Array(
		"hotel_day_dep",
		"hotel_month_dep",
		"hotel_year_dep",
		"hotel_date_dep"),
		Array()
		);
	cals_hotel_dep.builtCalendars();	

	cals_hotel_arr = new Calendars(new Date(),null,null,null,"cals_hotel_arr",document.getElementById("calendar_hotel_arr"),12,2,"TWOPLACES",Array(
		"hotel_day_arr",
		"hotel_month_arr",
		"hotel_year_arr",
		"hotel_date_arr"),
		Array()
		);
	cals_hotel_arr.builtCalendars();	

		//cals_hotel_dep.setRightCalendars(cals_hotel_arr);
	cals_hotel_arr.setRightCalendars(cals_hotel_dep);
}

//************* kalendarze dla PROMOCJI ******************

function createPromotionsCalendars(from,to)
{
	var temp = from.split('-');
	var date = new Date();
	
	date.setFullYear(temp[0],temp[1]-1,temp[2]);
	
	var today = new Date();
	
	if (date<today)
		var prom_begin = today;
	else
		var prom_begin = date;// data poczatku promocji
			
	temp = to.split('-');
	date = new Date();
	
	date.setFullYear(temp[0],temp[1]-1,temp[2]);
		
	
	var prom_end = date; // data konca promocji

	cals_from = new Calendars(prom_begin,prom_end,null,null,"cals_from",document.getElementById("calendar_from"),12,2,"INONEINPUT",Array("date_from"),Array(document.getElementById("godz_w_select")));
	cals_from.builtCalendars();	

	cals_to = new Calendars(prom_begin,prom_end,null,null,"cals_to",document.getElementById("calendar_to"),12,2,"INONEINPUT",Array("date_to"),Array(document.getElementById("godz_p_select")));
	cals_to.builtCalendars();	

		//cals_hotel_dep.setRightCalendars(cals_hotel_arr);
	cals_from.setRightCalendars(cals_to);
	cals_to.setLeftCalendars(cals_from);		
	//cals_to.setRightCalendars(cals_from);
}





function createCalendarsErrorform()
{
	if (document.getElementById("calendar_from"))
	{
		cals_from = new Calendars(new Date(),null,null,null,"cals_from",document.getElementById("calendar_from"),12,2,"INONEINPUT",Array("date_from"));
		
		cals_from.builtCalendars();
		if (!document.getElementById("calendar_to"))
			cals_from.addActionAfterClick("przepisz('w',document.getElementById('date_from').value);");
	}
	
	if (document.getElementById("calendar_to"))
	{
		cals_to = new Calendars(new Date(),null,null,null,"cals_to",document.getElementById("calendar_to"),12,2,"INONEINPUT",Array("date_to"));
		cals_to.builtCalendars();
		cals_to.addActionAfterClick("przepisz('p',document.getElementById('date_to').value);");
	}
	
	if (document.getElementById("calendar_from") && document.getElementById("calendar_to"))
	{
		cals_from.setRightCalendars(cals_to);
		cals_to.setLeftCalendars(cals_from);
		
		cals_from.addActionAfterClick("przepisz('w',document.getElementById('date_from').value); if (cals_to.selectedDate == cals_to.startDate) { document.getElementById('date_to').value = document.getElementById('date_from').value;}");
	}
	
	
	if (document.getElementById("calendar_from_2"))
	{
		cals_from_2 = new Calendars(new Date(),null,null,null,"cals_from_2",document.getElementById("calendar_from_2"),12,2,"INONEINPUT",Array("date_from_2"));
		cals_from_2.builtCalendars();
		cals_from_2.addActionAfterClick("przepisz('w2',document.getElementById('date_from_2').value);");
	}
	
	if (document.getElementById("calendar_from_3"))
	{
		cals_from_3 = new Calendars(new Date(),null,null,null,"cals_from_3",document.getElementById("calendar_from_3"),12,2,"INONEINPUT",Array("date_from_3"));
		cals_from_3.builtCalendars();
		cals_from_3.addActionAfterClick("przepisz('w3',document.getElementById('date_from_3').value);");
	}
	
	if (document.getElementById("calendar_from_4"))
	{
		cals_from_4 = new Calendars(new Date(),null,null,null,"cals_from_4",document.getElementById("calendar_from_4"),12,2,"INONEINPUT",Array("date_from_4"));
		cals_from_4.builtCalendars();
		cals_from_4.addActionAfterClick("przepisz('w4',document.getElementById('date_from_4').value);");
	}

}



function Calendars(
	startDate,
	endDate,
	firstDate,
	selectedDate,
	objName,
	destinationObj,
	countCalendars,
	countShowCalendars,
	destinationTypeDate,
	destinationIdDate,
	elementsToDisable
)
{
	this.className = "Calendars";
	this.version = "1.0";
	
	//od jakiego dnia można wybrać date
	this.startDate = startDate;
	//do jakiego dnia można wybrać date
	this.endDate = endDate;
	//jaki jest pierwszy miesiąc
	if (firstDate == null)
		this.firstDate = new Date();
	else
		this.firstDate = firstDate;
	//data zaznaczona
	this.selectedDate = selectedDate;
	
	this.destinationObj = destinationObj;
	
	this.destinationObj.className = "calendars";
	
	this.objName = objName;

	//ilosc kalendarzy
	this.countCalendars = countCalendars;
	//ilosc widocznych kalendarzy
	this.countShowCalendars = countShowCalendars;

	//przechowuje tablice idków w których znajdują się kalendarze
	this.calendarsArrayObj = Array();
	
	//id obiektu(ów) w którym należy umieścić kalendarz
	this.destinationIdDate = destinationIdDate;
	//typ daty jaki ma być zwrócony
	this.destinationTypeDate = destinationTypeDate;
	
	//zapamietuje który był wcześniej zaznaczony żeby wiedzieć który kalendarz przebudować
	this.prevSelected = null;
	
	//ustalenie zaleznosci miedzy kalendarzami
	this.leftCalendars = null;
	this.rightCalendars = null;
	
	//elementy które trzeba wyłączyć bo prześwitują (problem selectów w IE)
	if (elementsToDisable)
		this.elementsToDisable = elementsToDisable;
	else
		this.elementsToDisable = Array();
	
	//przechowuje index pierwszego i ostatniego kalendarza który jest widoczny
	this.shownCalendars = Array();
	
	//przechowuje obiekty z kalendarzami
	this.calendars = Array();
	
	this.ie = false;
	if(navigator.appName == "Microsoft Internet Explorer")
		this.ie=true;

	this.builtCalendars=function()
	{
		var buffor = "";

		//buffor += "<div class=\"upperHeader\"><div class=\"upperHeaderText\">okresl date wylotu</div><div class=\"upperHeaderClose\">zamknij</div><div style=\"clear:both\"></div></div>";
		//buffor += "<div class=\"navigationHeader\"><div class=\"navigationHeaderPrevBtn\"><a href=\"javascript:"+this.objName+".prevMonth()\">prev</a></div><div class=\"navigationHeaderNextBtn\"><a href=\"javascript:"+this.objName+".nextMonth()\">next</a></div><div style=\"clear:both;\"></div></div>";
		buffor += "<table class=\"calendarsTable\" >";
		
		if (this.destinationObj.id=="calendar_hotel_dep" || this.destinationObj.id=="calendar_hotel_arr")
		{//wstawka dla minisercza hotelowego aby mial inny kolor
			buffor += "<tr><td colspan=\"2\" class=\"red_upperHeader\"><div class=\"red_upperHeaderText\">określ datę</div><img class=\"red_upperHeaderClose\" src=\"https://www.fru.pl/img/FRU/PL/red_close.gif\" onclick=\""+this.objName+".hideCalendars()\" /> <div class=\"red_upperHeaderClose\" onclick=\""+this.objName+".hideCalendars()\">zamknij</div><div class=\"red_clearBoth\"></div></td></tr>";
			buffor += "<tr><td class=\"red_naviLeft\"><img src=\"https://www.fru.pl/img/FRU/PL/red_prevMonth.gif\" class=\"red_changeMonthPrev\" onclick=\""+this.objName+".prevMonth()\"/><div class=\"red_nameMonthPrev\" id=\""+this.objName+"_nameMont1\">Nazwa miesiaca</div></td><td class=\"red_naviRight\"><img src=\"https://www.fru.pl/img/FRU/PL/red_nextMonth.gif\" class=\"red_changeMonthNext\" onclick=\""+this.objName+".nextMonth()\" /><div class=\"red_nameMonthNext\" id=\""+this.objName+"_nameMont2\">Nazwa miesiaca</div></td></tr>"
		}
		else
		{
			buffor += "<tr><td colspan=\"2\" class=\"upperHeader\"><div class=\"upperHeaderText\">określ datę</div><img class=\"upperHeaderClose\" src=\"https://www.fru.pl/img/FRU/PL/close.gif\" onclick=\""+this.objName+".hideCalendars()\" /> <div class=\"upperHeaderClose\" onclick=\""+this.objName+".hideCalendars()\">zamknij</div><div class=\"clearBoth\"></div></td></tr>";
			buffor += "<tr><td class=\"naviLeft\"><img src=\"https://www.fru.pl/img/FRU/PL/prevMonth.gif\" class=\"changeMonthPrev\" onclick=\""+this.objName+".prevMonth()\"/><div class=\"nameMonthPrev\" id=\""+this.objName+"_nameMont1\">Nazwa miesiaca</div></td><td class=\"naviRight\"><img src=\"https://www.fru.pl/img/FRU/PL/nextMonth.gif\" class=\"changeMonthNext\" onclick=\""+this.objName+".nextMonth()\" /><div class=\"nameMonthNext\" id=\""+this.objName+"_nameMont2\">Nazwa miesiaca</div></td></tr>"
		}			
		buffor += "<tr>";
		for (var i=0; i<this.countCalendars; i++)
		{
			buffor += "<td class=\"calendar\" id=\""+this.objName+"_calendar_"+i+"\"></td>";
		}
		buffor += "</tr>";
		//buffor += "<div style=\"clear:both\"</div>";
		this.destinationObj.innerHTML = buffor;
		buffor = "";
		
		for (var i=0; i<this.countCalendars; i++)
		{
			this.calendarsArrayObj[this.calendarsArrayObj.length] = document.getElementById(this.objName+"_calendar_"+i);
		}
		
		dateTmp = this.firstDate;
		dateTmp.setDate(1);

		for (var i=0; i<this.countCalendars; i++)
		{   //jesli kalendarz jest dla hotelu - wowczas tworzymy new Calendar z flaga==red, wpp flaga==blue
			if (this.destinationObj.id=="calendar_hotel_dep" || this.destinationObj.id=="calendar_hotel_arr")
				this.calendars[this.calendars.length] = new Calendar(this.startDate,this.endDate,null,null,null,dateTmp,null,document.getElementById(this.objName+"_calendar_"+i),this.destinationTypeDate,this.destinationIdDate,"red");
			else
				this.calendars[this.calendars.length] = new Calendar(this.startDate,this.endDate,null,null,null,dateTmp,null,document.getElementById(this.objName+"_calendar_"+i),this.destinationTypeDate,this.destinationIdDate,"blue");
			//this.calendars[this.calendars.length-1].showMonthAndYearHeader = true;
			this.calendars[this.calendars.length-1].afterOnclick = this.objName+".hideCalendars();"+this.objName+".newSelectedDay(true);";
			//this.calendars[this.calendars.length-1].builtCalendar();
			dateTmp.setMonth(dateTmp.getMonth()+1);
		}
		//this.newSelectedDay(true);
		//dateTmp = this.checkDateFromDestinationInput();
	}

	this.displayCalendars=function()
	{
	//alert('??');
		clickOnCalendars();
		hideUnusedCalendars();
		this.hideElements();
		dateTmp = this.getDateFromDestinationInput();
		if (this.startDate != null)
		{
			if (this.startDate > dateTmp)
				dateTmp = this.startDate;
		}

		for (var i=0; i<this.calendars.length;i++)
		{
			if ( (this.calendars[i].getMonth() == dateTmp.getMonth()) && (this.calendars[i].getYear() == dateTmp.getFullYear()) )
			{
				this.shownCalendars["left"] = i;
				this.shownCalendars["right"] = i+this.countShowCalendars-1;
				while(!this.calendarsArrayObj[this.shownCalendars["right"]])
				{
					this.shownCalendars["left"]--;
					this.shownCalendars["right"]--;
				}
				break;
			}
		}
		if (!this.shownCalendars["left"])
		{
			this.shownCalendars["left"] = 0;
			this.shownCalendars["right"] = this.countShowCalendars-1;
		}

		var index = 1;
		for (var i=this.shownCalendars["left"]; i<=this.shownCalendars["right"]; i++)
		{
			this.newSelectedDay(true);
			if (!this.calendars[i].isBuild)
				this.calendars[i].builtCalendar();
							
			if (this.ie)
				this.calendarsArrayObj[i].style.display="block";
			else
				this.calendarsArrayObj[i].style.display="table-cell";

			document.getElementById(this.objName+"_nameMont"+index).innerHTML = this.calendars[i].getMonthName()+" "+this.calendars[i].getYear();
			index++;
		}
		
		this.destinationObj.style.display="block";

	}
	
	this.hideCalendars=function()
	{
		this.showElements();
		this.destinationObj.style.display="none";
		for (var i=this.shownCalendars["left"]; i<=this.shownCalendars["right"]; i++)
		{
			this.calendarsArrayObj[i].style.display="none";
		}
		this.shownCalendars = Array();
		
	}
		
	this.hideElements=function()
	{
	
		for (var i=0; i<this.elementsToDisable.length; i++)
		{
			if(this.elementsToDisable[i]) this.elementsToDisable[i].style.visibility="hidden";
			//alert(this.elementsToDisable[i].id);
		}
			//this.elementsToDisable[i].style.display="none";
			
	}
	
	this.showElements=function()
	{
	
		for (var i=0; i<this.elementsToDisable.length; i++)
		{
			if(this.elementsToDisable[i]) this.elementsToDisable[i].style.visibility="visible";
			//this.elementsToDisable[i].style.display="display";
		}
		
	}
	
	/**
	 *
	 */
	this.newSelectedDay=function(informDependencies)
	{
		if (this.prevSelected == null)
		{
			this.prevSelected = this.getDateFromDestinationInput();
			this.rebuildMonth(this.prevSelected,this.prevSelected);
			this.selectedDate = this.prevSelected;
		}
		else
		{
			var dateTmp = this.getDateFromDestinationInput();
			//jesli kliknelismy w ten sam miesiac to przebudowyujemy tylko jeden miesiac
			if ((dateTmp.getMonth() == this.prevSelected.getMonth()) && (dateTmp.getFullYear() == this.prevSelected.getFullYear()))
			{
				this.rebuildMonth(dateTmp,dateTmp);
				this.prevSelected = dateTmp;
			}
			else
			{
				this.rebuildMonth(this.prevSelected,dateTmp);
				this.rebuildMonth(dateTmp,dateTmp);
				this.prevSelected = dateTmp;
			}

		}

		this.selectedDate = this.prevSelected;		
		if (informDependencies)
			this.informDependencies();
			
		if (this.destinationObj.id=="calendar_hotel_dep" || this.destinationObj.id=="calendar_hotel_arr")
		{//wstawka dla minisercza hotelowego

			var day_arr   = parseInt(document.getElementById('hotel_day_arr').value);
			var month_arr = parseInt(document.getElementById('hotel_month_arr').value);
			var year_arr  = parseInt(document.getElementById('hotel_year_arr').value);
			
			var day_dep   = parseInt(document.getElementById('hotel_day_dep').value);
			var month_dep = parseInt(document.getElementById('hotel_month_dep').value);
			var year_dep  = parseInt(document.getElementById('hotel_year_dep').value);						
									

			if ((day_arr == day_dep && month_arr == month_dep && year_arr == year_dep) ||
			    (year_arr>year_dep || (year_arr==year_dep && month_arr>month_dep) ||
			         (year_arr==year_dep && month_arr==month_dep && day_arr>day_dep)))
			{
					var date = new Date();
					//alert(document.getElementById('hotel_day_dep').value);
    				date.setFullYear(year_arr); 
    				date.setMonth(month_arr-1);
    				date.setDate(day_arr+1);

    				var d = document.getElementById('hotel_day_dep').value = date.getDate();
    				var m = document.getElementById('hotel_month_dep').value = date.getMonth()+1;
    				var y = document.getElementById('hotel_year_dep').value = date.getFullYear();   
    				
    				document.getElementById('hotel_date_dep').value = d+'/'+m+'/'+y;
			}

			try
			{ 
			
				policz_noce();
				
				if (this.destinationObj.id=="calendar_hotel_arr" && document.getElementById('nights_number').innerHTML=="0")
				{

					var date = new Date();
					//alert(document.getElementById('hotel_day_dep').value);
    				date.setFullYear(document.getElementById('hotel_year_arr').value);
    				date.setMonth(parseInt(document.getElementById('hotel_month_arr').value)-1);
    				date.setDate(parseInt(document.getElementById('hotel_day_arr').value)+1);

    				//alert(document.getElementById('hotel_date_arr').value);
    				//alert(date.getDate()+'-'+(date.getMonth()+1)+'-'+date.getFullYear());
    				
    				var d = document.getElementById('hotel_day_dep').value = date.getDate();
    				var m = document.getElementById('hotel_month_dep').value = date.getMonth()+1;
    				var y = document.getElementById('hotel_year_dep').value = date.getFullYear();
    				
    				document.getElementById('hotel_date_dep').value = d+'/'+m+'/'+y;
    				document.getElementById('nights_number').innerHTML="1";
	
				}					
			
			}catch (e){}
			

			
			
		}			
	}
	
	this.informDependencies=function()
	{
		if (this.leftCalendars != null)
			this.leftCalendars.informFromRightCalendar(this.selectedDate);
			
		if (this.rightCalendars != null)
			this.rightCalendars.informFromLeftCalendar(this.selectedDate);
	}
	
	this.informFromLeftCalendar=function(startDate)
	{		
		if(this.selectedDate == null) this.newSelectedDay();
		if (startDate > this.selectedDate)
			{
				this.selectedDate = startDate;
				this.setHiddenInputFromSelectedDate();
			}
		
		this.rebuildMonthByStartDate(startDate);
	}
	
	this.informFromRightCalendar=function(endDate)
	{
		//alert(this.objName+" - otyrzymalem od prawego "+endDate);
	}
	
	this.rebuildMonth=function(dateToRebuild,selectedDate)
	{
		//znalezienie kalendarza ktory wymaga przebudowania
		for (var i=0; i<this.calendars.length; i++)
		{
			if ( (this.calendars[i].getMonth() == dateToRebuild.getMonth()) && (this.calendars[i].getYear() == dateToRebuild.getFullYear()))
			{
				this.calendars[i].setSelectedDate(selectedDate);
				this.calendars[i].builtCalendar();
				break;
			}
		}
	}
	
	this.rebuildMonthByStartDate=function(startDate)
	{
		if (this.startDate == null)
		{
			var i = 0;
			while(
					this.calendars[i]
					&& 
					(
						(
							(this.calendars[i].getMonth() <= startDate.getMonth())
							&& 
							(this.calendars[i].getYear() == startDate.getFullYear())
						)
						||
						(
							this.calendars[i].getYear() < startDate.getFullYear()
						)
					)
				)
			{
				this.calendars[i].setStartDate(startDate);
				this.calendars[i].setShowDate(startDate);
				this.calendars[i].builtCalendar();
				i++;
			}
			this.startDate = startDate;
		}
		else
		{
			var i = 0;
			dateTmp = this.startDate;
			if (this.startDate < startDate)
				dateTmp = startDate;
			while(
					this.calendars[i]
					&& 
					(
						(
							(this.calendars[i].getMonth() <= dateTmp.getMonth())
							&& 
							(this.calendars[i].getYear() == dateTmp.getFullYear())
						)
						||
						(
							this.calendars[i].getYear() < dateTmp.getFullYear()
						)
					)
				)
			{
				this.calendars[i].setStartDate(startDate);
				this.calendars[i].setShowDate(startDate);
				this.calendars[i].builtCalendar();
				i++;
			}
			this.startDate = startDate;
		}
		
		if (this.rightCalendars != null)
			this.rightCalendars.informFromLeftCalendar(startDate);
	}
	
	this.prevMonth=function()
	{
		if (this.isPrevMonth())
		{
			this.calendarsArrayObj[this.shownCalendars["right"]].style.display="none";
			
			if (!this.calendars[this.shownCalendars["left"]-1].isBuild)
				this.calendars[this.shownCalendars["left"]-1].builtCalendar();
				
			if (this.ie)
				this.calendarsArrayObj[this.shownCalendars["left"]-1].style.display="block";
			else
				this.calendarsArrayObj[this.shownCalendars["left"]-1].style.display="table-cell";
			this.shownCalendars["right"]--;
			this.shownCalendars["left"]--;
			
			var index=1;
			for (var i=this.shownCalendars["left"]; i<=this.shownCalendars["right"]; i++)
			{
				document.getElementById(this.objName+"_nameMont"+index).innerHTML = this.calendars[i].getMonthName()+" "+this.calendars[i].getYear();
				index++;
			}
		}
	}
	
	this.nextMonth=function()
	{
		if (this.isNextMonth())
		{
			this.calendarsArrayObj[this.shownCalendars["left"]].style.display="none";
			if (!this.calendars[this.shownCalendars["right"]+1].isBuild)
				this.calendars[this.shownCalendars["right"]+1].builtCalendar();
			if (this.ie)
				this.calendarsArrayObj[this.shownCalendars["right"]+1].style.display="block";
			else
				this.calendarsArrayObj[this.shownCalendars["right"]+1].style.display="table-cell";
			this.shownCalendars["right"]++;
			this.shownCalendars["left"]++;
			
			var index=1;
			for (var i=this.shownCalendars["left"]; i<=this.shownCalendars["right"]; i++)
			{
				document.getElementById(this.objName+"_nameMont"+index).innerHTML = this.calendars[i].getMonthName()+" "+this.calendars[i].getYear();
				index++;
			}
		}
	}
	
	this.isPrevMonth=function()
	{
		if (this.calendarsArrayObj[this.shownCalendars["left"]-1])
			return true;	
		return false;
	}
	
	this.isNextMonth=function()
	{
		if (this.calendarsArrayObj[this.shownCalendars["right"]+1])
			return true;	
		return false;
	}
	
	
	this.setHiddenInputFromSelectedDate=function()
	{
		
		switch(this.destinationTypeDate)
		{
			case "STANDARD" :
			case "TWOPLACES" :
				document.getElementById(destinationIdDate[0]).value = this.selectedDate.getDate();
				document.getElementById(destinationIdDate[1]).value = this.selectedDate.getMonth()+1;
				document.getElementById(destinationIdDate[2]).value = this.selectedDate.getFullYear();
				//if (this.checkDate(day,month,year))  
				//	returnDate.setFullYear(year,month-1,day);

			case "INONEINPUT" :
			//alert(destinationIdDate[3]);
			if (document.getElementById(destinationIdDate[3]))
				document.getElementById(destinationIdDate[3]).value = this.selectedDate.getDate()+"/"+(this.selectedDate.getMonth()+1)+"/"+this.selectedDate.getFullYear();
				//var explode = dateString.split("/");
				//if (explode.length == 3)
				//{
				//	returnDate.setFullYear(explode[2],explode[1]-1,explode[0]);
				//} 
		}
		//if (document.getElementById("calendar_hotel_dep") && document.getElementById("calendar_hotel_arr"))
		//{//jesli mamy kalendarze w miniserczu hotelowym
			//policz_noce();
			//alert('?');
		//	;
		//}
		
		//alert(destinationIdDate);
		//alert('przestaw hiddeny '+this.selectedDate);
	}

	this.getDateFromDestinationInput=function()
	{
		var returnDate = new Date();
		switch(this.destinationTypeDate)
		{
			case "STANDARD" :
			case "TWOPLACES" :
				day = document.getElementById(destinationIdDate[0]).value;
				month = document.getElementById(destinationIdDate[1]).value;
				year = document.getElementById(destinationIdDate[2]).value;
				if (this.checkDate(day,month,year))  
					returnDate.setFullYear(year,month-1,day);

			case "INONEINPUT" :
				dateString = document.getElementById(destinationIdDate[0]).value;
				var explode = dateString.split("/");
				if (explode.length == 3)
				{
					returnDate.setFullYear(explode[2],explode[1]-1,explode[0]);
				} 
		}

		this.monthToDisplay = returnDate.getMonth();
		this.yearToDisplay = returnDate.getFullYear();
		if (this.startDate != null)
		{
			if (returnDate < this.startDate)
			{
				this.monthToDisplay = this.startDate.getMonth();
				this.yearToDisplay = this.startDate.getFullYear();
			}
		}
		if (this.endDate != null)
		{
			if (returnDate > this.endDate)
			{
				this.monthToDisplay = this.endDate.getMonth();
				this.yearToDisplay = this.endDate.getFullYear();
			}
		}
		return returnDate;
	}
	
	this.checkDateFromDestinationInput=function()
	{
		switch(this.destinationTypeDate)
		{
			case "STANDARD" :
				day = document.getElementById(destinationIdDate[0]).value;
				month = document.getElementById(destinationIdDate[1]).value;
				year = document.getElementById(destinationIdDate[2]).value;
				if (this.checkDate(day,month,year))  
					return true

			case "INONEINPUT" :
				dateString = document.getElementById(destinationIdDate[0]).value;
				var explode = dateString.split("/");
				if (explode.length == 3)
				{
					returnDate.setFullYear(explode[2],explode[1]-1,explode[0]);
				} 
		}
		return false;
	}
	
	this.checkDate=function(day,month,year)
	{
		if ((day > 0) && (day < 32) && (month >= 0) && (month <= 12) && (year > 1990) && (year < 2050)) 
			return true;
		else
			return false;
	}
	this.setLeftCalendars=function(calendar)
	{
		this.leftCalendars = calendar;
	}
	
	this.setRightCalendars=function(calendar)
	{
		this.rightCalendars = calendar;
		
	}
	
	this.addActionAfterClick=function(action)
	{
		for (var i=0; i<this.calendars.length; i++)
		{
			this.calendars[i].afterOnclick += action+";";
		}
	}
	
}



function wstaw_date(destination,ile_dni)
{
    if (!document.getElementById(destination[0][0]) || !document.getElementById(destination[0][1]) || !document.getElementById(destination[0][2]))
    	return;

	d1 = document.getElementById(destination[0][0]).value;
	m1 = document.getElementById(destination[0][1]).value;
	r1 = document.getElementById(destination[0][2]).value;
	
	if ((d1=="") && (m1=="") && (r1==""))
	{
		data = new Date();
		milisec = data.getTime();
		
		data.setTime((milisec+(ile_dni*1000*60*60*24)));
		
		for (var i=0; i<destination.length; i++)
		{
			if (destination[i].length == 3)
			{
				document.getElementById(destination[i][0]).value = format(data.getDate());
				document.getElementById(destination[i][1]).value = format(data.getMonth()+1);
				document.getElementById(destination[i][2]).value = data.getFullYear();
			}
			else
			{
				var date = format(data.getDate())+"/"+format(data.getMonth()+1)+"/"+data.getFullYear();
				document.getElementById(destination[i][0]).value = 	date;
			}
		}
	}		
}

function format(str)
{
	if (str < 10)
		str = "0"+str;
	return str;
}

function wstaw_daty()
{
	wstaw_date(
		Array(
			Array('w_d','w_m','w_r'),
			Array('date_from'),
			Array('date_from_one_way'),
			Array('date_from_1')
			)
		,3); 
		
	wstaw_date(
		Array(
			Array('p_d','p_m','p_r'),
			Array('date_to')
			)
		,10);
			
		/*
	wstaw_date(
		Array(
			Array('w_d2','w_m2','w_r2'),
			Array('date_from_2')
			)
		,10); 
		
	wstaw_date(
		Array(
			Array('w_d3','w_m3','w_r3'),
			Array('date_from_3')
			)
		,17);  */
		/*
	wstaw_date(
		Array(
			Array('w_d4','w_m4','w_r4'),
			Array('date_from_4')
			)
		,24); */

	wstaw_date(
		Array(
			Array('hotel_day_arr','hotel_month_arr','hotel_year_arr'),
			Array('hotel_date_arr')
			)
		,3);

	wstaw_date(
		Array(
			Array('hotel_day_dep','hotel_month_dep','hotel_year_dep'),
			Array('hotel_date_dep')
			)
		,4);	

}

function getFromUrl()
{
	if(location.search)
	{
		var fromDate = location.href.split('fromDate=')[1].split('&')[0];
		var toDate = location.href.split('toDate=')[1].split('&')[0];
		var destCity = location.href.split('destCity=')[1].split('&')[0];
	
		var fDay = fromDate.split('-')[2]
		var fMonth = fromDate.split('-')[1];
		var fYear = fromDate.split('-')[0];
	
		var tDay = toDate.split('-')[2]
		var tMonth = toDate.split('-')[1];
		var tYear = toDate.split('-')[0];
	
		document.getElementById('b_destination').value = decodeURI(destCity);
	
		document.getElementById('hotel_date_arr').value = fDay+"/"+fMonth+"/"+fYear;
		document.getElementById('hotel_day_arr').value = fDay;
		document.getElementById('hotel_month_arr').value = fMonth;
		document.getElementById('hotel_year_arr').value = fYear;

	
		document.getElementById('hotel_date_dep').value = tDay+"/"+tMonth+"/"+tYear;
		document.getElementById('hotel_day_dep').value = tDay;
		document.getElementById('hotel_month_dep').value = tMonth;
		document.getElementById('hotel_year_dep').value = tYear;	
	}
} 