// source --> /inc/themes/common/assets/js/core.js?ver=1.0 
let DOMHistory = [];

document.addEventListener('click', function(event) {
    var target = event.target;
    if (target.tagName === 'A' || target.getAttribute('data-navigation')) {
        target.setAttribute('data-navigation', 'true');
        history.pushState(null, null, target.href);
    }
});

// Listen for the popstate event
window.addEventListener('popstate', function(event) {
    var isUserInitiatedNavigation = event.state === null;
    if (!isUserInitiatedNavigation) {
        load_page(window.location.href, false);
    }
});
function startLoader() {
    $('html').addClass('page--in-progress');
    $bar = $('.site-header-progressbar');
    $bar.addClass('site-header-progressbar--in-process');
}
function stopLoader(){
    $bar = $('.site-header-progressbar');
    $bar.addClass('site-header-progressbar--finished');
    setTimeout(function () {
        $bar.removeClass('site-header-progressbar--finished site-header-progressbar--in-process');
        $('html').removeClass('page--in-progress');
    }, 400);
}

function load_dom(href, update_history = true){
    if(typeof DOMHistory[href] !== 'undefined'){
        $('body').removeClass('--menu-show');
        let dom = DOMHistory[href];
        let page = false;
        dom.each(function(i, e){
            if(e.id === '_page'){
                page = $(e);
            }
        });
        $('#_page').html(page.html());
        setTimeout(function(){
            $('.--content-loader').removeClass('show');
        }, 300);
        // Scroll Top
        //if href has #, then scroll to that element
        if(href.indexOf('#') !== -1){
            let target = href.split('#')[1];
            let targetEl = $('#'+target);
            if(targetEl.length){
                $('html, body').animate({
                    scrollTop: targetEl.offset().top
                }, 200);
            }
        } else {
            $('html, body').scrollTop(0);
        }
        if($('.--category-header .--btn-category.selected').length > 0) {
            $('.--category-header > .--wrap .--items').scrollLeft($('.--category-header .--btn-category.selected').offset().left - 20);
        }
        //update history
        if(update_history) {
            let stateObj = {foo: "bar"};
            history.pushState(stateObj, "page 2", href);
        }
        _init();
    } else {
        load_page(href, update_history);
    }
}
function load_page(href, update_history = true){
    startLoader();
    $('body').removeClass('--menu-show');
    $.ajax({
        url: href,
        type: 'get',
        dataType: 'html',
        data: {
            action: 'load_page',
        },
        success: function (response) {
            let dom = $(response);
            let page = false;
            DOMHistory[href] = dom;
            dom.each(function(i, e){
                if(e.id === '_page'){
                    page = $(e);
                }
            });
            $('#_page').html(page.html());
            setTimeout(function(){
                stopLoader();
            }, 300);
            // Scroll Top
            //if href has #, then scroll to that element
            if(href.indexOf('#') !== -1){
                let target = href.split('#')[1];
                let targetEl = $('#'+target);
                if(targetEl.length){
                    $('html, body').animate({
                        scrollTop: targetEl.offset().top
                    }, 200);
                }
            } else {
                $('html, body').scrollTop(0);
            }

            //update history
            if(update_history) {
                let stateObj = {foo: "bar"};
                history.pushState(stateObj, "page 2", href);
            }
            _init();
        },
        error: function (response) {
            let dom = $(response.responseText);
            let page = false;
            DOMHistory[href] = dom;
            dom.each(function(i, e){
                if(e.id === '_page'){
                    page = $(e);
                }
            });
            if(page) {
                $('#_page').html(page.html());
            }
            setTimeout(function(){
                stopLoader();
            }, 300);
        }

    });
}

function _init_sliders(){
    $('.swiper:not(.swiper-initialized)').each(function(){
        let gap = 20;
        if($(this).attr('data-gap')){
            gap = parseInt($(this).attr('data-gap'));
        }

        if(window.innerWidth < 768){
            gap = gap/2
        }
        let swiper = new Swiper(this, {
            slidesPerView: 'auto',
            spaceBetween: gap,
            loop: true,
            navigation: {
                nextEl: $(this).find('.swiper-button-next')[0],
                prevEl: $(this).find('.swiper-button-prev')[0],
            },
        });
    });
}

function modal_open(id){
    $('main').after('<div class="--page-loader"><div class="--loader"><span></span></div><div class="overlay"></div></div>');
    let _hide_loader = function(){
        $('.--page-loader').removeClass('--loading');
        setTimeout(function(){
            $('.--page-loader').remove();
        }, 400);
    }
    $.ajax({
        url: site_url + '/app/admin-ajax.php',
        type: 'POST',
        dataType: 'json',
        data: {
            action: 'modal_open',
            id: id,
        },
        beforeSend: function () {
            setTimeout(function(){
                $('.--page-loader').addClass('--loading');
            }, 2);
        },
        success: function (response) {
            if(response.data !== ''){
                let modal = '<div class="--modal" id="modal_'+id+'">'+ response.data +'<div class="overlay"></div>\n' +
                    '</div>';
                $('body').append(modal);
                _init_eazyforms();
                setTimeout(function(){
                    $('#modal_'+id).addClass('--show');
                    $('#modal_'+id+' .close').on('click', function(){
                        modal_close(id);
                    });
                    $('#modal_'+id+' .overlay').on('click', function(){
                        modal_close(id);
                    });

                    let minWidth = $('#modal_'+id+' [data-min-width]').attr('data-min-width');
                    if(minWidth){
                        $('#modal_'+id+' --window').css('min-width', minWidth);
                    }
                }, 50);
            }
            _hide_loader();
        },
        error: function (response) {
            _hide_loader();
        }

    });
}

function modal_close(id){
    $('#modal_'+id).removeClass('--show');
    setTimeout(function(){
        $('#modal_'+id).remove();
    }, 400);
}

function _init(){
    _init_sliders();
     _init_eazyforms();
}
jQuery(document).ready(function($){

    // AJAX Load Page

    $('body').on('click', '[href]', function(){
        let href = $(this).attr('href');

        if(href.charAt(0) === '/'){
            href = site_url + href;
        }

        //if first character is #, then it's a link to a section on the page
        if(href.charAt(0) === '#'){
            return true;
        }

        if($(this).hasClass('noajax')){
            return true;
        }

        //if has target, then it's a link to another page
        if($(this).attr('target')){
            return true;
        }

        //if href has not current domain (var site_url), return true

        if(href.indexOf(site_url) === -1){
            return true;
        }


        if($(this).attr('data-transition')){
            var $transition = $(this).attr('data-transition');
            if($('#transition_'+$transition).length > 0){
                if($transition == 'category') {
                    $('.--category-layout > .--wrap > .--content').html($('#transition_' + $transition)[0].outerHTML);
                }
            }
        }
        load_dom(href);
        $('body').removeClass('--menu-open');
        return false;
    })

    _init();

    $('body').on('click', '.--review-card button', function(){
        let card = $(this).closest('.--review-card');
        let toggleText = $(this).attr('data-toggle-text');
        let labelText = $(this).find('.label').text();
        card.toggleClass('active');
        if(card.hasClass('active')){
            card.find('.--review').css('height', 'auto');
        } else {
            card.find('.--review').css('height', 102);
        }
        $(this).find('.label').text(toggleText)
        $(this).attr('data-toggle-text', labelText);
        return false;
    });

    $('body').on('click', '[data-youtube]', function(){
        let youtube_url = $(this).attr('data-youtube');
        // parse id
        // Example: https://youtu.be/5TxF9PQaq4U, https://www.youtube.com/watch?v=5TxF9PQaq4U, https://www.youtube.com/embed/5TxF9PQaq4U
        let video_id = youtube_url.split('v=')[1];
        if(!video_id){
            video_id = youtube_url.split('.be/')[1];
        }
        if(!video_id){
            video_id = youtube_url.split('/embed/')[1];
        }
        if(!video_id){
            return false;
        }
        if(typeof video_id.split('&')[1] !== 'undefined'){
            video_id = video_id+'&autoplay=1';
        } else {
            video_id = video_id+'?autoplay=1';
        }
        video_id = video_id.replace('&t=', '?start=');
        let frame = '<iframe width="100%" height="100%" src="https://www.youtube.com/embed/'+video_id+'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>';
        $('[data-youtube] > .--player').remove();
        $(this).append('<div class="--player">'+frame+'</div>');

        return false;

    });

    $('body').on('click', '.--menu-btn', function(){
        $('body').toggleClass('--menu-open');
    });

    $('body').on('click', '.menu-item-has-children > a', function(){
        $(this).closest('.menu-item-has-children').toggleClass('active');
        return false;
    });

    $('body').on('click', '.--accordion > .--wrap > .--head', function(){
        $(this).closest('.--accordion').toggleClass('--active');
    });

    $('body').on('click', '.--btn-dropdown > button', function(){
        $(this).closest('.--btn-dropdown').toggleClass('--active');
    });

    $('body').on('click', '[href^="#modal_"]', function(){
        let modal_id = $(this).attr('href').replace('#modal_', '');
        modal_open(modal_id);
        return false;
    });


    $('body').on('click', '.--modal-close', function(){
        let modal_id = $(this).closest('.--modal').attr('id').replace('modal_', '');
        modal_close(modal_id);
        return false;
    });


    //Открываем все внешние ссылки с новой вкладки
    // Получаем текущий домен
    const currentDomain = window.location.hostname;

    // Получаем все ссылки на странице
    const links = document.querySelectorAll('a');

    // Добавляем обработчик клика для каждой ссылки
    links.forEach(function(link) {
        link.addEventListener('click', function(event) {
            // Получаем href ссылки
            const href = link.getAttribute('href');

            // Проверяем, если href начинается с '/' или содержит текущий домен
            if (!href.startsWith('/') && !href.startsWith('#') && !href.includes(currentDomain)) {
                // Останавливаем стандартное поведение
                event.preventDefault();
                // Открываем ссылку в новой вкладке
                window.open(href, '_blank');
            }
        });
    });
    _init_eazyforms();
});
// source --> /inc/themes/common/assets/plugins/forms/jquery.forms.js 
//
// EazyForms 1.0.0
// Author: Andrei Tereshin
// Site: https://tereshin.co
//

let _EazyFormsHooks = [];
function _EazyPushHook(id, name, value) {
    if (typeof _EazyFormsHooks[id] === 'undefined') {
        _EazyFormsHooks[id] = {};
    }
    _EazyFormsHooks[id][name] = value;
}
function EazyForms(form) {
    let el = {
        id: false,
        form: false,
        valid: true,
        url: site_url + '/app/admin-ajax.php',
        headers: false,
        fields: {},
        success: function (response) {
            if (typeof response.redirect !== 'undefined') {
                location = response.redirect;
            }
            this.alert(response);
        },
        error: function (xhr) {
            var response = xhr.responseJSON;
            var html = EazyFormsAlert.get('error', response.title, response.message, 'info-circle', 'outline', response.actions);
            this.form.find('.--header .alerts').html(html);
        },
        alert: function (response) {
            this.form.find('.--header .alerts').html('');
            if (response.title !== false && response.message !== false) {
                var html = EazyFormsAlert.get(response.status, response.title, response.message, 'info-circle', 'outline', response.actions);
                this.form.find('.--header .alerts').html(html);
            }
        },
        init: function (form) {
            let $this = this;
            this.form = form;
            this.id = form.attr('id');
            if (form.attr('action')) {
                this.url = form.attr('action');
            }
            form.find('input, select, textarea').each(function (i, e) {
                let input = $(this).closest('.area');
                input.mouseenter(function () {
                    input.addClass('hover');
                })
                //remove class 'show' to toolbar_box after mouseleave

                input.mouseleave(function () {
                    input.removeClass('hover');
                });

                $(this).change(function () {
                    $this.validateInput(input);
                });

                input.click(function () {
                    $('.area').removeClass('focus');
                    input.addClass('focus');
                });
                $(this).focus(function () {
                    $('.area').removeClass('focus');
                    input.addClass('focus');
                });
                $(this).click(function () {
                    $('.area').removeClass('focus');
                    input.addClass('focus');
                    if ($(this).attr('type') === 'radio') {
                        let input_name = $(this).attr('name');
                        $('[name="' + input_name + '"]').closest('.area').removeClass('--active');
                    }
                    if ($(this).attr('type') === 'radio' || $(this).attr('type') === 'checkbox') {
                        if ($(this).prop('checked')) {
                            input.addClass('--active');
                        } else {
                            input.removeClass('--active');
                        }
                    }
                });

                if (typeof $(this).attr('disabled') !== 'undefined') {
                    $(this).closest('.--fieldset').addClass('disabled');
                }

                if ($(this).attr('type') === 'file') {
                    input.find('.upload:not(.dz-message)').click(function () {
                        $(this).closest('.area').find('input[type="file"]').click();
                        return false;
                    });
                    $(this).change(function () {
                        input.removeClass('focus');
                        let placeholder_id = $(this).attr('id') + '_placeholder';
                        var image_url = URL.createObjectURL($(this)[0].files[0]);
                        if ($('#' + placeholder_id).length > 0) {
                            $('#' + placeholder_id).html('<img src="' + image_url + '">');
                        }
                    });
                }
                if ($(this).attr('type') === 'checkbox' || $(this).attr('type') === 'radio') {
                    input.find('.box').click(function () {
                        $(this).prev('input').click();
                        input.removeClass('focus');
                    })
                }
                // Init mask
                if ($(this).data('mask')) {
                    if ($(this).data('mask') === 'phone') {
                        var placeholder = $(this).attr('placeholder');
                        //$(this).inputmask("+9 999-999 99 99");
                        input.keyup(function () {
                            $this.changeInputMask(input);
                        });
                        $(this).mouseenter('input', function () {
                            $(this).attr('placeholder', placeholder);
                        });
                    }
                }

                // Init toolbar
                let toolbar = $(this).closest('.--fieldset').find('[data-toolbar]');
                if (toolbar.length > 0) {
                    let toolbar_id = toolbar.data('toolbar');
                    if (form.find('#' + toolbar_id).length > 0) {
                        let toolbar_box = form.find('#' + toolbar_id);
                        //add class 'show' to toolbar_box after mouseenter
                        toolbar.mouseenter(function () {
                            toolbar_box.addClass('show');
                        })
                        //remove class 'show' to toolbar_box after mouseleave

                        toolbar.mouseleave(function () {
                            toolbar_box.removeClass('show');
                        });

                        //change box position
                        toolbar_box.css({});
                    }
                }


            });

            this.form.submit(function (e) {
                $this.send();
                return false;
            });
            this.form.find('button[type="submit"]').click(function (e) {
                $this.send();
                return false;
            });
            $('.--submit-' + this.id).click(function () {
                $this.send();
                return false;
            });
            if (typeof window['gifteyforms_' + this.id + '_values'] === 'object') {
                this.setValues(window['gifteyforms_' + this.id + '_values']);
            }
            this.form.find('input').trigger('keyup');
            this.form.addClass('init');
            if (typeof _EazyFormsHooks[this.id] !== 'undefined') {
                $.each(_EazyFormsHooks[this.id], function (name, v) {
                    $this[name] = v;
                })
            }
            return this;
        },

        getValues: function () {
            let fieldsets = this.form.find('input, textarea:not(.select2-search__field), select');
            let $this = this;
            let data = new FormData();
            fieldsets.each(function (i, e) {
                let name = $(this).attr('name');
                let value = $(this).val();
                if ($(this).attr('type') === 'checkbox') {
                    value = $(this).prop('checked');
                }
                if ($(this).attr('type') === 'file' && value !== '') {
                    value = $(this)[0].files[0];
                }
                if ($(this)[0].nodeName === 'SELECT') {
                    if (value === null) {
                        value = '';
                    }
                }
                if (typeof $(this).attr('disabled') === 'undefined') {
                    if ($(this).attr('type') === 'radio') {
                        if ($(this).prop('checked') === true) {
                            data.append(name, value);
                        }
                    } else {
                        data.append(name, value);
                    }

                }
            });
            this.fields = data;
            return this.fields;
        },
        prepareData: function (data) {
            return data;
        },
        setValues: function (data) {
            let $this = this.form;
            $.each(data, function (k, v) {
                let field = $this.find('[name="' + k + '"]');
                if (field.length > 0) {
                    let type = field.attr('type');
                    switch (type) {
                        case 'checkbox':
                            if (typeof v !== 'undefined' && typeof v !== 'object') {
                                field.prop('checked', true).closest('.area').addClass('--active');
                            } else {
                                $.each(v, function (i, item) {
                                    $this.find('[name="' + k + '[]"][value="' + item + '"]').prop('checked', true).closest('.area').addClass('--active');
                                });
                            }
                            break;
                        case 'radio':
                            $this.find('[name="' + k + '"][value="' + v + '"]').prop('checked', true).closest('.area').addClass('--active');
                            break;
                        default:
                            field.val(v);
                            break;
                    }
                }
            })
        },
        validate: function () {
            this.valid = true;
            this.form.find('.invalid').removeClass('invalid');
            this.form.find('.error').removeClass('show');
            this.form.find('.error .text').html('');
            let $this = this;
            let fieldsets = this.form.find('input, textarea, select');
            fieldsets.each(function (i, e) {
                $this.validateInput($(this).closest('.area'));
            });
            return this.valid;
        },
        validateInput: function (el) {
            let $this = this;
            let fieldset = el.closest('.--fieldset');
            fieldset.removeClass('invalid');
            fieldset.find('.error').removeClass('show');
            fieldset.find('.error .text').html('');

            let fieldsets = fieldset.find('input, textarea, select');
            fieldsets.each(function (i, e) {
                let error_text = '';
                if ($(this).data('error') !== '') {
                    error_text = $(this).data('error');
                }
                if ($(this).attr('required') && typeof $(this).attr('disabled') === 'undefined') {
                    if ($(this).attr('type') === 'checkbox') {
                        if (!$(this).prop('checked')) {
                            $this.valid = false;
                            $this.setError($(this), error_text);
                        }
                    }
                    if ($(this).val() === '') {
                        $this.valid = false;
                        $this.setError($(this), error_text);
                    }
                }
                if ($(this).data('mask') && typeof $(this).attr('disabled') === 'undefined') {
                    let function_name = 'validate_' + $(this).data('mask');
                    if (typeof $this[function_name] === 'function') {
                        if (!$this[function_name]($(this).val())) {
                            if (typeof $(this).attr('required') !== 'undefined' || (typeof $(this).attr('required') === 'undefined' && $(this).val() !== '')) {
                                $this.valid = false;
                                $this.setError($(this), error_text);
                            }
                        }
                    }
                }
            });
        },
        countriesPhoneCodes: {
            "1": ["US", "CA"],
            "7": ["RU", "KZ"],
            "20": ["EG"],
            "27": ["ZA"],
            "30": ["GR"],
            "31": ["NL"],
            "32": ["BE"],
            "33": ["FR"],
            "34": ["ES"],
            "36": ["HU"],
            "39": ["IT"],
            "40": ["RO"],
            "41": ["CH"],
            "43": ["AT"],
            "44": ["GB", "GG", "IM", "JE"],
            "45": ["DK"],
            "46": ["SE"],
            "47": ["NO", "SJ"],
            "48": ["PL"],
            "49": ["DE"],
            "51": ["PE"],
            "52": ["MX"],
            "53": ["CU"],
            "54": ["AR"],
            "55": ["BR"],
            "56": ["CL"],
            "57": ["CO"],
            "58": ["VE"],
            "60": ["MY"],
            "61": ["AU", "CC", "CX"],
            "62": ["ID"],
            "63": ["PH"],
            "64": ["NZ"],
            "65": ["SG"],
            "66": ["TH"],
            "81": ["JP"],
            "82": ["KR"],
            "84": ["VN"],
            "86": ["CN"],
            "90": ["TR"],
            "91": ["IN"],
            "92": ["PK"],
            "93": ["AF"],
            "94": ["LK"],
            "95": ["MM"],
            "98": ["IR"],
            "211": ["SS"],
            "212": ["MA", "EH"],
            "213": ["DZ"],
            "216": ["TN"],
            "218": ["LY"],
            "220": ["GM"],
            "221": ["SN"],
            "222": ["MR"],
            "223": ["ML"],
            "224": ["GN"],
            "225": ["CI"],
            "226": ["BF"],
            "227": ["NE"],
            "228": ["TG"],
            "229": ["BJ"],
            "230": ["MU"],
            "231": ["LR"],
            "232": ["SL"],
            "233": ["GH"],
            "234": ["NG"],
            "235": ["TD"],
            "236": ["CF"],
            "237": ["CM"],
            "238": ["CV"],
            "239": ["ST"],
            "240": ["GQ"],
            "241": ["GA"],
            "242": ["CG"],
            "243": ["CD"],
            "244": ["AO"],
            "245": ["GW"],
            "246": ["IO"],
            "248": ["SC"],
            "249": ["SD"],
            "250": ["RW"],
            "251": ["ET"],
            "252": ["SO"],
            "253": ["DJ"],
            "254": ["KE"],
            "255": ["TZ"],
            "256": ["UG"],
            "257": ["BI"],
            "258": ["MZ"],
            "260": ["ZM"],
            "261": ["MG"],
            "262": ["RE", "YT"],
            "263": ["ZW"],
            "264": ["NA"],
            "265": ["MW"],
            "266": ["LS"],
            "267": ["BW"],
            "268": ["SZ"],
            "269": ["KM"],
            "290": ["SH"],
            "291": ["ER"],
            "297": ["AW"],
            "298": ["FO"],
            "299": ["GL"],
            "350": ["GI"],
            "351": ["PT"],
            "352": ["LU"],
            "353": ["IE"],
            "354": ["IS"],
            "355": ["AL"],
            "356": ["MT"],
            "357": ["CY"],
            "358": ["FI", "AX"],
            "359": ["BG"],
            "370": ["LT"],
            "371": ["LV"],
            "372": ["EE"],
            "373": ["MD"],
            "374": ["AM"],
            "375": ["BY"],
            "376": ["AD"],
            "377": ["MC"],
            "378": ["SM"],
            "379": ["VA"],
            "380": ["UA"],
            "381": ["RS"],
            "382": ["ME"],
            "385": ["HR"],
            "386": ["SI"],
            "387": ["BA"],
            "389": ["MK"],
            "420": ["CZ"],
            "421": ["SK"],
            "423": ["LI"],
            "500": ["FK"],
            "501": ["BZ"],
            "502": ["GT"],
            "503": ["SV"],
            "504": ["HN"],
            "505": ["NI"],
            "506": ["CR"],
            "507": ["PA"],
            "508": ["PM"],
            "509": ["HT"],
            "590": ["GP", "BL", "MF"],
            "591": ["BO"],
            "592": ["GY"],
            "593": ["EC"],
            "594": ["GF"],
            "595": ["PY"],
            "596": ["MQ"],
            "597": ["SR"],
            "598": ["UY"],
            "599": ["BQ", "CW", "SX"],
            "670": ["TL"],
            "672": ["AQ"],
            "673": ["BN"],
            "674": ["NR"],
            "675": ["PG"],
            "676": ["TO"],
            "677": ["SB"],
            "678": ["VU"],
            "679": ["FJ"],
            "680": ["PW"],
            "681": ["WF"],
            "682": ["CK"],
            "683": ["NU"],
            "685": ["WS"],
            "686": ["KI"],
            "687": ["NC"],
            "688": ["TV"],
            "689": ["PF"],
            "690": ["TK"],
            "691": ["FM"],
            "692": ["MH"],
            "850": ["KP"],
            "852": ["HK"],
            "853": ["MO"],
            "855": ["KH"],
            "856": ["LA"],
            "880": ["BD"],
            "886": ["TW"],
            "960": ["MV"],
            "961": ["LB"],
            "962": ["JO"],
            "963": ["SY"],
            "964": ["IQ"],
            "965": ["KW"],
            "966": ["SA"],
            "967": ["YE"],
            "968": ["OM"],
            "970": ["PS"],
            "971": ["AE"],
            "972": ["IL"],
            "973": ["BH"],
            "974": ["QA"],
            "975": ["BT"],
            "976": ["MN"],
            "977": ["NP"],
            "992": ["TJ"],
            "993": ["TM"],
            "994": ["AZ"],
            "995": ["GE"],
            "996": ["KG"],
            "998": ["UZ"],
            "1242": ["BS"],
            "1246": ["BB"],
            "1268": ["AG"],
            "1284": ["VG"],
            "1809": ["DO", "Sint Maarten"],
            "1868": ["TT"],
            "1869": ["KN"],
            "1876": ["JM"],
            "1248": ["AI"],
            "1264": ["AG"],
            "1340": ["VI"],
            "1345": ["KY"],
            "1441": ["BM"],
            "1473": ["GD"],
            "1649": ["TC"],
            "1664": ["MS"],
            "1670": ["MP"],
            "1671": ["GU"],
            "1684": ["AS"],
            "1721": ["SX"],
            "1758": ["LC"],
            "1767": ["DM"],
            "1784": ["VC"],
            "1787": ["PR"]
        },
        changeInputMask: function (el) {
            var input = el.find('input');
            var current_code = "33";
            var current_mask = "+99 9 99 99 99 99";
            var country_code = "";
            var value = input.val().replace(/ /g, '').replace(/_/g, '').replace(/-/g, '').replace(/\(/g, '').replace(/\)/g, '').replace(/\+/g, '');
            if (typeof this.country_code === 'undefined') {
                this.country_code = country_code;
            }
            if (value.length > 3) {
                current_code = value.substr(0, 3);
                //if first 1 symbol is 1 or 7
                if (current_code.substr(0, 1) == "1" || current_code.substr(0, 1) == "7") {
                    current_code = value.substr(0, 1);
                }
                var codes_two_length = ["20", "27", "30", "31", "32", "33", "34", "36", "39", "40", "41", "43", "44", "45", "46", "47", "48", "49", "51", "52", "53", "54", "55", "56", "57", "58", "60", "61", "62", "63", "64", "65", "66", "81", "82", "84", "86", "90", "91", "92", "93", "94", "95", "98"];
                //if first 2 symbols is 20-99
                if (codes_two_length.indexOf(current_code.substr(0, 2)) != -1) {
                    current_code = value.substr(0, 2);
                }

                //detect country code from array this.countriesPhoneCodes
                if (typeof this.countriesPhoneCodes[current_code] !== 'undefined') {
                    country_code = this.countriesPhoneCodes[current_code][0];
                    if (this.country_code != country_code) {
                        this.country_code = country_code;
                        var flag = '/inc/themes/common/assets/icons/flags/' + country_code + '.svg';
                        el.find('.icon:first-child').addClass('flag');
                        el.find('.icon:first-child .giftey-icon').html('<img src="' + flag + '" alt="' + country_code + '">');
                    }
                }
                switch (current_code) {
                    case "7":
                        current_mask = "+9 (999) 999-99-99";
                        break;
                    case "47":
                        current_mask = "+99 999 9 9999";
                        break;
                    case "375":
                        current_mask = "+999 (99) 999-99-99";
                        break;
                    case "90":
                        current_mask = "+99 999-999-99-99";
                        break;
                    case "33":
                        current_mask = "+99 9 99 99 99 99";
                        break;
                    case "380":
                        current_mask = "+999 99 999 9999";
                        break;
                    case "972":
                        current_mask = "+999 99 999 9999";
                        break;
                    default:
                        current_mask = "+99 999 999 99 99";
                        break;
                }
            } else {
                input.attr('placeholder', '+33 6 **** ** **')
            }
            input.inputmask(current_mask);
        },
        send: function () {
            let $this = this;
            if (!this.validate()) {
                return false;
            }

            if (typeof grecaptcha !== 'undefined' && typeof RECAPTCHA_V3_SITE_KEY !== 'undefined') {
                grecaptcha.ready(function() {
                    grecaptcha.execute(RECAPTCHA_V3_SITE_KEY, {action: 'submit'}).then(function(token) {
                        $this.getValues();
                        $this.fields.append('recaptcha_token', token);
                        $this.executeSend();
                    });
                });
            } else {
                this.getValues();
                this.executeSend();
            }
        },
        executeSend: function() {
            let $this = this;
            let data = this.prepareData(this.fields);
            let params = {
                url: this.url,
                method: 'POST',
                processData: false,
                contentType: false,
                dataType: 'json',
                data: data,
                beforeSend: function () {
                    $this.form.addClass('--progress');
                    $this.toggleButtonProgress();

                    try {
                        eval('ajax_before_' + this.id)();
                    } catch (e) {

                    }
                },
                success: function (response) {
                    $this.success(response);
                    $this.form.removeClass('--progress');
                    $this.toggleButtonProgress();
                },
                error: function (response) {
                    $this.error(response);
                    $this.form.removeClass('--progress');
                    $this.toggleButtonProgress();
                }
            };
            if (this.headers) {
                params.headers = this.headers;
            }
            $.ajax(params);
        },
        setError: function (field, error_text = '') {
            let $field = field.closest('.--fieldset');
            $field.addClass('invalid');
            $field.find('.error .text').html(error_text);
            if (error_text !== '') {
                $field.find('.error').addClass('show');
            }
        },
        toggleButtonProgress: function ($button = false) {
            if (!$button) {
                $button = this.form.find('[type="submit"]');
            }
            let $wrap = $button.find('.--wrap');
            if (!$button.hasClass('--loading')) {
                if ($wrap.length > 0) {
                    $wrap.after('<div class="--loader"><span></span></div>');
                }
            }
            $button.toggleClass('--loading');
            if (!$button.hasClass('--loading')) {
                $button.find('.--loader').remove();
            }
        },
        clear: function () {
            this.form.find('input:not([type="hidden"]), textarea, select').val('');
        },
        validate_phone: function (phone) {
            const re = /^[+]?[7-8]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im;
            if (phone.charAt(0) == '8') {
                phone = phone.replace('8', '7');
            }
            phone = phone.replace(/ /g, '').replace(/-/g, '');
            return re.test(phone);
        },
        validate_email: function (string) {
            const re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            return re.test(string);
        },
        validate_name: function (string) {
            if (string != undefined) {
                const re = /^[A-zА-яЁё]+$/i;
                string = string.replace(' ', '').replace(' ', '');
                return re.test(string);
            }
        },
        validate_sms_code: function (value) {
            return value.length === 4;
        }
    }
    return el.init(form);
}

function EazyFormsVerification(form) {
    let el = {
        id: false,
        form: false,
        valid: true,
        url: site_url + '/app/admin-ajax.php',
        fields: {},
        success: function (response) {

            if (typeof response.redirect !== 'undefined') {
                location = response.redirect;
            }
            this.alert(response);
        },
        error: function (xhr) {
            var response = xhr.responseJSON;
            var html = EazyFormsAlert.get('error', response.title, response.message, 'info-circle', 'outline', response.actions);
            this.form.find('.--header .alerts').html(html);
        },
        alert: function (response) {
            this.form.find('.--header .alerts').html('');
            if (response.title !== false && response.message !== false) {
                var html = EazyFormsAlert.get(response.status, response.title, response.message, 'info-circle', 'outline', response.actions);
                this.form.find('.--header .alerts').html(html);
            }
        },
        init: function (form) {
            let $this = this;
            this.form = form;
            this.id = form.attr('id');
            if (form.attr('action')) {
                this.url = form.attr('action');
            }
            form.find('input[type="number"]').each(function (i, e) {
                let input = $(this).closest('.area');
                input.mouseenter(function () {
                    input.addClass('hover');
                })
                input.mouseleave(function () {
                    input.removeClass('hover');
                });

                $(this).change(function () {
                    $this.validateInput(input);
                });
                input.click(function () {
                    $('.area').removeClass('focus');
                    input.addClass('focus');
                });
                $(this).focus(function () {
                    $('.area').removeClass('focus');
                    input.addClass('focus');
                });
                $(this).click(function () {
                    $('.area').removeClass('focus');
                    input.addClass('focus');
                });
                $(this).keyup(function (event) {
                    var allow_keys = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
                    if (allow_keys.includes(event.keyCode)) {
                        //allow only one number
                        if ($(this).val().length > 1) {
                            $(this).val($(this).val().charAt(0));
                        }
                        var k = i + 1;
                        if (form.find('input[type="number"]')[k]) {
                            if ($(this).val() !== '') {
                                $(form.find('input[type="number"]')[k]).focus();
                            }
                        } else {
                            $('.area').removeClass('focus');
                            form.find('button[type="submit"]').focus();
                        }
                    }
                    if (event.keyCode === 8) {
                        var k = i - 1;
                        if (form.find('input[type="number"]')[k]) {
                            $(form.find('input[type="number"]')[k]).focus();
                        }
                    }
                });
                $(this).bind('paste', function (event) {
                    var $this = $(this);
                    setTimeout(function () {
                        var value = $this.val().split('');
                        $.each(value, function (i, e) {
                            if (form.find('input[type="number"]')[i]) {
                                form.find('input[type="number"]')[i].value = e;
                            }
                        });
                        form.find('button[type="submit"]').focus();
                        $('.area').removeClass('focus');
                    }, 10);
                })
            });

            this.form.find('button[type="submit"]').click(function (e) {
                $this.send();
                return false;
            });
            this.form.addClass('init');
            return this;
        },

        getValues: function () {
            let fieldsets = this.form.find('input, textarea, select');
            let $this = this;
            let data = new FormData();
            let fields = {};
            fieldsets.each(function (i, e) {
                let name = $(this).attr('name');
                let value = $(this).val();
                if (typeof fields[name] === 'undefined') {
                    fields[name] = value;
                } else {
                    fields[name] = fields[name] + value;
                }
            });

            $.each(fields, function (i, e) {
                data.append(i, e);
            });

            this.fields = data;
            return this.fields;
        },

        validate: function () {
            this.valid = true;
            this.form.find('.invalid').removeClass('invalid');
            this.form.find('.error').removeClass('show');
            this.form.find('.error .text').html('');
            let $this = this;
            let fieldsets = this.form.find('input, textarea, select');
            fieldsets.each(function (i, e) {
                $this.validateInput($(this).closest('.area'));
            });
            return this.valid;
        },
        validateInput: function (el) {
            let $this = this;
            let fieldset = el.closest('.--fieldset');
            fieldset.removeClass('invalid');
            fieldset.find('.error').removeClass('show');
            fieldset.find('.error .text').html('');
            let fieldsets = fieldset.find('input, textarea, select');
            fieldsets.each(function (i, e) {
                let error_text = '';
                if ($(this).data('error') !== '') {
                    error_text = $(this).data('error');
                }
                if ($(this).val() === '') {
                    $this.valid = false;
                    $this.setError($(this), error_text);
                }
            });
        },

        send: function () {
            let $this = this;
            if (!this.validate()) {
                return false;
            }
            let data = this.getValues();
            $.ajax({
                url: this.url,
                method: 'POST',
                processData: false,
                contentType: false,
                dataType: 'json',
                data: data,
                beforeSend: function () {
                    $this.form.addClass('--progress');
                    try {
                        eval('ajax_before_' + this.id)();
                    } catch (e) {

                    }
                },
                success: function (response) {
                    $this.form.removeClass('--progress');
                    $this.success(response);
                },
                error: function (response) {
                    $this.form.removeClass('--progress');
                    $this.error(response);
                }
            });
        },
        setError: function (field, error_text = '') {
            let $field = field.closest('.--fieldset');
            $field.addClass('invalid');
            $field.find('.error .text').html(error_text);
            if (error_text !== '') {
                $field.find('.error').addClass('show');
            }
        },
    }
    return el.init(form);
}

let EazyFormsAlert = {
    get: function (style = '', title, message = false, icon = false, icon_style = false, actions = false) {
        var icon_html = '';
        var actions_html = '';
        var text_html = '';
        if (icon_style) {
            icon_style = '--' + icon_style;
        }
        if (typeof title === 'undefined') {
            title = 'Something wrong'
        }
        if (message) {
            if (typeof message === 'string') {
                var paragraphs = message.split('\n');
                $.each(paragraphs, function (i, e) {
                    text_html += '<p>' + e + '</p>';
                });
            }
        }
        if (icon) {
            icon_html = '<div class="icon">\n' +
                '          <div class="giftey--feature-icon ' + icon_style + ' --' + style + '">\n' +
                '              <div class="wrap">\n' +
                '                 <div class="icon">\n' +
                '                     <div class="giftey-icon icon--' + icon + '"></div>\n' +
                '                 </div>\n' +
                '              </div>\n' +
                '          </div>\n' +
                '       </div>';
        }
        if (actions) {
            actions_html = '<div class="actions">';
            $.each(actions, function (i, e) {
                actions_html += '<div class="item"><a href="' + e.url + '" class="' + e.class + '">' + e.text + '</a></div>';
            });
            actions_html += '</div>';
        }
        var template = '<div class="giftey--alert --' + style + '">\n' +
            '                                        <div class="--wrap">\n' +
            icon_html +
            '                                            <div class="content">\n' +
            '                                                <div class="title">' + title + '</div>\n' +
            text_html + actions_html +
            '                                            </div>\n' +
            '                                            <div class="close">\n' +
            '                                                <button role="button" type="button" class="ui-btn --tertiary --icon">\n' +
            '                                                    <span class="icon">\n' +
            '                                                        <span class="giftey-icon icon--x"></span>\n' +
            '                                                    </span>\n' +
            '                                                </button>\n' +
            '                                            </div>\n' +
            '                                        </div>\n' +
            '                                    </div>';
        return template;
    }
}
function _init_eazyforms() {
    $('.gifteyforms:not(.init)').each(function (i, e) {
        let id = 'gifteyforms_' + $(this).attr('id');
        if (!$(this).hasClass('--verification')) {
            window[id] = EazyForms($(this));
        } else {
            window[id] = EazyFormsVerification($(this));
        }
    });
}
jQuery(document).ready(function ($) {
    _init_eazyforms();
    $('body').on('click', '.giftey--alert .close button', function () {
        $(this).closest('.giftey--alert').remove();
    });

    $('input, textarea, select').focusout(function () {
        $(this).closest('.area').removeClass('focus');
    })

    $('body').on('click', '.--fieldset.checkbox .box.--box', function () {
        $(this).closest('.--fieldset').find('input').click();
    });
});