var colorTargetElementId = null;
var colorCommandType = null;
var siteUrl = "";

function RichTextInit(targetElementId, width, height, formName, formElementName, url)
{
    var found = false;
    var elm = document.getElementsByTagName("*");
    var i;
    
    for(i = 0; i < elm.length;  i++)
    {
        if(elm[i].id == targetElementId)
        {
            found = true;
            break;
        }
    }
    
    if(!found)
    {
        return;
    }
    
    if(isFF)
    {
        var editor = document.createElement("IFrame");
        editor.id = targetElementId + "IFrame";
        editor.style.width = width + "px";
        editor.style.height = height + "px";
        editor.style.border = "1px solid #C0C0C0";
        editor.frameBorder = "0";
        editor.marginHeight = "0px";
        editor.marginLeft = "0px";
        document.getElementById(targetElementId).appendChild(editor);

        editor.onmouseout = function()
        {
            document.forms[formName][formElementName].value = this.contentDocument.body.innerHTML;
        }
        
        editor.onload = function()
        {
            var editorDoc = editor.contentDocument || editor.contentWindow.document;
            
            setTimeout(function() {
                editorDoc.body.innerHTML = document.forms[formName][formElementName].value;
                editorDoc.designMode = "on";
            }, 0);
        }
    }
    else if(isSafari)
    {
        var editor = document.createElement("IFrame");
        editor.id = targetElementId + "IFrame";
        editor.style.width = width + "px";
        editor.style.height = height + "px";
        editor.style.border = "1px solid #C0C0C0";
        editor.frameBorder = "0";
        editor.marginHeight = "0px";
        editor.marginLeft = "0px";
        document.getElementById(targetElementId).appendChild(editor);
        
        var doc = editor.contentDocument;
        doc.open("text/html");
        doc.write(document.forms[formName][formElementName].value);
        doc.close();
        doc.designMode = "on";
        doc.contentEditable = true;
        doc.body.style.font = "11px verdana";
        
        doc.onmouseout = function()
        {
            document.forms[formName][formElementName].value = this.body.innerHTML;
        }
        
        doc.onclick = function()
        {
            HideColorPalette();
        }
    }
    else
    {
        var editor = document.getElementById(targetElementId);
        editor.style.width = width + "px";
        editor.style.height = height + "px";
        editor.style.border = "1px solid #C0C0C0";
        editor.style.textAlign = "left";
        editor.style.verticalAlign = "top";
        editor.contentEditable = true;

        editor.onmouseout = function()
        {
            document.forms[formName][formElementName].value = this.innerHTML;
        }
        
        editor.onclick = function()
        {
            HideColorPalette();
        }
        
        editor.innerHTML = document.forms[formName][formElementName].value;
    }
    
    siteUrl = url;
    
    InitColorPalette();
}

function doRichEditCommand(targetElementId, aName, aArg)
{    
    if(isFF)
    {
        document.getElementById(targetElementId + "IFrame").contentDocument.execCommand(aName, false, aArg);
        document.getElementById(targetElementId + "IFrame").contentWindow.focus();
    }
    else if(isSafari)
    {
        document.getElementById(targetElementId + "IFrame").contentDocument.execCommand(aName, false, aArg);
        document.getElementById(targetElementId + "IFrame").contentWindow.focus();
    }
    else
    {
        var sText = document.selection.createRange();
        
        if(aName == "HiliteColor")
        {
            sText.execCommand("BackColor", false, aArg);
        }
        else
        {
            sText.execCommand(aName, false, aArg);
        }

        document.getElementById(targetElementId).focus();
    }
    
    HideColorPalette();
}

function doFontSize(targetElementId, aArg)
{    
    if(isFF)
    {
        document.getElementById(targetElementId + "IFrame").contentDocument.execCommand(aName, false, aArg);
        document.getElementById(targetElementId + "IFrame").contentWindow.focus();
    }
    else
    {
        var sText = document.selection.createRange();
        sText.pasteHTML("<FONT style=\"font-size:" + aArg + "\">" + sText.htmlText + "</FONT>");
    }
    
    //Clean up the unnecessary font attributes.
    /*
    var editor = document.getElementById(targetElementId);
    var newHTML = editor.innerHTML;
    while(newHTML.indexOf("</FONT></FONT>") > -1)
    {
        newHTML = newHTML.replace("</FONT></FONT>", "</FONT>");
    }
    
    var fontTags = editor.innerHTML.split("<");
    var previousIsOnlyFontTag = false;
    var i;
    for(i = 0; i < fontTags.length; i++)
    {
        alert(fontTags[i]);
    }
    
    editor.innerHTML = newHTML;
    */
    
    HideColorPalette();
}

function CreateLink(targetElementId)
{
    HideColorPalette();
    
    if(isFF)
    {
        var url = window.prompt("Anchor URL", siteUrl);
        document.getElementById(targetElementId + "IFrame").contentDocument.execCommand("createLink", false, url);
        document.getElementById(targetElementId + "IFrame").contentDocument.body.innerHTML = UpdateAnchors(document.getElementById(targetElementId + "IFrame").contentDocument.body.innerHTML);
        document.getElementById(targetElementId + "IFrame").contentWindow.focus();
    }
    else
    {
        var sText = document.selection.createRange();
        sText.execCommand("CreateLink");

        document.getElementById(targetElementId).innerHTML = UpdateAnchors(document.getElementById(targetElementId).innerHTML);
        document.getElementById(targetElementId).focus();
    }
}

function ShowColorPalette(evt, targetElementId, commandType, evt)
{
    colorTargetElementId = targetElementId;
    colorCommandType = commandType;
        
    var palette = document.getElementById("colorpalette");

    var pageXY = GetEventElementPosition(evt);
    var elmDim = GetEventElementDimensions(evt);
    
    palette.style.left = pageXY[0] + "px";
    palette.style.top = (pageXY[1] + elmDim[1] + 5) + "px";
    
    palette.style.visibility = "visible";
}

function HideColorPalette()
{
    document.getElementById("colorpalette").style.visibility = "hidden";
}

function InitColorPalette()
{
    var x;
    if (document.getElementsByTagName)
    {
        x = document.getElementsByTagName("TD");
    }
    else if (document.all)
    {
        x = document.all.tags("TD");
    }

    for (var i = 0; i < x.length; i++)
    {      
        if(x[i].id.indexOf("#") > -1)
        {
            x[i].onmouseover = function()
            {
                ColorPaletteOver(this);
            }
            
            x[i].onmouseout = function()
            {
                ColorPaletteOut(this);
            }
            
            x[i].onmousedown = function()
            {
                ColorPaletteClick(this);
            }
        }
    }
}

function ColorPaletteOver(elm)
{
    elm.style.border="1px dotted white";
}

function ColorPaletteOut(elm)
{
    elm.style.border="1px solid gray";
}

function ColorPaletteClick(elm)
{
    selectColor(elm.id);
}

function selectColor(color)
{
    doRichEditCommand(colorTargetElementId, colorCommandType, color);
    document.getElementById("colorpalette").style.visibility = "hidden";
}

function UpdateAnchors(html)
{
    var anchorsHtml = "";
    var anchors;
    
    if(html.indexOf("<A ") > -1)
    {
        anchors = html.split("<A ");
    }
    else
    {
        anchors = html.split("<a ");
    }

    var i;
    for(i = 0; i < anchors.length; i++)
    {
        anchors[i] = anchors[i].replace("</a>", "</A>");
        
        if(anchors[i].indexOf("</A>") > -1)
        {
            var anchor = "<A " + anchors[i].substring(0, anchors[i].indexOf("</A>"));
            var innerText = anchor.substring(anchor.indexOf(">") + 1);
            anchor = anchor.substring(0, anchor.indexOf(">") + 1);
            var postText = anchors[i].substring(anchors[i].indexOf("</A>") + 4);

            var span = document.createElement("SPAN");
            var elm = document.createElement("A");
            span.appendChild(elm);
            
            var url;
            var bInternal = false;
            
            //Get the href attribute.
            if(anchor.toUpperCase().indexOf(" HREF=") > -1)
            {
                var href = GetElementAttribute(anchor, "href");
                elm.setAttribute("href", href);

                if(href.indexOf("http://") > -1)
                {
                    if(href.toLowerCase().indexOf(siteUrl.toLowerCase()) > -1)
                    {
                        bInternal = true;
                    }
                }
                else
                {
                    bInternal = true;
                }

                if(bInternal)
                {
                   elm.setAttribute("onclick", "GetSubPage(this.getAttribute('href'), 'bodycontent'); return false;");
                }
            }

            //Get the style.
            var style = GetElementAttribute(anchor, "style");
            if(style != "")
            {
                alert("style:" + style + ":");
                elm.setAttribute("style", style);
            }
            
            //Get the internal text.
            elm.innerHTML = innerText;
            
            anchorsHtml += span.innerHTML + postText;
        }
        else
        {
            anchorsHtml += anchors[i];
        }
    }
    
    return anchorsHtml;
}

function GetElementAttribute(elm, attribute)
{
    var attributeValue = "";
    
    if(elm.toUpperCase().indexOf(" " + attribute.toUpperCase() + "=") > -1)
    {
        var attributeText = elm.substring(elm.toUpperCase().indexOf(" " + attribute.toUpperCase() + "=") + attribute.length + 2);

        if(attributeText.substring(0, 1) == "\"")
        {
            attributeValue = attributeText.substring(1);
            attributeValue = attributeValue.substring(0, attributeValue.indexOf("\""));
        }
        else if(attributeText.substring(0, 1) == "'")
        {
            attributeValue = attributeText.substring(1);
            attributeValue = attributeValue.substring(0, attributeValue.indexOf("'"));
        }
        else
        {
            if(attributeText.indexOf(" ") > -1)
            {
                attributeValue = attributeText.substring(0, attributeText.indexOf(" "));
            }
            else
            {
                attributeValue = attributeText.substring(0, attributeText.indexOf(">"));
            }
        }
    }
    return attributeValue;
}

function OpenTableEditor(id)
{
    InsertAtCaret(document.getElementById(id), "whatever");
    return;
    
    var left = (screen.width / 2) - 125;
    var top = (screen.height / 2) - 125;
    
    window.open("table_editor.asp?id=" + id, null, "height=250,width=250,top=" + top + ",left=" + left + ",titlebar=no,status=no,toolbar=no,menubar=no,location=no");
}

function InsertAtCaret(obj, text)
{
	if(document.selection)
	{
		obj.focus();
		var range = document.selection.createRange();
        range.pasteHTML(text);
    }
}
