$(document).ready(function() {
	// VARIABLES
	var add					= 'images/accept.png';
	var del					= 'images/delete.png';
	var valid				= true;
	var field_small_ok		= 'images/field-small-ok.gif';
	var field_small_error	= 'images/field-small-error.gif';
	var field_large_ok		= 'images/field-large-ok.gif';
	var field_large_error	= 'images/field-large-error.gif';
	
	// PRELOAD IMAGES
	$.preLoadImages(add, del, field_small_ok, field_small_error, field_large_ok, field_large_error);
	
	// GESLACHT
	$('#contactform [name=geslacht]').blur(function() {
		if($('#contactform [name=geslacht]:checked').val()) {
			$('[class=aanhefimg]').html($('<img>').attr('src', add));
		} else {
			$('[class=aanhefimg]').html($('<img>').attr('src', del));
			valid = false;
		}
	})

	// NAAM
	$('#contactform [name=naam]').blur(function() {
		if($(this).attr('value').length > 1) {
			$(this).css('background', "url('" + field_large_ok + "')");
		} else {
			$(this).css('background', "url('" + field_large_error + "')");
			valid = false;
		}
	})

	// POSTCODE
	$('#contactform [name=postcode]').blur(function() {
		// REMOVE MULTIPLE WHITESPACE
		$(this).attr('value', $(this).attr('value').replace(/\s{1,}/ig, ''));
		
		// UPPERCASE POSTCODE
		$(this).attr('value', $(this).attr('value').toUpperCase());

		if(/(^[0-9]{4}[a-zA-Z]{2}$)/i.test($(this).attr('value'))) {
			if ($(this).attr('value') == '1234AB')
			{
				$(this).css('background', "url('" + field_small_error + "')");
				valid = false;
			}
			else
			{
				$(this).css('background', "url('" + field_small_ok + "')");
			}
		} else {
			$(this).css('background', "url('" + field_small_error + "')");
			valid = false;
		}
	})

	// TELEFOON
	$('#contactform [name=telefoon]').blur(function() {
		// REPLACE +31 / 0031 -> 0
		$(this).attr('value', $(this).attr('value').replace('+31', '0'));
		$(this).attr('value', $(this).attr('value').replace('0031', '0'));
		
		// REMOVE ANYTHING BUT A DIGIT
		$(this).attr('value', $(this).attr('value').replace(/[\D]{1,}/ig, ''));
		
		if(/(^[0-9]{10}$)/i.test($(this).attr('value'))) {
			if (
				$(this).attr('value') == '0331234567'
				|| $(this).attr('value') == '0612345678'
				|| $(this).attr('value') == '0123456789'
				|| $(this).attr('value') == '0000000000'
				|| $(this).attr('value') == '1234567890'
				|| $(this).attr('value') == '0600000000'
				|| $(this).attr('value') == '033 1234567'
				|| $(this).attr('value') == '06 12345678'
				|| $(this).attr('value') == '06 00000000'
			)
			{
				$(this).css('background', "url('" + field_small_error + "')");
				valid = false;
			}
			else
			{
				$(this).css('background', "url('" + field_small_ok + "')");
			}
		} else {
			$(this).css('background', "url('" + field_small_error + "')");
			valid = false;
		}
	})
	
	//huisnummer
	$('#contactform [name=huisnummer]').blur(function() {
		if(/^([0-9]){1,}([A-Z]){0,3}/i.test($(this).attr('value'))) {
			$(this).css('background', "url('" + field_small_ok + "')");
		} else {
			$(this).css('background', "url('" + field_small_error + "')");
			valid = false;
		}
	})

	// E-MAILADRES
	$('#contactform [name=email]').blur(function() {
		// REMOVE MULTIPLE WHITESPACE
		$(this).attr('value', $(this).attr('value').replace(/\s{1,}/ig, ''));
		
		if($(this).attr('value').length > 1) {
			if(/^(("[\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.test($(this).attr('value'))) {
				$(this).css('background', "url('" + field_large_ok + "')");
			} else {
				$(this).css('background', "url('" + field_large_error + "')");
				valid = false;
			}
		}
	})
	
	// FORMULIER CONTROLE
	$('#contactform').submit(function() {
		// FORM IS ALWAYS INVALID
		valid = true;
		
		// BLUR ALL THE FORM ELEMENTS FOR VALIDATION
		$('#contactform input[type=text], input[type=radio], select').each(function() {
			$(this).blur();
		})

		// FORM NOT VALID? DON'T SUBMIT
		if(!valid) {
			return false;
		}
		
		// FORM VALID? SUBMIT
		return true;
	})
	
	// NIEUWSBRIEF
	$('#nieuwsbrief [name=submit]').click(function() {
		var email = $('#nieuwsbrief [name=email]').val();
		
		// VALID EMAIL?
		if((email != 'e-mail@adres.nl') && (/^(("[\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.test(email))) {			
			$.ajax({
				url: '/',
				data: 'soort=2&nieuwsbrief=1&email=' + email,
				type: 'POST',
				cache: false,
				async: true,
				success: function(){
						$('#nieuwsbrief [name=email]').hide();
						$('#nieuwsbrief [name=submit]').hide();
						$('#nieuwsbrief').html('<div class="newsletter-bedankt"><img src="/images/newsletter-ok.gif" /></div>');
						$('#newsletteralert').html('Ontvang als eerste het laatste nieuws op het gebied van horen en gehoortesten.');
						window.location.href = '/nieuwsbrief-bedankt/';
				},
				error: function(){
						$('#newsletteralert').html('<img src="/images/delete.png" /> Er is een technische storing opgetreden, probeer het opnieuw of neem contact op.');
				}
			})
		} else {
			$('#newsletteralert').html('<img src="/images/delete.png" /> Ongeldige e-mail, probeer opnieuw.');
		}
	})
	
	$('#nieuwsbrief [name=email]').focus(function() {
		if($(this).val() == 'e-mail@adres.nl') {
			$(this).val('');
		}
	})
	
	$('#nieuwsbrief [name=email]').blur(function() {
		if($(this).val() == '') {
			$(this).val('e-mail@adres.nl');
		}
	})
})
