/*
 * Example for a custom catalog that uses POST.
 */

// catalog property to remember current session id.  0 if no session has been established
thisCatalog.sessionid = 0;

// helper method to establish session
thisCatalog.getSession = function() {
    // check if valid session
    if (this.sessionid != 0)
        return this.sessionid;

    // establish session if not
    // XXX make some of the autho/password parameters use 'paramX'
    var searchDialog = libxEnv.getDocument(
        this.url + "/WebZ/Authorize?sessionid=0&autho=GUESTENG&password=GUEST&next=startscreen&bad=error/authofail.html&style=nfenglish");

    /* See http://geoweb.biblio.polymtl.ca:8000/WebZ/Authorize?sessionid=0&autho=GUESTENG&password=GUEST&next=startscreen&bad=error/authofail.html&style=nfenglish
       This page includes a link "click here..." which contains the session id. Extract it.
     */
    var m = searchDialog.match(/geacnfdblist.html\?sessionid=([^"]+)\"/);
    if (m != null)
        this.sessionid = m[1];
    else
        libxEnv.writeLog("Couldn't find session id in: " + searchDialog);

    // invalidate our session id after 5 minutes
    var idle = 5; 
    var cat = this;
    setTimeout(function () {
        cat.sessionid = 0;
    }, idle * 60 * 1000);

    return this.sessionid;
}

thisCatalog.makeSearch = function(stype, sterm) {
    var sessionid = this.getSession();

    var index = "kw:";
    switch (stype) {
        case 't': index = "ti:"; 
                break;
        case 'a': index = "au:"; 
                break;
        case 'd': index = "sh:"; 
                break;
        case 'i': index = "ib:";
                break;
        case 'is': index = "is:"; 
                break;

        // XXX add others here
        default:
        case 'Y': index = "kw:"; break;
    }
        
    /* Note: to tell LibX that this catalog requires POST, return
     * an array [ url, postdata ]  whereby 'url' and 'postdata' are strings.
     */
    var r = [ this.url + "/WebZ/GeacQUERY?sessionid=" + sessionid,
            "bad="+encodeURIComponent("error/badsearch.html")+"&"+
            "next="+encodeURIComponent("html/geacnfbrief.html")+"&"+
            "entitytoprecno=1&"+
            "entitycurrecno=1&"+
            "entitytempjds=TRUE&"+
            "dbgroup=ADVANCE&"+
            "dbname=ADVANCE&"+
            "numrecs=10&"+
            "key1=Phrase86&"+
            "direction1=d&"+
            "format=B&"+
            "fmtclass=&"+
            "termA="+sterm+"&"+
            "indexA="+encodeURIComponent(index)+"&"
        ];
    //libxEnv.writeLog("returning: " + typeof(r));
    return r;
};

thisCatalog.makeAdvancedSearch = function(fields) {
    /* TBD */
    return "http://libx.org/";
};

// thisCatalog.xisbn.opacid = "geoweb";

/*

The following is an excerpt from their search form:

<option value="ti:">Title</option>
<option value="au:">Author</option>
<option value="sh:">Subject (1980 & Later)</option>
<option value="se:">Series</option>
<option value="rn:">Series Number</option>

<option value="pb:">Publisher</option>
<option value="tc:">Table of Contents</option>
<option value="kw:">All Indexes</option>
<option value="ib:">ISBN</option>
<option value="is:">ISSN</option>
<option value="lcn:">LCN</option>
<option value="pi:">Item Barcode</option>
*/

