/* Created by jankoatwarpspeed.com */

(function($) {
    $.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 + "'><a href='#'>Step " + (i + 1) + "<span>" + name + "</span></a></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='#"+i+"' id='" + stepName + "Prev' class='prev'><img src='css/images/skin1/bt_retrocederEncomenda_en.png' /></a>");

            $("#" + stepName + "Prev").bind("click", function(e) {
				e.preventDefault();

                $("#" + stepName).hide();
                $("#step" + (i - 1)).fadeIn();
                $(submmitButtonName).hide();
                selectStep(i - 1);
            });
        }

        function createNextButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#"+i+"' id='" + stepName + "Next' class='next'><img src='css/images/skin1/bt_continuarEncomenda_en.png' /></a>");

            $("#" + stepName + "Next").bind("click", function(e) {
				e.preventDefault();
				
				validate_up(i, stepName);

            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }
		
        function validate_up(i, stepName) {
			if (i == 0) {
				if (($("#classic").val() == 0) && ($("#selection").val() == 0) && ($("#intense").val() == 0) && ($("#descafeinado").val() == 0) && ($("#maquina_inter1").val() == 0)) {
				//if (($("#classic").val() == 0) && ($("#selection").val() == 0) && ($("#intense").val() == 0) && ($("#descafeinado").val() == 0) && ($("#maquina_mini").val() == 0)){
					alert("Encomenda vazia. Por favor, adicione produtos.");
				}
				else {
					$("#" + stepName).hide();
					$("#step" + (i + 1)).fadeIn();
					if (i + 2 == count)
						$(submmitButtonName).show();
					
					selectStep(i + 1);
				}
			}
			if (i == 1) {
				if (($("#num_serie").val() == 0)) {
					alert("Please, fill in the field Serial Number of your machine");
					$("#num_serie").focus();
				}
				else if (($("#nome").val() == 0)) {
					alert("Please, fill in the field Name");
					$("#nome").focus();
				}
				else if (($("#morada").val() == 0)) {
					alert("Please, fill in the field Address");
					$("#morada").focus();
				}
				else if (($("#cod_postal").val() == 0)) {
					alert("Please, fill in the field Postal Code");
					$("#cod_postal").focus();
				}
				else if (($("#telefone").val() == 0)) {
					alert("Please, fill in the field Telephone");
					$("#telefone").focus();
				}
				else {
				
					if($("#nome_facturacao").val() == 0) {
						$("#nome_facturacao_txt input").val($("#nome").val());
					}
					if($("#morada_facturacao").val() == 0) {
						$("#morada_facturacao_txt input").val($("#morada").val());
					}
					if($("#cod_postal_facturacao").val() == 0) {
						$("#cod_postal_facturacao_txt input").val($("#cod_postal").val());
					}
					if($("#pais_facturacao").val() == 0) {
						$("#pais_facturacao_txt input").val($("#pais").val());
					}

					$("#" + stepName).hide();
					$("#step" + (i + 1)).fadeIn();
					if (i + 2 == count)
						$(submmitButtonName).show();
					
					selectStep(i + 1);
				}
			}

        }

    }
})(jQuery); 
