var Editor = null;
var gIsIE = document.all;

function setFocus(editorId) {
    // Sets focus to the editor directly after loading
    var finalContentHtmlId = editorId + '_txtContent';
    var temp;

    Editor = editorId + '_HtmlEdit';
    if (document.getElementById(Editor).contentDocument) {
        document.getElementById(Editor).contentDocument.designMode = 'on';
        document.getElementById(Editor).contentWindow.focus();

    }
    else {
        temp = document.getElementById(finalContentHtmlId).value;
        document.frames[Editor].document.designMode = 'on';
        document.frames[Editor].focus();
        window.frames[Editor].document.open();
        window.frames[Editor].document.clear();
        window.frames[Editor].document.write(temp);
        //window.frames[Editor].document.body.innerHTML = temp;
        window.frames[Editor].document.close();
    }
}
function SetEditor(val) {
    document.getElementById(Editor).contentWindow.focus();
    document.getElementById(Editor).contentWindow.document.execCommand(val)
}
function EditorTransForm(editorId, e) {
    var finalContentHtmlId = editorId + '_txtContent';
    var temp;
    switch (e) {
        case "Html":
            document.getElementById(editorId + "_ModeImg").style.display = "none";
            document.getElementById(editorId + "_HtmlBtn").className = "EditorBtnOn";
            document.getElementById(editorId + "_TextBtn").className = "EditorBtnOff";
            document.getElementById(finalContentHtmlId).style.display = "";
            document.getElementById(Editor).style.display = "none";
            document.getElementById(finalContentHtmlId).focus();
            break;
        case "Text":
            document.getElementById(editorId + "_ModeImg").style.display = "";
            document.getElementById(editorId + "_HtmlBtn").className = "EditorBtnOff";
            document.getElementById(editorId + "_TextBtn").className = "EditorBtnOn";
            temp = document.getElementById(finalContentHtmlId).value;
            window.frames[Editor].document.open();
            window.frames[Editor].document.clear();
            window.frames[Editor].document.write("<html><head><style>body{font-size:12px;font-family:verdana;margin:0px}p{margin:0px}</style></head><body></body></html>");
            window.frames[Editor].document.body.innerHTML = temp;
            window.frames[Editor].document.close()
            document.getElementById(finalContentHtmlId).style.display = "none";
            document.getElementById(Editor).style.display = "";
            break;
    }
}
function EditorSizePlus() {
    var _height = document.getElementById(Editor).style.height.replace("px", "");
    var __height = Number(_height) + 50;

    document.getElementById(Editor).style.height = __height + "px";
}

function EditorSizeMinus() {
    var _height = document.getElementById(Editor).style.height.replace("px", "");

    if (Number(_height) > 50) {
        var __height = Number(_height) - 50;

        document.getElementById(Editor).style.height = __height + "px";
    }
}
//运行代码
function EditorrunEx() {
    var code = window.frames[Editor].document.documentElement.outerHTML;
    if (code != "") {
        var newwin = window.open('', '', '');
        newwin.opener = null
        newwin.document.write("" + code + "");
        newwin.document.close();
    }
}
function CloneText(editorId) {
    var finalContentHtmlId = editorId + '_txtContent';
    if (document.getElementById(Editor).contentDocument) {
        if (document.getElementById(Editor).contentDocument.body != null) {
            // Copy the text into the first hidden field
            //document.getElementById(finalContentTxtId).textContent = document.getElementById(Editor).contentWindow.document.body.textContent;
            // Copy the html into the other hidden field
            document.getElementById(finalContentHtmlId).value = document.getElementById(Editor).contentWindow.document.documentElement.outerHTML;
        }
    }
    else {
        if (document.frames[Editor].document.body != null) {
            // Copy the text into the first hidden field
            //document.getElementById(finalContentTxtId).innerText = document.frames[Editor].document.body.innerText;
            // Copy the html into the other hidden field
            //window.frames[Editor].document.documentElement.outerHTML
            document.getElementById(finalContentHtmlId).value = document.frames[Editor].document.documentElement.outerHTML;
        }
    }
}

function ChangeFont(editorId, FontType, FontValue) {
    // Get the Iframe Id.
    if (document.getElementById(Editor).contentDocument) {
        document.getElementById(Editor).contentWindow.focus();
        // Change the font size.
        document.getElementById(Editor).contentDocument.execCommand(FontType, 0, FontValue);
        // Set the focus back to the Text Editor.
        document.getElementById(Editor).contentWindow.focus();
    }
    else {
        document.frames[Editor].focus();
        // Change the font size.
        document.frames[Editor].document.execCommand(FontType, 0, FontValue);
        // Set the focus back to the Text Editor.
        document.frames[Editor].focus();
    }
}

function createLink() {
    var sURL = window.prompt("请输入网站地址:", "http://");
    if ((sURL != null) && (sURL != "http://")) {
        format("CreateLink", sURL);
    }
}

function createImg() {
    var sPhoto = prompt("请输入图片位置:", "http://");
    if ((sPhoto != null) && (sPhoto != "http://")) {
        format("InsertImage", sPhoto);
    }
}

function format(type, para) {
    if (document.getElementById(Editor).contentDocument) {
        document.getElementById(Editor).contentDocument.execCommand(type, false, para);
        document.getElementById(Editor).contentWindow.focus();
    }
    else {
        document.frames[Editor].focus();
        document.frames[Editor].document.execCommand(type, false, para);
        document.frames[Editor].focus();
    }
}