// CLASSE AJAX

function ClassAJAX () {

	this.xmlhttp = null ;
	this.stato = "" ;

	this.reset = function() {
		
		this.execute = false; //se true fatica come sappiamo....mette codice ottenuto da file php in id_span
		this.returnVal = false ;
		this.html = "" ;
  		this.element = null;	// ÃƒÂ¨ l'id dell'oggetto (l'id_span)
		this.elementObj = null;
		this.responseStatus = new Array(2);
		this.vars = Array () ;
		this.stato = "" ;
		this.actionType = "" ;
		this.spanLoading = "loading" ;
		this.flagLoading = false ;
		
		this.onFailed = function() { };
	};
	
	//	creo oggetto XHTMLHTTP compatibile col browser altrimenti setto 
	//	variabile booleana this.failed = true ;
	this.createAJAX = function() {
		
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}
	
		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};
	
	
	// metodo che mi ritorna il codice dall' url che passo come parametro
	this.runAJAX = function(urlstring) {
		
	
		if (this.failed) {
			//se ho avuto problemi in createAJAX vado in function onFailed()
			this.onFailed() ;
		} else {			
			
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			
			if (this.xmlhttp) {
			
				var self = this ;
				
				this.xmlhttp.open("GET", urlstring, true);				
				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.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;
							
							if (self.execute) {	//se devo fare l'innerHTML del contenuto dinamico nell'elemento
								self.changeHtml();
							}
							else if (self.returnVal) {
								self.storeHtml();
							}
							else {
								
								/*
									QUI CAZZI PER AUTOCOMPLETE etc ....
								*/
								/*switch (self.actionType) {
										
									default :
									
										if (self.elementObj) {
											
											elemNodeName = self.elementObj.nodeName;
											elemNodeName.toLowerCase();
											
											if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
												//self.elementObj.value = self.response;
												showSuggest(self.response, '', self.vars) ;
																						
											} else {
												//self.elementObj.innerHTML = self.response;
												showSuggest(self.response, 'inner', self.vars) ; 
											}
										}
										
										if (self.responseStatus[0] == "200") {
											self.onCompletion();
										} else {
											self.onError();
										}
									break ; 
									
								}*/
							}
							
							self.onComplete() ;
							
						break;
					}
				} ;		
				this.xmlhttp.send(true);
			}
		}
		
	}
	
	this.changeHtml = function () { 
		this.elementObj.innerHTML = this.response ; 
	};
	
	this.storeHtml = function () {
		this.html = this.response ;
	};
	
	this.getHtml = function () {
		if (this.html) {
			return this.html ;
		} else {
			return false;	
		}
	};
	
	this.setElement = function(elementId) {
		this.element = elementId ;
	};
	
	this.setExecute = function(bool) {
		this.execute = bool ;
	};
	
	this.setReturn = function(bool) {
		this.returnVal = bool ;
	};
			
	this.setActionType = function(actType) {
		this.actionType = actType ;
	};
	
	this.setVars = function (key,val) {
		this.vars[key] = val ;
	};
	
	this.setLoading = function (spanloading) {
		this.spanLoading = spanloading ;
	};

	this.onFailed = function () {
		alert ("onFailed") ;
	};
	
	this.onLoading = function () {
		
		if (this.spanLoading) {
			
			switch (this.spanLoading) {
				
				default :
									
 
					var elemento = document.getElementById(this.spanLoading);
 						
				  if( typeof( window.innerWidth ) == 'number' ) {
				   //Non-IE
					myWidth = window.innerWidth;
					myHeight = window.innerHeight;
				  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
					//IE 6+ in 'standards compliant mode'
					myWidth = document.documentElement.clientWidth;
					myHeight = document.documentElement.clientHeight;
				  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
					//IE 4 compatible
					myWidth = document.body.clientWidth;
					myHeight = document.body.clientHeight;
				  }
					l = (myWidth - 100) / 2 ;
					t = (myHeight - 100) / 2 ;
					
					elemento.style.top = t ;
					elemento.style.left = l ;
					
					elemento.style.display = "";
					elemento.innerHTML = '<table border="0" celllpadding="0" cellspaging="0" width="100%" height="100%">' +
											'<tr><td valign="middle" align="center">' +
											'<img src="./images/loading.gif" alt="loading"/>' +
											'</td></tr>'+
											'</table>' ;
											
					this.flagLoading = true ;
					
					//setVariables() ;
					//checkLocation() ;
					
				break ;
			}
		}
		
		
	};
	
	this.onComplete = function () {
		
		if (this.flagLoading) {
			var elemento = document.getElementById(this.spanLoading);
			elemento.style.display = "none";

		}
	};
	
	this.onLoaded = function () {
		this.stato = "loaded" ;
	};
	
	this.onInteractive = function () {
		//alert ("onInteractive") ;
	};
	
	this.onCompletion = function () {
		//alert ("onCompletion") ;	
	};
	
	this.onError = function () {
		//alert ("onError") ;
	};
	
	this.getStatus = function () {
		
		return this.stato ;
	};
	
	
	this.reset();
	this.createAJAX();
}
/*
 ---------------------------------
*/
