var IdOfTimeout=null;
var asyncCmd = null;
var ajax_req = null;
var cbFunc   = null;

function abortAR()
 {
    ajax_req.abort();
    loaderPopUp('close');
    if(eval("typeof(ShowGlassWindow)") == "function")   ShowGlassWindow(false);
    alert("Проблемы при соединением с серверной программой. Пожалуйста убедитесь в соединеии с Internet и попытайтесь снова позднее.");
 }

function sendAR(postdata,ajax_url,interval)
 {
    if(interval==null) interval=10000;
    else               interval=parseInt(interval,10)*1000;

    if(ajax_url==null)
     {
        ajax_url = window.location;
     }
    if (window.XMLHttpRequest)
      {
        ajax_req = new XMLHttpRequest();
      }
    else
     {
       if (window.ActiveXObject)
         {
            var sign_a = ['MSXML2.XMLHTTP', 'Microsoft.XMLHTTP',
                          'MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0',
                          'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0',
                         ];
            for (var i=0, l = sign_a.length; i<l; ++i)
             {
               try {  ajax_req = new ActiveXObject(sign_a[i]);  i = l; }
               catch(e) {}
             }
         }
     }

    if(ajax_req)
     {
       ajax_req.open("POST", ajax_url, true);
       IdOfTimeout = setTimeout(abortAR,interval);
       ajax_req.onreadystatechange = CBAjax;
       ajax_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
       if(eval("typeof(ShowGlassWindow)") == "function")  ShowGlassWindow(true);
       ajax_req.send(postdata);
     }
    else
     {
        alert("Ваш броузер не разрешает использовать возможности приложения! Воспользуйтесь Internet Explorer, Mozilla-подобным броузером!");
     }
 }

function loaderPopUp(mode)
 {
   if(mode==null) mode="open";
   var divObj=$("loaderDiv");
   if(divObj)
    {
      var height=(window.innerHeight)?window.innerHeight:((document.all)?document.body.offsetHeight:null);
      switch(mode)
       {
         case "open":
          divObj.style.display="block";
          divObj.style.top = height/2 + document.body.scrollTop;
          break;
         case "close":
          divObj.style.display="none";
          divObj.style.top = height/2;
          break;
       }
    }
 }

function CBAjax()
 {
    try
    {
       if (ajax_req.readyState == 4)
       {
           clearTimeout(IdOfTimeout);
           if (ajax_req.status == 200)
           {
              ajax_req.onreadystatechange = CB(ajax_req);
           }
           else
           {
             //request.status :
             // 503 - if the application isn't available;
             // 500 - if the application has a bug.
             alert("Ошибка POST-запроса AJAX:" + ajax_req.status + " " + ajax_req.statusText);
           }
       }
       else
       {
           if(ajax_req.readyState > 0)
            {
              loaderPopUp('open');
            }
       }

    }
    catch(err)
    {
       //alert("It does not appear that the server is available for this application. Please try again very soon. \\nError: " + err.message);
    }
 }
function CB(httpRequest)
 {
    try
     {
      var re = /^<\?xml/i;
      loaderPopUp('close');
      if(eval("typeof(ShowGlassWindow)") == "function")   ShowGlassWindow(false);
      //alert(httpRequest.responseText);
      if( re.test(httpRequest.responseText) )
       {
          asyncCmd = null;
          var xmlDoc = httpRequest.responseXML;
          if(xmlDoc != null)
           {
              if(cbFunc)
               {
                  if(eval(("typeof("+cbFunc+")")) == "function")   eval((cbFunc+"(xmlDoc.documentElement)"));
                  cbFunc = null;
               }
              else if(eval("typeof(xmlAjaxCallBack)") == "function")  { xmlAjaxCallBack(xmlDoc.documentElement);  }
           }
          else 
           {
               cbFunc = null;
               alert('Ошибка! Сервер вернул не валидный XML!');
           }
       }
      else
       {
          cbFunc = null;
          if (winList['popupDiv']) 
           {
             winList['popupDiv'].clientArea.innerHTML = httpRequest.responseText;
           }
          if(asyncCmd)
           {
              if(eval("typeof(doPostCmd)") == "function")   doPostCmd(asyncCmd);
              asyncCmd = null;
           }
       }
     }
    catch (err)
     {
       cbFunc = null;
       asyncCmd = null;
       alert(err.message);
     }
 }

