// Objectes per fer els loads AJAX

function ajaxLoader() {
    this.xmlhttp = null; // Objecte AJAX
    this.method = "GET"; //Per defecte fem sempre GET de les planes
    this.asincrono = true; // Controla si la llamada es asíncrona o síncrona

    // Variables que contindran els valors "loaded" després del procés
    this.responseText = null;
    this.responseXML = null;
    
    // Variables de quant a trigat el procés
    this.startTime = null;
    this.endTime = null;

    this.onLoading = function() {}; // Event quan s'inicia la càrrega d'un arxiu
    this.onLoaded = function() {}; // Event quan s'ha carregat el contingut però encara no està disponible
    this.onInteractive = function() {}; // Event quan s'ha carregat el contingut però encara no està disponible, però si és un html pots interactuar amb ell
    this.onCompleted = function() {}; // Event quan s'hagi completat tota la càrrega d'un arxiu

    // Funció per crear l'objete que ens servirà per fer el load d'un arxiu ajax
    this.createLoader = function()  {
        this.xmlhttp = null;

	if (window.XMLHttpRequest)
            this.xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(err) { 
                this.xmlhttp = null; 
            }
            if (!this.xmlhttp) {
                try {
                    this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                } catch(err) { 
                    this.xmlhttp = null; 
                }
            }
        }
	
        if (!this.xmlhttp)
            alert("Tu navegador no soporta la tecnología AJAX. Ponte en contacto con tu administrador.");
    }    

    this.load = function(uriString, destiText) {
        if (this.xmlhttp && uriString) {
            var self = this;
            // Ficarem codi per tal de que les cachés no ens donin resultats incorrectes
            var timeNow = new Date().getTime();

            var uriStringAnticache = uriString;
            var haveParams = uriString.indexOf("?") >= 0;
            if (haveParams)
                uriStringAnticache += "&"; // Seguent parametre
            else
                uriStringAnticache += "?"; // Primer parametre
            uriStringAnticache += "grubitTime=" + timeNow;

            this.xmlhttp.onreadystatechange = function() {
            switch (self.xmlhttp.readyState){
                    case 1:
                        self.onLoading();
						break;
                    case 2:
						self.onLoaded();
						break;
                    case 3:
                        self.onInteractive();
                        break;
                    case 4:
                        self.responseText = self.xmlhttp.responseText;
                        self.responseXML = self.xmlhttp.responseXML;
                        if (destiText) {
                            var destiTextNodeName = destiText.nodeName;
                            destiTextNodeName.toLowerCase();
                            if (destiTextNodeName == "input" || destiTextNodeName == "select" || destiTextNodeName == "option" || destiTextNodeName == "textarea") {
                                destiText.value = self.responseText;
                            } else {
                                destiText.innerHTML = self.responseText;
                            }
						}
						self.endTime = new Date();
        				self.onCompleted();
						break;
			}
                
            }
            this.xmlhttp.open(this.method, uriStringAnticache, this.asincrono);
            this.xmlhttp.send(null);
            
            this.startTime = new Date();
        }
    }

    this.createLoader();
}