// Provide the XMLHttpRequest class for IE 5.x-6.x:
// Other browsers (including IE 7.x-8.x) ignore this
//   when XMLHttpRequest is predefined
if (typeof(XMLHttpRequest) == "undefined") {
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch(e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }
      catch(e) {}
    throw new Error("This browser does not support XMLHttpRequest.");
  };
}

ildiv = '';
targetSelect = '';

function handler() {
	if(this.readyState == 4 && this.status == 200) {
		if(this.responseXML)
		{
			xml_ = this.responseXML;
						
			document.getElementById(ildiv).options.length=0;
			
			for(o=0;o<xml_.getElementsByTagName('codice').length;o++)
			{
				codice = xml_.getElementsByTagName('codice')[o].childNodes[0].nodeValue;
				nome = xml_.getElementsByTagName('nome')[o].childNodes[0].nodeValue;
				document.getElementById(ildiv).options[o] = new Option(nome,codice);
			}
		}
	}
}

function handler2() {
	if(this.readyState == 4 && this.status == 200) {
		if(this.responseXML)
		{
			xml_ = this.responseXML;
			
			o=0;
			
			codice = xml_.getElementsByTagName('codice')[o].childNodes[0].nodeValue;
			nome = xml_.getElementsByTagName('nome')[o].childNodes[0].nodeValue;
			lat = xml_.getElementsByTagName('lat')[o].childNodes[0].nodeValue;
			lon = xml_.getElementsByTagName('lon')[o].childNodes[0].nodeValue;
			id_settore = xml_.getElementsByTagName('id_settore')[o].childNodes[0].nodeValue;
			regione = xml_.getElementsByTagName('id_regione')[o].childNodes[0].nodeValue;
			
			document.getElementById('lat').value=lat;
			document.getElementById('lon').value=lon;
			
			for (f=0;f<document.getElementById('settore_porto').options.length;f++)
			{
				if(id_settore == document.getElementById('settore_porto').options[f].value)
				{
					document.getElementById('settore_porto').options[f].selected=true;
				}
				else {
					document.getElementById('settore_porto').options[f].selected=false;					
				}
			}
		}
	}
}

function precomp_porti(idregione,divora)
{
	ildiv=divora;
	
	call_prog = 'get_porti.php?idregione='+escape(idregione);
	
	var client = new XMLHttpRequest();
	client.onreadystatechange = handler;
	client.open("GET", call_prog);
	client.send(null);
}

function compila_campi(idporto)
{
	call_prog = 'get_porti.php?idporto='+escape(idporto);
	
	var client = new XMLHttpRequest();
	client.onreadystatechange = handler2;
	client.open("GET", call_prog);
	client.send(null);
}

function updateSelect(selectObj, parameter, lat, lon, settoreSel)
{
    if (settoreSel != '' && settoreSel != null)
    {
        ajaxRequest("phpLibrary/getSettori.php?settoreSel="+settoreSel+"&lat="+lat+"&lon="+lon, "div_settore_porto");
    }
    else
    {
        ajaxRequest("phpLibrary/getSettori.php?lat="+lat+"&lon="+lon, "div_settore_porto");
    }
}

function ajax_page_request(pageName, changeStateMethod, parameter, value)
{	
	var call_prog = pageName;
	
	//Build the url
	call_prog = call_prog + "?" + parameter + "=" + escape(value);
	
	//var client;// = new XMLHttpRequest();

        if (window.XMLHttpRequest){

          // If IE7, Mozilla, Safari, etc: Use native object
          var client = new XMLHttpRequest()

        }
        else
        {
            if (window.ActiveXObject){

            // ...otherwise, use the ActiveX control for IE5.x and IE6
            var client = new ActiveXObject("Microsoft.XMLHTTP");
            }

        }


	client.onreadystatechange = updateSelectHandler;
	client.open("GET", call_prog);
	client.send(null);
}

//True if provided obj is an Array, otherwise false
function isArray(obj) {
	   if (obj.constructor.toString().indexOf("Array") == -1)
	      return false;
	   else
	      return true;
}

function updateSelectHandler()
{
	if(this.readyState == 4 && this.status == 200) {
		
		if(this.responseXML)
		{
			var xml_ = this.responseXML;
						
			document.getElementById(targetSelect).options.length=0;
			
			for(o=0;o<xml_.getElementsByTagName('settore').length;o++)
			{
				if (xml_.getElementsByTagName('codice')[o].childNodes.length > 0)
				{
					var codice = xml_.getElementsByTagName('codice')[o].childNodes[0].nodeValue;
					
					var nome = codice;
					
					while (nome.length != 4)
					{
						nome = nome + " ";
					}
					nome = nome + xml_.getElementsByTagName('nome')[o].childNodes[0].nodeValue;
					document.getElementById(targetSelect).options[o] = new Option(nome,codice);
				}
				else
				{
					var nome = xml_.getElementsByTagName('nome')[o].childNodes[0].nodeValue;
					document.getElementById(targetSelect).options[o] = new Option(nome,"0");
				}
			}
		}
	}
}
