/* Created by jankoatwarpspeed.com */

(function($) {

var english = false;
var currenturl = $(location).attr('href');
if( currenturl.indexOf( "/en/" ) !== -1 )
{
	english = true;
}

function IsValidEmail(email)
{
var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
return filter.test(email);
}


if(english)
            {
                 //console.log("english");
				 var str_stap = "Step";
				 var str_volgende = "Next";
				 var str_vorige = "Previous";
				 var str_alertfields = "Please fill in all required fields.";
            }
			else {
				var str_stap = "Stap";
				 var str_volgende = "Volgende";
				 var str_vorige = "Vorige";
				 var str_alertfields = "Vul a.u.b. alle verplichte velden in.";
			}



    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: "" 
        }, options); 
        
        var element = this;

        var steps = $(element).find("fieldset");
        var count = steps.size();
        var submmitButtonName = "#" + options.submitButton;
        $(submmitButtonName).hide();

        // 2
        $(element).before("<ul id='steps'></ul>");

        steps.each(function(i) {
            $(this).wrap("<div id='step" + i + "'></div>");
            $(this).append("<p id='step" + i + "commands'></p>");

            // 2
            var name = $(this).find("legend").html();
            $("#steps").append("<li id='stepDesc" + i + "'>" + str_stap + " " + (i + 1) + "<span>" + name + "</span></li>");

            if (i == 0) {
                createNextButton(i);
                selectStep(i);
            }
            else if (i == count - 1) {
                $("#step" + i).hide();
                createPrevButton(i);
            }
            else {
                $("#step" + i).hide();
                createPrevButton(i);
                createNextButton(i);
            }
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Prev' class='prev'>< "+ str_vorige + "</a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i - 1)).show();
                $(submmitButtonName).hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>" + str_volgende + " ></a>");
			
			//x_formulieren
            $("#" + stepName + "Next").bind("click", function(e) {
			
			$(".xvalidationerror").removeClass("xvalidationerror");

			var validnumber = 0;
			var totalnumber = 0;

			//basic input fields
			$( "#" + stepName + " input.wpcf7-validates-as-required:visible, #" + stepName + " textarea.wpcf7-validates-as-required:visible, " ).each(function(i, obj) {
				totalnumber += 1;
				var thisvalue = $(this).val();
				if(thisvalue != ""){
					validnumber += 1;
				}
				else{
					$(this).addClass("xvalidationerror");
					////console.log($(this).attr("name"));
				}

			});		
			//e-mail input fields
			$( "#" + stepName + " input.wpcf7-validates-as-email:visible" ).each(function(i, obj) {
				totalnumber += 1;
				var thisvalue = $(this).val();
				if(thisvalue != "" && IsValidEmail(thisvalue)){
					validnumber += 1;
				}
				else{
					$(this).addClass("xvalidationerror");
					////console.log($(this).attr("name"));
				}

			});				
			//listed checkboxes 
			$( "#" + stepName + " span.wpcf7-validates-as-required:visible" ).has("span.wpcf7-list-item" ).each(function(i, obj) {
			//console.log("list of checkboxes: "+$(this).attr("id"));
				var howmany = 0;
				totalnumber += 1;
				
				$(this).children().each(function(i, obj) {
					$(this).children("input[type='checkbox']:checked" ).each(function(i, obj) {
					//console.log("checked: "+$(this).attr("name"));
					howmany += 1;
					});
				});
				
				if(howmany > 0){
					validnumber += 1; //only update by 1 because only one checkbox needs to be checked per list usually..
				}
				else{
					$(this).children("span" ).addClass("xvalidationerror");
					//console.log($(this).attr("id") + " geen keuze gemaakt");
				}
				
			});		
			//listed checkboxes xvalidateme
			$( "#" + stepName + " span.xvalidateme:visible" ).has("span.wpcf7-list-item" ).each(function(i, obj) {
			//console.log("list of checkboxes: "+$(this).attr("id"));
				var howmany = 0;
				totalnumber += 1;
				
				$(this).children().each(function(i, obj) {
					$(this).children("input[type='checkbox']:checked" ).each(function(i, obj) {
					//console.log("checked: "+$(this).attr("name"));
					howmany += 1;
					});
				});
				
				if(howmany > 0){
					validnumber += 1; //only update by 1 because only one checkbox needs to be checked per list usually..
				}
				else{
					$(this).children("span" ).addClass("xvalidationerror");
					//console.log($(this).attr("id") + " geen keuze gemaakt");
				}
				
			});				

			//acceptance checkboxes
			$( "#" + stepName + " .wpcf7-acceptance" ).each(function(i, obj) {
				totalnumber += 1;
			});			
			$( "#" + stepName + " .wpcf7-acceptance:checked" ).each(function(i, obj) {
				validnumber += 1;
			});
			$( "#" + stepName + " .wpcf7-acceptance:not(checked)" ).each(function(i, obj) {
				//console.log($(this).attr("name"));
				$(this).next("span.xacceptvoorwaarden").addClass("xvalidationerror");
			});
			//dropdown menus
			$( "#" + stepName + " select.wpcf7-validates-as-required:visible option:selected" ).each(function(i, obj) {
				totalnumber += 1;
				var thisvalue = $(this).val();
				if(thisvalue != "---"){
					validnumber += 1;
				}
								else{
					$(this).addClass("xvalidationerror");
					//console.log($(this).attr("name"));
				}

			});
			//dropdown menus inval oppas
			$( "#wanneernodig div[id^='datum']:visible option:selected" ).each(function(i, obj) {
				totalnumber += 1;
				var thisvalue = $(this).val();
				if(thisvalue != "---"){
					validnumber += 1;
				}
								else{
					$(this).addClass("xvalidationerror");
					//console.log($(this).attr("name"));
				}

			});
			if(totalnumber == validnumber)
			{
                $("#" + stepName).hide();
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
			}
			else {
				alert(str_alertfields);
			}
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }

    }
})(jQuery); 
