﻿/* 
AJAX functions
Kevin Woodberry (21st March 2007)
*/

function createReq()
{
    try
    {
        req = new XMLHttpRequest(); /* Firefox */
    }
    catch(err1)
    {
        try
        {
            req = new ActiveXObject("Msxml2.XMLHTTP"); /* Some versions of IE */ 
        }
        catch(err2)
        {
            try
            {
                req = new ActiveXObject("Microsoft.XMLHTTP"); /* Other versions of IE */
            }
            catch(err3)
            {
                req = false;
            }
        }
    }
    return req;
}

function reqGet(url, q, req)
{
    rand = + new Date().getTime(); /* Gets time in milli-seconds, used to prevent caching */
    req.open("GET", url+'?'+q+'&rand='+rand, true);
    req.send(null);
}

function reqPost(url, q, req)
{
    req.open("POST", url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(q);
}

function doCallback(callback, item, status)
{
    eval(callback + '(item, status)');
}

function doAjax(url, q, callback, reqtype, getxml, varelementID)
{
    document.getElementById(varelementID).innerHTML = '<p><strong>Loading media, please wait ... ...</strong><br /><br /><img src=\"../images/Loading.gif\" width=\"220\" height=\"19\" alt=\"Loading, please wait\" /></p>';
    var myreq = createReq(); /* Create the XMLhttpRequest object instance */
    myreq.onreadystatechange = function()
    {
        if(myreq.readyState == 4)
        {
            //if(myreq.status == 200)
            //{
                var item = myreq.responseText;
                if(getxml == 1)
                {
                    item = myreq.responseXML;
                }
                doCallback(callback, item, myreq.status);
            //}           
        }        
    }
    if(reqtype == 'post' || reqtype == 'get')
    {
        reqPost(url, q, myreq);
    }
}
