var servUrl = "http:\/\/storing.ingv.it\/cfti4med\/";
var last, least;

// Mostra il box a destra, relativo a cio' che l'utente ha cliccato nelle tabelle
function showBox(obj)
{
	try
	{
		last.firstChild.style.display = 'none';
		last.style.color = 'black';
		if((last.getAttribute('class') == 'biblioSel') || (last.getAttribute('class') == 'locSel'))
		{
			last.firstChild.nextSibling.style.backgroundColor = '';
			last.firstChild.nextSibling.nextSibling.style.backgroundColor = '';
		}
		else last.style.backgroundColor = '';
		last.setAttribute('onmouseover', 'this.style.color="#256D1D"');
		last.setAttribute('onmouseout', 'this.style.color="black"');
	}
	catch(e){}

	obj.style.color = '#3643FF';
	if((obj.getAttribute('class') == 'biblioSel') || (obj.getAttribute('class') == 'locSel'))
	{
		obj.firstChild.nextSibling.style.backgroundColor = 'rgb(251, 253, 207)';
		obj.firstChild.nextSibling.nextSibling.style.backgroundColor = 'rgb(251, 253, 207)';
	}
	else obj.style.backgroundColor = 'rgb(251, 253, 207)';
	obj.removeAttribute('onmouseover');
	obj.removeAttribute('onmouseout');
	obj.firstChild.style.display = 'block';
	
	if(document.all)
	{
		try
		{
			try
			{
				$('contenuto').removeChild(least);
			}
			catch(e){}
			
			objMio = obj.firstChild.cloneNode(true);
			$('contenuto').appendChild(objMio);
			least = objMio;
			
			obj.firstChild.style.display = "none";
		}
		catch(e)
		{
			if(!window.XMLHttpRequest)
				obj.firstChild.style.width = "100%";
			else
				obj.firstChild.style.width = '450px';
		}
	}

	last = obj;
}

/* Quando accedo al terremoto per localita', in testa ai commenti ho quello relativo alla localita' selezionata.
   Questa funzione chiede al server di inviargli il commento relativo quando l'utente fa clic. */
function getLocComm(obj, nloc, nperiod)
{
	/* Faccio una richiesta AJAX allo script server-side che fa le query al database per le nloc */
	var url = servUrl + "info/findLocComm.php?nloc=" + nloc + "&nperiod=" + nperiod;
	
	httpRequest = assignXMLHttpRequest();
	httpRequest.open("GET", url, true);
	httpRequest.setRequestHeader('Content-Type', "text/xml");
	httpRequest.onreadystatechange = function(){ process(obj); };
	httpRequest.send(null);
}

function process(obj)
{
	if(httpRequest.readyState == 4)
	{
		if(httpRequest.status == 200)
		{
			// con lo slice() elimino tutto quello che c'e' prima del '<', in modo da parsare un file xml valido
			if(window.ActiveXObject)
			{
				xmlDocDOM = new ActiveXObject("Microsoft.XMLDOM");
				xmlDocDOM.async = "false";
				xmlDocDOM.loadXML(httpRequest.responseText.slice(httpRequest.responseText.indexOf('<')));
			}
			else
				xmlDocDOM = (new DOMParser()).parseFromString(
      						httpRequest.responseText.slice(httpRequest.responseText.indexOf('<')), "text/xml");
			try
			{
				obj.firstChild.removeChild(obj.firstChild.firstChild);
			}
			catch(e){}
			
			obj.firstChild.innerHTML = httpRequest.responseText.slice(httpRequest.responseText.indexOf('<'));
			showBox(obj);
		}
	}
}

function ajaxSort(type, sort, nloc, nperiod, nterr)
{
	var url = servUrl + "info/sortList.php?type=" + type + "&sort=" + sort + "&nloc=" + nloc + "&nperiod=" + nperiod + "&nterr=" + nterr;
	var obj;
	
	if(type == 'npq')
		obj = document.getElementById('npq');
	else if(type == 'npqSeq')
		obj = document.getElementById('npqSeq');
	else return;
	
	obj.innerHTML = '<img src="info/ajax-loader.gif" alt="Sorting..." style="margin-left:170px;margin-top:30px;"/>';
	
	httpRequest = assignXMLHttpRequest();
	httpRequest.open("GET", url, true);
	httpRequest.setRequestHeader('Content-Type', "text/xml");
	httpRequest.onreadystatechange = function(){ procesSort(obj); };
	httpRequest.send(null);
}

function procesSort(obj)
{
	if(httpRequest.readyState == 4)
	{
		if(httpRequest.status == 200)
		{
			// con lo slice() elimino tutto quello che c'e' prima del '<', in modo da parsare un file xml valido
			//xmlDocDOM = (new DOMParser()).parseFromString(httpRequest.responseText.slice(httpRequest.responseText.indexOf('<')), "text/xml");
			
			obj.innerHTML = httpRequest.responseText;
		}
	}
}

// funzione per assegnare l'oggetto XMLHttpRequest
// compatibile con i browsers piu' recenti e diffusi
function assignXMLHttpRequest()
{
	// variabile che sara' ritornata
	var XHR = null;
 
	// informazioni sul nome del browser
	browserUtente = navigator.userAgent.toUpperCase();

	// browser standard con supporto nativo
	// non importa il tipo di browser
	if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
		XHR = new XMLHttpRequest();
	// browser Internet Explorer
	// e' necessario filtrare la versione 4
	else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0)
	{
		// la versione 6 di IE ha un nome differente
		// per il tipo di oggetto ActiveX
		if(browserUtente.indexOf("MSIE 5") < 0)
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		// le versioni 5 e 5.5 invece sfruttano lo stesso nome
		else
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return XHR;
}
