function populateIndexPage(){
	xmlIndexInfo=loadXMLDoc("index.xml");
	xmlStandingInfo=loadXMLDoc("matches/standing04.xml");
	
	var latestresults = "";
	for (var i = 0 ; i < xmlIndexInfo.getElementsByTagName("latest")[0].getElementsByTagName("match").length; i++) {
		xmlMatchInfo=loadXMLDoc("matches/" + xmlIndexInfo.getElementsByTagName("latest")[0].getElementsByTagName("match")[i].getAttribute("id") + ".xml");
		latestresults = latestresults + '<li><span class="special"><strong>'
				+ xmlIndexInfo.getElementsByTagName("latest")[0].getElementsByTagName("match")[i].getAttribute("round") + ', '
				+ xmlMatchInfo.getElementsByTagName("date")[0].childNodes[0].nodeValue + ' г., '
				+ xmlMatchInfo.getElementsByTagName("time")[0].childNodes[0].nodeValue + ' ч.'
				+ '</strong></span></li><li class="borderbottom"><a href="match.html?matchid=' 
				+ xmlIndexInfo.getElementsByTagName("latest")[0].getElementsByTagName("match")[i].getAttribute("id") + '"><strong>' 
				+ xmlMatchInfo.getElementsByTagName("hometeam")[0].childNodes[0].nodeValue + ' <span class="result">'
				+ xmlMatchInfo.getElementsByTagName("fulltime")[0].childNodes[0].nodeValue + '</span> '
				+ xmlMatchInfo.getElementsByTagName("awayteam")[0].childNodes[0].nodeValue 
				+ '</strong></a></li>';
		
	}	
	document.getElementById("latestresults").innerHTML=latestresults;
	
	var fixtures = "";
	for (var i = 0 ; i < xmlIndexInfo.getElementsByTagName("fixtures")[0].getElementsByTagName("match").length; i++) {	
		xmlMatchInfo=loadXMLDoc("matches/" + xmlIndexInfo.getElementsByTagName("fixtures")[0].getElementsByTagName("match")[i].getAttribute("id") + ".xml");
		fixtures = fixtures + '<li><span class="special"><strong>'
				+ xmlIndexInfo.getElementsByTagName("fixtures")[0].getElementsByTagName("match")[i].getAttribute("round") + ', '
				+ xmlMatchInfo.getElementsByTagName("date")[0].childNodes[0].nodeValue + ' г., '
				+ xmlMatchInfo.getElementsByTagName("time")[0].childNodes[0].nodeValue + ' ч.'
				+ '</strong></span></li><li class="borderbottom"><a href="match.html?matchid=' 
				+ xmlIndexInfo.getElementsByTagName("fixtures")[0].getElementsByTagName("match")[i].getAttribute("id") + '"><strong>' 
				+ xmlMatchInfo.getElementsByTagName("hometeam")[0].childNodes[0].nodeValue + ' <span class="result">'
				+ xmlMatchInfo.getElementsByTagName("fulltime")[0].childNodes[0].nodeValue + '</span> '
				+ xmlMatchInfo.getElementsByTagName("awayteam")[0].childNodes[0].nodeValue 
				+ '</strong></a></li>';
	}	
	document.getElementById("fixtures").innerHTML=fixtures;
	
	var standing = '<table class="standingtable"><tr><th>М.</th><th>Отбор</th><th>М.</th><th>Г.Р.</th><th>Т.</th></tr>';
	for (var i = 0 ; i < xmlStandingInfo.getElementsByTagName("position").length; i++) {	
		if (xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("team").indexOf('Добрич') > -1) {
			standing = standing + '<tr class="standingdobrich">';
		} else {
			standing = standing + '<tr class="standingother">';
		}
		standing = standing + '<td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("id") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("team") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("matches") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("goals") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("points") + '</td></tr>'
	}
	standing = standing + '</table>';
	
	document.getElementById("standing").innerHTML=standing;
}

function populatePlayersPage(){	
	
	xmlPlayers08Info=loadXMLDoc("players/players08.xml");
	xmlPlayers09Info=loadXMLDoc("players/players09.xml");
	
	var players08 = "<h3>Сезон 2008/2009" + 
		"<img id=showImg src=images/expand.png onclick=show('cont08','hideImg','showImg'); class=playersHeader style=display:inline; />" +
		"<img id=hideImg src=images/collapse.png onclick=hide('cont08','hideImg','showImg'); class=playersHeader style=display:none; />" +
		"</h3><div id='cont08' style='display:none;'>";
	for (var i = 0 ; i < xmlPlayers08Info.getElementsByTagName("players")[0].childNodes.length; i++) {		
		players08 = players08 + '<div class="playerBox"><a href="playerinfo.html?playerid='
			+ xmlPlayers08Info.getElementsByTagName("players")[0].childNodes[i].nodeName + '"><img src="players/'
			+ xmlPlayers08Info.getElementsByTagName("players")[0].childNodes[i].nodeName + '_96.png" /></a><a href="playerinfo.html?playerid='
			+ xmlPlayers08Info.getElementsByTagName("players")[0].childNodes[i].nodeName + '"><br />'
			+ xmlPlayers08Info.getElementsByTagName("players")[0].childNodes[i].getAttribute("number") + '. '
			+ xmlPlayers08Info.getElementsByTagName("players")[0].childNodes[i].getAttribute("name") + '</a></div>';
	}
	players08 = players08 + "</div>"
	document.getElementById("players08").innerHTML=players08;
	
	var players09 = "<h3>Сезон 2009/2010</h3>";
	for (var i = 0 ; i < xmlPlayers09Info.getElementsByTagName("players")[0].childNodes.length; i++) {		
		players09 = players09 + '<div class="playerBox"><a href="playerinfo.html?playerid='
			+ xmlPlayers09Info.getElementsByTagName("players")[0].childNodes[i].nodeName + '"><img src="players/'
			+ xmlPlayers09Info.getElementsByTagName("players")[0].childNodes[i].nodeName + '_96.png" /></a><a href="playerinfo.html?playerid='
			+ xmlPlayers09Info.getElementsByTagName("players")[0].childNodes[i].nodeName + '"><br />'
			+ xmlPlayers09Info.getElementsByTagName("players")[0].childNodes[i].getAttribute("number") + '. '
			+ xmlPlayers09Info.getElementsByTagName("players")[0].childNodes[i].getAttribute("name") + '</a></div>';
	}
	document.getElementById("players09").innerHTML=players09;
}

function populatePlayerInfoPage(){
	xmlPlayerInfo=loadXMLDoc(getPlayerFile("playerid"));
	
	document.getElementById("playerimg").setAttribute("src", "players/" + getURLParam("playerid") + "_120_160.png");
	
	var name = "&nbsp;" + xmlPlayerInfo.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
	if (xmlPlayerInfo.getElementsByTagName("middlename")[0].childNodes.length > 0){
		name = name + "&nbsp;" + document.getElementById("name").innerHTML + xmlPlayerInfo.getElementsByTagName("middlename")[0].childNodes[0].nodeValue;
	}
	name = name + "&nbsp;"	+ xmlPlayerInfo.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
	
	document.getElementById("name").innerHTML =  name;
	document.getElementById("number").innerHTML= "&nbsp;" + xmlPlayerInfo.getElementsByTagName("number")[0].childNodes[0].nodeValue;
	document.getElementById("position").innerHTML= "&nbsp;" + xmlPlayerInfo.getElementsByTagName("position")[0].childNodes[0].nodeValue;
	document.getElementById("birthday").innerHTML= "&nbsp;" + xmlPlayerInfo.getElementsByTagName("birthday")[0].childNodes[0].nodeValue + " г.";
	document.getElementById("height").innerHTML= "&nbsp;" + xmlPlayerInfo.getElementsByTagName("height")[0].childNodes[0].nodeValue + " см.";
	document.getElementById("weight").innerHTML= "&nbsp;" + xmlPlayerInfo.getElementsByTagName("weight")[0].childNodes[0].nodeValue+ " кг.";

	var seasons = "<h3 class=h3nomargin><strong>Сезони</strong></h3><table id=seasons style=width:100%><tr><th><strong>Сезон</strong></th><th><strong>Мачове</strong></th><th><strong>Голове</strong></th><th><strong><img src=images/yellowcard.png /></strong></th><th><strong><img src=images/redcard.png /></strong></th></tr>";
	for (var i = 0 ; i < xmlPlayerInfo.getElementsByTagName("season").length; i++) {	
		seasons = seasons + '<tr><td>'
			+ xmlPlayerInfo.getElementsByTagName("season")[i].getAttribute("id") + '</td><td>'
			+ xmlPlayerInfo.getElementsByTagName("season")[i].getAttribute("matches") + '</td><td>'
			+ xmlPlayerInfo.getElementsByTagName("season")[i].getAttribute("goals") + '</td><td>'
			+ xmlPlayerInfo.getElementsByTagName("season")[i].getAttribute("yellowcards") + '</td><td>'
			+ xmlPlayerInfo.getElementsByTagName("season")[i].getAttribute("redcards") + '</td></tr>';
	}	
	seasons = seasons + "</table>";
	
	document.getElementById("playerseasondiv").innerHTML=seasons;
	
	if (xmlPlayerInfo.getElementsByTagName("albumid")[0].childNodes.length > 0){
		if (xmlPlayerInfo.getElementsByTagName("albumid")[0].childNodes[0].nodeValue != null)
		{
		document.getElementById("playeralbumdiv").innerHTML =
			"<embed type=application/x-shockwave-flash src=http://picasaweb.google.com/s/c/bin/slideshow.swf width=600 height=400 flashvars=host=picasaweb.google.com&hl=bg&feat=flashalbum&RGB=0x000000&feed=http%3A%2F%2Fpicasaweb.google.com%2Fdata%2Ffeed%2Fapi%2Fuser%2Famfk.dobrich%2Falbumid%" 
			+ xmlPlayerInfo.getElementsByTagName("albumid")[0].childNodes[0].nodeValue 
			+ "%3Falt%3Drss%26kind%3Dphoto%26authkey%3DGv1sRgCIH7que528GKOw%26hl%3Dbg pluginspage=http://www.macromedia.com/go/getflashplayer></embed>"
		}
	}
}

function populateResultsPage(){
	xmlMatchesInfo05=loadXMLDoc("matches/matches05.xml");	
	xmlMatchesInfo04=loadXMLDoc("matches/matches04.xml");
	xmlStandingInfo04=loadXMLDoc("matches/standing04.xml");
	xmlMatchesInfo03=loadXMLDoc("matches/matches03.xml");	
	xmlMatchesInfo02=loadXMLDoc("matches/matches02.xml");
	xmlStandingInfo02=loadXMLDoc("matches/standing02.xml");
	xmlMatchesInfo01=loadXMLDoc("matches/matches01.xml");
	xmlStandingInfo01=loadXMLDoc("matches/standing01.xml");	
	
	document.getElementById("matches05").innerHTML = fixturesContent(xmlMatchesInfo05, "spring");	
	
	document.getElementById("matches04spring").innerHTML = fixturesContent(xmlMatchesInfo04, "spring");
	document.getElementById("matches04autumn").innerHTML = fixturesContent(xmlMatchesInfo04, "autumn");
	document.getElementById("standing04").innerHTML = standingContent(xmlStandingInfo04);
	
	document.getElementById("matches03").innerHTML = fixturesContent(xmlMatchesInfo03, "spring");	
	
	document.getElementById("matches02").innerHTML = fixturesContent(xmlMatchesInfo02, "spring");	
	document.getElementById("standing02").innerHTML = standingContent(xmlStandingInfo02);

	document.getElementById("matches01spring").innerHTML = fixturesContent(xmlMatchesInfo01, "spring");
	document.getElementById("matches01autumn").innerHTML = fixturesContent(xmlMatchesInfo01, "autumn");	
	document.getElementById("standing01").innerHTML = standingContent(xmlStandingInfo01);	
}

function fixturesContent(xmlMatchesInfo, season){
	var matches = "";
	for (var i = 0 ; i < xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match").length; i++) {
		if (xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match")[i].getAttribute("played") != null){
			matches = matches + '<li><span class="special"><strong>'
				+ xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match")[i].getAttribute("round") + '</strong></span></li><li class="borderbottom"><strong>'
				+ xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match")[i].getAttribute("note") + '</strong></li>';
		} else {
			xmlMatchInfo=loadXMLDoc("matches/" + xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match")[i].getAttribute("id") + ".xml");
			matches = matches + '<li><span class="special"><strong>'
				+ xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match")[i].getAttribute("round") + ', '
				+ xmlMatchInfo.getElementsByTagName("date")[0].childNodes[0].nodeValue + ' г., '
				+ xmlMatchInfo.getElementsByTagName("time")[0].childNodes[0].nodeValue + ' ч.'
				+ '</strong></span></li><li class="borderbottom"><a href="match.html?matchid=' 
				+ xmlMatchesInfo.getElementsByTagName(season)[0].getElementsByTagName("match")[i].getAttribute("id") + '"><strong>' 
				+ xmlMatchInfo.getElementsByTagName("hometeam")[0].childNodes[0].nodeValue + ' <span class="result">'
				+ xmlMatchInfo.getElementsByTagName("fulltime")[0].childNodes[0].nodeValue + '</span> '
				+ xmlMatchInfo.getElementsByTagName("awayteam")[0].childNodes[0].nodeValue 
				+ '</strong></a></li>';
			}
	}	
	
	return matches;
}

function standingContent(xmlStandingInfo){
	var standing = '<table class="standingtable"><tr><th>М.</th><th>Отбор</th><th>М.</th><th>Г.Р.</th><th>Т.</th></tr>';
	for (var i = 0 ; i < xmlStandingInfo.getElementsByTagName("position").length; i++) {	
		if (xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("team").indexOf('Добрич') > -1) {
			standing = standing + '<tr class="standingdobrich">';
		} else {
			standing = standing + '<tr class="standingother">';
		}
		standing = standing + '<td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("id") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("team") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("matches") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("goals") + '</td><td>'
			+ xmlStandingInfo.getElementsByTagName("position")[i].getAttribute("points") + '</td></tr>'
	}
	standing = standing + '</table>';
	
	return standing
}

function populateMatchPage(){
	xmlMatchInfo=loadXMLDoc(getMatchFile("matchid"));
	xmlPlayersInfo=loadXMLDoc(getPlayersFile("matchid"));
	
	document.getElementById("hometeam").innerHTML=xmlMatchInfo.getElementsByTagName("hometeam")[0].childNodes[0].nodeValue;
	document.getElementById("hometeamlogo").setAttribute("src", xmlMatchInfo.getElementsByTagName("hometeam")[0].getAttribute("logo"));
	document.getElementById("awayteam").innerHTML=xmlMatchInfo.getElementsByTagName("awayteam")[0].childNodes[0].nodeValue;
	document.getElementById("awayteamlogo").setAttribute("src", xmlMatchInfo.getElementsByTagName("awayteam")[0].getAttribute("logo"));
	document.getElementById("fulltime").innerHTML=xmlMatchInfo.getElementsByTagName("fulltime")[0].childNodes[0].nodeValue;
		
	if (xmlMatchInfo.getElementsByTagName("fulltime")[0].getAttribute("played") == null){
		
		var isDobrichHomeTeam = false;
		if (document.getElementById("hometeam").innerHTML.indexOf("Добрич") > -1)
		{
			isDobrichHomeTeam = true;
		}
		
		var goals="";
		for (var i = 0 ; i < xmlMatchInfo.getElementsByTagName("goal").length ; i++) {
			goals = goals + '<span class="special">'
				+ xmlMatchInfo.getElementsByTagName("goal")[i].getAttribute("score") + ' </span>'
				+ (xmlMatchInfo.getElementsByTagName("goal")[i].getAttribute("playerid") != null ? 
					'<a href="playerinfo.html?playerid=' + xmlMatchInfo.getElementsByTagName("goal")[i].getAttribute("playerid") + '">' 
					+ xmlPlayersInfo.getElementsByTagName(xmlMatchInfo.getElementsByTagName("goal")[i].getAttribute("playerid"))[0].getAttribute("name")
					+ '</a>'
					: (xmlMatchInfo.getElementsByTagName("goal")[i].childNodes.length > 0 ?
						xmlMatchInfo.getElementsByTagName("goal")[i].childNodes[0].nodeValue : (isDobrichHomeTeam ? document.getElementById("awayteam").innerHTML : document.getElementById("hometeam").innerHTML)))
				+ ' ' + xmlMatchInfo.getElementsByTagName("goal")[i].getAttribute("minute") + "'"
				+ '<br />';
		}	
		document.getElementById("goals").innerHTML=goals;
		
		var lineup="";
		for (var i = 0 ; i < xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player").length; i++) {
			lineup = lineup + '<span class="special">'
				+ xmlPlayersInfo.getElementsByTagName(xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("id"))[0].getAttribute("number")
				+ '. </span>'
				+ '<a href="playerinfo.html?playerid=' 
				+ xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("id") + '">'
				+ xmlPlayersInfo.getElementsByTagName(xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("id"))[0].getAttribute("name")
				+ (xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("goalkeeper") != null ? 
					' (в.)' : '')
				+ (xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("captain") != null ? 
					' (к.)' : '')
				+ '</a>'
				+ (xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("yellow") != null ? 
					' <img src="images/yellowcard.png">' + xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("yellowminute") + "'" : '')
				+ (xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("red") != null ? 
					' <img src="images/redcard.png">' + xmlMatchInfo.getElementsByTagName("lineup")[0].getElementsByTagName("player")[i].getAttribute("redminute") + "'" : '')
				+ '<br />';
		}	
		document.getElementById("lineup").innerHTML=lineup;
		
		var reserves="";
		for (var i = 0 ; i < xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player").length; i++) {
			reserves = reserves + '<span class="special">'
				+ xmlPlayersInfo.getElementsByTagName(xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("id"))[0].getAttribute("number")
				+ '. </span>'
				+ '<a href="playerinfo.html?playerid=' + xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("id") + '">'
				+ xmlPlayersInfo.getElementsByTagName(xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("id"))[0].getAttribute("name")
				+ '</a>'
				+ (xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("yellow") != null ? 
					' <img src="images/yellowcard.png">' + xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("yellowminute") + "'" : '')
				+ (xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("red") != null ? 
					' <img src="images/redcard.png">' + xmlMatchInfo.getElementsByTagName("reserves")[0].getElementsByTagName("player")[i].getAttribute("redminute") + "'" : '')
				+ '<br />';
		}	
		document.getElementById("reserves").innerHTML=reserves;	
	}
	
	if (xmlMatchInfo.getElementsByTagName("note").length > 0)
	{
		document.getElementById("note").innerHTML=xmlMatchInfo.getElementsByTagName("note")[0].childNodes[0].nodeValue;
	}
}

function loadXMLDoc(dname){
	var xmlDoc;
	if (window.XMLHttpRequest)
	  {
	  xmlDoc=new window.XMLHttpRequest();
	  xmlDoc.open("GET",dname,false);
	  xmlDoc.send("");
	  return xmlDoc.responseXML;
	  }
	// IE 5 and IE 6
	else if (ActiveXObject("Microsoft.XMLDOM"))
	  {
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async=false;
	  xmlDoc.load(dname);
	  return xmlDoc;
	  }
	alert("Error loading document");
	return null;
}

function getURLParam(name){ 
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	var regexS = "[\\?&]"+name+"=([^&#]*)"; 
	var regex = new RegExp( regexS ); 
	var results = regex.exec( window.location.href ); 
	if( results == null )    
		return ""; 
	else 
		return results[1];
}

function getMatchFile(paramName){
	return "matches/" + getURLParam(paramName) + ".xml";
}

function getPlayersFile(paramName){	
	var tournamentId = parseInt(getURLParam(paramName).substring(1,3));
	if (tournamentId <= 3){
		return "players/players08.xml";
	} else {
		return "players/players09.xml";
	}
}

function getPlayerFile(paramName){
	return "players/" + getURLParam(paramName) + ".xml";
}

function getElementByIdXML(node, id) {
	//get all the tags in the doc
	nodes = node.getElementsByTagName('*');
	for (i=0;i<nodes.length;i++) {
	//is there an id attribute?
		if (nodes[i].hasAttribute('id')) {
			//if there is, test its value
			if (nodes[i].getAttribute('id') == id) {
				//and return it if it matches
				return [i];
			}
		}
	}
}

function showhideresults(value){
	objectS = document.getElementById(value);
	objectS.style.display = "inline";
	if (value.indexOf('spring') > -1){
		objectH = document.getElementById(value.replace('spring', 'autumn'));
		objectH.style.display = "none";
	} else {
		objectH = document.getElementById(value.replace('autumn', 'spring'));
		objectH.style.display = "none";
	}	
}

function show(object, hideBtn, showBtn){
	elObject = document.getElementById(object);
	elHideBtn = document.getElementById(hideBtn);
	elShowBtn = document.getElementById(showBtn);	
	
	elObject.style.display = "block";
	elHideBtn.style.display = "inline";
	elShowBtn.style.display = "none";	
}

function hide(object, hideBtn, showBtn){
	elObject = document.getElementById(object);
	elHideBtn = document.getElementById(hideBtn);
	elShowBtn = document.getElementById(showBtn);
	
	elObject.style.display = "none";
	elHideBtn.style.display = "none";
	elShowBtn.style.display = "inline";
}