jQuery.fn.slideFadeToggle = function(speed, easing, callback) {

  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);  

};



$(window).load(function(){

	$('form#contactform').each(function() {

		$(this).submit(function() {

			$('.required', this).removeClass('error')

			$('em.error').remove();

			

			var error = false,

				submittedForm = $(this);

			

			$('.required', this).each(function() {	

				var fieldName = $(this).attr('name'),

					fieldLabel = $(this).prev().text();

					

				if( $.trim($(this).val()) == '' ) {

					$(this).parent().append('<em class="error">Please enter your ' + fieldLabel + '.</em>');

					$(this).addClass('error');

					error = true;

				} else if( $(this).hasClass('email') ) {	

					if( !$.trim( $(this).val() ).match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/) ) {

						$(this).parent().append('<em class="error">Please enter a valid '+fieldLabel+'.</em>');

						$(this).addClass('error');

						error = true;

					}

				}

			});

							

			if(!error){

				$('.submit', this).after('<span class="loading"></span>')

				var fieldsVal = $(this).serialize();

								

				$.ajax({

					type: "POST",

					url: ajaxurl,

					data: fieldsVal,

					success: function(result){

						submittedForm.before(result);

						submittedForm.slideFadeToggle(500, function(){

							$(this).remove();	

						});

					}

				});

			}

			return false;

		});		

	});

});
