$(document).ready(function(){

	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}

	$("#mail").change(function(){
		var email = $("#mail").val();
		if(!isValidEmailAddress(email)) {
			$("#form span").text("Niepoprawny e-mail");
		} else { $("#form span").text("");}
	});
	
	$("#wyslij").click(function(){
		var name = $("#name").val();
		var mail = $("#mail").val();
		var topic = $("#topic").val();
		var contentf = $("#contentf").val();
		if ($("#contentf").val()=="") { 
			$("#bad").fadeIn("slow").fadeTo(2000, 1).fadeOut("slow");
		} else {
		if (!isValidEmailAddress(mail)) {
			$("#form span").text("Wpisz poprawny e-mail");
		} else {
			$.ajax({
			type: "POST",
			url: "mail.php",
			data: {name: name, mail: mail, topic: topic, content: contentf},
			onsuccess:$("#good").fadeIn("slow").fadeTo(2000, 1).fadeOut("slow")
		});
		}
		}
	});

});