﻿// JScript File
function CallBackObject()
{
    var req,e;
    try {
        req = new XMLHttpRequest();
    }
    catch(e) {
        try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) {
            req = null;
        }
    }

    this.XmlHttp = req;
    this.Refresh = false;
    this.RefreshTime = -1; // czas odswiezania ajaxa - bez odswiezania
    this.Active = true; // czy ajax jest aktywny 
}

CallBackObject.prototype.DoCallBack = function()
{
  var theData = '';
  var theform = document.forms[0];
  var thePage = window.location.pathname + window.location.search;
  var eName = '';
  var elem;
 
  theData = 'AjaxCallBack=true';
  theData = '';
  for( var i=0; i<theform.elements.length; i++ )
  {
    elem = theform.elements[i];
    // wykrywanie pola do wysylania pliku
    if (elem.type == 'file' && elem.value != null)
    {
        theForm.submit();
        return;
    }
    if (elem.type == 'button' || elem.type == 'submit') continue;
    if (elem.type == 'select-one' && elem.value == '') continue;

    eName = elem.name;
    if (eName == '') eName = elem.id;
    if (eName && eName != '')
    {
        if (eName == '__VIEWSTATE' || eName == '__EVENTVALIDATION')
        {
            theData = theData + '&' + escape(eName.split("$").join(":")) + '=';
            theData += escape(elem.value).replace(new RegExp('\\+', 'g'), '%2b');
        }
        else if (elem.type == 'checkbox') 
        {
            if (elem.checked == true)
                theData = theData + '&' + escape(eName.split("$").join(":")) + '=on';
        }
        else // pozostale elementy - text, hidden 
        {
            theData = theData + '&' + escape(eName.split("$").join(":")) + '=';
            theData += escape(elem.value);
        }
    }
  }
 
  if (this.XmlHttp)
  {
    if (this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0)
    {
      var oThis = this;
      this.XmlHttp.open('POST', thePage, false);
      this.XmlHttp.onreadystatechange = function()
                                        { oThis.ReadyStateChange(); };
      this.XmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      this.XmlHttp.send(theData);
      
      if (this.XmlHttp.responseText == 'SUBMIT')
          theForm.submit();
      else
          document.all.AJAXBODY.innerHTML = this.XmlHttp.responseText;
          __Initialize();
          theForm = document.Form1;
    }
  }
}
 
CallBackObject.prototype.AbortCallBack = function()
{
  if( this.XmlHttp )
    this.XmlHttp.abort();
}
 
CallBackObject.prototype.OnLoading = function()
{
  // Loading
}
 
CallBackObject.prototype.OnLoaded = function()
{
  // Loaded
}
 
CallBackObject.prototype.OnInteractive = function()
{
  // Interactive
}
 
CallBackObject.prototype.OnComplete = function(responseText, responseXml)
{
  // Complete
}
 
CallBackObject.prototype.OnAbort = function()
{
  // Abort
}
 
CallBackObject.prototype.OnError = function(status, statusText)
{
  // Error
}
 
CallBackObject.prototype.ReadyStateChange = function()
{
  if( this.XmlHttp.readyState == 1 )
  {
    this.OnLoading();
  }
  else if( this.XmlHttp.readyState == 2 )
  {
    this.OnLoaded();
  }
  else if( this.XmlHttp.readyState == 3 )
  {
    this.OnInteractive();
  }
  else if( this.XmlHttp.readyState == 4 )
  {
    if( this.XmlHttp.status == 0 )
      this.OnAbort();
    else if( this.XmlHttp.status == 200 && 
                            this.XmlHttp.statusText == "OK" )
      this.OnComplete(this.XmlHttp.responseText, 
                            this.XmlHttp.responseXML);
    else
      this.OnError(this.XmlHttp.status, 
           this.XmlHttp.statusText, this.XmlHttp.responseText);   
  }
}

// ----------------- uzywanie obiektu ---------------------
var AO = new CallBackObject();

function AjaxSend()
{
//    if (AO.Active == false) 
//    {
//        event.returnValue = true;
//        return;
//    }
    
    // ajax nie zostal zablokowany - wysylanie przez niego
//    if (AO.DoCallBack() == false) event.returnValue = false; // gdy dane zostały wysłane przez ajax - blokowanie standardowej metody   
    AO.DoCallBack(); // gdy dane zostały wysłane przez ajax - blokowanie standardowej metody   
}

function __doNewPostBack(eventTarget,eventArgument) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument; 
    AO.DoCallBack();
}

