var xmlhttp;

// Set patchlink text if not set
if(!patchlink_text) {
  var patchlink_text = "Patch downloaden";
}

function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Mozilla, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5, IE6
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=loadTable;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }
}


function loadTable()
{

	if(xmlhttp.readyState!=4) return;
	if(xmlhttp.status!=200)
	{
	  //alert("Problem retrieving XML data");	
	  //alert(xmlhttp.status); 
	  return;
	}
	
	var x = xmlhttp.responseXML.documentElement.getElementsByTagName("game");
	
	var gamestbl = document.createElement("TABLE");
	gamestbl.setAttribute("cellpadding", 0);
	gamestbl.setAttribute("cellspacing", 0);
	gamestbl.setAttribute("width", "100%");
	
	var tbody = document.createElement("TBODY");
	gamestbl.appendChild(tbody);
						
	// Display two rows of 7 columns (4 new, 3 coming soon)

  // Add images row -----------------------------------------------
	var row = document.createElement("TR");					
			
	for(var i=0;i<7;i++) {
				
		var col = document.createElement("TD");
		col.setAttribute("width", 101);
				
		if(i < x.length) {
										
					try {
						// Checked if the record exists			
						var img = document.createElement("IMG");
						img.setAttribute("src", "../../games/images/"+x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue);												
				
						if(x[i].getElementsByTagName("link")[0].childNodes[0]) 
						{		
							// Link behind the image
							var link = document.createElement("A");
							link.setAttribute("href", x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue);				
							link.appendChild(img);				
							col.appendChild(link);
						}
					  else 
						{
							// No link behind the image
							col.appendChild(img);
						}											
					}
					catch(ex){
						 // Do nothing, to catch empty tags
					}
		}
			  
		// add cols and rows to table
		row.appendChild(col);
		tbody.appendChild(row);
	}
		  
	// Add patch link row -----------------------------------------------
	var row = document.createElement("TR");		
			
	for(var i=0;i<7;i++) {
										
		var col = document.createElement("TD");	
				
		if(i < x.length) { 
				  // Checked if the record exists							
					try {										
						if(x[i].getElementsByTagName("patch")[0].childNodes[0].nodeValue) {	
							// Link to the patch download page
							var link = document.createElement("A"); 
							link.setAttribute("href", x[i].getElementsByTagName("patch")[0].childNodes[0].nodeValue);	
							link.appendChild(document.createTextNode(patchlink_text));				
							col.appendChild(link); 
						}														
					}
					catch(ex){
						 // Do nothing, to catch empty tag
					}
		}
				
		// Add rows and cols to table
		row.appendChild(col);
		tbody.appendChild(row);
	} 
		
		// Add to games table
		document.getElementById("gamestable").appendChild(gamestbl);		
		
		// Fix alignment
		var tbody = document.getElementById("gamestable").parentNode.parentNode.parentNode;			
		tbody.getElementsByTagName("TD")[0].setAttribute("width", "404px");
		tbody.getElementsByTagName("TD")[1].setAttribute("width", "303px");

}
