$(function(){
	$('.missing_error').hide();
	$('.email_error').hide();
	$('.progress').hide();
	$('#contact-response').hide();
	$('label.name').removeClass('error');
	$('label.email').removeClass('error');
	$('label.message').removeClass('error');

    $("#JqAjaxForm").submit(function(){

    	$('.missing_error').hide();
		$('.email_error').hide();
		$('.progress').hide();
		$('#contact-response').hide();
		$('label.name').removeClass('error');
		$('label.email').removeClass('error');
		$('label.message').removeClass('error');
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        
        var name = $('input#name').val();
        if(name == ''){
            $('.missing_error').show();
            $('label.name').addClass('error');
            $('input#name').focus();
            return false;
        }
        var email = $('input#email').val();
        if(email == ''){
            $('.missing_error').show();
            $('label.email').addClass('error');
            $('input#email').focus();
            return false;
       } else if(!emailReg.test(email)) {
            $('.email_error').show();
            $('label.email').addClass('error');
            $('input#email').focus();
            return false;
       }
        var message = $('textarea#message').val();
        if(message == ''){
           $('.missing_error').show();
            $('label.message').addClass('error');
            $('textarea#message').focus();
            return false;
        }
        
        $('#contactMessage')
		.css('opacity','0')
		.css('position','absolute')
		.css('color','#000')
		.css('background','#fff')
		.css('z-index','2000')
		.css('height','310px')
		.css('width','343px')
		.css('top','50%')
		.css('left','50%')
		.css('margin-left','-170px')
		.css('margin-top','-115px');
		
		 $('.progress')
		 .css('position','absolute')
		.css('top','30%')
		.css('left','50%')
		.css('margin-left','-18px')
		.css('margin-top','-18px');
		
		$('#contact-response')
		.css('position','absolute')
		.css('top','30%')
		.css('height','155px')
		.css('width','343px');
		
		$('#contactMessage').animate({
	    	opacity: 0.8
  		}, 500, function() {
    		$('.progress').show();
  		});

        $(':submit').attr('disabled', 'disabled');
        
        dataString = $("#JqAjaxForm").serialize();

        $.ajax({
        type: "POST",
        url: "scripts/contactProcess.php",
        data: dataString,
        dataType: "json",
        success: function(data) {
			
			setTimeout(function() {
				$('.progress').hide();
				$('#contact-response').show();
				$(':submit').removeAttr('disabled', 'disabled');
				$('form').clearForm();
				$('form')[ 0 ].reset();
				setTimeout(function() {
					$('#contactMessage').fadeOut( function() {
						$('#contact-response').hide();
      					$('#contactMessage').hide();
    				});
				}, 2000);
			}, 2000);
			
        }

        });

        return false;            

    });
});

$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

