$(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 $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')      		
      		}
      	})
      	
      	
      	
      	$('.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() == ''){
      				//$(this).addClass("invalid");
      				//set_error = 1;
      			}else{
      				//$(this).removeClass("invalid");
      			}
      		}
      		

      		
      		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 (postcode_error == 1){
	    	error_txt += "De postcode die u heeft ingevuld is niet correct.<br>";
	    	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 (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();
      		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();
 
   	
   	$('a.add_houder').click(function(){
   		content = $('.pashouder_1').html();
   		
   		$('#extra_pashouder').append(content);
   	});
   	
  
   	
   	
   	
 });
 
 


