/* Minification failed. Returning unminified contents.
(13,34-35): run-time error JS1195: Expected expression: .
(13,48-49): run-time error JS1003: Expected ':': )
(14,27-28): run-time error JS1008: Expected '{': ;
(16,5-9): run-time error JS1009: Expected '}': else
(16,5-9): run-time error JS1006: Expected ')': else
(16,5-9): run-time error JS1034: Unmatched 'else'; no 'if' defined: else
(16,45-46): run-time error JS1195: Expected expression: .
(16,59-60): run-time error JS1003: Expected ':': )
(17,27-28): run-time error JS1008: Expected '{': ;
(19,5-9): run-time error JS1009: Expected '}': else
(19,5-9): run-time error JS1006: Expected ')': else
(19,5-9): run-time error JS1034: Unmatched 'else'; no 'if' defined: else
(14,9,15,6): run-time error JS1314: Implicit property name must be identifier: attachCodeSubmit();
    }
(17,9,18,6): run-time error JS1314: Implicit property name must be identifier: attachCodeSubmit();
    }
 */
$(document).ready(function () {

    // Remove saved data from sessionStorage on /partners page
    sessionStorage.removeItem("has-subcode-page");
    $('#identify-arrow').show();
    $('a[data-toggle="tab"]').on('shown.bs.tab',
        function (e) {
            var target = $(e.target).attr("href") + '-arrow';
            $('.arrow').hide();
            $(target).show();
        });

    if ($("#registration-popup")?.val() != null) {
        attachCodeSubmit();
    }
    else if ($("#minor-registration-popup")?.val() != null) {
        attachCodeSubmit();
    }
    else {
        attachPartnerSubmit("partner-form", "ajax-content");
        attachPartnerSubmit("partner-form2", "ajax-content2");
    }
    attachContactUsForm();

    $('.partners-submit').removeAttr('disabled');
    $('.partners-login').removeAttr('disabled');

    $('#contact-us').on('focus', '.invalid-border', function () {
        $(this).removeClass('invalid-border');
    });

    $('#contact-us .form-control').on('blur', function () {
        if ($(this).val() === '') {
            $(this).addClass('invalid-border');
        }

        if ($(this).hasClass('email') && !isValidEmailAddress($(this).val())) {
            $(this).addClass('invalid-border');
        }
    });
});

function isValidEmailAddress(emailAddress) {
    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
    return pattern.test(emailAddress);
}

function attachContactUsForm() {
    $('#contactUsButton').off('click').on('click', function () {

        var valid = true;

        $('#contact-us .form-control').each(function (i, e) {
            if ($(e).val() == '') {

                $(e).addClass('invalid-border');
                valid = false;
            }
        });

        // check email address.
        if (!isValidEmailAddress($('#contactUsEmail').val())) {
            $('#contactUsEmail').addClass('invalid-border');
            valid = false;
        }

        var captchaValue = grecaptcha.getResponse();

        if (captchaValue === '') {
            $('#contactUsCaptcha iframe').addClass('invalid-border');
            valid = false;
        }

        if (!valid) {
            return;
        }

        $('#contactUsLoading').show();
        $('#contactUsButton').hide();

        var token = $('input[name="__RequestVerificationToken"]').val();

        $.ajax({
            url: '/Home/SendIntroContactUs',
            type: 'POST',
            data: {
                Name: $('#contactUsName').val(),
                EmailAddress: $('#contactUsEmail').val(),
                Message: $('#contactUsMessage').val(),
                CaptchaValue: captchaValue,
                PartnerCode: $('#contactUsParnterCode').val(),
                __RequestVerificationToken: token
            },
            cache: false,
            success: function () {

            },
            error: function () {

            },
            complete: function () {
                $('#contactUsLoading').hide();
                $('#contactUsCaptcha').hide();
                $('#contactUsResult').show();

            }

        });

    });
}

function attachCodeSubmit() {
    $('.partners-submit').off('click').on('click', function (ev) {
        ev.preventDefault();
        var code = $(this).closest($(".code-entry-container")).find(".code-entry").val();
        if (code.length === 0) {
            $(".code-error").html("Please enter a code");
            $(".code-error").show();
            return;
        }

        if ($("#partner-recaptcha").val() == "true") {
            var response = grecaptcha.getResponse();
            if ($("#partner-captcha-response").length) {
                $("#partner-captcha-response").val(response);
            }
            if (response.length === 0) {
                $(".captcha-error").show(250);
                return;
            } else {
                $(".captcha-error").hide(250);
            }
        }

        $("#partner-form-final .form-control").removeClass("invalid-border");
        $("#additional-information-error").hide();
        $("#terms-error").hide();
        $(".code-error").hide();
        $("#access-code-final").val(code);
        $("#additional-info-modal").modal("show");
        $("#is-login").val("False");

        if ($("#allow-linking-existing-sso").val() === "True" && $("#is-login").val() === "True") {
            $("#account-login-sso").show();
        }
        else {
            $("#account-login-sso").hide();
        }
    });

    $('.partners-login').off('click').on('click', function (ev) {
        ev.preventDefault();

        $("#partner-form-final .form-control").removeClass("invalid-border");
        $("#additional-information-error").hide();
        $("#terms-error").hide();
        $(".code-error").hide();

        $("#additional-info-modal").modal("show");
        $("#is-login").val("True");

        if ($("#allow-linking-existing-sso").val() === "True" && $("#is-login").val() === "True") {
            $("#account-login-sso").show();
        }
        else {
            $("#account-login-sso").hide();
        }
    });
}

function attachPartnerSubmit(frmName, ajaxName) {
    var frm = $('#' + frmName);
    frm.submit(function (ev) {
        ev.preventDefault();
        $(".partners-submit").hide();
        $(".loading-code").show();
        $(".code-error").hide();
        $(".invalid-border").removeClass(".invalid-border");

        if ($("#partner-recaptcha") != null && $("#partner-recaptcha").val() == "true") {
            var response = grecaptcha.getResponse();
            if ($("#partner-captcha-response").length) {
                $("#partner-captcha-response").val(response);
            }
            if (response.length === 0) {
                $(".captcha-error").show(250);
                return;
            } else {
                $(".captcha-error").hide(250);
            }
        }

        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                if (data.IsSuccess) {
                    
                    if (data.UseCustomHtmlPage) {
                        sessionStorage.setItem("has-subcode-page", data.UseCustomHtmlPage);
                        location.href = "/registration";     
                    }
                    else {
                        location.href = "/registration/";
                    }                                     
                    
                } else {
                    $(".code-error").html(data.ResponseHtml);
                    $("<i class='ph-fill ph-warning-circle fs-4 d-block'></i>").prependTo(".code-error");
                    $(".code-error").show();
                    $(".partners-submit").show();
                    $(".loading-code").hide();
                    $(".code-entry").addClass("invalid-border");
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $("#" + ajaxName).html(xhr.responseText);
                $(".partners-submit").show();
                $(".loading-code").hide();
            }
        });
        ev.preventDefault();
    });
};
jQuery(function ($) {

    $(function () {
        $('#video-tour-modal').off('show.bs.modal');
        $('#video-tour-modal').on('show.bs.modal', function () {

            $("#video-tour-modal-frame").attr('src', $("[id$='video-tour-modal-button']").attr('data-videosrc'));
            $('#modal-backdrop').addClass('modal-backdrop-default');
        });

        //stop video from playing when close modal
        $('[id^="video-"][id$="-modal"]').on('hide.bs.modal', function () {
            $(this).find("iframe").removeAttr('src');
            $('#modal-backdrop').removeClass('modal-backdrop-default');
        });
    });
});;
