modals.blade.php 8.71 KB
<script>
    var code;
    var code2;

    function createCaptcha() {
        //clear the contents of captcha div first
        var captha1 = $('#captcha1').html();

        document.getElementById('captcha1').innerHTML = "";
        document.getElementById('captcha2').innerHTML = "";
        var charsArray =
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@!#$%^&*";
        var lengthOtp = 6;
        var captcha = [];
        for (var i = 0; i < lengthOtp; i++) {
            //below code will not allow Repetition of Characters
            var index = Math.floor(Math.random() * charsArray.length + 1); //get the next character from the array
            if (captcha.indexOf(charsArray[index]) == -1)
                captcha.push(charsArray[index]);
            else i--;
        }
        var canv = document.createElement("canvas");
        canv.id = "captcha";
        canv.width = 100;
        canv.height = 50;
        var ctx = canv.getContext("2d");
        ctx.font = "25px Georgia";
        ctx.strokeText(captcha.join(""), 0, 30);
        //storing captcha so that can validate you can save it somewhere else according to your specific requirements
        code = captcha.join("");

        document.getElementById("captcha1").appendChild(canv);
        // adds the canvas to the body element

        createCaptcha2()
    }

    function createCaptcha2() {
        //clear the contents of captcha div first
        document.getElementById('captcha2').innerHTML = "";

        var captha2 = $('#captcha2').html();

        var charsArray =
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@!#$%^&*";
        var lengthOtp = 6;
        var captcha = [];
        for (var i = 0; i < lengthOtp; i++) {
            //below code will not allow Repetition of Characters
            var index = Math.floor(Math.random() * charsArray.length + 1); //get the next character from the array
            if (captcha.indexOf(charsArray[index]) == -1)
                captcha.push(charsArray[index]);
            else i--;
        }
        var canv = document.createElement("canvas");
        canv.id = "captcha";
        canv.width = 100;
        canv.height = 50;
        var ctx = canv.getContext("2d");
        ctx.font = "25px Georgia";
        ctx.strokeText(captcha.join(""), 0, 30);
        //storing captcha so that can validate you can save it somewhere else according to your specific requirements
        code2 = captcha.join("");

        document.getElementById("captcha2").appendChild(canv);
        // adds the canvas to the body element
    }

    function validateCaptcha() {

        if (document.getElementById("cpatchaTextBox").value == code) {
            console.log('Валидная капча 1!');
        }else{
            alert("Неверная капча! Повторите вновь");
            createCaptcha();
        }
    }

    function validateCaptcha2() {
        if (document.getElementById("cpatchaTextBox2").value == code2) {
            console.log('Валидная капча 2!');
        }else{
            alert("Неверная капча! Повторите вновь");
            createCaptcha();
        }
    }

    function successfulRegistrationWorker() {
        $.fancybox.close();

        $('#email_worker').val('');
        $('#password_worker').val('');
        $('#password_confirmation_worker').val('');
        $('#politik').prop('checked', false);
        $('#telephone_worker').val('');
        $('#job_titles_worker').val('').trigger('change');
        $('#surname_worker').val('');
        $('#name_man_worker').val('');
        $('#surname2_worker').val('');
        $('#cpatchaTextBox1').val('');

        $('#block-info').hide();
        $('#messages_error_reg').html('');

        $.fancybox.open({
            src: '#successful-registration-worker',
            type: 'inline',
            opts: {touch: false}
        });
    }

    function successfulRegistrationEmployer() {
        $.fancybox.close();

        $('#email_employer').val('');
        $('#password_employer').val('');
        $('#password_confirmation_employer').val('');
        $('#politik').prop('checked', false);
        $('#telephone_employer').val('');
        $('#surname_employer').val('');
        $('#name_man_employer').val('');
        $('#surname2_employer').val('');
        $('#company_employer').val('');
        $('#cpatchaTextBox1').val('');

        $('#block-info').hide();
        $('#messages_error_reg').html('');

        $.fancybox.open({
            src: '#successful-registration-employer',
            type: 'inline',
            opts: {touch: false}
        });
    }

    $(document).on('click', '#button_send', function() {
        var field_login = $('#email_input');
        var field_pwd = $('#password');
        var login_val = field_login.val();
        var pwd_val = field_pwd.val();

        console.log('login: '+login_val+' password: '+pwd_val);

        $.ajax({
            type: "GET",
            url: "{{ route('login') }}",
            data: "email="+login_val+"&password="+pwd_val,
            success: function (data) {
                console.log('Вход в систему');
                console.log(data);
                let d = JSON.parse(data);
                if(typeof d['REDIRECT'] !== "undefined") {
                    location.href = d['REDIRECT'];
                    console.log(d['REDIRECT']);
                }
                if (typeof d['ERROR'] !== "undefined") {
                    console.log(d['ERROR']);
                    $('#message_error').html(d['ERROR']);
                }
                //console.log(d['REDIRECT']);
                //$('#block_1').html(data);
            },
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            error: function (data) {
                data = JSON.stringify(data);
                console.log('Error: ' + data);
            }
        });
    });

    $('#form1').on('submit', function (e) {
        e.preventDefault();

        if (document.getElementById("cpatchaTextBox1").value == code) {
            console.log('Валидная капча');
        } else {
            $('#captcha_error').text('Капча неверная!');
            return false;
        }

        $('.error-message').text('');

        let formData = $(this).serialize();

        $.ajax({
            url: $(this).attr('action'),
            method: $(this).attr('method'),
            data: formData,
            success: function (response) {
                if (response.REDIRECT) {
                    successfulRegistrationWorker()
                }
            },
            error: function (jqXHR) {
                if (jqXHR.status === 422) {
                    let errors = jqXHR.responseJSON.ERRORS;

                    for (let field in errors) {
                        if (errors.hasOwnProperty(field)) {
                            $('#' + field + '_error').text(errors[field][0]);
                        }
                    }
                } else {
                    alert('Произошла ошибка при регистрации. Попробуйте снова.');
                }
            }
        });
    });

    $(document).on('click', '.open-login-modal', function(e) {
        e.preventDefault();
        $.fancybox.close();
        $.fancybox.open({
            src: '#sign',
            type: 'inline',
            opts: {touch: false}
        });
    });

    $('#form2').on('submit', function (e) {
        e.preventDefault();

        if (document.getElementById("cpatchaTextBox2").value == code2) {
            console.log('Валидная капча');
        } else {
            $('#captcha_employer_error').text('Капча неверная!');
            return false;
        }

        $('.employer_error-message').text('');

        let formData = $(this).serialize();

        $.ajax({
            url: $(this).attr('action'),
            method: $(this).attr('method'),
            data: formData,
            success: function (response) {
                if (response.REDIRECT) {
                    successfulRegistrationEmployer();
                }
            },
            error: function (jqXHR) {
                if (jqXHR.status === 422) {
                    let errors = jqXHR.responseJSON.ERRORS;

                    for (let field in errors) {
                        if (errors.hasOwnProperty(field)) {
                            $('#' + field + '_employer_error').text(errors[field][0]);
                        }
                    }
                } else {
                    alert('Произошла ошибка при регистрации. Попробуйте снова.');
                }
            }
        });
    });
</script>