//Variabile globale (TIPOTARGET definito in post_form.js)
function makeRequest(url, target, ico_loading, tipoTarget, codTarget) {
	
	if(ico_loading){
		html='<IMG src="'+'images/loading.gif" width="19" height="19">';
		document.getElementById(target).innerHTML = html
	} else {
		html='';
	}
	
	//Gestisce eventuali azioni dopo l'esecuzione dell'ajax
	if (tipoTarget!=null) {
		TIPOTARGET = tipoTarget
		CODTARGET = codTarget
	}	
			
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	if (url.indexOf("?")<0) {
		url=url+'?1=1'
	}	
		
	if (!document.getElementById(target)) {
		makeRequest('debug/ajax_error.asp?ajax_error='+target+' non trovato','DIVDebug');
	} else {
		//alert(url)	
		http_request.onreadystatechange = function() { Action(http_request, target); };
		http_request.open('GET', url+'&z='+new Date().getTime(), true);
		http_request.send(null);
	}
}

function Action(http_request, target) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {					
			
			document.getElementById(target).innerHTML = http_request.responseText;
					
			
			//AZIONI DOPO ESECUZIONE AJAX
			if (TIPOTARGET=='aggiorna_carello') {
				makeRequest('include/BIO/ajax/carrello_menu.asp', 'LeftCarrello', true, 'carello')				
				TIPOTARGET=null;			
			}
			
			//CANCELLAZIONE BENI DISMESSI
			if (TIPOTARGET=='delete_bene_dismesso') {
				FiltraBeniDismessi('frm_BIO_bene_dismesso');
				TIPOTARGET=null;							
			}
						
			//Modifica posizione nel sito "Ti trovi in:..."
			if (TIPOTARGET=='elenco_prodotti' || TIPOTARGET=='elenco_ordini_IT') {
				document.getElementById('PosizioneTesto').innerHTML='Beni informatici';
			}		
			
			if (TIPOTARGET=='elenco_richieste_BIO') {
				document.getElementById('PosizioneTesto').innerHTML='Beni biomedici';
			}	
			
			if (TIPOTARGET=='recupero_password') {
				document.getElementById('PosizioneTesto').innerHTML='Procedura di recupero password';
			}			
			
			if (TIPOTARGET=='feedback_richiesta') {
				loadmap('map');
				TIPOTARGET=null;
			}
			
			//pagina per conferma carrello IT
			if (TIPOTARGET=='elenco_prodotti') {
				if (document.getElementById('map')) {
					loadmap('map');
				}
				TIPOTARGET=null;
			}
						
																																																									
			
		} else {
			//alert("Errore AJAX");
			//alert(http_request.responseText);
			document.getElementById(target).innerHTML=http_request.responseText;
			
			if (document.getElementById('DIVContenuto')) {
				document.getElementById('DIVContenuto').innerHTML=http_request.responseText;		    	
			}								
			
			//Scrive su file di log (gestisce due tipologie di messaggio)
			/*
			if (http_request.responseText.indexOf('Tipo di errore:')>-1 && http_request.responseText.indexOf('Tipo di browser:')>-1) {
				
				var lunghezza = http_request.responseText.indexOf('Tipo di browser:') - http_request.responseText.indexOf('Tipo di errore:')
				var logtext = http_request.responseText.substr(http_request.responseText.indexOf('Tipo di errore:'), lunghezza);
								
			} else {			
				logtext = http_request.responseText;
			}
			*/
			//max 2083 caratteri URL)			
			/*
			if (logtext.length>1900) {				
				logtext = Left(logtext,1900);				
			}
			*/
				
			//scrive errore su file di log
			//makeRequest('debug/ajax_error.asp?ajax_error='+Left(logtext,1000),'DIVDebug');		
		}
	}
}
