/*
*	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();
	} else
    if (window.ActiveXObject != null)  {
        // IE, trova l' ActiveXObject migliore.
        var success = false;
        for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++) {
            try {
                xmlreq = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
                success = true;
            } catch (ex) { 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);
			}
		}
	}
}

function setCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    
    if (nDays==null || nDays==0) nDays=1;
    
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(cookieName + "=");
        if (c_start!=-1) { 
            c_start=c_start + cookieName.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}

function deleteCookie( cookieName, path, domain ) {
    if ( getCookie( cookieName ) )
        document.cookie = cookieName + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}