cache = new Array();
var myReq = null;
function AJAXReq(methodtype,url,asynch,resp){
 if(window.XMLHttpRequest)
 {
  myReq = new XMLHttpRequest();
 }else if(window.ActiveXObject){
   myReq = new ActiveXObject("Msxml2.XMLHTTP");
    if(!myReq){
      myReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
 }
 
 if(myReq){
  if(methodtype.toLowerCase() != "post"){
    execfunc(methodtype,url,asynch,resp);
  }else{
    var args = arguments[4];
    execfunc(methodtype,url,asynch,resp,args);
  }
 }else{
   alert("Your browser doesn't support AJAX utilities");
 }
}

function execfunc(methodtype,url,asynch,resp){
  try{
    myReq.onreadystatechange = resp;
    myReq.open(methodtype,url,asynch);
    
    if(methodtype.toLowerCase() == "post"){
      myReq.setRequestHeader("Content-Type",
                             "application/x-www-form-urlencoded; charset=UTF-8");
      myReq.send(arguments[4]);
    }else{
      myReq.send(null);
    }
  }catch(errv){
    alert("Enable to contact the server\nError: "+errv.message);
  }
}

function PreparaDati(){
  stringa = "";
  var form = document.forms[0];
  var numeroElementi = form.elements.length;
 
  for(var i = 0; i < numeroElementi; i++){
    if(i < numeroElementi-1){
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
    }else{
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
    } 
  }
}

function handleResponse(id,caching){
 if(myReq.readyState == 4 && myReq.status == 200)
 {
  // alert("Response  " +  id);
  if(caching){
    cache[id] = true;
  }  
  document.getElementById(id).innerHTML = myReq.responseText;
 }
}

function loader(id){
  document.getElementById(id).style.display = 'block';
  document.getElementById(id).style.visibility = 'visible';
  document.getElementById(id).innerHTML = '<img alt="loader" src="/js/ajax_loader/ajax-loader.gif" />';
}

function fallo(id,data,pagina){
//data son tute le variabili da passare in get...basta passare una stringa del genere var1=X?var2=Y?var3=K.....varN=Z
//pagina è la pagina che verra caricata (bisogna passargli tutto il path
//id è il nome del div nel quale andremmo a visulizzare la pagina
   loader(id); 
   var paginadacaricare = pagina+'?'+data;
   setTimeout("AJAXReq('GET','"+paginadacaricare+"',true,function (){handleResponse('"+id+"',true)});", 10);
 
} 

var arrayid,data,pagina;

function multiload(arrayid,data,pagina,initime,delay)
{
   for (i=0;i<arrayid.length;i=i+1) 
	{
	   loader(arrayid[i]); 
 	}
	this.arrayid=arrayid;
	this.data=data;
	this.pagina=pagina;	
   setTimeout("startMultiload(0);", initime);
} 

function handleResponseMulti(id,next)
{
   if(myReq.readyState == 4 && myReq.status == 200)
   {
     // alert("Response id " + id ); 	
     document.getElementById(id).innerHTML = myReq.responseText;
     startMultiload(next);
   }
}

function startMultiload(i)
{
	if ( i >= arrayid.length ) return;
   var idload = arrayid[i];
   var paginadacaricare = pagina+'?nomeid='+idload+'&'+data;
   // alert(paginadacaricare + " - " + idload);
   AJAXReq('GET',paginadacaricare,true, function () { handleResponseMulti(idload,i+1)});
} 



function chiudi(id){
 document.getElementById(id).style.display = 'none';
 document.getElementById(id).style.visibility = 'hidden';
}

function richiudi(id){
 document.getElementById(id).style.display = 'none';
 document.getElementById(id).style.visibility = 'hidden';
 document.getElementById(id).innerHTML = '';
}

