function showCharNo(source, oId) {
    obj    = window.document.getElementById(oId);
    source = window.document.getElementById(source);

    sourceLength = source.value.toString().length;

    // if(sourceLength > 1024) alert("Já ouviu falar em resumo?");

    obj.innerHTML = sourceLength + " caractéres";
}



function getSelectedText(obj) {
    obj = window.document.getElementById(obj);

    if(document.all) {
        return window.document.selection.createRange().text;
    }
    else if(obj.selectionStart || obj.selectionStart == '0') {
        return obj.value.substring(obj.selectionStart, obj.selectionEnd);
    }
    else {
        alert("Caramba... instala um browser decente!");
        return false;
    }
}



function setSelectedText(obj, newText, setFocus) {
    obj = window.document.getElementById(obj);
    
    if(document.all) {
        sel = window.document.selection.createRange();
        if(sel.text.length <= 0) return false;

        sel.text = newText;
        sel.select();
    }
    else if(obj.selectionStart || obj.selectionStart == '0') {
        _selectionStart = obj.selectionStart;
        _selectionEnd   = obj.selectionEnd;

        iniString = obj.value.substring(0, _selectionStart);
        endString = obj.value.substring(_selectionEnd, obj.value.length);
        obj.value = iniString + newText + endString;

        obj.focus();

        if(setFocus == 0) {
            obj.selectionStart = _selectionStart + newText.length;
            obj.selectionEnd   = _selectionStart + newText.length;
        }
        else {
            obj.selectionStart = _selectionStart;
            obj.selectionEnd   = _selectionStart + newText.length;
        }
    }
    else {
        alert("Caramba... instala um browser decente!");
        return false;
    }
}



function addLinkTag(objText) {
    url = prompt("Digite a URL", "http://", "Adicionar link");

    if(url == null) return false;

    _selectedText = getSelectedText(objText);

    link = '[link=' + url + ']' + _selectedText + '[/link]';

    setSelectedText(objText, link, 1);
    showCharNo('text', 'no');
}



function addTag(objText, tag) {
    switch(tag) {
        case "bold":
            iTag = '[b]';
            eTag = '[/b]';
            break;
        case "italic":
            iTag = '[i]';
            eTag = '[/i]';
            break;
        case "underline":
            iTag = '[u]';
            eTag = '[/u]';
            break;
    }

    _selectedText = getSelectedText(objText);
    newText = iTag + _selectedText + eTag;

    setSelectedText(objText, newText, 1);
    showCharNo('text', 'no');
}



function addSmile(objText, smile) {
    obj = window.document.getElementById(objText);
    smileTag = '[' + smile + ']';

    if(document.all) {
        obj.value = obj.value + smileTag;
    }
    else if(obj.selectionStart || obj.selectionStart == '0') {
        setSelectedText(objText, smileTag, 0);
    }
    else {
        alert("Que browser mais lixo!");
        return false;
    }
    showCharNo('text', 'no');
}



function addColorTag(objText, fromColor) {
    obj = window.document.getElementById(objText);

    color   = fromColor.style.backgroundColor;
    newText = '[' + color + ']' + getSelectedText(objText) + '[/' + color + ']';

    if(document.all) {
        setSelectedText(objText, newText);
    }
    else if(obj.selectionStart || obj.selectionStart == '0') {
        setSelectedText(objText, newText, 1);
    }
    else {
        alert("Que browser mais lixo!");
        return false;
    }
    showCharNo('text', 'no');
}