jQuery(document).ready(function($){ 

// contact                           
    var error = true;         
    function addLoading()
    {
		$('#sendbutton').val('wait...').attr('disabled', true);
	}    
   
    function removeLoading()
    {
		$('#sendbutton').val('Send').attr('disabled', false);
	}
	
	function addError(msg, e)
	{
		error = true;
		$(e).addClass('error');
		$('#usermessagea').html(msg);	
	}
	
	function removeError(e)
	{
		error = false;
		$('#usermessagea').html('');     
		$(e).removeClass('error');
	}
	
	$('#email').blur(function(){             
		var expr = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
		
		if( !expr.test( $(this).val() ) )  
			addError('<p class="error">Adres e-mail jest niepoprawny!</p>', this);            
		else 
			removeError(this);
	});
    	
	$('#form-contact .required').blur(function(){
		if( $(this).val() == '' )
			addError('<p class="error">Jedno lub więcej wymaganych pól nie są wypełnione!</p>', this);
		else               
			removeError(this);
	});
    
	$('#form-contact').submit(function(){
		if( !error )
		{                                                
			var datastring = '';
			
			$('#form-contact input, #form-contact select, #form-contact textarea').each(function(){
				datastring = datastring + $(this).attr('name') + "=" + $(this).val() + '&';
			});
			
			$('#usermessagea').html(''); 
			addLoading();
			
			$.post( $(this).attr('action'), datastring, function(response){
				$('#usermessagea').html(response);
				removeLoading();	
			});
		}
		else    
			addError('<p class="error">Jedno lub wiecej pól nie są wypełnione poprawnie!</p>', this);
		return false;
	});
});
