﻿/*
    HELPER FUNCTIONS
*/
function OnLoaded(script) {
    if(document.readyState == 'complete'){
        //window.setTimeout(script,200)
        window.eval(script);
    }else{
        window.setTimeout('OnLoaded('+script+')',200)
    }
}

    function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null ) node = document;
        if ( IsEmpty(searchClass)) searchClass = "*";
        if ( IsEmpty(tag) ) tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
        }
        return classElements;
    }
    function IsEmpty(input) {
        if(!input) return true;
        //if(input == undefined) {alert("undefined"); return true;}
        return input == "";
    }    
    function getLeft(MyObject)
    {
        if(MyObject.style.position == "fixed") return MyObject.offsetLeft;
        if (MyObject.offsetParent) {
             return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
        }
        else {
             return (MyObject.offsetLeft - document.documentElement.scrollLeft);
        }
    }
    function getTop(MyObject)
    {
        if(MyObject.style.position == "fixed") return MyObject.offsetTop;
        if (MyObject.offsetParent)  return MyObject.offsetTop + getTop(MyObject.offsetParent);
        else                        return MyObject.offsetTop - document.documentElement.scrollTop;
    }
    function nextObject(n) { 
do n = n.nextSibling; 
while (n && n.nodeType != 1); 
return n; 
}
function previousObject(p) { 
do p = p.previousSibling; 
while (p && p.nodeType != 1); 
return p; 
} 

    function popdownRTEpanels() {
        var panels = getElementsByClass('rtePopup');
        if(panels == null) return;
        for(var i = 0 ; i < panels.length; i++) {
            panels[i].style.display = "none";
        }
    }


function Insert(src) {
   var range = document.selection.createRange();
   range.pasteHTML(src.outerHTML);
}
function InsertImage(src) {
   var range = document.selection.createRange();
   range.pasteHTML("<img src='"+ src.src + "'/>");
}
function Test(frame, ContentID,css) {
    var ContentBox = document.getElementById(ContentID);
    if(frame == null) { return; }
    if(ContentBox == null) { return; }
    //frame.contentWindow.document.close();
    if(window.addEventListener) { // FF
        //alert("FF");
        if(frame.contentWindow.document.designMode == "On") {
            //window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100);
            return;
        }
        var ContentBox = document.getElementById(ContentID);
        frame.contentWindow.document.designMode="on";
        frame.contentWindow.document.open();
        frame.contentWindow.document.write(ContentBox.value);
        frame.contentWindow.addEventListener( "keyup" , function(event) {window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100);},false);
        frame.contentWindow.addEventListener( "mousemove" , function(event) {window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100);},false);
        frame.contentWindow.addEventListener( "click" , function(event) {window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100);},false);
        //frame.contentDocument.addEventListener( "keypress" , function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); },false);
        //frame.contentDocument.addEventListener( "keydown" , function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); },false);
        //frame.contentDocument.addEventListener("click",function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); },false);
        frame.contentWindow.document.close();
        //CopyStyle(frame.id);
        LoadStyle(frame.id,css);
        fitFrame(frame);
    } else { // IE
        if(frame.contentWindow.document.designMode == "On") {
            window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100);
            return;
        }
        var ContentBox = document.getElementById(ContentID);
        frame.contentWindow.document.designMode="on";
        frame.contentWindow.document.open();
        frame.contentWindow.document.write(ContentBox.value);
        frame.contentWindow.document.close();
        frame.contentWindow.document.body.onkeyup = function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); };
        frame.contentWindow.document.body.onclick = function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); };
        frame.contentWindow.document.body.onmousedown = function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); };
        frame.contentWindow.document.body.onmouseup = function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); };
        frame.contentWindow.document.body.onmousemove = function(event) { window.setTimeout("UpdateField(document.getElementById('"+frame.id+"'),document.getElementById('"+ContentID+"'))",100); };
        CopyStyle(frame.id);
        LoadStyle(frame.id,css);
        fitFrame(frame);
    }
    //UpdateField(EditorFrame,ContentBox);
    
}
function onLoad(FrameID,ContentID,ContainerID) {
    //alert("loading " + FrameID);
    var EditorFrame = document.getElementById(FrameID);
    if(EditorFrame == null) {
        //alert("RTEfunctions.js: onLoad(): editorframe not found");
        return;
    }
    var ContentBox = document.getElementById(ContentID);
    if(ContentBox == null) {
        //alert("RTEfunctions.js: onLoad(): contentbox not found");
        return;
    }
    //alert("elements found");
    EditorFrame.contentWindow.document.designMode="on";
    EditorFrame.contentWindow.document.open();
    EditorFrame.contentWindow.document.write(ContentBox.value);
    EditorFrame.contentWindow.document.close();
    EditorFrame.contentWindow.document.body.onkeyup = function(event) { setTimeout("UpdateField(document.getElementById('"+FrameID+"'),document.getElementById('"+ContentID+"'))",100); };
    UpdateField(EditorFrame,ContentBox);
    var ContainerBox = document.getElementById(ContainerID);
    if(ContainerBox == null) return;
    CopyStyle(FrameID);
    setTimeout("UpdateField(document.getElementById('"+FrameID+"'),document.getElementById('"+ContentID+"'))",500);
}
function LoadStyle(FrameID, css) {
    //alert("Loading styles");
    var EditorFrame = document.getElementById(FrameID);
    if(EditorFrame == null) {
        alert("RTEfunctions.js: LoadStyle(): editorframe not found");
        return;
    }
    var doc = EditorFrame.contentWindow.document;
    var hdr = doc.createElement("head");
    var tag = doc.createElement("style");
    tag.setAttribute("type","text/css");
    if(tag.styleSheet){// IE
        tag.styleSheet.cssText = InheritStyle(css);
        hdr.appendChild(tag);
        doc.appendChild(hdr);
    } else {
        //tag.appendChild(doc.createTextNode(InheritStyle(css)));
        //doc.head.appendChild(tag);
    }
}
function InheritStyle(css) {
    var result = "";
    var styles = document.getElementsByTagName('style');
    for (var i = 0, max = styles.length; i < max; i++) {
        result += styles[i].outerHTML;
    }
    return result + "<style type='text/css' id='RTEStyle'>"+css+"</style>";
}
function CopyStyle(FrameID) {
    var container = document.getElementById(FrameID).parentNode;
    var style = document.getElementById(FrameID).contentWindow.document.body.style;
    style.borderStyle = getStyleProp(container,"borderStyle");
    style.borderWidth = getStyleProp(container,"borderWidth");
    style.borderColor = getStyleProp(container,"borderColor");
    style.borderTopStyle = getStyleProp(container,"borderTopStyle");
    style.borderTopWidth = getStyleProp(container,"borderTopWidth");
    style.borderTopColor = getStyleProp(container,"borderTopColor");
    style.clear = getStyleProp(container,"clear");
    style.styleFloat = getStyleProp(container,"styleFloat");
    style.height = getStyleProp(container,"height");
    style.margin = getStyleProp(container,"margin");
    style.marginBottom = getStyleProp(container,"marginBottom");
    style.padding = getStyleProp(container,"padding");
    style.paddingBottom = getStyleProp(container,"paddingBottom");
    style.width = getStyleProp(container,"width");
    
    style.backgroundAttachment = getStyleProp(container,"backgroundAttachment");
    style.backgroundColor = getInheritedProperty(container, "backgroundColor", "transparent");
    style.backgroundImage = getStyleProp(container,"backgroundImage");
//    style.backgroundPosition = getStyleProp(container,"backgroundPosition");
    style.backgroundRepeat = getStyleProp(container,"backgroundRepeat");
    style.color = getStyleProp(container,"color");

    style.display = getStyleProp(container,"display");
    //style.listStyle = getStyleProp(container,"listStyle");// undefined
    style.listStyleImage = getStyleProp(container,"listStyleImage");
    style.listStyleType = getStyleProp(container,"listStyleType");
    style.listStylePosition = getStyleProp(container,"listStylePosition");
    //style.whitespace = getStyleProp(container,"whitespace");// undefined

    //style.font = getStyleProp(container,"font");// undefined
    style.fontFamily = getStyleProp(container,"fontFamily");
    style.fontSize = getStyleProp(container,"fontSize");
    style.fontStyle = getStyleProp(container,"fontStyle");
    style.fontVariant = getStyleProp(container,"fontVariant");
    style.fontWeight = getStyleProp(container,"fontWeight");
    
    style.letterSpacing = getStyleProp(container,"letterSpacing");
    style.lineHeight = getStyleProp(container,"lineHeight");
    style.textAlign = getStyleProp(container,"textAlign");
    style.textDecoration = getStyleProp(container,"textDecoration");
    style.textIndent = getStyleProp(container,"textIndent");
    style.textTransform = getStyleProp(container,"textTransform");
    style.verticalAlign = getStyleProp(container,"verticalAlign");
    style.wordSpacing = getStyleProp(container,"wordSpacing");
    
    style.borderColor = "red";
    style.borderStyle = "dotted";
    style.borderWidth = "1px";
}

function getInheritedProperty(container, property, escape) {
    var parent = container;
    var result;
    while(parent != null) {
        if (parent.style) {
            result = getStyleProp(parent,property);
            if (result != escape) break;
        }
        parent = parent.parentNode;
    }
    return result;
}
function getStyleProp(x,prop) {
    if(x.currentStyle) return(x.currentStyle[prop]);
    if(document.defaultView) {
        if(document.defaultView.getComputedStyle) return (document.defaultView.getComputedStyle(x,'')[prop]);
    }
    return(null);
}

function fontEdit(ID, x,y)
{
   var textEditor = document.getElementById("textEditor"+ID);
   textEditor.contentWindow.document.execCommand(x,"",y);
   textEditor.focus();
   setTimeout("UpdateField(document.getElementById('textEditor"+ID+"'),document.getElementById('"+ID+"'))",100);
}
function UpdateField(Editor,Content) {
    SaveIFrame(Editor,Content);
    fitFrame(Editor);
}
function SaveIFrame(Editor,Content) {
    Content.value = ReadIFrame(Editor);
}
function ReadIFrame(Editor){
    return Editor.contentWindow.document.body.innerHTML;
}
function fitFrame(Editor) {
    Editor.style.height = Math.max(50,Editor.contentWindow.document.body.scrollHeight) + "px";
}
function ToggleHTMLTab(Content_ID) {
    var HTML = document.getElementById(Content_ID+"HTML");
    var RTE  = document.getElementById(Content_ID+"RTE");
    if(HTML.style.visibility == "visible") {
        HTML.style.visibility = "hidden";
        HTML.style.height = "0px";
        RTE.style.visibility = "visible";
        RTE.style.height = "auto";
        document.getElementById("textEditor"+Content_ID).contentWindow.document.body.innerHTML = document.getElementById(Content_ID).value;
    } else {
        HTML.style.visibility = "visible";
        HTML.style.height = "auto";
        RTE.style.visibility = "hidden";
        RTE.style.height = "0px";
    }
}
/*if( Sys && Sys.Application ){
   Sys.Application.notifyScriptLoaded();
}*/