var flaga = 0;
var animacja = 0;

$(function()
{
	Date.format = 'yyyy-mm-dd';
	$('.date-pick')
		.datePicker({clickInput:true, horizontalOffset:'-155', startDate:(new Date()).asString()})
});

$(document).ready(function(){
  	$("#formreservation input").focus(
  		function() { 
			$(this).parent().children("span.error_message").html(''); 	
			$(this).parent().children("span.error_message").addClass('nodisplay'); 	
  		}
  	);  	  	
  	
  	$(".showteltab").mouseover(function(){
		if ($(this).parent().attr('class') != 'teletab animation')
		{			
			$(this).parent().animate({"top": "-1px"}, "fast",function(){
				$(this).addClass('animation');
			});
		}								
	})
	
	$(".teletabin").mouseleave(function(){
		if ($(this).parent().attr('class') == 'teletab animation')
		{			
			$(this).parent().animate({"top": "-49px"}, "fast", function(){
				$(this).removeClass('animation');		
			});
		}
	})
});	


$(document).click(function(e)
  {
    //getting height and width of the message box
    var height = $('#popuup_div').height();
    var width = $('#popuup_div').width();
    //calculating offset for displaying popup message
    leftVal=e.pageX-(width/2)+"px";
    topVal=e.pageY-(height/2)+"px"; 
    //show the popup message and hide with fading effect
    $('#popuup_div').css({left:leftVal,top:topVal}).show().fadeOut(1500);
  });   


function showDivBox( div1 ) {
	if($('#'+div1).css('display')=='none'){
		$('#'+div1).css({display: 'block'});
	}else if($('#'+div1).css('display')=='block') $('#'+div1).css({display:'none'});
	
}
function insertSig(form){
	var status = false;
	$.ajax({
   			type: "POST",
	   		url: "ajax.php",
	   		data: $(form).serialize()+'&option=insertSig',
	   		async: false,
	   		beforeSend: function() {
	   		},
	   	success: function(msg){
	   		if(msg != '') $("#sig").val(msg);
	   		//status = true;
   		}
 	});
 	return status;
}

function saveSteps(form, step){
	//if( $("#formreservationstep2 #ptyp_platnosc input:radio:checked").attr('value') == 2 ){
	$.ajax({
   		type: "POST",
   		url: "ajax.php",
   		data: $(form).serialize()+'&step='+step+'&option=saveSteps',
   		async: false,
   		beforeSend: function() {
   		},
	   	success: function(msg){
	   		//$("#loader").hide();
	   		form.submit();
   		}
 	});
	//}else{
//		form.submit();
//	}
}
function changesig(form){
	$.ajax({
	   		type: "POST",
	   		url: "ajax.php",
	   		data: $(form).serialize()+'&option=changesig',
	   		async: false,
	   		beforeSend: function() {
	   		},
		   	success: function(msg){
		   		$('#sig').val(msg);
	   		}
	 	});
}

function sendformcontact(form){
	$("#loader").show(function() {
		$.ajax({
	   		type: "POST",
	   		url: "ajax.php",
	   		data: $(form).serialize()+'&option=sendformcontact',
	   		async: false,
	   		beforeSend: function() {
	   		},
		   	success: function(msg){
		   		$("#loader").hide();
		   		if(msg == 'true'){
					$('.sukces').removeClass('hidden');
		   		}
	   		}
	 	});
	});
}


function checkAvailability(){
	if($('#data_przyjazdu').val() == '' || $('#data_wyjazdu').val() == '') return false;
	var dane = $("#formreservation").serialize();
	
	if( dane == ''){
		var dane = $("#formreservation2").serialize();
	}
	$.ajax({
   		type: "POST",
   		url: "ajax.php",
   		data: dane+'&option=checkavailability',
   		async: false,
   		beforeSend: function() {
   			$("#availability_msg").html('trwa sprawdzanie dostępności...');
   		},
	   	success: function(msg){
	   		if(msg == 'true'){
	   			$("#miejscaniedostepne").addClass('hidden');	
	   			$("#rezerwacjainfo").addClass('hidden');	
	   			$("#miejscadostepne").removeClass('hidden');	
	   		}else{
	   			$("#rezerwacjainfo").addClass('hidden');
	   			$("#miejscaniedostepne").removeClass('hidden');	
	   			$("#miejscadostepne").addClass('hidden');	
	   		}
   		}
 	});
}
function checkAvailability2(form){
	$.ajax({
   		type: "POST",
   		url: "ajax.php",
   		data: $(form).serialize()+'&option=checkavailability',
   		async: false,
   		beforeSend: function() {
   			$("#availability_msg").html('trwa sprawdzanie dostępności...');
   		},
	   success: function(msg){
	   		if(msg == 'true'){
				form.submit();
	   		}else if(msg == 'false'){ 
	   			$('#msg_error').removeClass('hidden');
	   			$('#msg_error').html('W zadanym terminie nie ma dostępnych miejsc dla wybranej ilość samochodów');
	   		}else if(msg == 'false2'){ 
	   			$('#msg_error').removeClass('hidden');
	   			$('#msg_error').html('Źle wybrany zakres dat. Data wyjazdu jest wcześniejsza niż data przyjazdu');
	   		}
   		}
 	});
}
function changePrice(data_przyjazdu, data_wyjazdu, ilosc_samochodow, godzina_przyjazdu, godzina_wyjazdu){
	if(data_przyjazdu == '' || data_wyjazdu == '' || ilosc_samochodow == 0 || ilosc_samochodow == '') return false;
	$.ajax({
   		type: "POST",
   		url: "ajax.php",
   		data: $("#formreservationstep1").serialize()+'&option=changePrice',
   		async: false,
   		beforeSend: function() {
   			$("#availability_msg").html('trwa wylicznaie ceny...');
   		},
	   	success: function(msg){
	   		if (msg != '')
	   		{
				$("#amount").val(msg);
				$("#pamount label.error").css({display: 'none'});
				$("#pamount").removeClass('perror');
	   		}	
    		else
    		{
    			$("#pamount").addClass('perror');
    			$("#pamount label.error").remove();
    			$("#pamount #amount").after('<label for="amount" generated="true" class="error">Błędny zakres czasowy.</label>')
    			$("#amount").val('');
    		}
    			
    			 
   		}
 	});
}

function changeNoHour(pole, wartosc){
	if(wartosc){
		$('#'+pole).val('')
		$('#'+pole+'_minuta').val('');
		$('#'+pole).parent().children("span.error_message").html(''); 	
		$('#'+pole).parent().children("span.error_message").addClass('nodisplay'); 	
	}else{
		$('#'+pole).focus();
	}
	
	
}
function changeNoHour2(pole){
	if(isChecked('#'+pole+"_nieznana")){
		$('#'+pole).val('');
		$('#'+pole).attr('readonly', 'readonly');
		$('#'+pole+'_minuta').val('');
		$('#'+pole+'_minuta').attr('readonly', 'readonly');
		$('#'+pole).parent().children('label.error').remove(); 	
	}else{
		$('#'+pole).removeAttr('readonly');
		$('#'+pole+'_minuta').removeAttr('readonly');
		$('#'+pole).focus();
	}
}


function isNumberKey(evt)
{
	try
	{
		var charCode = (evt.which) ? evt.which : event.keyCode
	    if (charCode > 31 && (charCode < 48 || charCode > 57))
	    	return false;
	}catch(err) { }
	
	return true;
}

function isKnowHour(pole, evt)
{
	if($('#'+pole+'_nieznana').attr('checked')) return false;
	return isNumberKey(evt);
}

function addSend()
{
	if( $("#input_summary_users").val() == 0 ) alert('Liczby odbiorców nie może być zerowa.');
	
	$.ajax({
   		type: "POST",
   		url: "../send/new",
   		data: $("#form_new_send").serialize(),
   		beforeSend: function() {
   			$("#send_progress_text").html('<br/>Trwa przygotowywanie danych do wysyłki.<br/><img src="../images/ajax-loader.gif" alt="">');
   		},
	   success: function(msg){
    		 $("#send_progress_text").html('<br/>Utworzono dane do wysyłki.');
   		}
 	});
 	
	return false;
}

function selectClick(element)
{
	$('.hourselect_in').addClass('hidden');
	flaga = 0;
	if  ($(element).parent().children(':last-child').attr('class') == 'hourselect_in hidden')	
	{
		$(document).bind('click', switchSelect);
		$(element).parent().children(':last-child').removeClass('hidden');		
		
	}	
	else
	{		
		$(element).parent().children(':last-child').addClass('hidden');
		$(document).unbind('click', switchSelect);		
	}	
			
}

function switchSelect(){
	if (flaga) {
		flaga = 0;
		$('.hourselect_in').addClass('hidden');
	}
	else flaga = 1;	
}

function toggleBoxAktualnosci() {
	if( $('#box_aktualnosci').css('display') != 'block' ) {
		$('#benefits').slideUp('slow', function() { $('#box_aktualnosci').css('display', 'block'); } );
		
	}
	else {
		$('#benefits').slideDown('slow');
		$('#box_aktualnosci').css('display', 'none');
	}
}


