$(function() {
	$(".sendButton").click(function() {
		if($("#email").length > 0) {
			$.post("/validate-email.php", { email: $("#email").val() },
				function(data) {
					if (data.succes) {
						$("#email").removeClass("errorMessageField");
						validateForm(true);
					} else {
						$("#email").addClass("errorMessageField");
						validateForm(false);
					}
				}, "json");
		}
		else {
			validateForm(true);
		}
		return false;
	});
});

function validateForm(status) {
	$(".validate").each(function(i) {
		if ($(this).attr("name") != "E-mailadres") {
			if ($(this).val() != "") {
				if ($(this).attr("name") == "Vraag") {
					$(".vraag").css({display : "none"});
				} else if ($(this).attr("name") == "Bericht") {
					$(".bericht").css({display : "none"});
				} else {
					$(this).removeClass("errorMessageField");
				}
			} else {
				status = false;
				if ($(this).attr("name") == "Vraag") {
					$(".vraag").css({display : "block"});
				} else if ($(this).attr("name") == "Bericht") {
					$(".bericht").css({display : "block"});
				} else {
					$(this).addClass("errorMessageField");
				}
			}
		}
	});
	if (status) {
		$(".defaultForm").submit();
	} else {
		$(".errorMessageText").css({display : "block"});	
	}
}
