/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzioni scorciatoia.
 */
 
function value(id){
	return document.getElementById(id).value;
}

function show(id){
         document.getElementById(id).style.display = "block";
}

function hide(id){
	document.getElementById(id).style.display = "none";
}

function hd_sh(id1, id2){
	document.getElementById(id1).style.display = "none";
	document.getElementById(id2).style.display = "block";
}

function empty(id){
	document.getElementById(id).value="";
}

function scrivi_div(nome_div,contenuto_div){
         e = document.getElementById(nome_div);
         e.innerHTML = contenuto_div;
}
function errore(id) {
  document.getElementById(id).className = "errore_form";
  document.getElementById(id).focus();
}
function change_class(id) {
  document.getElementById(id).className = id;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * LE STRINGHE SOTTOSTANTI VENGONO USATE NEL CASO DI PASSAGGIO DATI TRAMITE POST
 */

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");
  }
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzione per aggiungere un commento
 */

var myReq;
var stringa;
function invia_messaggio(){

   var controllo=false;

   if (value("messaggio") == "" )
      {
      errore('messaggio');
      controllo=true;
      }


   if ((((value("email").indexOf("@")== -1 )||(!(value("email").indexOf("'")== -1 ))||(!(value("email").indexOf("\"")== -1 )))||(value("email") == "")))
      {
      errore('email');
      controllo=true;
      }

   if (value("nome_cognome") == "" )
      {
      errore('nome_cognome');
      controllo=true;
      }

   if (!(controllo)){
   stringa="nome_cognome="+escape(value("nome_cognome"))+"&email="+escape(value("email"))+"&messaggio="+escape(value("messaggio"))+"&rand="+escape(Math.random());
   AJAXReq("POST","funzioni_supporto.php?c=0",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(){
  hd_sh('div_bottone_invia_messaggio','div_caricamento_messaggio');
  if(myReq.readyState == 4){
    if(myReq.status == 200){
      //alert("stronzo");

      var responso = myReq.responseText;
      if (responso){
      //alert("stronza");
        empty("nome_cognome");
        empty("email");
        empty("messaggio");
        hd_sh('div_caricamento_messaggio','div_bottone_invia_messaggio');
        show("div_conferma_messaggio_inviato");
        window.setTimeout("hide('div_conferma_messaggio_inviato')",6000);
      }else{
              //alert("stronzi");

        hd_sh('div_caricamento_messaggio','div_messaggio_non_inviato');
        window.setTimeout("hide('div_messaggio_non_inviato')",6000);
      }
      //alert(myReq.responseText);
    }else{
      alert("Niente da fare, AJAX non funziona :(");
    }
  }
}