function change_row(a) {
    var field = document.getElementsByClassName('field');
    for (var i = 0; i < field.length; i++) {
        field[i].style.display = "none";
    }
    field = document.getElementsByClassName('filter' + a);
    for (i = 0; i < field.length; i++) {
        field[i].style.display = "block";
    }
}

function add_new(a) {
    var tabmail = document.getElementById('tabmail');
    if (a) {
        tabmail.style.display = "table-row-group";
        var field = document.getElementsByClassName('field');
        for (var i = 0; i < field.length; i++) {
            field[i].style.display = "none";
        }
        field = document.getElementsByClassName('filter' + a);
        for (i = 0; i < field.length; i++) {
            field[i].style.display = "block";
        }
        var fmail = 'fmail' + a;
        fmail = document.getElementById(fmail);
        $('#tabmail1').val(fmail.value);
        $('#tabmail2').val('');
        $('#tabmail3').val('');
    } else {
        tabmail.style.display = "none";
        field = document.getElementsByClassName('field');
        for (i = 0; i < field.length; i++) {
            field[i].style.display = "none";
        }
    }
    init_chosen();
}

function add_new2(a) {
    if (a) {
        var field = document.getElementsByClassName('field');
        for (var i = 0; i < field.length; i++) {
            field[i].style.display = "none";
        }
        field = document.getElementsByClassName('filter' + a);
        for (i = 0; i < field.length; i++) {
            field[i].style.display = "block";
        }
        $('#tabmail1').val('');
        $('#tabmail2').val('');
        $('#tabmail3').val('');
    } else {
        field = document.getElementsByClassName('field');
        for (i = 0; i < field.length; i++) {
            field[i].style.display = "none";
        }
    }
}

function op(a) {
    var dop = document.getElementById('dop');
    var open_add_set = document.getElementById('open_add_set');
    var config_access_rules = document.getElementById('config_access_rules');
    var users = document.getElementById('users');
    var but_user = document.getElementById('but_user');
    if (a === 'add') {
        if (dop.style.display === 'none' || !dop.style.display) {
            dop.style.display = 'table-row-group';
            open_add_set.innerHTML = 'Скрыть';
            config_access_rules.innerHTML = 'Пользователи';
            users.style.display = 'none';
            but_user.style.display = 'none';
        } else {
            dop.style.display = 'none';
            open_add_set.innerHTML = 'Дополнительно';
            config_access_rules.innerHTML = 'Пользователи';
        }
    } else {
        if (users.style.display === 'none' || !users.style.display) {
            users.style.display = 'table-row-group';
            but_user.style.display = 'table-row-group';
            config_access_rules.innerHTML = 'Скрыть';
            open_add_set.innerHTML = 'Дополнительно';
            dop.style.display = 'none';
        } else {
            but_user.style.display = 'none';
            users.style.display = 'none';
            config_access_rules.innerHTML = 'Пользователи';
            open_add_set.innerHTML = 'Дополнительно';
        }
    }
}

function op2(a) {
    var tabmail = document.getElementById('tabmail');
    var open_add_set = document.getElementById('open_add_set');
    if (a === 'tab') {
        if (tabmail.style.display === 'none') {
            tabmail.style.display = 'block';
            open_add_set.innerHTML = 'Скрыть';
        } else {
            tabmail.style.display = "none";
            open_add_set.innerHTML = 'Дополнительно';
        }
    }
}

function checks(a) {
    var uss = document.getElementById('us' + a);
    var us = document.getElementById('uss');
    var all_us = document.getElementById('usall');
    if (!uss.checked) {
        var str = us.value;
        a = ";" + a + ";";
        str = str.replace(a, "");
        all_us.checked = false;
        us.value = str;
    } else {
        us.value = us.value + ";" + a + ";";
    }
}

function checkall(a) {
    var uss = document.getElementById('usall');
    var us = document.getElementById('uss');
    var check_class = document.getElementsByClassName('check_us');
    if (!uss.checked) {
        stat = false;
        us.value = '';
    } else {
        us.value = a;
        stat = true;
    }
    for (i = 0; i < check_class.length; i++) {
        check_class[i].checked = stat;
    }
}

function checkmail(a) {

    var em = document.getElementById('em');
    var c = ";" + em.value + ';';
    var alt = document.getElementById('alt');
    var butt = document.getElementById('butt');
    var us = document.getElementById('uss');
    if (a.indexOf(c) !== -1) {
        alt.innerHTML = "Аккаунт уже существует, введите другой";
        butt.setAttribute('disabled', 'disabled');
    } else {
        butt.removeAttribute("disabled");
        alt.innerHTML = "";
    }
    $.ajax({
        type: 'get',
        url: 'common.php?user_check=' + em.value,
        response: JSON,
        success: function (data) {
            user_row = document.getElementById('us' + data);
            user_row.checked = true;
            us.value = ';' + data + ';';
        }
    });
}

function op_user() {
    no_activ = document.getElementById('no_activ');
    but_user = document.getElementById('but_user');
    if (no_activ.style.display === 'none' || !no_activ.style.display) {
        no_activ.style.display = 'table-row-group';
        but_user.innerHTML = 'Скрыть';
    } else {
        no_activ.style.display = 'none';
        but_user.innerHTML = 'Показать не активных пользователей';
    }
}

function tips_check() {
    tip = document.getElementById('tip');
    tip_check = document.getElementById('tip_check');
    if (tip.value === 'on') {
        tip.value = '';
        tip_check.value = 'checked';
    } else {
        tip.value = 'on';
        tip_check.value = '';
    }
}

function checkmailimap(a) {
    $.ajax({
            type: 'get',
            url: 'common.php?check_mail_imap=' + a,
            response: JSON,
            success: function (data) {
                check_imap_mail = document.getElementById('check_imap' + a);
                if (data === '1') {
                    check_imap_mail.title = lang.module.mail.green_mail;
                    check_imap_mail.classList.remove('r_gr');
                    check_imap_mail.classList.add('r_g');
                } else {
                    if (data === '0') {
                        check_imap_mail.title = lang.module.mail.red_mail;
                        check_imap_mail.classList.remove('r_gr');
                        check_imap_mail.classList.add('r_r');
                    } else {
                        check_imap_mail.title = lang.module.mail.yellow_mail;
                        check_imap_mail.classList.remove('r_gr');
                        check_imap_mail.classList.add('r_y');
                    }
                }
            }
        }
    );
}

function set_cokkiess(disabled) {
    $.cookie('del_mails', disabled ? 1 : null);
}
