var miaRichiesta = null;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Creazione dell'oggetto "XML HTTP request".
 */
 
function creaConn(manipolatore) {
  
  var xmlhttp = null;
  try {
    xmlhttp = new XMLHttpRequest();
  } catch(e) {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  xmlhttp.onreadystatechange = manipolatore;
  return xmlhttp;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzione per il controllo della username
 */

function controllo_campo_ajax(nome_campo) {
	var valore_campo = value(nome_campo);
	var r = Math.random();
	miaRichiesta = creaConn(risp_controllo_campo_ajax);
	miaRichiesta.open("GET","concorso/funzioni_supporto.php?c=1&nome_campo="+escape(nome_campo)+"&valore_campo="+escape(valore_campo)+"&rand="+escape(r));
	miaRichiesta.send(null);
}

function risp_controllo_campo_ajax() {
           document.getElementById('pulsante_iscrizione').disabled=1;
	if (miaRichiesta.readyState == 4 && miaRichiesta.status == 200) {
          document.getElementById('pulsante_iscrizione').disabled=0;
                var responso = miaRichiesta.responseText.split("||||");
                e = document.getElementById(responso[1]);
                e.innerHTML = responso[0];

	}
}

/*
		if(responso != "") {
			e.innerHTML = responso;
			e.style.display = "block";
		} else {
			e.style.display = "none";
		}
*/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzione per il controllo della username
 */

function controllo_campo_ajax_votante(nome_campo) {
	var valore_campo = value(nome_campo);
	var r = Math.random();
	miaRichiesta = creaConn(risp_controllo_campo_ajax_votante);
	miaRichiesta.open("GET","../funzioni_supporto.php?c=2&nome_campo="+escape(nome_campo)+"&valore_campo="+escape(valore_campo)+"&rand="+escape(r));
	miaRichiesta.send(null);
}

function risp_controllo_campo_ajax_votante() {
           document.getElementById('pulsante_iscrizione').disabled=1;
	if (miaRichiesta.readyState == 4 && miaRichiesta.status == 200) {
          document.getElementById('pulsante_iscrizione').disabled=0;
                var responso = miaRichiesta.responseText.split("||||");
                e = document.getElementById(responso[1]);
                e.innerHTML = responso[0];

	}
}

/*
		if(responso != "") {
			e.innerHTML = responso;
			e.style.display = "block";
		} else {
			e.style.display = "none";
		}
*/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 */

function convalida_forms(){
         var c = 0;
         function controllo(variabile){
                  if (variabile == 0)
                     c = c + 1;
         }
         controllo(value('confemail'));
         controllo(value('confusername'));
         controllo(value('accetto_regolamento'));

         if (c != 0)
            show('errore_form_concorso'); // div_errore è l'id della divisione in cui è contenuto il messaggio di errore

         else{

              miaRichiesta = creaConn(risp_convalida_forms);
              miaRichiesta.open("GET","concorso/funzioni_supporto.php?c=0&email="+escape(value('email'))+"&username="+escape(value('username'))+"&rand="+escape(Math.random()));
              miaRichiesta.send(null);
              hd_sh('div_form_concorso','div_form_concorso_inviato');
              hide('errore_form_concorso');
         }
}

function risp_convalida_forms() {
         show('div_caricamento');
	if (miaRichiesta.readyState == 4 && miaRichiesta.status == 200) {
         hide('div_caricamento');
		var responso = miaRichiesta.responseText;
                e = document.getElementById('div_form_concorso_inviato');
                e.innerHTML = responso;
         }
}




function controllo_checkbox()
   {
   var accetto_regolamento=false;
   for (i=1;i<6;i++)
      {
      if  ((accetto_regolamento) || (document.getElementById("accetto_regolamento").checked)){document.getElementById("accetto_regolamento").value = "1"; accetto_regolamento=true;}
      }
   if (!accetto_regolamento)
      {
      document.getElementById("accetto_regolamento").value = "0";
      }
  }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzione per iscrizione votanti
 */

function convalida_forms_votanti(){
         var c = 0;
         function controllo(variabile){
                  if (variabile == 0)
                     c = c + 1;
         }
         controllo(value('confemail'));
         controllo(value('confusername'));
         controllo(value('accetto_regolamento'));

         if (c != 0)
            show('errore_form_concorso'); // div_errore è l'id della divisione in cui è contenuto il messaggio di errore

         else{

              miaRichiesta = creaConn(risp_convalida_forms_votanti);
              miaRichiesta.open("GET","../funzioni_supporto.php?c=6&email="+escape(value('email'))+"&username="+escape(value('username'))+"&rand="+escape(Math.random()));
              miaRichiesta.send(null);
              hide('div_form_concorso');
              hide('errore_form_concorso');
         }
}

function risp_convalida_forms_votanti() {
         show('div_caricamento');
	if (miaRichiesta.readyState == 4 && miaRichiesta.status == 200) {
         hide('div_caricamento');
		var responso = miaRichiesta.responseText;
                e = document.getElementById('div_form_concorso_inviato');
                e.innerHTML = responso;
         }
}

  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzione per attribuire un voto al video
 */

function vota_video(username_votante,video_votato) {
	var r = Math.random();
	miaRichiesta = creaConn(risp_vota_video);
	miaRichiesta.open("GET","../funzioni_supporto.php?c=3&username_votante="+escape(username_votante)+"&video_votato="+escape(video_votato)+"&rand="+escape(r));
	miaRichiesta.send(null);
}

function risp_vota_video() {
          hd_sh('div_vota_questo_video','div_caricamento');
	if (miaRichiesta.readyState == 4 && miaRichiesta.status == 200) {
          hd_sh('div_caricamento','div_voto_attribuito');
          var responso = miaRichiesta.responseText.split("||");
          document.getElementById('div_voti_residui').innerHTML = responso[1];
          document.getElementById('div_voti_ricevuti').innerHTML = responso[0];


	}
}

/*
		if(responso != "") {
			e.innerHTML = responso;
			e.style.display = "block";
		} else {
			e.style.display = "none";
		}
*/

  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Funzione per attribuire un voto al video
 */

function naviga_commenti(commenti_da,video_votato,avanti_indietro) {
	var r = Math.random();
	miaRichiesta = creaConn(risp_naviga_commenti);
	miaRichiesta.open("GET","../funzioni_supporto.php?c=5&commenti_da="+escape(commenti_da)+"&video_votato="+escape(video_votato)+"&avanti_indietro="+escape(avanti_indietro)+"&rand="+escape(r));
	miaRichiesta.send(null);
}

function risp_naviga_commenti() {
        hd_sh('div_tabella_navigazione_commenti','div_caricamento_navigazione_commenti');
	if (miaRichiesta.readyState == 4 && miaRichiesta.status == 200) {
          hd_sh('div_caricamento_navigazione_commenti','div_tabella_navigazione_commenti');
          hide('div_commenti');
          var responso = miaRichiesta.responseText.split("divido_");
          document.getElementById('div_commenti_navigazione').innerHTML = responso[0];
          document.getElementById('div_tabella_navigazione_commenti').innerHTML = responso[1];

	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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_commento(username_votante,video_votato){
   if (value('commento')!=""){
   stringa="username_votante="+escape(username_votante)+"&video_votato="+escape(video_votato)+"&commento="+escape(value("commento"))+"&rand="+escape(Math.random());
   AJAXReq("POST","../funzioni_supporto.php?c=4",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_commento','div_caricamento_commento');
  if(myReq.readyState == 4){
    if(myReq.status == 200){
      document.getElementById('commento').value="";
      hd_sh('div_caricamento_commento','div_bottone_invia_commento');
      hd_sh('div_nessun_commento_ricevuto','div_conferma_messaggio_inserito');

      var responso = myReq.responseText.split("_stop_^");
      document.getElementById('div_commenti_ricevuti').innerHTML = responso[1];
      document.getElementById('nuovo_commento_utente').innerHTML = responso[0]+document.getElementById('nuovo_commento_utente').innerHTML;

      window.setTimeout("hide('div_conferma_messaggio_inserito')",6000);

      //alert(myReq.responseText);
    }else{
      alert("Niente da fare, AJAX non funziona :(");
    }
  }
}
