function step(onstep, next, steps, dir) {
    $("#step" + onstep).toggle('fade', function () {
        if (next === steps || (dir === 'prev' && onstep === steps)) {
            $("#next").toggle();
            $("#complete").toggle();
        }
        if (next === 1 || (dir === 'next' && next === 2)) {
            $("#prev").toggle();
        }
        $("#step" + next).toggle('slide');
    });
    $("#form-container").data('step', next);
    $("input[name=appstep]").val(next);
}

function trackSubmissions(category, name, value) {
    try {
        _gaq.push(['_trackEvent', category, name, value, 1]);
        //alert(name);
    } catch (err) {
        // alert(err);
    }
}

$(document).ready(function () {

    $(".tip").tooltip();

    $(".walkform").click(function () {
        var dir = $(this).attr("id");
        var onstep = $("#form-container").data('step');
        var next = (dir === 'next') ? onstep + 1 : onstep - 1;
        var steps = $(".steps").length;
        if ($("#step" + next).hasClass("skip")) {
            next = (dir === 'next') ? next + 1 : next - 1;
        }
        if (dir === 'next') {
            $('form[name=submitapp]').ajaxForm({
                dataType: 'json',
                success: function (res) {
                    if (res.type === 'error') {
                        $("#alertbox").html('<div class="alert alert-' + res.type + '"><button class="close" data-dismiss="alert" type="button">×</button>' + res.message + '</div>');
                    } else if (res.load) {
                        $('#form-container').load('process/load/' + res.load);
                    } else if (res.direct) {
                        //alert(res.direct + '?confirm=' + res.confirmation);
                        window.location = res.direct + '?confirm=' + res.confirmation + '&site=' + res.sitetoken;
                    } else {
                        step(onstep, next, steps, dir);
                        $("#alertbox").html("");
                    }
                }
            });
            $('form[name=submitapp]').submit();
            return false;
        } else {
            step(onstep, next, steps, dir);
        }
    });

    $('#complete').click(function () {
        $('form[name=submitapp]').ajaxForm({
            dataType: 'json',
            success: function (res) {
                if (res.type === 'error') {
                    $("#alertbox").html('<div class="alert alert-' + res.type + '"><button class="close" data-dismiss="alert" type="button">×</button>' + res.message + '</div>');
                } else if (res.load) {
                    $('#content').load('process/load/' + res.load);
                    var category = 'Finance';
                    var value = 'Test';
                    var name = res.site;
                    trackSubmissions('Finance', res.site, 'Test');
                } else if (res.direct) {
                    //alert(res.direct + '?confirm=' + res.confirmation);
                    window.location = res.direct + '?confirm=' + res.confirmation + '&site=' + res.sitetoken;
                    var category = 'Finance';
                    var value = 'Test';
                    var name = res.site;
                    trackSubmissions('Finance', res.site, 'Test');
                }
                if (res.directtop) {
                    window.top.location.href = res.directtop;
                }
            }
        });
        $('form[name=submitapp]').submit();
        return false;
    });

    $("input[name=app_type]").change(function () {
        if ($(this).val() === 'joint') {
            $(".coapp").removeClass('skip');
            $(".coappsign").removeClass('hide');
        } else {
            $(".coapp").addClass("skip");
            $(".coappsign").addClass("hide");
        }
    });


});