var loading = "<img src='/images/ajax-loaderJaune.gif' style='display:inline;'>Chargement ...";

function atpTournamentRequest(url,tid,date)
{
	var httpRequest;
	
	if (window.XMLHttpRequest)  // Mozilla, Safari, ...
	{
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType)
		{
			httpRequest.overrideMimeType('text/xml');
		}
	}else if (window.ActiveXObject) // IE
	{
		try
		{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e)
		{
			try
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e) {}
		}
	}
	
	if (!httpRequest)
	{
		document.getElementById("atp_tournaments").innerHTML = 'Votre navigateur ne supporte pas une instance XMLHTTP';
		return false;
	}
	
	httpRequest.onreadystatechange = function() { atp_live_tournaments(httpRequest,tid,date);};
	httpRequest.open('GET', 'http://www.rds.ca/hockey/get_xml.php?provider=statsinc&file='+url+'.XML', true);
	httpRequest.send('');
}

function atp_live_tournaments(httpRequest,tid,date)
{
	document.getElementById("atp_tournaments").innerHTML = loading;
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200) 
		{
			var xmldoc = httpRequest.responseXML;
			var txt = '';
			var tournaments = xmldoc.getElementsByTagName('tennis-scores');
			
			for(a = 0; a< tournaments.length;a++)
			{
					var score_list = xmldoc.getElementsByTagName('tennis-scores').item(a).getElementsByTagName('tennis-score');
					var tournament_name = xmldoc.getElementsByTagName('tennis-scores').item(a).getElementsByTagName('tournament').item(0).getAttribute("short-name");
					var tournament_id = xmldoc.getElementsByTagName('tennis-scores').item(a).getElementsByTagName('tournament').item(0).getAttribute("id");
					
					if(a == 0)
					{
						if(tid == tournament_id || tid == 'first'){txt = 'Tournois: <b>' + tournament_name + '</b>';tid = tournament_id;}
							else {txt = txt + 'Tournois: <a href="resultats_atp.php?tid=' + tournament_id + '">' + tournament_name + '</a>';}
					} else {
						if(tid == tournament_id){txt = txt +' | <b>' + tournament_name + '</b>';}
							else {txt = txt + ' | <a href="resultats_atp.php?tid=' + tournament_id + '">' + tournament_name + '</a>';}
						
					}
			}
			document.getElementById("atp_tournaments").innerHTML = txt+' | <img src="/images/ajax-loaderJaune.gif" style="display:inline;">';
			atp_live_results(httpRequest,tid,date);
		}
	}
}


function atp_live_results(httpRequest,tid,date)
{
	document.getElementById("boxContent").innerHTML = loading;
	var txtTmp = '';
	var txtDates = '';
	var quebecIDs = new Array(184386,281795,264180);
	var canadaIDs = new Array(461860,245591);
	
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200) 
		{
			var xmldoc = httpRequest.responseXML;
			var status = '';
			var tournamentDates = new Array();
			
			var tournaments = xmldoc.getElementsByTagName('tennis-scores');
			for(a = 0; a< tournaments.length;a++)
			{
				if(tournaments.item(a).getElementsByTagName('tournament').item(0).getAttribute("id") == tid)
				{
					var score_list = xmldoc.getElementsByTagName('tennis-scores').item(a).getElementsByTagName('tennis-score');			
				}
			}
      
	  		// Information pour Chaque Partie
			var queInd = 0;
			var canInd = 0;
	  		for(i = 0; i < score_list.length; i++)
			{
				xmlDay = score_list.item(i).getElementsByTagName('date').item(0).getAttribute('date');
				xmlMonth = score_list.item(i).getElementsByTagName('date').item(0).getAttribute('month');
				xmlYear = score_list.item(i).getElementsByTagName('date').item(0).getAttribute('year');
				
				if(xmlMonth<10){xmlMonth = "0"+xmlMonth;}
				if(xmlDay<10){xmlDay = "0"+xmlDay;}
				xmlDate = xmlDay+xmlMonth+xmlYear;
				
				if(xmlDate == date)
				{
					tournamentDates[xmlDate] = xmlDate;
					status = score_list.item(i).getElementsByTagName('gamestate').item(0).getAttribute('status');
					if (status == "Final"){statusFr = "<b>Final</b>";}
						else if (status == "Final - (After Postponed)"){statusFr = "<b>Final</b>";}
						else if (status == "In-Progress"){statusFr = "<b>En cours</b>";}
						else if (status == "Pre-Game"){statusFr = "<b>Avant-match</b>";}
						
					winnerID = score_list.item(i).getElementsByTagName('result').item(0).getAttribute('win-id'); 
					ronde = score_list.item(i).getElementsByTagName('round').item(0).getAttribute('round-name');
					if (ronde == "First Round"){ronde = "1<sup>re</sup> Ronde";}
						else if (ronde == "Second Round"){ronde = "2<sup>e</sup> Ronde";}
						else if (ronde == "Third Round"){ronde = "3<sup>e</sup> Ronde";}
						else if (ronde == "Fourth Round"){ronde = "4<sup>e</sup> Ronde";}
						else if (ronde == "Quarter Finals"){ronde = "Quart de finale";}
						else if (ronde == "Semi Finals"){ronde = "Demi-finale";}
						else if (ronde == "Finals"){ronde = "finale";}
					
					player1 = score_list.item(i).getElementsByTagName('player-one').item(0).getAttribute('last-name');
					player1ID = score_list.item(i).getElementsByTagName('player-one').item(0).getAttribute('player-id');
					player1Rank = score_list.item(i).getElementsByTagName('player-one').item(0).getAttribute('seed');
					player1LineScore = score_list.item(i).getElementsByTagName('linescore').item(0);	
					player1Sets = new Array(player1LineScore.getElementsByTagName('set').length);
					player1Quebec = '';
					player1Canada = '';
					for(j = 0; j < player1LineScore.getElementsByTagName('set').length; j++)
					{
						player1Sets[j] = player1LineScore.getElementsByTagName('set').item(j).getAttribute('games');
					}
					
					player2 = score_list.item(i).getElementsByTagName('player-two').item(0).getAttribute('last-name');
					player2ID = score_list.item(i).getElementsByTagName('player-two').item(0).getAttribute('player-id');
					player2Rank = score_list.item(i).getElementsByTagName('player-two').item(0).getAttribute('seed');
					player2LineScore = score_list.item(i).getElementsByTagName('linescore').item(1);	
					player2Sets = new Array(player2LineScore.getElementsByTagName('set').length);
					player2Quebec = '';
					player2Canada = '';
					for(k = 0; k < player2LineScore.getElementsByTagName('set').length; k++)
					{
						player2Sets[k] = player2LineScore.getElementsByTagName('set').item(k).getAttribute('games');
					}
					
					
					for(queInd = 0; queInd < quebecIDs.length;queInd++)
					{
						if(quebecIDs[queInd] == player1ID){player1Quebec = "<img src='http://www.rds.ca/images/centrale/jo/drapeaux_quebec.gif' height='10' style='display:inline;'>";}
						if(quebecIDs[queInd] == player2ID){player2Quebec = "<img src='http://www.rds.ca/images/centrale/jo/drapeaux_quebec.gif' height='10' style='display:inline;'>";}
					}
					for(canInd = 0; canInd < 1;canInd++)
					{
						if(canadaIDs[canInd] == player1ID){player1Canada = "<img src='http://www.rds.ca/images/centrale/jo/drapeaux_canada.gif' height='10' style='display:inline;'>";}
						if(canadaIDs[canInd] == player2ID){player2Canada = "<img src='http://www.rds.ca/images/centrale/jo/drapeaux_canada.gif' height='10' style='display:inline;'>";}
					}


					
					// PRINT
					if(status != "Bye")
					{
						txtTmp = txtTmp + '<div class="TennisBox">';
						txtTmp = txtTmp + '<table width="95%"><tr><td width="150">' + statusFr + '</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>';
						txtTmp = txtTmp + '<tr><td>';
						// PLAYER 1
						if(player1Rank != "") 
						{
							if(winnerID == player1ID){txtTmp = txtTmp + player1Quebec+player1Canada+"<a onclick=\"showProfile('"+player1ID+"', event)\" href=\"javascript:void(0);\"><b>" + player1 + " (" + player1Rank + ")" + "</b></a>";}
								else {txtTmp = txtTmp + player1Quebec+player1Canada+"<a onclick=\"showProfile('"+player1ID+"', event)\" href=\"javascript:void(0);\">"+ player1 + " (" + player1Rank + ")</a>" ;}
						} else {
							if(winnerID == player1ID){txtTmp = txtTmp + player1Quebec+player1Canada+"<a onclick=\"showProfile('"+player1ID+"', event)\" href=\"javascript:void(0);\"><b>" + player1 + "</b></a>";}
								else {txtTmp = txtTmp +player1Quebec+player1Canada+"<a onclick=\"showProfile('"+player1ID+"', event)\" href=\"javascript:void(0);\">"+player1+"</a>";}
						}
						txtTmp = txtTmp + '</td>';
						
						for(j = 0; j< player1Sets.length; j++)
						{
							if(player1Sets[j] > player2Sets[j]) {txtTmp = txtTmp + "<td><b>" + player1Sets[j] + "</b></td>";}
								else {txtTmp = txtTmp + "<td>" + player1Sets[j] + "</td>";}
						}
						if(j==0)
						{
							txtTmp = txtTmp + "<td>-</td>";
							j++;
						}
						
						for(c = j;c<5;c++)
						{
							txtTmp = txtTmp + "<td>-</td>";
						}
					
						// PLAYER 2
						txtTmp = txtTmp + '</tr><td>';
						if(player2Rank != "") 
						{
							if(winnerID == player2ID){txtTmp = txtTmp + player2Quebec+player2Canada+"<a onclick=\"showProfile('"+player2ID+"', event)\" href=\"javascript:void(0);\"><b>" + player2 + " (" + player2Rank + ")" + "</b></a>";}
								else {txtTmp = txtTmp + player2Quebec+player2Canada+"<a onclick=\"showProfile('"+player2ID+"', event)\" href=\"javascript:void(0);\">"+ player2 + " (" + player2Rank + ")</a>"}
						} else {
							if(winnerID == player2ID){txtTmp = txtTmp + player2Quebec+player2Canada+"<a onclick=\"showProfile('"+player2ID+"', event)\" href=\"javascript:void(0);\"><b>" + player2 + "</b></a>";}
								else {txtTmp = txtTmp + player2Quebec+player2Canada +"<a onclick=\"showProfile('"+player2ID+"', event)\" href=\"javascript:void(0);\">"+ player2 + "</a>";}
						}
						txtTmp = txtTmp + '</td>';
						
						for(j = 0; j< player2Sets.length; j++)
						{
							if (player2Sets[j] > player1Sets[j]) {txtTmp = txtTmp + "<td><b>" + player2Sets[j] + "</b></td>";}
								else {txtTmp = txtTmp + "<td>" + player2Sets[j] + "</td>";}
						}
						if(j==0)
						{
							txtTmp = txtTmp + "<td>-</td>";
							j++;
						} 
							
						for(c = j;c<5;c++)
						{
							txtTmp = txtTmp + "<td>-</td>";
						}
					
						txtTmp = txtTmp + '</tr></table><table width="300" style="border-left:1px solid #999999;border-bottom:1px solid #999999;border-right:1px solid #999999;"><tr><td>'+ronde+'</td></tr></table></div>';
					} // End of For Loop
				} else {
					tournamentDates[xmlDate] = xmlDate;
				}
			}// End For Loop
			

			document.getElementById("boxContent").innerHTML = txtTmp;
			var dateCount = 0
			for(c in tournamentDates)
			{
				if(c == date) 
				{
					if (dateCount != 0) {txtDates = txtDates + ' - ';}
					txtDates = txtDates +tournamentDates[c].substring(0,2);
				} else {
					if(!isNaN(c))
					{
							if (dateCount != 0) {txtDates = txtDates + ' - ';}
						txtDates = txtDates + '<a href="resultats_atp.php?tid='+tid+'&date='+c+'">' +tournamentDates[c].substring(0,2) + "</a>";
					}
				}
				dateCount++;
			}
			document.getElementById("atp_dates").innerHTML = txtDates;
		}// End httpRequest.status == 200
	} else { // HttpRequest.readyState != 4
		txtTmp = txtTmp + 'Chargement ...';
		document.getElementById("boxContent").innerHTML = txtTmp;
	}
}//End Function atp_live_contents()
