function createXmlHttpRequestObject() 
{
  var xmlHttp;
  try
  {
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }

  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}







function send()
{
 if (this.address!='') {
  if (this.t) clearTimeout(this.t);
  if (!(this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0)) {
  }
  if (this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0)
  {
  
	if (this.instance!='') {
	    var l = this.param_count();
	    var parameters = '';
	    for (var i=0; i<l; i++) {
			if (i!=0) {
				parameters+='&';
			}
			parameters+=this.param[i];
	    }
	    
	    
		
	    this.xmlHttp.open("POST", this.address, true);  
	    this.xmlHttp.onreadystatechange = new Function(this.instance+".handleServerResponseObject();");
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xmlHttp.setRequestHeader("Content-length", parameters.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	    this.xmlHttp.send(parameters);
    } else {
		alert('Nieje nastavene meno instancie!');
	}
  } else {
  	if (this.t) clearTimeout(this.t);
    this.t = setTimeout(this.send(), 100);
  }
 } else alert('Nieje zadaná adresa!');
}



function handleServerResponseObject() {

  if (this.xmlHttp.readyState == 4) 
  {

	var status;
	try{
	  status = this.xmlHttp.status;
	    }
	catch(e){
	  status = "Trouble accessing it!";
	}  
    if (status == 200) 
    {
    
    	if (this.handleFunction!=null) {
    
			this.handleFunction(this.xmlHttp.responseText);
		
		} else alert('Nieje nastavena handleFunction!');
    }

  }

}


function Ajax() {

	this.t = null;
	this.address = '';
	this.instance = '';
	this.createXmlHttpRequestObject = createXmlHttpRequestObject;
	this.xmlHttp = this.createXmlHttpRequestObject();
	this.param = new Array();
	this.param_count = new Function("return this.param.length;");
	
	this.handleFunction = null;
	this.send = send;
	this.handleServerResponseObject = handleServerResponseObject;

}


