$(document).ready(function(){
	
	$('.item .box').css({'opacity':0.2, 'display': 'none'});
	
	
	
	$('.item h2').hover(
		function(){
			if(!$(this).is('.active'))
			$(this).css('text-decoration', 'underline');
		},
		function(){
			if(!$(this).is('.active'))
			$(this).css('text-decoration', 'none');
		}
	);
	
	$('.item h2').each(function(){
	
		$(this).click(function(){
			obj = $(this);
			if($('.item .box:visible').length)
			{
				$('.item .box:visible').prev('h2').children('span').fadeIn('slow');
				$('.item .box:visible').prev('h2').removeClass('active');
				$('.item .box:visible').animate({'height':'toggle', 'opacity':0.2}, 300, function(){
					obj.next('.box').animate({'height':'toggle', 'opacity':1}, 500);
				});	
			}
			else
				obj.next('.box').animate({'height':'toggle', 'opacity':1}, 500);
				
			$(this).children('span').fadeOut('slow');
			$(this).addClass('active');
			
			
		});
	});
	$('.item h2:first').click();
	
	/* 	===== formular validation ======	*/
	
	$('#form').submit(function(){
		err = false;
		
		resetForm();
		
		
		/* zgoda  */
		if(!$('#agree:checked').length)
		{
			$('.form-err').html('Proszę wyrazić zgodę na przetwarzanie danych.');
			$('#agree').parent().addClass('err-input');
			return false;
		}
		
		/* imie i nazwisko */
		obj = $('#name');
		if(!obj.val() || validateAlpha(obj.val())===false || obj.val()=='Pole wymagane')
		{
			err = true;
			obj.parent().addClass('err-input');
			if(!obj.val())
				obj.val('Pole wymagane');
		}
		
		/* text  */
		obj = $('#text');
		if(!obj.val() || obj.val()=='Pole wymagane')
		{
			err = true;
			obj.parent().addClass('err-input');
			obj.val('Pole wymagane');
		}
		
		
		/* email */
		obj = $('#email');
		if(!obj.val() || validateEmail(obj.val())===false || obj.val()=='Pole wymagane')
		{
			err = true;
			obj.parent().addClass('err-input');
			if(!obj.val())
				obj.val('Pole wymagane');
		}
		
		
		/* telefon */
		obj = $('#phone');
		if(!obj.val() || validatePhone(obj.val())===false || obj.val()=='Pole wymagane')
		{
			err = true;
			obj.parent().addClass('err-input');
			
			if(!obj.val())
				obj.val('Pole wymagane');
		}
		
		if(err)
		{
			$('.form-err').html('Prosimy o poprawne wypełnienie pól oznaczonych na czerwono.');
			return false;
		}
		
		//return false;	
	});
	
	$('input').click(function(){
		if($(this).val()=='Pole wymagane')
			$(this).val('');
			
		$(this).parent().removeClass('err-input');
	});
	

	$('textarea').click(function(){
		if($(this).val()=='Pole wymagane')
			$(this).val('');
			
		$(this).parent().removeClass('err-input');
	});
	
})


function validateEmail(str)
{
	var regex = /^[0-9a-z]+([\.\-_][0-9a-z]+)*\@[0-9a-z]+([\.\-][0-9a-z]+)*\.[a-z]{2,4}$/;
	return regex.test(str);
}

function validatePhone(str)
{
	var regex = /^[0-9\s]{1,}$/;
	return regex.test(str);
}

function validateAlpha(str)
{
	var regex = /^[a-zA-ZąęółśćźżćńŁŻŹŚĆ\s\-]{1,}$/;
	return regex.test(str);
}

function resetForm(){
	$('.err').hide();
}
