var browser = "unknown";
var browserVersion = 0;
var toolSize = 160;
var margin = 5;
var presetsAvailable = new Array();
var goWebSpotURL = "http://www.data.jumpwebspot.com/";
var editorPath = "";
var pageName = "";

function HTMLEditorChangeNewlines(editorName)
{
    var iframe = document.getElementById(editorName + "_IFrame");
    var e = iframe.contentWindow.event;
    var range, rangeText;
    var brIndex;
    
    if(e.keyCode == 13)
    {
        //see if there's a br tag just before the selection
        if(iframe.contentWindow)
        {
            range = iframe.contentWindow.document.selection.createRange();
        }
        else
        {
            range = iframe.document.selection.createRange();
        }
        range.moveStart("character", -1);
        rangeText = range.htmlText.toLowerCase();
        if((brIndex = rangeText.indexOf("<br>")) >= 0 || (brIndex = rangeText.indexOf("<br/>")) >= 0 || (brIndex = rangeText.indexOf("<br />")) >= 0)
        {
            //get rid of the <br> and continue on
            range.moveStart("character", brIndex);
            range.collapse(true);
            range.moveEnd("character", 1); //rangeText.indexOf(">", brIndex));
            range.select();
            iframe.contentWindow.document.execCommand("delete", false, 0);
            //let IE start a new paragraph
        }
        else
        {
            //insert a <br /> instead
            HTMLEditorInsertHTML(editorName, "<br />");
            range = iframe.contentWindow.document.selection.createRange();
            range.collapse(true);
            range.select();
            return false;
        }
    }
    return true;
}

function HTMLEditorDisplayColorName(editorName, tableID, colorName)
{
    document.getElementById(editorName + tableID + "ColorName").innerHTML = (colorName != "" ? colorName : "Pick a color");
}

function HTMLEditorColorPalette(editorName, tableID, tableStyle, tableTitle, functionToCall)
{
    var outputHTML = "";
    var colorBases = "0369cf";
    var colors = new Array();
    var numColors = 0;
    var colorsPerRow = 0;
    var numSimple = 18;
    
    //add simple colors
    colors[0] = "Black";
    colors[1] = "Gray";
    colors[2] = "DarkRed";
    colors[3] = "SaddleBrown";
    colors[4] = "GoldenRod";
    colors[5] = "DarkGreen";
    
    colors[6] = "Navy";
    colors[7] = "Purple";
    colors[8] = "DeepPink";
    colors[9] = "White";
    colors[10] = "Silver";
    colors[11] = "Red";
    
    colors[12] = "Orange";
    colors[13] = "Yellow";
    colors[14] = "Green";
    colors[15] = "Blue";
    colors[16] = "Magenta";
    colors[17] = "Bisque";
    
    numColors += 18;
    
    //generate a color table
    for(var r=0; r<colorBases.length; ++r)
    {
        for(var g=0; g<colorBases.length; ++g)
        {
            for(var b=0; b<colorBases.length; ++b)
            {
                colors[numColors] = colorBases.charAt(r) + colorBases.charAt(r) + colorBases.charAt(g) + colorBases.charAt(g) + colorBases.charAt(b) + colorBases.charAt(b);
                numColors++;
            }
        }
    }
    colorsPerRow = colorBases.length * 3;
    
    //output the colors
    outputHTML = "<div id=\"" + editorName + tableID + "\" style=\"background-color: #999; border: 1px solid #000000;" + tableStyle + "\">";
    outputHTML += "<table cellpadding=\"0\" cellspacing=\"0\">";
    outputHTML += "<tr><td style=\"text-align: left; padding-left: 4px; color: #fff; font-family: sans-serif; font-size: 70%;\" colspan=\"" + (colorsPerRow-5) + "\">" + tableTitle + "</td><td style=\"text-align: right;\" colspan=\"5\"><a style=\"font-family: sans-serif; font-size: 70%; text-decoration: none; color: #fff; padding-bottom: 4px; padding-right: 4px; padding-left: 4px;\" href=\"javascript: void(0);\" onclick=\"HTMLEditorPopupPalette('" + editorName + "', '" + tableID + "'); return false;\">x</a></td></tr>";
    for(var i=0; i<numColors; ++i)
    {
        if(i % colorsPerRow == 0)
        {
            //end of a row
            if(i > 0)
            {
                outputHTML += "</tr>";
            }
            if(i == numSimple)
            {
                outputHTML += "<tr><td colspan=\"" + colorsPerRow + "\">&nbsp;</td></tr>";
            }
            outputHTML += "<tr>";
        }
        if(i < numSimple)
        {
            if(i % (colorsPerRow / 3) == 0 && i % colorsPerRow != 0)
            {
                outputHTML += "</tr><tr>";
            }
            outputHTML += "<td colspan=\"3\" style=\"background-color: " + colors[i] + ";\"><a href=\"javascript: void(0);\" style=\"text-decoration: none; display: block; width: 21px; height: 14px; font-size: 14px; line-height: 14px;\" onmouseover=\"HTMLEditorDisplayColorName('" + editorName + "', '" + tableID + "', '" + colors[i] + "');\" \" onclick=\"" + functionToCall + "('" + editorName + "', '" + colors[i] + "'); HTMLEditorPopupPalette('" + editorName + "', '" + tableID + "'); return false;\">&nbsp;</a></td>";
        }
        else
        {
            outputHTML += "<td style=\"background-color: #" + colors[i] + ";\"><a href=\"javascript: void(0);\" style=\"text-decoration: none; display: block; width: 7px; height: 7px; font-size: 7px; line-height: 7px;\" onmouseover=\"HTMLEditorDisplayColorName('" + editorName + "', '" + tableID + "', 'RGB #" + colors[i] + "');\" \" onclick=\"" + functionToCall + "('" + editorName + "', '#" + colors[i] + "'); HTMLEditorPopupPalette('" + editorName + "', '" + tableID + "'); return false;\">&nbsp;</a></td>";
        }
    }
    outputHTML += "</tr></table>";
    outputHTML += "<div id=\"" + editorName + tableID + "ColorName\" style=\"background-color: #000; font-size: small; font-family: monospace; color: #fff;\">Pick a color</div>";
    outputHTML += "</div>";
    
    return outputHTML;
}

function HTMLEditorDisplay(outputDiv, editorName, inHTML)
{
    var outputEl, browserDetectIFrame;
    var htmlToEdit = "";
    var editorHTML = "", toolsHTML = "";
    var offBG = "#6a970d url('http://www.data.jumpwebspot.com/images/buttons/20x20greenoff.jpg') repeat-x";
    var onBG = "#ffff0d url('http://www.data.jumpwebspot.com/images/buttons/20x20greenon.jpg') repeat-x"
    var btnText = "width: 140px; line-height: 22px; margin-bottom: 4px; border: 1px solid #360; text-decoration: none; font-family: Arial, Verdana, sans-serif; font-size: 75%; font-weight: bold; color: #fff; background: "+offBG;
    var versionStart;
    
    //display an HTML editor to the div with id specified as a parameter
    if(outputEl = document.getElementById(outputDiv))
    {
        //figure out which browser is being used
        if(document.designMode)
        {
            /*-----TO-DO-----
            Inprove this section - this is really REALLY basic
            ---------------*/
            //supports designMode, at least to a certain extent
            if(navigator.appName.indexOf("Microsoft") >= 0)
            {
                browser = "ie";
                versionStart = navigator.appVersion.indexOf("MSIE ");
                if(versionStart >= 0)
                {
                    versionStart += ("MSIE ").length;
                    browserVersion = navigator.appVersion.charAt(versionStart) * 1;
                }
            }
            else if(navigator.userAgent && navigator.userAgent.indexOf("AppleWebKit") >= 0)
            {
                browser = "webkit";
            }
            else if(navigator.appName.indexOf("Netscape") >= 0)
            {
                //probably firefox
                browserDetectIFrame = document.createElement("iframe");
                if(browserDetectIFrame)
                {
                    browserDetectIFrame.style.position = "absolute";
                    browserDetectIFrame.style.left = "-1000px";
                    browserDetectIFrame.style.top = "-1000px";
                    browserDetectIFrame.style.width = "1px";
                    browserDetectIFrame.style.height = "1px";
                    browserDetectIFrame = document.body.insertBefore(browserDetectIFrame, null);
                    if(browserDetectIFrame)
                    {
                        try
                        {
                            browserDetectIFrame.contentWindow.document.designMode = "on";
                            browserDetectIFrame.contentWindow.document.execCommand("Undo", false, "");
                            browser = "mozilla";
                        }
                        catch(e)
                        {
                            browser = "unknown";
                        }
                        browserDetectIFrame.parentNode.removeChild(browserDetectIFrame);
                    }
                }
            }
            else if(navigator.appName.indexOf("Opera") >= 0)
            {
                browser = "opera";
            }
        }
        //display a different style of editor depending on the browser used
        if(inHTML)
        {
            htmlToEdit = inHTML;
        }
        else
        {
            htmlToEdit = outputEl.innerHTML;
        }
        if(htmlToEdit == "")
        {
            htmlToEdit = "\n\t\n";
        }
        if(htmlToEdit.toLowerCase().indexOf("<form") >= 0 || htmlToEdit.toLowerCase().indexOf("</form") >= 0 || htmlToEdit.toLowerCase().indexOf("<iframe") >= 0)
        {
            browser = "unknown";
            toolsHTML = "Welcome to the HTML Editor!  The reason you're seeing this window instead of the normal graphical editor is that there are elements in this field that the graphical editor does not support.  Reasons for this could be:<br /><ul><li>It contains a form</li><li>It contains an IFrame</li></ul>";
        }
        outputEl.innerHTML = "";
        switch(browser)
        {
            case "ie":
            case "mozilla":
            case "opera":
            case "webkit":
            {
                editorHTML = "<iframe style=\"width: " + (browser == "ie" && browserVersion < 7 ? "auto" : "100%") + "; height: 100%;\" id=\"" + editorName + "_IFrame\" src=\"" + window.location.href + (window.location.href.indexOf("?") >= 0 ? "&amp;" : "?") + "HTMLEditorBlank=yes\" onload=\"HTMLEditorActivate('" + editorName + "');\">Please upgrade to a newer browser</iframe>";
                toolsHTML = "<fieldset id=\"" + editorName + "GraphicModeTools\" style=\"width: 100%; text-align: center; padding-bottom: 5px;\"" + (browser != "webkit" ? " onmousedown=\"return false;\"" : "") + " onselectstart=\"return false;\"><legend>Text tools</legend>";
                toolsHTML += "<select id=\"" + editorName + "TextStyle\" style=\"width: 142px; margin-bottom: 4px;\" onclick=\"HTMLEditorChangeTextStyle('" + editorName + "')\" onchange=\"HTMLEditorChangeTextStyle('" + editorName + "')\">";
                toolsHTML += "<option value=\"p\" selected=\"selected\">Normal text</option>";
                toolsHTML += "<option value=\"h1\">Main heading</option>";
                toolsHTML += "<option value=\"h2\">Subheading</option>";
                toolsHTML += "<option value=\"h3\">Sub-subheading</option>";
                toolsHTML += "<option value=\"address\">Address</option>";
                toolsHTML += "</select>";
                toolsHTML += "<select id=\"" + editorName + "TextFont\" style=\"width: 142px; margin-bottom: 4px;\" onclick=\"HTMLEditorChangeTextFont('" + editorName + "')\" onchange=\"HTMLEditorChangeTextFont('" + editorName + "')\">";
                toolsHTML += "<option value=\"Arial\" style=\"font-family: Arial, Helvetica, sans-serif;\">Arial</option>";
                toolsHTML += "<option value=\"Comic Sans\" style=\"font-family: Comic Sans, Comic Sans MS, sans-serif;\">Comic Sans</option>";
                toolsHTML += "<option value=\"Georgia\" style=\"font-family: Georgia, serif;\">Georgia</option>";
                toolsHTML += "<option value=\"Monospace\" style=\"font-family: Courier New, Courier, monospace;\">Monospace</option>";
                toolsHTML += "<option value=\"Times New Roman\" style=\"font-family: Times New Roman, Times, serif\" selected=\"selected\">Times New Roman</option>";
                toolsHTML += "<option value=\"Trebuchet\" style=\"font-family: Trebuchet, Verdana, sans-serif\">Trebuchet</option>";
                toolsHTML += "<option value=\"Verdana\" style=\"font-family: Verdana, Trebuchet, sans-serif\">Verdana</option></select>";
                toolsHTML += "<select id=\"" + editorName + "TextSize\" style=\"width: 142px; margin-bottom: 4px;\" onclick=\"HTMLEditorChangeTextSize('" + editorName + "');\" onchange=\"HTMLEditorChangeTextSize('" + editorName + "');\"><option value=\"1\">XX-Small</option><option value=\"2\">Small</option><option value=\"3\" selected=\"selected\">Medium size</option><option value=\"4\">Large</option><option value=\"5\">X-Large</option><option value=\"6\">XX-Large</option></select>";
                toolsHTML += "<br />";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorBoldText('" + editorName + "'); return false;\"><img id=\"" + editorName + "Bold\" src=\"" + goWebSpotURL + "images/htmleditor/boldoutoff.jpg\" style=\"border: 0\" alt=\"Bold\" title=\"Bold\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorItalicText('" + editorName + "'); return false;\"><img id=\"" + editorName + "Italic\" src=\"" + goWebSpotURL + "images/htmleditor/italicoutoff.jpg\" style=\"border: 0\" alt=\"Italic\" title=\"Italic\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorUnderlineText('" + editorName + "'); return false;\"><img id=\"" + editorName + "Underline\" src=\"" + goWebSpotURL + "images/htmleditor/underlineoutoff.jpg\" style=\"border: 0\" alt=\"Underline\" title=\"Underline\" /></a>";
                toolsHTML += "&nbsp;&nbsp;";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorAlign('" + editorName + "', 'Left'); return false;\"><img id=\"" + editorName + "AlignLeft\" src=\"" + goWebSpotURL + "images/htmleditor/aleftoutoff.jpg\" style=\"border: 0\" alt=\"Left justify\" title=\"Left justify\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorAlign('" + editorName + "', 'Center'); return false;\"><img id=\"" + editorName + "AlignCenter\" src=\"" + goWebSpotURL + "images/htmleditor/acenteroutoff.jpg\" style=\"border: 0\" alt=\"Center justify\" title=\"Center justify\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorAlign('" + editorName + "', 'Right'); return false;\"><img id=\"" + editorName + "AlignRight\" src=\"" + goWebSpotURL + "images/htmleditor/arightoutoff.jpg\" style=\"border: 0\" alt=\"Right justify\" title=\"Right justify\" /></a>";
                toolsHTML += "<br />";
                toolsHTML += "<div style=\"position: relative;\">";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorPopupPalette('" + editorName + "', 'FGPalette'); return false;\"><img id=\"" + editorName + "TextColor\" src=\"" + goWebSpotURL + "images/htmleditor/textcoloroutoff.jpg\" style=\"border: 0\" alt=\"Text color\" title=\"Text color\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorPopupPalette('" + editorName + "', 'BGPalette'); return false;\"><img id=\"" + editorName + "TextHilite\" src=\"" + goWebSpotURL + "images/htmleditor/texthiliteoutoff.jpg\" style=\"border: 0\" alt=\"Highlight color\" title=\"Highlight color\" /></a>";
                toolsHTML += "&nbsp;";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorToggleOrderedList('" + editorName + "'); return false;\"><img id=\"" + editorName + "OrderedList\" src=\"" + goWebSpotURL + "images/htmleditor/oloutoff.jpg\" style=\"border: 0\" alt=\"Ordered list\" title=\"Ordered list\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorToggleUnorderedList('" + editorName + "'); return false;\"><img id=\"" + editorName + "UnorderedList\" src=\"" + goWebSpotURL + "images/htmleditor/uloutoff.jpg\" style=\"border: 0\" alt=\"Unordered list\" title=\"Unordered list\" /></a>";
                toolsHTML += "&nbsp;";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorOutdentText('" + editorName + "'); return false;\"><img id=\"" + editorName + "Outdent\" src=\"" + goWebSpotURL + "images/htmleditor/outdentoutoff.jpg\" style=\"border: 0\" alt=\"Outdent\" title=\"Outdent\" /></a>";
                toolsHTML += "<a href=\"javascript: void(0);\" onclick=\"HTMLEditorIndentText('" + editorName + "'); return false;\"><img id=\"" + editorName + "Indent\" src=\"" + goWebSpotURL + "images/htmleditor/indentoutoff.jpg\" style=\"border: 0\" alt=\"Indent\" title=\"Indent\" /></a>";
                toolsHTML += HTMLEditorColorPalette(editorName, "FGPalette", "position: absolute; left: 0px; top: 22px; display: none;", "text color", "HTMLEditorSetTextColor");
                toolsHTML += HTMLEditorColorPalette(editorName, "BGPalette", "position: absolute; left: 22px; top: 22px; display: none;", "highlight color", "HTMLEditorSetHighlightColor");
                toolsHTML += "</div>";
                toolsHTML += "<a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorViewSource('" + editorName + "'); return false;\">edit html</a>";
                toolsHTML += "<a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorCleanUp('" + editorName + "'); return false;\">clean up text</a>";
                toolsHTML += "</fieldset><br /><br />";
                toolsHTML += "<fieldset id=\"" + editorName + "InsertTools\" style=\"width: 100%; text-align: center;\"><legend>Insert</legend>";
                toolsHTML += "<ul style=\"list-style: none; margin: 0; padding: 0; width: 142px;\">";
                toolsHTML += "<li><a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorInsertLink('" + editorName + "'); return false;\">insert link</a></li>";
                toolsHTML += "<li><a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorInsertPicture('" + editorName + "'); return false;\">insert picture</a></li>";
                toolsHTML += "<li><a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorInsertHR('" + editorName + "'); return false;\">insert line</a></li>";
                toolsHTML += "<li><a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorInsertTable('" + editorName + "'); return false;\">insert table</a></li>";
                //toolsHTML += "<li><a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorInsertPreset('" + editorName + "'); return false;\">insert text idea</a></li>";
                toolsHTML += "</ul>";
                toolsHTML += "</fieldset>";
                toolsHTML += "<fieldset id=\"" + editorName + "SourceModeTools\" style=\"display: none; width: 100%; text-align: center;\">";
                toolsHTML += "<legend>html editor</legend>";
                toolsHTML += "<p>You are currently in HTML edit mode.  If you want to go back to the graphical editor, click the green button below.</p>";
                toolsHTML += "<a href=\"javascript: void(0);\" style=\"display: block;" + btnText + "\" onmouseover=\"this.style.background = &quot;" + onBG + "&quot;\" onmouseout=\"this.style.background = &quot;" + offBG + "&quot;\" onclick=\"HTMLEditorViewGraphics('" + editorName + "'); return false;\">edit text</a>";
                toolsHTML += "</fieldset>";
                editorHTML += "<textarea style=\"display: none; width: " + (browser == "ie" && browserVersion < 7 ? "auto" : "100%") + "; height: 100%;\" name=\"" + editorName + "\" id=\"" + editorName + "\" cols=\"60\" rows=\"20\">" + htmlToEdit + "</textarea>";
                setTimeout("HTMLEditorActivate('" + editorName + "');", 1000);
            } break;
            default:
            {
                editorHTML = "<textarea style=\"width: " + (outputEl.clientWidth - toolSize - margin * 4) + "px; height: " + (outputEl.clientHeight - margin * 2 - 8) + "px;\" name=\"" + editorName + "\" id=\"" + editorName + "\" cols=\"60\" rows=\"20\">" + htmlToEdit + "</textarea>";
                if(toolsHTML == "")
                {
                    toolsHTML = "Welcome to the text editor!  You can edit your text in the box on the left.  For more options, like bold, underline, and fonts, please use a newer browser.";
                }
                if(loadingEl = document.getElementById(editorName + "Loading"))
                {
                    loadingEl.style.display = "none";
                }
            } break;
        }
        //output the html
        outputEl.innerHTML = "<div style=\"height: 100%;\"><div style=\"float: right; width: " + (toolSize - (browser == "mozilla" ? 20 : 0)) + "px;\">" + toolsHTML + "</div><div style=\"margin-right: " + (toolSize - (browser == "mozilla" ? 20 : 0) + margin * 2) + "px; height: 100%;\"><div style=\"height: 100%;\">" + editorHTML + "</div></div></div>";
    }
}

function HTMLEditorSetEditorPath(editorName, newPath)
{
    editorPath = newPath;
}

function HTMLEditorActivate(editorName)
{
    var iframeEl;
    
    //try to activate the specified iframe for editing
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        //activate it
        try
        {
            iframeEl.contentWindow.document.designMode = "on";
            if(browser == "ie" || browser == "opera")
            {
                //iframeEl.document.onkeydown = "HTMLEditorChangeNewlines('" + editorName + "', event.keyCode);";
                iframeEl.contentWindow.document.attachEvent("onkeypress", function() { return eval("HTMLEditorChangeNewlines('" + editorName + "');"); });
            }
            else if(browser == "mozilla")
            {
                iframeEl.contentWindow.document.execCommand("bold", false, null);
                iframeEl.contentWindow.document.execCommand("undo", false, null);
            }
            setTimeout("HTMLEditorUpdateIFrame('" + editorName + "');", 1500);
            //start the update cycle
            HTMLEditorUpdateButtons(editorName);
        }
        catch(e)
        {
            //try it again later
            setTimeout("HTMLEditorActivate('" + editorName + "');", 100);
        }
    }
    else
    {
        //try again in another tenth of a second
        setTimeout("HTMLEditorActivate('" + editorName + "');", 100);
    }
}

function HTMLEditorUpdateIFrame(editorName)
{
    var iframeEl, sourceEl, loadingEl;
    
    //update the source code
    iframeEl = document.getElementById(editorName + "_IFrame");
    sourceEl = document.getElementById(editorName);
    //if(iframeEl && iframeEl.contentWindow && iframeEl.contentWindow.document && iframeEl.contentWindow.document.body && iframeEl.contentWindow.document.body.innerHTML && sourceEl && sourceEl.value)
    {
        if(iframeEl.contentWindow.document.designMode.toLowerCase() == "on")
        {
            if(iframeEl.contentWindow.document.body)
            {
                iframeEl.contentWindow.document.body.innerHTML = sourceEl.value;
            }
            else
            {
                iframeEl.contentWindow.innerHTML = sourceEl.value;
            }
            if(browser == "mozilla")
            {
                //execute a command to finalize editing in FireFox
                iframeEl.contentWindow.document.execCommand("bold", false, null);
                iframeEl.contentWindow.document.execCommand("undo", false, null);
            }
            if(loadingEl = document.getElementById(editorName + "Loading"))
            {
                loadingEl.style.display = "none";
            }
        }
        else
        {
            HTMLEditorActivate(editorName);
        }
    }
}

function HTMLEditorUpdateSource(editorName)
{
    var iframeEl, sourceEl;
    
    //update the source code
    iframeEl = document.getElementById(editorName + "_IFrame");
    sourceEl = document.getElementById(editorName);
    if(sourceEl.style.display && sourceEl.style.display.toLowerCase() == "block")
    {
        return;
    }
    if(iframeEl && iframeEl.contentWindow && iframeEl.contentWindow.document && iframeEl.contentWindow.document.body && iframeEl.contentWindow.document.body.innerHTML && sourceEl && sourceEl.value)
    {
        sourceEl.value = iframeEl.contentWindow.document.body.innerHTML;
    }
}

function HTMLEditorChangeTextStyle(editorName)
{
    var iframeEl, textStyle;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    textStyle = document.getElementById(editorName + "TextStyle");
    if(iframeEl && textStyle)
    {
        iframeEl.contentWindow.document.execCommand("FormatBlock", false, "<" + textStyle.options[textStyle.selectedIndex].value + ">");
    }
}

function HTMLEditorChangeTextFont(editorName)
{
    var iframeEl, textFont, newFont = "";
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    textFont = document.getElementById(editorName + "TextFont");
    if(iframeEl && textFont)
    {
        switch(textFont.options[textFont.selectedIndex].value)
        {
            case "Arial":
            {
                newFont = "Arial, Helvetica, sans-serif";
            } break;
            case "Comic Sans":
            {
                newFont = "Comic Sans, Comic Sans MS, sans-serif";
            } break;
            case "Georgia":
            {
                newFont = "Georgia, serif";
            } break;
            case "Monospace":
            {
                newFont = "Courier New, Courier, monospace";
            } break;
            case "Times New Roman":
            {
                newFont = "Times New Roman, Times, serif";
            } break;
            case "Trebuchet":
            {
                newFont = "Trebuchet, Verdana, sans-serif";
            } break;
            case "Verdana":
            {
                newFont = "Verdana, Trebuchet, sans-serif";
            } break;
            default:
            {
                newFont = "serif";
            }
        }
        iframeEl.contentWindow.document.execCommand("FontName", false, newFont);
    }
}

function HTMLEditorChangeTextSize(editorName)
{
    var iframeEl, textSize;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    textSize = document.getElementById(editorName + "TextSize");
    if(iframeEl && textSize)
    {
        iframeEl.contentWindow.document.execCommand("FontSize", false, textSize.options[textSize.selectedIndex].value);
    }
}

function HTMLEditorUpdateSingleButton(editorName, commandToQuery, buttonName, buttonImage)
{
    var iframeEl, curIMG, newSRC;
    var buttonState;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        try
        {
            buttonState = iframeEl.contentWindow.document.queryCommandValue(commandToQuery);
            curIMG = document.getElementById(editorName + buttonName);
            if(curIMG)
            {
                newSRC = "";
                if(buttonState)
                {
                    //change the bold state to down
                    newSRC = goWebSpotURL + "images/htmleditor/" + buttonImage + "inoff.jpg";
                }
                else
                {
                    //change the bold state to up
                    newSRC = goWebSpotURL + "images/htmleditor/" + buttonImage + "outoff.jpg";
                }
                if(curIMG.src != newSRC)
                {
                    curIMG.src = newSRC;
                }
            }
        }
        catch(e)
        {
            //ignore it
        }
    }
}

function HTMLEditorUpdateButtons(editorName)
{
    HTMLEditorUpdateSingleButton(editorName, "Bold", "Bold", "bold");
    HTMLEditorUpdateSingleButton(editorName, "Italic", "Italic", "italic");
    HTMLEditorUpdateSingleButton(editorName, "Underline", "Underline", "underline");
    HTMLEditorUpdateSingleButton(editorName, "JustifyLeft", "AlignLeft", "aleft");
    HTMLEditorUpdateSingleButton(editorName, "JustifyCenter", "AlignCenter", "acenter");
    HTMLEditorUpdateSingleButton(editorName, "JustifyRight", "AlignRight", "aright");
    setTimeout("HTMLEditorUpdateButtons('" + editorName + "');", 40);
}

function HTMLEditorBoldText(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Bold", false, null);
    }
}

function HTMLEditorItalicText(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Italic", false, null);
    }
}

function HTMLEditorUnderlineText(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Underline", false, null);
    }
}

function HTMLEditorAlign(editorName, whichSide)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Justify" + whichSide, false, null);
    }
}

function HTMLEditorToggleOrderedList(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("InsertOrderedList", false, null);
    }
}

function HTMLEditorToggleUnorderedList(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("InsertUnorderedList", false, null);
    }
}

function HTMLEditorOutdentText(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Outdent", false, null);
    }
}

function HTMLEditorIndentText(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Indent", false, null);
    }
}

function HTMLEditorPopupPalette(editorName, paletteName)
{
    var palette, otherPalette;
    
    if(palette = document.getElementById(editorName + paletteName))
    {
        HTMLEditorDisplayColorName(editorName, paletteName, "");
        if(!palette.style.display || palette.style.display == "none")
        {
            //see if the other palette needs to be hidden
            if((paletteName.charAt(paletteName.length - ("FGPalette").length)) == "F")
            {
                otherPalette = paletteName.substring(0, paletteName.length - ("FGPalette").length) + "B" + paletteName.substring(paletteName.length - ("FGPalette").length + 1, paletteName.length);
            }
            else
            {
                otherPalette = paletteName.substring(0, paletteName.length - ("FGPalette").length) + "F" + paletteName.substring(paletteName.length - ("FGPalette").length + 1, paletteName.length);
            }
            otherPalette = document.getElementById(editorName + otherPalette);
            if(otherPalette && otherPalette.style.display && otherPalette.style.display == "block")
            {
                otherPalette.style.display = "none";
            }
            //display it
            palette.style.display = "block";
        }
        else
        {
            //hide the palette
            palette.style.display = "none";
        }
    }
}

function HTMLEditorSetTextColor(editorName, newColor)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("Forecolor", false, newColor);
    }
}

function HTMLEditorSetHighlightColor(editorName, newColor)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        if(browser.toLowerCase() == "ie")
        {
            iframeEl.contentWindow.document.execCommand("BackColor", false, newColor);
        }
        else
        {
            iframeEl.contentWindow.document.execCommand("HiliteColor", false, newColor);
        }
    }
}

function HTMLEditorCleanUp(editorName)
{
    var badTags = new Array("address", "blockquote", "div", "font", "h1", "h2", "h3", "h4", "h5", "h6", "h7", "p", "span");
    var iframeEl;
    var oldHTML = "", newHTML = "";
    
    //clean the tags out of the text
    if(confirm("This feature removes most of the formatting information (fonts, headings, etc.) from your text.  Doing this may help optimize your site for search engines, as well as repair the effects of some bugs in Internet Explorer.  However, you will have to put any formatting back in once it's done.  If you'd like to continue, click OK.  Otherwise, click Cancel."))
    {
        //clean up the text
        iframeEl = document.getElementById(editorName + "_IFrame");
        if(iframeEl && iframeEl.contentWindow && iframeEl.contentWindow.document && iframeEl.contentWindow.document.body && iframeEl.contentWindow.document.body.innerHTML)
        {
            /*look for bad tags*/
            oldHTML = iframeEl.contentWindow.document.body.innerHTML;
            for(var i=0; i<oldHTML.length; ++i)
            {
                if(oldHTML.charAt(i) == "<")
                {
                    for(var j=0; j<badTags.length; ++j)
                    {
                        if(oldHTML.substring(i+(oldHTML.charAt(i+1) == "/" ? 2 : 1), i+(oldHTML.charAt(i+1) == "/" ? 2 : 1)+badTags[j].length).toLowerCase() == badTags[j].toLowerCase())
                        {
                            newHTML += "<" + oldHTML.charAt(i+1) + "p>";
                            i = oldHTML.indexOf(">", i);
                            break;
                        }
                    }
                    if(j >= badTags.length)
                    {
                        newHTML += oldHTML.charAt(i);
                    }
                }
                else
                {
                    newHTML += oldHTML.charAt(i);
                }
            }
            //clean out redundant paragraph tags
            newHTML = "<p>" + newHTML + "</p>";
            /*for(var i=0; i<newHTML.length; ++i)
            {
                if(newHTML.substring(i, i + ("<p><p>").length).toLowerCase() == "<p><p>")
                {
                    //redundant <p>
                    newHTML = newHTML.substring(0, i) + newHTML.substring(i+("<p>").length, newHTML.length);
                    --i;
                }
                else if(oldHTML.substring(i, i + ("</p></p>").length).toLowerCase() == "</p></p>")
                {
                    //redundant </p>
                    newHTML = newHTML.substring(0, i) + newHTML.substring(i+("</p>").length, newHTML.length);
                    --i;
                }
            }
            alert("Just testing something: " + newHTML);*/
            //wrap it in a paragraph, since it probably is one already
            iframeEl.contentWindow.document.body.innerHTML = newHTML;
        }
    }
}

function HTMLEditorSaveRange(editorName)
{
    var iframeEl;
    
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl) // && ((iframeEl.document && iframeEl.document.selection) || (iframeEl.contentWindow && iframeEl.contentWindow.document && iframeEl.contentWindow.document.selection)))
    {
        if(browser.toLowerCase() == "ie")
        {
            if(iframeEl.contentWindow)
            {
                iframeEl.savedRange = iframeEl.contentWindow.document.selection.createRange();
            }
            else
            {
                iframeEl.savedRange = iframeEl.document.selection.createRange();
            }
            return (iframeEl.savedRange.htmlText ? iframeEl.savedRange.htmlText : iframeEl.savedRange.text);
        }
        else
        {
            iframeEl.savedRange = iframeEl.contentWindow.getSelection().getRangeAt(0);
            return iframeEl.contentWindow.getSelection().toString();
        }
    }
    return "";
}

function HTMLEditorInsertHTML(editorName, newHTML)
{
    var iframeEl;
    var range;
    var imageID = goWebSpotURL + "images/icons/boldoutoff.gif";
    var imageStart, imageEnd;
    
    //insert generic html into the document
    if(browser.toLowerCase() == "ie")
    {
        iframeEl = frames[editorName + "_IFrame"];
        if(iframeEl)
        {
            //insert the html
            iframeEl.focus();
            if(iframeEl.document.selection)
            {
                range = iframeEl.document.selection.createRange();
            }
            else if(iframeEl.savedRange)
            {
                range = iframeEl.savedRange;
            }
            if(range)
            {
                range.select();
                range.pasteHTML(newHTML);
                range.collapse(false);
            }
            //insert an image and then replace it with html
            //(old, not here anymore, but a cool technique to fall back on if needed)
        }
    }
    else
    {
        iframeEl = document.getElementById(editorName + "_IFrame");
        if(iframeEl)
        {
            iframeEl.contentWindow.document.execCommand("InsertHTML", false, newHTML);
        }
    }
}

function HTMLEditorViewSource(editorName)
{
    HTMLEditorUpdateSource(editorName);
    document.getElementById(editorName + "_IFrame").style.display = "none";
    document.getElementById(editorName).style.display = "block";
    document.getElementById(editorName + "GraphicModeTools").style.display = "none";
    document.getElementById(editorName + "InsertTools").style.display = "none";
    document.getElementById(editorName + "SourceModeTools").style.display = "block";
}

function HTMLEditorViewGraphics(editorName)
{
    HTMLEditorUpdateIFrame(editorName);
    document.getElementById(editorName).style.display = "none";
    document.getElementById(editorName + "_IFrame").style.display = "block";
    document.getElementById(editorName + "SourceModeTools").style.display = "none";
    document.getElementById(editorName + "GraphicModeTools").style.display = "block";
    document.getElementById(editorName + "InsertTools").style.display = "block";
}

function HTMLEditorInsertLink(editorName)
{
    var selectedText = "";
    
    //pop up the link editor
    selectedText = HTMLEditorSaveRange(editorName);
    
    window.open(editorPath + "edit.php?Type=HTMLEdit&Mode=InsertLink&Source=Parent_" + encodeURI(editorName) + "&LinkText=" + encodeURI(selectedText), "InsertLink", "location=no,menubar=no,resizable=no,toolbar=no,width=640,height=480").focus();
}

function HTMLEditorInsertPicture(editorName)
{
    //pop up the picture chooser
    HTMLEditorSaveRange(editorName);
    window.open(editorPath + "choosefile.php?Source=Parent_" + encodeURI(editorName) + "&FileTypes=jpg,gif,png&TypeDescription=Picture", "ChooseFile", "location=no,menubar=no,resizable=yes,toolbar=no,width=640,height=480").focus();
}

function HTMLEditorInsertPictureURL(editorName, newURL)
{
    var iframeEl;
    
    //insert a picture
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("InsertImage", false, newURL);
    }
}

function HTMLEditorInsertHR(editorName)
{
    var iframeEl;
    
    //insert a horizontal rule
    iframeEl = document.getElementById(editorName + "_IFrame");
    if(iframeEl)
    {
        iframeEl.contentWindow.document.execCommand("InsertHorizontalRule", false, null);
    }
}

function HTMLEditorInsertTable(editorName)
{
    //insert a table
    HTMLEditorSaveRange(editorName);
    window.open(editorPath + "edit.php?Type=HTMLEdit&Mode=InsertTable&Source=Parent_" + encodeURI(editorName), "InsertTable", "location=no,menubar=no,resizable=no,toolbar=no,width=320,height=240").focus();
}

function HTMLEditorInsertPreset(editorName)
{
    //insert a text idea
    if(confirm("Please click OK to continue"))
    {
        document.getElementById("Presets").style.display = "block";
    }
    else
    {
        window.open(editorPath + "edit.php?Type=HTMLEdit&Mode=TextIdeas" + (pageName != "" ? "&PageName=" + encodeURI(pageName) : "") + "&EditorName=" + encodeURI(editorName), "InsertTable", "location=no,menubar=no,resizable=no,toolbar=no,width=640,height=480").focus();
    }
}
