var rich_sourceCode;

function rich_setFormat(id, action, value) {
    //actions available:
    //bold, underline, italic,...
    document.getElementById(id).contentWindow.document.execCommand(action, false, value);
    document.getElementById(id).contentWindow.focus();
}

function rich_switchView(id, state) {
    var htmtext;
    
    rich_sourceCode = state;
    if (document.all) {
        //---IE---
        if (state == true) {
            //source code view
            htmtext=document.getElementById(id).contentWindow.document.body.innerHTML;
            document.getElementById(id).contentWindow.document.body.innerText=htmtext;
        } else {
            //default view
            htmtext=document.getElementById(id).contentWindow.document.body.innerText;
            document.getElementById(id).contentWindow.document.body.innerHTML=htmtext;
        }
    } else {
        //---MOZILLA---
        if (state == true) {
            //source code view
            htmtext = document.createTextNode(document.getElementById(id).contentWindow.document.body.innerHTML);
            document.getElementById(id).contentWindow.document.body.innerHTML = "";
            document.getElementById(id).contentWindow.document.body.appendChild(htmtext);
        } else {
            //default view
            htmtext = document.getElementById(id).contentWindow.document.createRange();
            htmtext.selectNodeContents(document.getElementById(id).contentWindow.document.body);
            document.getElementById(id).contentWindow.document.body.innerHTML = htmtext.toString();
        }
    }
}

function rich_getSource(id) {
    var htmtext;
    
    if (rich_sourceCode==true) rich_switchView(id, false);
    htmtext=document.getElementById(id).contentWindow.document.body.innerHTML;
    return htmtext;
}    

function rich_init(id) {
    rich_sourceCode = false;
    
    document.getElementById(id).contentWindow.document.designMode="On";   
    try { // IE makes troubles
        document.getElementById(id).contentWindow.document.execCommand("useCSS", false, true);
        document.getElementById(id).contentWindow.document.execCommand("styleWithCSS", false, false);
    } catch (e) {}
}

function rich_write(id, text) {
    document.getElementById(id).contentWindow.document.open();
    document.getElementById(id).contentWindow.document.write(text);
    document.getElementById(id).contentWindow.document.close(); 
}

function rich_btn_hover(obj, state) {
    if (state==1) {
        obj.className="rich-btn-hover";
    } else {
        obj.className="rich-btn";
    }
}

