function insertBB(code, type) {
    let desc;
    let change;
    let end;
    let start;
    let selectedText;
    let obj;
    if (userMod == true) {
        obj = window.opener.document.getElementById(textareaName);
    }
    else {
        obj = gid(textareaName);
    }

    if (window.getSelection) {
        start = obj.selectionStart;
        end = obj.selectionEnd;
        selectedText = obj.value.substr(start, end - start);
    }
    else if (document.selection) {

        const txt = document.selection.createRange();
        const clone = txt.duplicate();
        const oldstr = txt.text;

        txt.collapse();
        clone.moveToElementText(obj);
        clone.setEndPoint('EndToEnd', txt);

        start = clone.text.length;
        end = start + oldstr.length;

        selectedText = obj.value.substr(start, end - start);
    }

    if (selectedText.length > 3) {
        change = true;
    }
    else {
        change = false;
    }

    if (code == 'image') {
        if (change == true) {
            insertCode('img');
        }
        else {
            desc = prompt('Адрес картинки:', 'http://');
            const align = prompt('Выравнивание(center|left|right):', 'center');
            if (desc) {
                insertCode('img', align, desc);
            }
        }
    }
    else if (code == 'url') {

        const url = prompt('Адрес:', 'http://');
        if (url) {
            if (change == true) {
                insertCode('url', url);
            }
            else {
                desc = prompt('Описание ссылки:', 'Перейти по адресу');
                if (!desc) {
                    desc = 'Перейти по адресу';
                }
                insertCode('url', url, desc);
            }
        }
        else {
            alert('Вы не ввели адрес сылки!');
        }
    }
    else if (code == 'mail') {
        const mail = prompt('Введите почту:', '');
        if (mail) {
            if (isValidEmail(mail)) {
                if (change == true) {
                    insertCode('email', mail);
                }
                else {
                    desc = prompt('Описание:', 'мой e-mail');
                    if (!desc) {
                        desc = 'мой e-mail';
                    }
                    insertCode('email', mail, desc);
                }
            }
            else {
                alert('E-Mail имеет неправильный формат (email@site.ru)!');
            }
        }
        else {
            alert('Вы не ввели адрес почты!');
        }
    }
    else if (type == 'smile') {
        insertCode('smile', code);
    }
    else {
        insertCode(code, type);
    }
    hideBBPanel();
}

function insertIN(code, module) {
    if (module != 'user') {
        gid(textareaName).value += code;
    }
    else {
        window.opener.document.getElementById(textareaName).value += code;
    }
}

function insertCode(code, add, simple) {
    let toAdd;
    let obj;
    let start, end;
    if (userMod == true) {
        obj = window.opener.document.getElementById(textareaName);
    }
    else {
        obj = gid(textareaName);
    }

    obj.focus();
    const miniCode = new Array('[hr]', 'smile', 'iwannamini');

    if (simple) {
        if ((code == 'img' || code == 'thumb') && add == 'center') {
            toAdd = '[center][' + code + ']' + simple + '[/' + code + '][/center]';
        } else if (code == 'b' && add == 'nick') {
            toAdd = '<' + code + '>' + simple + '</' + code + '>,&nbsp;';
        }
        else {
            toAdd = '[' + code + (add ? '=' + add : '') + ']' + simple + '[/' + code + ']';
        }
    }
    else {
        toAdd = '[' + code + (add ? '=' + add : '') + ']{val}[/' + code + ']';
    }

    if (in_array(miniCode, code)) {
        toAdd = add ? add : code;
    }

    if (window.getSelection) {
        start = obj.selectionStart;
        end = obj.selectionEnd;
        obj.value = obj.value.substr(0, start) + toAdd.replace('{val}', obj.value.substr(start, end - start)) + obj.value.substr(end);
        obj.setSelectionRange(start + code.length + 2 + (add ? add.length + 1 : ''), end + code.length + 2 + (add ? add.length + 1 : ''));

    }
    else if (document.selection) {

        const txt = document.selection.createRange();
        const clone = txt.duplicate();
        const oldstr = txt.text;

        txt.collapse();
        clone.moveToElementText(obj);
        clone.setEndPoint('EndToEnd', txt);

        start = clone.text.length;
        end = start + oldstr.length;

        obj.value = obj.value.substr(0, start) + toAdd.replace('{val}', obj.value.substr(start, end - start)) + obj.value.substr(end);

        if (oldstr) {
            clone.findText(oldstr);
            clone.select();
        }
        else {
            txt.moveStart("character", start + code.length + 2 + (add ? add.length + 1 : ''));
            txt.collapse();
            txt.select();
        }
    }

    tinymce.activeEditor.execCommand('mceInsertContent', false, obj.value);

}

function changeHeight(action) {
    let rows;
    const area = gid(textareaName);

    if (action == 'plus') {
        rows = +5;
    }
    else {
        rows = -5;
    }

    const checkRows = area.rows + rows;

    if (checkRows >= 5 && checkRows < 25) {
        area.rows = checkRows;
        return true;
    }
}

function QuickQuote(date, id) {
    const txt = '';
    window.txt = '';
    if (document.getSelection) {
        window.txt = document.getSelection()
    }
    else if (document.selection) {
        window.txt = document.selection.createRange().text;
    }
    if (window.txt == txt) {
        alert('Сначала выделите текст!');
    }
    else {
        tinymce.activeEditor.execCommand('mceInsertContent', false, '<!--quote--><div class="quote"><strong>Цитата:</strong><br />' + window.txt + '</div><!--quote:end--><br /><br />');
    }

}


function insert_tag(text) {
    if (tinyMCE && tinyMCE.activeEditor) {
        tinyMCE.activeEditor.selection.setContent('<div>' + text + '</div>');
    }
    return false;
}

function hideBBPanel() {
    if (document.getElementById("test") != null) {
        alert(123);
        if (gid(textareaName + '_codes').style.display !== "none") {
            gid(textareaName + '_codes').style.display = 'none';
        }

        if (gid(textareaName + '_smiles').style.display !== "none") {
            gid(textareaName + '_smiles').style.display = 'none';
        }

        if (gid(textareaName + '_fonts').style.display !== "none") {
            gid(textareaName + '_fonts').style.display = 'none';
        }
    }
}

function mainArea(name) {
    if (textareaName != name) {
        textareaName = name;
    }
}

function smiley(a) {
    doInsert(" " + a + " ", "", !1);
    ie_range_cache = null
}

function reply_topic(id) {
    gid('reply_to').value = id;
    gid('reply_topic').innerHTML = (id > 0 ? '<strong>Ответ на комментарий №:</strong> ' + id + ' <a href="javascript:void(0)" onclick="reply_topic(0);">не отвечать' : '');
    window.scroll(0, parseInt(gid('t_comment_add').offsetTop));
}
