var req;
var soundFlag = false;

 function createXMLHttpRequest() {
   try { return new ActiveXObject("Msxml2.XMLHTTP.7.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP.5.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP.4.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
 }


function loadXMLDoc(url) {
	req = createXMLHttpRequest();
    if (req!=null) {
        //req.open("GET", url, true);
		
        req.open("GET", url, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       
		//req.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //This line doesn't work.
		
        req.onreadystatechange =xmlAction;
        req.send(null);
    }
	// open new XML by onse per <time>
	//setTimeout(XmlReader, 10000);   
	setTimeout(XmlReader, 60000); 
}

function loadXMLRefresh(url) {
	req = createXMLHttpRequest();
    if (req!=null) {
        //req.open("GET", url, true);
		
        req.open("GET", url, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       
		//req.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); //This line doesn't work.
		
        req.onreadystatechange =xmlActionRefrashe;
        req.send(null);
    }
	
}
function xmlActionRefrashe(){
	if(req)
	if (req.readyState == 4) {
        // only if "OK"
   //     alert("OKRefresh");
        //alert("OK -0 ");
  //      alert(req.status);
        if (req.status == 200) {
        
//			alert("OK - 1Refrash");
    	}
    }	
}

function XmlReader() {
	//loadXMLDoc('../SportResXML/XMLGamesBoard.ashx?sport=1&as=' +  Math.random ( ));
	
	//loadXMLDoc('../MakeXMLFiles/GamesBoard.xml?as=' +  Math.random ( ));
	loadXMLRefresh('../SportResXML/XMLGamesBoardSoccCache.ashx?sport=1&as=' +  Math.random ( ));
	//loadXMLDoc('../SportResXml/GamesBoard.xml?as=' +  Math.random ( ));
	//SportResXML/GetXml.aspx
	
	//loadXMLRefresh('../SportResXML/GetXml.ashx?as=' +  Math.random ( ));
    loadXMLDoc('../SportResXml/GamesBoard.xml?as=' +  Math.random ( ));
	//loadXMLDoc('../SportResXML/XMLGamesBoardSocc.ashx?sport=1&as=' +  Math.random ( ));
	
}
function xmlAction(){
  
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
 //       alert("OK");
        //alert("OK -0 ");
//        alert(req.status);
        if (req.status == 200) {
        	
		    var i, j, oneRecord, oneObject  
		    req = req.responseXML.documentElement
		    if( req != null )
		    {
		    reqData = req.getElementsByTagName('Game');
		     for ( i = 0; i < reqData.length; i++) {
		     	setActiveGame(reqData[i])
		     	setGoolGame(reqData[i])
		     }
		     }
	//		alert("OK - 1");
    	}
    }	
    playGoal();
   
}

function setActiveGame(nodeGame)
{
	var indexOfGame = getTextFromItem(nodeGame.childNodes.item(0)) ;
	if (document.getElementById("act"+indexOfGame ))
	{
		//change active picture
		var actOfGame = getTextFromItem(nodeGame.childNodes.item(13)) ;
		var start = getTextFromItem(nodeGame.childNodes.item(15)) ;
		var imgAct = document.getElementById("actimg" + indexOfGame );
		var newimg;
		if (actOfGame == "1")
		{
			newimg = "../ImagesNew/active.png";
		}
		else if(start == "0")
		{
			newimg = "../ImagesNew/finish.GIF";
		}else newimg = "../ImagesNew/noactive.png";
		
		if (imgAct.src != newimg)
			imgAct.src = newimg;
		
		//change active
		var act = document.getElementById("act"+indexOfGame ).innerText;
		var newAct = getTextFromItem(nodeGame.childNodes[16]);
		if (!isNaN(newAct)){
			newAct  = newAct + "&nbsp;<img src='../imagesNew/psik1.gif'/>";
		}
		if (act != newAct)
		{
			document.getElementById("act"+indexOfGame ).innerHTML = newAct;
		}
	}
}

function setGoolGame(nodeGame)
{
	var actOfGame = getTextFromItem(nodeGame.childNodes.item(13)) ;
	if (actOfGame == "1")
	{
		
		var indexOfGame = getTextFromItem(nodeGame.childNodes.item(0)) ;
		
		if (document.getElementById("res"+indexOfGame ))
		{
	
			var resGame = document.getElementById("res"+indexOfGame ).innerText;
			var newResGame = getTextFromItem(nodeGame.childNodes[8]) + " : " + getTextFromItem(nodeGame.childNodes[7]) ;
			//var imgGool = document.getElementById("img" + indexOfGame );
			if (resGame != newResGame)
			{
			//	imgGool.src = "imagesNew/Gamesboard/goal.gif";
				document.getElementById("res"+indexOfGame ).innerText = newResGame;
			//	soundFlag  = true;	
			}
			//else
			//{
			//	if (imgGool.src != "images/Gamesboard/goal_emp.gif")
			//		imgGool.src = "images/Gamesboard/goal_emp.gif";
			//}
		}
	}
}

function getTextFromItem(itemT)
{
	var txtItem = itemT.firstChild.nodeValue;
	return txtItem;
}

function playGoal()
{
	//if (soundFlag )
	//	document.getElementById("vgoal").src="Voice/Goal.wav";
	//soundFlag = false;
}

