function formatText(workWith, openTag, closeTag) {
    var msgfield = document.getElementById(workWith);

    if (msgfield && (typeof(msgfield) != 'undefined')) {
        // IE support
        if (document.selection && document.selection.createRange) {
            msgfield.focus();
            var sel = document.selection.createRange();
            if (!sel.text.length) {
                alert('Выделите текст, который вы желаете отформатировать.');
            } else {
                sel.text = openTag + sel.text + closeTag;
            }
            msgfield.focus();
        } else if (msgfield.selectionStart || msgfield.selectionStart == '0') {
            // Moz support
            var startPos = msgfield.selectionStart;
            var endPos = msgfield.selectionEnd;

            if (startPos == endPos) {
                alert('Выделите текст, который вы желаете отформатировать.');
            } else {
                    msgfield.value = msgfield.value.substring(0, startPos) + openTag + msgfield.value.substring(startPos, endPos) + closeTag + msgfield.value.substring(endPos, msgfield.value.length);
            }
            msgfield.selectionStart = msgfield.selectionEnd = endPos + openTag.length + closeTag.length;
            msgfield.focus();
        } else {
            // Fallback support for other browsers
            field.value += openTag + closeTag;
            field.focus();
        }
    }
}

function insertImg(workWith) {
    var imgUploader = window.open(location.href + '?upload_img=1&work_with='+workWith, 'img_uploader', 'width=400,height=300,top=400,left=300,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no');
    imgUploader.focus();
}

function pasteImage(workWith, path) {
    //alert(workWith + '|' + path);

    var code = '<img src="' + path + '" alt="" />';

    var field = document.getElementById(workWith);

    if (field && (typeof(field) != 'undefined')) {
        // IE support
        if (document.selection && document.selection.createRange) {
            field.focus();
            sel = document.selection.createRange();
            sel.text += code;
            field.focus();
        } else if (field.selectionStart || field.selectionStart == '0') {
            // Moz support
            var startPos = field.selectionStart;
            var endPos = field.selectionEnd;

            field.value = field.value.substring(0, endPos) + code + field.value.substring(endPos, field.value.length);
            field.selectionStart = field.selectionEnd = endPos + code.length;
            field.focus();
        } else {
            // Fallback support for other browsers
            field.value += code;
            field.focus();
        }
    }
}

function insertLink(workWith) {
    var field = document.getElementById(workWith);

    if (field && (typeof(field) != 'undefined')) {
        // IE support
        var url = prompt('Введите адрес ссылки:', '');
        if (url.indexOf('http://') === 0) {
            var openTag = '<a href="'+url+'" target="_blank">';
        } else {
            var openTag = '<a href="'+url+'">';
        }
        var closeTag = '</a>';

        if (document.selection && document.selection.createRange) {
            field.focus();
            sel = document.selection.createRange();
            if (!sel.text.length) {
                sel.text = prompt('Введите текст ссылки:', '');
            }
            sel.text = openTag + sel.text + closeTag;
            field.focus();
        } else if (field.selectionStart || field.selectionStart == '0') {
            // Moz support
            var startPos = field.selectionStart;
            var endPos = field.selectionEnd;

            if (startPos == endPos) {
                var txt = prompt('Введите текст ссылки:', '');
                field.value = field.value.substring(0, startPos) + openTag + txt + closeTag + field.value.substring(endPos, field.value.length);
            } else {
                field.value = field.value.substring(0, startPos) + openTag + field.value.substring(startPos, endPos) + closeTag + field.value.substring(endPos, field.value.length);
            }
            field.selectionStart = field.selectionEnd = endPos + openTag.length + closeTag.length;
            field.focus();
        } else {
            // Fallback support for other browsers
            var txt = prompt('Введите текст ссылки:', '');
            field.value += openTag + txt + closeTag;
            field.focus();
        }
    }
}
