$(document).ready(function(){ 

//// valideer //////////////////////

$(".btnverstuur").click
(function () {
	
		$(".form").addClass("send");
		var formvalid = validate();

		if(formvalid == true)
		{	  
			
											
			var src = $(".form").serialize();
		
			$.ajax
			({
				type: "POST",
				url: "verzenden.php",
				data: src,
				beforeSend: function()
				{
					$(".loadingdiv").show();
					$(".btnverstuur").animate
					({
						"opacity": 0.2
					});
					
					$(".inputleft").animate
					({
						"opacity": 0.2
					});
					
					$(".inputright").animate
					({
						"opacity": 0.2
					});
					
					$(".jq_watermark").animate
					({
						"opacity": 0.2
					});
				
				
				},
				success: function(msg)
				{
					
					$(".emailheader").html("Bedankt voor uw E-mail");
					$(".emailbox").html(msg);
					
				}
			});
		}

});


//// on blur//////////////////////
 var arrblur = { "naam":true, "email":false, "onderwerp":false, "bericht":false };
jQuery.each(arrblur, function(classname, cap) {
	
	$("."+classname).blur(function () {
		if($(".form").hasClass("send")){validate();}
	});

});

//// on focus//////////////////////
							   
 var arrFocus = { "naam":true, "email":false, "onderwerp":false, "bericht":false  };
jQuery.each(arrFocus, function(classname, smallBox) {							   

	$("."+classname).focus(function () {
		
		 $(".alert"+classname).animate({"opacity": 0},200);
		 
		if(classname == "email")
		{
			$(".alerttxt").slideUp("slow");
		}
	});

});

//// bewerk ingevoerde tekst//////////////////////

function capital(classname, voorletters)
{	
	var str = $(classname).val();
	if (voorletters == true)
	{
		textBoxValue = str.split(".");
		textBoxValue = textBoxValue.join("");
		textBoxValue = textBoxValue.toUpperCase().split("");
		textBoxValue = textBoxValue.join(".")
		
	}
	else 
	{
		textBoxValue = str.charAt(0).toUpperCase() + str.slice(1);
	}
	
	
	$(classname).val(textBoxValue);							
}


});




//// valideer //////////////////////

function validate()
{

	var valid = true;
	
	/// als er iets niet ingevuld is, verstuur dan niet ///
	
	if($(".naam").val() == "")							{ var valid = false; }
	if($(".email").val() == "")							{ var valid = false; }
	if($(".onderwerp").val() == "")						{ var valid = false; }
	if($(".bericht").val() == "")						{ var valid = false; }
	

///// valideer de invoer velden /////

 var arrValidate = { "naam":true, "email":false, "onderwerp":false, "bericht":false  };
jQuery.each(arrValidate, function(classname, smallBox) {	
	
		if($("."+classname).val() == "")
		{
			
			var position = 		$(".bericht").offset();
			var htmlalert = "<div class=\"alert alert"+classname+" \"></div>";
			 $(".field"+classname).append(htmlalert);
			 
			 
		}
});


////// valideer het opgegeven email adres ////

var email = $(".email").val();
if(email != "")
{
	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);
	
	var validemail = pattern.test(email);
}

if (validemail == false)
{
			
	$(".alerttxt").html("U heeft een ongeldig e-mail adres opgegeven");
	$(".alerttxt").slideDown("slow");
	
	var valid = false;
}


return valid;

}

