$(function(){
	
	//Aanmelden vervoerder
	$("#carrierSignup").hide();
	$("#signinDesc").hide();
	$('a.signin').click(function(){ 
		$.fancybox.close();
			$('#signinLink').hide();
			$('#carrierSignin').hide("slow");
			$("#carrierSignup").show("slow");
	});
    $("#carrierForm").submit(function(){
    	$('#carrierForm').validate();
		if ($("#carrierForm").valid() ) {
	    	dataString = $("#carrierForm").serialize();
	        $.ajax({
		        type: "POST",
		        url: "forms/carrierFormProcess.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        	if (data.error === true){
						$("#signinDesc").html(data.msg);
						setTimeout(function() {
							$("#signinDesc").show();
							$("#carrierSignup").slideUp('slow',function() {$("#carrierSignup").remove();});
							$("#carrierSignin").show("slow");
			
						}, 500);	

					} else {
						$('form').clearForm();
						$("#signinDesc").html(data.msg);
						setTimeout(function() {
							$("#signinDesc").show();
							$("#carrierSignup").slideUp('slow',function() {$("#carrierSignup").remove();});
							$("#carrierSignin").show("slow");
			
						}, 500);
			        }
			      
		        }
	        });
		}
        return false;            
    });
    
    //Aanpassen account vervoerder
	$("#accountEdited").hide();
     $("#carrierEditForm").submit(function(){
    	$('#carrierEditForm').validate();
		if ($("#carrierEditForm").valid() ) {
	    	dataString = $("#carrierEditForm").serialize();
	        $.ajax({
		        type: "POST",
		        url: "../forms/carrierEditFormProcess.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        		
					$('form').clearForm();
					$("#accountEdit").slideUp('slow',function() {$("#accountEdit").remove();});
					setTimeout(function() {
						$("#accountEdited").show("slow");	
						setTimeout(function(response) {
							if (data.response == 'logout') {
				      			window.location = data.url;
							}
						}, 2000);
					}, 500);
		        }
	        });
		}
        return false;            
    });
    
     // Aanmaken profiel vervoerder
	$("#profileAdded").hide();
     $("#profileForm").submit(function(){
    	$('#profileForm').validate();
		if ($("#profileForm").valid() ) {
	    	dataString = $("#profileForm").serialize();
	        $.ajax({
		        type: "POST",
		        url: "../forms/profileFormProcess.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        		
					$('form').clearForm();
					$("#profileAdd").slideUp('slow',function() {$("#profileAdd").remove();});
					setTimeout(function() {
						$("#profileAdded").show("slow");	
						setTimeout(function(response) {
							window.location.href = "http://" + window.location.host + window.location.pathname ;
						}, 2000);
					}, 500);
		        }
	        });
		} 
        return false;            
    });
         // Aanmaken profiel vervoerder
	$("#profileEdited").hide();
     $("#profileEditForm").submit(function(){
    	$('#profileEditForm').validate();
		if ($("#profileEditForm").valid() ) {
	    	dataString = $("#profileEditForm").serialize();
	        $.ajax({
		        type: "POST",
		        url: "../../forms/profileEditFormProcess.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        		
					$('form').clearForm();
					$("#profileEdit").slideUp('slow',function() {$("#profileEdit").remove();});
					setTimeout(function() {
						$("#profileEdited").show("slow");	
						setTimeout(function(response) {
							window.location.href = "http://" + window.location.host + window.location.pathname ;
						}, 2000);
					}, 500);
		        }
	        });
		} 
        return false;            
    });
    	
    //Offerte uitbrengen
	$("#tenderSet").hide();
     $("#tenderForm").submit(function(){

    	$('#tenderForm').validate();
		if ($("#tenderForm").valid() ) {
	    	dataString = $("#tenderForm").serialize();
	    	
	        $.ajax({
		        type: "POST",
		        url: "../../forms/tenderInsertForm.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
					$('form').clearForm();
					$("#tenderInsertForm").slideUp('slow',function() {$("#tenderInsertForm").remove();});
					setTimeout(function() {
						$("#tenderSet").show("slow");	
					}, 500);
		        }
	        });
		}
        return false;            
    });
    
    //Aanvraag afsluiten klant
	$("#closingSucces").hide();
     $("#closingForm").submit(function(){

    	$('#closingForm').validate();
		if ($("#closingForm").valid() ) {
	    	dataString = $("#closingForm").serialize();
	    	
	        $.ajax({
		        type: "POST",
		        url: "../../forms/closingForm.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        	
					
					$('form').clearForm();
					$("#closingForm").slideUp('slow',function() {$("#closingForm").remove();});
					setTimeout(function() {
						$("#closingSucces").show("slow");	
					}, 500);
		        }
	        });
		}
        return false;            
    }); 
    
    //Aanvraag afsluiten vervoerder
	$("#closingCarrierSucces").hide();
     $("#closingCarrierForm").submit(function(){
		var $that = $(this);
    	$('#closingCarrierForm').validate();
		if ($("#closingCarrierForm").valid() ) {
			
			var submitButton = $that.find("input[type='submit']");
         	btnText = $(submitButton).attr("value");
         	$(submitButton).attr("value", "Verzenden…");
        	 $(submitButton).attr("disabled", "true");
			
	    	dataString = $("#closingCarrierForm").serialize();
	    	
	        $.ajax({
		        type: "POST",
		        url: "../../forms/closingCarrierForm.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        	setTimeout(function() {
						$('form').clearForm();
						$("#closingCarrierForm").slideUp('slow',function() {$("#closingCarrierForm").remove();});
						setTimeout(function() {
							$("#closingCarrierSucces").show("slow");	
						}, 500);
					}, 500);
		        }
	        });
		}
        return false;            
    }); 
    
     //Contact form
	$("#contactSend").hide();
     $("#contactForm").submit(function(){

    	$('#contactForm').validate();
		if ($("#contactForm").valid() ) {
	    	dataString = $("#contactForm").serialize();
	    	
	        $.ajax({
		        type: "POST",
		        url: "../../forms/contactProcess.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        	$('form').clearForm();
					$("#contactForm").slideUp('slow');
					setTimeout(function() {
						$("#contactSend").show("slow");
						setTimeout(function() {
							$("#contactForm").show("slow");
							$("#contactSend").hide("slow");
						}, 2000);
					}, 500);
		        }
	        });
		}
        return false;            
    });
    
    //Contact form Languages
	$("#contactSendLanguage").hide();
     $("#contactLanguageForm").submit(function(){

    	$('#contactLanguageForm').validate();
		if ($("#contactLanguageForm").valid() ) {
	    	dataString = $("#contactLanguageForm").serialize();
	    	
	        $.ajax({
		        type: "POST",
		        url: "../../forms/contactProcessLanguage.php",
		        data: dataString,
		        dataType: "json",
		        success: function(data) {
		        	$('form').clearForm();
					$("#contactLanguageForm").slideUp('slow');
					setTimeout(function() {
						$("#contactSendLanguage").show("slow");
						setTimeout(function() {
							$("#contactLanguageForm").show("slow");
							$("#contactSendLanguage").hide("slow");
						}, 2000);
					}, 500);
		        }
	        });
		}
        return false;            
    });
     
    
    // Callback form
    $('#callSuccess').hide();
    $("#callBackForm").submit(function(){
    	
    	 var callName = $('input#callName').val();
        if(callName == ''){
            $('input.callName').addClass('error');
            $('input#callName').focus();
            return false;
        }
		var callPhone = $('input#callPhone').val();
        if(callPhone == ''){
            $('input.callPhone').addClass('error');
            $('input#callPhone').focus();
            return false;
        }


		dataString = $("#callBackForm").serialize();

        $.ajax({
        type: "POST",
        url: "forms/callFormProcess.php",
        data: dataString,
        dataType: "json",
        success: function(data) {
            $('input.callName').removeClass('error');
            $('input.callPhone').removeClass('error');
			$('form').clearForm();
			setTimeout(function() {
				$('#callSuccess').show();
			}, 500);
			
        }

        });

        return false;            

    });
});

$.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return $(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};
