var options = { 
	beforeSubmit: validate,  // pre-submit callback 
	success: showResponse,  // post-submit callback 
	resetForm: true        // reset the form after successful submit 
}; 

// Comment form
$('#commentForm').ajaxForm(options); 

function showResponse(responseText, statusText){
	$('#commentForm, .commentsReply h4').animate({ opacity: 'hide' }, 'fast');
	$('.success').animate({ opacity: 'show' }, 'slow');
}

function validate(formData, jqForm, options) {
	$('.error').animate({ opacity: 'hide' }, 'slow');

	var authorValue = $('input[name=author]').fieldValue();
	var emailValue = $('input[name=email]').fieldValue();
	var commentValue = $('textarea[name=comment]').fieldValue();

	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var correct = true;

	if (!authorValue[0]) {
		$('.error.wrong_author').animate({ opacity: 'show' }, 'slow');
		correct = false;
	}

	if (!emailValue[0]) {
		$('.error.wrong_email').animate({ opacity: 'show' }, 'slow');
		correct = false;
	} else if(!emailReg.test(emailValue[0])) {
		$('.error.wrong_email').animate({ opacity: 'show' }, 'slow');
		correct = false;
	}

	if (!commentValue[0]) {
		$('.error.wrong_comment').animate({ opacity: 'show' }, 'slow');
		correct = false;
	}

	if (!correct) {return false;}
} 	

$('.success').click( function () { 
	$(this).animate({ opacity: 'hide' }, 'fast');
	$('.commentsReply h4').animate({ opacity: 'show' }, 'slow');
});

