/**
 * @copyright Internet Labs
 * @author Bartosz Błaszczyk
 * @version 2008.07.15.17.59
 */


var clientPC = navigator.userAgent.toLowerCase();
var is_gecko = ((clientPC.indexOf('gecko') != -1) &&
                (clientPC.indexOf('spoofer') == -1) &&
                (clientPC.indexOf('khtml') == -1) &&
                (clientPC.indexOf('netscape/7.0') == -1));


function focusTextarea(id)
{
    obj = document.getElementById(id);
    obj.focus();
    obj.value = obj.value + "";
    obj.scrollTop = obj.scrollHeight;
}


function insertTagsById(id, tagOpen, tagClose, sampleText)
{
    obj = document.getElementById(id);
    insertTagsByObject(obj, tagOpen, tagClose, sampleText);
}


function insertTagsByObject(obj, tagOpen, tagClose, sampleText)
{
    // IE
    if (document.selection && !is_gecko) {

        var selection = document.selection.createRange().text;
        if (selection == "") {
            selection = sampleText;
        }

        obj.focus();

        if (selection.charAt(selection.length - 1) == " ") {
            selection = selection.substring(0, selection.length - 1);
            document.selection.createRange().text = tagOpen + selection + tagClose + " ";
        } else {
            document.selection.createRange().text = tagOpen + selection + tagClose;
        }

    // Mozilla
    } else if (obj.selectionStart || (obj.selectionStart == '0')) {

        var replaced = false;
        var startPos = obj.selectionStart;
        var endPos = obj.selectionEnd;
        if ((endPos - startPos) > 0) {
            replaced = true;
        }

        var scrollTop = obj.scrollTop;
        var text = (obj.value).substring(startPos, endPos);
        if (text == "") {
            text = sampleText;
        }

        var subst;
        if (text.charAt(text.length - 1) == " ") {
            subst = tagOpen + text.substring(0, (text.length - 1)) + tagClose + " ";
        } else {
            subst = tagOpen + text + tagClose;
        }
        obj.value = obj.value.substring(0, startPos) + subst + obj.value.substring(endPos, obj.value.length);

        obj.focus();

        if (replaced) {
            var caretPos = startPos + (tagOpen.length + text.length + tagClose.length);
            obj.selectionStart = caretPos;
            obj.selectionEnd = caretPos;
        } else {
            obj.selectionStart = startPos + tagOpen.length;
            obj.selectionEnd = startPos + tagOpen.length + text.length;
        }

        obj.scrollTop = scrollTop;
    }

    if (obj.createTextRange) {
        obj.caretPos = document.selection.createRange().duplicate();
    }
}


function insertUrlTagsById(id, urlPromptText, anchorTextPromptText)
{
    obj = document.getElementById(id);

    var selectedAnchorText = "";

    // IE
    if (document.selection && !is_gecko) {
        selectedAnchorText = document.selection.createRange().text;

    // Mozilla
    } else if (obj.selectionStart || (obj.selectionStart == "0")) {

        var startPos = obj.selectionStart;
        var endPos = obj.selectionEnd;
        selectedAnchorText = (obj.value).substring(startPos, endPos);
    }

    var url = prompt(urlPromptText, "http://www.");

    if (!url || (url == null) || (url == "http://www.")) {
        return;
    }

    var anchorText = prompt(anchorTextPromptText, selectedAnchorText);

    if (!anchorText || (anchorText == null)) {
        return;
    }

    if (selectedAnchorText != "") {
        insertTagsById(id, "[url=" + url + "]", "[/url]", "");
    } else {
        insertTagsById(id, "[url=" + url + "]" + anchorText + "[/url]", "", "");
    }
}


function toggleBBPanel()
{
    obj = document.getElementById("bbCodePanelEmoticons");

    if (obj.style.display === 'none') {
        obj.style.display = 'block';
    } else {
        obj.style.display = 'none';
    }
}
