﻿// **************************************************************************************************************************
// AJAX functions
// **************************************************************************************************************************

var xmlhttp = null;

var ajaxResultsXML = null;
var ajaxResultsText = "";

var ajaxResultReady = false;
var ajaxLoadingData = false;

function AjaxTransfer(url)
{
    ajaxLoadingData = true;
    ajaxResultReady = false;
    ajaxResultsXML = null;
    ajaxResultsText = "";

    // setting right object according to web browser
    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest(); // code for Mozilla, etc.
    } 
    else 
    {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (error) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (xmlhttp != null) 
    {
        // setting the action after receiving data
        xmlhttp.onreadystatechange = function()
        { 
            if (xmlhttp.readyState == 4)  // check if receiving action is completed
            {
                if(xmlhttp.status == 200) // check if there is not any error
                {
                    // ******************************************************************************
                    // work with received data ------------------------------------------------------
                    ajaxResultsXML = xmlhttp.responseXML;
                    ajaxResultsText = xmlhttp.responseText;
                    ajaxResultReady = true;
                    ajaxLoadingData = false;
                    // end of work with received data -----------------------------------------------
                    // ******************************************************************************
                }
                else
                {
                    alert("Problem retrieving XML data.");
                }
            }
        };
        xmlhttp.open("GET", url + (url.indexOf("?") == -1 ? "?" : "&") +"dsasfkiugbgadwqtruzozs=" + Math.random().toString(), true);
        xmlhttp.send(null);
    }
    else alert("Your browser does not support XMLHTTP.");
}
