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 = "";
  try{
		var frm = document.forms[1];
	}catch(Exception){
 		var frm = document.forms[0];
	}
 // var frm = document.forms[1];
  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;
var sId;
function InviaDati(id,urlx){
  document.getElementById('tom_reports').innerHTML = '<span style="color: #000;clear: both;position: fixed;top:10em;font-weight: bold;left: 40%;"><img src="/_htdocs/images/loading.gif" alt="Loading" /></span>';
  sId = id;
  PreparaDati();
  AJAXReq("POST","?ajax="+id+"&sid="+Math.random()+"&"+urlx,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(sId).innerHTML = myReq.responseText;
	    try{
		    document.getElementById(sId).innerHTML = myReq.responseText;
		}catch(Exception){}
		try{
	       	if(document.getElementById('id_province').disabled == true
	       		|| document.getElementById('id_province').selectedIndex == 0) {
	       		document.getElementById('id_comune_id').selectedIndex = 0;
	    		document.getElementById('id_comune_id').disabled = true;
	    		  
				   	document.getElementById('id_zone').selectedIndex = 0;
	    			document.getElementById('id_zone').disabled = true;
	    	}
		}catch(Exception){}
    	
    	//alert(document.getElementById('id_region').options[0].text);
    	document.getElementById('tom_reports').innerHTML = '';
    	//document.getElementById('tom_reports').innerHTML = '<span style="color: #000000;clear: both;position: fixed;top: 0;font-weight: bold;"><img src="/casaveloce/_htdocs/images/loading_ajax2.gif" alt="Loading" /></span>';
    	//document.getElementById('tom_reports').innerHTML = '<span style="background-color: #ce1a06;color: #f0f0f0;clear: both;position: fixed;top: 0;font-weight: bold;">Operazione compiuta</span>';
     //alert(myReq.responseText);
    }else{
      alert("Niente da fare, AJAX non funziona :(");
    }
  }
}