$(function() {

	
	$.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 = 0;
 		});
 	};

	var use_ajax=true;

	$('#contact-form').validate({		
		success :  function(){use_ajax=true},
		failure : function(){use_ajax=false;}
	 })

	$('#contact-form').submit(function(e){
	
			if(use_ajax)
			{
				$('#loading').css('visibility','visible');
				$.post('http://www.4muladesign.co.uk/assets/includes/submit.php',$(this).serialize()+'&ajax=1',
				
					function(data){
						if(parseInt(data)==-1)
							$('#verifyError').html('<p>Check verification</p>');
							
						else
						{	
							$('#verifyError').html('');
							$('#success').html('<p>Message sent, thank you!</p>');
							$('#contact-form').clearForm();
						}
						
						$('#loading').css('visibility','hidden');
					}
				
				);
			}
			e.preventDefault();
	})

});