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 PreparaDati_reg_ins(){
  stringa = "";
  var form = document.myform;
  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 PreparaDati_cerca(){
  stringa = "";
  var form = document.form_search;
  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 PreparaDati_struttura_2(){
  stringa = "";
  var form = document.form1;
  var numeroElementi = form.elements.length;
 
  
}

function handleResponse(id){
 if(myReq.readyState == 4 && myReq.status == 200){
  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="/script/spinner.gif" />';
}
//NUOVO LOADER RICERCA
function loader_cerca(id){
  document.getElementById(id).style.display = 'block';
  document.getElementById(id).style.visibility = 'visible';
  document.getElementById(id).innerHTML = '<div style="margin-top:10px; width:632px; height:142px; background-image:url(images/bg_box.gif); background-repeat:no-repeat; padding-bottom:80px;"><div style="width:412px; float:top; padding-left:3px; padding-bottom:20px; padding-top:20px; padding-left:20px;font-size:22px; color:#326776;">Caricamento...</div><center><img alt="loader" src="ajax-loader.gif" /></center></div>';
}


function fallo(id){
  loader(id);
  setTimeout("AJAXReq('GET','loader.php',true,function (){handleResponse('"+id+"')});", 1000);
}

function cerca_tipo(id,tipo){
  loader_cerca(id);
  setTimeout("AJAXReq('GET','search.php?tipo="+tipo+"',true,function (){handleResponse('"+id+"')});", 1000);
}

function vota(id,pagina){
  loader(id);
  setTimeout("AJAXReq('GET','/"+pagina+"',true,function (){handleResponse('"+id+"')});", 1000);
}

function bblist(id){
  loader(id);
  setTimeout("AJAXReq('GET','/bb_list.php',true,function (){handleResponse('"+id+"')});", 1000);
}
function nolist(id){
  loader(id);
  setTimeout("AJAXReq('GET','/no_list.php',true,function (){handleResponse('"+id+"')});", 1000);
}
function cvlist(id){
  loader(id);
  setTimeout("AJAXReq('GET','/cv_list.php',true,function (){handleResponse('"+id+"')});", 1000);
}

function registrazione_ins(id){
  loader(id);
  PreparaDati_reg_ins();
  setTimeout("AJAXReq('POST','validate.php',true,function (){handleResponse('"+id+"')},stringa);", 1000);
}
function struttura_2(id){
  loader(id);
  PreparaDati_struttura_2();
  setTimeout("AJAXReq('POST','struttura2.php',true,function (){handleResponse('"+id+"')},stringa);", 1000);
}
function cerca(id){
  PreparaDati_cerca();
  loader_cerca(id);
  setTimeout("AJAXReq('POST','search.php',true,function (){handleResponse('"+id+"')},stringa);", 1000);
}


function chiudi(id){
 document.getElementById(id).style.display = 'none';
 document.getElementById(id).style.visibility = 'hidden';
}
