var gblBgColorFocus =   "#FFEE88";
var gblBgColorUnFocus = "#FFFFFF";
var limInf = 30;
var myArray = new Array();
var myArrayT = new Array();
var activated = 0;
var timeStep = 100;
var step = 5;
var mandArray = new Array ('FLD_nome','FLD_cognome','FLD_email','FLD_testata');
var mandLabel = new Array ('nome','cognome','email','testata');

function fixIEevent(ev) {
  if(typeof ev=="undefined")ev=window.event;
  if(typeof ev.target=="undefined")ev.target=window.event.srcElement;
  return ev;
}

function doFilter() {
      var v1,v2;
      var repeat = 0;
      for (i in myArray) {
         if (myArrayT[i]) continue;
         if (myArray[i] == 100) continue;
         repeat = 1;
         myArray[i] += step;
         v1 = Math.abs(myArray[i]);
         v2 = v1/100;
         _gEl(i).childNodes[0].style.filter = 'alpha(opacity='+v1+')';
         _gEl(i).childNodes[0].style.opacity = v2;
      }
      if (repeat) {
         setTimeout('doFilter()',timeStep);
      }
      else {
         activated = 0;
      }
}

function _gEl(n) { return document.getElementById(n); }

function toggleImg(ev) {
      ev = fixIEevent(ev);
      var i = ev.target.parentNode.id;
      if (myArrayT[i]) {
         myArrayT[i] = false;
         _gEl(i).childNodes[0].style.filter = 'alpha(opacity=100)';
         _gEl(i).childNodes[0].style.opacity = 1;
      }
      else {
         myArrayT[i] = true;
         _gEl(i).childNodes[0].style.filter = 'alpha(opacity=0)';
         _gEl(i).childNodes[0].style.opacity = 0;
      }
}

function hideImg(ev) {
      ev = fixIEevent(ev);
      if (ev.target.nodeName == 'IMG') {
         var v = parseInt(100-step);
         var i = ev.target.parentNode.id;
         if (myArrayT[i]) return;
         if (myArray[i] && 
             myArray[i] != 100) return;
         
         myArray[i] = -v;
         ev.target.style.filter = 'alpha(opacity='+v+')';
         ev.target.style.opacity = v/100;
         if (activated == 0) {
            setTimeout('doFilter()',timeStep);
            activated = 1;
         }
      }
}

function mostraInformativa() {
  document.getElementById('extra-discl-ita').style.display = 'block';
  document.getElementById('div_form').style.display = 'none';
}

function nascondiInformativa() {
  document.getElementById('extra-discl-ita').style.display = 'none';
  document.getElementById('div_form').style.display = 'block';
}


function checkEmail (obj) {
  var content = obj.value;
  content = content.replace(/ /g,"");
  obj.value = content;

  if (obj.value == '') return true;

  // email address validation - Sandeep V. Tamhankar (stamhankar@hotmail.com)
  // for this code thanks to http://javascript.internet.com/link-us.html
  var emailStr = obj.value;
  var emailPat=/^(.+)@(.+)$/;
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  var validChars="\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
  var matchArray=emailStr.match(emailPat);
  if (matchArray==null) {
    alert('email non valido: controllare @ e .');
    //    obj.focus();
    return 1;
  }
  var user=matchArray[1];
  var domain=matchArray[2];

  // See if "user" is valid 
  if (user.match(userPat)==null) {
  // user is not valid
    alert('email non valido: username non corretto');
    obj.focus();
    return 1;
  }

  var IPArray=domain.match(ipDomainPat);
  if (IPArray!=null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
	alert('email non valido: ip destinazione non valido');
	obj.focus();
	return 1;
      }
    }
    return 0;
  }

  // Domain is symbolic name
  var domainArray=domain.match(domainPat);
  if (domainArray==null) {
    alert('email non valido: nome a dominio non corretto');
    obj.focus();
    return 1;
  }
  
  // domain name seems valid, but now make sure that it ends in a
  //  three-letter word (like com, edu, gov) or a two-letter word,
  //   representing country (uk, nl), and that there's a hostname preceding 
  //   the domain or country. 
  
  // Now we need to break up the domain to get a count of how many atoms
  // it consists of. 
  var atomPat=new RegExp(atom,"g");
  var domArr=domain.match(atomPat);
  var len=domArr.length;
  if (domArr[domArr.length-1].length<2 || 
      domArr[domArr.length-1].length>4) {
    // the address must end in a two letter or three letter word.
    alert('email non valido: il dominio deve terminare con 2/3/4 lettere');
    obj.focus();
    return 1;
  }

  // Make sure there's a host name preceding the domain.
  if (len<2) {
    alert('email non valido: nome a dominio non corretto (hostname mancante)');
    obj.focus();
    return 1;
  }

  // If we've gotten this far, everything's valid!
  return 0;
}


function setFocus(o) {
  o.style.backgroundColor = gblBgColorFocus;
}

function unsetFocus(o) {
  if (o.style)
    o.style.backgroundColor = gblBgColorUnFocus;
}

function checkForm() {
  for (var i=0;i<mandArray.length;i++) {
    var o = document.getElementById(mandArray[i]);
    o.value = o.value.replace(/ +/g," ");
    o.value = o.value.replace(/^ +/,"");
    o.value = o.value.replace(/ +$/,"");
    if (o.value == "") {
      alert('campo ' + mandLabel[i] + ' obbligatorio');
      o.focus();
      return false;
    }
  }
  if (checkEmail(document.getElementById('FLD_email'))) return false;
  document.myForm.submit();
  
}

function mostraForm() {
  var o = document.getElementById('div_registrazione');
  if (o.style.display == 'block') {
    o.style.display = 'none';
  }
  else {
    nascondiInformativa();
    o.style.display = 'block';
  }
    
}
