

(function($) {
    $.fn.formToWizard = function(options) {
        options = $.extend({  
            submitButton: "",
            validationEnabled : true
        }, 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 + "'>Step " + (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);
            }
            
           // $("#steps").append("<li id='stepDesc" + i + "' Class='Stepsname'>Step " + (i + 1) + "<span>" + name + "</span></li>"); 
        });

        function createPrevButton(i) {
            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Prev' class='prev'>< Back</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;
            var disStepName = "Step " + (i+2);
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Goto " + disStepName + "></a>");

            $("#" + stepName + "Next").bind("click", function(e) {
            	if (options.validationEnabled) {
            	    var stepIsValid = true;
            	    if (i=="1") {
                    if (!$('#CompanyName').val()) {
                      alert("Error: Company name is required");
                      stepIsValid = false;
                    }
                  }
                  if (i=="3") {
                    if ($('#sysreq').attr('checked')==false) {
                      alert("Please read and verify that you meet or exceed the following system requirements:\n\t Pentium III 1GHz Processor\n\t 512MB of RAM\n\t Windows XP or newer Microsoft OS\n\t Full Duplex Sound Card with Headset\n\nOnce you have verified that your system meets or exceeds these requirements, please select the required check box to continue.");
                      stepIsValid = false;
                    }
                    else if ($('#bandreq').attr('checked')==false) {
                    	alert("Please read and verify that you meet or exceed the following bandwidth requirements:\n\t 68KB bandwidth per concurrent user\n\t Each Supervisor and Agent is on an operating network with Internet access\n\nOnce you have verified that your system meets or exceeds these requirements, please select the required check box to continue.");
                      stepIsValid = false;
                    }
                  }
            	    if (!stepIsValid) {
            	         return false; 
            	    }
            	} 
                $("#" + stepName).hide();
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
            });
        }

        function selectStep(i) {
            $("#steps li").removeClass("current");
            $("#stepDesc" + i).addClass("current");
        }

    }
})(jQuery); 