// restituisce un oggetto XMLHttpRequest
function getXMLHttpRequest() {
    if(typeof XMLHttpRequest!="undefined") {//disponibile in modo nativo sulla maggior parte dei browser
        return new XMLHttpRequest();
    }
    else if(window.ActiveXObject) { //versione di IE precedente alla 7
        var ajax_version = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp.2.0","MSXML2.XMLHttp", "Msxml2.XMLHTTP"];
        for(var ajax_i = 0; ajax_i < ajax_version.length; ajax_i++) {
            try{
                var ajax_obj = new ActiveXObject(ajax_version[ajax_i]);
                return ajax_obj;
            }
            catch(e){}
        }
    }
    return null;
}

// Aggiorna il contenuto del container caricandoci la pag specificata.
//i parametri post possono essere omessi
function ajaxUpdater(containerID, page, post) {
    var req = getXMLHttpRequest();
    var onreadystate = function() {
    if(req.readyState == 4) {
        //alert(req.responseText);
        if(req.status == 200 || req.status == 0) { // strano, su firefox mi restituisce 0!!!
            var risposta = req.responseText.replace("&amp;", "&");
            if(containerID != null) document.getElementById(containerID).innerHTML = risposta;
        }
        else {
            alert("Si e\' verificato un errore: "+req.statusText+" ("+req.status+")"+page);
        }
    }
    else {}// attesa
    }
    var method = "POST";
    if(post == null || post == "") {post = ""; method = "GET";}
    req.onreadystatechange = onreadystate;
    req.open(method, page, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-15");
    req.send(post); // eventuale spedizione parametri
}

// e' in grado di caricare script dopo una chiamata ajax
function loadScript(scriptpath, functions) {
	var oXML = getXMLHttpRequest();
	oXML.open('GET', scriptpath, false);  // il false e' importante perché il browser aspetta di averlo caricato tutto
	oXML.send('');
	eval(oXML.responseText);
	for(var i=0; i<functions.length; i++)
		window[functions[i]] = eval(functions[i]);
}

// crea un iframe nascosto al quale invia i dati del form (non usa ajax!)
function ajaxLikeUpload(idForm) {
    var form = document.getElementById(idForm);
    if(form.getAttribute("enctype") != "multipart/form-data")
        form.setAttribute("enctype", "multipart/form-data");
    form.setAttribute("target", "iframeAjaxLike");
    if(!document.getElementById("iframeAjaxLike")) {	// se non c'e' gia'
        var iframe = null;
        try {iframe = document.createElement("<iframe name='iframeAjaxLike'>");}//per Explorer
        catch(e){}
        if(!iframe || iframe.nodeName.toLowerCase() != "iframe") iframe = document.createElement("iframe");
        iframe.setAttribute("id", "iframeAjaxLike");
        iframe.setAttribute("name", "iframeAjaxLike");
        iframe.style.display = "none";
        document.body.appendChild(iframe);
    }// altrimenti forzo l'attributo name
    else document.getElementById("iframeAjaxLike").setAttribute("name", "iframeAjaxLike");	
    form.submit();
}
