/*
*	In C++ e' l' equivalente di creare una classe con 2 membri statici
*	class ResponseType {
*	public:
*		static const char* xml;
*		static const char* html;
*	};
*
*	const char* ResponseType::xml  = "XML;
*	const char* ResponseType::html = "HTML;
*/

var XMLHTTPREQUEST_MS_PROGIDS = new Array(
  "Msxml2.XMLHTTP.7.0",
  "Msxml2.XMLHTTP.6.0",
  "Msxml2.XMLHTTP.5.0",
  "Msxml2.XMLHTTP.4.0",
  "MSXML2.XMLHTTP.3.0",
  "MSXML2.XMLHTTP",
  "Microsoft.XMLHTTP"
);

function ResponseType(){
}

ResponseType.xml  = "XML";
ResponseType.html = "HTML";


function newXMLHttpRequest(){
	var xmlreq = false;

	//Controllo il tipo di oggetto XMLHttpRequest da utilizzare
	if(window.XMLHttpRequest){
		//Per browser non Microsoft
		xmlreq = new XMLHttpRequest();
        //alert("stantard");
	} else
    if (window.ActiveXObject != null)  {
        // Must be IE, find the right ActiveXObject.
        var success = false;
        for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++) {
            try {
                xmlreq = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
                //alert(XMLHTTPREQUEST_MS_PROGIDS[i]);
                success = true;
            } catch (ex) { xmlreq = false; }
        }
    }
    
    /*
    else if(window.ActiveXObject){
		//Cerco di creare l'oggetto via MS ActiveX
		try{
			//Nuove versioni per browser IE
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e1){ //Errore riscontrato durante la creazione dell'oggetto
			try{
				//Precedenti versioni per browser IE
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e2){ //Nuovo errore durante la creazione dell'oggetto
				xmlreq = false;
			}
		}
	}*/
	//Restituisco l'eventuale oggetto XMLHttpRequest
	return xmlreq;
} 

/*
* Ritorna una funzione per la gestione dello stato dell'oggetto req.
* Ed infine a stato finale raggiunto, invia la risposta XML alla funzione che dovra' gestire il tutto.
* reg = Istanza XMLHTTPRequest
* XMLHandler = nome della funzione a cui passare il risultato XML da gestire
*/
function handleResponse(req, XMLHandler, mode, params){
	return function(){
		//Controlla se l'oggetto req ha raggiunto lo stato finale
		if(req.readyState == 4){			
			if (req.status == 200){				
				switch(mode) { 
					case ResponseType.xml  : XMLHandler(req.responseXML, params);
										break;

					case ResponseType.html : XMLHandler(req.responseText, params);
								    		break;
					default: break;					
				}
			}else{
				alert("Errore HTTP: " + req.status);
			}
		}
	}
}
