
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(theForm)
{	
	myOption = -1;
	
	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.codigopostal.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Código Postal\".");
	theForm.codigopostal.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.telefone.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Telefone\".");
	theForm.telefone.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.quota.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Quota\".");
	theForm.quota.focus();
	return (false);
	}
	
	if (theForm.mes.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Mês\".");
	theForm.mes.focus();
	return (false);
	}
	
	if (theForm.contribuinte.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Contribuinte\".");
	theForm.contribuinte.focus();
	return (false);
	}

	for (i=theform.oferta.length-1; i > -1; i--)
	{
		if (theform.oferta[i].checked)
		{
		myOption = i; i = -1;
		}
	}

	if (myOption == -1)
	{
	alert("Por favor seleccione uma oferta");
	return false;
	}
	
return (true);
}



function FormValidator_login(theForm)
{

	if (theForm.email1.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Email\".");
	theForm.email1.focus();
	return (false);
	}
	
	if (theForm.email1.value.length >0)
	{
	if (!isEmailAddr(theForm.email1.value))
	{
	alert("Por favor escreva um endereço de email completo no formato oseunome@oseudominio.com");
	theForm.email1.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_newsletter(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);
	}
	}
	
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.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.password.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Password\".");
	theForm.password.focus();
	return (false);
	}
	
	if(theForm.condicoes.checked == false)
	{
	alert("Por favor leia e aceite as condicoes gerais"); 
	return (false);
	} 

return (true);
}



function FormValidator_encomendar(theform)
{
myOption = -1;
myOption2 = -1;

for (i=theform.modalidade.length-1; i > -1; i--)
{
	if (theform.modalidade[i].checked)
	{
	myOption = i; i = -1;
	}
}

for (i=theform.expedicao.length-1; i > -1; i--)
{
	if (theform.expedicao[i].checked)
	{
	myOption2 = i; i = -1;
	}
}

if (myOption == -1)
{
alert("Por favor seleccione uma modalidade de pagamento");
return false;
}

if (myOption2 == -1)
{
alert("Por favor seleccione um método de envio");
return false;
}

// alert("You selected button number " + myOption
//+ " which has a value of "
//+ thisform.myradiobutton[myOption].value);

// place any other field validations that you require here
//theform.submit(); // this line submits the form after validation
return (true);
}



function FormValidator_encomendar2(theform)
{
	if (theForm.valor.value == "")
	{
	alert("Por favor escreva o valor pretendido.");
	theForm.valor.focus();
	return (false);
	}


	if (theForm.cc_num.value == "")
	{
	alert("Por favor escreva o número do seu cartão de crédito.");
	theForm.cc_num.focus();
	return (false);
	}
	
	if (theForm.mes.value == "")
	{
	alert("Por favor seleccione o mês de validade.");
	theForm.mes.focus();
	return (false);
	}
	
	if (theForm.ano.value == "")
	{
	alert("Por favor seleccione o ano de validade.");
	theForm.ano.focus();
	return (false);
	}
	
	if (theForm.cc_cvc.value == "")
	{
	alert("Por favor escreva os últimos 3 digitos do verso do seu cartão de crédito.");
	theForm.cc_cvc.focus();
	return (false);
	}
	
return (true);	
}



function FormValidator_encomendar3(theform)
{

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

	if (theForm.expedicao_pais.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"País\".");
	theForm.expedicao_pais.focus();
	return (false);
	}
	
	if (theForm.expedicao_telefone.value == "")
	{
	alert("Por favor escreva os seus dados para o campo \"Telefone\".");
	theForm.expedicao_telefone.focus();
	return (false);
	}
	
return (true);	
}



