// Funzione per il caricamento del documento XML e passaggio alla funzione per la gestione dei risultati ottenuti
function loadXML(url, handler) {
	// Determino se l'implementazione DOM Level 2 e' supportata
	if ( document.implementation && document.implementation.createDocument ) {
		//alert("Ciao");
		// Creo il nuovo oggetto XML
		//var xml = document.implementation.createDocument("", "", null);
			var xmlhttp = new window.XMLHttpRequest();
			xmlhttp.open("GET",url,false);
			xmlhttp.send(null);
			var xmlDoc = xmlhttp.responseXML.documentElement;
			
			handler(xmlDoc);
		// Imposto l'evento onload del documento
		/*xml.onload = function() 
		{ 
			handler(xml); 
		}*/
		// Carico il file XML dall'URL passato alla funzione
		//xml.load(url);
	} else if ( window.ActiveXObject ) { // Caso contrario, ricorro all'API proprietaria Microsoft per Browser IE
		// Creo il nuovo oggetto XML
		var xml = new ActiveXObject("Microsoft.XMLDOM");
		// Imposto l'evento onload del documento
		xml.onreadystatechange = function() {
			if ( xml.readyState == 4 ) { handler(xml); }
		}
		// Carico il file XML dall'URL passatto alla funzione
		xml.load(url);
	}
}


// Funzione per la popolazione della tabella
function popolaTabella(xml) {
	// Recupero la Tabella HTML
	
	var table = document.getElementById("t1");
	
	// Recupero tutti gli elementi "utente" definiti all'interno del documento XML
	var utenti = xml.getElementsByTagName("page");
	
	// Ciclo attraverso tutti gli elementi e popolazione della tabella HTML
	var j = 1;
	for (var i = 0; i < utenti.length; i++) {
		// Recupero ogni singolo elemento "utente" del documento XML
		var e = utenti[i];
		
		// Recupero i vari valori per ogni elemento "utente"
		var numero = e.getElementsByTagName("numero")[0].firstChild.data;
		var titolo = e.getElementsByTagName("titolo")[0].firstChild.data;
		var description = e.getElementsByTagName("description")[0].firstChild.data;
		var linkdesc = e.getElementsByTagName("link")[0].firstChild.data;
		
		//alert(description);
		
		if(i == 0)
		{
			//alert(i + ":" + j);
			// Creo ogni singolo riga della tabella HTML
			// Prima riga
			var row1 = table.insertRow(i); // + 1 = Non considero la testata della tabella
				var cell_1 = row1.insertCell(0);
				cell_1.innerHTML = "<font color='#ff0000' face='Arial, Helvetica, sans-serif' style='font-size: 20px;'><b>" + numero + "</b></font>";
				cell_1.style.backgroundColor = "white";
				cell_1.style.width = "20px";
				
				var cell_2 = row1.insertCell(1);
				cell_2.innerHTML = "<font color='#ff0000' face='Arial, Helvetica, sans-serif' style='font-size: 20px;'><b>" + titolo + "</b></font>";
				cell_2.style.backgroundColor = "#FFFF00";
				
			// Seconda riga
			var row2 = table.insertRow(i+1); // + 1 = Non considero la testata della tabella
				var cell2_1 = row2.insertCell(0);
				cell2_1.style.backgroundColor = "white";
				var cell2_2 = row2.insertCell(1);
				cell2_2.innerHTML = "<a href='" + linkdesc + "' style='font-family: Arial; font-zie: 16px; color:#000000'><b>" + description + "</b></a>";
				cell2_2.style.backgroundColor = "#FFFF00";
				
			// Terza riga
			var row3 = table.insertRow(i+2); // + 1 = Non considero la testata della tabella
				var cell3_1 = row3.insertCell(0);
				cell3_1.innerHTML = "<br>";
				var cell3_2 = row3.insertCell(1);
				cell3_1.style.backgroundColor = "white";
				cell3_2.style.backgroundColor = "white";
		}
		else
		{
			
			//alert((i*3) + ":" + ((j * 3) - 1));			
			// Creo ogni singolo riga della tabella HTML
			var row1 = table.insertRow(i*3); // + 1 = Non considero la testata della tabella
				var cell_1 = row1.insertCell(0);
				cell_1.innerHTML = "<font color='#ff0000' face='Arial, Helvetica, sans-serif' style='font-size: 20px;'><b>" + numero + "</b></font>";
				cell_1.style.backgroundColor = "white";
				
				var cell_2 = row1.insertCell(1);
				cell_2.innerHTML = "<font color='#ff0000' face='Arial, Helvetica, sans-serif' style='font-size: 20px;'><b>" + titolo + "</b></font>";
				cell_2.style.backgroundColor = "#FFFF00";
				
			// Seconda riga
			var row2 = table.insertRow((i*3) + 1); // + 1 = Non considero la testata della tabella
				var cell2_1 = row2.insertCell(0);
				cell2_1.style.backgroundColor = "white";
				var cell2_2 = row2.insertCell(1);
				cell2_2.innerHTML = "<a href='" + linkdesc + "' style='font-family: Arial; font-zie: 16px; color:#000000'><b>" + description + "</b></a>";
				cell2_2.style.backgroundColor = "#FFFF00";
					
			// Terza riga
			var row3 = table.insertRow((i*3) + 2); // + 1 = Non considero la testata della tabella
				var cell3_1 = row3.insertCell(0);
				cell3_1.innerHTML = "<br>";
				var cell3_2 = row3.insertCell(1);
				cell3_1.style.backgroundColor = "white";
				cell3_2.style.backgroundColor = "white";
		}
	}
}