//chiamata asincrona al server
function makeRequest(url, param){
	var httpRequest;
	var theObject;
	
	if (window.XMLHttpRequest)
	{ // Mozilla e altri browser
		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)
	{
		alert("Si e' verificato un errore temporaneo con il servizio");
	}
	httpRequest.onreadystatechange = function() {  
	  if (httpRequest.readyState == 4) {
	   if ( httpRequest.status == 200 ) {
			//elaborazione della risposta JSON
			var jsonText = httpRequest.responseText;
			jsonText = jsonText.replace("[", "");
			jsonText = jsonText.replace("]", "");
			theObject = eval( '(' + jsonText + ')' );
			switch(param) {	
				case "p_ba":
					var exp_p_ba = document.getElementById("exp_p_ba"); 
					exp_p_ba.innerHTML += theObject["Exp"];
					var sched_p_ba = document.getElementById("sched_p_ba"); 
					sched_p_ba.innerHTML += theObject["Flight"];
					var dest_p_ba = document.getElementById("dest_p_ba"); 
					dest_p_ba.innerHTML += theObject["Dest_ITA"];
					break;
				case "p_br":
					var exp_p_br = document.getElementById("exp_p_br"); 
					exp_p_br.innerHTML += theObject["Exp"];
					var sched_p_br = document.getElementById("sched_p_br"); 
					sched_p_br.innerHTML += theObject["Flight"];
					var dest_p_br = document.getElementById("dest_p_br"); 
					dest_p_br.innerHTML += theObject["Dest_ITA"];
					break;
				case "a_ba":
					var exp_a_ba = document.getElementById("exp_a_ba"); 
					exp_a_ba.innerHTML += theObject["Exp"];
					var sched_a_ba = document.getElementById("sched_a_ba"); 
					sched_a_ba.innerHTML += theObject["Flight"];
					var orig_a_ba = document.getElementById("orig_a_ba"); 
					orig_a_ba.innerHTML += theObject["From_ITA"];
					break;
				case "a_br":
					var exp_a_br = document.getElementById("exp_a_br"); 
					exp_a_br.innerHTML += theObject["Exp"];
					var sched_a_br = document.getElementById("sched_a_br"); 
					sched_a_br.innerHTML += theObject["Flight"];
					var orig_a_br = document.getElementById("orig_a_br"); 
					orig_a_br.innerHTML += theObject["From_ITA"];
					break;
				default:
					alert("Parametro non valido");
			}
		} else {
			alert("Si e' verificato un errore temporaneo con il servizio");
	   }
	  }
	};
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}

//chiamate asincrone
function showData() {
	var jsonUrl = 'http://www.aeroportidipuglia.it/gazzetta/adpService.asp?flightType=p_ba'
	makeRequest(jsonUrl, "p_ba");
	var jsonUrl = 'http://www.aeroportidipuglia.it/gazzetta/adpService.asp?flightType=p_br'
	makeRequest(jsonUrl, "p_br");
	var jsonUrl = 'http://www.aeroportidipuglia.it/gazzetta/adpService.asp?flightType=a_ba'
	makeRequest(jsonUrl, "a_ba");
	var jsonUrl = 'http://www.aeroportidipuglia.it/gazzetta/adpService.asp?flightType=a_br'
	makeRequest(jsonUrl, "a_br");
}

