AddBasket(this.baskname);
            this.baskidx = this.GetIndexBask();
        } else
            this.hist.EmptyBasket(this.baskidx);

        this.idx = this.hist.BasketContentCount(this.baskidx);
    } catch (err) {
        //alert("ERROR function SettingHistory: " + err);
    }
}

function GetIndexBask() {
    try {
        return this.hist.GetBasketIndexByName(this.baskname);
    } catch (err) {
        //alert("ERROR function GetIndexBask:\n" + err);
    }
}

function GetBasketContentCount() {
    try {
        return this.hist.BasketContentCount(this.baskname);
    } catch (err) {
        //alert("ERROR function GetBasketContentCount:\n" + err);
    }
}

function AddInDicoBasket(psValeur) {
    try {
        //Remove elements at the end of the history (if the current position is not the last)
        if (this.idx < this.hist.BasketContentCount(this.baskidx)-1) {
            for (var i=this.hist.BasketContentCount(this.baskidx)-1; i>this.idx; i--)
                this.hist.RemoveElement(this.baskidx, i);
        }

        //Prevent the insertion of duplicated elements
        if ( this.hist.BasketContentCount(this.baskidx) > 0 
             && (psValeur == this.hist.ReadNameAt(this.baskidx, this.hist.BasketContentCount(this.baskidx)-1)) )
            return;

        this.hist.AddContentInBasket("title", psValeur, this.baskidx);
        this.idx = this.hist.BasketContentCount(this.baskidx)-1;
        this.manageHistoBtn((this.idx <= 0), true);
    } catch (err) {
        //alert("ERROR function AddInDicoBasket:\n" + err);
    }
}

function NavigInHisto(piIncrement) {
    try {
        this.idx          = Number(this.idx) + Number(piIncrement);
        var iIndexElement = this.idx;
        this.manageHistoBtn( (iIndexElement == 0),
                             (iIndexElement >= this.hist.BasketContentCount(this.baskidx)-1) );
        var sElement = this.hist.ReadNameAt(this.baskidx, iIndexElement);
        var oXml     = goDomParser.parseFromString(sElement, "text/xml");
        this.ShowHistoryItem(oXml);
    } catch (err) {
        //alert("ERROR function NavigInHisto:\n" + err);
    }
}

function SetButtonState() {
    try {
        var oBackBtn   = document.getElementById((gbHelpContext ? "help-": "") + "histo-back");
        var oNextBtn   = document.getElementById((gbHelpContext ? "help-": "") + "histo-next");
        var oPMBackBtn = document.getElementById("pm-histo-back");

        oBackBtn.disabled    = (this.idx == 0);
        oPMBackBtn.disabled  = (this.idx == 0);
        oNextBtn.disabled    = (this.idx >= this.hist.BasketContentCount(this.baskidx)-1);
    } catch (err) {
        //alert("ERROR function SetButtonState:\n" + err);
    }
}

function manageHistoBtn(pbBackDisa, pbNextDisa) {
    try {
        var oBackBtn   = document.getElementById((gbHelpContext ? "help-": "") + "histo-back");
        var oNextBtn   = document.getElementById((gbHelpContext ? "help-": "") + "histo-next");
        var oPMBackBtn = document.getElementById("pm-histo-back");

        oBackBtn.disabled = pbBackDisa;
        oNextBtn.disabled = pbNextDisa;
        if (!gbHelpContext && oPMBackBtn)
            oPMBackBtn.disabled = pbBackDisa;
    } catch (err) {
        //alert("ERROR function manageHistoBtn: " + err);
    }
}

function ShowHistoryItem(poXml) {
    try {
        var oQuerybox, oRadioGroup, oSearchNode;
        var iSeletedTab, iIndex, iForcePhr, iContext, iFreqW, iFreqS, iPos, iLabel, iUnfolded, iPicture;
        var iSound, iSubject, iLanguage, iCentury, iHwd, iPhr, iTab, iSubtab, iExpo;
        var sSearch, sHwdlist, sFilename;
        var bSearchPhr;

        oRadioGroup = document.getElementById("toolbar-radiogroup");

        if (oRadioGroup)
            iSeletedTab = Number(oRadioGroup.getAttribute("selected"));

        if (iSeletedTab == APP_POPUPMODE)
            oQuerybox = document.getElementById("autocomp-pm");
        else
            oQuerybox = document.getElementById("autocomp-dico");

        switch (poXml.firstChild.getAttribute('type')) {
            case "search":
                //Simple search (Dictionary & Activator)
                sSearch = poXml.firstChild.getElementsByTagName("SEARCHSTRING")[0].firstChild.data;
                iForcePhr = poXml.firstChild.getAttribute("forcephr");
                bSearchPhr = (Number(iForcePhr) == 1 ? true : false);
                oQuerybox.value = sSearch;
                oQuerybox.valueComment = null;
                ExecSearch(true, bSearchPhr);
                break;
            case "autocomplete":
                //Selected item in the autocomplete (Dictionary & Activator)
                sSearch = poXml.firstChild.getElementsByTagName("SEARCHSTRING")[0].firstChild.data;
                iIndex = poXml.firstChild.getAttribute("selected");
                oQuerybox.value = sSearch;
                oQuerybox.valueComment = iIndex;
                ExecSearch(true, false);
                break;
            case "advsearch":
                //Advanced Search (Dictionary)
                oSearchNode = poXml.firstChild.getElementsByTagName("SEARCHSTRING")[0].firstChild;
                sSearch  = (oSearchNode ? oSearchNode.data : "");
                iContext = poXml.firstChild.getAttribute("ctxt");
                iFreqW   = poXml.firstChild.getAttribute("freqw");
                iFreqS   = poXml.firstChild.getAttribute("freqs");
                iPos     = poXml.firstChild.getAttribute("pos");
                iLabel   = poXml.firstChild.getAttribute("label");
                ExecAdvSearch(true, false, true, sSearch, iContext, iFreqW, iFreqS, iPos, iLabel);
                break;
            case "multimediasearch":
                //Multimedia Search (Dictionary)
                sSearch = poXml.firstChild.getElementsByTagName("SEARCHSTRING")[0].firstChild.data;
                iPicture = (poXml.firstChild.getAttribute("picture") == "true") ? true : false;
                iSound   = (poXml.firstChild.getAttribute("sound") == "true") ? true : false;
                ExecMultimediaSearch(true, false, true, sSearch, iPicture, iSound);
                break;
            case "subjectsearch":
                //Subject Search (Dictionary)
                iSubject = poXml.firstChild.getAttribute("subject");
                ExecSubjectSearch(true, false, true, iSubject);
                break;
            case "wosearch":
                //Word origin Search (Dictionary)
                iLanguage = poXml.firstChild.getAttribute("language");
                iCentury  = poXml.firstChild.getAttribute("century");
                ExecWordOriginSearch(true, false, true, iLanguage, iCentury);
                break;
            case "pronsearch":
                //Pronunciation Search (Dictionary)
                sSearch = poXml.firstChild.getElementsByTagName("SEARCHSTRING")[0].firstChild.data;
                ExecPronunciationSearch(true, false, true, sSearch);
                break;
            case "unfoldhwd":
                //Headword list (Dictionary)
                sHwdlist  = poXml.firstChild.getAttribute("hwdlist");
                iUnfolded = poXml.firstChild.getAttribute("unfolded");
                showHwdlist(sHwdlist, iUnfolded);
                break;
            case "conceptbar":
                //Concept bar (Activator)
                iIndex = poXml.firstChild.getAttribute("selected");
                loadCpt(iIndex, true);
                break;
            case "help":
                sFilename = poXml.firstChild.getAttribute("filename");
                opener.goHelpFsViewer.loadFsDoc(sFilename, false);
                break;
        }
    } catch (err) {
        //alert("ERROR function ShowHistoryItem:\n" + err);
    }
}

function getHistoryXml(piIncrement){
    try {
        var iIndexElement = Number(this.idx) + Number(piIncrement);
        var sElement      = this.hist.ReadNameAt(this.baskidx, iIndexElement);
        return goDomParser.parseFromString(sElement, "text/xml");    	
    } catch (err) {
       //alert("ERROR function getHistoryXml:\n" + err);
    }
}

function getHistoItemNumber(){
    try {
        return this.hist.BasketContentCount(this.baskidx);
    } catch (err) {
       //alert("ERROR function getHistoItemNumber:\n" + err);
    }
}

/*
History functions that stores xml depending of the search
*/

function saveSimpleSearch(poHistory, psSearch, pbForcePhrasal) {
    try {
        var xmlString = "<HISTO type='search' forcephr='" + (pbForcePhrasal ? "1" : "0") + "'><SEARCHSTRING>" + psSearch + "</SEARCHSTRING></HISTO>";
        poHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveSimpleSearch:\n" + err);
    }
}

function saveAutocompleteItem(poHistory, piIndex, psSearch) {
    try {
        var xmlString = "<HISTO type='autocomplete' selected='" + piIndex + "'><SEARCHSTRING>" + psSearch + "</SEARCHSTRING></HISTO>";
        poHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveSelectingItemFromResultList:\n" + err);
    }
}

function saveAdvancedSearch(psSearch, piContext, piFreqW, piFreqS, piPos, piLabel) {
    try {
        var xmlString = "<HISTO type='advsearch' ctxt='" + piContext + "' freqw='" + piFreqW + "' freqs='" + piFreqS + "' pos='" + piPos + "' label='" + piLabel + "'><SEARCHSTRING>" + psSearch + "</SEARCHSTRING></HISTO>";
        setDicoHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveAdvancedSearch:\n" + err);
    }
}

function saveUnfoldHwd(poHistory, psHwdlist, piUnfolded) {
    try {
        var xmlString = "<HISTO type='unfoldhwd' hwdlist='" + psHwdlist + "' unfolded='" + piUnfolded + "'/>";
        poHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveUnfoldHwd:\n" + err);
    }
}

function saveMultimediaSearch(psSearch, piPicture, piSound) {
    try {
        var xmlString = "<HISTO type='multimediasearch' picture='" + piPicture + "' sound='" + piSound + "'><SEARCHSTRING>" + psSearch + "</SEARCHSTRING></HISTO>";
        setDicoHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveMultimediaSearch:\n" + err);
    }
}

function saveSubjectSearch(piSubject) {
    try {
        var xmlString = "<HISTO type='subjectsearch' subject='" + piSubject + "'/>";
        setDicoHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveSubjectSearch:\n" + err);
    }
}

function saveWordOriginSearch(piLanguage, piCentury) {
    try {
        var xmlString = "<HISTO type='wosearch' language='" + piLanguage + "' century='" + piCentury + "'/>";
        setDicoHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveWordOriginSearch:\n" + err);
    }
}

function savePronunciationSearch(psSearch) {
    try {
        var xmlString = "<HISTO type='pronsearch'><SEARCHSTRING>" + psSearch + "</SEARCHSTRING></HISTO>";
        setDicoHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function savePronunciationSearch:\n" + err);
    }
}

function saveActiConceptbarItem(piIndex) {
    try {
        var xmlString = "<HISTO tab='acti' type='conceptbar' selected='" + piIndex + "'/>";
        setActiHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveActiConceptbarItem:\n" + err);
    }
}

function saveHelpDoc(psFilename) {
    try {
        var xmlString = "<HISTO type='help' filename='" + psFilename + "'/>";
        setHelpHistory.AddInDicoBasket(xmlString);
    } catch (err) {
        //alert("ERROR function saveHelpDoc:\n" + err);
    }
}

/* Function that calls the history object to move to the preceding/following stored searches */
function moveInHisto(piIncrement) {
    var oExamCoachMenu = document.getElementById("examcoach_navigation");
    var oRadioGroup    = document.getElementById("toolbar-radiogroup");
    var iContext;

    if (gbHelpContext)
        iContext = APP_HELP;
    else
        iContext = giSelTab

    switch (iContext) {
        case APP_DICTIONARY:
        case APP_POPUPMODE:
            setDicoHistory.NavigInHisto(piIncrement);
            break;
        case APP_ACTIVATOR:
            setActiHistory.NavigInHisto(piIncrement);
            break;
        case APP_EXAMCOACH:
            oExamCoachMenu.SetVariable("activator.toolBar", (piIncrement == -1 ? "previous" : "next"));
            break;
        case APP_HELP:
            setHelpHistory.NavigInHisto(piIncrement);
            break;
    }
}

SettingHistory.prototype.GetIndexBask          = GetIndexBask;
SettingHistory.prototype.GetBasketContentCount = GetBasketContentCount;
SettingHistory.prototype.AddInDicoBasket       = AddInDicoBasket;
SettingHistory.prototype.NavigInHisto          = NavigInHisto;
SettingHistory.prototype.SetButtonState        = SetButtonState;
SettingHistory.prototype.manageHistoBtn        = manageHistoBtn;
SettingHistory.prototype.ShowHistoryItem       = ShowHistoryItem;
SettingHistory.prototype.getHistoryXml         = getHistoryXml;
SettingHistory.prototype.getHistoItemNumber    = getHistoItemNumber;
