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;
    //display the right window
    document.getElementById(id+"_formated").style.display = (state)? "none":"";    
    document.getElementById(id+"_plaincode").style.display = (state)? "":"none";    
    setTimeout("rich_setup(\""+id+"\")", 100);    //initialize with delay!!!    
    
    //display
    if (state == true) {
        //code view
        htmtext=document.getElementById(id).contentWindow.document.body.innerHTML;
        document.getElementById(id+"_html").value = htmtext;        
    } else {
        //rich edit view
        htmtext = document.getElementById(id+"_html").value;
        document.getElementById(id).contentWindow.document.body.innerHTML = htmtext;
    }    
}

function rich_setup(id) {
    try { // IE makes troubles
        document.getElementById(id).contentWindow.document.execCommand("useCSS", false, true);
        document.getElementById(id).contentWindow.document.execCommand("styleWithCSS", false, false);
        document.getElementById(id).contentWindow.document.execCommand("enableInlineTableEditing", false, false);
    } catch (e) {}
}

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) {
    //initialize design mode
    rich_sourceCode = false;    
    document.getElementById(id).contentWindow.document.designMode="On";   
    document.getElementById(id).contentWindow.document.execCommand("undo", false, null);
    
    //switch to richedit view
    setTimeout("rich_switchView(\""+id+"\", 0)", 10);    //initialize with delay!!!
}

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";
    }
}

