function AJAXReq(method,url,bool){
  if(window.XMLHttpRequest){
    myReq = new XMLHttpRequest();
  } else 
  
  if(window.ActiveXObject){
    myReq = new ActiveXObject("Microsoft.XMLHTTP");
    
    if(!myReq){
      myReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
  }
  
  if(myReq){
    execfunc(method,url,bool);
  }else{
    alert("Impossibilitati ad usare AJAX");
  }
}

function PreparaDati(){
  stringa = "";
  var frm = document.forms[0];
  var numeroElementi = frm.elements.length;
  
  for(var i = 0; i < numeroElementi; i++){
    if(i < numeroElementi-1){
      stringa += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value)+"&";
    }else{
      stringa += frm.elements[i].name+"="+encodeURIComponent(frm.elements[i].value);
    }  
  }
}

var myReq;
var stringa;
function InviaDati(){
  PreparaDati();
  AJAXReq("POST","contatti-end.php",true);
}

function execfunc(method,url,bool){
  myReq.onreadystatechange = handleResponse;
  myReq.open(method,url,bool);
  
  /* Spiegare setRequestHeader */
  myReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
  myReq.send(stringa);
}

function handleResponse(){
  if(myReq.readyState == 4){
    if(myReq.status == 200){
      
	 document.getElementById("risultati").innerHTML = "<img src=\"img/loading.gif\" />...caricamento in corso...";

        document.getElementById("risultati").innerHTML =   myReq.responseText;
	  
    }else{
      alert("Niente da fare, AJAX non funziona :(");
    }
  }
}

