var xmlHttp=Array();
xmlHttp[0]=createXmlHttpRequestObject();
xmlHttp[1]=createXmlHttpRequestObject();
var xmlHttpTS=Array();
xmlHttpTS[0]=-1;
xmlHttpTS[1]=-1;
var xmlHttpURL=new Array();
xmlHttpURL[0]=-1;
xmlHttpURL[1]=-1;
var checkingTO;

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("Your browser does not support ajax");
	else return xmlHttp;
}
var corrOI=new Array(0,1,0);


function process(url, oi)
{
	var timeSt=new Date();
	timeSt=timeSt.getTime();
	xmlHttpTS[corrOI[oi]]=timeSt;
	xmlHttpURL[corrOI[oi]]=url;
	if(xmlHttp[corrOI[oi]])
	{
		xmlHttp[corrOI[oi]].abort();
		try
		{
			xmlHttp[corrOI[oi]].open("POST", url+"&tm="+timeSt, true);
			xmlHttp[corrOI[oi]].onreadystatechange=function(){handleRequestStateChange(url, oi);};
			xmlHttp[corrOI[oi]].send("");
		}
		catch(e)
		{ }
	}
}

var changTO=new Array();
var changeCount=0;
var checkGamesTimes=0;

function handleRequestStateChange(url, oi)
{
	var x=new Date();
	x=x.getTime();
	if(xmlHttp[corrOI[oi]].readyState==4)
	{
		xmlHttpTS[corrOI[oi]]=-1;
		
		if(xmlHttp[corrOI[oi]].status==200)
		{
			try
			{
				handleServerResponse(oi);
			}
			catch(e)
			{}
		}
		
			clearTimeout(changTO[corrOI[oi]]);
			
		if(currentShowingDay==0)	
		{
			if(oi==0 || oi==2)
			{
				
				
				if(changeCount==100 || oi==2)
				{
					changTO[corrOI[oi]]=setTimeout('process("'+urlD+'",'+corrOI[oi]+');', timeInt);
					checkGamesTimes=1;
					changeCount=0;
				}
				else
				{
					changTO[corrOI[oi]]=setTimeout('process("'+chURL+'",'+corrOI[oi]+');', timeInt);
					changeCount++;
				}
				
			}
		}
	}
}

function handleServerResponse(oi)
{
	var xmlResponse;
	xmlResponse=xmlHttp[corrOI[oi]].responseText;
	eval(xmlResponse);
	
	if(!oi)//0=popoxutyun
	{
		
	
		if(fillAllChanges())	//no new games
		{
			
			showAllChanges();
		}
		else	
		{
		
			process(urlD, 2);
		}
	
	}
	else
	{
		if(oi==2)	//sax nkarelov
		{
			
			showAll();
		}
	}
	
}

function checkByTS()
{
	minuteUpd(0);
	var currentTS=new Date();
	currentTS=currentTS.getTime();
	for(var i=0; i<xmlHttp.length; i++)
	{
		if(xmlHttpTS[i]<0)continue;	//inactive
		
		if(currentTS-xmlHttpTS[i]>12000)
		{
			refreshRequest(i);
		}
	}
	
	//ropener++
	
	checkingTO=setTimeout('checkByTS()',60000);
}

function refreshRequest(oi)
{
	xmlHttp[corrOI[oi]].abort();
	clearTimeout(changTO[corrOI[oi]]);
	var nURL=xmlHttpURL[corrOI[oi]];
	if(currentShowingDay==0 && oi==0){
		nURL=urlD;
	}
	process(nURL, oi);
}
function stopUpdating(oi)
{
	xmlHttp[corrOI[oi]].abort();
	xmlHttpTS[corrOI[oi]]=-1;
	clearTimeout(changTO[corrOI[oi]]);
}