$(document).ready(function(){
	$('body').attr('id','p_form');
	$('.checkfield').change(function(){
		$(this).removeClass("invalid");
	});
     
      $('#formToggle').val('14550');
     
    formCheck = function (){
      	set_error = 0;
      	postcode_error = 0;
      	pasnr_error = 0;
      	klsnr_error = 0;
      	var error_txt = '';
      	var $checkboxparent;
  		$(".error_output").html("");
      	$('.invalid').removeClass('invalid')
      	
      	$('.fakelabel.radio-check .wrapper[id^=checkfield]').each(function(){
      		$checkboxparent = $(this);
	    	if($(this).find('input[type=checkbox]').length != 0){
	    		$checkboxparent.addClass('checkboxes');
	    	}
      	});
      	
      	$('.fakelabel.radio-check .wrapper.checkboxes[id^=checkfield]').each(function(){
      		if($(this).find('input:checked[type=checkbox]').length == 0){
      			$(this).addClass('invalid')      		
      		}
      	});
      	      		
		$('input[name^=sms_pas]').each(function(){
			var curnumber = $(this).parents('fieldset').attr('id').split("_")[1];
			if (curnumber == 1){
			curnumber = '';
			}
			if ($(this).is(":checked")){
				$('input[name=mob_pas' + curnumber + ']').addClass('checkedsms');
	      	}else{
	      		$('input[name=mob_pas' + curnumber + ']').removeClass('checkedsms');
	      		$('input[name=mob_pas' + curnumber + ']').removeClass('invalid');
	      		checked_sms_error = 0;
	      	}
      	});
   		
   		$('input[name^=nieuwsbrief_pas]').each(function(){
   			var curnumber = $(this).parents('fieldset').attr('id').split("_")[1];
   			if (curnumber == 1){
   			curnumber = '';
   			}
   			if ($(this).is(":checked")){
   				$('input[name=eadres_pas' + curnumber + ']').addClass('checkedmail');
   		  	}else{
   		  		$('input[name=eadres_pas' + curnumber + ']').removeClass('checkedmail');
   		  		$('input[name=eadres_pas' + curnumber + ']').removeClass('invalid');
   		  		checked_mail_error = 0;
   		  	}
   		});
   		
   		$('.checkedmail').each(function(){     			
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if (!filter.test($(this).val())){
				$(this).addClass("invalid");
				checked_mail_error = 1;
			}else{
				checked_mail_error = 0;
			}
   		});
   		
   		$('.checkedsms').each(function(){   			
			if ((isNaN($(this).val())) || ($(this).val().length != 10)){
				$(this).addClass("invalid");
				checked_sms_error = 1;	
			}else{
				checked_sms_error = 0;	
			}
   		});
   		
   		$('.nonereqemail').each(function(){  
   			if ($(this).val() != ''){
   				var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
   				if (!filter.test($(this).val())){
   					error_txt += "Het opgegeven mailadres is niet juist.<br>";
   					$(this).addClass("invalid");
   					set_error = 1;
   				}
   			}
   		});
   		
   		$('.nonereqtel').each(function(){
   			if ($(this).val() != ''){
   				if ((isNaN($(this).val())) || ($(this).val().length != 10)){
   					error_txt += "U heeft geen juist telefoonnummer opgegeven. Gebruik bij het ingeven van een telefoonnummer alleen cijfers.<br>";
   					$(this).addClass("invalid");
   					set_error = 1;	
   				}
   			}
   		});
   		   				
   		$('.nonereqfax').each(function(){ 
   			if ($(this).val() != ''){
   				if ((isNaN($(this).val())) || ($(this).val().length != 10)){
   					error_txt += "U heeft geen juist faxnummer opgegeven. Gebruik bij het ingeven van een faxnummer alleen cijfers.<br>";
   					$(this).addClass("invalid");
   					set_error = 1;	
   				}
   			}
   		});
   		
      	
      	$('.checkfield').each(function(){
      		
      		$(this).removeClass("invalid");
      		
      		
      		if (this.type == 'radio'){				
      			radio_name = $(this).attr('name');
      			if ($('input[name='+radio_name+']').is(":checked")){
	      			$(this).parent().parent('.wrapper').removeClass("invalid");
      			}else{
      				$(this).parent().parent('.wrapper').addClass("invalid");
      				set_error = 1;
      			}
      		}
      		
      		if (this.type == 'select-one'){
      			if ($(this).val() == ''){
      				if (!$(this).parent().hasClass('dorpdowninvalid')){
      					$(this).wrap('<div class="dorpdowninvalid" />');
      				}
      				set_error = 1;
      			}else{
      				if ($(this).parent().hasClass('dorpdowninvalid')){
      					$(this).unwrap('<div class="dorpdowninvalid" />');
		      		}
      			}
      		}
      		
      		if ((this.type == 'text') || (this.type == 'textarea')){
      			if ($(this).val() == ''){
      				$(this).addClass("invalid");
      				set_error = 1;
      			}else{
      				$(this).removeClass("invalid");
      			}
      		}
      		
      		
      		if ($(this).hasClass('zipnum')){ 		
      			if ((isNaN($(this).val())) || ($(this).val().length != 4)){
      				$(this).addClass("invalid");
      				postcode_error = 1;
      			}
      		}
      		if ($(this).hasClass('ziplet')){      				
  				if ((!isNaN($(this).val())) || ($(this).val().length != 2)){
  					$(this).addClass("invalid");
  					postcode_error = 1;
  				}
      		}

			if ($(this).hasClass('klnr1')){      				
				if ((isNaN($(this).val())) || ($(this).val().length != 2)){
					$(this).addClass("invalid");
					klsnr_error = 1;
				}
			}
			if ($(this).hasClass('klnr2')){      				
				if ((isNaN($(this).val())) || ($(this).val().length != 7)){
					$(this).addClass("invalid");
					klsnr_error = 1;	
				}
			}
      		
      		
      		
      		if ($(this).hasClass('pasnr1')){      				
      			if ((isNaN($(this).val())) || ($(this).val().length != 2)){
      				$(this).addClass("invalid");
      				pasnr_error = 1;
      			}
      		}
      		if ($(this).hasClass('pasnr2')){      				
      			if ((isNaN($(this).val())) || ($(this).val().length != 7)){
      				$(this).addClass("invalid");
      				pasnr_error = 1;
      			}
      		}
      		if ($(this).hasClass('pasnr3')){      				
      			if (isNaN($(this).val())){
      				$(this).addClass("invalid");
      				pasnr_error = 1;
      			}
      		}
      		
      		
      		if ($(this).hasClass('email')){  
      			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
      			if (!filter.test($(this).val())){
      				error_txt += "U heeft geen juist mailadres opgegeven.<br>";
      				$(this).addClass("invalid");
      				set_error = 1;
      			}
      		}
      		
      		
      		if ($(this).hasClass('tel')){      				
      			if ((isNaN($(this).val())) || ($(this).val().length != 10)){
      				error_txt += "U heeft geen juist telefoonnummer opgegeven. Gebruik bij het ingeven van een telefoonnummer alleen cijfers.<br>";
      				$(this).addClass("invalid");
      				set_error = 1;	
      			}
      		}
      		
      		
      	});
	    
	    if (pasnr_error == 1){
	    	error_txt += "U heeft geen juist pasnummer opgegeven.<br>";
	    	set_error = 1;	
	    }
	    if (klsnr_error == 1){
	    	error_txt += "U heeft geen juist klantnummer opgegeven.<br>";
	    	set_error = 1;
	    }
	    if (postcode_error == 1){
	    	error_txt += "De postcode die u heeft ingevuld is niet correct.<br>";
	    	set_error = 1;
	    }
	    if (checked_mail_error == 1){
	    	error_txt += "Bij het aanmelden voor de nieuwsbrief is een geldig mailadres vereist<br>";   	
	    	set_error = 1;
	    }
	    if (checked_sms_error == 1){
	    	error_txt += "Bij het aanmelden voor sms berichten is een geldig mobiel nummer vereist<br>";  	
	    	set_error = 1;
	    }  
      	if (set_error == 1){
      		error_txt += "Alle rood gemarkeerde velden zijn verplicht.<br>";
      		if($('#error-output').length < 1){
      			$('<div id="error-output"><p>'+error_txt+'</p></div>').prependTo('body');
      			var outputheight = 0;
      		}else{
      			$('#error-output').slideUp("slow",function(){
      				$('#error-output').html('<p>'+error_txt+'</p>')
      			});      			
      		}
      	    
      	    $('#error-output').slideDown();
      	    		
      		$.doTimeout(5000, function(){
      			$('#error-output').slideUp();
      		});
      				      		
      		return false;
      	}else{
      		$('#error-output').slideUp();
      		$('input[name=sendmail]').val('1');
      		return true;
      	}
   	}
   	
   	$.fn.activate_form = function(){
   		var options = { 
   		target:        '#content_holder',
   		beforeSubmit:  formCheck,
   		success: function(){
   			$('html, body').animate({scrollTop:0}, 'fast');
   		}
   		};

		var testoptions = { 
		target:        '#content_holder',
		success: function(){
			$('html, body').animate({scrollTop:0}, 'fast');
		}
		};

   		
   		$('#pasaanvraag').submit(function() { 
   			$(this).ajaxSubmit(options); 
   			return false; 
   		}); 
   	}
   	
   	$.fn.activate_form();
   	
   	var nr_forms = 1;
   	
	$('a.add-houder').click(function(){
		nr_forms++;	
		
		$('input[name=pashouders]').val(nr_forms);
		
		content = $('#pashouder_1').html();
		$('#extra_pashouder').append('<div class="hr"></div><fieldset id="pashouder_'+nr_forms+'">'+content+'</fieldset>');
		$('#pashouder_'+nr_forms+' input').each(function(){
			$(this).attr('name', $(this).attr('name')+nr_forms);
		});
		return false;
	});
  
   	
   	
   	
 });
 
 


