
var xmlHttpCache = 0;

function create_http_object()
{
    

    // try the function XMLHttpRequest() first If fails try others.
    try
    {
        return new XMLHttpRequest();
    }
    catch(e)
    { }

    // This array is a list of all/most possible XMLHTTP types
    var ActiveXTypes = [
        "Microsoft.XMLHTTP",
        "MSXML2.XMLHTTP.5.0",
        "MSXML2.XMLHTTP.4.0",
        "MSXML2.XMLHTTP.3.0",
        "MSXML2.XMLHTTP"
    ];

    if (xmlHttpCache > 0)
    {
       return new ActiveXObject(ActiveXTypes[xmlHttpCache]);
    }

    // Loop through the ActiveXTypes array and 
    for (var i = 0; i < ActiveXTypes.length; i++)
    {
        // try to use the current XMLHTTP object,
        // if not it's not possible try the next
        try
        {
            xmlHttpCache = i;
            return new ActiveXObject(ActiveXTypes[i]);
        }
        catch(e)
        { }
    }

    xmlHttpCache = 0;

    // It seems that the browser doesn't support AJAX Stuff
    return false;
}

function ajaxSubmit(url, data, stateFunction)
{
    // url             location of the scripting
    // data            array of submit data
    // stateFunction   is the object to return the received data to

    saveDate = new Date();

    // open XMLHTTP connection
    xmlHttp = create_http_object();

    // connection open?
    if (!xmlHttp)
    {
        // display ERROR notice
        notifyNoAjax();
    }
    else
    {
        // when state is ready, do save
        xmlHttp.onreadystatechange = stateFunction;
        // open url
        xmlHttp.open('POST', url + "?cache=" + saveDate.getTime(), true);

        // set headers before posting
        xmlHttp.setRequestHeader("Content-type",  "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", data.length);
        xmlHttp.setRequestHeader("Connection",    "close");

        // send data to server
        xmlHttp.send(data);
    }
}

function ajaxSubmitFunc()
{
    // is readyState 4? 4 =  all data is received 
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            // return data to xmlHttpCache variable
            document.getElementById('receiveddatadiv').value = "AjaxSubmitFunc\r\n" + xmlHttp.responseText;
        }
    }
}


function ajaxGet(url, data, stateFunction)
{
    // url             location of scripting
    // data            compilation of string paramters
    // stateFunction   object to return received data to 

    saveDate = new Date();

    // open XMLHTTP connection
    xmlHttp = create_http_object();

    // connection open?
    if (!xmlHttp)
    {
        // display ERROR notice!
        notifyNoAjax();
    }
    else
    {
        // what to do with received data!
        xmlHttp.onreadystatechange = stateFunction;
        
        // open url location
        if (data != "")
        {
            xmlHttp.open('GET', url + data + "&cache=" + saveDate.getTime(), true);
        }
        else
        {
            xmlHttp.open('GET', url + "?cache=" + saveDate.getTime(), true);
        }
 
        // send nothing, just go receive data
        xmlHttp.send(null);
    }
}

function ajaxGetFunc()
{
    // is readyState 4? 4 =  all data is received 
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            // return data to xmlHttpCache variable
            document.getElementById('receiveddatadiv').value = "AjaxGetFunc\r\n" + xmlHttp.responseText;
        }
    }
}

function notifyNoAjax()
{
    if (xmlHttpFailure == false)
    {
        alert('Sorry, uw brouwser ondersteund geen AJAX / Javascript calls');
    }
}
