// UWAGA na naglowki od cacheowania! Trzeba je dodac do skryptow w php ktore zwracaja tresci
// header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
// header("Cache-Control: post-check=0, pre-check=0", false);
// header("Pragma: no-cache");                          // HTTP/1.0
// header("Expires: 0");

// Ajax - klasa wrapperowa dla XMLHTTPRequest
function Ajax(caller)
{
  var that=this;

  this.toString=function() { return "Ajax object"; };
  this.XMLHttpRequest=false;
  this.Request=Request;
  this.onUpdate=onUpdate;
  this.caller=caller;

  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
   try {
    this.XMLHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     this.XMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     this.XMLHttpRequest = false;
    }
   }
  @end @*/
  if (!this.XMLHttpRequest && typeof XMLHttpRequest!='undefined') {
  	try {
  		this.XMLHttpRequest = new XMLHttpRequest();
  	} catch (e) {
  		this.XMLHttpRequest=false;
  	}
  }
  if (!this.XMLHttpRequest && window.createRequest) {
  	try {
  		this.XMLHttpRequest = window.createRequest();
  	} catch (e) {
  		this.XMLHttpRequest=false;
  	}
  };

  function Request(req, func)
  {
    // set the var so we can scope the callback
    // http://www.mikechambers.com/blog/2006/01/31/encapsulating-ajax-xmlhttprequest-calls-within-javascript-classes/
    var _this=this;
    
    if(this.XMLHttpRequest.readyState!=0)
      this.XMLHttpRequest.abort();
    this.XMLHttpRequest.open("GET", req, true);
    if(typeof(func)=='function')
      this.XMLHttpRequest.onreadystatechange=func;
    else
      this.XMLHttpRequest.onreadystatechange=function() { _this.onUpdate(); };
    this.XMLHttpRequest.send(null);
  }

  function onUpdate()
  {
    if(this.caller && this.caller.onUpdate)
      this.caller.onUpdate();
  }
}

/*
  onUpdateStart();
  onUpdateTick(readyState);
  onUpdateEnd(readyState, responseText);
*/

// AjaxUpdater - sluzy do updatowania konkretnego elementu html (diva)
// elem_id - element w ktorym zostanie podmieniony innerHTML, mozna nie podac, wtedy mozna samemu cos updatnac w onUpdateEnd
// updrequest - tresc zapytania zwracajacego innerHtml (np. /wit/rejestracja/get_cos.php?par=sda)
function AjaxUpdater(elem_id, updrequest)
{
  var that=this;

  this.toString=function() { return "AjaxUpdater object"; };
  this.onUpdate=onUpdate;
  this.Update=Update;
  this.ajax=new Ajax(this);
  this.elem_id=elem_id;
  if(typeof(updrequest)=='string')
    this.updrequest=updrequest;
  else
    this.updrequest=null;
  this.onUpdateStart=null;
  this.onUpdateTick=null;
  this.onUpdateEnd=null;

// updrequest - tresc zapytania (np. /wit/rejestracja/get_cos.php?par=sda)
  function Update(updrequest)
  {
    var updreq=null;
    if(typeof(updrequest)=='string')
      updreq=updrequest;
    else
    if(typeof(this.updrequest)=='string')
      updreq=this.updrequest;

    if(updreq)
    {
      if(this.onUpdateStart)
        this.onUpdateStart();
      this.ajax.Request(updreq);
    };
  }

  function onUpdate()
  {
    readyState=this.ajax.XMLHttpRequest.readyState;

    if(this.onUpdateTick)
      this.onUpdateTick(readyState);

    if(readyState==4)
    {
      responseText=this.ajax.XMLHttpRequest.responseText;
      if(this.elem_id)
        document.getElementById(this.elem_id).innerHTML=responseText;
      if(this.onUpdateEnd)
        this.onUpdateEnd(readyState, responseText);
    };
  }
}

/*
  onRequestStart();
  onRequestTick(readyState);
  onRequestEnd(readyState, responseText);
  onUpdateStart();
  onUpdateTick(readyState);
  onUpdateEnd(readyState, responseText);
*/

// AjaxRequestUpdater - sluzy do wyslania requestu (na przyklad zapisujacego cos do bazy) a nastepnie updatuje element html (diva)
// elem_id - element w ktorym zostanie podmieniony innerHTML, mozna nie podac, wtedy mozna samemu cos updatnac w onUpdateEnd
// updrequest - tresc zapytania zwracajacego innerHtml (np. /wit/rejestracja/get_cos.php?par=sda)
function AjaxRequestUpdater(elem_id, updrequest)
{
  var that=this;
  
  this.toString=function() { return "AjaxUpdater object"; };
  this.Update=Update;
  this.Request=Request;
  this.ajax=new Ajax(this);
  this.elem_id=elem_id;
  this.doupd=false;
  if(typeof(updrequest)=='string')
    this.updrequest=updrequest;
  else
    this.updrequest=null;
  this.onRequestStart=null;
  this.onRequestTick=null;
  this.onRequestEnd=null;
  this.onUpdateStart=null;
  this.onUpdateTick=null;
  this.onUpdateEnd=null;

// request - tresc zapytania modyfikujacego cos w bazie
// doupd - boolean (default true) czy po requescie robic Update
  function Request(request, doupd)
  {
    if(typeof(request)=='string')
    {
      if(typeof(doupd)=='boolean')
        this.doupd=doupd;
      else
        this.doupd=true;

      if(this.onRequestStart)
        this.onRequestStart();
      this.onUpdate=onRequest;
      this.ajax.Request(request);
    };
  }

  function Update(updrequest)
  {
    var updreq=null;
    if(typeof(updrequest)=='string')
      udpreq=updrequest;
    else
    if(typeof(this.updrequest)=='string')
      updreq=this.updrequest;

    if(updreq)
    {
      if(this.onUpdateStart)
        this.onUpdateStart();
      this.onUpdate=onUpdate;
      this.ajax.Request(updreq);
    };
  }

  function onRequest()
  {
    readyState=this.ajax.XMLHttpRequest.readyState;

    if(this.onRequestTick)
      this.onRequestTick(readyState);

    if(readyState==4)
    {
      responseText=this.ajax.XMLHttpRequest.responseText;
      if(this.onRequestEnd)
        this.onRequestEnd(readyState, responseText);
      if(this.doupd)
        this.Update();
    };
  }

  function onUpdate()
  {
    readyState=this.ajax.XMLHttpRequest.readyState;

    if(this.onUpdateTick)
      this.onUpdateTick(readyState);

    if(readyState==4)
    {
      responseText=this.ajax.XMLHttpRequest.responseText;
      if(this.elem_id)
        document.getElementById(this.elem_id).innerHTML=responseText;
      if(this.onUpdateEnd)
        this.onUpdateEnd(readyState, responseText);
    };
  }
}

