// framework leggero per la gestione di dati XML in AJAX
// realizzato da Riccardo Raneri <riccardo@crweb.it>
// v. rc2

function ajax(){

    var Carica;
    var Inizializza;
    var Parsa;
    var Restituisci;
    var LoadAtt;
    var LoadVar;
    var LoadCdata;
    var InsVal;
    var InsAtt;
    
    var _this;
    var _callBack;
    var xmlhttp;
    
    this.Carica = function(metodo, paginaxml, callBack){
      _this = this;
      _callBack = callBack;
      this.Richiesta = this.Inizializza();
      this.Richiesta.open(metodo, paginaxml, false);
      this.Richiesta.onreadystatechange = function(){ _this.Parsa() }
      this.Richiesta.send(null);
      }
    
    
    this.Inizializza = function(){
	  var xmlhttp=false;
	  /*@cc_on @*/
	  /*@if (@_jscript_version >= 5)

	   try {
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	   } catch (e) {
	    try {
	     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (E) {
	     xmlhttp = false;
	    }
	   }
	  @end @*/
	  if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest() }
	  return xmlhttp;
    }
   
    
    this.Parsa = function(){
      if(this.Richiesta.readyState == 4){
        if(this.Richiesta.status == "200"){ this.Restituisci() }
        else{ this.Restituisci(this.Richiesta.status) }
        }
      }
    
    
    this.Restituisci = function(codErr){
      if(!codErr){
        sorgenteXML = this.Richiesta.responseXML;
        eval(_callBack + '()');
        }
      else{
        eval(_callBack + '(\'errore \' + codErr)');
        }
      }
    
    }
    
    this.LoadVal = function(elementoXML, indice){
      if (!indice){indice = 0}
      if (typeof(this.sorgenteXML.getElementsByTagName(elementoXML)[indice]) != 'undefined'){
        if (typeof(this.sorgenteXML.getElementsByTagName(elementoXML)[indice].firstChild) != 'undefined'){
          return this.sorgenteXML.getElementsByTagName(elementoXML)[indice].firstChild.nodeValue;
          }
        }
      else{
        return 'non definito'
        }
    }
    
    this.lunghezza = function(elementoXML){
      return this.sorgenteXML.getElementsByTagName(elementoXML).length
      }
    
    this.LoadAtt = function(elementoXML, attributo, indice){
      if (!indice){indice = 0}
      
      if (this.sorgenteXML.getElementsByTagName(elementoXML)[indice]){
        return this.sorgenteXML.getElementsByTagName(elementoXML)[indice].getAttribute(attributo);
        }
      else{
        return 'non definito'
        }
    }
    
    this.LoadCdata = function(elementoXML, indice){
      if (!indice){indice = 0}
      
      if (this.sorgenteXML.getElementsByTagName(elementoXML)[indice]){
        return this.sorgenteXML.getElementsByTagName(elementoXML)[indice].lastChild.data;
        }
      else{
        return 'non definito'
        }
    }
    
    this.InsVal = function(elementoHTML, elementoXML){
      document.getElementById(elementoHTML).innerHTML = this.sorgenteXML.getElementsByTagName(elementoXML)[0].firstChild.nodeValue;
    }
    
    this.InsAtt = function(elementoHTML, elementoXML, attributo){
      document.getElementById(elementoHTML).innerHTML = this.sorgenteXML.getElementsByTagName(elementoXML)[0].getAttribute(attributo);
    }
    
function Scrivi(id, HTML){
  document.getElementById(id).innerHTML = HTML
  }
  
function Fade(id, opacita, verso){
  clearTimeout(timerFade);
  var elemento = document.getElementById(id);
  if (verso == 'in') {
    if (opacita<100) {
      opacita = opacita+10;
      elemento.style.MozOpacity = opacita/100;
      elemento.style.filter = 'alpha(opacity=' + opacita + ')'; 
      timerFade = setTimeout('Fade("' + id + '", "' + opacita + '", "' + verso + '")', 100)
      }
    }
  else {
    if (opacita>0) {
      opacita = opacita-10;
      elemento.style.MozOpacity = opacita/100;
      elemento.style.filter = 'alpha(opacity=' + opacita + ')'; 
      timerFade = setTimeout('Fade("' + id + '", "' + opacita + '", "' + verso + '")', 100)
      }
    }
  }
