var xmlHttp = createXmlHttpRequestObject();
var serverPOST_URL = serverURL;
var serverGET_URL = serverURL + "?p=ax";
var elIds = new Array();
var lang;
var active = false;

function createXmlHttpRequestObject()
{
    var xmlHttp = false;

    try
    {
        if (window.ActiveXObject)
        {
            xmlHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
        }
        else
        {
            xmlHttp = new window.XMLHttpRequest();
        }
    }
    catch (e) { }
    
    return xmlHttp;
}

function ax_process()
{
    if (xmlHttp)
    {
        if (arguments.length > 0)
        {
            var pe = "";
            var pp = "";
            var q = "";

            for (n = 0; n < arguments.length; n++)
            {
                var arg = ax_process.arguments[n];

                pp += q + "'" + arg + "'";
                q = ",";

                if (n == 0)
                    elId = arg;
                else
                {
                    if (document.getElementById(arg))
                    {
                        pe += "&" + arg + "=" + encodeURIComponent(document.getElementById(arg).value);
                    }
                }
            }

            if (!active && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0))
            {
                active = true;
                
                xmlHttp.open("GET", serverGET_URL + "&ax_el=" + elId + pe, true);
                xmlHttp.onreadystatechange = handleServerResponse;
                xmlHttp.send(null);
            }
            else
            {
                setTimeout("ax_process(" + pp + ")", 1000);
            }
        }
    }
}

function ax_process_POST()
{
    if (xmlHttp)
    {
        if (arguments.length > 0)
        {
            var pe = "";
            var pp = "";
            var q = "";

            for (n = 0; n < arguments.length; n++)
            {
                var arg = ax_process_POST.arguments[n];

                pp += q + "'" + arg + "'";
                q = ",";

                if (n == 0)
                    elId = arg;
                else
                {
                    if (document.getElementById(arg))
                    {
                        pe += "&" + arg + "=" + encodeURIComponent(document.getElementById(arg).value);
                    }
                }
            }

            if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
            {
                var params = "p=ax&ax_el=" + elId + pe;
                xmlHttp.open("POST", serverPOST_URL, true);

                xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlHttp.setRequestHeader("Content-length", params.length);
                xmlHttp.setRequestHeader("Connection", "close");

                xmlHttp.onreadystatechange = handleServerResponse;

                xmlHttp.send(params);
            }
            else
            {
                setTimeout("ax_process_POST(" + pp + ")", 1000);
            }
        }
    }
}

function ax_init()
{
    if (xmlHttp)
    {
        if (arguments.length > 0)
        {
            for (n = 0; n < arguments.length; n++)
            {
                var arg = ax_init.arguments[n];
                elIds[n] = arg;

                if (active)
                {
                    setTimeout("ax_init('" + arg + "')", 1000);
                }
                else
                {
                    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
                    {
                        active = true;

                        try
                        {
                            xmlHttp.open("GET", serverGET_URL + '&ax_ac=get&ax_el=' + arg + lang, true);
                            xmlHttp.onreadystatechange = handleServerResponse;
                            xmlHttp.send(null);
                        }
                        catch (e)
                        {
                            active = false;
                        }
                    }
                }
            }
        }
    }
}

function handleServerResponse()
{
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            xmlResponse = xmlHttp.responseXML;
            xmlDocumentElement = xmlResponse.documentElement;

            if (xmlDocumentElement.getElementsByTagName("elId")[0].childNodes[0])
                elId = xmlDocumentElement.getElementsByTagName("elId")[0].childNodes[0].nodeValue;
            else
                elId = null;

            if (xmlDocumentElement.getElementsByTagName("data")[0].childNodes[0])
                retMsg = xmlDocumentElement.getElementsByTagName("data")[0].childNodes[0].nodeValue;
            else
                retMsg = "";
            
            if (elId && retMsg && document.getElementById(elId))
                document.getElementById(elId).innerHTML = retMsg;
        }

        active = false;
    }
}

function SetLang(lng)
{
    lang = "&ax_l=" + encodeURIComponent(document.getElementById(lng).value);
}
