﻿function data(checkStr,checkOK) {
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length) {
			return (false);
			break;
		}
	}
	return (true);
}	
//Valida formulário de contacto individual
function ValidaContactoIndividual(theForm,lg){
	if (theForm.Nome.value == "") {
		if (lg == "1")
			alert("O campo \"Nome\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name\" field.");    
	    theForm.Nome.focus();		
	    return (false);
	}
	
	if (theForm.Telefone.value == "") {
		if (theForm.Telemovel.value == ""){
			if (lg == "1")
				alert("O campo \"Telefone\" ou \"Telemóvel\" não pode ser nulo.");
			if (lg == "2")
				alert("Please enter a value for the \"Phone\" or \"Mobile\"  field.");    
			theForm.Telefone.focus();		
			return (false);
		}
	}

	if (theForm.Telefone.value != ""){
		if (!data(theForm.Telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.Telefone.focus();
			return (false);
		}
	}
	
	if (theForm.Telemovel.value != ""){
		if (!data(theForm.Telemovel.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telemóvel\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Mobil\" field.");    
			theForm.Telemovel.focus();
			return (false);
		}
	}
	
	//Valida o campo "Email"
	var checkemail = "@.";
	var checkStr = theForm.email.value;
	var emailValid = false;
	var emailAt = false;
	var emailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++) {
			if (ch == checkemail.charAt(j) && ch == "@")
			emailAt = true;
			if (ch == checkemail.charAt(j) && ch == ".")
			emailPeriod = true;
			if (emailAt && emailPeriod)
			break;
			if (j == checkemail.length)
			break;
		}
		if (emailAt && emailPeriod) {
			emailValid = true
			break;
		}
	}
	
	//Endereço de email incorrecto
	if (!emailValid && (theForm.email.value != "")) {
		if (lg == "1")
			alert("Endereço de \"e-mail\" inválido.");
		if (lg == "2")
			alert("Invalid \"e-mail\".");    
	    theForm.email.focus();		
	    return (false);
	}	
	
	if (theForm.Local.value == "") {
		if (lg == "1")
			alert("O campo \"Local\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Place\" field.");    
	    theForm.Local.focus();		
	    return (false);
	}	
	
	if (theForm.Nvaos.value != ""){
		if (!data(theForm.Nvaos.value,"0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Nº de vãos\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Nº of vain\" field.");    
			theForm.Nvaos.focus();
			return (false);
		}
	}
	
	return (true);
}

//Valida formulário de contacto empresa
function ValidaContactoEmpresa(theForm,lg){
	if (theForm.CodPostal.value != ""){
		if (!data(theForm.CodPostal.value,"-0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Código Postal\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Postal Code\" field.");    
			theForm.CodPostal.focus();
			return (false);
		}
	}	
	
	if (theForm.Telefone.value != ""){
		if (!data(theForm.Telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.Telefone.focus();
			return (false);
		}
	}
	
	if (theForm.Fax.value != ""){
		if (!data(theForm.Fax.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Fax\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Fax\" field.");    
			theForm.Fax.focus();
			return (false);
		}
	}
	
	//Valida o campo "Email"
	var checkemail = "@.";
	var checkStr = theForm.email.value;
	var emailValid = false;
	var emailAt = false;
	var emailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++) {
			if (ch == checkemail.charAt(j) && ch == "@")
			emailAt = true;
			if (ch == checkemail.charAt(j) && ch == ".")
			emailPeriod = true;
			if (emailAt && emailPeriod)
			break;
			if (j == checkemail.length)
			break;
		}
		if (emailAt && emailPeriod) {
			emailValid = true
			break;
		}
	}
	
	//Endereço de email incorrecto
	if (!emailValid && (theForm.email.value != "")) {
		if (lg == "1")
			alert("Endereço de \"e-mail\" inválido.");
		if (lg == "2")
			alert("Invalid \"e-mail\".");    
	    theForm.email.focus();		
	    return (false);
	}		
	return (true);
}

//Valida formulário de recrutamento
function ValidaRecrutamento(theForm,lg){
	if (theForm.Nome.value == "") {
		if (lg == "1")
			alert("O campo \"Nome\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name\" field.");    
	    theForm.Nome.focus();		
	    return (false);
	}
	
	//Valida o campo "Email"
	var checkemail = "@.";
	var checkStr = theForm.email.value;
	var emailValid = false;
	var emailAt = false;
	var emailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++) {
			if (ch == checkemail.charAt(j) && ch == "@")
			emailAt = true;
			if (ch == checkemail.charAt(j) && ch == ".")
			emailPeriod = true;
			if (emailAt && emailPeriod)
			break;
			if (j == checkemail.length)
			break;
		}
		if (emailAt && emailPeriod) {
			emailValid = true
			break;
		}
	}
	
	if (theForm.email.value == "") {
		if (lg == "1")
			alert("O campo \"email\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"email\" field.");    
	    theForm.email.focus();		
	    return (false);
	}	
	
	//Endereço de email incorrecto
	if (!emailValid && (theForm.email.value != "")) {
		if (lg == "1")
			alert("Endereço de \"e-mail\" inválido.");
		if (lg == "2")
			alert("Invalid \"e-mail\".");    
	    theForm.email.focus();		
	    return (false);
	}	
	
	if (theForm.CodPostal.value != ""){
		if (!data(theForm.CodPostal.value,"-0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Código Postal\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Postal Code\" field.");    
			theForm.CodPostal.focus();
			return (false);
		}
	}		
	
	if (theForm.Telefone.value == "") {
		if (theForm.Telemovel.value == ""){
			if (lg == "1")
				alert("O campo \"Telefone\" ou \"Telemóvel\" não pode ser nulo.");
			if (lg == "2")
				alert("Please enter a value for the \"Phone\" or \"Mobile\"  field.");    
			theForm.Telefone.focus();		
			return (false);
		}
	}
	
	if (theForm.Telefone.value != ""){
		if (!data(theForm.Telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.Telefone.focus();
			return (false);
		}
	}
	
	if (theForm.Telemovel.value != ""){
		if (!data(theForm.Telemovel.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telemóvel\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Mobil\" field.");    
			theForm.Telemovel.focus();
			return (false);
		}
	}
	
	if (theForm.Data_Nascimento.value == "") {
		if (lg == "1")
			alert("O campo \"Data de Nascimento\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Date of Birth\" field.");    
	    theForm.Data_Nascimento.focus();		
	    return (false);
	}	
	
	if (theForm.Idade.value != ""){
		if (!data(theForm.Idade.value,"0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Idade\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Age\" field.");    
			theForm.Idade.focus();
			return (false);
		}
	}
	
	if (theForm.Nfilhos.value != ""){
		if (!data(theForm.Nfilhos.value,"0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Nº de filhos\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Nº of children\" field.");    
			theForm.Nfilhos.focus();
			return (false);
		}
	}
	
	if (theForm.BI.value != ""){
		if (!data(theForm.BI.value,"0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"BI\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"ID Card nº\" field.");    
			theForm.BI.focus();
			return (false);
		}
	}		
	
	if (theForm.NContribuinte.value != ""){
		if (lg == "1"){
			if (!validaContribuinte(theForm.NContribuinte)) return false;		
		}
		if (lg == "2"){
			if (!data(theForm.NContribuinte.value,"0123456789")){
				alert("Please enter only numeric characters in the \"Tax nº\" field.");    
				theForm.NContribuinte.focus();
				return (false);
			}
		}	
	}
	
	if (theForm.NBeneficiario.value != ""){
		if (!data(theForm.NBeneficiario.value,"0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Nº Beneficiário\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Social Service nº\" field.");    
			theForm.NBeneficiario.focus();
			return (false);
		}
	}
	
	if (theForm.Habilitacoes_Literarias.value == "") {
		if (lg == "1")
			alert("O campo \"Habilitações Literárias\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Education\" field.");    
	    theForm.Habilitacoes_Literarias.focus();		
	    return (false);
	}	
	
	if (theForm.Disponibilidade){
		var radioSelected = false;
		for (i = 0;  i < theForm.Disponibilidade.length;  i++){
			if (theForm.Disponibilidade[i].checked)
			radioSelected = true;
		}
		if (!radioSelected){
			if (lg == "1")
				alert("O campo \"Disponibilidade\" não pode ser nulo.");
			if (lg == "2")
				alert("Please enter a value for the \"Availability\" field.");    
				return (false);
		}
	}
	
	if (theForm.Disponibilidade_Turnos){
		var radioSelected = false;
		for (i = 0;  i < theForm.Disponibilidade_Turnos.length;  i++){
			if (theForm.Disponibilidade_Turnos[i].checked)
			radioSelected = true;
		}
		if (!radioSelected){
			if (lg == "1")
				alert("O campo \"Disponibilidade para trabalhar por turnos\" não pode ser nulo.");
			if (lg == "2")
				alert("Please enter a value for the \"Availability for shift work\" field.");    
				return (false);
		}
	}
	
	return (true);
}

//Valida formulário do projecto
function ValidaProjecto(theForm,lg){
	if (theForm.Companhia.value == "") {
		if (lg == "1")
			alert("O campo \"Nome da Companhia\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name of Company\" field.");    
	    theForm.Companhia.focus();		
	    return (false);
	}
	
	if (theForm.Contacto.value == "") {
		if (lg == "1")
			alert("O campo \"Nome do Contacto\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Contact name\" field.");    
	    theForm.Contacto.focus();		
	    return (false);
	}
	
	if (theForm.Morada.value == "") {
		if (lg == "1")
			alert("O campo \"Morada\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Address\" field.");    
	    theForm.Morada.focus();		
	    return (false);
	}
	if (theForm.CodPostal.value == "") {
		if (lg == "1")
			alert("O campo \"Código Postal\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Postal Code\" field.");    
		theForm.CodPostal.focus();		
		return (false);
	}
	
	if (theForm.CodPostal.value != ""){
		if (!data(theForm.CodPostal.value,"-0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Código Postal\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Postal Code\" field.");    
			theForm.CodPostal.focus();
			return (false);
		}
	}	
	
	if (theForm.Localidade.value == "") {
		if (lg == "1")
			alert("O campo \"Localidade\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Postal Code\" field.");    
	    theForm.Localidade.focus();		
	    return (false);
	}
	
	if (theForm.Telefone.value == "") {
		if (lg == "1")
			alert("O campo \"Telefone\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Phone\" field.");    
		theForm.Telefone.focus();		
		return (false);
	}
	
	if (theForm.Telefone.value != ""){
		if (!data(theForm.Telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.Telefone.focus();
			return (false);
		}
	}	
	
	if (theForm.Fax.value != ""){
		if (!data(theForm.Fax.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Fax\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Fax\" field.");    
			theForm.Fax.focus();
			return (false);
		}
	}	
	
	if (theForm.Telemovel.value != ""){
		if (!data(theForm.Telemovel.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telemóvel\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Mobil\" field.");    
			theForm.Telemovel.focus();
			return (false);
		}
	}
	
	//Valida o campo "Email"
	var checkemail = "@.";
	var checkStr = theForm.email.value;
	var emailValid = false;
	var emailAt = false;
	var emailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++) {
			if (ch == checkemail.charAt(j) && ch == "@")
			emailAt = true;
			if (ch == checkemail.charAt(j) && ch == ".")
			emailPeriod = true;
			if (emailAt && emailPeriod)
			break;
			if (j == checkemail.length)
			break;
		}
		if (emailAt && emailPeriod) {
			emailValid = true
			break;
		}
	}
	
	//Endereço de email incorrecto
	if (!emailValid && (theForm.email.value != "")) {
		if (lg == "1")
			alert("Endereço de \"e-mail\" inválido.");
		if (lg == "2")
			alert("Invalid \"e-mail\".");    
	    theForm.email.focus();		
	    return (false);
	}		
	
	if (theForm.Num_Colaboradores.value != ""){
		if (!data(theForm.Num_Colaboradores.value,"0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Nº Colaboradores efectivos\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Nº of permanent staff\" field.");    
			theForm.Num_Colaboradores.focus();
			return (false);
		}
	}	
	if (theForm.NContribuinte.value != ""){
		if (lg == "1"){
			if (!validaContribuinte(theForm.NContribuinte)) return false;		
		}
		if (lg == "2"){
			if (!data(theForm.NContribuinte.value,"0123456789")){
				alert("Please enter only numeric characters in the \"Tax nº\" field.");    
				theForm.NContribuinte.focus();
				return (false);
			}
		}	
	}
	return (true);
}

//Valida formulário de pedido de newsletter
function ValidaNewsletter(theForm,lg){
	if (theForm.Nome.value == "") {
		if (lg == "1")
			alert("O campo \"Nome\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name\" field.");    
	    theForm.Nome.focus();		
	    return (false);
	}
	
	//Valida o campo "Email"
	var checkemail = "@.";
	var checkStr = theForm.email.value;
	var emailValid = false;
	var emailAt = false;
	var emailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++) {
			if (ch == checkemail.charAt(j) && ch == "@")
			emailAt = true;
			if (ch == checkemail.charAt(j) && ch == ".")
			emailPeriod = true;
			if (emailAt && emailPeriod)
			break;
			if (j == checkemail.length)
			break;
		}
		if (emailAt && emailPeriod) {
			emailValid = true
			break;
		}
	}
	
	//Endereço de email incorrecto
	if (!emailValid && (theForm.email.value != "")) {
		if (lg == "1")
			alert("Endereço de \"e-mail\" inválido.");
		if (lg == "2")
			alert("Invalid \"e-mail\".");    
	    theForm.email.focus();		
	    return (false);
	}	
	
	if (theForm.email.value == "") {
		if (lg == "1")
			alert("O campo \"e-mail\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"e-mail\" field.");    
	    theForm.email.focus();		
	    return (false);
	}	
	return (true);
}

//Valida formulário de pedido de software
function ValidaPedidoSoftware(theForm,lg){
	if (theForm.Empresa.value == "") {
		if (lg == "1")
			alert("O campo \"Empresa\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Company\" field.");    
	    theForm.Empresa.focus();		
	    return (false);
	}
	
	if (theForm.Nome.value == "") {
		if (lg == "1")
			alert("O campo \"Nome\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name\" field.");    
	    theForm.Nome.focus();		
	    return (false);
	}
	
	if (theForm.Morada.value == "") {
		if (lg == "1")
			alert("O campo \"Morada\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Address\" field.");    
	    theForm.Morada.focus();		
	    return (false);
	}
	if (theForm.CodPostal.value == "") {
		if (lg == "1")
			alert("O campo \"Código Postal\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Postal Code\" field.");    
		theForm.CodPostal.focus();		
		return (false);
	}
	
	if (theForm.CodPostal.value != ""){
		if (!data(theForm.CodPostal.value,"-0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Código Postal\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Postal Code\" field.");    
			theForm.CodPostal.focus();
			return (false);
		}
	}	
	
	if (theForm.Localidade.value == "") {
		if (lg == "1")
			alert("O campo \"Localidade\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Postal Code\" field.");    
	    theForm.Localidade.focus();		
	    return (false);
	}
	
	if (theForm.Telefone.value == "") {
		if (lg == "1")
			alert("O campo \"Telefone\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Phone\" field.");    
		theForm.Telefone.focus();		
		return (false);
	}
	
	if (theForm.Telefone.value != ""){
		if (!data(theForm.Telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.Telefone.focus();
			return (false);
		}
	}		
	
	if (theForm.Fax.value != ""){
		if (!data(theForm.Fax.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Fax\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Fax\" field.");    
			theForm.Fax.focus();
			return (false);
		}
	}	
	
	if (theForm.Telemovel.value != ""){
		if (!data(theForm.Telemovel.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telemóvel\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Mobil\" field.");    
			theForm.Telemovel.focus();
			return (false);
		}
	}	
	
	//Valida o campo "Email"
	var checkemail = "@.";
	var checkStr = theForm.email.value;
	var emailValid = false;
	var emailAt = false;
	var emailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkemail.length;  j++) {
			if (ch == checkemail.charAt(j) && ch == "@")
			emailAt = true;
			if (ch == checkemail.charAt(j) && ch == ".")
			emailPeriod = true;
			if (emailAt && emailPeriod)
			break;
			if (j == checkemail.length)
			break;
		}
		if (emailAt && emailPeriod) {
			emailValid = true
			break;
		}
	}
	
	//Endereço de email incorrecto
	if (!emailValid && (theForm.email.value != "")) {
		if (lg == "1")
			alert("Endereço de \"e-mail\" inválido.");
		if (lg == "2")
			alert("Invalid \"e-mail\".");    
	    theForm.email.focus();		
	    return (false);
	}		
	
	return (true);
}

//Valida formulário de registo de utilizador
function pcusers_confirma_registo(theForm,lg) {
	if ((theForm.nome.value == "") ) {
		if (lg == "1")
			alert("O campo \"Nome\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name\" field.");    
		theForm.nome.focus();        
		return (false); 
	}  
	
	if ((theForm.actividade.value == "") ) {
		if (lg == "1")
			alert("O campo \"Actividade\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Activity\" field.");    
		theForm.actividade.focus();        
		return (false); 
	}  
	
	if (theForm.contribuinte.value == "") {
		if (lg == "1")
			alert("O campo \"Contribuinte\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Tax nº\" field.");    
		theForm.contribuinte.focus();    
		return (false);    
	} 

	/*if (theForm.contribuinte.value != ""){
		if (lg == "1"){
			if (!validaContribuinte(theForm.contribuinte)) return false;		
		}
		if (lg == "2"){
			if (!data(theForm.contribuinte.value,"0123456789")){
				alert("Please enter only numeric characters in the \"Tax nº\" field.");    
				theForm.contribuinte.focus();
				return (false);
			}
		}	
	}*/
	
	if (theForm.morada.value == "") {  
		if (lg == "1")
			alert("O campo \"Morada\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Address\" field.");    
		theForm.morada.focus();        
		return (false);    
	}
	
	if (theForm.codpostal.value == "") {  
		if (lg == "1")
			alert("O campo \"Código Postal\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Postal Code\" field.");    
		theForm.codpostal.focus();    
		return (false);    
	}
	
	/*if (theForm.codpostal.value != ""){
		if (!data(theForm.codpostal.value,"-0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Código Postal\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Postal Code\" field.");    
			theForm.codpostal.focus();
			return (false);
		}
	}	*/
	
	if (theForm.localidade.value == "") {  
		if (lg == "1")
			alert("O campo \"Localidade\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Town/City\" field.");    
		theForm.localidade.focus();        
		return (false);    
	}		
	
	if (theForm.telefone.value == "") {  
		if (lg == "1")
			alert("O campo \"Telefone\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Phone\" field.");    
		theForm.telefone.focus();    
		return (false);    
	}
	
	if (theForm.telefone.value != ""){
		if (!data(theForm.telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.telefone.focus();
			return (false);
		}
	}	
	
	if (theForm.fax.value != ""){
		if (!data(theForm.fax.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Fax\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Fax\" field.");    
			theForm.fax.focus();
			return (false);
		}
	}	
	
	if (theForm.mail.value == "")  {
		if (lg == "1")
			alert("O campo \"E-mail\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"E-mail\" field.");    
		theForm.mail.focus();    
		return (false);  
	}	
	
	if (theForm.pass) {  
		if (theForm.pass.value == "") {
			if (lg == "1")
				alert("O campo \"Password\" não pode ser nulo.");
			if (lg == "2")
				alert("Please enter a value for the \"Password\" field.");    
    		theForm.pass.focus();
    		return (false);    
  		}   
		if (theForm.pass.value != theForm.confpass.value) {
			if (lg == "1")
				alert("Password inválida.");
			if (lg == "2")
				alert("Invalid password.");    
    		theForm.pass.value = "";
    		theForm.confpass.value = "";
    		theForm.pass.focus();
    		return (false);
  		}
	}		
	return (true);
}

//Valida formulário de alteração de dados do utilizador
function pcusers_confirma_dados(theForm,lg) {
	if ((theForm.nome.value == "") ) {
		if (lg == "1")
			alert("O campo \"Nome\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Name\" field.");    
		theForm.nome.focus();        
		return (false); 
	}  
	
	if (theForm.contribuinte.value == "") {
		if (lg == "1")
			alert("O campo \"Contribuinte\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Tax nº\" field.");    
		theForm.contribuinte.focus();    
		return (false);    
	}
	
	/*if (theForm.contribuinte.value != ""){
		if (lg == "1"){
			if (!validaContribuinte(theForm.contribuinte)) return false;		
		}
		if (lg == "2"){
			if (!data(theForm.contribuinte.value,"0123456789")){
				alert("Please enter only numeric characters in the \"Tax nº\" field.");    
				theForm.contribuinte.focus();
				return (false);
			}
		}	
	}*/
		
	if (theForm.morada.value == "") {  
		if (lg == "1")
			alert("O campo \"Morada\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Address\" field.");    
		theForm.morada.focus();        
		return (false);    
	}
	
	if (theForm.codpostal.value == "") {  
		if (lg == "1")
			alert("O campo \"Código Postal\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Postal Code\" field.");    
		theForm.codpostal.focus();    
		return (false);    
	}	
	
	/*if (theForm.codpostal.value != ""){
		if (!data(theForm.codpostal.value,"-0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Código Postal\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Postal Code\" field.");    
			theForm.codpostal.focus();
			return (false);
		}
	}	*/
	
	if (theForm.localidade.value == "") {  
		if (lg == "1")
			alert("O campo \"Localidade\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Town/City\" field.");    
		theForm.localidade.focus();        
		return (false);    
	}	
	
	if (theForm.telefone.value == "") {  
		if (lg == "1")
			alert("O campo \"Telefone\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Phone\" field.");    
		theForm.telefone.focus();    
		return (false);    
	}
	
	if (theForm.telefone.value != ""){
		if (!data(theForm.telefone.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Telefone\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Phone\" field.");    
			theForm.telefone.focus();
			return (false);
		}
	}
	
	if (theForm.fax.value != ""){
		if (!data(theForm.fax.value,"+0123456789")){
			if (lg == "1")
				alert("Utilize apenas números para preencher o campo \"Fax\".");
			if (lg == "2")
				alert("Please enter only numeric characters in the \"Fax\" field.");    
			theForm.fax.focus();
			return (false);
		}
	}		
	
	if (theForm.mail.value == "")  {
		if (lg == "1")
			alert("O campo \"E-mail\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"E-mail\" field.");    
		theForm.mail.focus();    
		return (false);  
	}	
	return (true);
}

//Valida login
function ValidaLogin(theForm,lg){
	if (theForm.mail.value == ""){
		if (lg == "1")
			alert("O campo \"E-mail\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"E-mail\" field.");      
		theForm.mail.focus();
		return (false);
	}
	
	if (theForm.mail.value.length > 50){
		if (lg == "1")
			alert("O campo \"E-mail\" não pode ter mais de 50 caracteres.");
		if (lg == "2")
			alert("Please enter at least 50 characters in the \"E-mail\" field.");      
		theForm.mail.focus();
		return (false);
	}
	
	if (theForm.pass.value == ""){
		if (lg == "1")
			alert("O campo \"Password\" não pode ser nulo.");
		if (lg == "2")
			alert("Please enter a value for the \"Password\" field.");      
		theForm.pass.focus();
		return (false);
	}
	
	if (theForm.pass.value.length > 15){
		if (lg == "1")
			alert("O campo \"Password\" não pode ter mais de 15 caracteres.");
		if (lg == "2")
			alert("Please enter at least 15 characters in the \"Password\" field.");      
		theForm.pass.focus();
		return (false);
	}
	
	return (true);
}

//----------------------------------------------------------------------------
//Validação do nº de contribuinte
//----------------------------------------------------------------------------
function validaContribuinte (contribuinte) {
	var nif, slashIdx = contribuinte.value.indexOf("/");
	if (slashIdx == -1) {
	    nif = contribuinte.value;
	} else {
	    nif = contribuinte.value.substring(0, slashIdx);
	}

	if (!isNumeroContribuinte(nif)) {
		alert("Nº de Contribuinte inválido.");
		contribuinte.focus();
		return (false);
	}
	return true;
}

function isNumeroContribuinte(s) {
	if (s.length!=9 || !isPositiveInteger(s)) {
		return false;
	}

	var soma,resto,digi;
    var nif = new Array(9);
  	for (var i=0;i<9;i++) {
    	nif[i] = Number(s.substring(i,i+1));
  	}
  	for (var i=0,soma=0;i<8;i++) {
    	soma += nif[i]*(9-i);
  	}
  	resto = soma%11;
  	digi = 11-resto;
  	if (digi>9) digi=0;
  	return (digi==nif[8]);
}

function isSignedInteger (s){
    if (isEmpty(s)) return false;

    else {
        var startPos = 0;

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isInteger(s.substring(startPos, s.length)))
    }
}

function isPositiveInteger (s){
    if (isEmpty(s)) return false;
    return (isSignedInteger(s) && Number (s) > 0);
}

function isEmpty(s){
	return (s == null || s.length == 0 || trim(s).length == 0)
}

function trim (s) {
	if (s.length == 0) return s;
    return s.replace(/^\s*(.*?)\s*$/,'$1');
}

function isInteger (s){
    if (isEmpty(s)) return false;

	for (var i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}

function isDigit (c){   
	return ((c >= "0") && (c <= "9"))
}