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_login(){
  stringa = "";
  var form = document.form_login;
  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_ins_news(){
  stringa = "";
  var form = document.form_in;
  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_ins_prog(){
  stringa = "";
  var form = document.form_ip;
  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){
 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 = '<div style="text-align:center; height:50px; font-family:verdana; font-size:12px; color:#000000;"><strong>...caricamento...</strong><br><img alt="loader" src="images/ajax-loader.gif" /></div>';
}

function carica(id,pagina){
  loader(id);
  setTimeout("AJAXReq('GET','"+pagina+"',true,function (){handleResponse('"+id+"')});", 1000);
}

function ins_p(id){
  loader(id);
  setTimeout("AJAXReq('GET','ins_prog.php',true,function (){handleResponse('"+id+"')});", 1000);
}

function login(id){
  PreparaDati_login();
  loader(id);
  setTimeout("AJAXReq('POST','validate.php',true,function (){handleResponse('"+id+"')},stringa);", 1000);
}
function ins_news(id){
  PreparaDati_ins_news();
  loader(id);
  setTimeout("AJAXReq('POST','ins_news.php',true,function (){handleResponse('"+id+"')},stringa);", 1000);
}
function ins_prog(id){
  PreparaDati_ins_prog();
  loader(id);
  setTimeout("AJAXReq('POST','ins_prog.php',true,function (){handleResponse('"+id+"')},stringa);", 1000);
}

function chiudi(id){
 document.getElementById(id).style.display = 'none';
 document.getElementById(id).style.visibility = 'hidden';
}
