y functions...)==== */

function ScrollToElt(Elt,FrameElt,MoveScroll,toTop) {
    try {
        var height = 0;
        var winFrame = FrameElt.contentWindow;
        var docFrame = FrameElt.contentDocument;
        var savHeight = 0;
        try {
            while( Elt != docFrame && Elt.nodeName!="BODY") {
                height += Elt.offsetTop;
                Elt = Elt.offsetParent;
            }
        } catch (err) { }

        //Scroll to the element
        var ScrollVal = height;

        //The element must be shown at the middle of the frame
        if(!toTop)
            ScrollVal = ScrollVal - (winFrame.innerHeight/2);

        //Scroll to the element even if it is already displayed on the screen
        if (!MoveScroll) {
            if ((winFrame.scrollY + winFrame.innerHeight < (height + 20))||(winFrame.scrollY > (height-20)))
                winFrame.scrollTo(0,ScrollVal);
        } else
            winFrame.scrollTo(0,ScrollVal);
    } catch (err) {
       //alert("ERROR function ScrollToElt:\n" + err);
    }
}

function emptyIframe(poWindowContainer, psIframeId) {
    try {
        var oIframe     = poWindowContainer.document.getElementById(psIframeId);
        var oBody       = oIframe.contentDocument.getElementsByTagName("body")[0];
        oBody.innerHTML = "";
        oBody.scrollIntoView(true);
    } catch (err) {
       //alert("ERROR function emptyIframe:\n" + err);
    }
}

function loadAutocomplete(piTab) {
    try {
        var oTextBox;
        
        //if((piTab == APP_DICTIONARY || piTab == APP_ACTIVATOR) && oTextBox.sessionCount)
        //    oTextBox.clearSession();

        switch(piTab) {
            case APP_DICTIONARY:
                oTextBox = document.getElementById("autocomp-dico");

                if (goSessionDico)
                    return;

                //if (goSessionDico)
                //    oTextBox.addSession(goSessionDico);
                //else {
                    oTextBox.setLinkTable("data/index/hwd.skn", "r_target", "targetid");
                    oTextBox.addFields("data/index/doc.skn", "doc", "pos", " ");
                    goSessionDico = oTextBox.getSession(0);
                //}
                break;
            case APP_ACTIVATOR:
                oTextBox = document.getElementById("autocomp-acti");

                if (goSessionActi)
                    return;

                //if (goSessionActi)
                //    oTextBox.addSession(goSessionActi);
                //else {
                    oTextBox.setLinkTable("data/index/exp.skn", "r_exp", "targetid");
                    oTextBox.addFields("data/index/expword.skn", "lookup", "", "", " : ", "", "r_expword", "targetid");
                    oTextBox.addFields("data/index/explist.skn", "explist");
                    goSessionActi = oTextBox.getSession(0);
                //}
                break;
            case APP_POPUPMODE:
                var oSession;
                oTextBox = document.getElementById("autocomp-pm");

                if (goSessionDico || goSessionPM) {
                    oSession = (goSessionDico ? goSessionDico : goSessionPM);
                    oTextBox.addSession(oSession);
                } else {
                    oTextBox.setLinkTable("data/index/hwd.skn", "r_target", "targetid");
                    oTextBox.addFields("data/index/doc.skn", "doc", "pos", " ");
                    goSessionPM = oTextBox.getSession(0);
                }
                break;
        }
        
        oTextBox.focus();
    } catch (err) {
       //alert("ERROR function loadAutocomplete:\n" + err);
    }
}

function AlertMsg(Message,Title) {
    const kPromptSvcContractID = "@mozilla.org/embedcomp/prompt-service;1";
    const kPromptSvcIID = Components.interfaces.nsIPromptService;
    const kPromptSvc = Components.classes[kPromptSvcContractID].getService(kPromptSvcIID);
    kPromptSvc.alert(window, Title, Message)
}

function TrimString(inputString) {
    if (typeof inputString != "string") {
        return inputString;
    }
    return inputString.replace(/(^\s+)|(\s+$)/g, '');
}

function launchQFListener() {
    try {
        QFClient.addListenerFunc( Listener );
    } catch (err) {}
}

function closeApp() {
    try {
        //Close QuickFind
        var oMainWindow = document.getElementById('content').contentWindow;
        oMainWindow.QFClient.disconnect( );
    } catch (err) { }
    killApp();
}

function killApp() {
    try {
        var plip = Components.classes["@mozilla.org/commandlinehandler/general-startup;1?type=killAll"].createInstance().QueryInterface(Components.interfaces.nsICmdLineHandler);
        plip.chromeUrlForTask();
    } catch (err) {
        top.window.close();
    }
}

function resizeApp(psMnem) {
    try {
        var iPosX   = setLed.GetPrefSize(psMnem + ".x", (psMnem == "PM" ? 250 : (screen.availWidth-DEFAULTWIN_WIDTH)/2));
        var iPosY   = setLed.GetPrefSize(psMnem + ".y", (psMnem == "PM" ? 200 : (screen.availHeight-DEFAULTWIN_HEIGHT)/2));
        var iWidth  = setLed.GetPrefSize(psMnem + ".w", (psMnem == "PM" ? 350 : DEFAULTWIN_WIDTH));
        var iHeight = setLed.GetPrefSize(psMnem + ".h", (psMnem == "PM" ? 420 : DEFAULTWIN_HEIGHT));
        iWidth      = (iWidth < 150 ? DEFAULTWIN_WIDTH : iWidth);
        iHeight     = (iHeight < 50 ? DEFAULTWIN_HEIGHT : iHeight);
        window.moveTo(iPosX, iPosY);
        window.resizeTo(iWidth, iHeight);    	
    } catch (err) {
       //alert("ERROR function resizeApp:\n" + err);
    }
}

function isInScreen() {
    if (window.screenX > screen.width) //position X outside the screen
        return false;

    if (window.screenX < (0 - window.innerWidth - 10)) //left part of the window outside the screen
        return false;

    if (window.screenY > screen.height) //bottom part of the window outside the screen
        return false;

    if (window.screenY < (0 - window.innerWidth -10 )) //top part of the window outside the screen
        return false;

    return true;
}

function saveAppPref() {
    try {
        //Don't save param if the window is out of screen
        if (!isInScreen())
            return;

        setLed.SetPrefSize( (document.getElementById("ov_popupmode").collapsed ? "DIC" : "PM") );
    } catch(err) {
        //alert("ERROR FUNCTION saveAppPref" + err);
    }
}

//Use to display the tooltip
function FillInHTMLTooltip(tipElement,tipNode) {
    var retVal = false;
    if (tipElement.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
        return retVal;
    const XLinkNS = "http://www.w3.org/1999/xlink";

    var titleText = null;
    var XLinkTitleText = null;

    while (!titleText && !XLinkTitleText && tipElement) {
        if (tipElement.nodeType == Node.ELEMENT_NODE) {
            titleText = tipElement.getAttribute("title");
            XLinkTitleText = tipElement.getAttributeNS(XLinkNS, "title");
        }
        tipElement = tipElement.parentNode;
    }

    var texts = [titleText, XLinkTitleText];

    for (var i = 0; i < texts.length; ++i) {
        var t = texts[i];
        if (t && t.search(/\S/) >= 0) {
            tipNode.setAttribute("label", t);
            retVal = true;
        }
    }

    return retVal;
}
