
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}



function TmlisValido(telemovel)
{
var result = false;
if (telemovel.substring(0,2) == "91") 
{
result = true;
}
if (telemovel.substring(0,2) == "92") 
{
result = true;
}
if (telemovel.substring(0,2) == "93") 
{
result = true;
}
if (telemovel.substring(0,2) == "96") 
{
result = true;
}
return result;				   
}


function Numericos(string)
{
var result = false;
pattern = /[^0-9.,]/g;
    if (string.match(pattern))
    {
    result = false;
    }
    else
    {
    result = true;
    }
return result;
}


function FormValidator_enviar_amigo(theForm)
{
	if (theForm.amigo_nome.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Nome\".");
	theForm.amigo_nome.focus();
	return (false);
	}

	if (theForm.amigo_email.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Email\".");
	theForm.amigo_email.focus();
	return (false);
	}

	if (theForm.amigo_email.value.length > 0)
	{
	if (!isEmailAddr(theForm.amigo_email.value))
	{
	alert("Por favor escreva um endereço de email completo no formato oseunome@oseudominio.com");
	theForm.amigo_email.focus();
	return (false);
	}
	}
	
	if (theForm.amigo_nome_amigo.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Nome do seu amigo\".");
	theForm.amigo_nome_amigo.focus();
	return (false);
	}
	
	if (theForm.amigo_email_amigo.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Email do seu amigo\".");
	theForm.amigo_email_amigo.focus();
	return (false);
	}

	if (theForm.amigo_email_amigo.value.length > 0)
	{
	if (!isEmailAddr(theForm.amigo_email_amigo.value))
	{
	alert("Por favor escreva um endereço de email completo no formato oseunome@oseudominio.com");
	theForm.amigo_email_amigo.focus();
	return (false);
	}
	}
	
	return (true);
}


function FormValidator_login(theForm)
{
	if (theForm.username.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"username\".");
	theForm.username.focus();
	return (false);
	}

	if (theForm.password.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"password\".");
	theForm.password.focus();
	return (false);
	}
	
	return (true);
}



function FormValidator_registo(theForm)
{
	if (theForm.nome.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Nome\".");
	theForm.nome.focus();
	return (false);
	}

	if (theForm.email.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Email\".");
	theForm.email.focus();
	return (false);
	}

	if (theForm.email.value.length > 0)
	{
	if (!isEmailAddr(theForm.email.value))
	{
	alert("Por favor escreva um endereço de email completo no formato oseunome@oseudominio.com");
	theForm.email.focus();
	return (false);
	}
	}


	if (theForm.telemovel.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Telemóvel\".");
	theForm.telemovel.focus();
	return (false);
	}
	
	if (theForm.telemovel.value.length >0)
	{
	if (theForm.telemovel.value.length < 9)
  	{
    alert("Por favor introduza 9 caracteres no campo \"Telemóvel\.");
    theForm.telemovel.focus();
    return (false);
	}
	}
	
	if (!Numericos(theForm.telemovel.value))
	{
	alert("Por favor escreva só caracteres numéricos no campo \"Telemóvel\".");
	theForm.telemovel.focus();
	return (false);
	}
	
	if (!TmlisValido(theForm.telemovel.value))
	{
	alert("Por favor escreva um número de telemóvel válido.");
	theForm.telemovel.focus();
	return (false);
	}
		
	if (theForm.username.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Username\".");
	theForm.username.focus();
	return (false);
	}
	
	if (theForm.password.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Password\".");
	theForm.password.focus();
	return (false);
	}
	
	return (true);
}



function FormValidator_dados_envio(theForm)
{
	if (theForm.nome.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Nome\".");
	theForm.nome.focus();
	return (false);
	}

	if (theForm.morada.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Morada\".");
	theForm.morada.focus();
	return (false);
	}

	if (theForm.codigo_postal.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Código Postal\".");
	theForm.codigo_postal.focus();
	return (false);
	}

	if (theForm.localidade.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Localidade\".");
	theForm.localidade.focus();
	return (false);
	}
	
	if (theForm.telemovel.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Telemóvel\".");
	theForm.telemovel.focus();
	return (false);
	}

	if (theForm.telemovel.value.length >0)
	{
	if (theForm.telemovel.value.length < 9)
  	{
    alert("Por favor introduza 9 caracteres no campo \"Telemóvel\.");
    theForm.telemovel.focus();
    return (false);
	}
	}
	
	if (!Numericos(theForm.telemovel.value))
	{
	alert("Por favor escreva só caracteres numéricos no campo \"Telemóvel\".");
	theForm.telemovel.focus();
	return (false);
	}
	
	if (!TmlisValido(theForm.telemovel.value))
	{
	alert("Por favor escreva um número de telemóvel válido.");
	theForm.telemovel.focus();
	return (false);
	}
	
	if (theForm.email.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"email\".");
	theForm.email.focus();
	return (false);
	}
	
	if (theForm.email.value.length > 0)
	{
	if (!isEmailAddr(theForm.email.value))
	{
	alert("Por favor escreva um endereço de email completo no formato oseunome@oseudominio.com");
	theForm.email.focus();
	return (false);
	}
	}
	
	if (theForm.facturacao_nome.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Nome\".");
	theForm.facturacao_nome.focus();
	return (false);
	}

	if (theForm.facturacao_morada.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Morada\".");
	theForm.facturacao_morada.focus();
	return (false);
	}

	if (theForm.facturacao_codigo_postal.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Código Postal\".");
	theForm.facturacao_codigo_postal.focus();
	return (false);
	}

	if (theForm.facturacao_localidade.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Localidade\".");
	theForm.facturacao_localidade.focus();
	return (false);
	}
	
	if (theForm.facturacao_telemovel.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Telemóvel\".");
	theForm.facturacao_telemovel.focus();
	return (false);
	}
	
	if (theForm.facturacao_telemovel.value.length >0)
	{
	if (theForm.facturacao_telemovel.value.length < 9)
  	{
    alert("Por favor introduza 9 caracteres no campo \"Telemóvel\.");
    theForm.facturacao_telemovel.focus();
    return (false);
	}
	}
	
	if (!Numericos(theForm.facturacao_telemovel.value))
	{
	alert("Por favor escreva só caracteres numéricos no campo \"Telemóvel\".");
	theForm.facturacao_telemovel.focus();
	return (false);
	}
	
	if (!TmlisValido(theForm.facturacao_telemovel.value))
	{
	alert("Por favor escreva um número de telemóvel válido.");
	theForm.facturacao_telemovel.focus();
	return (false);
	}
	
	if (theForm.facturacao_contribuinte.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Contribuinte\".");
	theForm.facturacao_contribuinte.focus();
	return (false);
	}
		
	return (true);
}




 

    

function checkRadios()
{
 var el = document.forms[0].elements;
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
    alert("Please choose an answer for "+el[i].name+".");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 


function FormValidator_pesquisa(theForm)
{
	if (theForm.pesquisa.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Pesquisa\".");
	theForm.pesquisa.focus();
	return (false);
	}
		
	return (true);
}
