var tekstZaznaczony = 'red';
var tekstPodswietlony = '#FF9804';
var tekstOdznaczony = '';
var tekstCzerNieb = '';
var tekstNiebCzer = '';
var tekstNiebSzar = '';
var tekstSzarNieb = '';
var tekstSzarCzer = 'red';
var tekstCzerSzar = 'red';

var kolorZaznaczony = 'url("./pix/kal2.png")';
var kolorPodswietlony = 'url("./pix/kal3.png")';
var kolorOdznaczony = 'url("./pix/kal1.png")';
var kolorNiedostepny = 'url("./pix/kal4.png")';
var kolorCzerNieb = 'url("./pix/kal6.png")';
var kolorNiebCzer = 'url("./pix/kal7.png")';
var kolorNiebSzar = 'url("./pix/kal11.png")';
var kolorSzarNieb = 'url("./pix/kal10.png")';
var kolorSzarCzer = 'url("./pix/kal9.png")';
var kolorCzerSzar = 'url("./pix/kal8.png")';


var ktory = 0;
var ilosc = 0;
var licz = 0;
var poczatek_liczenia = 2;
var ile_powtorzen = 0;
var sprawdz_pole_min = 0;
var policz_dni = 0;
var suma = 0;
var suma2 = 0;
var sumaPoRabacie = 0;
var sumaRoznica = 0;
var sumaPoRabacieZadatek = 0;
var opisCzynnosci = 0;

var arrMiesiac = new Array();
arrMiesiac[1] = "stycznia";
arrMiesiac[2] = "lutego";
arrMiesiac[3] = "marca";
arrMiesiac[4] = "kwietnia";
arrMiesiac[5] = "maja";
arrMiesiac[6] = "czerwca";
arrMiesiac[7] = "lipca";
arrMiesiac[8] = "sierpnia";
arrMiesiac[9] = "wrze¶nia";
arrMiesiac[10] = "paĽdziernika";
arrMiesiac[11] = "listopada";
arrMiesiac[12] = "grudnia";

var arrTermin = new Array();
arrTermin[1] = "Odznaczyłe¶ termin czarteru. Aby zarezerwować jacht wybierz interesuj±c± Cię datę.";
arrTermin[2] = "Wybierz termin czarteru.";
arrTermin[3] = "Wybrałe¶ termin czarteru.";
arrTermin[5] = "Przedłużyłe¶ termin czarteru.";
arrTermin[6] = "Skróciłe¶ termin czarteru.";

/*
    Funkcja tak samo działająca jak php str_replace
*/
function str_replace (searchTXT, replaceTXT, subject) {
    var newValue, result;
    result = subject.search(searchTXT);
    for(i = 1; i <= result; i++) {
		if(i == 1) {
			newValue = subject.replace(searchTXT, replaceTXT);
		} else {
			newValue = newValue.replace(searchTXT, replaceTXT);
		}
    }
    return newValue;
}

/*
    Funkcja sprawdzająca jaka przeglądarka jest i jaka wersja
*/
function checkBrowser() {
  var isFF36up = false;
  if (navigator.userAgent) {
		 var ffver = navigator.userAgent.match(/Firefox\/3\.(\d+)/);
  	 var ff4ver = navigator.userAgent.match(/Firefox\/4\.(\d+)/);
  	 var ff5ver = navigator.userAgent.match(/Firefox\/5\.(\d+)/);
  	 var ff6ver = navigator.userAgent.match(/Firefox\/6\.(\d+)/);
  	 var ff7ver = navigator.userAgent.match(/Firefox\/7\.(\d+)/);
  	 var ff8ver = navigator.userAgent.match(/Firefox\/8\.(\d+)/);
  	 var ff9ver = navigator.userAgent.match(/Firefox\/9\.(\d+)/);
		 isFF36up = ffver && parseInt(ffver[1], 10) >= 6;
  }
  if(ff4ver == null && (isFF36up == false || isFF36up == null) && ff5ver == null && ff6ver == null && ff7ver == null && ff8ver == null && ff9ver == null) {
    	kolorZaznaczony = str_replace('"', '', kolorZaznaczony);
    	kolorPodswietlony = str_replace('"', '', kolorPodswietlony);
    	kolorOdznaczony = str_replace('"', '', kolorOdznaczony);
    	kolorNiedostepny = str_replace('"', '', kolorNiedostepny);
    	kolorCzerNieb = str_replace('"', '', kolorCzerNieb);
    	kolorNiebCzer = str_replace('"', '', kolorNiebCzer);
    	kolorNiebSzar = str_replace('"', '', kolorNiebSzar);
    	kolorSzarNieb = str_replace('"', '', kolorSzarNieb);
    	kolorSzarCzer = str_replace('"', '', kolorSzarCzer);
    	kolorCzerSzar = str_replace('"', '', kolorCzerSzar);
    }

}
checkBrowser();

var arrInfo = new Array();
/*
   Funkcja zaznaczająca termin zarezerwowany
   Zaznacza wszystkie pola, które są oznaczone na pomarańczowo
   Dodoje infomarcje portów i ceny
*/
function termin(pierwszy, start, stop, rabat, adm, oneway, doplata_oneway, port_start_zaznacz, port_stop_zaznacz) {
    var policz = 0;
    var sprawdz = 0;
    if((document.getElementById(pierwszy).style.backgroundImage == kolorPodswietlony) && (document.getElementById(pierwszy).style.backgroundImage != kolorZaznaczony) && (document.getElementById(pierwszy).style.backgroundImage != kolorCzerNieb) && (document.getElementById(pierwszy).style.backgroundImage != kolorNiebCzer) && (document.getElementById(pierwszy).style.backgroundImage != kolorSzarCzer) && (document.getElementById(pierwszy).style.backgroundImage != kolorCzerSzar)){
        for(licz = start; licz <= stop; licz++)
        {
            if(document.getElementById(licz).style.backgroundImage == kolorPodswietlony) {
                var sprawdz_poprzedni = licz - 1;
                if((poczatek == 0) || (poczatek > licz)) {
                    if(poczatek != 0) {
                        document.getElementById(poczatek).style.backgroundImage = kolorZaznaczony;
                        document.getElementById(poczatek).style.color=tekstZaznaczony;
                    }
                    if((poczatek > 0) || (koniec > 0)) {
                        if(document.getElementById('termin') != null) document.getElementById('termin').innerHTML = arrTermin[5];
                    } else {
                        if(document.getElementById('termin') != null) document.getElementById('termin').innerHTML = arrTermin[3];
                    }
                    poczatek = licz;
                }
                var sprawdz_nastepny = licz + 1;
                if((koniec == 0) || (koniec < licz))
                {
                    if((koniec != 0) && (document.getElementById(koniec) != null))
                    {
                        document.getElementById(koniec).style.backgroundImage = kolorZaznaczony;
                        document.getElementById(koniec).style.color=tekstZaznaczony;
                    }
                    koniec = licz;
                }
                document.getElementById(licz).style.backgroundImage = kolorZaznaczony;
                document.getElementById(licz).style.color=tekstZaznaczony;
                policz++;
            }
        }
	
        if(poczatek == koniec)
        {
            koniec = poczatek + 1;
        }
		port_start_zaznacz = arrPortyZmianaPrzed[poczatek];
		port_stop_zaznacz = arrPortyZmianaPo[koniec];
        suma = 0;
        if((poczatek != 0) && (koniec != 0))
        {
            for(licz = poczatek; licz <= koniec; licz++)
            {
                if(document.getElementById(licz).style.backgroundImage != kolorNiedostepny)
                {
                    suma += arrCeny[licz];
                }
            }
            if(adm == 0)
            {
                if(document.getElementById('informacje') != null) document.getElementById('informacje').style.display = "";
				if(document.getElementById('komentarze') != null) document.getElementById('komentarze').style.display = "";
                if(document.getElementById('rezerwacja') != null) document.getElementById('rezerwacja').innerHTML = jd_to_gregorian(poczatek, 0)+' - '+jd_to_gregorian(koniec, 0);
                if(document.getElementById('termin_od') != null) document.getElementById('termin_od').innerHTML = jd_to_gregorian(poczatek, 0);
                if(document.getElementById('termin_do') != null) document.getElementById('termin_do').innerHTML = jd_to_gregorian(koniec, 0);
                if(document.getElementById('form_od') != null) document.getElementById('form_od').value = poczatek;
                if(document.getElementById('form_do') != null) document.getElementById('form_do').value = koniec;
            } else {
                if(document.getElementById('od') != null) document.getElementById('od').value = jd_to_gregorian(poczatek, 1);
                if(document.getElementById('do') != null) document.getElementById('do').value = jd_to_gregorian(koniec, 1);
                czasy();
            }
        }
        arrInfo[1] = "W tym terminie nie ma możliwo¶ci przedłużenia czarteru tego jachtu.";
        arrInfo[2] = "W tym terminie istnieje możliwo¶ć przedłużenia czarteru "+(arrPrzed[poczatek] == 0 ? '' : '- o minimum '+arrPrzed[poczatek]+' dni')+" przed zaznaczonym terminem czarteru oraz "+(arrPo[koniec] == 0 ? '' : '- o minimum '+arrPo[koniec]+' dni')+" po zaznaczonym terminie czarteru";
        arrInfo[3] = "W tym terminie istnieje możliwo¶ć przedłużenia czarteru "+(arrPrzed[poczatek] == 0 ? '' : '- o minimum '+arrPrzed[poczatek]+' dni')+" przed zaznaczonym terminem czarteru.";
        arrInfo[4] = "W tym terminie istnieje możliwo¶ć przedłużenia czarteru "+(arrPo[koniec] == 0 ? '' : '- o minimum '+arrPo[koniec]+' dni')+" po zaznaczonym terminie czarteru.";
        if((document.getElementById(poczatek - (arrPrzed[poczatek] == 0 ? 1 : arrPrzed[poczatek])) != null) && (document.getElementById(poczatek - (arrPrzed[poczatek] == 0 ? 1 : arrPrzed[poczatek])).style.backgroundImage != kolorNiedostepny) && (document.getElementById(koniec + (arrPo[koniec] == 0 ? 1 : arrPo[koniec])) != null && document.getElementById(koniec + (arrPo[koniec] == 0 ? 1 : arrPo[koniec])).style.backgroundImage == kolorNiedostepny))
        {
            if(document.getElementById('opis') != null) document.getElementById('opis').innerHTML = arrInfo[3];
            if(document.getElementById('opisCZ') != null) document.getElementById('opisCZ').value = arrInfo[3];
        } else if((document.getElementById(poczatek - (arrPrzed[poczatek] == 0 ? 1 : arrPrzed[poczatek])) != null) && (document.getElementById(poczatek - (arrPrzed[poczatek] == 0 ? 1 : arrPrzed[poczatek])).style.backgroundImage == kolorNiedostepny) && (document.getElementById(koniec + (arrPo[koniec] == 0 ? 1 : arrPo[koniec])) != null && document.getElementById(koniec + (arrPo[koniec] == 0 ? 1 : arrPo[koniec])).style.backgroundImage != kolorNiedostepny))
        {
            if(document.getElementById('opis') != null) document.getElementById('opis').innerHTML = arrInfo[4];
            if(document.getElementById('opisCZ') != null) document.getElementById('opisCZ').value = arrInfo[4];
        } else if((document.getElementById(poczatek - (arrPrzed[poczatek] == 0 ? 1 : arrPrzed[poczatek])) != null) && (document.getElementById(poczatek - (arrPrzed[poczatek] == 0 ? 1 : arrPrzed[poczatek])).style.backgroundImage != kolorNiedostepny) && (document.getElementById(koniec + (arrPo[koniec] == 0 ? 1 : arrPo[koniec])) != null && document.getElementById(koniec + (arrPo[koniec] == 0 ? 1 : arrPo[koniec])).style.backgroundImage != kolorNiedostepny))
        {
            if(document.getElementById('opis') != null) document.getElementById('opis').innerHTML = arrInfo[2];
            if(document.getElementById('opisCZ') != null) document.getElementById('opisCZ').value = arrInfo[2];
        } else {
            if(document.getElementById('opis') != null) document.getElementById('opis').innerHTML = arrInfo[1];
            if(document.getElementById('opisCZ') != null) document.getElementById('opisCZ').value = arrInfo[1];
        }
        if(policz > 0) status = 1;
		//alert(suma);
        suma = suma - arrCeny[koniec];
        sumaPoRabacie = suma - (suma * ((rabat/10)/100));
        sumaRoznica = suma - sumaPoRabacie;
		sumaPoRabacieZadatek = ((sumaPoRabacie*30)/100);

        var iloscPortow=0;
        for(var i in arrPortNazwa)
        {
            iloscPortow++;
        }
        var MultiPort = true;
        if(oneway > 0 && iloscPortow > 1)
        {
            if((document.getElementById(poczatek - oneway) != null) && (arrPortyPo[poczatek - oneway] || poczatek == start))
            {
		var odKiedyLiczyc;
		if(poczatek == start) {
			odKiedyLiczyc = poczatek;
		} else {
			odKiedyLiczyc = poczatek - oneway;
		}
                for(licz = odKiedyLiczyc; licz <= poczatek; licz++)
                {
                    if((document.getElementById(licz).style.backgroundImage == kolorNiedostepny) || (document.getElementById(licz).style.backgroundImage == kolorSzarNieb) || (document.getElementById(licz).style.backgroundImage == kolorNiebSzar))
                    {
                        MultiPort = false;
                    }
                }
                if(MultiPort && port_start_zaznacz == 1) {
                    if(document.getElementById('port_start_select') != null) document.getElementById('port_start_select').style.display = '';
                    if(document.getElementById('port_godz_start') != null) document.getElementById('port_godz_start').innerHTML = arrPortGodzStart[arrPortyPo[poczatek]]+':00';
                    if(document.getElementById('port_start_font') != null) document.getElementById('port_start_font').style.display = 'none';
                } else {
					for(liczDni = poczatek; liczDni >= start; liczDni--) {
						if(document.getElementById(liczDni) != null) {
							if(document.getElementById(liczDni).style.backgroundImage == kolorSzarCzer || document.getElementById(liczDni).style.backgroundImage == kolorSzarNieb || (document.getElementById(liczDni).style.backgroundImage == kolorZaznaczony && document.getElementById(liczDni-1).style.backgroundImage == kolorNiedostepny)) {
								if(document.getElementById(liczDni-1) != null && arrPortyPo[liczDni-1] > 0) {
									var ktoryPort = liczDni-1;
								} else {
									var ktoryPort = liczDni;
								}
								break;
							} else {
								var ktoryPort = poczatek;
							}
						} else {
							var ktoryPort = poczatek;
						}
					}
		    
                    if(document.getElementById('port_start_font') != null) document.getElementById('port_start_font').style.display = '';
                    if(document.getElementById('port_start_font') != null) document.getElementById('port_start_font').innerHTML = arrPortNazwa[arrPortyPo[ktoryPort]]+', '+arrPortyMiejscowosc[arrPortyPo[ktoryPort]];
                    if(document.getElementById('port_godz_start') != null) document.getElementById('port_godz_start').innerHTML = arrPortGodzStart[arrPortyPo[ktoryPort]]+':00';
                    if(document.getElementById('port_start') != null) document.getElementById('port_start').value = arrPortyPo[ktoryPort];
                    if(document.getElementById('port_start_select') != null) document.getElementById('port_start_select').style.display = 'none';
                }
            }
            MultiPort = true;
            if((document.getElementById(koniec + oneway) != null) && (arrPortyPrzed[koniec + oneway] || koniec == stop))
            {
				var doKiedyLiczyc;
				if(koniec == stop) {
					doKiedyLiczyc = poczatek;
				} else {
					doKiedyLiczyc = koniec + oneway;
				}
                for(licz = koniec; licz <= doKiedyLiczyc; licz++) {
                    if((document.getElementById(licz).style.backgroundImage == kolorNiedostepny) || (document.getElementById(licz).style.backgroundImage == kolorSzarNieb) || (document.getElementById(licz).style.backgroundImage == kolorNiebSzar)) {
                        MultiPort = false;
                    }
                }
                if(MultiPort && port_stop_zaznacz == 1) {
                    if(document.getElementById('port_stop_select') != null) document.getElementById('port_stop_select').style.display = '';
                    if(document.getElementById('port_stop_font') != null) document.getElementById('port_stop_font').style.display = 'none';
                    if(document.getElementById('port_godz_stop') != null) document.getElementById('port_godz_stop').innerHTML = arrPortGodzStop[arrPortyPrzed[koniec]]+':00';
                } else {
					for(liczDni = koniec; liczDni <= stop; liczDni++) {
						if(document.getElementById(liczDni) != null) {
							if(document.getElementById(liczDni).style.backgroundImage == kolorCzerSzar || document.getElementById(liczDni).style.backgroundImage == kolorNiebSzar || (document.getElementById(liczDni).style.backgroundImage == kolorZaznaczony && document.getElementById(liczDni+1).style.backgroundImage == kolorNiedostepny)) {
								if(document.getElementById(liczDni+1) != null && arrPortyPrzed[liczDni+1] > 0)  {
									var ktoryPort = liczDni+1;
								} else {
									var ktoryPort = liczDni;
								}
								break;
							} else {
								var ktoryPort = koniec;
							}
						} else {
							var ktoryPort = koniec;
						}
					}
                    if(document.getElementById('port_stop_font') != null) document.getElementById('port_stop_font').style.display = '';
                    if(document.getElementById('port_stop_font') != null) document.getElementById('port_stop_font').innerHTML = arrPortNazwa[arrPortyPrzed[ktoryPort]]+', '+arrPortyMiejscowosc[arrPortyPrzed[ktoryPort]];
                    if(document.getElementById('port_godz_stop') != null) document.getElementById('port_godz_stop').innerHTML = arrPortGodzStop[arrPortyPrzed[ktoryPort]]+':00';
                    if(document.getElementById('port_stop') != null) document.getElementById('port_stop').value = arrPortyPrzed[ktoryPort];
                    if(document.getElementById('port_stop_select') != null) document.getElementById('port_stop_select').style.display = 'none';
                }
            }
            zmianaPortu(doplata_oneway*100, rabat);
        } else {
            if(document.getElementById('port_start_font') != null) document.getElementById('port_start_font').style.display = '';
            if(document.getElementById('port_start_font') != null) document.getElementById('port_start_font').innerHTML = arrPortNazwa[arrPortyPo[poczatek]]+', '+arrPortyMiejscowosc[arrPortyPo[poczatek]];
            if(document.getElementById('port_godz_start') != null) document.getElementById('port_godz_start').innerHTML = arrPortGodzStart[arrPortyPo[poczatek]]+':00';
            if(document.getElementById('port_stop_font') != null) document.getElementById('port_stop_font').style.display = '';
            if(document.getElementById('port_stop_font') != null) document.getElementById('port_stop_font').innerHTML = arrPortNazwa[arrPortyPrzed[koniec]]+', '+arrPortyMiejscowosc[arrPortyPrzed[koniec]];
            if(document.getElementById('port_godz_stop') != null) document.getElementById('port_godz_stop').innerHTML = arrPortGodzStop[arrPortyPrzed[koniec]]+':00';
	    if(document.getElementById('port_start_select') != null) document.getElementById('port_start_select').style.display = 'none';
	    if(document.getElementById('port_stop_select') != null) document.getElementById('port_stop_select').style.display = 'none';
            if(document.getElementById('start_ktory') != null)  document.getElementById('start_ktory').value = arrPortyPo[poczatek];
            if(document.getElementById('stop_ktory') != null)  document.getElementById('stop_ktory').value = arrPortyPrzed[koniec];
            if(document.getElementById('ktory_1') != null) document.getElementById('ktory_1').value = 2;
            if(document.getElementById('ktory_2') != null) document.getElementById('ktory_2').value = 2;
	    port_start = arrPortyPo[poczatek];
	    port_stop = arrPortyPrzed[koniec];
        }
	if(port_start != port_stop) {
		suma2 = suma + doplata_oneway*100;
		sumaPoRabacie = suma2 - (suma2 * ((rabat/10)/100));
		sumaRoznica = suma2 - sumaPoRabacie;
		if(document.getElementById('oplata') != null) document.getElementById('oplata').innerHTML = doplata_oneway*100+'.00 zł';
		if(document.getElementById('oplata_tabela') != null) document.getElementById('oplata_tabela').style.display = '';
		if(document.getElementById('doplac_oneway') != null) document.getElementById('doplac_oneway').value = doplata_oneway;
	} else {
		if(document.getElementById('oplata_tabela') != null) document.getElementById('oplata_tabela').style.display = 'none';
		sumaPoRabacie = suma - (suma * ((rabat/10)/100));
		sumaRoznica = suma - sumaPoRabacie;
		suma2 = suma;
	}
        if((suma2 > 0) && (document.getElementById('suma_cennik') != null))  document.getElementById('suma_cennik').innerHTML = suma2.toFixed(2)+' zł';
        if((sumaPoRabacie > 0) && (document.getElementById('suma_rabat') != null))  document.getElementById('suma_rabat').innerHTML = sumaPoRabacie.toFixed(2)+' zł';
        if((sumaPoRabacie > 0) && (document.getElementById('suma_rabat2') != null))  document.getElementById('suma_rabat2').innerHTML = sumaPoRabacie.toFixed(2)+' zł';
        if((sumaRoznica > 0) && (document.getElementById('rabat') != null))  document.getElementById('rabat').innerHTML = sumaRoznica.toFixed(2)+' zł';
        if((suma > 0) && (document.getElementById('cena') != null))  document.getElementById('cena').value = suma.toFixed(2);
        if((sumaPoRabacie > 0) && (document.getElementById('cenaRabat') != null))  document.getElementById('cenaRabat').value = sumaPoRabacie.toFixed(2);
        if((sumaRoznica > 0) && (document.getElementById('rabaty') != null))  document.getElementById('rabaty').value = sumaRoznica.toFixed(2);
	if((sumaPoRabacieZadatek > 0) && (document.getElementById('suma_rabat_zadatek') != null))  document.getElementById('suma_rabat_zadatek').innerHTML = sumaPoRabacieZadatek.toFixed(2)+' zł';
	if((sumaPoRabacieZadatek > 0) && (document.getElementById('suma_rabat_zadatek2') != null))  document.getElementById('suma_rabat_zadatek2').innerHTML = sumaPoRabacieZadatek.toFixed(2)+' zł';
                
    } else if(document.getElementById(pierwszy).style.backgroundImage == kolorZaznaczony || document.getElementById(pierwszy).style.backgroundImage == kolorCzerNieb || document.getElementById(pierwszy).style.backgroundImage == kolorNiebCzer || document.getElementById(pierwszy).style.backgroundImage == kolorCzerSzar || document.getElementById(pierwszy).style.backgroundImage == kolorSzarCzer) {
        for(licz = poczatek; licz <= koniec; licz++)
        {
            if((document.getElementById(licz).style.backgroundImage != kolorNiedostepny) && (document.getElementById(licz).style.backgroundImage != kolorSzarNieb) && (document.getElementById(licz).style.backgroundImage != kolorNiebSzar))
            {
                if((document.getElementById(licz - 1) != null) && (document.getElementById(licz - 1).style.backgroundImage == kolorNiedostepny))
                {
                    document.getElementById(licz).style.backgroundImage = kolorSzarNieb;
                    document.getElementById(licz).style.color=tekstSzarNieb;
                } else if((document.getElementById(licz + 1) != null) && (document.getElementById(licz + 1).style.backgroundImage == kolorNiedostepny))
                {
                    document.getElementById(licz).style.backgroundImage = kolorNiebSzar;
                    document.getElementById(licz).style.color=tekstNiebSzar;
                } else {
                    document.getElementById(licz).style.backgroundImage = kolorOdznaczony;
                    document.getElementById(licz).style.color=tekstOdznaczony;
                }
            }
        }
        suma = 0;
        sumaPoRabacie = 0;
        status = 0;
        poczatek = 0;
        koniec = 0;
        
        if((poczatek == 0) && (koniec == 0))
        {
            if(adm == 0)
            {
                if(document.getElementById('termin') != null)document.getElementById('termin').innerHTML = arrTermin[1];
                if(document.getElementById('informacje') != null)document.getElementById('informacje').style.display = "none";
                if(document.getElementById('opis') != null)document.getElementById('opis').innerHTML = "Prosze zaznaczyć termin czarteru.";
				if(document.getElementById('komentarze') != null) document.getElementById('komentarze').style.display = "none";
                if(document.getElementById('rezerwacja') != null)document.getElementById('rezerwacja').innerHTML = 'Proszę wybrać termin rezerwacji.';
                if(document.getElementById('form_od') != null) document.getElementById('form_od').value = '';
                if(document.getElementById('form_do') != null) document.getElementById('form_do').value = '';
            } else {
                if(document.getElementById('od') != null) document.getElementById('od').value = '';
                if(document.getElementById('do') != null) document.getElementById('do').value = '';
            }
        }
    }
    if((poczatek != 0) && (koniec != 0))
    {
        if((document.getElementById(poczatek - 1) != null) && (document.getElementById(poczatek - 1).style.backgroundImage == kolorNiedostepny))
        {
            document.getElementById(poczatek).style.backgroundImage = kolorSzarCzer;
            document.getElementById(poczatek).style.color=tekstSzarCzer;
        } else {
            document.getElementById(poczatek).style.backgroundImage = kolorNiebCzer;
            document.getElementById(poczatek).style.color=tekstNiebCzer;
        }
        if((document.getElementById(koniec + 1) != null) && (document.getElementById(koniec + 1).style.backgroundImage == kolorNiedostepny))
        {
            document.getElementById(koniec).style.backgroundImage = kolorCzerSzar;
            document.getElementById(koniec).style.color=tekstCzerSzar;
        } else {
            document.getElementById(koniec).style.backgroundImage = kolorCzerNieb;
            document.getElementById(koniec).style.color=tekstCzerNieb;
        }
    }
}

function zaznacz(pierwszy, ile, terminod, termindo, start, stop, minPrzed, minPo, dziura, adm) {
    var minimum = pierwszy + ile - 1;
    var wyjatek = 0;
    var sprawdz = 0;
    if(ile != 0) ile += 1;
    if(adm == 0 || adm == 2)
    {
		if(adm == 2) {
			kolorZaznaczony = 'url("./pix/kal2_20.png")';
			kolorPodswietlony = 'url("./pix/kal3_20.png")';
			tekstPodswietlony = '#FF9804';
			kolorOdznaczony = 'url("./pix/kal1_20.png")';
			kolorNiedostepny = 'url("./pix/kal4_20.png")';
			kolorCzerNieb = 'url("./pix/kal6_20.png")';
			kolorNiebCzer = 'url("./pix/kal7_20.png")';
			kolorNiebSzar = 'url("./pix/kal11_20.png")';
			kolorSzarNieb = 'url("./pix/kal10_20.png")';
			kolorSzarCzer = 'url("./pix/kal9_20.png")';
			kolorCzerSzar = 'url("./pix/kal8_20.png")';
			checkBrowser();
		}
        if(status != 1) {
            if((ile == -98) || (ile == 8)) {
                if((termindo >= start) && (termindo <= stop) && (terminod >= start) && (terminod <= stop) && (document.getElementById(pierwszy).style.backgroundImage!=kolorNiedostepny)) {
                    pierwszy = terminod;
                    ile_powtorzen = termindo - terminod + 1;
                    for(licz = poczatek_liczenia; licz <= ile_powtorzen; licz++) {
                        var dodaj = licz - 1;
                        var nast = parseInt(pierwszy) + dodaj;
                        if(document.getElementById(pierwszy).style.backgroundImage == kolorNiedostepny) {
                            sprawdz = 1;
                            break;
                        }
                        if(document.getElementById(nast).style.backgroundImage == kolorNiedostepny) {
                            sprawdz = 1;
                            break;
                        }
                    }
                    if(sprawdz == 0) {
                        document.getElementById(pierwszy).style.backgroundImage=kolorPodswietlony;
                        document.getElementById(pierwszy).style.color=tekstPodswietlony;
                        for(licz = poczatek_liczenia; licz <= ile_powtorzen; licz++) {
                            var dodaj = licz - 1;
                            var nast = parseInt(pierwszy) + dodaj;
                            document.getElementById(nast).style.backgroundImage=kolorPodswietlony;
                            document.getElementById(nast).style.color=tekstPodswietlony;
                        }
                    }
                }
            }
            if((ile > 0) && (ile < 8)) {
                var pole_min = pierwszy - dziura + 1;
                var sprawdz_pole_min = pole_min - 1;
                policz_dni = 0;
                if((dziura > 0) && (document.getElementById(pierwszy).style.backgroundImage!=kolorNiedostepny) && (document.getElementById(sprawdz_pole_min).style.backgroundImage==kolorNiedostepny) && (document.getElementById(minimum).style.backgroundImage!=kolorNiedostepny)) {
                    var sprawdz_pierwszy = pierwszy - 1;
                    for(licz = sprawdz_pierwszy; licz >= pole_min; licz--) {
                        if(document.getElementById(licz).style.backgroundImage!=kolorNiedostepny) {
                            document.getElementById(licz).style.backgroundImage=kolorPodswietlony;
                            document.getElementById(licz).style.color=tekstPodswietlony;
                            policz_dni++;
                        }
                    }
                }
                if(policz_dni > 0) {
                    ile_powtorzen = ile - policz_dni;
                    minimum = minimum - policz_dni + 1;
                } else {
                    ile_powtorzen = ile;
                }
				var sprawdzCzyMoznaZaznaczyc = false;
				for(licz = poczatek_liczenia; licz <= ile_powtorzen; licz++) {
					var dodaj = licz - 1;
					var nast = parseInt(pierwszy) + dodaj;
					if((pierwszy >= start) && (pierwszy <= stop) && (nast >= start) && (nast <= stop) && (document.getElementById(nast) != null) && (document.getElementById(nast).style.backgroundImage!=kolorNiedostepny) && (document.getElementById(pierwszy).style.backgroundImage!=kolorNiedostepny) && (document.getElementById(minimum).style.backgroundImage != kolorNiedostepny)) {
							sprawdzCzyMoznaZaznaczyc = true;
					} else {
						sprawdzCzyMoznaZaznaczyc = false;
						break;
					}
				}
				if(sprawdzCzyMoznaZaznaczyc == true) {
					if((pierwszy >= start) && (pierwszy <= stop) && (document.getElementById(pierwszy).style.backgroundImage!=kolorNiedostepny) && (document.getElementById(minimum).style.backgroundImage != kolorNiedostepny)) {
						document.getElementById(pierwszy).style.backgroundImage=kolorPodswietlony;
						document.getElementById(pierwszy).style.color=tekstPodswietlony;
					}
					for(licz = poczatek_liczenia; licz <= ile_powtorzen; licz++) {
						var dodaj = licz - 1;
						var nast = parseInt(pierwszy) + dodaj;
						if((pierwszy >= start) && (pierwszy <= stop) && (nast >= start) && (nast <= stop) && (document.getElementById(nast) != null) && (document.getElementById(nast).style.backgroundImage!=kolorNiedostepny) && (document.getElementById(pierwszy).style.backgroundImage!=kolorNiedostepny) && (document.getElementById(minimum).style.backgroundImage != kolorNiedostepny))
						{
							document.getElementById(nast).style.backgroundImage=kolorPodswietlony;
							document.getElementById(nast).style.color=tekstPodswietlony;
						}
					}
				}
            }
        } else {
            var dodatkowekoniec;
            if(pierwszy > koniec) {
				if(minPo != 0) {
					sprawdz = 0;
					var roznica = pierwszy - koniec;
					if(ile == 8 && minPo == 7) {
						minPo = roznica + (termindo - pierwszy);
					} else if((terminod - koniec) < minPo && koniec < terminod) {
						minPo = terminod - koniec + minPo;
					}
					if(roznica > minPo) ilosc = roznica; else ilosc = minPo;
					var nastepny_minimum = koniec + minPo - 1;
					//alert(nastepny_minimum);
					if(((document.getElementById(nastepny_minimum) != null) && (document.getElementById(nastepny_minimum).style.backgroundImage != kolorNiedostepny) || (roznica == minPo)) && (document.getElementById(pierwszy).style.backgroundImage != kolorNiedostepny)) {
						for(dodatkowekoniec = 0; dodatkowekoniec <= ilosc; dodatkowekoniec++) {
							ktory = koniec + dodatkowekoniec;
							if(document.getElementById(ktory).style.backgroundImage == kolorNiedostepny) {
								sprawdz = 1;
							}
						}
						if(sprawdz == 0) {
							for(dodatkowekoniec = 0; dodatkowekoniec <= ilosc; dodatkowekoniec++) {
								ktory = koniec + dodatkowekoniec;
								if((ktory >= start) && (ktory <= stop) && (document.getElementById(ktory).style.backgroundImage!=kolorNiedostepny)) {
									document.getElementById(ktory).style.backgroundImage=kolorPodswietlony;
									document.getElementById(ktory).style.color=tekstPodswietlony;
								}
							}
						}
					}
				}
			}
            if(pierwszy < poczatek) {
				if(minPrzed != 0) {
					sprawdz = 0;
					var roznica = poczatek - pierwszy;
					if(ile == 8 && minPrzed == 7) {
						minPrzed = roznica + (pierwszy - terminod);
					} else if((poczatek - termindo) < minPrzed && poczatek > termindo) {
						minPrzed = poczatek - termindo + minPrzed;
					}
					if(roznica > minPrzed) ilosc = roznica; else ilosc = minPrzed;
					var nastepny_minimum = poczatek - minPrzed + 1;
					if(((document.getElementById(nastepny_minimum) != null) && (document.getElementById(nastepny_minimum).style.backgroundImage != kolorNiedostepny) || (roznica == minPo)) && (document.getElementById(pierwszy).style.backgroundImage != kolorNiedostepny)) {
						for(dodatkowekoniec = 0; dodatkowekoniec <= ilosc; dodatkowekoniec++) {
							ktory = poczatek - dodatkowekoniec;
							if(document.getElementById(ktory).style.backgroundImage == kolorNiedostepny) {
								sprawdz = 1;
							}
						}
						if(sprawdz == 0) {
							for(dodatkowekoniec = 0; dodatkowekoniec <= ilosc; dodatkowekoniec++) {
								ktory = poczatek - dodatkowekoniec;
								if((ktory >= start) && (ktory <= stop) && (document.getElementById(ktory).style.backgroundImage!=kolorNiedostepny)) {
									document.getElementById(ktory).style.backgroundImage=kolorPodswietlony;
									document.getElementById(ktory).style.color=tekstPodswietlony;
								}
							}
						}
					}
				}
            }
        }
    } else {
        kolorZaznaczony = 'url("./pix/kal2_20.png")';
        kolorPodswietlony = 'url("./pix/kal3_20.png")';
        kolorOdznaczony = 'url("./pix/kal1_20.png")';
        kolorNiedostepny = 'url("./pix/kal4_20.png")';
        kolorCzerNieb = 'url("./pix/kal6_20.png")';
        kolorNiebCzer = 'url("./pix/kal7_20.png")';
        kolorNiebSzar = 'url("./pix/kal11_20.png")';
        kolorSzarNieb = 'url("./pix/kal10_20.png")';
        kolorSzarCzer = 'url("./pix/kal9_20.png")';
        kolorCzerSzar = 'url("./pix/kal8_20.png")';
	checkBrowser();
        
        if(status == 0)
        {
            if((document.getElementById(pierwszy) != null)&& (document.getElementById(pierwszy).style.backgroundImage != kolorNiedostepny))
            {
                document.getElementById(pierwszy).style.backgroundImage = kolorPodswietlony;
                document.getElementById(pierwszy).color = tekstPodswietlony;
            }
        } else {
            if(pierwszy < poczatek)
            {
                sprawdz = 0;
                for(licz = pierwszy; licz <= poczatek; licz++)
                {
                    if(document.getElementById(licz).style.backgroundImage == kolorNiedostepny)
                    {
                        sprawdz = 1;
                        break;
                    }
                }
                if(sprawdz == 0)
                {
                    for(licz = poczatek; licz >= pierwszy; licz--)
                    {
                        document.getElementById(licz).style.backgroundImage = kolorPodswietlony;
                        document.getElementById(licz).color = tekstPodswietlony;
                    }
                }
            }
            if(pierwszy > koniec)
            {
                sprawdz = 0;
                for(licz = koniec; licz <= pierwszy; licz++)
                {
                    if(document.getElementById(licz).style.backgroundImage == kolorNiedostepny)
                    {
                        sprawdz = 1;
                        break;
                    }
                }
                if(sprawdz == 0)
                {
                    for(licz = koniec; licz <= pierwszy; licz++)
                    {
                        document.getElementById(licz).style.backgroundImage = kolorPodswietlony;
                        document.getElementById(licz).color = tekstPodswietlony;
                    }
                }
            }
            
        }
    }
    
}

function odznacz(pierwszy, ile, terminod, termindo, start, stop, minPrzed, minPo, dziura) {
    for(licz = start; licz <= stop; licz++)
    {
        if(document.getElementById(licz) != null)
	if(document.getElementById(licz).style.backgroundImage == kolorPodswietlony)
        { 
            
            if((document.getElementById(licz - 1).style.backgroundImage == kolorNiedostepny) && (licz != start))
            {
                document.getElementById(licz).style.backgroundImage = kolorSzarNieb;
                document.getElementById(licz).style.color = tekstSzarNieb;
            } else if((document.getElementById(licz + 1) != null) && (document.getElementById(licz + 1).style.backgroundImage == kolorNiedostepny) && (licz != stop))
            {
                document.getElementById(licz).style.backgroundImage = kolorNiebSzar;
                document.getElementById(licz).style.color=tekstNiebSzar;
            } else {
                document.getElementById(licz).style.backgroundImage = kolorOdznaczony;
                document.getElementById(licz).style.color=tekstOdznaczony; 
            }
            if((document.getElementById(poczatek) != null) && (document.getElementById(poczatek).style.backgroundImage != kolorNiedostepny))
            {
                if((document.getElementById(poczatek - 1) != null) && (document.getElementById(poczatek - 1).style.backgroundImage == kolorNiedostepny))
                {
                    document.getElementById(poczatek).style.backgroundImage = kolorSzarCzer;
                    document.getElementById(poczatek).style.color=tekstSzarCzer;
                } else {
                    document.getElementById(poczatek).style.backgroundImage = kolorNiebCzer;
                    document.getElementById(poczatek).style.color=tekstNiebCzer;
                }
            }
            if((document.getElementById(koniec) != null) && (document.getElementById(koniec).style.backgroundImage != kolorNiedostepny))
            {
                if((document.getElementById(koniec + 1) != null) && (document.getElementById(koniec + 1).style.backgroundImage == kolorNiedostepny))
                {
                    document.getElementById(koniec).style.backgroundImage = kolorCzerSzar;
                    document.getElementById(koniec).style.color=tekstCzerSzar;
                } else {
                    document.getElementById(koniec).style.backgroundImage = kolorCzerNieb;
                    document.getElementById(koniec).style.color=tekstCzerNieb;
                }
            }
        }
    }
}

function ukryj(id, start, stop, dzis) {
    var status = 0;
    var sprawdz_miesiac = 0;
    var nast_id = 0;
    var drugi_id = 0;
    if(dzis <= start)
    {
        sprawdz_miesiac = start + 1;
    } else {
        sprawdz_miesiac = dzis + 1;
    }
    if(id == 1)
    {
        for(licz = 12; licz >= 1; licz--)
        {
            if((status == 0) && (document.getElementById(licz) != null) && (document.getElementById(licz).style.display != "none"))
            {
                nast_id = licz - 1;
                status = 1;
                document.getElementById(licz).style.display = "none";
                document.getElementById(nast_id).style.display = "";
                document.getElementById('miesiac_'+nast_id).style.textAlign = "left";
                drugi_id = nast_id - 1;
                document.getElementById(drugi_id).style.display = "";
                document.getElementById('miesiac_'+drugi_id).style.textAlign = "right";
                if(start < drugi_id)
                {
                    if(document.getElementById('poprzedni_'+drugi_id) != null) document.getElementById('poprzedni_'+drugi_id).style.display = "";
                    //document.getElementById('poprzedni_off').style.display = "none";
                } else {
                    if(document.getElementById('poprzedni_'+drugi_id) != null) document.getElementById('poprzedni_'+drugi_id).style.display = "none";
                    //document.getElementById('poprzedni_off').style.display = "";
                }
                if(stop > nast_id)
                {
                    if(document.getElementById('nastepny_'+nast_id) != null) document.getElementById('nastepny_'+nast_id).style.display = "";
                    //document.getElementById('nastepny_off').style.display = "none";
                } else {
                    if(document.getElementById('nastepny_'+nast_id) != null) document.getElementById('nastepny_'+nast_id).style.display = "none";
                    //document.getElementById('nastepny_off').style.display = "";
                }
                if(document.getElementById('nastepny_'+drugi_id) != null) document.getElementById('nastepny_'+drugi_id).style.display = "none";
                if(document.getElementById('poprzedni_'+nast_id) != null) document.getElementById('poprzedni_'+nast_id).style.display = "none";
    
                break;
                
            } else {
                if(document.getElementById(licz) != null) document.getElementById(licz).style.display = "none";
            }
        }
    } else {
        for(licz = 1; licz <= 12; licz++)
        {
            if((status == 0) && (document.getElementById(licz) != null) && (document.getElementById(licz).style.display != "none"))
            {
                nast_id = licz + 1;
                status = 1;
                document.getElementById(licz).style.display = "none";
                document.getElementById(nast_id).style.display = "";
                document.getElementById('miesiac_'+nast_id).style.textAlign = "right";
                drugi_id = nast_id + 1;
                document.getElementById(drugi_id).style.display = "";
                document.getElementById('miesiac_'+drugi_id).style.textAlign = "left";
                if(start < nast_id)
                {
                    if(document.getElementById('poprzedni_'+nast_id) != null) document.getElementById('poprzedni_'+nast_id).style.display = "";
                    //document.getElementById('poprzedni_off').style.display = "none";
                } else {
                    if(document.getElementById('poprzedni_'+nast_id) != null) document.getElementById('poprzedni_'+nast_id).style.display = "none";
                    //document.getElementById('poprzedni_off').style.display = "";
                }
                if(stop > drugi_id)
                {
                    if(document.getElementById('nastepny_'+drugi_id) != null) document.getElementById('nastepny_'+drugi_id).style.display = "";
                    //document.getElementById('nastepny_off').style.display = "none";
                } else {
                    if(document.getElementById('nastepny_'+drugi_id) != null) document.getElementById('nastepny_'+drugi_id).style.display = "none";
                    //document.getElementById('nastepny_off').style.display = "";
                }
                if(document.getElementById('nastepny_'+nast_id) != null) document.getElementById('nastepny_'+nast_id).style.display = "none";
                if(document.getElementById('poprzedni_'+drugi_id) != null) document.getElementById('poprzedni_'+drugi_id).style.display = "none";
                break;
                
            } else {
                if(document.getElementById(licz) != null) document.getElementById(licz).style.display = "none";
            }
        }
    }
    
}

function mod(w1, w2)
{
    return (w1 % w2);
}

function leap_gregorian(year)
{
    return ((year % 4) == 0) &&
            (!(((year % 100) == 0) && ((year % 400) != 0)));
}

var GREGORIAN_EPOCH = 1721425.5;

function gregorian_to_jd(year, month, day)
{
    return (GREGORIAN_EPOCH - 1) +
           (365 * (year - 1)) +
           Math.floor((year - 1) / 4) +
           (-Math.floor((year - 1) / 100)) +
           Math.floor((year - 1) / 400) +
           Math.floor((((367 * month) - 362) / 12) +
           ((month <= 2) ? 0 :
                               (leap_gregorian(year) ? -1 : -2)
           ) +
           day);
}

function jd_to_gregorian(jd, ver) {
    var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad,
        yindex, dyindex, year, yearday, leapadj;

    wjd = Math.floor(jd - 0.5) + 0.5;
    depoch = wjd - GREGORIAN_EPOCH;
    quadricent = Math.floor(depoch / 146097);
    dqc = mod(depoch, 146097);
    cent = Math.floor(dqc / 36524);
    dcent = mod(dqc, 36524);
    quad = Math.floor(dcent / 1461);
    dquad = mod(dcent, 1461);
    yindex = Math.floor(dquad / 365);
    year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
    if (!((cent == 4) || (yindex == 4))) {
        year++;
    }
    yearday = wjd - gregorian_to_jd(year, 1, 1);
    leapadj = ((wjd < gregorian_to_jd(year, 3, 1)) ? 0
                                                  :
                  (leap_gregorian(year) ? 1 : 2)
              );
    month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
    day = (wjd - gregorian_to_jd(year, month, 1)) + 1;

    if(ver == 0)
    {
        return day+' '+arrMiesiac[month]+' '+year+' r.';
    } else {
        if(month < 10)
        {
            month = '0'+month;
        }
        if(day < 10)
        {
            day = '0'+day;
        }
        return year+'-'+month+'-'+day;
    }
}

function zmianaPortu(doplata_oneway, rabat)
{
    port_start = 0;
    port_stop = 0;
    if(document.getElementById('port_start_select') != null && document.getElementById('port_start_select').style.display == '')
    {
        if(document.getElementById('port_start_select') != null) port_start = document.getElementById('port_start_select').value;
        if(document.getElementById('ktory_1') != null) document.getElementById('ktory_1').value = 1;
    } else {
        if(document.getElementById('port_start') != null) port_start = document.getElementById('port_start').value;
        if(document.getElementById('ktory_1') != null) document.getElementById('ktory_1').value = 2;
    }
    
    
    if(document.getElementById('port_stop_select') != null && document.getElementById('port_stop_select').style.display == '')
    {
        if(document.getElementById('port_stop_select') != null) port_stop = document.getElementById('port_stop_select').value;
        if(document.getElementById('ktory_2') != null) document.getElementById('ktory_2').value = 1;
    } else {
        if(document.getElementById('port_stop') != null) port_stop = document.getElementById('port_stop').value;
        if(document.getElementById('ktory_2') != null) document.getElementById('ktory_2').value = 2;
    }
	//alert(port_start+' = '+port_stop);
    if(port_start != port_stop)
    {
        suma2 = suma + doplata_oneway;
		//alert(suma+" === "+suma2);
        sumaPoRabacie = suma2 - (suma2 * ((rabat/10)/100));
        sumaRoznica = suma2 - sumaPoRabacie;
        if(document.getElementById('oplata') != null) document.getElementById('oplata').innerHTML = doplata_oneway+'.00 zł';
        if(document.getElementById('oplata_tabela') != null) document.getElementById('oplata_tabela').style.display = '';
        if(document.getElementById('doplac_oneway') != null) document.getElementById('doplac_oneway').value = doplata_oneway;
    } else {
        if(document.getElementById('oplata_tabela') != null) document.getElementById('oplata_tabela').style.display = 'none';
        sumaPoRabacie = suma - (suma * ((rabat/10)/100));
        sumaRoznica = suma - sumaPoRabacie;
		suma2 = suma;
    }
    if((suma2 > 0) && (document.getElementById('suma_cennik') != null))  document.getElementById('suma_cennik').innerHTML = suma2.toFixed(2)+' zł';
    if((sumaPoRabacie > 0) && (document.getElementById('suma_rabat') != null))  document.getElementById('suma_rabat').innerHTML = sumaPoRabacie.toFixed(2)+' zł';
    if((sumaPoRabacie > 0) && (document.getElementById('suma_rabat2') != null))  document.getElementById('suma_rabat2').innerHTML = sumaPoRabacie.toFixed(2)+' zł';
    if((sumaRoznica > 0) && (document.getElementById('rabat') != null))  document.getElementById('rabat').innerHTML = sumaRoznica.toFixed(2)+' zł';
    if((suma > 0) && (document.getElementById('cena') != null))  document.getElementById('cena').value = suma.toFixed(2);
    if((sumaPoRabacie > 0) && (document.getElementById('cenaRabat') != null))  document.getElementById('cenaRabat').value = sumaPoRabacie.toFixed(2);
    if((sumaRoznica > 0) && (document.getElementById('rabaty') != null))  document.getElementById('rabaty').value = sumaRoznica.toFixed(2);
	if((sumaPoRabacieZadatek > 0) && (document.getElementById('suma_rabat_zadatek') != null))  document.getElementById('suma_rabat_zadatek').innerHTML = sumaPoRabacieZadatek.toFixed(2)+' zł';
	if((sumaPoRabacieZadatek > 0) && (document.getElementById('suma_rabat_zadatek2') != null))  document.getElementById('suma_rabat_zadatek2').innerHTML = sumaPoRabacieZadatek.toFixed(2)+' zł';
    if(document.getElementById('start_ktory') != null)  document.getElementById('start_ktory').value = port_start;
    if(document.getElementById('stop_ktory') != null)  document.getElementById('stop_ktory').value = port_stop;
}
