$(function() {
	$(".submitBtn").click(function() {
		$.post("validate-email.php", { email: $("#email").val() },
			function(data) {
				if (data.succes) {
					if ($("#novalidate_email").val() == 'novalidate_email') {
						$("#email").removeClass("fieldValidate");
						$("#email_error").html('');
						validateForm(true);
					} else {
						$.post("check_email.php", { email: $("#email").val() },
							function(data) {
								$("#email").removeClass("fieldValidate");
								$("#email_error").html('');
								validateForm(true);
							}, 
						"json");
						}
				} else {
					$("#email_error").html('Het opgegeven e-mailadres is ongeldig.');
					$("#email").addClass("fieldValidate");
					$("#formHint_email").css({display : "block"});
					validateForm(false);
				}
			}, "json");
		return false;
	});
});

function validateForm(status) {
	$(".validate").each(function(i) {
		if ($(this).attr("name") != "Emailadres") {
			if ($(this).val() != "") {
				$(this).removeClass("fieldValidate");
				$("#formHint_"+ $(this).attr("id")).css({display : "none"});
			} else {
				status = false;
				$(this).addClass("fieldValidate");	
				$("#formHint_"+ $(this).attr("id")).css({display : "block"});
			}
		}
	});
	if (status) {
		$("form").submit();
	} else {
		var password = $("#password").val();
		var confirmpassword = $("#confirmpassword").val();
		if (password != "" && confirmpassword != "") {
			if (password != confirmpassword) {
				alert('De door u ingevoerde wachtwoorden komen niet overeen.')
			}
		}
		
		$(".errorMessage").css({display : "block"});	
	}
}
