 // Retira espaços da string
function trim(str)
{
    while (str.charAt(0) == " ")
        str = str.substr(1,str.length -1);

    while (str.charAt(str.length-1) == " ")
        str = str.substr(0,str.length-1);

    return str;
}

// Mostra msg de erro
function erro(nm_campo, id_campo)
{
   document.getElementById("erro").className="d_erro";
    document.getElementById("erro").innerHTML= nm_campo;
	document.getElementById(id_campo).focus();
}

// Valida tamanho da string e se ela está vazia
function tamanho(nmCampo, idStr)
{
    var varTexto;

    varTexto = document.getElementById(idStr).value;

    varTexto = trim(varTexto);
    
	if (varTexto.length < 4)
	{
		erro(nmCampo, idStr);
		return false;
	}
	else
	{
        return true;
	}
}

  //----------------------------------------------------------//
 //----- *** Inicia validações a partir desse ponto *** -----//
//----------------------------------------------------------//

// AMBIO - CONFIGURAÇÕES
function verifica_ano()
{
   if (!tamanho('O ano deve ter 4 dígitos', 'CadEscIniAno'))
   {
      return false;
   }
   if (!tamanho('O ano deve ter 4 dígitos', 'CadEscFimAno'))
   {
      return false;
   }
   if (!tamanho('O ano deve ter 4 dígitos', 'CadProfIniAno'))
   {
      return false;
   }
   if (!tamanho('O ano deve ter 4 dígitos', 'CadProfFimAno'))
   {
      return false;
   }

   if (!tamanho('O ano deve ter 4 dígitos', 'Nota1IniAno'))
   {
      return false;
   }
   if (!tamanho('O ano deve ter 4 dígitos', 'Nota1FimAno'))
   {
      return false;
   }

   if (!tamanho('O ano deve ter 4 dígitos', 'Nota2IniAno'))
   {
      return false;
   }
   if (!tamanho('O ano deve ter 4 dígitos', 'Nota2FimAno'))
   {
      return false;
   }

   if (!tamanho('O ano deve ter 4 dígitos', 'Nota3IniAno'))
   {
      return false;
   }
   if (!tamanho('O ano deve ter 4 dígitos', 'Nota3FimAno'))
   {
      return false;
   }
}

// INCLUSÃO DE ESCOLA
function valida_escola_incluir()
	{
		var n;
		n = document.form;
		if(n.txtEscNome.value == '')
		{
			alert('Informe um nome de escola!');
			n.txtEscNome.focus();
			return false;
		}

      marcado = -1
   	for (i=0; i<n.optEscTipo.length; i++)
      {
   		if (n.optEscTipo[i].checked)
         {
   			marcado = i
   		}
   	}
   	if (marcado == -1)
      {
   		alert("Selecione o tipo de escola!");
         return false;
   	}
   	
 		if(n.txtEscEndNome.value == '')
		{
			alert('Informe o logradouro da escola!');
			n.txtEscEndNome.focus();
			return false;
		}
		if(n.txtEscEndNum.value == '')
		{
			alert('Informe o número do logradouro da escola!');
			n.txtEscEndNum.focus();
			return false;
		}
		if(n.txtEscCidade.value == '')
		{
			alert('Informe a cidade da escola!');
			n.txtEscCidade.focus();
			return false;
		}
		if(n.txtEscCep.value == '')
		{
			alert('Informe o CEP da escola!');
			n.txtEscCep.focus();
			return false;
		}
		if(n.txtEscTelDdd.value == '')
		{
			alert('Informe o DDD do telefone da escola!');
			n.txtEscTelDdd.focus();
			return false;
		}
		if(n.txtEscTelNum.value == '')
		{
			alert('Informe o número do telefone da escola!');
			n.txtEscTelNum.focus();
			return false;
		}
		if(n.txtEscEmail.value == '')
		{
			alert('Informe o email da escola!');
			n.txtEscEmail.focus();
			return false;
		}
		if(n.txtProfNome.value == '')
		{
			alert('Informe o nome do professor!');
			n.txtProfNome.focus();
			return false;
		}
		if(n.txtProfEndNome.value == '')
		{
			alert('Informe o logradouro do professor!');
			n.txtProfEndNome.focus();
			return false;
		}
		if(n.txtProfEndNum.value == '')
		{
			alert('Informe o número do logradouro do professor!');
			n.txtProfEndNum.focus();
			return false;
		}
		if(n.txtProfCidade.value == '')
		{
			alert('Informe a cidade do professor!');
			n.txtProfCidade.focus();
			return false;
		}
		if(n.txtProfCep.value == '')
		{
			alert('Informe o CEP do professor!');
			n.txtProfCep.focus();
			return false;
		}
		if(n.txtProfTelDdd.value == '')
		{
			alert('Informe o DDD do telefone do professor!');
			n.txtProfTelDdd.focus();
			return false;
		}
		if(n.txtProfTelNum.value == '')
		{
			alert('Informe o telefone do professor!');
			n.txtProfTelNum.focus();
			return false;
		}
		if(n.txtProfEmail.value == '')
		{
			alert('Informe o email do professor!');
			n.txtProfEmail.focus();
			return false;
		}
}


// ALTERAÇÃO DE ESCOLA
function valida_escola_alterar()
	{
		var n;
		n = document.form;
		if(n.txtEscNome.value == '')
		{
			alert('Informe um nome de escola!');
			n.txtEscNome.focus();
			return false;
		}

      marcado = -1
   	for (i=0; i<n.optEscTipo.length; i++)
      {
   		if (n.optEscTipo[i].checked)
         {
   			marcado = i
   		}
   	}
   	if (marcado == -1)
      {
   		alert("Selecione o tipo de escola!");
         return false;
   	}

 		if(n.txtEscEndNome.value == '')
		{
			alert('Informe o logradouro da escola!');
			n.txtEscEndNome.focus();
			return false;
		}
		if(n.txtEscEndNum.value == '')
		{
			alert('Informe o número do logradouro da escola!');
			n.txtEscEndNum.focus();
			return false;
		}
		if(n.txtEscCidade.value == '')
		{
			alert('Informe a cidade da escola!');
			n.txtEscCidade.focus();
			return false;
		}
		if(n.txtEscCep.value == '')
		{
			alert('Informe o CEP da escola!');
			n.txtEscCep.focus();
			return false;
		}
		if(n.txtEscTelDdd.value == '')
		{
			alert('Informe o DDD do telefone da escola!');
			n.txtEscTelDdd.focus();
			return false;
		}
		if(n.txtEscTelNum.value == '')
		{
			alert('Informe o número do telefone da escola!');
			n.txtEscTelNum.focus();
			return false;
		}
		if(n.txtEscEmail.value == '')
		{
			alert('Informe o email da escola!');
			n.txtEscEmail.focus();
			return false;
		}
}


// DADOS DO ALUNO
function valida_aluno_incluir()
	{
		var n;
		n = document.form;
		if(n.txtAlunoNome.value == '')
		{
			alert('Informe o nome do aluno!');
			n.txtAlunoNome.focus();
			return false;
		}

 		if(n.txtAlunoNascDia.value == '')
		{
			alert('Informe o dia do nascimento do aluno!');
			n.txtAlunoNascDia.focus();
			return false;
		}
		if(n.txtAlunoNascMes.value == '')
		{
			alert('Informe o mês de nascimento do aluno!');
			n.txtAlunoNascMes.focus();
			return false;
		}
		if(n.txtAlunoNascAno.value == '')
		{
			alert('Informe o ano de nascimento do aluno!');
			n.txtAlunoNascAno.focus();
			return false;
		}
}

// DADOS DE PROFESSOR
function valida_professor()
	{
		var n;
		n = document.form;
		if(n.txtProfNome.value == '')
		{
			alert('Informe o nome do professor!');
			n.txtProfNome.focus();
			return false;
		}
		if(n.txtProfEndNome.value == '')
		{
			alert('Informe o logradouro do professor!');
			n.txtProfEndNome.focus();
			return false;
		}
		if(n.txtProfEndNum.value == '')
		{
			alert('Informe o número do logradouro do professor!');
			n.txtProfEndNum.focus();
			return false;
		}
		if(n.txtProfCidade.value == '')
		{
			alert('Informe a cidade do professor!');
			n.txtProfCidade.focus();
			return false;
		}
		if(n.txtProfCep.value == '')
		{
			alert('Informe o CEP do professor!');
			n.txtProfCep.focus();
			return false;
		}
		if(n.txtProfTelDdd.value == '')
		{
			alert('Informe o DDD do telefone do professor!');
			n.txtProfTelDdd.focus();
			return false;
		}
		if(n.txtProfTelNum.value == '')
		{
			alert('Informe o telefone do professor!');
			n.txtProfTelNum.focus();
			return false;
		}
		if(n.txtProfEmail.value == '')
		{
			alert('Informe o email do professor!');
			n.txtProfEmail.focus();
			return false;
		}
}

function teste()
{
   cnpj = document.getElementById("txtCadCNPJ").value;
   if (cnpj.length < 14)
   {
      alert('O CNPJ deve ter 14 caracteres!')
      return false;
   }
   else
   {
      return true;
   }
}

