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 = window.document.forms[0];
  var numeroElementi = frm.elements.length;
  
  for(var i = 0; i < numeroElementi; i++){
  	
    if(i < numeroElementi){
      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(){
  if (!window.document.forms[0].consenso.checked){
  	window.document.getElementById("risultato").innerHTML='<span class="home1">Non hai accettato la Privacy Policy</span>';
  	return false;
  }
  PreparaDati();
  AJAXReq("POST","inc/send_mail_index.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){
    	var ajaxDisplay = window.document.getElementById("risultato");
    	ajaxDisplay.innerHTML = myReq.responseText;
    }
  }
}

