//////////////////////////////////
/// Kniznica na pracu s AJAXom
////////////////////////////////// 

var httpreq = CreateAJAXRequest(); // Objekt na komunikaciu so serverom
var httpdebug = false; // Vypisuj hlasenia v priebehu prace
var httpdivname = ""; // Meno cieloveho objektu

///
/// Vytvorenie objektu
///
function CreateAJAXRequest() 
{  
var req;
// Internet Explorer
if(window.ActiveXObject)
  {
  try
    {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    }
  catch (e) 
    {
    req = false;
    }
  }
// Mozilla a vsetko ostatne
else
  {
  try 
    {
    req = new XMLHttpRequest();
    }
  catch (e) 
    {
    req = false;
    }
  }

// kontrola vysledku
if (!req)
  alert("Error creating the XMLHttpRequest object.");
else 
  return req;
}

///
/// Asynchronny HTTP request 
/// 
function GetAJAXRequest(URL,target,wantxml)
{
if(httpdebug) alert("URL:"+URL+" target:"+target);
// iba ak nepracuje mozeme ho pouzit
if (httpreq.readyState == 4 || httpreq.readyState == 0)
  {
  httpdivname=target;
  // stiahni stranku
  httpreq.open("GET", URL, true);  
  // nastav handler
  if(wantxml)
    httpreq.onreadystatechange = HandleXMLAJAXResponse;
  else
    httpreq.onreadystatechange = HandleTextAJAXResponse;
  // stiahni to
  httpreq.send(null);
  }
}

///
/// Asynchronny HTTP request 
/// 
function PostAJAXRequest(URL,target,wantxml,parameters)
{
if(httpdebug) alert("URL:"+URL+" target:"+target);
// iba ak nepracuje mozeme ho pouzit
if (httpreq.readyState == 4 || httpreq.readyState == 0)
  {
  httpdivname=target;
  // stiahni stranku
  httpreq.open("POST", URL, true);  
  // nastav handler
  if(wantxml)
    httpreq.onreadystatechange = HandleXMLAJAXResponse;
  else
    httpreq.onreadystatechange = HandleTextAJAXResponse;
  // stiahni to
  httpreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpreq.setRequestHeader("Content-length", parameters.length);
  httpreq.setRequestHeader("Connection", "close");
  httpreq.send(parameters);
  }
}

///
/// Handler spracovania vysledku
/// Ocakava zaslanie odpovede ako plain text.
///
/// Priklad v PHP:
/// <?php Header('Content-Type: text/plain');?>
/// <b>ahoj</b>
///
function HandleTextAJAXResponse() 
{
// iba ked je stiahnute
if (httpreq.readyState == 4) 
  {
  // a status hovori ze je OK
  if (httpreq.status == 200) 
    {
    if(httpdebug) alert(httpreq.responseText);
    document.getElementById(httpdivname).innerHTML =httpreq.responseText;
    } 
  // HTTP status iny nez 200 znamena chybu
  else 
    {
    alert("There was a problem accessing the server: " + httpreq.statusText);
    }
  }
}

///
/// Handler spracovania vysledku
/// Ocakava zaslanie odpovede ako XML.
///
/// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
/// <response>
/// Mickey Mouse, I don't know you!
/// </response>
///
function HandleXMLAJAXResponse() 
{
// iba ked je stiahnute
if (httpreq.readyState == 4) 
  {
  // a status hovori ze je OK
  if (httpreq.status == 200) 
    {
    if(httpdebug) alert(httpreq.responseText);
    // extract the XML retrieved from the server
    xmlResponse = httpreq.responseXML;
    // obtain the document element (the root element) of the XML structure
    xmlDocumentElement = xmlResponse.documentElement;
    // get the text message, which is in the first child of
    // the the document element
    resultMessage = xmlDocumentElement.firstChild.data;
    // update the client display using the data received from the server
    document.getElementById(httpdivname).innerHTML =resultMessage;
    } 
  // HTTP status iny nez 200 znamena chybu
  else 
    {
    alert("There was a problem accessing the server: " + httpreq.statusText);
    }
  }
}

///
/// Test
///
function process()
{
GetAJAXRequest("quickstart.php","divMessage",false);
}

function process2()
{
PostAJAXRequest("quickstart.php","tabulka",false);
}

///
/// Objektovy pristup
///
function TAjaxRequest()
{
var thisObj = this;
this.req = CreateAJAXRequest(); // Objekt na komunikaciu so serverom
this.debug = false; // Vypisuj hlasenia v priebehu prace
this.divname = ""; // Meno cieloveho objektu


///
/// Handler spracovania vysledku
/// Ocakava zaslanie odpovede ako plain text.
///
this.HandleText=function () 
  {
  // iba ked je stiahnute
  //alert(this.req.readyState);
  if (this.req.readyState == 4) 
    {
    // a status hovori ze je OK
    //alert(this.req.status);
    if (this.req.status == 200) 
      {
      if(this.debug) alert(this.req.responseText);
      elem=document.getElementById(this.divname);
      elem.innerHTML =this.req.responseText;
      } 
    // HTTP status iny nez 200 znamena chybu
    else 
      {
      alert("There was a problem accessing the server: " + this.req.statusText);
      }
    }
  }

///
/// Asynchronny HTTP request 
/// 
this.PostRequest=function (URL,target,parameters)
  {
  if(this.debug) alert("URL:"+URL+" target:"+target);
  // iba ak nepracuje mozeme ho pouzit
  if (this.req.readyState == 4 || this.req.readyState == 0)
    {
    this.divname=target;
    // stiahni stranku
    this.req.open("POST", URL, true);  
    // nastav handler
    this.req.onreadystatechange = function() {thisObj.HandleText(); }
    
    // stiahni to
    this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.req.setRequestHeader("Content-length", parameters.length);
    this.req.setRequestHeader("Connection", "close");
    this.req.send(parameters);
    }    
  }

}

