ROR function InitpopupManager :\n" + err)
    }
}

function openPopup(piId, psUrl) {
    try {
        var iWidth, iHeight, iPosX, iPosY, iPopupType;
        var sDependent, sAlwaysraised, sResizable, strArgs;
        sDependent    = "yes";
        sAlwaysraised = "yes";
        sResizable    = "no";
        iPopupType    = POPTYPE_GENERIC;

        //Prevent the opening of popups with the same id 
        closePopupById(piId);

        switch (piId) {
            case POPUP_PRONUNCIATION:
                iWidth  = 325;
                iHeight = 300;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_THESAURUS:
                iWidth  = 400;
                iHeight = 350;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_COMMONERROR:
                iWidth  = 400;
                iHeight = 350;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_MENU:
                iWidth     = 350;
                iHeight    = 300;
                iPosX      = 300;
                iPosY      = 250;
                iPopupType = POPTYPE_PERMANENT;
                break;
            case POPUP_WORDORIGIN:
                iWidth  = 300;
                iHeight = 250;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_VERBFORMS:
                iWidth  = 600;
                iHeight = 500;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_WORDSET:
                iWidth  = 300;
                iHeight = 350;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_USERNOTES:
                iWidth  = 536;
                iHeight = 376;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_PICTURE:
                iWidth  = 400;
                iHeight = 450;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_MAP:
                iWidth  = 400;
                iHeight = 450;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_EXAMPLEBANK:
                iWidth  = 400;
                iHeight = 450;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_PHRASEBANK:
                iWidth  = 400;
                iHeight = 450;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_DICSEARCH:
                iWidth  = 475;
                iHeight = 450;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_MULTIMEDIA_SCH:
                iWidth  = 510;
                iHeight = 200;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_SUBJECT_SCH:
                iWidth  = 600;
                iHeight = 250;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_WORDORIGIN_SCH:
                iWidth  = 515;
                iHeight = 250;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_PRONUNCIATION_SCH:
                iWidth  = 515;
                iHeight = 420;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_ACADEMIC_SCH:
                iWidth  = 350;
                iHeight = 400;
                iPosX   = 300;
                iPosY   = 250;
                iPopupType = POPTYPE_PERMANENT;
                break;
            case POPUP_POPMODE:
                iWidth  = 350;
                iHeight = 420;
                iPosX   = 300;
                iPosY   = 250;
                break;
            case POPUP_RESLIST:
                iWidth     = 300;
                iHeight    = 300;
                iPosX      = 300;
                iPosY      = 250;
                sResizable = "yes";
                iPopupType = POPTYPE_PERMANENT;
                break;
            case POPUP_HELP:
                iWidth  = 760;
                iHeight = 450;
                iPosX   = 300;
                iPosY   = 250;
                sDependent    = "no";
                sAlwaysraised = "no";
                sResizable    = "yes";
                iPopupType    = POPTYPE_MAIN;
                break;
        }
    	
        var sArgs = "chrome=yes,modal=no,titlebar=yes"
                  + ",width=" + iWidth + ",height=" + iHeight
                  + ",left=" + iPosX + ",top=" + iPosY
                  + ",dependent=" + sDependent + ",alwaysraised=" + sAlwaysraised + ",resizable=" + sResizable;
        var oPopup = getSkPopupManager().OpenPopup(psUrl, sArgs, iPopupType, piId);
        oPopup.popupid = piId;
        return oPopup;
    } catch (err) {
       //alert("ERROR function openPopup:\n" + err);
    }
}

/*Function used to close the popup window from its Id*/
function closePopupById(psIdWindow) {
    try {
        try {
            window.focus();
        } catch( e ) { }
        getSkPopupManager().ClosePopupById(psIdWindow);
    } catch (err) {
        throw("ERROR function closePopupById :\n" + err)
    }
}

/* Function used to close popups of a defined type */
function closePopupByType(piWindowType) {
    try {
        try {
            window.focus();
        } catch( e ) { }
        getSkPopupManager().ClosePopupByType(piWindowType);
    } catch (err) {
        throw("ERROR function closePopupByType :\n" + err)
    }
}

function closePopupsOfType(paWindowType) {
    try {
        for (var i=0; i<paWindowType.length; i++)
            closePopupByType(paWindowType[i]);
    } catch (err) {
        throw("ERROR function closePopupsOfType :\n" + err)
    }
}

function loadContentPopup(poWindow) {
    try {
        var oIframe, oIframeBody, oBody, oImg, oDoc, oDiv;
        var sImgContent, sMapContent;
        var iPopupType = poWindow.popupid;

        oIframe = poWindow.document.getElementById("popupIframe");
        if (oIframe)
            oIframeBody = oIframe.contentDocument.getElementsByTagName("BODY").item(0);

        switch (iPopupType) {
            case POPUP_PRONUNCIATION:
                loadPronPopup(poWindow);
                break;
            case POPUP_THESAURUS:
                oIframeBody.innerHTML = GetHtmlFs(giOpenEntry, "thesarusFs", f_thesaurus);
                break;
            case POPUP_MENU:
                oIframeBody.innerHTML = GetHtmlFs(giOpenEntry, "contentFs", f_content);
                break;
            case POPUP_WORDORIGIN:
                //setPopupTitle(poWindow);
                oDoc = document.getElementById("frame_dictionary").contentDocument;
                oDiv = oDoc.getElementById(giOpenEntry);
                try {
                    oIframeBody.innerHTML = oDiv.parentNode.parentNode.previousSibling.previousSibling.innerHTML;
                } catch (e) { }
                break;
            case POPUP_VERBFORMS:
                //setPopupTitle(poWindow);
                oIframeBody.innerHTML = GetHtmlFs(giOpenEntry, "verbFs", f_verb);
                break;
            case POPUP_WORDSET:
                initWordsetPopup(oIframe);
                break;
            case POPUP_COMMONERROR:
                    oIframeBody.innerHTML = GetHtmlFs(giOpenEntry, "commonerrorFs", f_commonerror);
                break;
            case POPUP_PICTURE:
                oImg = poWindow.document.getElementById("popup_picture");
                oImg.setAttribute("src", poWindow.srcMedia);
                //window.setTimeout("window.sizeToContent()", 100);
                break;
            case POPUP_MAP:
                sImgContent     = "<img border=\"0\" src=\"" + poWindow.srcMedia + "\"/ usemap=\"mapMedia\">";
                sMapContent     = "<map name=\"mapMedia\">" + GetHtmlFs(poWindow.iDocId, "htmlMapFs", f_map) + "</map>";
                oBody           = poWindow.document.getElementById("popup_map").contentDocument.getElementsByTagName("body")[0];
                oBody.innerHTML = sImgContent + sMapContent;
                poWindow.setTimeout("resizeHtmlMapPopup()", 100);
                break;
            case POPUP_EXAMPLEBANK:
                oIframeBody.innerHTML = document.getElementById("iExample").contentDocument.getElementsByTagName("body")[0].innerHTML;
                break;
            case POPUP_PHRASEBANK:
                oIframeBody.innerHTML = document.getElementById("iPhrase").contentDocument.getElementsByTagName("body")[0].innerHTML;
                break;
            case POPUP_RESLIST:
                initResultList(poWindow);
                break;
            case POPUP_ACADEMIC_SCH:
                initAcademicResultList(poWindow);
                break;
            case POPUP_POPMODE:
                initPopupmode_ctxt(poWindow, POPMODE_INTERNAL);
                break;
        }
    } catch (err) {
       //alert("ERROR function loadContentPopup:\n" + err);
    }
}

function resizeHtmlMapPopup() {
    try {
        var oIframe  = document.getElementById("popup_map");
        var oPicture = oIframe.contentDocument.getElementsByTagName('img')[0];
        var iWidth   = 40 + oPicture.offsetWidth;
        var iHeight  = 130 + oPicture.offsetHeight;
        window.resizeTo(iWidth, iHeight);
    } catch (err) {
        //alert("ERROR function resizeHtmlMapPopup:\n" + err);
    }
}

function initResultList(poWindow) {
    try {
        var oVirtualList;
        var iCount;

        oVirtualList = poWindow.document.getElementById("hwdList");
        //List of categories ("word set" > category)
        if (goCategoryRs)
            iCount = goCategoryRs.GetCount();
        //List of the advanced search results
        if (gaResultIds) {
            if(!giResultFreq)
                iCount = gaResultIds.length;
            else
                iCount = giResultFreq;
        }

        //List of the phrasal search reults
        if (gaResultPhrIds) {
            oVirtualList                         = poWindow.document.getElementById("phrList");
            iCount                               = gaResultPhrIds.length;
            oVirtualList.myFuncGetContent        = "parent.opener.getResultPhr(i)";
            oVirtualList.HtmlStringTooltip       = "<div class='divTooltipParent' xmlns='http://www.w3.org/1999/xhtml'><div class='divTooltip' xmlns='http://www.w3.org/1999/xhtml'> </div></div>";
            oVirtualList.myFuncGetTooltipContent = "parent.opener.getPhrTooltipText(iIndex)";
            oVirtualList.maxElement              = iCount - 1;
        } else {
            oVirtualList.myFuncGetContent        = "parent.opener.getResultHwd(i)";
            oVirtualList.HtmlStringTooltip       = "<div class='divTooltipParent' xmlns='http://www.w3.org/1999/xhtml'><div class='divTooltip' xmlns='http://www.w3.org/1999/xhtml'> </div></div>";
            oVirtualList.myFuncGetTooltipContent = "parent.opener.getTooltipText(iIndex);"
            if (goCategoryRs)
                oVirtualList.maxElement = goCategoryRs.GetCount()-1;
            if (gaResultIds)
                oVirtualList.maxElement = gaResultIds.length - 1 ;
        }

        oVirtualList.collapsed     = false;
        oVirtualList.xblChildClass = "xblHwdResult";
        oVirtualList.LoadDico();

        /*
        var oToolbar = poWindow.document.getElementById("popuptoolbar");
        if(gsSearchAdv) {
            oToolbar.setAttribute("titlePopup", iCount + " results found for");
            oToolbar.setAttribute("titlePopupHwd"," " + gsSearchAdv);
        } else
            oToolbar.setAttribute("titlePopup", iCount + " results found");
        
        gsSearchAdv = null;
        */
    } catch (err) {
        //alert("ERROR function initResultList:\n" + err);
    }
}

function initAcademicResultList(poWindow){
    try {
        var oVirtualList                     = poWindow.document.getElementById("acaList");
        goAcademicTab                        = GetSkTab("academicTab");
        oVirtualList.myFuncGetContent        = "parent.opener.getResultAca(i)";
        oVirtualList.HtmlStringTooltip       = "<div class='divTooltipParent' xmlns='http://www.w3.org/1999/xhtml'><div class='divTooltip' xmlns='http://www.w3.org/1999/xhtml'> </div></div>";
        oVirtualList.myFuncGetTooltipContent = "parent.opener.getAcaTooltipText(iIndex);"
        oVirtualList.maxElement              = goAcademicTab.GetCount()-1;
        oVirtualList.collapsed               = false;
        oVirtualList.xblChildClass           = "xblAcaResult";
        oVirtualList.LoadDico();
    } catch (err) {
       //alert("ERROR function initAcademicResultList:\n" + err);
    }
}

//Result list of the phrasal search : Get the content of the virtuallist
function getResultPhr(piPhr) {
    try {
        return goDomParser.parseFromString(getResultPhrText(piPhr, "hwdResult"), "text/xml");
    } catch (err) {
        //alert("ERROR function getResultPhr:\n" + err);
    }
}

function getResultPhrText(piPhr, psClass) {
    try {
        var iPhrListId = gaResultPhrIds[piPhr];
        var oPhrlistTab = GetSkTab("phrlistTab");
        var oLdocectxtTab = GetSkTab("ldocectxTab");
        var sPhr = GetBinaryField(oPhrlistTab, iPhrListId, "phrlist");
        sPhr = sPhr.substr(sPhr.indexOf("__")+2); //Get only the end of phrlist (phrase = text after "__")
        sPhr = ConvertToUnicode(sPhr, "utf-8");

        var oLdocectxtCursor = getCursorFromTable(oPhrlistTab, iPhrListId, "r_phrlist");
        var iLdocectxt = oLdocectxtCursor.GetElement(0);
        var iDocId = GetUNumField(oLdocectxtTab, iLdocectxt, "entryid");
        var iPhraseId = GetUNumField(oLdocectxtTab, iLdocectxt, "phraseid");
        return "<div id='doc" + piPhr + "' hwdid='" + iDocId + "' phrid='" + iPhraseId + "' phrctxt='" + iLdocectxt + "' class='" + psClass + "' xmlns='http://www.w3.org/1999/xhtml'>" + sPhr + "</div>";
    } catch (err) {
        //alert("ERROR function getResultPhr:\n" + err);
    }
}

/* Result list of the phrasal search : Get the content of the tooltip */
function getPhrTooltipText(piPhr) {
    try {
        var iPhrId      = gaResultPhrIds[piPhr];
        var oPhrlistTab = GetSkTab("phrlistTab");
        var sPhr        = GetSField(oPhrlistTab, iPhrId, "phrlist");
        sPhr            = sPhr.substr(sPhr.indexOf("__")+2); //Get only the end of phrlist (phrase = text after "__")
        sPhr            = ConvertToUnicode(sPhr, "utf-8");
        return sPhr;
    } catch (err) {
        //alert("ERROR function getPhrTooltipText:\n" + err);
    }
}

function getTooltipText(piHwd) {
    try {
        var iHwdId;

        if (goCategoryRs)
            iHwdId = GetUNumField(goCategoryRs, piHwd, "targetid");
        if (gaResultIds)
            iHwdId = gaResultIds[piHwd];

        var oDocTab = GetSkTab("docTab");
        var sHwd = GetSField(oDocTab, iHwdId, "doc");
        sHwd = ConvertToUnicode(sHwd, "utf-8");
        return sHwd;
    } catch (err) {
        //alert("ERROR function getTooltipText:\n" + err);
    }
}

function giveWordsetList() {
    try {
        var oDocIframe = document.getElementById("frame_dictionary").contentDocument;
        var oBtnentriesList =  oDocIframe.getElementsByTagName("xblbtnentries");
        var oSubjectTable;
        var aWordset = new Array();
        var aWordsetId = new Array();
        var aCategory;
        var sHwdId;
        var i, j, iCatId;

        if (oBtnentriesList.length > 0) {
            sHwdId = String(oBtnentriesList[0].id);
            //remove the useless space at the beginning of the list
            if(sHwdId.charAt(0)!=-1)
                sHwdId = sHwdId.substring(1);
            aCategory = sHwdId.split(" ");
            aCategory = removeDoublons(aCategory);

            oSubjectTable = GetSkTab("subjectTab");
            for (i=0; i<aCategory.length; i++) {
                iCatId        = aCategory[i];
                aWordset[i]   = GetSField(oSubjectTable, iCatId, "subject");
                aWordsetId[i] = iCatId;
            }
        }

        return new Array(aWordsetId, aWordset);
    } catch (err) {
       //alert("ERROR function giveWordsetList:\n" + err);
    }
}

function removeDoublons(inArray) {
    try {
        var outArray = new Array();
        var bIsDbl;
        for(var i=0;i<inArray.length;i++) {
            bIsDbl=false;
            for(var j=0;j<outArray.length;j++) {
                if(parseInt(inArray[i]) == parseInt(outArray[j])) {
                    bIsDbl=true;
                    break;
                }
            }
            if(!bIsDbl)
                outArray[outArray.length] = inArray[i];
        }
        return outArray;
    } catch (err) {
       //alert("ERROR function removeDoublons:\n" + err);
    }
}

//Function called when wordset.html is loading
function initWordsetPopup(poIframe) {
    try {
        var oRsParent;
        var i, j, iCatId, iParentId;
        var aWordsetList  = giveWordsetList();
        var oSubjectTable = GetSkTab("subjectTab");
        var oIframe       = document.getElementById("popupIframe");
        var aWrongCat     = new Array();
        var sContent      = "";
        var bInArray;

        //Get only specific categories
        for (i=0; i<aWordsetList[0].length; i++) {
            iCatId = aWordsetList[0][i];
            oRsParent = GetLinkField(oSubjectTable, iCatId, "r_parent");
            if (oRsParent.GetCount() > 0) {
                iParentId = GetUNumField(oRsParent, 0, "parent");
                bInArray = false;
                //Check if the parent of the current category is in the list
                for (j=0; j<aWrongCat.length; j++) {
                    if (aWrongCat[j] == iParentId) {
                        bInArray = true;
                        break;
                    }
                }
                if (!bInArray)
                    aWrongCat[aWrongCat.length] = iParentId;
            }
        }

        //Display categories
        for (i=0; i<aWordsetList[0].length; i++) {
            bInArray = false;
            for (j=0; j<aWrongCat.length; j++) {
                if (aWrongCat[j] == aWordsetList[0][i])
                    bInArray = true;
            }
            if(!bInArray)
                sContent += "<tr><td class=\"category\" onclick=\"parent.opener.showSubjectBody(" + aWordsetList[0][i] + ",'" + aWordsetList[1][i] + "');\">" + aWordsetList[1][i] + "</tr>";
        }
        poIframe.contentDocument.getElementsByTagName("table")[0].innerHTML += sContent;
    } catch (err) {
       //alert("ERROR function initWordsetPopup:\n" + err);
    }
}

function showSubjectBody(piCatId, psCatTitle) {
    try {
        var oSubjectTable = GetSkTab("subjectTab");
        gaResultIds       = null;
        gaResultPhrIds    = null;

        //Get & sort the category recordset
        goCategoryRs = GetLinkField(oSubjectTable, piCatId, "r_target");
        SortRecordset(goCategoryRs, "targetid", "subjectTargetTab");

        openPopup(POPUP_RESLIST, 'chrome://led/content/popup/popup_reslist.xul');
        closePopupById(POPUP_WORDSET);
    } catch (err) {
       //alert("ERROR function showSubjectBody:\n" + err);
    }
}

/* Result list : Get the content of the virtuallist */
function getResultHwd(piHwd) {
    try {
        var iHwdId;

        if (goCategoryRs)
            iHwdId = GetUNumField(goCategoryRs, piHwd, "targetid");
        if (gaResultIds)
            iHwdId = gaResultIds[piHwd];

        var sContent = getResultLabel(iHwdId);

        return goDomParser.parseFromString("<div id='doc" + piHwd + "' hwdid='" + iHwdId + "' class='hwdResult' xmlns='http://www.w3.org/1999/xhtml'>" + sContent + "</div>", "text/xml");
    } catch (err) {
        //alert("ERROR function getResultHwd:\n" + err);
    }
}

function getResultLabel(piHwd) {
    try {
        var oDocTab  = GetSkTab("docTab");
        var sHwd     = GetSField(oDocTab, piHwd, "doc");
        var sHommnum = GetSField(oDocTab, piHwd, "homnum");
        var sPos     = GetSField(oDocTab, piHwd, "pos");
        var sContent = ConvertToUnicode(sHwd, "utf-8");

        if (sHommnum)
            sContent += "<sup>" + sHommnum + "</sup>";
        if (sPos)
            sContent += ", <i>" + sPos + "</i>";
        return sContent;
    } catch (err) {
       //alert("ERROR function getResultLabel:\n" + err);
    }
}

//Academic result list - get the contents of the virtual list
function getResultAca(piHwd) {
    try {
        var iHwdId   = GetUNumField(goAcademicTab, piHwd, "entryid");
        var sContent = getResultLabel(iHwdId);

        return goDomParser.parseFromString("<div id='doc" + piHwd + "' hwdid='" + iHwdId + "' class='hwdResult' xmlns='http://www.w3.org/1999/xhtml'>" + sContent + "</div>", "text/xml");
    } catch (err) {
        //alert("ERROR function getResultAca:\n" + err);
    }
}

function getAcaTooltipText(piHwd) {
    try {
        var iHwdId  = GetUNumField(goAcademicTab, piHwd, "entryid");
        var oDocTab = GetSkTab("docTab");
        var sHwd    = GetSField(oDocTab, iHwdId, "doc");
        sHwd = ConvertToUnicode(sHwd, "utf-8");
        return sHwd;
    } catch (err) {
        //alert("ERROR function getAcaTooltipText:\n" + err);
    }
}
@