$(document).ready(function(){
	
	$('#info_form').submit(function() {
		return false;
	});
	
	$('.more-info-popup').hide();
	
	$('#info-aanvraag #ctrlchk label')
	
	$('#info-aanvraag #ctrlchk label').each(function(){
	
		$thisPop = $('#more-info-popup'+$(this).attr('id'));
		$currentid = $('#'+$(this).attr('id'))
		
		$thisPop.offset($(this).offset())
		$thisPop.css({		
			top: $(this).offset().top + 22
		})
		
		if($thisPop.length >= 1){
		
			$(this).append('<span class="popup"></span>')
			$(this).hover(function(){
				$(this).addClass('pop');
				$thisPop = $('#more-info-popup'+$(this).attr('id'));			
				$thisPop.show();
			
			},function(){
			
				$(this).removeClass('pop');	
				$thisPop = $('#more-info-popup'+$(this).attr('id'));	
				$thisPop.fadeOut();
			
			})
			
		
		}
		
	})
	
	$('#info-aanvraag #ctrlchk  label input:checked').parents('label').addClass('active');
	
	$('#info-aanvraag #ctrlchk  input').change(function(){
		$(this).parents('label').addClass('active');
	});
	$('#info-aanvraag form').change(function(){
		$('#info-aanvraag #ctrlchk label input:not(:checked)').parents('label').removeClass('active');
	});
	
	$('#reset').click(function(){
		$('#info-aanvraag label input').parents('label').removeClass('active');
	});
	
	$('#verder').click(function(){
		if ($('input.adverteerder').is(":checked")){
			$('#error-output').slideUp();
			$('#stap1').hide();
			$('#stap2').show();
			$("html, body").animate({ scrollTop: 0 });
		}else{
			error_txt = "U dient minimaal 1 item te selecteren";
			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;
	});
	
	$('#terug').click(function(){
		$('#stap2').hide();
		$('#stap1').show();
		return false;
	});

$('.checkfield').change(function(){
		$(this).removeClass("invalid");
	});
      
    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')      		
      		}
      	})
      	
      	
      	
      	$('.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 == '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('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 (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:        '#info-aanvraag',
   	   		beforeSubmit:  formCheck,
   	   		success: function(){
   	   			$('html, body').animate({scrollTop:0}, 'fast');
   	   		}
   	   		};
    	   		
   	   		$('#info_form').submit(function() { 
   	   			$(this).ajaxSubmit(options); 
   	   			return false; 
   	   		}); 
   	   	}
   	   	
   	   	$.fn.activate_form();
			
})
