var format="HTML";

function setFocus()
{
    textEdit.focus();
}

function execCommand(command)
{
    textEdit.focus();
    if (format=="HTML")
    {
        var edit = textEdit.document.selection.createRange();
        if (arguments[1]==null)
        {
            edit.execCommand(command);
        }
        else
        {
            edit.execCommand(command,false, arguments[1]);
            edit.select();
            textEdit.focus();
        }
    }
}

function selectAllText()
{
    var edit = textEdit.document;
    edit.execCommand('SelectAll');
    textEdit.focus();
}

function newDocument()
{
    textEdit.document.open();
    textEdit.document.write("");
    textEdit.document.close();
    textEdit.focus();
    if(fontFlag == 0)
    {
        textEdit.document.body.innerHTML = textEdit.document.body.innerText;
        textEdit.document.body.style.fontFamily = "verdana";
        textEdit.document.body.style.fontSize = "8pt";
    }
    else
    {
        textEdit.document.body.innerHTML = textEdit.document.body.innerText;
        textEdit.document.body.style.fontFamily = "";
        textEdit.document.body.style.fontSize = "";
    }
}

function loadDoc(htmlString)
{
    textEdit.document.open();
    textEdit.document.write(htmlString);
    textEdit.document.close();
}

function initEditor()
{
    var htmlString = parent.document.all.EditorValue.value;
    textEdit.document.designMode = "On";
    textEdit.document.open();
    textEdit.document.write(htmlString);
    textEdit.document.close();
    textEdit.focus();
    //alert(fontFlag);
    if(fontFlag == 0)
    {
        textEdit.document.body.innerHTML = textEdit.document.body.innerText;
        textEdit.document.body.style.fontFamily = "verdana";
        textEdit.document.body.style.fontSize = "8pt";
    }
    else
    {
        textEdit.document.body.innerHTML = textEdit.document.body.innerText;
        textEdit.document.body.style.fontFamily = "";
        textEdit.document.body.style.fontSize = "";
    }
    testLoad();
}

function swapModes()
{
    if (format=="HTML")
    {
        textEdit.document.body.innerText = textEdit.document.body.innerHTML;
        textEdit.document.body.style.fontFamily = "monospace";
        textEdit.document.body.style.fontSize = "10pt";
        format="Text";
    }
    else
    {
        if(fontFlag == 0)
        {
            textEdit.document.body.innerHTML = textEdit.document.body.innerText;
            textEdit.document.body.style.fontFamily = "verdana";
            textEdit.document.body.style.fontSize = "8pt";
        }
        else
        {
            textEdit.document.body.innerHTML = textEdit.document.body.innerText;
            textEdit.document.body.style.fontFamily = "";
            textEdit.document.body.style.fontSize = "";
        }
        format="HTML";
    }
    
    var s = textEdit.document.body.createTextRange();
    s.collapse(false);
    s.select();
}

////////////////////////////////////////////////////////
// Called from initEditor()
// Fix function to paste the HTML from EditorValue
// into the main text field onLoad
// For some reason it doesn't work otherwise!!?!!
////////////////////////////////////////////////////////

function testLoad()
{
    var main = parent.document.frames("myEditor").document.frames("textEdit").document.body.createTextRange();
    var paste = parent.document.all.EditorValue.value;
    //alert(paste);
    main.pasteHTML(paste);
}

////////////////////////////////////////////////////////

window.onload = initEditor;
