﻿function InitRichEdit(ctl, content)
{
    var userAgent = GetUserAgent();
    if (userAgent == "MSIE")
    {
        GetRichEditDiv(ctl).innerHTML = document.getElementById(content).value;
        GetRichEditDiv(ctl).contentEditable = true;
    }
    else if (userAgent == "FIREFOX")
    {
        var richEditDiv = GetRichEditDiv(ctl);
        richEditDiv.style.overflow = "hidden";
        var iframe = document.createElement("iframe");
        iframe.style.width = "100%";
        iframe.style.height = richEditDiv.clientHeight + "px";
        iframe.style.border = "none";
        richEditDiv.appendChild(iframe);
        richEditDiv.editFrame = iframe;
        
        GetRichTab(ctl).style.marginBottom = "4px";
        
        var doc = iframe.contentDocument;
        
        doc.open();
        doc.write("<html><body>");
        doc.write(document.getElementById(content).value);
        doc.write("</body></html>");
        doc.close();
        doc.designMode = "on";
    }
    else
    {
//        ShowHtmlTab(ctl);
        GetHtmlTB(ctl).value = document.getElementById(content).value;
        var richTabButton = GetRichTabButton(ctl);
        richTabButton.disabled = true;
        richTabButton.onclick = new Function("return false;");
        return;
    }
    
//    ShowRichTab(ctl);
}

function GetRichTab(ctl)
{
    return document.getElementById(ctl + "_Tabs_RichEdit");
}

function GetRichEditDiv(ctl)
{
    return document.getElementById(ctl + "_Tabs_RichEdit_div");
}

function GetHtmlTab(ctl)
{
    return document.getElementById(ctl + "_Tabs_HtmlEdit");
}

function GetHtmlTB(ctl)
{
    return document.getElementById(ctl + "_Tabs_HtmlEdit_TB");
}

function GetRichTabButton(ctl)
{
    return document.getElementById(ctl + "_showrich");
}

function GetHtmlTabButton(ctl)
{
    return document.getElementById(ctl + "_showhtml");
}


function ExecRichEdit(ctl, verb, param, showUI)
{
    if (!showUI)
    {
        showUI = false;
    }
    var div = GetRichEditDiv(ctl);
    div.focus();
    GetRichEditTarget(ctl).execCommand(verb, showUI, param);
}

function ShowRichTab(ctl)
{
    if (!InRichTab(ctl))
    {
        GetHtmlTab(ctl).style.display = "none";
        GetRichTab(ctl).style.display = "";
        SetRichContent(ctl, GetHtmlTB(ctl).value);
    }
    GetRichTabButton(ctl).style.fontWeight = "bold";
    GetHtmlTabButton(ctl).style.fontWeight = "";
}

function ShowHtmlTab(ctl)
{
    if (InRichTab(ctl))
    {
        GetRichTab(ctl).style.display = "none";
        GetHtmlTab(ctl).style.display = "";
        GetHtmlTB(ctl).value = GetRichContent(ctl);
    }
    GetRichTabButton(ctl).style.fontWeight = "";
    GetHtmlTabButton(ctl).style.fontWeight = "bold";
}

function SetRichContent(ctl, content)
{
    var userAgent = GetUserAgent();
    if (userAgent == "MSIE")
    {
        GetRichEditDiv(ctl).innerHTML = content;
    }
    else if (userAgent == "FIREFOX")
    {
        GetRichEditDiv(ctl).editFrame.contentWindow.document.body.innerHTML = content;
    }
    else
    {
        return "";
    }
}

function GetRichContent(ctl)
{
    var userAgent = GetUserAgent();
    if (userAgent == "MSIE")
    {
        return GetRichEditDiv(ctl).innerHTML;
    }
    else if (userAgent == "FIREFOX")
    {
        return GetRichEditDiv(ctl).editFrame.contentWindow.document.body.innerHTML;
    }
    else
    {
        return "";
    }
}

function GetRichEditTarget(ctl)
{
    var userAgent = GetUserAgent();
    if (userAgent == "MSIE")
    {
        return document;
    }
    else if (userAgent == "FIREFOX")
    {
        return GetRichEditDiv(ctl).editFrame.contentWindow.document;
    }
    else
    {
        return null;
    }
}

function InRichTab(ctl)
{
    return GetRichTab(ctl).style.display == "";
}

var richtext_colors = new Array(
    'ffffff', 'ffcccc', 'ffcc99', 'ffff99', 'ffffcc', '99ff99', '99ffff', 'ccffff', 'ccccff', 'ffccff',
    'cccccc', 'ff6666', 'ff9966', 'ffff66', 'ffff33', '66ff99', '33ffff', '66ffff', '9999ff', 'ff99ff',
    'c0c0c0', 'ff0000', 'ff9900', 'ffcc66', 'ffff00', '33ff33', '66cccc', '33ccff', '6666cc', 'cc66cc',
    '999999', 'cc0000', 'ff6600', 'ffcc33', 'ffcc00', '33cc00', '00cccc', '3366ff', '6633ff', 'cc33cc',
    '666666', '990000', 'cc6600', 'cc9933', '999900', '009900', '339999', '3333ff', '6600cc', '993399',
    '333333', '660000', '993300', '996633', '666600', '006600', '336666', '000099', '333399', '663366',
    '000000', '330000', '663300', '663333', '333300', '003300', '003333', '000066', '330099', '330033'
  );


function ShowOrHideColorView(ctl, button, clrview)
{
    var pal = document.getElementById(clrview);
    if (pal.style.display == "none")
    {        
        if (pal.initialized == null)
        {
            var html = "";
            var count = 0;
            for (var i = 0; i < richtext_colors.length; i++)
            {
                if (count % 10 == 0)
                {
                    if (count > 0)
                    {
                        html += "</div>";
                    }
                    html += "<div class=\"rtColorRow\">";
                }
                html += "<a href=\"#\" class=\"rtColorCell\" style=\"background-color: #";
                html += richtext_colors[i];
                html += "\" onclick=\"ChooseColor(\'";
                html += ctl;
                html += "\', \'";
                html += clrview;
                html += "\', \'";
                html += richtext_colors[i];
                html += "\'); return false;\">&nbsp;</a>";
                count++;
            }
            
            html += "</div>";
            
            html += "<input type=\"button\" class=\"rtCloseButton\" onclick=\"HideColorView(\'";
            html += clrview;
            html += "\');\" value=\"Close\" />";
            
            html += "</tr>";
            
            pal.innerHTML = html;
            pal.initialized = true;
        }
        pal.style.position = "absolute";
        pal.style.left = button.offsetLeft + button.offsetWidth + document.body.offsetLeft - 120;
        pal.style.top = button.offsetTop + button.offsetHeight + document.body.offsetTop;
        pal.style.display = "";
    }
    else
    {
        HideColorView(clrview);
    }
}

function ChooseColor(ctl, clrview, color)
{
    ExecRichEdit(ctl, "forecolor", "#" + color);
    HideColorView(clrview);
}

function HideColorView(clrview)
{
    var pal = document.getElementById(clrview);
    pal.style.display = "none";
}

function PrepareRichEdit(ctl, content)
{
    var richview = GetRichTab(ctl);
    if (richview.style.display == "")
    {
        document.getElementById(content).value = GetRichContent(ctl);
    }
    else
    {
        document.getElementById(content).value = GetHtmlTB(ctl).value;
    }
}

function GetUserAgent()
{
    var userAgent = navigator.userAgent.toLowerCase();
    if (userAgent.indexOf("msie") != -1)
    {
        return "MSIE";
    }
    else if (userAgent.indexOf("firefox") != -1)
    {
        return "FIREFOX";
    }
    else
    {
        return "UNKNOWN";
    }
}    
    
