$(function() {
  $('.errorInline').hide();
	$('.errorContact').hide();
	$('.successContact').hide();
	
	$('#sendContact').click(function(e){

				e.preventDefault();
				/* Variables */
				var error = false;
				var name = $('#name').val();
				var email = $('#email').val();
				var message = $('#message').val();
				var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

				/* Variables check */
				if(name.length == 0){
					var error = true;
					$('#name_error').fadeIn(500);
					$('#name').focus();
					return false;
				}else{
					$('#name_error').fadeOut(500);
				}
				if (!filter.test(email)) {
					var error = true;
					$('#email_error').fadeOut(500);
					$('#email_error').fadeIn(500);
					$('#email').focus();
					return false;
				}
				else{
					$('#email_error').fadeOut(500);
				}
    	  if((message.length == 0)){
    	  	$("#message_error").fadeIn(500);
    	  	$('#message').focus();
    	  	return false;
    	  }
    	  else{
					$('#message_error').fadeOut(500);
				}

				if(error == false){
					//disable the submit button to avoid spamming
					//and change the button text to Sending...
					$('#sendContact').attr({'disabled' : 'true', 'value' : 'Sending...' });

					$.post("php/send.php", $("#contact").serialize(),function(result){
					 // alert ($("#contact").serialize());return false;
						if(result == 'sent'){
							//if the mail is sent remove the submit paragraph
							 $('#sendContact').remove();
							//and show the mail success div with fadeIn
							$('#contact_success').fadeIn(500);
						}else{
							//show the mail failed div
							$('#contact_fail').fadeIn(500);
							//reenable the submit button by removing attribute disabled and change the text back to Send The Message
							$('#sendContact').removeAttr('disabled').attr('value', 'Retry');
						}
					});
				}
			});    
		});
