﻿function MakeXMLHttpObject()
{
    var xmlHttpRequest;
    if (typeof ActiveXObject != 'undefined')
    {
        xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
    }
    else if (typeof XMLHttpRequest != 'undefined')
    {
        xmlHttpRequest = new XMLHttpRequest();
    }
    else
    {
        xmlHttpRequest = false;
    }
    return xmlHttpRequest;
}

function ProcessAjax(config)
{
    var method = config.method == 'undefined' ? 'GET' : config.method;
    var data = config.data == 'undefined' ? null : config.data;
    var url = config.url;
    var callback = config.callback;
    var errback = config.errback;
    var beforeSend = config.beforeSend;
    
    var oDate = new Date();

    if( method == 'GET' && data != null )
    {
        url += '?' + data;
        data = null;
    }
    
    if( url.indexOf('?') != -1 )
    {
        url += '&time=' + oDate.getTime();
    }
    else
    {
        url += '?time=' + oDate.getTime();
    }
    
    var xmlHttpRequest = MakeXMLHttpObject();
    xmlHttpRequest.open(method, url, true); 
    xmlHttpRequest.onreadystatechange = function() {
        if (xmlHttpRequest.readyState==4) {
            if (xmlHttpRequest.status==200) {
                callback(xmlHttpRequest.responseXML);
            } else {
                if( callback != 'undefined' )
                {
                    errback(xmlHttpRequest.status, xmlHttpRequest.statusText);
                }
            }
        }
    }
    if( beforeSend != undefined )
    {
        beforeSend();
    }
    xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpRequest.send(data);
    
    return xmlHttpRequest;
}
