// JavaScript Document

if ( !document.layers && !document.all ) event = "test";

function showtip2(current, e, text) {
  if ( document.all && document.readyState == "complete" ) {
    document.all.tooltip2.innerHTML = '<div id="livello">' + text + '</div>';
    document.all.tooltip2.style.pixelLeft = event.clientX + document.body.scrollLeft+10;
    document.all.tooltip2.style.pixelTop = event.clientY + document.body.scrollTop+10;
    document.all.tooltip2.style.visibility = "visible";
  } else if ( document.layers ) {
    document.tooltip2.document.nstip.document.write('<b>' + text + '</b>');
    document.tooltip2.document.nstip.document.close();
    document.tooltip2.document.nstip.left = 0;
    document.tooltip2.left = e.pageX + 10;
    document.tooltip2.top = e.pageY + 10;
    document.tooltip2.visibility = "show";
  } else {
    alert(text);
  }
};

function hidetip2() {
  if ( document.all ) document.all.tooltip2.style.visibility = "hidden";
  else if (document.layers) {
    clearInterval(currentscroll);
    document.tooltip2.visibility = "hidden";
  }
};

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height) {
  if ( popUpWin ) {
    if( !popUpWin.closed ) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');
};

function switchImage(imgName, imgSrc) {
  if ( document.images ) {
    if ( imgSrc != "none" ) {
      document.images[imgName].src = imgSrc;
    }
  }
};

// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload() { 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for (var i=0; i<args.length; i++) {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
};

var loop, timer;
var initialised, delay;
var bw;
delay=100;

function verifyCompatibleBrowser() {
  this.ver = navigator.appVersion;
  this.dom = document.getElementById ? 1 : 0;
  this.ope = (this.ver.indexOf("Opera") > -1);
  this.ie6 = (this.ver.indexOf("MSIE 6") > -1 && this.dom) ? 1 : 0;
  this.ie4 = (document.all && !this.dom) ? 1 : 0;
  this.ie5 = ( (this.ver.indexOf("MSIE 5") > -1 && this.dom) || this.ie4 || this.ie6 || this.ope) ? 1 : 0;
  this.ns5 = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0;
  this.ns4 = (document.layers && !this.dom) ? 1 : 0;
  this.bw = (this.ie5 || this.ie4 || this.ns4 || this.ns5);
  return this;
};

function CreaScroll(obj, nest) {
  nest = (!nest) ? '':'document.'+nest+'.';
  this.el = bw.dom?document.getElementById(obj) : bw.ie4 ? document.all[obj] : bw.ns4?eval(nest + 'document.' + obj) : 0;
  this.css = bw.dom?document.getElementById(obj).style : bw.ie4 ? document.all[obj].style : bw.ns4 ? eval(nest + 'document.' + obj):0;
  this.scrollHeight = bw.ns4 ? this.css.document.height : this.el.offsetHeight;
  this.clipHeight = bw.ns4 ? this.css.clip.height : this.el.offsetHeight;
  this.up = MoveAreaUp;
  this.down = MoveAreaDown;
  this.left = MoveAreaLeft;
  this.right = MoveAreaRight;
  this.MoveArea = MoveArea;
  this.x;
  this.y;
  this.obj = obj + "Object";
  eval(this.obj + "=this");
  return this;
};

function CreaContenitore(obj) {
  this.el = bw.dom ? document.getElementById(obj) : bw.ie4 ? document.all[obj] : bw.ns4 ? eval('document.' + obj) : 0;
  this.css = bw.dom ? document.getElementById(obj).style : bw.ie4 ? document.all[obj].style : bw.ns4 ? eval('document.' + obj) : 0;
  this.obj = obj + "Object";
  eval(this.obj + "=this");
  return this;
};

function MoveArea(x, y){
  this.x = x;
  this.y = y;
  this.css.left = this.x;
  this.css.top = this.y;
};

function MoveAreaDown(passo, altezza){
  if(this.y > -altezza){
    this.MoveArea(0, this.y - passo);
    if(loop) setTimeout(this.obj + ".down(" + passo + "," + altezza + ")", delay);
  }
};

function MoveAreaUp(passo){
  if(this.y < 0){
    this.MoveArea(0, this.y - passo);
    if(loop) setTimeout(this.obj + ".up(" + passo + ")",delay)
  }
};

function MoveAreaLeft(passo, altezza){
  if(this.x > -altezza){
    this.MoveArea(this.x - passo, 0);
    if(loop) setTimeout(this.obj + ".left(" + passo + "," + altezza + ")", delay)
  }
};

function MoveAreaRight(passo){
  if(this.x<0){
    this.MoveArea(this.x - passo, 0);
    if(loop) setTimeout(this.obj + ".right(" + passo + ")", delay)
  }
};

function PerformScroll(passo, oggetto, altezza) {
  if(initialised) {
    loop = true;
    if (passo > 0) {
      oggetto.down(passo, altezza);
    } else {
      oggetto.up(passo);
    }
  }
};

function PerformLaterale(passo, oggetto, altezza) {
  if( initialised ) {
    loop = true;
    if( passo > 0 ) {
      oggetto.left(passo, altezza);
    } else {
      oggetto.right(passo);
    }
  }
};


function CeaseScroll() {
  loop = false;
  if ( timer ) clearTimeout(timer);
};

function Posiziona(oggetto, left, top) { //vuole il nome della div, la distanza dal bordo sinistro e la distanza dal bordo destro
  if (bw.ns4) {
    //left-=1
    eval('document.' + oggetto + '.left=' + left);
    eval('document.' + oggetto + '.top=' + top);
    eval("document." + oggetto + ".visibility='visible'");
  }
  if ( bw.ns5 ) {
    //left-=1
    top -= 1;
    document.getElementById(oggetto).style.left = left;
    document.getElementById(oggetto).style.top = top;
    document.getElementById(oggetto).style.visibility = 'visible';
  };
  if ( bw.ie5 || bw.ie4 ) {
    document.all[oggetto].style.left = left;
    document.all[oggetto].style.top = top;
    document.all[oggetto].style.visibility = 'visible';
  }
};

function mostra(oggetto, pswitch) {
  if ( pswitch == "si" ) pswitch = 'visible';
  else pswitch = 'hidden';
  if ( bw.ns4 ) {
    eval("document." + oggetto + ".visibility='" + pswitch + "'");
  }
  if ( bw.ns5 ) {
    document.getElementById(oggetto).style.visibility = pswitch;
  }
  if ( bw.ie5 || bw.ie4 ) {
    document.all[oggetto].style.visibility = pswitch;
  }
};

function oset(padre, figlio) { 
  if ( bw.ie5 ) return eval(figlio + ".el.offsetHeight-" + padre + ".el.offsetHeight-5");
  if ( bw.ns4 ) return eval(figlio + ".css.document.height-" + padre + ".css.clip.height-5");
  if ( bw.ns5 ) return eval(figlio + ".el.offsetHeight-" + padre + ".el.offsetHeight");
};


function osetlar(padre, figlio) { 
  if ( bw.ie5 ) return eval(figlio + ".el.offsetWidth-" + padre + ".el.offsetWidth-5");
  if ( bw.ns4 ) return eval(figlio + ".css.document.width-" + padre + ".css.clip.width-5");
  if ( bw.ns5 ) return eval(figlio + ".el.offsetWidth-" + padre + ".el.offsetWidth");
};

function checkText(input) {
  if ( input.value == null || input.value == "" ) {
    alert("Campo " + input.name + " obbligatorio");
	input.select();
    input.focus();
    return false;
  } else return true;
};

function checkSelect(input) {
  if ( input.selectedIndex == 0 ) {
    alert("Campo " + input.name + " obbligatorio");
    input.focus();
    return false;
  } else return true;
};

function checkRadio(input) {
  var trovato = false;
  for (i = 0; i < input.length && !trovato; i++) trovato = input[i].checked;
  if ( !trovato ) {
	alert("Campo " + input[0].name + " obbligatorio");
    input[0].select();
	input[0].focus();
  }
  return trovato;
};

function checkTextareaLength(input, n_char) {
  if ( input.length > n_char ) input.value = input.value.substr(0, n_char-1);
};

//controlla la bisestilità dell'anno
function isLeapYear (year) {
  return ( ( ( year % 4 )==0 ) && ( ( year % 100 )!= 0 ) || ( ( year % 400) == 0 ) );
};

// controlla se il mese impostato è corretto
function checkMonth(month) {
  if (parseFloat(month) < 1 || parseFloat(month) > 12) {
    alert("Attenzione, il mese deve essere compreso fra 1 e 12");
    return false;
  } else return true;
};

// controlla il formato della data
function checkFormat(date) {
  var objRegExp = /[0-3]\d\/[0-1]\d\/\d{4}/;
  if ( !( objRegExp.test(date) ) ) {
    alert('Il formato della data immesso non è valido\nImmettere i dati nel formato dd/mm/yyyy');
	return false;
  } else return true;
};

/* controlla se il giorno impostato è corretto, in base alle caratteristiche del mese
e alla bisestilità dell'anno, verificando l'esattezza della data completa */
function checkDate(day, month, year) {
  var corretto = true;
  if ( checkFormat(day + "/" + month + "/" + year) && checkMonth(month) ) {
    var arrGiorni = new Array(null, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if ( isLeapYear(year) ) arrGiorni[2] = 29;
    if ( day < 1 || day > arrGiorni[parseFloat(month)] ) {
	  alert('Il giorno impostato è errato');
	  corretto = false;
	}
  } else corretto = false;
  return corretto;
};

function verificaData(input) {
  var arrData = input.value.split('/');
  var giorno = arrData[0];
  var mese = arrData[1];
  var anno = arrData[2];
  if ( checkDate(giorno, mese, anno) ) return true;
  else {
    input.select();
    input.focus();
	return false;
  };
};

function verificaTelefono(input) {
  var result = checkText(input);
  for (ch = 0; ch < input.value.length; ch++) {
    if ( !(input.value.substring(ch, ch + 1) >= 0 && input.value.substring(ch, ch + 1) <= 9) ) {
	  alert("N. telefonico immesso non valido\nIl recapito telefonico può contenere solo numeri");
	  input.select();
      input.focus();
	  result = false;
	};
  };
  return result;
};

function verificaCAP(input) {
  result = checkText(input);
  if ( input.value.length > 5 ) {
	alert("CAP immesso non valido\nIl CAP consta di 5 numeri");
	input.select();
    input.focus();
	result = false;
  };
  for (ch = 0; ch < input.value.length; ch++) {
    if ( !(input.value.substring(ch, ch + 1) >= 0 && input.value.substring(ch, ch + 1) <= 9) ) {
	  alert("CAP immesso non valido\nIl CAP può contenere solo numeri");
	  input.select();
      input.focus();
	  result = false;
	};
  };
  return result;
};

function verificaEmail(input) {
  var result = checkText(input) || input.value.indexOf('@');
  if ( !result ) {
	  alert("Indirizzo email non immesso o non valido");
	  input.select();
      input.focus();
  };
  return result;
};

document.getElementsByClassName = function(cl, tag) {
  var retnode = [];
  var myclass = new RegExp('\\b' + cl + '\\b');
  var mytag = tag || '*';
  var elem = this.getElementsByTagName(mytag);
  for (var i=0; i<elem.length; i++) {
    var classes = elem[i].className;
    if ( myclass.test(classes) ) retnode.push(elem[i]);
  }
  return retnode;
};

function applySelectedTo(link) {
  var allLinksMenu = document.getElementsByClassName("a_menu", "a");
  alert(allLinkMenu.length);
  for (var i=0; i<allLinksMenu.length; i++) { 
    allLinksMenu[i].className = "a_menu"; 
  }
  link.className = "a_menu_selected"; 
}

function openNumberJournal(form) {
  var number = form.selectNumber[form.selectNumber.selectedIndex].value;
  var returnval;
  if ( number==0 ) {
    returnval = false;
  } else {
    // var link = "/sitoCM/pagine/pagine.php?id_pagina=143&linkmenu=il_nostro_periodico&lin=ita";
    var link = "/sitoCM/docs/rivista/" + number + ".pdf";
    window.document.location.href = link;
    // window.document.location.target = "_new";
    returnval = true;
  }
  return returnval;
}