var sys=new System();

function System()
{
	this.astos=new Array();
	this.adiv=new Array();
	this.ajxLoading=true;
	
	this.dodaj=function(url,pst,did,f)
	{
		if(this.adiv.indexOf(did)==-1) 
		{
			this.adiv.push(did);
			this.astos.push(new Array());
		}
		this.astos[this.adiv.indexOf(did)].push("Ajax('"+url+"','"+pst+"','"+did+"','"+f+"',true);");
	}
	this.usun=function(did)
	{
		var ind=this.adiv.indexOf(did);
		var tmp=new Array();
		for(var i=1;i<this.astos[ind].length;i++) tmp.push(this.astos[ind][i]);
		this.astos[ind]=tmp;
		
		if(this.astos[ind].length>0) eval(this.astos[ind][0]);
	}
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
  {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function xmlIni()
{
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		  alert ("Twoja przeglądarka nie obsługuje AJAXa!");
		  return;
	} 
	else return xmlHttp;
}

function loading(did)
{
	if($(did)) $(did).innerHTML='<center><img src="img/load.gif" border="0" /></center>';
}

function Ajax(url,pst,did,f,s)
{
	if(s==null)
	{
		sys.dodaj(url,pst,did,f);
		if(sys.astos[sys.adiv.indexOf(did)].length>1) return;
	}
	if(sys.ajxLoading) loading(did);
	var xmlHttp=xmlIni();
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4)
		{
			obj = $(did);
			if(obj) obj.innerHTML = xmlHttp.responseText;
			var wynik=xmlHttp.responseText;
			if(f!="") eval(f);
			sys.usun(did);
		}
	}
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; text/html; charset=utf-8');
	xmlHttp.send(pst);
}