$(function() {

	// load the modal window
	$('#contact_button').click(function(){
		$('.QapTcha').empty();
		$('.QapTcha').QapTcha({disabledSubmit:true,autoRevert:false});
		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#name').val('Naam en voornaam');
		$('#email').val('E-mail adres');
		$('#comment').val('Vul uw vraag in...');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#contact, div#mask').stop().fadeOut('slow');

	});

	$('#contactForm input#name, input#email').focus(function() {
		$(this).val('');
	});
	
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
		var name = $('#name').val(),
			email = $('#email').val(),
			comment = $('#comment').val();
		
	
		//Error Count
		var error_count = 0;
		
		//Regex Strings
		//var username_regex = /^[a-zA-Z0-9_-\s]$/;
		var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		var errors = '';
			//Test Username
			if(name == '' || name == 'Naam en voornaam') {
				//$('#contact_header').after('<li class=error>Ongelidg naam en voornaam ingevuld!</li>');
				errors += '<li> - Ongelidg naam en voornaam ingevuld!</li>';
				error_count += 1;
			}
			
			//Test Email
			if(!email_regex.test(email)) {
				//$('#contact_header').after('<li class=error>Ongeldig email ingevuld!</li>');
				errors += '<li> - Ongeldig email ingevuld!</li>';
				error_count += 1;
			}
			
			//Blank Comment?
			if(comment == '' || comment == 'Vul uw vraag in...') {
				//$('#contact_header').after('<li class=error>Geen vraag was ingevuld!</li>');
				errors += '<li> - Geen vraag was ingevuld!</li>';
				error_count += 1;
			}
		
			//No Errors?
			if(error_count === 0) {
				$.ajax({
					type: "post",
					url: "/uc/send.php",
					data: "name=" + name + "&email=" + email + "&comment=" + comment,
					error: function(data) {
						$('.error').hide();
						$('#sendError').slideDown('slow');
					},
					success: function (data) {
						$('.error').hide();
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
					}				
				});	
			}
			else {
				$('#contact_header').after('<ul class="error">' + errors + '</ul>');
                $('.error').show();
            }
			
		return false;
	});
	
});
