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

function newMWindow(wTitle, wWidth, wHeight){
	wWidth=Number(wWidth);
	wHeight=Number(wHeight);
	if(wWidth==0)  {wWidth=300;}
	if(wHeight==0) {wHeight=150;}
	
	var timestamp = new Date();
	var tmpName = 'WH' + timestamp.getTime();
	
	var win = new Window({id: tmpName, className: "alphacube", title: wTitle, width: wWidth, height: wHeight}); 
	win.getContent().innerHTML = '<div id="' + tmpName + '_results" style="display:none;text-align:left;" align="left"></div><div id="' + tmpName + '_wait" style="display:none;margin-top:50px;" align="center"><h3>...caricamento...</h3><div><img src="/admin/_images/msc_loader_big.gif" alt="...caricamento..." width="35" height="35"><img src="/admin/_images/msc_loader_big.gif" alt="...caricamento..." width="35" height="35"><img src="/admin/_images/msc_loader_big.gif" alt="...caricamento..." width="35" height="35"></div></div>';
	win.setDestroyOnClose(); 
	win.showCenter(true);
	win.toFront();

	return tmpName;
}

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);}
					}
		}
	);
}
/*
function InviaMessaggio(IDMessaggio){
	IDMessaggio =Number(IDMessaggio);
	var strAzione ='Invia';
	var tmpName   = newMWindow(strAzione + ' messaggio', 420, 420);
	var timestamp = new Date(); //EVITA LA CACHE DEL FILE ;)
	new Ajax.Updater(tmpName + '_results', 'ajax.php?' + timestamp.getTime(), {
			asynchronous: true,
			method      : 'get',
			evalScripts : true,
			parameters  : {   Action: 'Open' 
							, IDMessaggio : IDMessaggio
							, WID: tmpName
						},
			onLoading   : function(rq){$(tmpName + '_results').hide();$(tmpName + '_wait').show();},
			onComplete  : function(rq){$(tmpName + '_wait').hide();$(tmpName + '_results').show();},
			onFailure   : function(rq){alert('Errore ' + rq.status + ' -- ' + rq.statusText);}
		}
	);
}

function newMWindow(wTitle, wWidth, wHeight){
	wWidth=Number(wWidth);
	wHeight=Number(wHeight);
	if(wWidth==0)  {wWidth=300;}
	if(wHeight==0) {wHeight=150;}
	
	var timestamp = new Date();
	var tmpName = 'WH' + timestamp.getTime();
	
	var win = new Window({id: tmpName, className: "alphacube", title: wTitle, width: wWidth, height: wHeight}); 
	win.getContent().innerHTML = '<div id="' + tmpName + '_results" style="display:none;text-align:left;" align="left"></div><div id="' + tmpName + '_wait" style="display:none;margin-top:50px;" align="center"><h3>...caricamento...</h3><div><img src="/admin/_images/msc_loader_big.gif" alt="...caricamento..." width="35" height="35"><img src="/admin/_images/msc_loader_big.gif" alt="...caricamento..." width="35" height="35"><img src="/admin/_images/msc_loader_big.gif" alt="...caricamento..." width="35" height="35"></div></div>';
	win.setDestroyOnClose(); 
	win.showCenter(true);
	win.toFront();

	return tmpName;
} */
