// JavaScript Document
$(function()
	{
		 $('#postcode').keyup(function() {

			 var myLength = $("#postcode").val().length;	
			 
			 if(myLength == 4)
			 {
				 $("#postcodeCijfers").focus();
			 }
		 
		 });
		 
		$(".btnSendForm").click(function() {
			
			$("span.errorMessage").each(function(i) {
				$(this).remove();
			});
			
			var valid = true;
			
			$(".validateField").each(function(i) {
			   
			  if ($(this).attr("name") == "omschrijving" && $(this).css("display") != "none") {
					
					if($.trim($(this).val())=="")
					{
					  $(this).addClass('inputError');
					  $(this).after('<span class="errorMessage">Omschrijft u uw zaak of situatie alstublieft zo specifiek mogelijk.</span>');
						valid = false;
					}
					else
					{
					  $(this).css({borderColor: "#b9b9b9"});
						$(this).removeClass('inputError');
					}
					
				}
				else if ($(this).attr("name") == "voornaam" && $(this).css("display") != "none") {
					
					if($.trim($(this).val())=="")
					{
						$(this).addClass('inputError');
						$(this).after('<span class="errorMessage">Vult u alstublieft uw voornaam in.</span>');
						valid = false;
					}
					else
					{
					  $(this).css({borderColor: "#b9b9b9"});
						$(this).removeClass('inputError');
					}
					
				}
				else if ($(this).attr("name") == "achternaam" && $(this).css("display") != "none") {
					
					if($.trim($(this).val())=="")
					{
						$(this).addClass('inputError');
						$(this).after('<span class="errorMessage">Vult u alstublieft uw achternaam in.</span>');
						valid = false;
					}
					else
					{
					  $(this).css({borderColor: "#b9b9b9"});
						$(this).removeClass('inputError');
					}
					
				}
				else if ($(this).attr("name") == "email" && $(this).css("display") != "none") {
					
					if(isValidEmailAddress($.trim($(this).val())))
					{
						$(this).css({borderColor: "#b9b9b9"});
						$(this).removeClass('inputError');
					}
					
					else
					{
						$(this).addClass('inputError');
						$(this).after('<span class="errorMessage">Ongeldig e-mailadres</span>');
						valid = false;
					}
					
				}
				else if ($(this).attr("name") == "telefoonnummer" && $(this).css("display") != "none") {
					
					if($.trim($(this).val())=="")
					{
						$(this).addClass('inputError');
						$(this).after('<span class="errorMessage">Vult u alstublieft uw telefoonnummer in.</span>');
						valid = false;
					}
					else
					{
					  $(this).css({borderColor: "#b9b9b9"});
						$(this).removeClass('inputError');
					}
					
				}
			});
			
			if(!checkPostcode($('#postcodecijfers').val()+$('#postcodeletters').val()))
      { 
        $('#postcodecijfers').addClass('inputError');
        $('#postcodeletters').addClass('inputError');
        $('#postcodeletters').after('<span class="errorMessage">Vult u alstublieft uw postcode in.</span>');
        valid = false;
      }
      else if(checkPostcode($('#postcodecijfers').val()+$('#postcodeletters').val()))
      {
        $('#postcodecijfers').css({borderColor: "#b9b9b9"});
        $('#postcodecijfers').removeClass('inputError');
        $('#postcodeletters').css({borderColor: "#b9b9b9"});
        $('#postcodeletters').removeClass('inputError');
      }
			
			if(valid)
			  $("#contactForm").submit();
			else
			  return false;
			
		});
		 
 });
 
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);
}

function checkPostcode(postcode)
{	
  if(validatePostalCode(postcode))
  {
    return true;
  }
  return false;
}

function validatePostalCode(postcode)
{
  return ((typeof postcode=='string') ? (postcode.match(/[1-9][0-9]{3} *[a-zA-Z]{2}/)==postcode):false);
}			

