function clearFields() {
	$('form#form_contact input, form#form_contact textarea').focus(function(){
			if ($(this).attr('rel') != 'cleared') {
				$(this).val('').attr('rel', 'cleared');
			}		
		});
	$('form#form_contact input, form#form_contact textarea').blur(function(){
			if($(this).val() == ""){	
				$(this).val(this.defaultValue).attr('rel', '');
				
			}		
		});		
}
function verifyErrors() {
	var ready = 1;
	$('.required_field').css('color', '#6b6b6b');
	$('.required_field').each(function(){			
			if(($(this).val() == "")||(!$(this).attr('rel'))) {
				$(this).css('color', '#d6041f');
				ready = 0;
			}		
		});
	$('input.email').each(function(){
			var email_val = $(this).val();
			if(email_val.indexOf('@') < 0) {
				$(this).css('color', '#d6041f');
				ready = 0;
			}		
		});
	$('input.security').each(function(){			
			if($(this).val() != "4") {
				$(this).css('color', '#d6041f');
				ready = 0;
			}		
		});
	if (ready == 0) {
		$('p.warning').text('* Vul s.v.p alle verplichte velden in').css('color', '#d6041f');
		return false;
	};
	return true;	
}
function formControl() {
	$('form#form_contact').submit(function(){
		if (verifyErrors()) {
			action = $(this).attr('action');
			mesagetosend = $(this).serialize();
			$.ajax({
					type: "POST",
					url: 'email.php',
					data: mesagetosend,
					dataType : "html",
					timeout:10000,
					error: function() {
						replaceContent();
					},
					success: function(msg){
						replaceContent(); 
					}
				});
			}
		return false;
	});
}
function replaceContent() {
	$('div#contact').replaceWith('<div id="contact_rechts" class="ajax"><h3>Succes!</h3><p><strong>Hartelijk dank voor het invullen van dit formulier.</strong><br/>Het formulier is succesvol verzonden.<br/><em>Wij zullen binnen 2 werkdagen reageren op uw aanvraag.</em></p><p>&nbsp;</p></div>');
}
jQuery(function($) {
	clearFields();
	formControl();
});

