// JavaScript Document
$(function(){
	
	$(".submit-button").click(function() {
		
		validateForm(true);
		
		return false;
		
		
	});
	
	$(".validateField").each(function(i) {

		$(this).keyup(function(){
			if ($(this).attr("name") == "Email" && $(this).css("display") != "none") {
				
				if(isValidEmailAddress($.trim($(this).val())))
				{
					$(this).removeClass("errorBorder");
				}
				
				else
				{
					status = false;
					$(this).addClass("errorBorder");
				}
				
			}
		});
		
		$(this).blur(function(){
			if ($(this).attr("name") == "Email" && $(this).css("display") != "none") {
				
				if(isValidEmailAddress($.trim($(this).val())))
				{
					$(this).removeClass("errorBorder");
					
				}
				
				else
				{
					status = false;
					$(this).addClass("errorBorder");
				}
				
			}
			
			if ($(this).attr("name") != "Email") {
				if ($.trim($(this).val()) == "" && $(this).css("display") != "none") {
					status = false;
					$(this).addClass("errorBorder");
				}
				
				else
				{
					$(this).removeClass("errorBorder");
				}
			}
			
			if ($(this).attr("name") == "Bericht" && $(this).css("display") != "none") {
			
				if ($.trim($(this).val()) == "" && $(this).css("display") != "none") {
					status = false;
					$(this).addClass("errorBorderTextarea");
				}
				
				else
				{
					$(this).removeClass("errorBorderTextarea");
				}
			
			}
			
		});
		
	});

});

function validateForm(status) {
		
	$("input.errorBorder").each(function(i) {
		$(this).removeClass("errorBorder");
	});
	
	
	$(".validateField").each(function(i) {
									
				
		if ($(this).attr("name") == "email" && $(this).css("display") != "none") {
			
			if(isValidEmailAddress($.trim($(this).val())))
			{
				//$(this).css({border: "1px solid #acc458"});
			}
			
			else
			{
				status = false;
				$(this).addClass("errorBorder");
			}
			
		}

		
		if ($(this).attr("name") != "email") {
			if ($.trim($(this).val()) == "" && $(this).css("display") != "none") {
				status = false;
				$(this).addClass("errorBorder");
			}
			
			else
			{
				//$(this).css({border: "1px solid #acc458"});
			}
		}
		
	});
	
	if (status) {
	
		// hier de php om te versturen
		var data = makeForm("#contact-form");
			// path = pad naar php file in de action uit het formulier
		path = $("#contact-form").attr("action");

		sendData(path, data);
		
	}

}

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);
}

