function ciclaNews(quale) {
  /* le news sono contenute in questo specifico div */
  var fasciaNews = document.getElementById("fascia_news");
  if (!fasciaNews) return false;  

  /* vediamo se ce ne sono */
  var leNews = fasciaNews.getElementsByTagName("li");
  if (!leNews) return false; 

  /* se abbiamo una sola news, la mostro e non faccio il ciclo */
  if (leNews.length == 1) {
    leNews[0].className = "mostra";
    return true;
  }

  /* nascondiamo quella in questo momento visibile */
  if (quale == 0) {
    leNews[leNews.length - 1].className = "";
  } else {
    leNews[quale - 1].className = "";
  }

  /* rendiamo visibile la news che ci interessa */
  leNews[quale].className = "mostra";

  /* le news vengono cambiate ogni 8 secondi */
  setTimeout("ciclaNews(" + ((quale + 1) % leNews.length) + ")", 8000);
}

function mostraRichiestaAllegato() {
  var fileAllegato = document.getElementById("fileAllegato");
  if (!fileAllegato) return false;
  
  var tipoAllegato = valoreCheckbox();
  if (!tipoAllegato) return false;
  
  if (tipoAllegato == "file") {
    fileAllegato.className = "";
  } else {
    fileAllegato.className = "nascosto";
  }
}

function valoreCheckbox() {
  var check = document.forms[0].chkAllegoMateriale;
  if (!check) return false;
  
  for (var i = 0; i < check.length; i++) { if (check[i].checked) return check[i].value; }
}

// 20070605 - edoardo.galvagno@gruppotesi.com
// funzione che cerca i textarea con una determinata classe ("limita")
// e aggancia le funzioni necessarie a contare le parole nel box e a limitare l'inserimento
function limitaParole() {
  // cerco tra tutti i textarea quelli con classe "limita" e li metto nell'array specifico
  var ta = document.getElementsByTagName('textarea');
  for (var i = 0; i < ta.length; i++) {
  	if (ta[i].className.search(/limita/i) != -1) {
  	  // appendo le funzioni necessarie per il controllo
  	  ta[i].onkeyup = function() { contaCaratteri(this.id); }
  	  ta[i].onblur = function() { document.getElementById(this.id + '_cp').className = ""; };
  	}
  }
}

// 20070605 - edoardo.galvagno@gruppotesi.com
// conta le parole inserite nell'input associato e scrive quante ne rimangono
function contaParole(campo) {
  var maxParole = 150;
  var daControllare = document.getElementById(campo).value;
  // trim degli spazi prima e dopo
  daControllare = daControllare.replace(/^\s*|\s*$/g, "");
  // rimuovo i doppi spazi
  daControllare = daControllare.replace(/\s+/g, ' ');
  if (daControllare == "") {
    numParole = 0;
  } else {
    // spezzo agli spazi
    var parole = daControllare.split(' ');
    var numParole = parole.length;
  }
  
  var ancoraParole = maxParole - numParole;
  if (ancoraParole >= 1) {
    var fineFrase = (ancoraParole > 1) ? " parole" : " parola";
    document.getElementById(campo + '_cp').innerHTML = "ancora " + ancoraParole + fineFrase;
    document.getElementById(campo + '_cp').className = "attivo";
  } else {
    // tronco il testo al numero massimo di parole
    parole.length = maxParole;
    var testoRimpiazzato = parole.join(" ");
    document.getElementById(campo).value = testoRimpiazzato;
    document.getElementById(campo + '_cp').innerHTML = "RAGGIUNTO LIMITE MASSIMO LUNGHEZZA TESTO";
    document.getElementById(campo + '_cp').className = "attivo";
  }
}

// 20090614 - edoardo.galvagno@gruppotesi.com
// conto i caratteri
function contaCaratteri(campo) {
  // var re = /\d+/;
  // var maxCaratteri = parseInt(re.exec(document.getElementById(campo + '_cp').innerHTML)[0]);
  var maxCaratteri = parseInt(document.getElementById(campo + '_cp').title);
  var daControllare = document.getElementById(campo).value;
  var numCaratteri = daControllare.length;
  
  var ancoraCaratteri = maxCaratteri - numCaratteri;
  if (ancoraCaratteri >= 1) {
    var fineFrase = (ancoraCaratteri > 1) ? " caratteri" : " carattere";
    document.getElementById(campo + '_cp').innerHTML = "ancora " + ancoraCaratteri + fineFrase;
    document.getElementById(campo + '_cp').className = "attivo";
  } else {
    document.getElementById(campo).value = daControllare.slice(0, maxCaratteri);
    document.getElementById(campo + '_cp').innerHTML = "RAGGIUNTO LIMITE MASSIMO LUNGHEZZA TESTO";
    document.getElementById(campo + '_cp').className = "attivo";
  }
}
