/*-------------------------------------------------------------------------------------------------
	PERMITE SOMENTE NUMEROS
-------------------------------------------------------------------------------------------------*/

function soNumeros(e)
{
    var tecla = (window.event) ? event.keyCode : e.which;

	if((tecla > 47 && tecla < 58)) 
    {
		return true;
    }
    else
	{
		if (tecla != 8 && tecla != 46 && tecla != 16 && tecla != 91 && tecla != 92) 
		{
			return false;
		}
		else
		{
			return true;
		}
    }
}

/*-------------------------------------------------------------------------------------------------
	VERIFICA SE O VALOR DO CAMPO É NUMÉRICO
-------------------------------------------------------------------------------------------------*/

function verificaNumero(f, c)
{
	if (isNaN(document[f][c].value))
	{
		alert("O valor digitado é inválido. Neste campo é permitido somente números.");
		document[f][c].value = "";
	}
}


/*-------------------------------------------------------------------------------------------------
	VERIFICA O TIPO DE DIGITOS
-------------------------------------------------------------------------------------------------*/

function IsNumeric(sText,sDigitos)
{
	var ValidChars = sDigitos;
	var IsNumber   =  true;
	var Char;
	
	for (i=0; i<sText.length && IsNumber == true; i++) 
	{ 
	Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

/*-------------------------------------------------------------------------------------------------
	VALIDA EMAIL
-------------------------------------------------------------------------------------------------*/

function checkMail(mail)
{
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
   
    if(typeof(mail) == "string")
	{
        if(er.test(mail))
		{ 
			return true; 
		}
    }
	else if(typeof(mail) == "object")
	{
        if(er.test(mail.value))
		{
           return true;
        }
    }
	else
	{
        return false;
    }
}

/*-------------------------------------------------------------------------------------------------
	VALIDA DATA
-------------------------------------------------------------------------------------------------*/

function isDate(day, month, year) 
{
	//IsDate(29, 2, 2005)
	//IsDate(29, 2, 2004)
	var date   = new Date();
	var blnRet = false;
	var blnDay;
	var blnMonth;
	var blnYear;

	date.setFullYear(year, month -1, day);

	blnDay   = (date.getDate()      == day);
	blnMonth = (date.getMonth()     == month -1);
	blnYear  = (date.getFullYear()  == year);

	if (blnDay && blnMonth && blnYear)
	blnRet = true;

	return blnRet;
}

/*-------------------------------------------------------------------------------------------------
	VALIDA MATRICULA
-------------------------------------------------------------------------------------------------*/

function validaMatricula()
{
	with(document.matricula)
	{
		if (tipo.value == "Teórico")
		{
			if (curso.value == "")
			{
				alert("Selecione o curso que deseja se matricular.");
				curso.focus();
				return;
			}
		}
		
		if (tipo.value == "")
		{
			alert("Selecione o tipo de  curso que deseja se matricular.");
			tipo.focus();
			return;
		}
		
		if (tipo.value == "Teórico")
		{
			if (pgto.value == "")
			{
				alert("Selecione a forma de pagamento.");
				pgto.focus();
				return;	
			}
			
			if (disponivel.value == "N")
			{
				alert("Não é possível completar a inscrição pois no curso selecionado não há turmas disponíveis.");
				disponivel.focus();
				return;	
			}
			
			if (disponivel.value == "S")
			{
				var marcado  = 0;
				var resposta = "";
				
				for (i=0; i<elements.length; i++)
				{
					 var campo = elements[i];
					 var radio = campo.type;
					 
					 if (radio == "radio")
					 {
						 if (campo.checked)
						 {
							 marcado = 1;
							 resposta = campo.value;
						 }
					 }
				}
				
				if (marcado == 0)
				{
					alert("Você selecionar uma turma para completar a inscrição.");
					return false;
				}
			}
		}
		
		if (nome_pista.value == "")
		{
			alert("Preencha um nome de pista.");
			nome_pista.focus();
			return;
		}
		
		if (lic_tipo.value != "")
		{
			if (lic_numero.value == "")
			{
				alert("Preencha o número de sua licença.");
				lic_numero.focus();
				return;	
			}
		}
		
		if (nome.value == "")
		{
			alert("Preencha seu nome completo.");
			nome.focus();
			return;
		}
		
		if (nascimento.value == "")
		{
			alert("Preencha sua data de nascimento.");
			nascimento.focus();
			return;
		}
		
		if(!isDate(nascimento.value.substr(0,2), nascimento.value.substr(3,2), nascimento.value.substr(6,4)))
		{
			alert('A data de nascimento ('+nascimento.value.substr(0,2)+'/'+nascimento.value.substr(3,2)+'/'+nascimento.value.substr(6,4)+') fornecida é inválida.');
			nascimento.focus();
			return;
		}
		
		if (sexo.value == "")
		{
			alert("Selecione seu gênero.");
			sexo.focus();
			return;
		}
		
		if (nacionalidade.value == "")
		{
			alert("Preencha sua nacionalidade.");
			nacionalidade.focus();
			return;
		}
		
		if (estado_civil.value == "")
		{
			alert("Selecione seu estado civil.");
			estado_civil.focus();
			return;
		}
		
		if (email.value == "" || email.value.indexOf("@") == -1 || email.value.indexOf(".") == -1)
		{
			alert("Preencha seu e-mail corretamente.");
			email.focus();
			return;
		}
		
		if(!checkMail(email))
		{
			alert('E-mail inválido.');
			email.focus();
			return;
		}
		
		if (escolaridade.value == "")
		{
			alert("Selecione sua escolaridade.");
			escolaridade.focus();
			return;
		}

		if (cpf.value == "")
		{
			alert("Preencha seu CPF.");
			cpf.focus();
			return;
		}
		
		if (!isCpf(cpf.value))
		{
			alert("CPF digitado é inválido.");
			cpf.focus();
			return;
		}
		
		if (rg.value == "")
		{
			alert("Preencha seu RG.");
			rg.focus();
			return;
		}
		
		if (emissao.value == "")
		{
			alert("Preencha a data de emissão de seu rg.");
			emissao.focus();
			return;
		}
		
		if(!isDate(emissao.value.substr(0,2), emissao.value.substr(3,2), emissao.value.substr(6,4)))
		{
			alert('A data de emissão de seu rg ('+emissao.value.substr(0,2)+'/'+emissao.value.substr(3,2)+'/'+emissao.value.substr(6,4)+') fornecida é inválida.');
			emissao.focus();
			return;
		}
		
		if (orgao_exp.value == "")
		{
			alert("Preencha o órgão expedidor de sua carteira de identidade.");
			orgao_exp.focus();
			return;
		}
		
		/*if (militar_numero.value == "")
		{
			alert("Preencha o número de seu certificado reservista.");
			militar_numero.focus();
			return;
		}
		
		if (militar_orgao.value == "")
		{
			alert("Preencha o órgão de seu certificado reservista.");
			militar_orgao.focus();
			return;
		}
		
		if (militar_categoria.value == "")
		{
			alert("Preencha a categoria de seu certificado reservista.");
			militar_categoria.focus();
			return;
		}
		
		if (militar_estado.value == "")
		{
			alert("Selecione o estado de seu certificado reservista.");
			militar_estado.focus();
			return;
		}*/
		
		if (ccf_classe.value == "")
		{
			alert("Preencha a classe do seu certificado de capacidade física.");
			ccf_classe.focus();
			return;
		}
		
		if (ccf_orgao.value == "")
		{
			alert("Preencha o órgão do seu certificado de capacidade física.");
			ccf_orgao.focus();
			return;
		}
		
		if (ccf_validade.value == "")
		{
			alert("Preencha a validade do seu certificado de capacidade física.");
			ccf_validade.focus();
			return;
		}
		
		if(!isDate(ccf_validade.value.substr(0,2), ccf_validade.value.substr(3,2), ccf_validade.value.substr(6,4)))
		{
			alert('A data de validade ('+ccf_validade.value.substr(0,2)+'/'+ccf_validade.value.substr(3,2)+'/'+ccf_validade.value.substr(6,4)+') fornecida é inválida.');
			ccf_validade.focus();
			return;
		}
		
		/*if (titulo_eleitor.value == "")
		{
			alert("Preencha o nº do seu título de eleitor.");
			titulo_eleitor.focus();
			return;
		}
		
		if (titulo_zona.value == "")
		{
			alert("Preencha a zona de votação do seu título de eleitor.");
			titulo_zona.focus();
			return;
		}
		
		if (titulo_estado.value == "")
		{
			alert("Selecione o estado onde vota.");
			titulo_estado.focus();
			return;
		}*/
		
		if (endereco.value == "")
		{
			alert("Preencha seu endereço residencial.");
			endereco.focus();
			return;
		}
		
		if (numero.value == "")
		{
			alert("Preencha o número de sua residencia.");
			numero.focus();
			return;
		}
		
		if (bairro.value == "")
		{
			alert("Preencha o bairro.");
			bairro.focus();
			return;
		}
		
		if (cep.value == "")
		{
			alert("Preencha o cep de sua rua.");
			cep.focus();
			return;
		}
		
		if (estado.value == "")
		{
			alert("Selecione o estado onde reside.");
			estado.focus();
			return;
		}
		
		if (cidade.value == "")
		{
			alert("Selecione a cidade onde reside.");
			cidade.focus();
			return;
		}

		if (ddd.value == "")
		{
			alert("Preencha o código de área de sua região.");
			ddd.focus();
			return;
		}
		
		if (telefone.value == "")
		{
			alert("Preencha seu telefone de contato.");
			telefone.focus();
			return;
		}
		
		if (acordo.checked == false)
		{
			alert("Você deve concordar com as condições previstas no regulamento da escola.");
			acordo.focus();
			return;
		}
		
		if (contrato.checked == false)
		{
			alert("Você deve concordar com as condições previstas no contrato de compra de horas de vôo da escola.");
			contrato.focus();
			return;
		}

		target = "_self";
		action = "acoes.php?acao=matricula";
		submit();
	}
}

/*-------------------------------------------------------------------------------------------------
	ENVIA CONTRATO
-------------------------------------------------------------------------------------------------*/

function enviaContrato()
{
	with(document.matricula)
	{
		if (nome.value == "" || cpf.value == "" || rg.value == "" || estado_civil.value == "" || nacionalidade.value == "" || estado.value == "" || cidade.value == "")
		{
			alert("Para emitir o contrato é necessário que o formulário de matrícula esteja preenchido corretamente.");
			return;
		}
		
		target = "_blank";
		action = "contrato.php";
		submit();
	}
}


