function GetInnerText(node){
	return (node.textContent || node.innerText || node.text) ;
}

function getProvince(Container, ID){
	
	var ddl = $(Container);
	var timestamp = new Date(); //EVITA LA CACHE DEL FILE ;)
	new Ajax.Request('/ajax.php', 
		{
		asynchronous: true,
		method      : "get",
		parameters  : "action=Province&IDRegione=" + ID + "&" + timestamp.getTime(),
		onLoading   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('...caricamento...','',false,false);},
		onFailure   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('--','',false,false);alert('Errore ' + rq.status + ' -- ' + rq.statusText);},
		onSuccess   : function(rq){
						ddl.options.length=0;
						ddl.options[ddl.options.length]=new Option("--", "",  false, false);
						var XMLDoc    = rq.responseXML.documentElement;
						var XMLResult = XMLDoc.getElementsByTagName('result');
						for (var i=0; i<XMLResult.length; i++){ddl.options[ddl.length]=new Option(GetInnerText(XMLResult[i]), XMLResult[i].getAttribute("id"),  false, false);}
					}
		}
	);
}

function getComuni(Container, ID){
	var ddl = $(Container);
	var timestamp = new Date(); //EVITA LA CACHE DEL FILE ;)
	new Ajax.Request('/ajax.php', 
		{
		asynchronous: true,
		method      : "get",
		parameters  : "action=Comuni&IDProvincia=" + ID + "&" + timestamp.getTime(),
		onLoading   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('...caricamento...','',false,false);},
		onFailure   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('--','',false,false);alert('Errore ' + rq.status + ' -- ' + rq.statusText);},
		onSuccess   : function(rq){
						ddl.options.length=0;
						ddl.options[ddl.options.length]=new Option("--", "",  false, false);
						var XMLDoc    = rq.responseXML.documentElement;
						var XMLResult = XMLDoc.getElementsByTagName('result');
						for (var i=0; i<XMLResult.length; i++){ddl.options[ddl.length]=new Option(GetInnerText(XMLResult[i]), XMLResult[i].getAttribute("id"),  false, false);}
					}
		}
	);
}

function getCap(Container, ID){
	var ddl = $(Container);
	var timestamp = new Date(); //EVITA LA CACHE DEL FILE ;)
	new Ajax.Request('/ajax.php', 
		{
		asynchronous: true,
		method      : "get",
		parameters  : "action=Cap&IDComune=" + ID + "&" + timestamp.getTime(),
		onLoading   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('...caricamento...','',false,false);},
		onFailure   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('--'               ,'',false,false);alert('Errore ' + rq.status + ' -- ' + rq.statusText);},
		onSuccess   : function(rq){
						ddl.options.length=0;
						var XMLDoc    = rq.responseXML.documentElement;
						var XMLResult = XMLDoc.getElementsByTagName('result');
						if (XMLResult.length==0){ddl.options[ddl.options.length]=new Option("--", "",  false, false);}
						for (var i=0; i<XMLResult.length; i++){ddl.options[ddl.length]=new Option(GetInnerText(XMLResult[i]), XMLResult[i].getAttribute("id"),  false, false);}
					}
		}
	);
}

function getRComuni(Container, ID, Categoria){
	var ddl = $(Container);
	var timestamp = new Date(); //EVITA LA CACHE DEL FILE ;)
	var Categoria = 0;
	
	new Ajax.Request('/ajax.php', 
		{
		asynchronous: true,
		method      : "get",
		parameters  : "action=RComuni&IDProvincia=" + ID + "&Categoria" + Categoria + "&" + timestamp.getTime(),
		onLoading   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('...caricamento...','',false,false);},
		onFailure   : function(rq){ddl.options.length=0;ddl.options[ddl.length]=new Option('--','',false,false);alert('Errore ' + rq.status + ' -- ' + rq.statusText);},
		onSuccess   : function(rq){
						ddl.options.length=0;
						ddl.options[ddl.options.length]=new Option("--", "",  false, false);
						var XMLDoc    = rq.responseXML.documentElement;
						var XMLResult = XMLDoc.getElementsByTagName('result');
						for (var i=0; i<XMLResult.length; i++){ddl.options[ddl.length]=new Option(GetInnerText(XMLResult[i]), XMLResult[i].getAttribute("id"),  false, false);}
					}
		}
	);
}

