//INIZIO funzioni per gestione select dei POI
function ClearOptions(idOptionList) {
  // Always clear an option list from the last entry to the first
	var OptionList = document.getElementById(idOptionList);
  for (x = OptionList.length; x >= 0; x--) {
		OptionList[x] = null;
  }
}

function AddToOptionList(idOptionList, OptionValue, OptionText) {
  // Add option to the bottom of the list
	var OptionList = document.getElementById(idOptionList);
  OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}
//FINE funzioni per gestione select dei POI




var arrText = [];
var arrValue = [];

//Effettua il parsing del risultato restituito dalla chiamata a getMonuments.asp in caso di esito positivo
function parseString(str) {
	//for (i=0; i<str.length; i++) {
		var arrTemp = [];
		arrTemp = str.split('#');
		arrText = arrTemp[0].split(',');
		arrValue = arrTemp[1].split(',');
  //} 
}

// Create an instance of the XML HTTP Request object
var oXMLHTTP; //= new ActiveXObject("Microsoft.XMLHTTP");

// XMLHttpRequest native object (Mozilla)
if (window.XMLHttpRequest) {
	oXMLHTTP = new XMLHttpRequest();
}
// ActiveX version (IE/Windows)
else if (window.ActiveXObject) {
	oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
}

//INIZIO funzioni per gestione popolamento POI
function getMonuments(idCity, idType) {
	// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
	var sURL = " http://www.best-bookings.com/v13/getMonuments.asp?idC="+ idCity +"&idT="+ idType;
	oXMLHTTP.open("GET", sURL, true);

	// Define an event handler for processing
	oXMLHTTP.onreadystatechange = managestatechange;
		
	// Execute the request
	try {
		oXMLHTTP.send(null);
	}
	catch (e) {
		alert("Could not retrieve monuments at this time. ("+e.message+")");
		//document.all.item("FirstName").focus;
	}
}

/*
Legenda per gli stati della xmlHttpRequest:
0 (UNINITIALIZED)	The object has been created, but not initialized (open method has not been called).
(1) LOADING	The object has been created, but the send method has not been called.
(2) LOADED	The send method has been called and the status and headers are available, but the response is not yet available.
(3) INTERACTIVE	Some data has been received. You can call responseBody and responseText to get the current partial results.
(4) COMPLETED	All the data has been received, and the complete data is available in responseBody and responseText.
*/
function managestatechange() {
	switch (oXMLHTTP.readyState) {
	case 2, 3:
		// Display a progress indicator of some kind, informing the
		// user that you are processing the request
		//document.getElementById("divProgress").style.display = "block";
	break;
	case 4:
		if (oXMLHTTP.status == 200 || oXMLHTTP.status == 0) {
			if (oXMLHTTP.responseText == "-") {
				//alert("No monuments found");
				document.getElementById('POICoord').value = '';
				document.getElementById('POIName').value = '';
				ClearOptions('pointInterest');
				AddToOptionList('pointInterest', '', 'No point found');
			}
			else {
				if (oXMLHTTP.responseText == "EX") {
					alert("Wrong parameters into the request");
				}
				else {
					var mList = oXMLHTTP.responseText;
					//formato mList: [nomePOI_1,...,nomePOI_n]#[idPOI_1,...,idPOI_n]@<lati_1>#<longi_1>
					var arrTmp = [];
					arrTmp = mList.split('@');
					document.getElementById('POICoord').value = arrTmp[1];
					parseString(arrTmp[0]);
					document.getElementById('POIName').value = arrText[0];
					ClearOptions('pointInterest');
					for (x = 0; x < arrValue.length; x++) {				
						AddToOptionList('pointInterest', arrValue[x], arrText[x]);
					}
				}
			}
		}
		else
			alert("Server comunication failed (cod: "+oXMLHTTP.status+" msg: "+oXMLHTTP.statusText+")");
			
		//document.getElementById("divProgress").style.display = "none";
	break;
	}	
}
//FINE gestione funzioni per popolamento POI

//INIZIO gestione funzioni per recupero coordinate di un POI
function getMonumentCoord(idM) {
	// Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
	var sURL = " http://www.best-bookings.com/v13/getPoiCoord.asp?id="+ idM;
	oXMLHTTP.open("GET", sURL, true);

	// Define an event handler for processing
	oXMLHTTP.onreadystatechange = managestatechange_coord;
		
	// Execute the request
	try {
		oXMLHTTP.send(null);
	}
	catch (e) {
		alert("Could not retrieve monument value at this time. ("+e.message+")");
		//document.all.item("FirstName").focus;
	}
}

function managestatechange_coord() {
	switch (oXMLHTTP.readyState) {
	case 2, 3:
		// Display a progress indicator of some kind, informing the
		// user that you are processing the request
		//document.getElementById("divProgress").style.display = "block";
	break;
	case 4:
		if (oXMLHTTP.status == 200 || oXMLHTTP.status == 0) {
			if (oXMLHTTP.responseText == "-") {
				//alert("No monuments found");
				document.getElementById('POICoord').value = "";
			}
			else {
				if (oXMLHTTP.responseText == "EX") {
					alert("Wrong parameters into the request");
				}
				else {
					var mList = oXMLHTTP.responseText;
					document.getElementById('POICoord').value = mList;
				}
			}
		}
		else
			alert("Server comunication failed (cod: "+oXMLHTTP.status+" msg: "+oXMLHTTP.statusText+")");
			
		//document.getElementById("divProgress").style.display = "none";
	break;
	}	
}
//FINE gestione funzioni per recupero coordinate di un POI


// Hide the progress indicator for now
//document.getElementById("divProgress").style.display = "none";


