﻿
    originalOnClientLoadFunctions = new Object();

    function OnClientInitFilter(editor)
    {
        //use the filter only on IE
        if (editor.IsIE)
        {
            //save the original onclientload function reference
            if (typeof(editor.OnClientLoad) != "undefined" && editor.OnClientLoad != null)
            {
                originalOnClientLoadFunctions[editor.Id] = editor.OnClientLoad;
            }
            editor.OnClientLoad = OnClientLoadFilter;

            if (typeof(RadEditorNamespace.OriginalSetElementInnerHTML) == "undefined")
            {
                RadEditorNamespace.OriginalSetElementInnerHTML = RadEditorNamespace.SetElementInnerHTML;
                RadEditorNamespace.SetElementInnerHTML = function (elem, content)
                {
                    content = GetStoredOriginalPathsAndAttributes(content);
                    RadEditorNamespace.OriginalSetElementInnerHTML(elem,content);
                    RestoreOriginalPathsAndAttributes(elem);
                };
            }

            editor.OriginalSetContentMethod = editor.SetContent;
            editor.SetContent = function(content)
            {
                if (!editor.InitialSetContentCalled)
                {
                    editor.InitialSetContentCalled = true;
                    editor.InitialNonModifiedContent = content;
                }
                //Call the original editor method:
                editor.OriginalSetContentMethod(content);
            }
            editor.OriginalPasteHtml = editor.PasteHtml;
            editor.PasteHtml = function(content, sTitle, bSelectText, bFireSelChanged, bAddUndo)
            {
                content = GetStoredOriginalPathsAndAttributes(content);
                editor.OriginalPasteHtml(content, sTitle, bSelectText, bFireSelChanged, bAddUndo);
                RestoreOriginalPathsAndAttributes(editor.GetContentArea());
            }
        }
    }

    function GetStoredOriginalPathsAndAttributes(content)
    {
        var pathsRegExp = /((href|src)\s*=\s*('|")?)(.*?)(\3)(\s|(\/)?>)/ig;
        content = content.replace(pathsRegExp, "$2=$3$4$3 originalAttribute=\"$2\" originalPath=\"$4\"$6");
        return content;
    }

    function RestoreOriginalPathsAndAttributes(contentArea)
    {
        var children = contentArea.getElementsByTagName("*");
        for(var i=0; i < children.length; i++)
        {
            var currentChild = children[i];
            var originalPath = currentChild.getAttribute("originalPath");
            var originalAttribute = currentChild.getAttribute("originalAttribute");
            if (originalPath != null && originalAttribute != null)
            {
                currentChild.removeAttribute("originalPath");
                currentChild.removeAttribute("originalAttribute");
                if (originalPath.toLowerCase().indexOf("mailto:")==0)
                {
                    //fix the IE bug where a mailto link with a subject will change the anchor text
                    continue;
                }
                
                //fix anchors
                originalPath = originalPath.replace(window.location.href+"#","#");
                //
                
                currentChild.removeAttribute(originalAttribute);
                currentChild.setAttribute(originalAttribute, originalPath);
            }
        }
    }

    function OnClientLoadFilter(editor)
    {
        if (editor.InitialSetContentCalled)
        {
            editor.SetHtml(editor.InitialNonModifiedContent);
        }
        editor.InitialNonModifiedContent = null;
        //call the original OnClientLoad function
        if (typeof(originalOnClientLoadFunctions[editor.Id]) != "undefined" && originalOnClientLoadFunctions[editor.Id] != null)
        {
            originalOnClientLoadFunctions[editor.Id](editor);
        }
    }

