// Get: commentDiv tag-id, authorName string, commentForm tag-id
// Insert qouted text from commentDiv into commentForm

function appendQuote(commentDiv, authorName, commentForm) {
    var _result = "";
    var node = document.getElementById(commentDiv);
    if (node == null) {
        return _result;
    }
    var childrens = node.childNodes;
    var i = 0;
    while (i < childrens.length) {
        var child = childrens.item(i);
        if (child.nodeType == 1 && child.tagName == 'P') {
            var childText = getElemText(child);
            if (childText) {
                if (_result) {
                    _result += "\n\n";
                }
                _result += childText;
            }
        }
        i++;
    }
    var node = document.getElementById(commentForm);
    var value = node.value;
    if (value != '') {
        value += "\n";
    }
    node.value = value+"[QUOTE=\""+authorName+"\"]"+_result+"[/QUOTE]\n";
    try {
        node.scrollIntoView(false);
        node.focus();
        return false;
    } catch (e) {
    }
    node.focus();
    return true;
}

// From: http://htmlcoder.visions.ru/JavaScript/?31
// Author: alshur
// Get: DOM node
// Return: plain text in node

function getElemText(node){
    return node.text || node.textContent || (function(node){
        var _result = "";
        if (node == null) {
            return _result;
        }
        var childrens = node.childNodes;
        var i = 0;
        while (i < childrens.length) {
            var child = childrens.item(i);
            switch (child.nodeType) {
                case 1: // ELEMENT_NODE
                    if (child.nodeName == 'BR') {
                        _result += "\n";
                    } else {
                        _result += arguments.callee(child);
                    }
                    break;
                case 5: // ENTITY_REFERENCE_NODE
                    _result += trim(arguments.callee(child));
                    break;
                case 3: // TEXT_NODE
                case 2: // ATTRIBUTE_NODE
                case 4: // CDATA_SECTION_NODE
                    _result += child.nodeValue;
                    break;
            }
            i++;
        }
        return trim(_result);
    }(node));
}

// From: http://www.somacon.com/p355.php "Javascript Trim LTrim and RTrim Functions"

function trim(str) {
    try {
        str = str.replace(/^\\s+|\\s+$/g,'');
    } catch(e) {
        for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
        str = str.substring(k, str.length);
        for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)); j--);
        str = str.substring(0,j+1);
    }
    return str;
}

function insertTag(tag, commentForm) {
    var node = document.getElementById(commentForm);
    node.focus();
    if (document.selection) {
        SelectedText = node.document.selection.createRange();
        SelectedText.text = '['+tag+']'+SelectedText.text+'[/'+tag+']';
    } else if (typeof(node.selectionStart)=="number") {
        // thx: http://www.tigir.com/javascript.htm
        var start = node.selectionStart;
        var end = node.selectionEnd;
        var rs = '['+tag+']'+node.value.substr(start,end-start)+'[/'+tag+']';
        node.value = node.value.substr(0,start)+rs+node.value.substr(end);
        var newpos = start + rs.length;
        node.setSelectionRange(newpos,newpos);
    } else {
        node.value += '['+tag+'][/'+tag+']';
    }
    return false;
}
