﻿var wizard;
jQuery().ready(function() {
    wizard = $("div#wizard");
    var index = $.cookie("accordion");
    var active;
    if ((index !== undefined) && (index != null)) {
        active = wizard.find("div#title:eq(" + index + ")");
        $.cookie('accordion', null);
    }
    else {
        active = '.selected';
    }
    wizard = $("#wizard").accordion({
        header: 'div#title',
        active: active,
        autoheight: false
    });

    var wizardButtons = $([]);
    $("div#title", wizard).each(function(index) {
        wizardButtons = wizardButtons.add($(this)
	            .next()
	            .children("div").children("a")
	            .filter(".pijlGeel")
	            .click(function() {
	                if (Page_ClientValidate("Step" + (index + 1))) {
	                    wizard.accordion("activate", index + ($(this).is(".pijlGeel") ? 1 : -1))
	                    $.cookie("accordion", index + 1);
	                }
	                else {
	                    return false;
	                }
	            }));
    });

    $("div#title", wizard).each(function(index) {
        $(this).bind("click", function() {
            return ValidateSteps(index);
        });
    });
});

function ValidateSteps(index) {
    var active = 0;
    $("div#title", wizard).each(function(index) {
        if ($(this).attr('class') == 'Tab_Form selected') {
            active = index;
        }
    });

    if (active > index)
        active = index;
        
    var retvalue = true;
    var firstpanel;

    for (var i = active;i<=index;i=i+1) {
        if (Page_ClientValidate("Step" + (i)) == false) {
            if (retvalue)
                firstpanel = i;
            retvalue = false;
        }
    }

    if (retvalue == true) {
        $.cookie("accordion", active + 1);
    }
    else {
        Page_ClientValidate("Step" + (firstpanel))
        return false;
    }
}
