var NS4 = (document.layers)?true:false;
var MSIE = (document.all)?true:false;
var NS6 = (navigator.userAgent.indexOf("Gecko")>0)?1:0;
var validdata;


//Gestione news scroller su home page
function scrollObject(main, width, height, direct, pause, speed) {
  var self = this;
  this.main = main;
  this.width = width;
  this.height = height;
  this.direct = direct;
  this.pause = pause;
  this.speed = Math.max(1.001, Math.min((direct == "up" || direct == "down") ? height : width, speed));
  this.slope = (direct == "up" || direct == "left") ? 1 : -1;
  this.prev = this.offset = 0;
  this.curr = 1;
  this.mouse = false;
  this.scroll = function() {
    this.main = document.getElementById(this.main);
    this.main.style.overflow = "hidden";
    this.main.style.position = "relative";
    this.main.style.width = this.width + "px";
    this.main.style.height = this.height + "px";
    var b = [], c;
    while (this.main.firstChild) if ((c = this.main.removeChild(this.main.firstChild)).nodeName == "DIV") b.push(c);
    for (var x = 0; x < b.length; x++) {
      var table = document.createElement('table');
          table.cellPadding = table.cellSpacing = table.border = "0";
          table.style.position = "absolute";
          table.style.left = table.style.top = "0px";
          table.style.width = table.style.height = "100%";
          table.style.overflow = table.style.visibility = "hidden";
        var tbody = document.createElement('tbody');
          var tr = document.createElement('tr');
            var td = document.createElement('td');
              while (b[x].firstChild)
                  td.appendChild(b[x].removeChild(b[x].firstChild));
              tr.appendChild(td);
            tbody.appendChild(tr);
          table.appendChild(tbody);
      this.main.appendChild(table);
    } b = c = null;
    if (this.main.childNodes.length > 1) {
      this.main.onmouseover = function() { self.mouse = true; };
      this.main.onmouseout = function() { self.mouse = false; };
      setInterval(function() {
        if (!self.offset && self.scrollLoop()) self.main.childNodes[self.curr].style.visibility = "visible";
      }, this.pause);
    } this.main.childNodes[this.prev].style.visibility = "visible";
  };
  this.scrollLoop = function() {
    if (!this.offset) {
      if (this.mouse) return false;
      this.offset = (this.direct == "up" || this.direct == "down") ? this.height : this.width;
    } else this.offset = Math.floor(this.offset / this.speed);
    if (this.direct == "up" || this.direct == "down") {
      this.main.childNodes[this.curr].style.top = (this.offset * this.slope) + "px";
      this.main.childNodes[this.prev].style.top = ((this.offset - this.height) * this.slope) + "px";
    } else {
      this.main.childNodes[this.curr].style.left = (this.offset * this.slope) + "px";
      this.main.childNodes[this.prev].style.left = ((this.offset - this.width) * this.slope) + "px";
    }
    if (!this.offset) {
      this.main.childNodes[this.prev].style.visibility = "hidden";
      this.prev = this.curr;
      if (++this.curr >= this.main.childNodes.length) this.curr = 0;
    } else setTimeout(function() { self.scrollLoop(); }, 30);
    return true;
  };
  if (window.addEventListener) {
    window.addEventListener('load', function() { self.scroll(); }, false); 
  } else if (window.attachEvent)
    window.attachEvent('onload', function() { self.scroll(); });
}

//Usata nella form di sottoscrizione a newsletter per sezione ferroviaria. Regola attivazione ddl in base a stato di una cbx
function gestioneAbbonato(stato){
	document.getElementById('ddlTipoAbbonato').disabled = (! stato);
  return false;
}

//Usata nelle form di sottoscrizione a newsletter  per verificare cbx d'invio email e/o sms
function gestioneTipoAvviso(elEmail, elSms, elCell){
	var email = document.getElementById(elEmail);
	var sms = document.getElementById(elSms);
	var cell = document.getElementById(elCell);
	if ((! email.checked) && (! sms.checked)){
		validdata = false;
		window.alert('ATTENZIONE: selezionare almeno una modalità di avviso.');
	}
	else{
		if ((sms.checked) && (cell.value == "")) {
			validdata = false;
			window.alert('ATTENZIONE: compilare il campo cellulare se si desidera ricevere gli avvisi via sms.');
		}
		else{
			validdata = true;
		}
	}
}

//Verifica funzionamento campo confermaEmail
function confermaEmail(e, ce){
	var email = document.getElementById(e).value;
	var cEmail = document.getElementById(ce).value;
	if ((email != cEmail) || (email == '') || (cEmail == '')) {
		validdata = false;
		window.alert('ATTENZIONE: campi email e conferma email incoerenti.');
	}
	else {
		validdata = true;
	}
}

//OBSOLETA E MAL PROGETTATA!!!!!
function checkValid(){
	if (! validdata) {
		return false;
	}
	for (i=1;i<12;i++){
		if ( (document.forms[0].elements[i].name).substring(0,5)=="oblig" ){
			 nomeimg = 'img' + document.forms[0].elements[i].name;
			 document.images[nomeimg].src='../imag_condivise/puntinografico.gif';
		}
		if ( (document.forms[0].elements[i].name).substring(0,5)=="oblig"&&document.forms[0].elements[i].value==""){
			validdata=false;
			stringa = document.forms[0].elements[i].name.length;
			window.alert('ATTENZIONE: il campo ' + (document.forms[0].elements[i].name).substring(5,stringa) + ' è obbligatorio');
			document.images[nomeimg].src='../imag_condivise/puntinograficoan.gif';
			document.forms[0].elements[i].focus();
			return false;    	  
		}
		else{
			validdata = true;
		}
	}	
 	if (validdata){
		document.forms[0].submit();
 	}
	return false;	
}

/*
Nuova versione di checkValid.
NOTE:
- i campi obbligatori devono avere specificati gli attributi ID e NAME uguali;
- l'attributo ID/NAME di cui sopra deve rispettare la sintassi: <ext><nomeCampo>, dove <ext> è lungo 3 caratteri;
- il puntino grafico che accompagna i campi obbligatori può essere omesso (senza alterare la funzione sottostante);
- il puntino grafico che accompagna ciascun campo obbligatorio deve avere specificato l'attributo ID;
- l'attributo ID di cui sopra deve rispettare la sintassi: img<ext><nomeCampo>, dove <ext><nomeCampo> sono gli stessi del campo obbligatori associato.
*/
function verificaObbligatori(arrElementi){
	if (! validdata) {
		return false;
	}
	var elemento;
	for (i=0; i<arrElementi.length; i++){
		elemento = document.getElementById(arrElementi[i]);
		nomeimg = 'img' + arrElementi[i];			 
		if (elemento.value == ""){
			 if (document.getElementById(nomeimg) != null) {
			 	document.getElementById(nomeimg).src = '../imag_condivise/puntinograficoan.gif';
			 }
			 window.alert('ATTENZIONE: il campo '+ elemento.id.substring(3, elemento.id.length) +' è obbligatorio.');
			 elemento.focus();
			 validdata = false;
			 return false;
		}
		else{
			if (document.getElementById(nomeimg) != null) {
				document.getElementById(nomeimg).src = '../imag_condivise/puntinografico.gif';
			}
			validdata = true;
		}
	}	
 	if (validdata){
		document.forms[0].submit();
 	}
}

//Verifica estensione file selezionato in <input type="File"...
function verificaEstensione(cid){
  var file = document.getElementById(cid);
  var nomeFile = file.value;
  var estensione = nomeFile.substr(nomeFile.length-3);
  //tiff > iff
  if ((estensione != "") && (!(estensione in {pdf:1, doc:1, xls:1, dwg:1, dxf:1, iff:1, jpg:1, tif:1, png:1}))){
    window.alert("Il file allegato ha un'estensione non consentita.");
    validdata = false;
  }
  else {
   validdata = true;
  }
}

//Forza la deselezione della checkbox2 in base al valore della checkbox1 (simula radio)
function exclusiveChecked(cbx1, cbx2) {
         if (document.getElementById(cbx1).checked == true)
             document.getElementById(cbx2).checked = false;
		 else {
			document.getElementById(cbx1).checked = true;
			document.getElementById(cbx2).checked = false;
		}
}

//Gestisce risposte a scomparsa per le FAQ
function manageFaq(idF){
	var elemento = document.getElementById(idF);
	if (elemento.style.visibility == 'visible') {
		elemento.style.visibility = 'hidden';
		elemento.style.display = 'none';
	}
	else {
		elemento.style.visibility = 'visible';
		elemento.style.display = 'block';
	}
}