function Toggle(field, width, height) {
    // Try to get the FCKeditor instance, if available.
    var oEditor ;
    if (typeof(FCKeditorAPI) != 'undefined') oEditor = FCKeditorAPI.GetInstance('fck_'+field);

    // Get the _Textarea and _FCKeditor DIVs.
    var eTextareaDiv  = document.getElementById('div_'+field);
    var eFCKeditorDiv = document.getElementById('div_fck_'+field);

    // If the _Textarea DIV is visible, switch to FCKeditor.
    if (eTextareaDiv.style.display != 'none') {

        document.getElementById(field).value = text2html(document.getElementById(field).value);
        
        // If it is the first time, create the editor.
        if (!oEditor) CreateEditor(field, width, height); else {
            // Set the current text in the textarea to the editor.
            oEditor.SetData(document.getElementById(field).value);
        }

        // Switch the DIVs display.
        eTextareaDiv.style.display  = 'none';
        eFCKeditorDiv.style.display = '';

        // This is a hack for Gecko 1.0.x ... it stops editing when the editor is hidden.
        if (oEditor && !document.all) if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG) oEditor.MakeEditable();
        
    } else {
        // Set the textarea value to the editor value.
        document.getElementById(field).value = '<!--HTML-->\r\n'+htmlfix(oEditor.GetXHTML());

        // Switch the DIVs display.
        eTextareaDiv.style.display  = '';
        eFCKeditorDiv.style.display = 'none';
    }
}

function CreateEditor(field, width, height) {
    // Copy the value of the current textarea, to the textarea that will be used by the editor.
    document.getElementById('fck_'+field).value = document.getElementById(field).value;
    
    // Create an instance of FCKeditor (using the target textarea as the name).
    var oFCKeditor = new FCKeditor('fck_'+field);
    oFCKeditor.BasePath = '/inc/fckeditor/';
    oFCKeditor.Width = (width < 500 ? 500 : width);
    oFCKeditor.Height = (height < 350 ? 350 : height);
    oFCKeditor.ReplaceTextarea();
}

// The FCKeditor_OnComplete function is a special function called everytime an
// editor instance is completely loaded and available for API interactions.
function FCKeditor_OnComplete(editorInstance) {
    // Enable the switch button. It is disabled at startup, waiting the editor to be loaded.
    document.getElementById('btn_'+editorInstance.Name).disabled = false;
}

function PrepareSave(evt, field) {
    // If the textarea isn't visible update the content from the editor.
    if (document.getElementById('div_'+field).style.display == 'none') {
        var oEditor = FCKeditorAPI.GetInstance('fck_'+field);
        document.getElementById(field).value = '<!--HTML-->\r\n'+htmlfix(oEditor.GetXHTML());
    }
}
