var TablicaObrazkow = new Array();
var TablicaIdkow = new Array();

function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width - w)/2
	TopPosition = 60;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
	window.open(mypage,myname,settings);
}

function HrefOn(a,td){
	a.style.color = "#3c5b80"
	document.getElementById(td).style.backgroundColor = '#c1d1e3';
}

function HrefOff(a,td){
	a.style.color = "#FFF"
	document.getElementById(td).style.backgroundColor = "#9bb4d2";
}

function WczytajDate(pole, dzis){
	if(pole.value == "yyyy-mm-dd" || pole.value == "rrrr-mm-dd"){
		pole.value = dzis;
	}
}

function ObliczCene(APID){
	document.getElementById('searching_ilosc_osob').value = document.getElementById('price_ilosc_osob').value;
	document.rezerwuje.target='orderPrice';
	document.rezerwuje.action='/oblicz_cene.php?apid='+APID;
	document.rezerwuje.submit();
}

function SubmitForm(APID){
	document.rezerwuje.target='';
	document.rezerwuje.action='/apartamenty,mode,reservation,apid,'+APID+'.html';
	document.rezerwuje.submit();
}

function ZmienKolor(tedek,akcja,wybranadata,obrazek,IdApartamentu){
	ImagePorownaj = 'url("images/checked.gif")';
	FirstImage = "FirstImage";
	SecondImage = "SecondImage";
	if (akcja == 1){
		if (tedek.style.backgroundImage == ImagePorownaj){
			if (obrazek == 0){
				tedek.style.backgroundImage = "";
			}else{
				tedek.style.backgroundImage = 'url(images/'+obrazek+')';
			}
		}else{
			tedek.style.backgroundImage = ImagePorownaj;
		}
			if (document.getElementById(FirstImage).value == ""){
				document.getElementById(FirstImage).value = 'url(images/'+obrazek+')';
			}else{
				document.getElementById(SecondImage).value = 'url(images/'+obrazek+')';
			}
		Rezerwuj(wybranadata,IdApartamentu);
	}else if(akcja == 2){
		if (obrazek == 0){
			tedek.style.backgroundColor = '#b3ffB3';
		}else{
			if (tedek.style.backgroundImage != ImagePorownaj){
				tedek.style.backgroundImage = 'url(images/najedz.gif)';
			}
		}
		tedek.style.cursor = 'pointer';
	}else{
		if (obrazek == 0){
			tedek.style.backgroundColor = '#ffffff';
		}else{
			if (tedek.style.backgroundImage != ImagePorownaj){
				tedek.style.backgroundImage = 'url(images/'+obrazek+')';
			}
		}
		tedek.style.cursor = 'normal';
	}
}

function Rezerwuj(data,IdApartamentu){
	DataPrzyjazdu = "DataPrzyjazdu";
	DataWyjazdu = "DataWyjazdu";
	FirstImage = "FirstImage";
	SecondImage = "SecondImage";
	przyjazd = document.getElementById(DataPrzyjazdu);
	wyjazd = document.getElementById(DataWyjazdu);
	RezerwujeDataPrzyjazd = document.getElementById('rezerwuje_data_przyjazd');
	RezerwujeDataWyjazd = document.getElementById('rezerwuje_data_wyjazd');
	if(przyjazd.value == data){
		przyjazd.value = "";
		document.getElementById(FirstImage).value = "";
	}else if(wyjazd.value == data){
		wyjazd.value = "";
		document.getElementById(SecondImage).value = "";
	}else if(przyjazd.value == ""){
		przyjazd.value = data;
		if(RezerwujeDataPrzyjazd != null){
			RezerwujeDataPrzyjazd.value = data;
		}
	}else if(wyjazd.value == ""){
		wyjazd.value = data;
		if(RezerwujeDataWyjazd != null){
			RezerwujeDataWyjazd.value = data;
		}
	}else{
		if (data < przyjazd.value){
			KtoraKomorka = przyjazd.value;
			przyjazd.value = data;
		}else{
			KtoraKomorka = wyjazd.value;
			wyjazd.value = data;
		}
		if (document.getElementById(FirstImage).value == ""){
			document.getElementById(KtoraKomorka).style.backgroundImage = "";
		}else{
			document.getElementById(KtoraKomorka).style.backgroundImage = document.getElementById(FirstImage).value;
		}
	}
	if (przyjazd.value != "" && wyjazd.value != ""){
		if (wyjazd.value < przyjazd.value){
			bufor = wyjazd.value;
			wyjazd.value = przyjazd.value;
			if(RezerwujeDataWyjazd != null){
				RezerwujeDataWyjazd.value = przyjazd.value;
			}
			przyjazd.value = bufor;
			if(RezerwujeDataPrzyjazd != null){
				RezerwujeDataPrzyjazd.value = bufor;
			}
			buforobrazek = document.getElementById(SecondImage).value;
			document.getElementById(SecondImage).value = document.getElementById(FirstImage).value;
			document.getElementById(FirstImage).value = buforobrazek;
		}
		potwierdzenie(IdApartamentu);
	}
}

function potwierdzenie(IdApartamentu) {
	DataPrzyjazdu = "DataPrzyjazdu";
	DataWyjazdu = "DataWyjazdu";
	IdRezerwacji = "rezerwacja";
	FirstImage = "FirstImage";
	SecondImage = "SecondImage";
	przyjazd = document.getElementById(DataPrzyjazdu);
	wyjazd = document.getElementById(DataWyjazdu);
	ZaznaczWszystkieDniNaKalendarzu(przyjazd.value, wyjazd.value, 1);
	var sprawdz = window.confirm("Czy chesz przejść do rezerwacji?\nTermin od "+przyjazd.value+" do "+wyjazd.value);
	if (sprawdz == true) {
		document.getElementById(IdRezerwacji).submit();
		return true;
	}else{
		ZaznaczWszystkieDniNaKalendarzu(przyjazd.value, wyjazd.value, 2);
		if (document.getElementById(FirstImage).value == ""){
			document.getElementById(przyjazd.value).style.backgroundImage = "";
		}else{
			document.getElementById(przyjazd.value).style.backgroundImage = document.getElementById(FirstImage).value;
		}
		przyjazd.value="";
		if (document.getElementById(SecondImage).value == ""){
		document.getElementById(wyjazd.value).style.backgroundImage = "";
		}else{
			document.getElementById(wyjazd.value).style.backgroundImage = document.getElementById(SecondImage).value;
		}
		wyjazd.value="";
		document.getElementById(FirstImage).value = "";
		document.getElementById(SecondImage).value = "";
	}
}

function ZaznaczWszystkieDniNaKalendarzu(p, w, action){
	RokP = parseInt(p.substr(0, 4), 10);
	MiesiacP = parseInt(p.substr(5, 2), 10)-1;
	DzienP = parseInt(p.substr(8, 2), 10);
	RokW = parseInt(w.substr(0, 4), 10);
	MiesiacW = parseInt(w.substr(5, 2), 10)-1;
	DzienW = parseInt(w.substr(8, 2), 10);
	DataP = new Date(RokP, MiesiacP, DzienP);
	DataW = new Date(RokW, MiesiacW, DzienW);
	if(DataP > DataW){
		DataBufor = DataP;
		DataP = DataW;
		DataW = DataBufor;
	}
	SprawdzDate = DataP;
	i = 0;
	while(DataW > SprawdzDate){
		SprawdzDate.setDate(DataP.getDate()+1);
		ZrodlowyRok = SprawdzDate.getFullYear();
		ZrodlowyMiesiac = SprawdzDate.getMonth();
		ZrodlowyDzien = SprawdzDate.getDate();
		Miesiac1 = parseInt(ZrodlowyMiesiac) + 1;
		NazwaPola = ZrodlowyRok+"-"+(Miesiac1 < 10 ? '0' : '')+(Miesiac1)+"-"+(ZrodlowyDzien < 10 ? '0' : '')+ZrodlowyDzien;
		Polko = document.getElementById(NazwaPola);
		if(Polko != null){
			if(action == 1){
				if(Polko.style.backgroundImage != '' 
					&& Polko.style.backgroundImage != 'url(images/checked.gif)'
					&& Polko.style.backgroundImage != 'url(images/0)'){
					TablicaObrazkow[i] = Polko.style.backgroundImage;
					TablicaIdkow[i] = NazwaPola;
					i++;
				}
				Polko.style.backgroundImage = 'url(images/najedz.gif)';
			}else{
				obrazek = '';
				for (x in TablicaIdkow){
					if(TablicaIdkow[x] == NazwaPola){
						obrazek = TablicaObrazkow[x];	
					}
				}
				Polko.style.backgroundImage = obrazek;
			}
		}
	}
}

function SkopiujDane(pole){
	if(pole.checked == true){
		document.getElementById("invoice_name").value = document.getElementById("first_name").value+" "+document.getElementById("second_name").value;
		document.getElementById("invoice_city").value = document.getElementById("city").value;
		document.getElementById("invoice_postcode").value = document.getElementById("postcode").value;
		document.getElementById("invoice_country").value = document.getElementById("country").value;
	}
}

var map = false;

function cleanUp(){
	if (map) {GUnload();}
}

var picArr = [];
function preloadImages(imgs){
	var url;
	for (i = 0; i<imgs.length; i++){
		picArr[i]= new Image(); 
		url = imgs[i];
		picArr[i].src=url.replace(/&amp;/gi, "&");
	}
}

function ChangePicture(Pict){
	document.getElementById('BigPicture').src = picArr[Pict].src;
}

function Recommend(ApID){
	mypage = "polec_znajomemu.php?apid="+ApID;
	NewWindow(mypage, "polec", 600, 500, "no");
}

// JQuery

var url_base = '';
var url_fullPath = '';
var jq = jQuery.noConflict();

function create_function(setting){
	var params = '';
	var appers = '';

    jq.each(setting['params'], function(key, value) {
        params = params + appers + key + '=' + value;
        appers = '&';
    });

    jq.ajax({ type: setting['type'],
             url: url_base + setting['action'],
             data: params,
             success: function(html)
             {  
                if(setting['return_object_id'])
                {
                    jq(setting['return_object_id']).html(html);
                }                   
             }
          })
}

function Search(form){
	dp = jq("#search_data_p").val();
	dw = jq("#search_data_w").val();
	msc = jq("#search_miejsce").val();
	ppl = jq("#searching_ilosc_osob").val();
	wal = jq("#search_waluta").val();
	prf = jq("#search_cena_od").val();
	prt = jq("#search_cena_do").val();
	var new_action = "main,mode,wg_lokalizacji,search_params,"+dp+"_"+dw+"_"+msc+"_"+ppl+"_"+wal+"_"+prf+"_"+prt+".html"
	form.action = new_action;
	form.submit();
}


// Hotele
function setSelectValue( select , valueToSelect )
	{
		var count = select.options.length;
	
		for( var i=0; i<count; i++ )
			if( select.options[i].value == valueToSelect )
			{
				select.selectedIndex = i;
				break;
			}
	}
	
	function zmianaARM( hotels_form )
	{
		var moje = document.hotels_form.ar_month;
		zaznaczone = moje.options[moje.selectedIndex].value;
		ustawDPM(hotels_form, zaznaczone );
	
	}
	
	function ustawDPM( hotels_form, date )
	{
	
		setSelectValue( form.dep_month , zaznaczone);
	
	}
	
	function zmianaARY( hotels_form)
	{
		var moje2 = document.hotels_form.ar_year;
		zaznaczone2 = moje2.options[moje2.selectedIndex].value;
		ustawDPY(hotels_form, zaznaczone2 );
	
	}
	
	function ustawDPY( hotels_form, date )
	{
		setSelectValue( form.dep_year , zaznaczone2);
	}

   function setHSValues()
   {
   	var f=document.hotels_form;
	f.checkInDate.value=f.ar_year.value+"-"+f.ar_month.value+"-"+f.ar_day.value;
	f.checkOutDate.value=f.dep_year.value+"-"+f.dep_month.value+"-"+f.dep_day.value;
	var bed=0;
	var person=0;
	switch(f.roomType.options[f.roomType.selectedIndex].value)
	{
		case "11":bed=1;person=1;break;
		case "22":bed=2;person=2;break;
		case "21":bed=1;person=2;break;
		case "33":bed=3;person=3;break;
		case "32":bed=2;person=3;break;
		case "44":bed=4;person=4;break;
		case "43":bed=3;person=4;break;
		case "42":bed=2;person=4;break;
	}
	f.numOfAdultsSelect.value=person;
	f.numOfBedsSelect.value=bed;
   }

   function checkDates2(ind,outd)
   {
   	arr=ind.value.split("-");
	dep=outd.value.split("-");
	arr=Date.UTC(arr[0],arr[1]-1,arr[2],0,0,0);
	dep=Date.UTC(dep[0],dep[1]-1,dep[2],0,0,0);
	if (dep>arr){
		return true;
	}

	alert("Data odjazdu musi byc pozniejsza niz przyjazdu!");
	return false;

   }