//___________________________//
//       JS developer        //
//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
//            ·^·            //
//         /7\øñø©ë®ø        //
//     · Leonardo Alia ·     //
//  .: ©å§7ø®ø £åßø®ïø§ø :.  //
//___________________________//
//                           //
/* 

COME RICHIAMARE IL NUOVO OGGETTO E LA CHIAMATA AJAX:

var myRequest = new ajaxObject('http://www.somedomain.com/process.php','GET'); 
//--> CREAZIONE DELL'OGGETTO: il primo parametro è l'url di chiamata e il secondo è il metodo di chiamata
myRequest.ajaxRequest('id=1234&color=blue','div1',0);  
//--> FUNZIONE DI CHIAMATA: il primo parametro è la stringa dei dati passati e il secondo è l'id dell'oggetto in cui inserire l'output
// il terzo è per impostare il tipo di output con 0 viene inserito in un oggetto, con 1 viene inserita in una variabile javascript
// per usare questa variabile va integrata questa funzione con quella che elabora la variabile:

function [nomefunzione](){
	if(myRequest.AJAX.readyState < 4){var timer=setTimeout("[nomefunzione]()",1)}else{
	var nomevariabile=myRequest.output;
	//--> script di elaborazione variabile da inserire!
	}
}

//--> PER CHIAMARE CON IL METODO POST:
var myRequest = new ajaxObject('http://www.somedomain.com/process.php','POST'); 
myRequest.ajaxRequest('id=1234&color=blue','div1',0);  

*/
/************************************************************************************
COSTRUZIONE OGGETTO AJAX
************************************************************************************/
function ajaxObject(url,pM) {
	var that=this;  
	var urlCall=url;
	var passMethod=pM;
	that.output;
	//var stateArray=new Array("Non inizializzato","In caricamento","Caricato con successo","Interattivo","Completato");
	//var satateStr="Loading... ";
	var stateArray=new Array("Uninitialized","Loading","Loaded","Interactive","Complete");
	var stateStr="State... ";
	this.ajaxRequest = function(passData,targetResponse,typeOutput){
		that.AJAX=null;
		if(window.XMLHttpRequest) {
		    try {
			that.AJAX = new XMLHttpRequest();
		    } catch(e) {}
		} else if(window.ActiveXObject) {
		    try {
			that.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
		    } catch(e) {}
		} 
		if (that.AJAX==null) {return false;} else {
			
			if(passMethod=="GET"){
				var uC=urlCall+"?"+passData+"&ajaxTime="+((new Date()).valueOf());
				that.AJAX.open(passMethod, uC, true);
				that.AJAX.onreadystatechange = function() {
					if(typeOutput==0){
						
	      					if(that.AJAX.readyState == 4){
	      						document.getElementById(targetResponse).innerHTML = that.AJAX.responseText;
	      					}else{
	      						document.getElementById(targetResponse).innerHTML ="<span style='font-size:10px'>"+ stateStr + stateArray[that.AJAX.readyState]+"</span>";
	      					}
					}else{
	      					if(that.AJAX.readyState == 4){
	      						that.output = that.AJAX.responseText;
	      					}else{
	      						that.output = stateArray[that.AJAX.readyState];
	      						
	      					}
	      				}
				}
				that.AJAX.send(null); 
			} else {
				var uC=urlCall+"?ajaxTime="+((new Date()).valueOf());
				that.AJAX.open(passMethod, uC, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				that.AJAX.setRequestHeader("Content-Length", passData.length);
				that.AJAX.onreadystatechange = function() {
					if(typeOutput==0){
	      					if(that.AJAX.readyState == 4){
	      						document.getElementById(targetResponse).innerHTML = that.AJAX.responseText;
	      						that.output = that.AJAX.responseText;
	      					}else{
	      						//document.getElementById(targetResponse).innerHTML ="<span style='font-size:10px'>"+ stateStr + stateArray[that.AJAX.readyState]+"</span>";
	      					}
					}else{
	      					if(that.AJAX.readyState == 4){
	      						that.output = that.AJAX.responseText;
	      					}else{
	      						that.output = stateArray[that.AJAX.readyState];
	      						
	      					}
	      				}
				}
				that.AJAX.send(passData);
			}
			return true;
		}
	} 
}