// form validation functions //
// form Add colaborador //
function validateFormColaborador(formcliente) {
  var name = form.name.value;
  var morada = form.morada.value;
  var localidade = form.localidade.value;
  var cod1 = form.cod1.value;
  var cod2 = form.cod2.value;
  var cod3 = form.cod3.value;
  var telemovel = form.telemovel.value;
  var email = form.email.value;
  var password = form.password.value;
  
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  
  if(name == "") {
    inlineMsg('name','Deve inserir um nome!',2);
    return false;
  }
  if(morada== "") {
    inlineMsg('morada','Deve inserir uma morada!',2);
    return false;
  }
  if(localidade== "") {
    inlineMsg('localidade','Deve inserir uma localidade!',2);
    return false;
  }
  if(cod1== "") {
    inlineMsg('cod1','Deve inserir os 4 dígitos do código postal!',2);
    return false;
  }
  if(cod2== "") {
    inlineMsg('cod2','Deve inserir os 3 dígitos do código postal!',2);
    return false;
  }
  if(cod3== "") {
    inlineMsg('cod3','Deve inserir um concelho!',2);
    return false;
  }
  if(telemovel== "") {
    inlineMsg('telemovel','Deve inserir um n.º de telemóvel!',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Deve inserir um e-mail!',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />O e-mail que introduziu está incorrecto!',2);
    return false;
  }
  if(password== "") {
    inlineMsg('password','Deve inserir uma password para o colaborador!',2);
    return false;
  }
  return true;
}

// form Add Férias //
function validateFormFerias(form) {
  var txtData = form.txtData.value;
  var txtDataFim = form.txtDataFim.value;
  
  if(txtData == "") {
    inlineMsg('txtData','Insira uma data de início!',2);
    return false;
  }
  if(txtDataFim == "") {
    inlineMsg('txtDataFim','Insira uma data de fim!',2);
    return false;
  }
  return true;
}

// form add evento agenda //
function validateFormAgenda(form){
var titulo = form.titulo.value;
var data = form.data.value;
var local = form.local.value;
var descricao = form.descricao.value;

if(titulo == "") {
    inlineMsg('titulo','Deve inserir um título ao evento!',2);
    return false;
  }
if(data == "") {
    inlineMsg('data','Deve inserir uma data ao evento!',2);
    return false;
  }
if(local == "") {
    inlineMsg('local','Deve inserir o local do evento!',2);
    return false;
  }
if(descricao == "") {
    inlineMsg('descricao','Deve inserir uma descrição ao evento!',2);
    return false;
  }
  return true;
}
// form add settings agenda //
function validateFormSettingsAgenda(form){
var grupo = form.grupo.value;
var email = form.email.value;
var password = form.password.value;
var path = form.path.value;

var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

if(grupo == "") {
    inlineMsg('grupo','Deve inserir a designação da lista!',2);
    return false;
  }
if(email == "") {
    inlineMsg('email','Deve inserir um e-mail!',2);
    return false;
  }
if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />O e-mail que introduziu está incorrecto!',2);
    return false;
  }
if(password == "") {
    inlineMsg('password','Deve inserir a password da lista!',2);
    return false;
  }
if(path == "") {
    inlineMsg('path','Insira o caminho absoluto para o ficheiro [users.lst]!',2);
    return false;
  }
  return true;
}
// form add categoria documentos //
function validateFormAddCategoria(form){
var titulo = form.titulo.value;

if(titulo == "") {
    inlineMsg('titulo','Deve inserir um título para a categoria!',2);
    return false;
  }
  return true;
}
// form add documento //
function validateFormAddDocumento(form){
var categoria = form.categoria.value;
var titulo = form.titulo.value;
var documento = form.documento.value;

if(categoria == "") {
    inlineMsg('categoria','Deve seleccionar uma categoria para o documento!',2);
    return false;
  }
if(titulo == "") {
    inlineMsg('titulo','Deve inserir um título ao documento!',2);
    return false;
  }
if(documento == "") {
    inlineMsg('documento','Indique o ficheiro que pretende adicionar!',2);
    return false;
  }
  return true;
}
// form edit documento //
function validateFormEditDocumento(form){
var categoria = form.categoria.value;
var titulo = form.titulo.value;

if(categoria == "") {
    inlineMsg('categoria','Deve seleccionar uma categoria para o documento!',2);
    return false;
  }
if(titulo == "") {
    inlineMsg('titulo','Deve inserir um título ao documento!',2);
    return false;
  }
  return true;
}
// form send contact //
function validateFormContactos(form){
var nome = form.nome.value;
var email = form.email.value;
var mensagem = form.mensagem.value;

var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

if(nome == "") {
    inlineMsg('nome','Deve inserir um nome!',2);
    return false;
  }
if(email == "") {
    inlineMsg('email','Deve inserir um e-mail!',2);
    return false;
  }
if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />O e-mail que introduziu está incorrecto!',2);
    return false;
  }
if(mensagem == "") {
    inlineMsg('mensagem','Deve inserir uma mensagem ao seu pedido de contacto!',2);
    return false;
  }
  return true;
}
// form inscricao //
function validateFormInscricao(form){
var nome = form.nome.value;
var email = form.email.value;

var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

if(nome == "") {
    inlineMsg('nome','Deve inserir um nome!',2);
    return false;
  }
if(email == "") {
    inlineMsg('email','Deve inserir um e-mail!',2);
    return false;
  }
if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />O e-mail que introduziu está incorrecto!',2);
    return false;
  }
  return true;
}
// form inscricao //
function validateFormLogin(form){
var password = form.password.value;
var email = form.email.value;

var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

if(email == "") {
    inlineMsg('email','Deve inserir um e-mail!',2);
    return false;
  }
if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Erro</strong><br />O e-mail que introduziu está incorrecto!',2);
    return false;
  }
if(password == "") {
    inlineMsg('password','Deve inserir a sua password!',2);
    return false;
  }
  return true;
}
// 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 * 3000));
}

// 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 = "../img/msg_arrow.gif"; 
}