var xmlHttp = createXmlHttpRequestObject();
// holds the remote server address and parameters
// variables that establish how often to access the server
var updateInterval = 15; // how many seconds to wait to get new message
var errorRetryInterval = 30; // seconds to wait after server error
// when set to true, display detailed error messages

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try
	{
		
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
		
	}
	catch(e)
	{
		
		
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		
		// try every prog id until one works
		for (var i=0; i < XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
	else
	return xmlHttp;
}


// read a file from the server
function process()
{

	// only continue if xmlHttp isn't void
	if (!xmlHttp) return;
	// don't try to make server requests if the XMLHttpObject is busy
	if (!(xmlHttp.readyState == 0 || xmlHttp.readyState == 4)){
		alert("Can't connect to server, please try again later.");
	}	
	else
	{
		// try to connect to the server
		try
		{
			// get the two values entered by the user
			//			var firstNumber = document.getElementById("firstNumber").value;
			var amount = document.getElementById("amount").value;
			
			// create the params string 
			var params = "amount=" + amount;
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", "../app/livefajla.php?" + params, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

//Vraca sve tekme
function process2()
{

	// only continue if xmlHttp isn't void
	if (!xmlHttp) return;
	// don't try to make server requests if the XMLHttpObject is busy
	if (!(xmlHttp.readyState == 0 || xmlHttp.readyState == 4)){
		alert("Can't connect to server, please try again later.");
	}	
	else
	{
		// try to connect to the server
		try
		{
			// get the two values entered by the user
			//			var firstNumber = document.getElementById("firstNumber").value;
			var amount = document.getElementById("amount").value;
			// create the params string
			var params = "amount=" + amount;
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", "../app/livefajla.php?" + params, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}



// function called when the state of the HTTP request changes
function handleRequestStateChange()
{
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4)
	{
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200)
		{
			try
			{
				// do something with the response from the server
				handleServerResponse();
			}
			catch(e)
			{
				// display error message
				alert("Error reading the response: " + e.toString());
			}
		}
		else
		{
			// display status message
			alert("There was a problem retrieving the data:\n" +
			xmlHttp.statusText);
		}
	}
}



// handles the response received from the server
function handleServerResponse(){
	
	// retrieve the server's response packaged as an XML DOM object
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement) throw("Invalid XML structure:\n" + xmlHttp.responseText);

	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	
	if (rootNodeName == "message"){
		
		message = document.getElementById("message");
		message.innerHTML = 'Nema aktivnih utakmica.';
		
		
	}else{
		
		xmlRoot = xmlResponse.documentElement;
		tekmas = xmlRoot.getElementsByTagName('tekma1');
		
		n = tekmas.length; 
		
		if(n == 1){
			
			xtime 		= xmlRoot.getElementsByTagName('time');
			xcountry 	= xmlRoot.getElementsByTagName('country');
			xhome 		= xmlRoot.getElementsByTagName('home');
			xaway 		= xmlRoot.getElementsByTagName('away');
			xscoreh		= xmlRoot.getElementsByTagName('scoreh');	
			xscorea		= xmlRoot.getElementsByTagName('scorea');		
				
			htime 		= document.getElementById("time");
			hcountry 	= document.getElementById("country");
			hhome 		= document.getElementById("home");
			haway 		= document.getElementById("away");
			hscoreh		= document.getElementById("scoreh");
			hscorea		= document.getElementById("scorea");
			
			htime.innerHTML 		= xtime.item(0).firstChild.data;
			hcountry.innerHTML 		= xcountry.item(0).firstChild.data;
			hhome.innerHTML 		= xhome.item(0).firstChild.data;
			haway.innerHTML 		= xaway.item(0).firstChild.data;
			hscorea.innerHTML		= xscorea.item(0).firstChild.data;
			hscoreh.innerHTML		= xscoreh.item(0).firstChild.data;
			
			//status.innerHTML 	= xstatus.item(i).firstChild.data;		
			
		}else{
			//sve tekme
					
			xtime 		= xmlRoot.getElementsByTagName('time');
			xtimepass 	= xmlRoot.getElementsByTagName('timepass');
			xstatus 	= xmlRoot.getElementsByTagName('status');
			xcountry 	= xmlRoot.getElementsByTagName('country');
			xhome 		= xmlRoot.getElementsByTagName('home');
			xaway 		= xmlRoot.getElementsByTagName('away');
			xscore		= xmlRoot.getElementsByTagName('score');
			xyellowa	= xmlRoot.getElementsByTagName('yellowa');	
			xyellowh	= xmlRoot.getElementsByTagName('yellowh');
			xreda		= xmlRoot.getElementsByTagName('reda');	
			xredh		= xmlRoot.getElementsByTagName('redh');
			xet			= xmlRoot.getElementsByTagName('et');	
			xpn			= xmlRoot.getElementsByTagName('pn');
			xht			= xmlRoot.getElementsByTagName('ht');
						
			html = "<table><tr class=\"eyebrow\"><th>K/O</th><th>Stat</th><th>Time</th><th>League</th><th>Domaćin</th><th>Y</th>";
			html += "<th>R</th><th>Gost</th><th>Y</th><th>R</th><th>HT</th><th>FT</th><th>PN</th><th>ET</th></tr>";
			
			for (var i=0; i < xtime.length; i++){
				
				 even = ((i+1)%2) == 0;
				 even = (!even) ? '' : 'class="even"';
				 row = '<tr ' + even +'>';
				
				 html += row;
				  html += "<td class=\"sum\" id=\"time\">" + xtime.item(i).firstChild.data + "</td>";
				 html += "<td class=\"sum\" id=\"status\">"+ xstatus.item(i).firstChild.data +"</td>";
				 html += "<td class=\"sum\" id=\"timepass\">" + xtimepass.item(i).firstChild.data + "</td>";
				 html += "<td class=\"sum\" id=\"country\">" + xcountry.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"home\" id=\"home\">" + xhome.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"home\" id=\"yellowh\">" + xyellowh.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"home\" id=\"redh\">" + xredh.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"away\" id=\"away\">" + xaway.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"away\" id=\"yellowa\">" + xyellowa.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"away\" id=\"reda\">" + xreda.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"sum\" id=\"ht\">(" + xht.item(i).firstChild.data + ")</td>";
				 html += "<td  class=\"sum\" id=\"score\">" + xscore.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"sum\"  id=\"pn\">" + xpn.item(i).firstChild.data + "</td>";
				 html += "<td  class=\"sum\"  id=\"et\">" + xet.item(i).firstChild.data + "</td>";
				 html += "</tr>";
										
			}
			 html += "</table>";
			 
			 targetDiv = document.getElementById("u_toku_tbl");
			 targetDiv.innerHTML = html; 			
		}
				
	}
	
	if(n == 1){
		
		setTimeout("process();", updateInterval * 1000);
		
	}else{
		
		setTimeout("process2();", updateInterval * 1000);
	}
	
}

