// form validation function //
function validoForm_segnala(form,antispam) {
  var mittente = form.mittente.value;
  var emailsegnala = form.emailsegnala.value;
  var nomesegnala = form.nomesegnala.value;
 var botcontrol = form.botcontrol.value.toLowerCase();
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(botcontrol != antispam) {
    inlineMsg('botcontrol','<strong>Errore</strong><br />Il testo dell\'antispam non coincide',2);
    return false;
  }
  if(mittente == "") {
    inlineMsg('mittente','<strong>Errore</strong><br />Per favore inserisci il tuo nome.',2);
	//alert("e")
    return false;
  }
  if(mittente.match(messageRegex)) {
    inlineMsg('mittente','Hai inserito un nome non valido.');
    return false;
  }
  if(nomesegnala == "") {
    inlineMsg('nomesegnala','<strong>Errore</strong><br />Per favore inserisci il tuo nome.');
    return false;
  }
  if(nomesegnala.match(messageRegex)) {
    inlineMsg('nomesegnala','<strong>Errore</strong><br />Nome non valido');
    return false;
  }
  if(emailsegnala == "") {
    inlineMsg('emailsegnala','<strong>Errore</strong><br />Per favore inserisci l\' email.',2);
    return false;
  }
  if(!emailsegnala.match(emailRegex)) {
    inlineMsg('emailsegnala','<strong>Errore</strong><br />Email non valida.',2);
    return false;
  }
 // return true;
}
function validoForm_invia(form,antispam) {
  var mittente = form.Mittente.value;
  var nome = form.Nome.value;
  var allegato = form.Allegato.value;
 var botcontrol = form.botcontrol.value.toLowerCase();
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(allegato == "") {
    inlineMsg('Allegato','<strong>Errore</strong><br />Non hai allegato nessun file.',2);
	//alert("e")
    return false;
  }
  if(botcontrol != antispam) {
    inlineMsg('botcontrol','<strong>Errore</strong><br />Il testo dell\'antispam non coincide',2);
    return false;
  }
  if(mittente == "") {
    inlineMsg('Mittente','<strong>Errore</strong><br />Per favore inserisci l\' email.',2);
    return false;
  }
  if(!mittente.match(emailRegex)) {
    inlineMsg('Mittente','<strong>Errore</strong><br />Email non valida.',2);
    return false;
  }
  if(nome == "") {
    inlineMsg('Nome','<strong>Errore</strong><br />Per favore inserisci il tuo nome.',2);
	//alert("e")
    return false;
  }
 // return true;
}
function validoForm_iscriviti(form,antispam) {
  var cognome = form.Cognome.value;
  var nome = form.Nome.value;
  var mail = form.Mail.value;
  var mailv = form.Emailv.value;
  var acconsenti = form.consenso;
  
 var botcontrol = form.botcontrol.value.toLowerCase();
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(!acconsenti.checked) {
	      inlineMsg('consenso','<strong>Errore</strong><br />Devi dare il consenso alla legge sulla privacy.',2);
    return false;
  }

  if(botcontrol != antispam) {
    inlineMsg('botcontrol','<strong>Errore</strong><br />Il testo dell\'antispam non coincide',2);
    return false;
  }
  if(nome == "") {
    inlineMsg('Nome','<strong>Errore</strong><br />Per favore inserisci il tuo nome.',2);
    return false;
  }
  if(cognome == "") {
    inlineMsg('Cognome','<strong>Errore</strong><br />Per favore inserisci il tuo cognome.',2);
    return false;
  }
  if(mail == "") {
    inlineMsg('Mail','<strong>Errore</strong><br />Per favore inserisci l\' email.',2);
    return false;
  }
  if(!mail.match(emailRegex)) {
    inlineMsg('Mail','<strong>Errore</strong><br />Email non valida.',2);
    return false;
  }
  if(mail != mailv) {
    inlineMsg('Emailv','<strong>Errore</strong><br />Verifica la tua mail.',2);
    return false;
  }
 
 // return true;
}
function validoForm_commento(form,antispam) {
  var nome = form.nome.value;
  var commento = form.Commento.value;
  var titolo = form.Titolo.value;
 var botcontrol = form.botcontrol.value.toLowerCase();
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  if(botcontrol != antispam) {
    inlineMsg('botcontrol','<strong>Errore</strong><br />Il testo dell\'antispam non coincide',2);
    return false;
  }
  if(nome == "") {
    inlineMsg('nome','<strong>Errore</strong><br />Per favore inserisci il tuo nome.',2);
	//alert("e")
    return false;
  }
  if(titolo == "") {
    inlineMsg('Titolo','<strong>Errore</strong><br />Per favore inserisci l\' email.',2);
    return false;
  }
  if(!titolo.match(emailRegex)) {
    inlineMsg('Titolo','<strong>Errore</strong><br />Email non valida.',2);
    return false;
  }

if(commento == "") {
    inlineMsg('Commento','<strong>Errore</strong><br />Per favore inserisci un commento.');
    return false;
  }
 // return true;
 		window.open('', 'commento_win', 'height=200,width=400,scrollbars=no');
		form.target='commento_win';
		form.submit();
		document.getElementById('commento').style.display='none';		
		return false;


}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}