function validaEmail(campo_email)
{

var email = campo_email.value;
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return true;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function valida_alphanumerico(objValue)
{ 

return true;

  var charpos = objValue.value.search("[^A-Za-z0-9]"); 
  if(objValue.value.length > 0 &&  charpos >= 0) 
  {
  return false;
  }
  return true;
}//if

function valida_alpha(objValue)
{
	return true;
	
  var charpos = objValue.value.search("[^A-Za-z]"); 
  if(objValue.value.length > 0 &&  charpos >= 0) 
  {
  return false;
  }
  return true;
}//if  

function valida_require(campo){
if(campo.value.length <= 0){return false;} else {return true;} 
}


function valida_formulario(){
   var nome = document.getElementById('nome');
   var morada = document.getElementById('morada');
   var email = document.getElementById('email');
   var telefone = document.getElementById('telefone');
   var assunto = document.getElementById('assunto');

var cor_erro = '#ffc8a4';
var erros = false;

if((!valida_alpha(nome)) || (!valida_require(nome))){nome.style.background = cor_erro; nome.focus(); erros=true;} else{nome.style.background = '';}

if(!valida_alphanumerico(morada)){morada.style.background = cor_erro; morada.focus(); erros=true;} else{morada.style.background = '';}

if((!validaEmail(email)) ||(!valida_require(email))){email.style.background = cor_erro; email.focus(); erros=true;} else{email.style.background = '';}

if((!valida_require(telefone))){telefone.style.background = cor_erro; telefone.focus(); erros=true;} else{telefone.style.background = '';}

if((!valida_require(assunto))){assunto.style.background = cor_erro; assunto.focus(); erros=true;} else{assunto.style.background = '';}


if(erros == false){
	document.formulario_contacto.submit();} else {window.alert("Foram encontrados erros.\nCorriga os campos assinalados antes de prosseguir.");}
}
