if (typeof jsnetweb == 'undefined'){
    jsnetweb = function() {}
}

jsnetweb.ajax = function() {
	
    var that = this;

    this.updating = false;

    this.annulla = function() {
        if (that.updating) {
            that.updating=false;
            that.richiesta.abort();
            that.richiesta=null;
        }
    }
	
    this.chiama = function(url, dati, elemento, metodo) {
        if (that.updating != false) {
            return false;
        }
        metodo = metodo || 'GET';
        elemento = elemento || null;
        if (elemento != null){
            that.contenitore=document.getElementById(elemento);
        }

        that.richiesta = null;
        try {
            that.richiesta = new XMLHttpRequest();
        } catch(error) {
            try {
                that.richiesta = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error) {
                return false;
            }
        }
		
        that.richiesta.onreadystatechange = function() {
					
            // connessione aperta
            if (that.richiesta.readyState==1) {
                that.attesa();
            }
            // richiesta inviata
            if (that.richiesta.readyState==2) {
            }
            // risposta in ricezione
            if (that.richiesta.readyState==3) {
            }
            // risposta ricevuta
            if (that.richiesta.readyState==4) {
                if (that.richiesta.status && /200|304/.test(that.richiesta.status)) {
                    that.updating = false;
                    that.callback(that.richiesta);
                    that.richiesta = null;
                } else {
                    that.updating = false;
                    that.errore(that.richiesta);
                    that.richiesta = null;
                }
            }
        }

        that.updating = true;
        if (/post/i.test(metodo)){
            that.richiesta.open('POST', url, true);
            that.richiesta.setRequestHeader('If-Modified-Since','Wed, 05 Apr 1900 00:00:00 GMT');
            that.richiesta.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            that.richiesta.setRequestHeader('Content-length', dati.length);
            that.richiesta.setRequestHeader('Connection', 'close');
            that.richiesta.send(dati);
        } else {
            url = url + "?" +dati;
            that.richiesta.open('GET', url, true);
            that.richiesta.setRequestHeader('If-Modified-Since','Wed, 05 Apr 1977 00:00:00 GMT');
            that.richiesta.send(null);
        }
        return true;
    }

    this.attesa = function() {
        that.contenitore.innerHTML = '<div style="text-align: center; margin: auto; height: 100%; background: url(immagini/caricamento.gif) center center no-repeat" lat="caricamento" /></div>';
        return false;
    }
	
    this.errore = function(requester) {
        alert('XMLHttp non ha funzionato. Status: '+requester.status);
        return true;
    }
	
    this.callback = function(requester) {
        that.data = requester.responseText;
        that.contenitore.innerHTML = that.data;
        return false;
    }
}
