$(function() {
	$(".sendButton").click(function() {
		$.post("/validate-email.php", { email: $("#email").val() },
			function(data) {
				if (data.succes) {
					$("#email").removeClass("errorMessageField");
					validateForm(true);
				} else {
					$("#email").addClass("errorMessageField");
					validateForm(false);
				}
			}, "json");
		return false;
	});
});

function validateForm(status) {
	$(".validate").each(function(i) {
		
		if ($(this).attr("type") == "checkbox")
		{
			
			if($(this).is(':checked'))
			{
			    // next
			}
			else {
				status = false;	
			}
			
		}
		else
		{
			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) {
		$(".validateForm").submit();
	} else {
		$(".errorMessageText").css({display : "block"});	
	}
}
