/******************************************************
** AJAX Routines
*/

/**
*    Makes an AJAX request using GET.  Parameters are included in the URI.
*
*    If target is "#" then the return buffer is assumed to be javascript and
*    is executed.  If target is any other name then the return contents are
*    used to replace the innerHTML for that node.
*/
function makeRequest(url,target)
{
    changed_data = new Object();
    var xhr = false;
    if (window.XMLHttpRequest)
    {
        xhr = new XMLHttpRequest();
    }
    else
    {
        if (window.ActiveXObject)
        {
            try { xhr = new ActiveXObject("Microsoft.XMLHTTP");} catch (e)
            {
                try { xhr = new ActiveXObject("Msxml2.XMLHTTP");} catch (ee)
                {
                    alert("Could not create xhr object: "+ee.message);
                }
            }
        }
    }
    
    if (xhr)
    {
        /* passing '#' implies the return data is a function call */
        if (target == "#")
        {
            xhr.onreadystatechange = function()
            {
                if (xhr.readyState == 4)
                {
                    var outMsg;
                    if ((xhr.status == 200) || (xhr.status == 0))
                    {
                        var f = new Function(xhr.responseText);
                        f();
                        delete f;
                        f = null;
                        xhr.onreadystatechange = null;
                        xhr = null;
                    }
                    else
                    {
                        window.status = "Error fetching data: " + xhr.status;
                    }
                }        
            }
        }
        /* any name implies innerHTML to be replaced */
        else
        {
            xhr.onreadystatechange = function()
            {
                if (xhr.readyState == 4)
                {
                    var outMsg;
                    if ((xhr.status == 200) || (xhr.status == 0))
                    {
                        outMsg = xhr.responseText;
                    }
                    else
                    {
                        outMsg = "Error fetching data: " + xhr.status;
                    }
                    document.getElementById(target).innerHTML = outMsg;
                    xhr.onreadystatechange = null;
                    xhr = null;
                }
            };
        }
        xhr.open("GET", encodeURI(url), true);
        xhr.send(null);
    }
    else
    {
        document.getElementById(target).innerHTML =
            "Error fetching data - Could not create xhr object.";
    }
}

/**
*    Makes an AJAX request using POST.  The parameters are passed in
*    using an array of attributes and values.  All data will be URI
*    encoded.
*
*    If target is "#" then the return buffer is assumed to be javascript and
*    is executed.  If target is any other name then the return contents are
*    used to replace the innerHTML for that node.
*/
function makePOSTRequest(url,target,parms)
{
    changed_data = new Object();
    var xhr = false;
    if (window.XMLHttpRequest)
    {
        xhr = new XMLHttpRequest();
    }
    else
    {
        if (window.ActiveXObject)
        {
            try { xhr = new ActiveXObject("Microsoft.XMLHTTP");} catch (e)
            {
                try { xhr = new ActiveXObject("Msxml2.XMLHTTP");} catch (ee)
                {
                    alert("Could not create xhr object: "+ee.message);
                }
            }
        }
    }
    
    if (xhr)
    {
        /* passing '#' implies the return data is a function call */
        if (target == "#")
        {
            xhr.onreadystatechange = function()
            {
                if (xhr.readyState == 4)
                {
                    var outMsg;
                    if ((xhr.status == 200) || (xhr.status == 0))
                    {
                        var f = new Function(xhr.responseText);
                        f();
                        delete f;
                        f = null;
                        xhr.onreadystatechange = null;
                        xhr = null;
                    }
                    else
                    {
                        window.status = "Error fetching data: " + xhr.status;
                    }
                }        
            }
        }
        /* any name implies innerHTML to be replaced */
        else
        {
            xhr.onreadystatechange = function()
            {
                if (xhr.readyState == 4)
                {
                    var outMsg;
                    if ((xhr.status == 200) || (xhr.status == 0))
                    {
                        outMsg = xhr.responseText;
                    }
                    else
                    {
                        outMsg = "Error fetching data: " + xhr.status;
                    }
                    document.getElementById(target).innerHTML = outMsg;
                    xhr.onreadystatechange = null;
                    xhr = null;
                }
            };
        }
        // reformat the paramters
        var parmstr = "";
        for ( var p in parms )
        {
            if (parmstr != "") parmstr += "&";
            parmstr += p+"="+encodeURI(parms[p]);
        }
        // POST the message
        xhr.open("POST", encodeURI(url), true);        
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.setRequestHeader("Content-length", parmstr.length);
        xhr.setRequestHeader("Connection", "close");
        xhr.send(parmstr);
    }
    else
    {
        document.getElementById(target).innerHTML =
            "Error fetching data - Could not create xhr object.";
    }
}

/******************************************************
** Utility Routines
*/

function gup( name, defval )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return defval;
    else
        return results[1];
}

