$(function() {
	$('.error').hide();
	$(".button").click(function() {
	   //validate and process here
	   $('.error').hide();
	   var name = $("input#name").val();
		if(name == ""){
			$("label#name_error").show();
			$("input#name").focus();
			return false;
		}
	   
		var email = $("input#email").val();
			if(email == ""){
				$("label#email_error").show();
				$("input#email").focus();
				return false;
			}
		var phone = $("input#phone").val();
			if(phone == ""){
				$("label#phone_error").show();
				$("input#phone").focus();
				return false;
			}
		var msg = $("textarea#msg").val();
			if(msg == ""){
				$("label#msg_error").show();
				$("input#msg").focus();
				return false;
			}	
			
		var image = $("input#cap").val();
			if(image == ""){
				$("label#cap_error").show();
				$("input#cap").focus();
				return false;
			}	
			if(image != "2x4b" && image != "2X4b" && image != "2x4B" && image != "2X4B"){
			   	$("label#cap_error").show();
				$("input#cap").focus();
				return false;
			}	
	
	//check for html in the form
	function dirty(test_text){
		var hunt = new RegExp("http://");
		var hunter = new RegExp("www.");
		var hunted = /<(\S+).*>(.*)<\/\1>/gi;
		
		if(hunted.test(test_text)){
			alert("You appear to have used some HTML on the form, please reove it and re-submit.");
			return true;
		}
		else if(hunt.test(test_text)){
			alert("You appear to have used a web address on the form, please reove it and re-submit.");
			return true;
		}
		else if(hunter.test(test_text)){
			alert("You appear to have used a web address on the form, please reove it and re-submit.");
			return true;
		}
		else {
			return false;
		}
	}
	if(dirty(msg)){
		alert("message");
		return false;
	}
	else if(dirty(phone)){
		alert("phone");
		return false;
	}
	/*else if(dirty(email)){
		alert("email");
		return false;
	}*/
	else if(dirty(name)){
		alert("name");
		return false;
	}
	//end validation
	//start of submit form
	var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&message=' + msg;
	//alert (dataString);return false;
	$.ajax({
		  type: "POST",
		  url: "http://www.scottreed.co.uk/blog/php47/contact/contact.php",
		  data: dataString,
		  success: function(){
			  //alert('success');
				$('#contact_form').html("<div id='message'></div>");
				$('#message').html("<img id='ret_logo' src='http://www.scottreed.co.uk/blog/php47/contact/ret_logo.png' />")
				.append("<p>Thank you for your enquiry.</p><p>We will be in touch shortly.</p>")
				.hide()
				.fadeIn(1500, function(){
				   $('#message').append("");
				 });
		  }
	});
	return false;
	//end of form submit
	});
});
