/*
Created 09/27/09										
Questions/Comments: jorenrapini@gmail.com						
COPYRIGHT NOTICE		
Copyright 2009 Joren Rapini
*/

	

$(document).ready(function(){
	// Place ID's of all required fields here.
	required = ["voornaam", "achternaam", "adres","postcode_a","postcode_b","woonplaats","telefoon","email"];
	// If using an ID other than #email or #error then replace it here
	email = $("#email");
	errornotice = $("#error");
	// The text to show up within a field when it is incorrect
	emptyerror = "Vereist.";
	emailerror = "Voer een geldig e-mail adres in.";
	
	$("#inschrijven_form select.birthday_select").change(function(){
		var birthday = $("#inschrijven_form select#birthday").val();
		var birthmonth = $("#inschrijven_form select#birthmonth").val();
		var birthyear = $("#inschrijven_form select#birthyear").val();
		
		if( birthday != '' && birthmonth != '' && birthyear != ''){
		
			$("#geboortedatum").val( birthday + '-' + birthmonth + '-' + birthyear);
			
			
		}
		
		
	});
	
	
	$("#inschrijven_form li#postcode input").change(function(){
		var postcode_a = $("input#postcode_a").val();
		var postcode_b = $("input#postcode_b").val();
		
		if( postcode_a != '' && postcode_b != '' ){
			$("input#postcode_input").val( postcode_a +' '+ postcode_b);
		}
		
	});
	
	
	$("#inschrijven_form").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}
		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}
		
		
		// validate birthdate
		var birthdate_val = $("#geboortedatum").val();
		if( birthdate_val == ''){
			$('select.birthday_select').addClass("needsfilled");		
		}
		
		
		// validate agreement
		var agreement = $("#agree").is(':checked');
		
		if(agreement == false){
			alert('U dient akkoord te gaan met de leveringsvoorwaarde alvorens u verder kunt gaan met uw inschrijving');		
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
			return true;
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
});	
