// CLASSE AJAX

function ClassAJAX () {

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

	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 = new Array();
  		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.method="GET" ;
		
		this.movex=0 ;
		this.movey=0 ;
		this.xdiff=0 ;
		this.ydiff=0 ;
		this.ystart=0 ;
		this.xstart=0 ;
		this.elFake ;
		this.x ;
		this.y ;
		
		this.idSpanCounter = 0 ;
		
		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(this.method, urlstring, true);
				//this.xmlhttp.open("HEAD", urlstring, true);	
				
				this.xmlhttp.setRequestHeader("Content-type", "text/html");
				this.xmlhttp.setRequestHeader("Content-length", urlstring.length);
				this.xmlhttp.setRequestHeader("connection", "close");
				this.xmlhttp.setRequestHeader("keep-alive", 0);
				this.xmlhttp.setRequestHeader("Cache-Control", "no-cache");
				this.xmlhttp.setRequestHeader("Pragma", "no-cache");
				this.xmlhttp.setRequestHeader("Accept-Encoding", "deflate,gzip");
				
							
				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 ;
									
									case "autocomplete_rep" :
									
										if (self.elementObj) {
											
											elemNodeName = self.elementObj.nodeName;
											elemNodeName.toLowerCase();
																							
											if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
												//self.elementObj.value = self.response;
												showSuggest_rep(self.response, '', self.vars) ;
																						
											} else {
												//self.elementObj.innerHTML = self.response;
												showSuggest_rep(self.response, 'inner', self.vars) ; 
											}
										}
										
										if (self.responseStatus[0] == "200") {
											self.onCompletion();
										} else {
											self.onError();
										}
									break ;
									
									
									/*****************************************************
									*	PARTE PAQ DDT
									*****************************************************/
									/*
								case "insertDOCMAG" :
										insertDOCMAG(self.response, '', self.vars);
									break ;
									*/
									
									/*case "scriviMVMAG" :
										reloadMVMAG(self.vars) ;
									break ;*/
									/***************************************************/
									
									/*****************************************************
									*	PARTE MAURO ORDINI
									*****************************************************/
									case "scriviTextareaAjax" :										
										closeTextareaAjaxAfterSave(self.response, '', self.vars) ;										
									break ;
									/***************************************************/
									
								}
							}
							
							self.onComplete() ;
							
						break;
					}
				} ;
				
				this.xmlhttp.send(true);
			}
		}
		
	}
	
	this.setMethod = function (m) {
		this.method = m ;
	};
	
	this.changeHtml = function () {
		this.elementObj.innerHTML = '' ;
		this.elementObj.innerHTML = this.response ;
		delete this.response ; 
	};
	
	this.setIDX = function (id) {
		this.idx = id ;
	};
	
	this.storeHtml = function () {
		this.html[this.idx] = this.response ;
		delete this.response ; 
	};
	
	this.getHtml = function () {
		if (this.html[this.idx]) {
			return this.html[this.idx] ;
		} 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 l = (window.innerWidth - 300) / 2 ;
					var t = (window.innerHeight - 100) / 2 ;
					
					var arrayPageSize = getPageSize();
					var w = arrayPageSize[0] ;
					var h = arrayPageSize[1] ;
					
					var bodyPage = document.getElementById("body") ;
					
					var divNode = document.createElement('div') ;
					
					divNode.setAttribute('id', this.spanLoading + this.idSpanCounter) ;	
					divNode.style.display="" ;
					divNode.style.backgroundImage="url(http://www.imperato.it/img/griglia.gif)" ;
					divNode.style.position="absolute" ;
					divNode.style.width=w+"px" ;
					divNode.style.height=h+"px";
					
					divNode.style.zIndex="91";	
					divNode.style.top = "0px" ;
					divNode.style.left = "0px" ;
							
					divNode.innerHTML = '<div id="'+this.spanLoading + this.idSpanCounter +'_inner" style="position:absolute; left:'+l+'px; top:'+t+'px; width:300px; height:100px;">' + 
										'<table class="caricamento" border="0" cellpadding="0" cellspacing="0" width="300" height="100">' +
										'<tr><td valign="middle" align="center">' +
										'<b>Attendere...</b>' +
										'</td></tr>'+
										'</table>' +
										'</div>' ;
											
									
					bodyPage.appendChild(divNode) ;
					
					this.flagLoading = true ;
					
					this.setVariables() ;
					this.checkLocation() ;
					
				break ;
			}
		}
		
		
	};
	
	this.onComplete = function () {
		
		if (this.flagLoading) {		
			
			var bodyPage = document.getElementById("body") ;
			var divNode = document.getElementById(this.spanLoading + this.idSpanCounter);
				
			bodyPage.removeChild(divNode) ;
			
			this.flagLoading = false ;
			
		}
	};
	
	
	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.setVariables = function (){
		
		if (navigator.appName == "Netscape") {
			if (parseInt(navigator.appVersion) >= 5){
				v=".top=";
				h=".left=";
				dS="document.getElementById(\"";sD="\").style";
				this.y="window.pageYOffset";
				this.x="window.pageXOffset";
			}
			else {
				v=".top=";
				h=".left=";
				dS="document.";
				sD="";
				this.y="window.pageYOffset";
				this.x="window.pageXOffset";
			}
		}
		else {
		//ENTRO QUI
			h=".pixelLeft=";
			v=".pixelTop=";
			dS="";
			sD=".style";
			this.y="document.body.scrollTop";
			this.x="document.body.scrollLeft";
		}
				
		this.checkLocationA() ;
	};
	
	
	this.checkLocation = function(){
		
		try {
		
			object="obj";
			yy=eval(this.y);
			xx=eval(this.x);
			this.ydiff=this.ystart-yy;
			this.xdiff=this.xstart-xx;
			
			if ((this.ydiff<(-1))||(this.ydiff>(1))) {
				this.movey=Math.round(this.ydiff/10) ;
				this.ystart-=this.movey ;
			}
			if ((this.xdiff<(-1))||(this.xdiff>(1))) {
				this.movex=Math.round(this.xdiff/10) ; 
				this.xstart-=this.movex ;
			}
			
			var divNode = document.getElementById(this.spanLoading + this.idSpanCounter +'_inner');
			
			if (this.ystart>=10){
				
				var newY = this.ystart+10 ;
				divNode.style.top=newY+'px' ;
				
			} 
			else {
				
				var newY = 10 ;
				divNode.style.top=newY+'px' ;
			}
		
			var newX = this.xstart ;
			divNode.style.left=newX+'px' ;
			
			//alert("sposto in left:" + newX + " top:" + newY) ;
			
			if (this.flagLoading) {
				var self = this;
				setTimeout(function(){
					self.checkLocation();
				}, 10);
			}
		}
		catch(e) {}
		
	}
	
	this.checkLocationA = function(){
		this.ystart=eval(this.y);
		this.xstart=eval(this.x);
	}
	
	
	
	this.reset();
	this.createAJAX();
}
/*
 ---------------------------------
*/


function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
