/*
    Copyright RE-phrase BV, 2008.
    Joop Ringelberg.
    
    This file provides basic AJAX like capabilities to the information pages of the REphrase websites.
    
    We assume the javascript variable "db" is bound to the database name, on the page. Obviously one should only
    use that variable on 'member' pages: in the public pages, no database has been selected.
 
*/

////////////////////// SERVER PROXY ////////////////////////////

function rp_Server ()
{
    try
    {
        this.xmlhttp = new XMLHttpRequest();
    }
    catch (e)
    {
        try 
        {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) 
        {
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    };
    // Get the base uri.
    var base = document.getElementsByTagName("base")[0];
    if (base)
    {
        this.baseURI = base.href;
    }
}

rp_Server.prototype.callFunction =
function (functionName, element)
{
    var thisServer = this;
    this.xmlhttp.open("GET",  this.baseURI + "cgi-bin/run.cgi?command=callfunction;functionname=" + functionName + ";db=" + db, true);
    this.xmlhttp.onreadystatechange=function() 
    {
        if (thisServer.xmlhttp.readyState==4  && thisServer.xmlhttp.statusText == "OK") 
        {
            element.innerHTML = thisServer.xmlhttp.responseText;
        }
    }
    this.xmlhttp.send(null);
};

function callFunction (functionName, elementID)
{
    var element = document.getElementById(elementID);
    if (element)
    {
        _rpserver.callFunction(functionName, element);
    }
}

var _rpserver = new rp_Server();
