var Browser;
var xml;

if ( document.implementation && document.implementation.createDocument ) {
	Browser = "Mo";
}else if(window.XMLHttpRequest){
	Browser = "IE";
}else if ( window.ActiveXObject ){
	Browser = "IE";
}else{
	alert('xml function not available');
}


// Funzione per il caricamento del documento XML
function loadXML(url){

	if ( Browser == "Mo") {
		// Creo il nuovo oggetto XML
		xml = document.implementation.createDocument("", "", null);

	} else if ( Browser == "IE" ) { 
		// Creo il nuovo oggetto XML
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.onreadystatechange = function() {	if ( xml.readyState == 4 ) { handler(xml); }}
		xml.async = false;

	} else if ( Browser == "IE7" ) { 
		// Creo il nuovo oggetto XML
		xml = new XMLHttpRequest();

		//xml.async = false;
	}


	xml.load(url);
}


//--- Mozilla e affini ----------------------------------------------------------------------------------------
//Recupera qualsiasi valore con qualsiasi tipo di chiave
function RecuperaMo(Lingua, TipoNodo, TipoNodoValore, ChiaveNodo){
	var LinguaScelta = xml.getElementsByTagName(Lingua);
	var NodoScelto = LinguaScelta[0].getElementsByTagName(TipoNodo);
	var NumeroElementi = NodoScelto[0].getElementsByTagName(TipoNodoValore).length;
	var Valore;
	var Chiave;
	
	for (i = 0; i<NumeroElementi; i++) {
		Valore = NodoScelto[0].getElementsByTagName(TipoNodoValore)[i].firstChild.data;
		Chiave = NodoScelto[0].getElementsByTagName(TipoNodoValore)[i].getAttribute('Key');

		if (ChiaveNodo == Chiave){
			return Valore;
		}
		 
	}
    return "NO DEFINED";
    
}


//Recupera qualsiasi valore da qualsiasi nodo dando il numero del nodo
function SelezionaMoNumeroNodo(Lingua, TipoNodo, TipoNodoValore, NumeroNodo){
	var LinguaScelta = xml.getElementsByTagName(Lingua);
	var NodoScelto = LinguaScelta[0].getElementsByTagName(TipoNodo);
    return NodoScelto[0].getElementsByTagName(TipoNodoValore)[NumeroNodo].firstChild.data;
}
//Recupera qualsiasi valore da qualsiasi nodo dando il numero del nodo
function SelezionaValoreMo2(Lingua, TipoNodo, TipoNodoValore, NumeroNodo){
	var LinguaScelta = xml.getElementsByTagName(Lingua);
	var NodoScelto = LinguaScelta[0].getElementsByTagName(TipoNodo);
    return NodoScelto[0].getElementsByTagName(TipoNodoValore)[NumeroNodo].firstChild.data;
}


//Recupera qualsiasi valore del nodo VALUE dando il numero del nodo
function SelezionaValoreMo(Lingua, TipoNodo, NumeroNodo){
	var LinguaScelta = xml.getElementsByTagName(Lingua);
	var NodoScelto = LinguaScelta[0].getElementsByTagName(TipoNodo);
    return NodoScelto[0].getElementsByTagName('Value')[NumeroNodo].firstChild.data;
}

//Recupera solo le intestazioni
function RecuperaValoreMo(Lingua, Nodo){
	var LinguaScelta = xml.getElementsByTagName(Lingua);
	var NodoScelto = LinguaScelta[0].getElementsByTagName(Nodo);
    return NodoScelto[0].getElementsByTagName('Intestazione')[0].firstChild.data;

}


function CreaSelectMo(ID, Lingua, NodoScelto, ValueSelected){

	var LinguaSelect = xml.getElementsByTagName(Lingua);
	var Nodo = LinguaSelect[0].getElementsByTagName(NodoScelto);
	var NumeroElementi = Nodo[0].getElementsByTagName('Value').length;
	var Valore;
	var Chiave;

	var Sel = document.getElementById(ID);
	var CreaOption;

	for (i = 0; i<NumeroElementi; i++) {
		Valore = Nodo[0].getElementsByTagName('Value')[i].firstChild.data;
		Chiave = Nodo[0].getElementsByTagName('Value')[i].getAttribute('Key');

		CreaOption = document.createElement('option');
		CreaOption.text = Valore;
		CreaOption.value = Chiave;
		Sel.add(CreaOption, null);
		
		if (ValueSelected == Chiave){
			Sel.selectedIndex = i;
		}
		 
	}
}


//--- IE ------------------------------------------------------------------------------


function RecuperaValoreIE(XMLQuery){
	var Dati = xml.selectNodes(XMLQuery);
	return Dati(0).text;
}

function CreaSelectIE(ID, XMLQuery, ValueSelected){

   	var Dati = xml.selectNodes(XMLQuery);
   	var NumeroElementi = Dati.length;
	var Valore;
	var Chiave;    	

	var Sel = document.getElementById(ID);
	var CreaOption;

	for (i = 0; i<NumeroElementi; i++) {

       		Valore = Dati(i).text;
       		Chiave = Dati(i).getAttribute('Key');

		CreaOption = document.createElement('option');
		CreaOption.text = Valore;
		CreaOption.value = Chiave;
		Sel.add(CreaOption); 		

		
		if (ValueSelected == Chiave){
			Sel.selectedIndex = i;
		}
		
	}
	
}
