
//
// Zamienia string w formacie YYYY-MM-DD na obiekt typu Date.
//
function parseDate(dateString)
{
	var str = new String(dateString);
	var array = str.split('-');
	if (array.length!=3)
		return null;
	var year = array[0];
	var month = array[1].charAt(0)=='0' ? parseInt(array[1].substr(1, 1)) : parseInt(array[1]);
	var day = array[2].charAt(0)=='0' ? parseInt(array[2].substr(1, 1)) : parseInt(array[2]);
	var date = new Date(year, month-1, day);
	if (isNaN(date))
		return null;
	date.setHours(0);
	date.setMinutes(0);
	date.setSeconds(0);
	date.setMilliseconds(0);
	return date;
}

//
// Zamienia obiekt Date na string w formacie YYYY-MM-DD.
//
function formatDate(date)
{
	return date.getFullYear() + "-" + formatDatePart(date.getMonth()+1) +
		"-" + formatDatePart(date.getDate());
}

//
// Funkcja pomocnicza dla funckcji formatDate(). Dodaje znak '0' do czesci daty.
//
function formatDatePart(value)
{
	var v = String(value);
	return v.length==1 ? "0"+v : v;
}


//
// Funkcja obslugi zdarzenia - zmiana wartosci w liscie NumOfNights.
//
function onNumOfNightsChange(form, numOfNights)
{
	var arrivalDate = parseDate(document.hotelbookform.checkin.value);
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');	//"Nieprawidłowy format Daty Przybycia..."
		return false;
	}
	
	numOfNights = parseInt(numOfNights);
	
	var newDate = new Date(arrivalDate.getFullYear(), arrivalDate.getMonth(), arrivalDate.getDate()+numOfNights);
	
	document.hotelbookform.checkout.value = formatDate(newDate);
}

//
// Funkcja obslugi zdarzenia - zmiana wartosci w okienku edycyjnym ArrivalDate.
//
function onArrivalDateChange(form, arrivalDate, hotel)
{
	var arrivalDate = parseDate(arrivalDate);
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');	//"Nieprawidłowy format Daty Przybycia..."
		return false;
	}
	
	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	today.setMilliseconds(0);
	
	if (arrivalDate < today)
	{
		alert('Data Przybycia nie może być wcześniejsza od daty dzisiejszej.');	//"Data Przybycia nie może być wcześniejsza od daty dzisiejszej."
		return false;
	}

	if (hotel)
		document.hotelbookform.checkout.value = formatDate(addDaysToDate(arrivalDate, parseInt(document.hotelbookform.nights.value)));
}

//
// Funkcja obslugi zdarzenia - zmiana wartosci w okienku edycyjnym DepartureDate.
//
function onDepartureDateChange(form, departureDate)
{
	var arrivalDate = parseDate(document.hotelbookform.checkin.value);
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');	//"Nieprawidłowy format Daty Przybycia..."
		return false;
	}
	
	var depDate = parseDate(departureDate);
	if (depDate==null)
	{
		alert('Nieprawidłowy format Daty Wyjazdu.\n\nPrawidłowy format daty: YYYY-MM-DD.');	//"Nieprawidłowy format Daty Wyjazdu..."
		return false;
	}
	
	if (depDate <= arrivalDate)
	{
		alert('Data Wyjazdu musi być późniejsza od Daty Przybycia.');	//"Data Wyjazdu musi być późniejsza od Daty Przybycia."
		return false;	
	}
	if ((depDate-arrivalDate)/(1000*60*60*24) > 60)
	{
		alert('Data Wyjazdu nie może być późniejsza niż 60 dni od Daty Przybycia.');	//"Data Wyjazdu nie może być późniejsza niż 60 dni od Daty Przybycia."
		return false;
	}
	document.hotelbookform.nights.value = Math.round((depDate.getTime() - arrivalDate.getTime())/(1000*60*60*24));
}
//
// Dodaje dni do daty.
//
function addDaysToDate(date, numOfDays)
{
	return new Date(date.getFullYear(), date.getMonth(), date.getDate()+numOfDays);
}


function ValidateForm()
{
	
	var arrivalDate = parseDate(document.hotelbookform.checkin.value);
	var depDate = parseDate(document.hotelbookform.checkout.value);
	
	var today = new Date();
	today.setHours(0);
	today.setMinutes(0);
	today.setSeconds(0);
	today.setMilliseconds(0);
	
	if (arrivalDate < today)
	{
		alert('Data Przybycia nie może być wcześniejsza od daty dzisiejszej.');	//"Data Przybycia nie może być wcześniejsza od daty dzisiejszej."
		return false;
	}
	
	if (arrivalDate==null)
	{
		alert('Nieprawidłowy format Daty Przybycia.\n\nPrawidłowy format daty: YYYY-MM-DD.');	//"Nieprawidłowy format Daty Przybycia..."
		return false;
	}
	
	
	if (depDate==null)
	{
		alert('Nieprawidłowy format Daty Wyjazdu.\n\nPrawidłowy format daty: YYYY-MM-DD.');	//"Nieprawidłowy format Daty Wyjazdu..."
		return false;
	}
	
	if (depDate <= arrivalDate)
	{
		alert('Data Wyjazdu musi być późniejsza od Daty Przybycia.');	//"Data Wyjazdu musi być późniejsza od Daty Przybycia."
		return false;	
	}
	
	if ((depDate-arrivalDate)/(1000*60*60*24) > 60)
	{
		alert('Data Wyjazdu nie może być późniejsza niż 60 dni od Daty Przybycia.');	//"Data Wyjazdu nie może być późniejsza niż 60 dni od Daty Przybycia."
		return false;
	}
	
	$("#overlay_loading").center();
	$("#overlay_loading_bg").center();
	$("#overlay_loading").show( function(){
			$("#overlay_loading_bg").show();
			return true;
	});
	
	
	
}

function change_city()
{
	counrty_id = document.hotelbookform.countryid.options[document.hotelbookform.countryid.selectedIndex].value;
	if (counrty_id==-1) {
		document.hotelbookform.cityid.options[0] = new Option(' - wybierz kraj -', 0);
		document.hotelbookform.cityid.disabled = true;
		return;
	}
	
	while(document.hotelbookform.cityid.options.length > 1)		
		document.hotelbookform.cityid.options[1]=null;

	document.hotelbookform.cityid.disabled = false;
		
	var x = 0;
	var cities = counties[counrty_id];
	for(var i=0; i<cities.length; i+=2)
	{
		document.hotelbookform.cityid.options[x] = new Option(cities[i+1], cities[i]);
		x++;
	}
}
//
// Funkcja obslugi zdarzenia - zmiana wartosci w liscie NumOfAdults.
//
function onNumOfAdultsChange(form, numOfAdults)
{

	var numOfBeds = document.hotelbookform.beds;
	var count = numOfBeds.options.length;
	var i;
	 

  	while(numOfBeds.options.length >= 1)
  	{		
		numOfBeds.options[0]=null;
	}

	var start = numOfAdults > 1 ? numOfAdults-1 : 1;
	var stop = numOfAdults > 4 ? 4 : numOfAdults;
	
	if(numOfAdults==4)
	    start=2;
	
	j=1;

	for (i=start; i<=stop; i++)
	{
			if (navigator.userAgent.indexOf('Opera')>-1) 
			{
				var obiekt = new Option( i, i);
	  			numOfBeds.options[j++] = obiekt;
	  		}
	  		else
	  		{
	  			var el = document.createElement('OPTION');
				el.text = String(i);
		  		el.value = i;
				numOfBeds.add(el, document.all ? i : null);
	  		}

	}
	
	if (navigator.userAgent.indexOf('Opera')>-1) 
	{
		numOfBeds.options[0]=null;
	}

	numOfBeds.value = stop;
}

function change_city_jq()
{
	$("#cityid").html("");
	var countryid = $("#countryid").val();
	
	if (countryid==-1) {
		$("#cityid").prepend(new Option(' - wybierz kraj -', 0));
		$("#cityid").attr("disabled","disabled");
		return;
	}
		
	var x = 0;
	var cities = countries[countryid];
	for(var i=0; i<cities.length; i+=2)
	{
		$("#cityid").append('<option value="' + cities[i] + '">' + cities[i+1] + '</option>');
		x++;
	}
}

function set_city(id) {
	$("#cityid").html("");
	var countryid = $("#countryid").val();
	
	if (countryid==-1) {
		$("#cityid").prepend(new Option(' - wybierz kraj -', 0));
		$("#cityid").attr("disabled","disabled");
		return;
	}
		
	var x = 0;
	var cities = countries[countryid];
	var add = null;
	for(var i=0; i<cities.length; i+=2)
	{
		if(cities[i] == id) {
			add = 'selected="selected"';	
		} else {
			add = null;	
		}
		$("#cityid").append('<option value="' + cities[i] + '" ' + add + ' >' + cities[i+1] + '</option>');
		x++;
	}
}



function showsearchbutton() {
	$("li.button").show();	
}

jQuery.fn.sort = function() {  
   return this.pushStack( [].sort.apply( this, arguments ), []);  
};  


function sortPriceAlg(a,b) {
	return parseInt(a.getAttribute("min")) > parseInt(b.getAttribute("min")) ? 1 : -1;
}

function sortLocationAlg(a,b) {
	return a.getAttribute("area") > b.getAttribute("area") ? 1 : -1;
}

function sortNameAlg(a,b) {
	return a.getAttribute("name") > b.getAttribute("name") ? 1 : -1;
}

function sortRatingAlg(a,b) {
	return parseInt(a.getAttribute("stars")) > parseInt(b.getAttribute("stars")) ? 1 : -1;
}

function sortPriceAlgRev(a,b) {
	return parseInt(a.getAttribute("min")) > parseInt(b.getAttribute("min")) ? -1 : 1;
}

function sortLocationAlgRev(a,b) {
	return a.getAttribute("area") > b.getAttribute("area") ? -1 : 1;
}

function sortRatingAlgRev(a,b) {
	return parseInt(a.getAttribute("stars")) > parseInt(b.getAttribute("stars")) ? -1 : 1;
}

function sortNameAlgRev(a,b) {
	return a.getAttribute("name") > b.getAttribute("name") ? -1 : 1;
}

// function sortAlpha(a,b){  
//    return a.innerHTML > b.innerHTML ? 1 : -1;  
//};

function sortPrice(reverse) 
{

	$("#overlay_update").center();
	$("#overlay_update_bg").center();	
	$("#overlay_update").show(function() 
	{
		$("#overlay_update").show(function() 
		{

			if(!reverse) 
			{
				$("#recommended .box_oferta").sort(sortPriceAlg).appendTo('#recommended');
				$("#online .box_oferta").sort(sortPriceAlg).appendTo('#online');
				$("#offline .box_oferta").sort(sortPriceAlg).appendTo('#offline');
			} else {
				$("#recommended .box_oferta").sort(sortPriceAlgRev).appendTo('#recommended');
				$("#online .box_oferta").sort(sortPriceAlgRev).appendTo('#online');
				$("#offline .box_oferta").sort(sortPriceAlgRev).appendTo('#offline');		
			}
		showMarkers();
		});
    	     $("#overlay_update_bg").hide(); 
	     $("#overlay_update").hide(); 
	});
}

function sortLocation(reverse) {
	// $("#below_overlay").hide(); $("#overlay_loading").show(); 
	$("#overlay_update").center();
	$("#overlay_update_bg").center();	
	$("#overlay_update").show(function() 
	{
		$("#overlay_update").show(function() 
		{

			if(!reverse) {
				$("#recommended .box_oferta").sort(sortLocationAlg).appendTo('#recommended');
				$("#online .box_oferta").sort(sortLocationAlg).appendTo('#online');
				$("#offline .box_oferta").sort(sortLocationAlg).appendTo('#offline');
			} else {
				$("#recommended .box_oferta").sort(sortLocationAlgRev).appendTo('#recommended');
				$("#online .box_oferta").sort(sortLocationAlgRev).appendTo('#online');
				$("#offline .box_oferta").sort(sortLocationAlgRev).appendTo('#offline');		
			}
			showMarkers();
		});
    	     $("#overlay_update_bg").hide(); 
	     $("#overlay_update").hide(); 
	});
	// // $("#overlay_loading").hide(); $("#below_overlay").show();
}

function sortRating(reverse) {
	// $("#below_overlay").hide(); $("#overlay_loading").show(); 
	$("#overlay_update").center();
	$("#overlay_update_bg").center();
	
	$("#overlay_update").show(function() 
	{
		$("#overlay_update").show(function() 
		{
		if(!reverse) {
			$("#recommended .box_oferta").sort(sortRatingAlg).appendTo('#recommended');
			$("#online .box_oferta").sort(sortRatingAlg).appendTo('#online');
			$("#offline .box_oferta").sort(sortRatingAlg).appendTo('#offline');
		} else {
			$("#recommended .box_oferta").sort(sortRatingAlgRev).appendTo('#recommended');
			$("#online .box_oferta").sort(sortRatingAlgRev).appendTo('#online');
			$("#offline .box_oferta").sort(sortRatingAlgRev).appendTo('#offline');
		}
		showMarkers();
		});
    	     $("#overlay_update_bg").hide(); 
	     $("#overlay_update").hide(); 
	});
}

function sortName(reverse) {
	// $("#below_overlay").hide(); $("#overlay_loading").show(); 
	$("#overlay_update").center();
	$("#overlay_update_bg").center();	
	$("#overlay_update").show(function() 
	{
		$("#overlay_update").show(function() 
		{
	if(!reverse) {
		$("#recommended .box_oferta").sort(sortNameAlg).appendTo('#recommended');
		$("#online .box_oferta").sort(sortNameAlg).appendTo('#online');
		$("#offline .box_oferta").sort(sortNameAlg).appendTo('#offline');
	} else {
		$("#recommended .box_oferta").sort(sortNameAlgRev).appendTo('#recommended');
		$("#online .box_oferta").sort(sortNameAlgRev).appendTo('#online');
		$("#offline .box_oferta").sort(sortNameAlgRev).appendTo('#offline');
	}
	showMarkers();
		});
    	     $("#overlay_update_bg").hide(); 
	     $("#overlay_update").hide(); 
	});
	// // $("#overlay_loading").hide(); $("#below_overlay").show();
}


function kontrolaf()
{
	 if(document.mail_form.email.value == "")
	 {
		 alert("Podaj e-mail");
		 return false;
	 }
	 else if(document.mail_form.bookingnr.value == "")
	 {
		 alert("Podaj numer rezerwacji");
		 return false;
	 }
	 else if(document.mail_form.token1.value == "")
	 {
		 alert("Podaj token");
		 return false;
	 }
	
	 return true;
} 

$(document).ready(function() {
	
		
	$(".hotel_search").find("input.szukaj").hover(
		
		function() {
			$(this).css("cursor","pointer");
			$(this).css("background-image","/datastore/images/polish/search/szukaj_h.gif");
		},
		function() {
			$(this).css("cursor","normal");
			$(this).css("background-image","/datastore/images/polish/search/szukaj.gif");
		}
	);
	$(".flight_search").find("input.szukaj").hover(
		function() {
			$(this).css("cursor","pointer");
			$(this).css("background-image","/datastore/images/polish/search/szukaj_biletow_h.gif");
		},
		function() {
			$(this).css("cursor","normal");
			$(this).css("background-image","/datastore/images/polish/search/szukaj_biletow.gif");
		}
	);
	
	$("input.button").hover(
		function() {
			$(this).css("cursor","pointer");
		},
		function() {
			$(this).css("cursor","normal");
		}
	);
});

