var XMLHTTP;

var fileImmaginePreloader = "images/loading.gif";
var id_contenitore = "risultati";
		

function RichiestaForm(Stringa,id_contenitore)
{
	var arrayPageScroll = getPageScroll();
		document.getElementById(id_contenitore).style.top = arrayPageScroll[1]+200;
	id_div = id_contenitore;
    if (Stringa.length > 0)
    {
		PreparaDati();
		mostraloader(id_contenitore);
		
        XMLHTTP = RicavaBrowser(CambioStato);
        XMLHTTP.open("POST", Stringa, true);
		XMLHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        XMLHTTP.send(stringadati);
		
		//Richiesta(Stringa2,id_contenitore2);
		return false;
    }
    else
    {
        document.getElementById(id_contenitore).innerHTML = "";
    }
}

function Richiesta(Stringa,id_contenitore)
{
	var arrayPageScroll = getPageScroll();
		document.getElementById(id_contenitore).style.top = arrayPageScroll[1]+100;
	id_div = id_contenitore;
    if (Stringa.length > 0)
    {
		mostraloader(id_contenitore);
		
        XMLHTTP = RicavaBrowser(CambioStato);
        XMLHTTP.open("get", Stringa, true);
        XMLHTTP.send(null);
		//ahahEvalJS(id_contenitore);
		return false;
    }
    else
    {
        document.getElementById(id_contenitore).innerHTML = "";
    } 
}

function PreparaDati(){
  stringadati = "";
  var form = document.forms.formcat;
  var numeroElementi = form.elements.length;
  
  for(i = 0; i < numeroElementi; i++){
    if(i < numeroElementi-1){
      stringadati += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
    }else{
      stringadati += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
    }  
  }
}


function CambioStato()
{
    if (XMLHTTP.readyState == 4)
    {
        var R = document.getElementById(id_div);
        R.innerHTML = XMLHTTP.responseText;
    }
}

function RicavaBrowser(QualeBrowser)
{
    if (navigator.userAgent.indexOf("MSIE") != (-1))
    {
        var Classe = "Msxml2.XMLHTTP";
        if (navigator.appVersion.indexOf("MSIE 5.5") != (-1));
        {
            Classe = "Microsoft.XMLHTTP";
        } 
        try
        {
            OggettoXMLHTTP = new ActiveXObject(Classe);
            OggettoXMLHTTP.onreadystatechange = QualeBrowser;
            return OggettoXMLHTTP;
        }
        catch(e)
        {
            alert("Errore: l'ActiveX non verrą eseguito!");
        }
    }
    else if (navigator.userAgent.indexOf("Mozilla") != (-1))
    {
        OggettoXMLHTTP = new XMLHttpRequest();
        OggettoXMLHTTP.onload = QualeBrowser;
        OggettoXMLHTTP.onerror = QualeBrowser;
        return OggettoXMLHTTP;
    }
    else
    {
        alert("L'esempio non funziona con altri browser!");
    }
}
	// funzione mostra loader attesa
		function mostraloader(id_elemento) {
			document.getElementById(id_elemento).innerHTML = "<img src='"+fileImmaginePreloader+"' alt=''/>";
			return true;
		}

//funzione per far eseguire gli script JS all'interno della pagina caricata da ajax 
function ahahEvalJS(elementId) {
	var scripts = document.getElementById(elementId).getElementsByTagName('script');
	var code;
	//alert(scripts.length);
	for (i = 0; i < scripts.length; i++) {
		code =	scripts[i].innerHTML ? scripts[i].innerHTML : 
			scripts[i].text ? scripts[i].text : 
			scripts[i].textContent;
		try {
			eval(code);
			
		} catch(e) {
			alert(e);
		}
	}
}
