$(document).ready(function($) {

  $('.error').hide();
  
  $(".form").submit(function() {
  
 var formid = $(this).closest('.form').attr('id');
  
	var dString = '';
 var form = new Array()
 var error = false;
  $(this).closest('.form').find("input:text").each( function() {
 form[$(this).attr('id')] = true;

	if($(this).attr('class') == 'required'){
	
		if ($(this).val() == "") {
		         
    		$("#"+$(this).attr('id')+'_error').show();
    		$("#"+$(this).attr('id')).focus();
    		 form[$(this).attr('id')] = false;
     		    		
	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		form[$(this).attr('id')] = true;
    		$("#"+$(this).attr('id')+'_error').hide();

    		
    		
    	
    	}
	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		form[$(this).attr('id')] = true;

    		
    	
    	}

		
});






  $(this).closest('.form').find("input:password").each( function() {
 form[$(this).attr('id')] = true;

	if($(this).attr('class') == 'required'){
	
		if ($(this).val() == "") {
		         
    		$("#"+$(this).attr('id')+'_error').show();
    		$("#"+$(this).attr('id')).focus();
    		 form[$(this).attr('id')] = false;
     		    		
	} else {
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		form[$(this).attr('id')] = true;
    		$("#"+$(this).attr('id')+'_error').hide();

    		
    		
    	
    	}
	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		form[$(this).attr('id')] = true;

    		
    	
    	}

		
});








  $(this).closest('.form').find("input[type=radio]:checked").each( function() {

 form[$(this).attr('id')] = true;
 
 //alert($(this).val());

	if($(this).attr('class') == 'required'){
	
		if ($(this).val() == "") {
      
      		//$("#evenement_naam_error").show();
         
    		$("#"+$(this).attr('id')+'_error').show();
    		 form[$(this).attr('id')] = false;
     		    	
    	}	else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		 form[$(this).attr('id')] = true;

    	} 
	
	}	else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		 form[$(this).attr('id')] = true;

    	}
		
});


  $(this).closest('.form').find("select").each( function() {

 
 form[$(this).attr('id')] = true;

	if($(this).attr('class') == 'required'){
	
		if ($(this).val() == "") {
			
			
      
      		//$("#evenement_naam_error").show();
         
    		$("#"+$(this).attr('id')+'_error').show();
    		 form[$(this).attr('id')] = false;

     		    	
    	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		 form[$(this).attr('id')] = true;

    	
    	} 	
	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		 form[$(this).attr('id')] = true;

    	
    	}

	

		
});

  $(this).closest('.form').find("textarea").each( function() {
 
 form[$(this).attr('id')] = true;

	if($(this).attr('class') == 'required'){
	
		if ($(this).val() == "") {
			
			
      
      		//$("#evenement_naam_error").show();
         
    		$("#"+$(this).attr('id')+'_error').show();
    		 form[$(this).attr('id')] = false;

     		    	
    	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		 form[$(this).attr('id')] = true;

    	
    	} 	
	} else {
    	
    		var name = $(this).attr('name');
    		dString += name+ '=' + $(this).val() + '&';
    		form[$(this).attr('id')] = true;

    	
    	}

	

		
});
		for(var i in form) {	

	        if( form[i] == false)
	    	    { 
	    	   		var error = true;
	    	 	} 
	   }
	    	  	
 if(error == false) {

		$.ajax({
      type: "POST",
      url: "submit.php",
      data: dString,
      cache: false,
      success: function(data) {
     
     
		  if (data == "true" || data == "truetrue") {
        
        	$("#"+formid).slideUp();
       		$('#confirm_box').show();
		
		} else if(data == "mailerror"){
		  	$("#emailadres_error").show();
		  	$("#emailadres_error").html(' Emailadres is ongeldig');
    	  	form["#emailadres"] = false;
    	  	$("#emailadres").focus();
		  	return false;
		} else if(data == "loginerror"){
		  	$("#loginerror").show();
		  	$("#loginerror").html('Gebruikersnaam of wachtwoord onjuist');
    	  	
		  	return false;
		} else if(data == "passwordshort"){
		  	$("#wachtwoord_error").show();
		  	$("#wachtwoord_error").html('Wachtwoord te kort');
		  	$("#wachtwoord_nogmaals_error").show();
		  	$("#wachtwoord_nogmaals_error").html('Wachtwoord te kort');
		  	$("#wachtwoord").focus();
    	  	
		  	return false;
		} else if(data == "password"){
		  	$("#wachtwoord_error").show();
		  	$("#wachtwoord_error").html('Wachtwoorden niet gelijk');
		  	$("#wachtwoord_nogmaals_error").show();
		  	$("#wachtwoord_nogmaals_error").html('Wachtwoorden niet gelijk');
    	  	
		  	return false;
		} else if(data == "username"){
		  	$("#gebruikersnaam_error").show();
		  	$("#gebruikersnaam").focus();
		  	$("#gebruikersnaam_error").html('Gebruikersnaam is al in gebruik');
    	  	
		  	return false;
		} else if(data == "passwordoud"){
		  	$("#oud_wachtwoord_error").show();
		  	$("#oud_wachtwoord_error").focus();
		  	$("#oud_wachtwoord_error").html('Verkeerd wachtwoord');
    	  	
		  	return false;
		} else if(data == "loginok"){
		  	location.reload();
		  	
		}
		  
		  return false;
      }
     });
     } else {
     return false;
     }
     return false;
	});
});

