/* Minification failed. Returning unminified contents.
(18,21-22): run-time error JS1195: Expected expression: >
(22,6-7): run-time error JS1195: Expected expression: )
(34,25-26): run-time error JS1195: Expected expression: >
(40,10-11): run-time error JS1195: Expected expression: )
(45,29-30): run-time error JS1195: Expected expression: >
(56,14-15): run-time error JS1195: Expected expression: )
(65,29-30): run-time error JS1195: Expected expression: >
(67,14-15): run-time error JS1195: Expected expression: )
(74,25-26): run-time error JS1195: Expected expression: >
(82,10-11): run-time error JS1195: Expected expression: )
(83,38-39): run-time error JS1197: Too many errors. The file might not be a JavaScript file: )
(1,1-27): run-time error JS1301: End of file encountered before function is properly closed: function click_text(e, id)
(85,5-6): run-time error JS1002: Syntax error: }
(86,1-2): run-time error JS1197: Too many errors. The file might not be a JavaScript file: }
 */
function click_text(e, id) {
    let localizeText = document.getElementsByClassName("localize-text")[0].value;
    var inpClass = ".inp" + id;
    var listClass = ".list" + id;
    var listGroupClass = ".list-group" + id;
    var scrollResult = "scroll" + id;
    var dispClass = ".disp" + id;
    var displayNotFoundClass = ".disp-none" + id;
    var iconOpen = "#icon-open" + id;
    var iconClose = "#icon-close" + id;
    $(iconOpen).hide();
    $(iconClose).show();

    let inputText = document.querySelector(inpClass);
    let list = document.querySelector('.list ' + listGroupClass).querySelectorAll('li');
    let arrLength = document.querySelectorAll(listGroupClass + ' li').length;
    let ignoreCount = 0;
    list.forEach(x => {
        if (x.classList.contains("not-searchable") || x.classList.contains("sub-heading")) {
            ignoreCount++;
        }
    })
    let matched = arrLength - ignoreCount;

    var scrollText = Webropol.Shared.Common.stringFormat(localizeText, matched);
    document.getElementById(scrollResult).textContent = scrollText;

    $(listClass).show();

    let text = $(e).val();
    if (text != "") {
        let isPreselect = false;
        let exp = new RegExp(text, 'i');
        list.forEach(x => {
            if (!x.classList.contains('not-searchable') && !x.classList.contains('sub-heading')) {
                if (exp.test(x.textContent)) {
                    isPreselect = true;
                }
            }
        })

        if (isPreselect) {
            let reg = new RegExp(inputText.value, 'i');
            let count = 0;
            list.forEach(x => {
                if (!x.classList.contains('not-searchable') && !x.classList.contains('sub-heading')) {
                    if (reg.test(x.textContent)) {
                        x.innerHTML = x.textContent.replace(reg, '<p style="background-color:#137A98;color:#fff">$&</p>')
                        x.style.display = 'block';
                        count++;
                    }
                    else {
                        x.style.display = 'none';
                    } 
                }
            })
            var scrollText = Webropol.Shared.Common.stringFormat(localizeText, count);
            document.getElementById(scrollResult).textContent = scrollText;

            $(displayNotFoundClass).hide();
            $(dispClass).show();
            $(listClass).show();
        }
        else {
            list.forEach(x => {
                x.style.display = 'none';
            })
            $(displayNotFoundClass).show();
            $(dispClass).hide();
            $(listClass).hide();
        }
    }
    else {
        list.forEach(x => {
            x.innerHTML = x.textContent;
            if (!x.classList.contains('not-searchable')) {
                x.style.display = 'block';
            }
            else {
                x.style.display = 'none';
            }
        })
        $(displayNotFoundClass).hide();
        $(dispClass).show();
    }
}

function click_icon_open(id) {
    var iconOpen = "#icon-open" + id;
    var iconClose = "#icon-close" + id;
    $(iconOpen).hide();
    $(iconClose).show();
    var inpClass = ".inp" + id;
    $(inpClass).click();   
}

function click_icon_close(id) {
    var iconOpen = "#icon-open" + id;
    var iconClose = "#icon-close" + id;
    $(iconClose).hide();
    $(iconOpen).show();
    $('.list').hide();
    $('.disp-none').hide();
    $('.disp-result').hide();
}

function filter_list(id) {
    let localizeText = document.getElementsByClassName("localize-text")[0].value;
    var inpClass = ".inp" + id;
    var listGroupClass = ".list-group" + id;
    var listClass = ".list" + id;
    var scrollResult = "scroll" + id;
    var dispClass = ".disp" + id;
    var displayNotFoundClass = ".disp-none" + id;

    let inputText = document.querySelector(inpClass);
    let list = document.querySelector('.list ' + listGroupClass).querySelectorAll('li');

    $(listClass).show();

    let reg = new RegExp(inputText.value, 'i');
    let count = 0;
    list.forEach(x => {
        if (!x.classList.contains('not-searchable') && !x.classList.contains('sub-heading')) {
            if (reg.test(x.textContent)) {
                x.innerHTML = x.textContent.replace(reg, '<p style="background-color:#137A98;color:#fff">$&</p>')
                x.style.display = 'block';
                count++;
            }
            else {
                x.style.display = 'none';
            } 
        }
    })

    var scrollText = Webropol.Shared.Common.stringFormat(localizeText, count);
    document.getElementById(scrollResult).textContent = scrollText;

    if (count == 0 && inputText.value != "") {
        list.forEach(x => {
            x.innerHTML = x.textContent;
            x.style.display = 'none';
        })
        $(displayNotFoundClass).show();
        $(dispClass).hide();
        $(listClass).hide();
    }
    else {
        list.forEach(x => {
            if (x.classList.contains('sub-heading') && !x.classList.contains('not-searchable')) {
                x.style.display = 'block';
            }
        })
        $(displayNotFoundClass).hide();
        $(dispClass).show();
        $(listClass).show();
    }
}

function moveFocus(e, id) {
    var listGroupClass = ".list-group" + id;
    const tabbableItems = Array.from(document.querySelector('.list ' + listGroupClass).querySelectorAll('li:not([style*="display:none"]):not([style*="display: none"]):not([disabled]):not(.disabled-li)'));
    let currentIndex = e.currentTarget.getAttribute("tabIndex");
    if (e.key === "ArrowDown") {
        currentIndex = (parseFloat(currentIndex) + 0.001).toFixed(3);
    } else if (e.key === "ArrowUp") {
        currentIndex = (parseFloat(currentIndex) - 0.001).toFixed(3);
    }
    if (currentIndex.charAt(currentIndex.length - 1) == 0) {
        if (currentIndex.charAt(currentIndex.length - 2) == 0) {
            currentIndex = currentIndex.slice(0, -2);
        }
        else {
            currentIndex = currentIndex.slice(0, -1);
        }
    }
    if (currentIndex == 0.00) {
        currentIndex = 0;
    }
    const toFocus = tabbableItems.find(el =>
        el.getAttribute("tabIndex") == currentIndex
    )
    if (toFocus) {
        toFocus.focus();

        var lid = toFocus.getAttribute("id");
        document.getElementById(lid)
            .addEventListener("keyup", function (event) {
                event.preventDefault();
                if (event.keyCode === 13) {
                    document.getElementById(lid).click();
                }
        });
    }
}

function click_li(e, qid) {
    var inpClass = ".inp" + qid;
    var selectId = "#select" + qid;
    var dispClass = ".disp" + qid;
    var displayNotFoundClass = ".disp-none" + qid;
    var listClass = ".list" + qid;

    if (!e.classList.contains('disabled')) {
        $(inpClass).val($(e).text());
        var id = $(e).attr('data-val');
        $(selectId).val(id).change();

        $(listClass).hide();
        $(dispClass).hide();
        $(displayNotFoundClass).hide();
    }
}

function blur_text(id, shortid) {
    var inpClass = ".inp" + id;
    if ($(inpClass).val() == "") {
        var selectId = "#select" + id;
        var selectedAttribute = "#" + shortid + "_SelectedAttribute";
        $(selectedAttribute).val("");
        $(selectId).prop("selectedIndex", 0);
    }
}

$(document).ready(function () {
    $('.list').hide();
    $('.disp-none').hide();
    $('.disp-result').hide();
    $('.icon-close').hide();
});

$(document).mouseup(function (e) {
    var container = $('select-container');
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        $('.list').hide();
        $('.disp-none').hide();
        $('.disp-result').hide();
        $('.icon-close').hide();
        $('.icon-open').show();
    }
});;
