$(document).ready(function () {


    var ok = true;

    function next() {
        ok = false;
        $('#wizard').bootstrapWizard('next');
    }

    $(".iCheck").iCheck({
        checkboxClass: 'icheckbox_square-grey',
        radioClass: 'iradio_square-grey'
    });


    //lang
    $('body').on('click', '[lang]', function () {
        $.cookie('lang', $(this).attr('lang'), {
            expires: 5
        });
        location.reload();
    });
    $('.pref').click(function (e) {
        e.preventDefault();
        $.get("/cabinet/prefix", function (data) {
            $('#prefix').val(data);
        });
    });

    //capcha
    function RECaptcha() {
        $('input[name="captcha"]').val('');
        $('#capchaimg').attr('src', '/captcha');

    }

    $('#capchaimg').click(function () {
        RECaptcha();
    });
    $(".chzn-select").each(function () {
        $(this).select2($(this).data());
    });


    $('#save_text').click(function () {
        window.location.href = "/library/savetxt.php?login=" + $('#login').val() + '&pass=' + $('#password').val() + '&email=' + $('#email').val() + '&server=' + $("#server option:selected").text()+ '&pref=' + $('#prefix').val();
    });


    $("#refCode").mask("9999@9999");

    function register() {

        var prefix = $('#prefix');
        var server = $('#server');
        var login = $('#login');
        var mail = $('#email');
        var password2 = $('#password2');
        var password = $('#password');
        var refCode = $('#refCode');
        var checkbox = $('#rules');
        var capcha = $('#capcha');

        if(prefix.val() !== undefined) {
            prefix2 = '&pref=' + $('#prefix').val();
            prefix = '&prefix=' + prefix.val();
        }else{
            prefix = '';
            prefix2 = '';
        }

        if (server.val() != '' && login.val() != '' && password.val() != '' && capcha.val() != '' && mail.val() != '' && checkbox.val() != '' && password2.val() != '') { // Check the login and password and capcha values is not empty and make the ajax request
            var UrlToPass = 'reg=1&server=' + server.val() + prefix + '&login=' + login.val() + '&password=' + password.val() + '&password2=' + password.val() + '&capcha=' + capcha.val() + '&mail=' + mail.val() + '&ref=' + refCode.val();
            $.ajax({
                type: 'POST',
                data: UrlToPass,
                url: '/',
                beforeSend: function () {
                    $('#next').attr("disabled", "disabled");
                    $('#next').html('<span class="small-circle"><i class="fa fa-spinner fa-spin"></i></span><small> Loading</small>');

                },
                success: function (data) {
                    $('#next').removeAttr("disabled");
                    $('#next').html('Вперед <i class="fa fa-caret-right"></i>');
                    event = JSON.parse(data);
                    if (event.complet) {
                        var complet = event.complet;
                        var title = event.title;
                        var button = event.button;
                        $('.finish a').html(button);
                        $('.finish_text h2').html(title);
                        $('.finish_text p').html(complet);
                        next();
                        window.location.href = "/library/savetxt.php?login=" + $('#login').val() + '&pass=' + $('#password').val() + '&email=' + $('#email').val() + '&server=' + $("#server option:selected").text()+ prefix2;

                    } else if (event.referror) {
                        var referror = event.referror;
                        $.notij(referror, {'type': 'error'});
                        RECaptcha();
                        $('#refCode').removeAttr('disabled');
                        $('#refCode').val('');
                    }
                    else {
                        var error = event.error;
                        RECaptcha();
                        $.notij(error, {'type': 'error'});
                    }
                }
            });
        }

    }


    $('#validate-form').validate({
        errorElement: 'span',
        errorClass: 'help-block',
        focusInvalid: true,
        rules: {

            server: {
                required: true
            },
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 5
            },
            password2: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            login: {
                required: true,
                minlength: 5
            },
            capcha: {
                required: true
            },
            refCode: {
                required: false
            },
            prefix: {
                required: true
            },
            rules: 'required'
        },

        messages: {
            login: {
                required: "Please specify a login.",
                minlength: "Please specify a secure login."
            },
            prefix: {
                required: "Префикс не может быть пустым нажмите на него чтобы сгенерировать новый!."
            },
            email: {
                required: "Please provide a valid email.",
                email: "Please provide a valid email."
            },
            password: {
                required: "Please specify a password.",
                minlength: "Please specify a secure password."
            },
            capcha: {
                required: "Введите Capcha"
            },
            refCode: {
                required: "Реферальный код."
            },
            server: {
                required: "Select server."
            },
            rules: ""

        },


        highlight: function (e) {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },

        success: function (e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
            $(e).remove();
            $(this).submit();
        },

        submitHandler: function (form) {
        },
        invalidHandler: function (form) {
        }
    });
    $("#wizard").bootstrapWizard({
        'nextSelector': '.right',

        onTabClick: function (tab, navigation, index) {
            return false;

        }, onNext: function (tab, navigation, index) {

            if (ok) {
                var $valid = $("#validate-form").valid();
                if (!$valid) {
                    $valid.focusInvalid();
                    return false;
                } else {
                    register()
                    return false;

                }
            }


        }, onTabShow: function (tab, navigation, index) {

            var $total = navigation.find('li').length;
            var $current = index + 1;
            var $percent = ($current / $total) * 100;
            var $wizard = $("#wizard");
            $wizard.find('.progress-bar').css({width: $percent + '%'});

            if ($current >= $total) {

                $wizard.find('.pager .next').hide();
                $wizard.find('.pager .finish').show();
                $wizard.find('.pager .finish').removeClass('disabled');
            } else {
                $wizard.find('.pager .next').show();
                $wizard.find('.pager .finish').hide();
            }
        }
    });

});