
// This method does not return a value (window object or null)
function OpenNewWindow(winName, Page, y, x, w, h, tool, menu, scroll, location, directories, status, resize, fullscreen) {
    OpenAndReturnNewWindow(winName, Page, y, x, w, h, tool, menu, scroll, location, directories, status, resize, fullscreen)
}


// This method returns a value (window object or null)
    function OpenAndReturnNewWindow(winName, Page, y, x, w, h, tool, menu, scroll, location, directories, status, resize, fullscreen, returnWindow) {
        var IE = navigator.appName.indexOf("Microsoft") != -1;

        if (window.OpenWindow)
            if (!window.OpenWindow.closed)
                window.OpenWindow.StayIN = true

        if (fullscreen == 'no')
        {
            if (navigator.appVersion.indexOf('Chrome')>0) {
               h = parseInt(h) - 31;
            }
            var objWindow = window.open(Page, winName, 'width=' + w + ',height=' + h + ',toolbar=' + tool + ',menubar=' + menu + ',scrollbars=' + scroll + ',location=' + location + ',directories=' + directories + ',status=' + status + ',resizable=' + resize + ',top=' + x + ',left=' + y + ';')
        }
        else
        {
            var objWindow = IE ? window.open(Page, winName, 'fullscreen=yes') : OpenFullWindow(winName, Page);
        }

        if (objWindow) {
            objWindow.focus();
            return objWindow;
        }
        else
            return null;
    }


    function OpenFullWindow(winName, Page, realFullScreen) {
        w = 800
        h = 600
        if (window.screen) {
            w = window.screen.availWidth - 10;
            h = window.screen.availHeight - 30;
        }

        if (realFullScreen == true) {
            var StrDefine = 'fullscreen=yes,toolbar=no,menubar=no,scrollbars=yes,location=no,directories=no,status=no,resizable=yes'
        }
        else {
            var StrDefine = 'width=' + w + ',height=' + h + ',toolbar=no,menubar=no,scrollbars=yes,location=no,directories=no,status=no,resizable=yes,top=1,left=1'
        }

        var OpenWindow = window.open(Page, winName, StrDefine);
        OpenWindow.focus()
        
        if (realFullScreen == false && navigator.appVersion.indexOf('Chrome')>0) {
           OpenWindow.resizeBy(0, h - OpenWindow.innerHeight);
        }
        return OpenWindow;
    }

    function OpenWindow(strUrl, winName, intWidth, intHeight) {
        var strFeatures = "toolbar=no,menubar=no,location=no,directories=no,status=no"
        if (intWidth != undefined) strFeatures += (",width=" + intWidth);
        if (intHeight != undefined) strFeatures += (",height=" + intHeight);
        var objWindow = window.open(strUrl, winName, strFeatures);

        if (objWindow) {
            objWindow.focus()
            return objWindow;
        }
        else
            return null;
    }

    //fermeture de la fenetre
    function CloseWindow() {
        self.close();
    }

    function MoveLang(element, Page) {
        if (Page == undefined) {
            Page = window.location.href
        }


        if (Page.match(/([\?|\&])lang=([a-zA-Z\-]*)/i)) {
            Page = Page.replace(/([\?|\&])lang=([a-zA-Z\-]*)/i, "$1Lang=" + element.options[element.selectedIndex].value);
        }
        else {
            Page = Page + (Page.indexOf("?") == -1 ? "?" : "&") + "Lang=" + element.options[element.selectedIndex].value;
        }

        window.location = Page;
    }


    function replaceSubstring(inputString, fromString, toString) {

        var temp = inputString;
        if (fromString == "") {
            return inputString;
        }
        if (toString.indexOf(fromString) == -1) {
            while (temp.indexOf(fromString) != -1) {
                var toTheLeft = temp.substring(0, temp.indexOf(fromString));
                var toTheRight = temp.substring(temp.indexOf(fromString) + fromString.length, temp.length);
                temp = toTheLeft + toString + toTheRight;
            }
        } else {
            var midStrings = new Array("~", "`", "_", "^", "#");
            var midStringLen = 1;
            var midString = "";
            while (midString == "") {
                for (var i = 0; i < midStrings.length; i++) {
                    var tempMidString = "";
                    for (var j = 0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
                    if (fromString.indexOf(tempMidString) == -1) {
                        midString = tempMidString;
                        i = midStrings.length + 1;
                    }
                }
            }
            while (temp.indexOf(fromString) != -1) {
                var toTheLeft = temp.substring(0, temp.indexOf(fromString));
                var toTheRight = temp.substring(temp.indexOf(fromString) + fromString.length, temp.length);
                temp = toTheLeft + midString + toTheRight;
            }

            while (temp.indexOf(midString) != -1) {
                var toTheLeft = temp.substring(0, temp.indexOf(midString));
                var toTheRight = temp.substring(temp.indexOf(midString) + midString.length, temp.length);
                temp = toTheLeft + toString + toTheRight;
            }
        }
        return temp;
    }

    function unescapeHtmlCodes(strText) {

        strText = strText.replace(/&nbsp;/g, " ");
        strText = strText.replace(/&quot;/g, "\"");
        strText = strText.replace(/&amp;/g, "&");
        strText = strText.replace(/&lt;/g, "<");
        strText = strText.replace(/&gt;/g, ">");

        return strText;
    }


    function createScormTagObject(PATH_SERVER) {
        document.write('<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="0" height="0" id="APIAdapter" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_3_1_09-windows-i586.cab#Version=1,3,0,0">');
        document.write('<param name = "code" value = "com/strategia/applet/APIAdapterApplet.class" >');
        document.write('<param name = "codebase"" 	value = "' + PATH_SERVER + 'Includes/Scorm" >');
        document.write('<param name = "type" 		value = "application/x-java-applet;version=1.3">');
        document.write('<param name = "mayscript" 	value = "true" >');
        document.write('<param name = "scriptable" 	value = "true" >');
        document.write('<param name = "archive" 	value = "applet.jar,jaxp.jar,parser.jar" >');
        document.write('<comment>');
        document.write('<applet   code="com/strategia/applet/APIAdapterApplet.class"');
        document.write('archive="applet.jar,jaxp.jar,parser.jar"');
        document.write('codebase=" & PATH_SERVER & "Includes/Scorm"');
        document.write('src="' + PATH_SERVER + 'Includes/Scorm"');
        document.write('height="1"');
        document.write('id="APIAdapter"');
        document.write('name="APIAdapter"');
        document.write('width="1"');
        document.write('mayscript= "true">');
        document.write('</applet>');
        document.write('</comment>');
        document.write('</object>');

    }

    function ResizeLegacyFrame(iframe) {
        try {
            var increment = 50;
            if (document.all) // if IE
            {
                if (iframe && document.frames[iframe.name] && document.frames[iframe.name].document && document.frames[iframe.name].document.body) {
                    var newHeight = document.frames[iframe.name].document.body.scrollHeight;
                    newHeight = (Math.floor((newHeight - 1) / increment) + 1) * increment;
                    iframe.style.height = newHeight;
                }
            }
            else {
                if (iframe && iframe.contentDocument.body) {
                    var newHeight = getIFrameDocument(iframe).body.offsetHeight;
                    newHeight = (Math.floor((newHeight - 1) / increment) + 1) * increment;
                    iframe.height = newHeight;
                }
            }
        }
        catch (err) {
        }
    }

    function eventKeyDownNumericOnly() {
        var str = event.srcElement.value + String.fromCharCode(event.keyCode) + "0";
        event.returnValue = !isNaN(str * 1);
    }


    function getIFrameDocument(frame) {
        var rv = null;
        // if contentDocument exists, W3C  compliant(e.g.Mozilla)
        if (frame.contentDocument)
            rv = frame.contentDocument;
        else // bad Internet Explorer  ;)
            rv = document.frames[aID].document;
        return rv;
    }

    Standard = new function () {
        this.nodeTypes =
                          {
                            ELEMENT_NODE                :  1,
                            ATTRIBUTE_NODE              :  2,
                            TEXT_NODE                   :  3,
                            CDATA_SECTION_NODE          :  4,
                            ENTITY_REFERENCE_NODE       :  5,
                            ENTITY_NODE                 :  6,
                            PROCESSING_INSTRUCTION_NODE :  7,
                            COMMENT_NODE                :  8,
                            DOCUMENT_NODE               :  9,
                            DOCUMENT_TYPE_NODE          : 10,
                            DOCUMENT_FRAGMENT_NODE      : 11,
                            NOTATION_NODE               : 12
                          };
        
        
        this.IsIe = function () { return (navigator.appName == 'Microsoft Internet Explorer'); }
        this.IsIe9OrMore = function () { return this.IsIe() && this.IeVersion() >= 9; }
        this.IeVersion = function(){    
            var v = 3, div = document.createElement('div');
            do { div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->';} 
            while ( div.getElementsByTagName('i')[0] );
            return v > 4 ? v : null;            
        }
        this.getMouseX = function (event) { return this.IsIe() ? window.event.x : event.clientX; }
        this.getMouseY = function (event) { return this.IsIe() ? window.event.y : event.clientY; }
        this.removeNode = function (obj) {
            if (this.IsIe()) {
                return obj.removeNode(true);
            }
            if (obj.parentNode != null) {
                return obj.parentNode.removeChild(obj);
            }
            return null;
        }
        this.getSourceElement = function (event) {
            //debugger;
            var srcElement;
            if (!event) event = window.event;
            if (event.target) srcElement = event.target;
            else if (event.srcElement) srcElement = event.srcElement;
            if (srcElement.nodeType == 3) // defeat Safari bug
                srcElement = targ.parentNode;

            return srcElement;
        }
        this.stopPropagation = function (event) {
            if (window.event) {
                window.event.cancelBubble = true;
            }
            else if (event) {
                event.stopPropagation();
            }
        }
        this.isLeftMouseButton = function (event) {
            return (window.event && document.all) ? (window.event.button == 1) : (event.button == 0);
        }
        this.isRightMouseButton = function (event) {
            return (window.event && document.all) ? (window.event.button == 2) : (event.button == 2);
        }
        this.getTopLeft = function (elm) {
            var x, y = 0;

            //set x to elm’s offsetLeft
            x = elm.offsetLeft;

            //set y to elm’s offsetTop
            y = elm.offsetTop;

            //set elm to its offsetParent
            elm = elm.offsetParent;

            //use while loop to check if elm is null
            // if not then add current elm’s offsetLeft to x
            //offsetTop to y and set elm to its offsetParent

            while (elm != null) {

                x = parseInt(x) + parseInt(elm.offsetLeft);
                y = parseInt(y) + parseInt(elm.offsetTop);
                elm = elm.offsetParent;
            }

            //here is interesting thing
            //it return Object with two properties
            //Top and Left

            return { Top: y, Left: x };
        }
        this.getHeight = function (element) {
            return element.clientHeight;
        }
        this.getWidth = function (element) {
            return element.clientWidth;
        }
        this.createElementWithName = function (tagName, name) {
            if (document.all && !this.IsIe9OrMore()) {
                return document.createElement('<' + tagName + ' name="' + name + '" />');
            }
            else {
                var element = document.createElement(tagName);
                element.name = name;
                return element;
            }
        }
        this.isNodeOfTypeElement = function(node) { return node && node.nodeType == Standard.nodeTypes.ELEMENT_NODE; }
        this.getKeyCode = function(event) {
            var code;
            if (!event) var event = window.event;
            if (event.keyCode) code = event.keyCode;
            else if (event.which) code = event.which;
            
            return code;
        }
        this.getInnerText = function(element) { return this.IsIe() ? element.innerText : element.textContent; }
        this.setInnerText = function(element, value) {
           if (this.IsIe()) {
               element.innerText = value;
           }
           else
           {
                element.textContent = value;
           }
        }
        this.setCapture = function(element, value) { 
            if (this.IsIe() && element) {
                element.setCapture(value);
            }
        }
        this.releaseCapture = function(element, value) { 
            if (this.IsIe() && element) {
                element.releaseCapture();
            }
        }
        this.insertAdjacentHTML = function(element, where, htmlStr) {
            if (this.IsIe()) {
                element.insertAdjacentHTML(where, htmlStr);
            }
            else {
                var r = element.ownerDocument.createRange();
                r.setStartBefore(element);
                var parsedHTML = r.createContextualFragment(htmlStr);
                this.insertAdjacentElement(element, where, parsedHTML)
            }
        }
        this.insertAdjacentElement = function(element, where,parsedNode) {
                switch (where.toLowerCase()){
                case 'beforebegin':
                        element.parentNode.insertBefore(parsedNode,element)
                        break;
                case 'afterbegin':
                        element.insertBefore(parsedNode,element.firstChild);
                        break;
                case 'beforeend':
                        element.appendChild(parsedNode);
                        break;
                case 'afterend':
                        if (element.nextSibling) element.parentNode.insertBefore(parsedNode,element.nextSibling);
                        else element.parentNode.appendChild(parsedNode);
                        break;
                }
        }
        this.attachEvent = function (element, trigger, funct) {
            if (document.all) {
                element.attachEvent(trigger, funct);
            } else {
                if (trigger.substring(0, 2).toLowerCase() == 'on')
                    element.addEventListener(trigger.substring(2).toLowerCase(), funct, false);
            }
        }
        this.getChildElementById = function (element, id) {
            for(var i = 0 ; i < element.children.length ; i++) {
                
                var child = element.children[i];
                if (child.id == id) {
                    return child;
                }                
            }
            return null;
        }
        this.isChrome = function() {
            return navigator.appVersion.indexOf('Chrome') > 0;
        }

        
        
    };
