var ajaxes = new Array();
var timeoutseconds = 20;

function RegisterAjax(ajax)
{
    for(var i=0;i<=ajaxes.length-1;i++)
    {
         if (ajaxes[i]==null)
         {
             ajaxes[i] = ajax;
             return i;
         }
    }
    ajaxes[ajaxes.length] = ajax;
    return ajaxes.length-1;
}

function UnRegisterAjax(ajax)
{
    for(var i=0;i<=ajaxes.length-1;i++)
    {
         if (ajaxes[i]==ajax)
         {
             ajaxes[i] = null;
             return;
         }
    }
 }

function RetrieveAjax(xmlHttp)
{
    for(var i=0;i<=ajaxes.length-1;i++)
    {
       var ajax = ajaxes[i];
       if (ajax!=null)
       {
           if (ajax.xmlHttp == xmlHttp) return ajax;
       }
    }
    return null;
}

function TAjaxRequest(url,callback,renewin)
{
    this.url = url;
    this.safetimeout = 0;
    this.renewtimeout = 0;
    this.callback = callback;
    this.renewin = renewin;
    this.Process = TAjaxRequest_Process;
    this.SafeProcess = TAjaxRequest_SafeProcess;
    this.Duplicate = TAjaxRequest_Duplicate;
    this.RenewProcess = TAjaxRequest_RenewProcess;
    this.index = RegisterAjax(this);
}

function TAjaxRequest_Duplicate()
{
    return new TAjaxRequest(this.url,this.callback,this.renewin);
}

function TAjaxRequest_Process()
{
	var xmlHttp;
	try
    {
    	xmlHttp=new XMLHttpRequest();
    }
  	catch(e)
    {
    	try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
    	catch(e)
    	{
    		try
        	{
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
      		catch(e)
        	{
        		alert("Your browser does not support AJAX!");
        		return false;
        	}
    	}
    }
    this.xmlHttp = xmlHttp;
    xmlHttp.onreadystatechange=function()
    {
    	if (xmlHttp.readyState==4)
    	{
            var ajax = RetrieveAjax(xmlHttp);
            if (ajax!=null)
            {
                clearTimeout(ajax.safetimeout);
                var renewed = false;
                if (ajax.callback(xmlHttp.responseText,ajax))
                {
                    if (ajax.renewin>0)
                    {
                        ajax.renewtimeout = setTimeout("ajaxes["+ajax.index.toString()+"].RenewProcess()",ajax.renewin*1000);
                        renewed = true;
                    }
                }
                if (!renewed) UnRegisterAjax(ajax);
            }
    	}
    }
    xmlHttp.open("get",this.url,true);
    xmlHttp.send(null);
    this.safetimeout = setTimeout("ajaxes["+this.index.toString()+"].SafeProcess()",timeoutseconds*1000);
}

function TAjaxRequest_SafeProcess()
{
    clearTimeout(this.safetimeout);
    UnRegisterAjax(this);
    var ajax = this.Duplicate();
    ajax.Process();
}

function TAjaxRequest_RenewProcess()
{
    clearTimeout(this.renewtimeout);
    UnRegisterAjax(this);
    var ajax = this.Duplicate();
    ajax.Process();
}
