﻿function s_Debug () {
	alert ('debug');
}

//##################################################

function pokaz_poprzednie_komentarze ()
{
	var update_komentarze = '';
	for (i=0; i<komentarzy_na_raz; i++)
	{
		new_komentarz_index = id_pierwszego_komentarza - komentarzy_na_raz + i;
		update_komentarze += komentarze[new_komentarz_index];
	}
	id_pierwszego_komentarza -= komentarzy_na_raz;
	document.getElementById('komentarze').innerHTML = update_komentarze;
	document.getElementById('pokaz_nastepne_komentarze').style.display = 'block';
	if (id_pierwszego_komentarza <= 0)
	{
		document.getElementById('pokaz_poprzednie_komentarze').style.display = 'none';
	}
}

//##################################################

function pokaz_nastepne_komentarze ()
{
	var update_komentarze = '';
	for (i=0; i<komentarzy_na_raz; i++)
	{
		new_komentarz_index = id_pierwszego_komentarza + komentarzy_na_raz + i;
		if (new_komentarz_index<komentarzy) update_komentarze += komentarze[new_komentarz_index];
	}
	id_pierwszego_komentarza += komentarzy_na_raz;
	document.getElementById('komentarze').innerHTML = update_komentarze;
	document.getElementById('pokaz_poprzednie_komentarze').style.display = 'block';
	if (id_pierwszego_komentarza + komentarzy_na_raz >= komentarzy)
	{
		document.getElementById('pokaz_nastepne_komentarze').style.display = 'none';
	}
}

//##################################################

function s_SzukajPokazOdpowiadajaceDzielnice (lang)
{
	var ilosc_osob = document.getElementById('szukaj_ilosc_osob').value;
	var drop_down_list = document.getElementById('dzielnica');
	while (drop_down_list.firstChild)
	{
		drop_down_list.removeChild(drop_down_list.firstChild);
	}

	var nowa_dzielnica = document.createElement('option');
	nowa_dzielnica.setAttribute('value', "");
	nowa_dzielnica.setAttribute('selected', "selected");
	if (lang == 'pl') nowa_dzielnica.innerHTML = '- wybierz dzielnicę -'; else nowa_dzielnica.innerHTML = '- choose a quarter -';
	drop_down_list.appendChild (nowa_dzielnica);

	for (i=0; i<arr_dzielnice.length; i++)
	{
		if (ilosc_osob <= arr_dzielnice[i][1])
		{
			var nowa_dzielnica = document.createElement('option');
			nowa_dzielnica.setAttribute('value', arr_dzielnice[i][0]);
			nowa_dzielnica.innerHTML = arr_dzielnice[i][0];
			drop_down_list.appendChild (nowa_dzielnica);
		}
	}
/*		for (i=0; i<arr_dzielnice[ilosc_osob-1].length; i++)
		{
			var nowa_dzielnica = document.createElement('option');
			nowa_dzielnica.setAttribute('value', arr_dzielnice[ilosc_osob-1][i]);
			nowa_dzielnica.innerHTML = arr_dzielnice[ilosc_osob-1][i];
			drop_down_list.appendChild (nowa_dzielnica);
		}
*/
}

//##################################################

function s_FormularzFakturaShow () {
	document.getElementById('formularz_faktura').style.display = 'block';
}

//##################################################

function s_FormularzFakturaHide () {
	document.getElementById('formularz_faktura').style.display = 'none';
}

//##################################################

function s_ShowPaymentType () {
	selectedIndex = document.getElementById('platnosc').selectedIndex;
	items = document.getElementById('platnosc').length;

	for (i=1; i<=items; i++) {
		document.getElementById('platnosc_' + i).style.display = 'none';
	}
	document.getElementById('platnosc_' + (selectedIndex + 1)).style.display = 'block';
}

//##################################################

function number_format(numstr) {
  var numstr = String(numstr);
  var re0 = /(\d+)(\d{3})($|\..*)/;
  if (re0.test(numstr))
    return numstr.replace(
      re0,
      function(str,p1,p2,p3) { return number_format(p1) + " " + p2 + p3; }
    );
  else
    return numstr;
}



function s_PrzeliczCeneWynajmu (cena, cenaSezon, lang) {
	/**/	
	cena = parseFloat(cena);
	cenaSezon = parseFloat(cenaSezon);
	var data_od = document.getElementById('RezerwacjaDataOd').value.split('/')
	data_od = new Date(parseInt(data_od[2]), parseInt(data_od[1]) - 1, parseInt(data_od[0])).getTime();
	var data_do = document.getElementById('RezerwacjaDataDo').value.split('/')
	data_do = new Date(parseInt(data_do[2]), parseInt(data_do[1]) - 1, parseInt(data_do[0]) - 1).getTime();
	var suma = 0;
	var cd = new Date(data_od).getTime();
	while (cd <= data_do) {		
		var y = new Date(cd).getFullYear()
		if (
			(cd >= new Date(y, 4 - 1, 1) && cd <= new Date(y, 10 - 1, 31)) ||
			(cd >= new Date(y - 1, 12 - 1, 21) && cd <= new Date(y, 1 - 1, 3)) ||
			(cd >= new Date(y, 12 - 1, 21) && cd <= new Date(y + 1, 1 - 1, 3))
		) {
			suma += cenaSezon;
		} else {
			suma += cena;
		}
		cd += 24*60*60*1000;
	}
	if ((data_do - data_od) / (24*60*60*1000) >= 5) {
		suma *= 1 - 0.125;
	}	
	var wartosc = suma
	/*
	przyjazd = document.getElementById('RezerwacjaDataOd').value;
	wyjazd = document.getElementById('RezerwacjaDataDo').value;
	przyjazd = przyjazd.split ("/");
	wyjazd = wyjazd.split ("/");
	data_przyjazdu = new Date (przyjazd[2],przyjazd[1] - 1,przyjazd[0]);
	data_wyjazdu = new Date (wyjazd[2],wyjazd[1] - 1,wyjazd[0]);
	roznica = (data_wyjazdu - data_przyjazdu)/(60*60*24*1000);

	var iloscOsobElement = document.getElementById('ilosc_osob');
	var iloscOsob = iloscOsobElement.options[iloscOsobElement.selectedIndex].value;
	iloscOsob = parseInt(iloscOsob);
	if(iloscOsob<=0 || isNaN(iloscOsob)){
		iloscOsob= 1;
	}


	//wartosc = iloscOsob * roznica * cena * 1.22;
	wartosc =  roznica * cena ;
	*/
	if ((wartosc<0) || isNaN(wartosc)) wartosc = 0;
	wartosc = wartosc.toFixed(2);
	wartosc = number_format(wartosc);



	if (lang == 'pl') {
		document.getElementById('s_CenaWybranegoOkresu').innerHTML = "Cena wybranego okresu: <span class=\"strong\">" + wartosc + " PLN brutto</brutto>";
	} else {
		document.getElementById('s_CenaWybranegoOkresu').innerHTML = "Price: <span class=\"strong\">" + wartosc + " PLN gross</brutto>";
	}
}



function s_PokazReszteKalendarza () {
document.getElementById('s_ApartamentRezerwacjaResztaKalendarza').style.display = 'block';
document.getElementById('s_ApartamentRezerwacjaPokazNext12Miesiecy').style.display = 'none';
}

function s_PokazMiniaturkeFotkiMieszkania(fotka) {
//document.getElementById('s_ApartamentBigImage').src = '';
source = document.getElementById('s_ApartamentBigImage').src;
slash_index = source.lastIndexOf("/");
new_source = source.substr (0, slash_index + 1);
document.getElementById('s_ApartamentBigImage').src = new_source + fotka;
}