function MudaCampoEnter(event)
{
    Tecla = event.which;
    if(Tecla == null)
        Tecla = event.keyCode;

    if (Tecla == 13)
    {
		objPrimeiro = null
		objAtual = null
		objProximo = null
		
		for (x=0 ; x<=document.forms[0].elements.length - 1; x++) 
		{
			if ((document.forms[0].elements[x].type == "text" && document.forms[0].elements[x].readOnly == false) || document.forms[0].elements[x].type == "password" || (document.forms[0].elements[x].type == "select-one" && document.forms[0].elements[x].disabled == false) || document.forms[0].elements[x].type == "textarea" || document.forms[0].elements[x].type == "checkbox" || document.forms[0].elements[x].type == "button") {
				if (objPrimeiro == null) {
					objPrimeiro = document.forms[0].elements[x];
				}
						
				if (objAtual != null) {
					objProximo = document.forms[0].elements[x];
					break;
				}
				if (event.srcElement.name == document.forms[0].elements[x].name) {
					objAtual = document.forms[0].elements[x];
				}
			}
		}
        event.keyCode = 0;
		if (objProximo != null) {
	        objProximo.focus();
	    }
	    else
	    {
	        objPrimeiro.focus();
	    }
    }
}


function IsEnter(event)
{
    Tecla = event.which;
    
    if(Tecla == null)
        Tecla = event.keyCode;

    if (Tecla == 13)
return true;
}


function submit_novo() {
   document.frm.pAcao.value = 'N';
   document.frm.submit();
}

function submit_novo(id) {
   document.frm.solicitacao_id = id;
   document.frm.pAcao.value = 'N';
   document.frm.submit();
}

function submit_novo_arquivo(id) {
   self.location = 'solicitacao_arquivo_novo.jsp?solicitacao_id=' + id;
}

function submit_novo_arquivo_curso(id) {
   self.location = 'curso_arquivo_novo.jsp?curso_id=' + id;
}

function submit_novo_arquivo_atividade_geral(id) {
   self.location = 'atividade_geral_arquivo_novo.jsp?atividade_id=' + id;
}

function submit_novo_arquivo_atividade_individual(id) {
   self.location = 'atividade_individual_arquivo_novo.jsp?atividade_id=' + id;
}

function novo_registro(url) {
   self.location = url;
}

function pesquisa(url) {
   self.location = url;
}

function excluir_sistema(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
   if (confirm('Deseja realmente remover o sistema?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.sistema_id.value = id;
   document.frm.submit(); }
}

function excluir_perfil(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover o perfil?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.perfil_id.value = id;
   document.frm.submit(); }
}

function excluir_usuario(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover o usuário?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.usuario_id.value = id;
   document.frm.submit(); }
}

function excluir_grupo_usuario(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover o grupo de usuário?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.grupo_usuario_id.value = id;
   document.frm.submit(); }
}

function excluir_cliente(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover o cliente?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.cliente_id.value = id;
   document.frm.submit(); }
}

function excluir_projeto(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover o projeto?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.projeto_id.value = id;
   document.frm.submit(); }
}

function excluir_solicitacao(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover a solicitacao?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.solicitacao_id.value = id;
   document.frm.submit(); }
}

function excluir_curso(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover o curso?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.curso_id.value = id;
   document.frm.submit(); }
}

function excluir_atividade(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover a atividade?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.atividade_id.value = id;
   document.frm.submit(); }
}

function excluir_atividade_arquivo_geral(id, id_ati) {
  alert('Esta operação eliminará o registro do arquivo.');
  if (confirm('Deseja realmente remover o arquivo?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.atividade_arquivo_id.value = id;
   document.frm.atividade_id.value = id_ati;
   document.frm.action = "atividade_geral_arquivo_edita.jsp?descricao=" + escape(document.frm_aux.descricao.value) + "&exclui=" + escape(document.frm.exclui.value) + "&acao=1&atividade_id=" + escape(document.frm.atividade_id.value) + "&atividade_arquivo_id=" + escape(document.frm.atividade_arquivo_id.value);
   document.frm.submit(); }
}

function excluir_atividade_arquivo_individual(id, id_ati) {
  alert('Esta operação eliminará o registro do arquivo.');
  if (confirm('Deseja realmente remover o arquivo?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.atividade_arquivo_id.value = id;
   document.frm.atividade_id.value = id_ati;
   document.frm.action = "atividade_individual_arquivo_edita.jsp?descricao=" + escape(document.frm_aux.descricao.value) + "&exclui=" + escape(document.frm.exclui.value) + "&acao=1&atividade_id=" + escape(document.frm.atividade_id.value) + "&atividade_arquivo_id=" + escape(document.frm.atividade_arquivo_id.value);
   document.frm.submit(); }
}

function excluir_solicitacao_arquivo(id, id_ati) {
  alert('Esta operação eliminará o registro do arquivo.');
  if (confirm('Deseja realmente remover o arquivo?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.solicitacao_arquivo_id.value = id;
   document.frm.solicitacao_id.value = id_ati;
   document.frm.action = "solicitacao_arquivo_edita.jsp?descricao=" + escape(document.frm_aux.descricao.value) + "&exclui=" + escape(document.frm.exclui.value) + "&acao=1&solicitacao_id=" + escape(document.frm.solicitacao_id.value) + "&solicitacao_arquivo_id=" + escape(document.frm.solicitacao_arquivo_id.value);
   document.frm.submit(); }
}

function excluir_curso_arquivo(id, id_ati) {
  alert('Esta operação eliminará o registro da apostila.');
  if (confirm('Deseja realmente remover a apostila?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.curso_arquivo_id.value = id;
   document.frm.curso_id.value = id_ati;
   document.frm.action = "curso_arquivo_edita.jsp?descricao=" + escape(document.frm_aux.descricao.value) + "&exclui=" + escape(document.frm.exclui.value) + "&acao=1&curso_id=" + escape(document.frm.curso_id.value) + "&curso_arquivo_id=" + escape(document.frm.curso_arquivo_id.value);
   document.frm.submit(); }
}

function excluir_agenda(id, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover essa atividade na Agenda?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.agenda_id.value = id;
   document.frm.submit(); }
}

function excluir_aula(id,data,hora, msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover essa aula?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.curso_id.value = id;
   document.frm.data_ini.value = data;
   document.frm.hora_ini.value = hora;
   document.frm.submit(); }
}

function excluir_aluno(id,aluno,msg) {
  if (msg!="")
  {
     alert(msg);
  }
  if (confirm('Deseja realmente remover essa matrícula?\n\nOK = remover\nCancel = ficar nesta página')) {
   document.frm.exclui.value = '1';
   document.frm.curso_id.value = id;
   document.frm.aluno_id.value = aluno;
   document.frm.submit(); }
}


function FormataMascaraHora(obj, evento)
{
	var tecla = evento.keyCode;
        var tammax = 5;
        var pos = 2;
	vr = obj.value;
	vr = vr.replace( ":", "" );
	vr = vr.replace( " ", "" );
	tam = vr.length ;

	if (tam < tammax && tecla != 8) tam = vr.length + 1;

        if (tam < tammax)
	{
		if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
		{
			if ( tam <= 2 ) obj.value = vr;
			if ( tam > (pos+1) && tam <= tammax ) obj.value = vr.substr( 0, tam - pos ) + ':' + vr.substr( tam - pos, tam );
			else if ( tam == 3 ) obj.value = vr.substr( 0, 2 ) + ':' + vr.substr( 2, 1 );
		}
	}

}

function FormataMascaraData(obj,evento)
{
	var tecla = evento.keyCode;
	vr = obj.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;
	if ( tecla != 9 && tecla != 8 )
	{
		if ( tam > 2 && tam < 5 )
			obj.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			obj.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
	
}

function MudaFoco (campo,tammax,evento)
{
	var tecla = evento.keyCode;
	vr = document.frm[campo].value;
	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108 )
	{
		document.frm[campo].value = vr.substr( 0, vr.length - 1 );
	}
	else
	{
	 	vr = vr.replace( "-", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( ":", "" );
	 	tam = vr.length;	
	 	if (tecla != 0 && tecla != 9 && tecla != 16 )
		{
			if ( tam == tammax )
			{
				document.frm[campo+1].focus();
			}
		}
	}
}
//---------------------------
//formatação de validação de CNPJ
function isNUMB(c)
 {
 if((cx=c.indexOf(","))!=-1)
  {
  c = c.substring(0,cx)+"."+c.substring(cx+1);
  }
 if((parseFloat(c) / c != 1))
  {
  if(parseFloat(c) * c == 0)
   {
   return(1);
   }
  else
   {
   return(0);
   }
  }
 else
  {
  return(1);
  }
 }

function LIMP(c)
 {
 while((cx=c.indexOf("-"))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 while((cx=c.indexOf("/"))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 while((cx=c.indexOf(","))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 while((cx=c.indexOf("."))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 while((cx=c.indexOf("("))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 while((cx=c.indexOf(")"))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 while((cx=c.indexOf(" "))!=-1)
  {
  c = c.substring(0,cx)+c.substring(cx+1);
  }
 return(c);
 }

function VerifyCNPJ(CNPJ)
 {
 CNPJ = LIMP(CNPJ);
 if(isNUMB(CNPJ) != 1)
  {
  return(0);
  }
 else
  {
  if(CNPJ == 0)
   {
   return(0);
   }
  else
   {
   g=CNPJ.length-2;
   if(RealTestaCNPJ(CNPJ,g) == 1)
    {
    g=CNPJ.length-1;
    if(RealTestaCNPJ(CNPJ,g) == 1)
     {
     return(1);
     }
    else
     {
     return(0);
     }
    }
   else
    {
    return(0);
    }
   }
  }
 }
function RealTestaCNPJ(CNPJ,g)
 {
 var VerCNPJ=0;
 var ind=2;
 var tam;
 for(f=g;f>0;f--)
  {
  VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
  if(ind>8)
   {
   ind=2;
   }
  else
   {
   ind++;
   }
  }
  VerCNPJ%=11;
  if(VerCNPJ==0 || VerCNPJ==1)
   {
   VerCNPJ=0;
   }
  else
   {
   VerCNPJ=11-VerCNPJ;
   }
 if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
  {
  return(0);
  }
 else
  {
  return(1);
  }
 }


  function FormataCGC(Formulario, Campo, TeclaPres)
  {
    var tecla = TeclaPres.keyCode;
    var strCampo;
    var vr;
    var tam;
    var TamanhoMaximo = 14;

    eval("strCampo = document." + Formulario + "." + Campo);

    vr = strCampo.value;
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    vr = vr.replace("/", "");
    vr = vr.replace(",", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace(".", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    vr = vr.replace("-", "");
    tam = vr.length;

    if (tam < TamanhoMaximo && tecla != 8)
    {
      tam = vr.length + 1;
    }

    if (tecla == 8)
    {
      tam = tam - 1;
    }

    if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105)
    {
       if (tam <= 2)
       {
        strCampo.value = vr;
       }
       if ((tam > 2) && (tam <= 6))
       {
         strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 7) && (tam <= 9))
       {
         strCampo.value = vr.substr(0, tam - 6) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 10) && (tam <= 12))
       {
         strCampo.value = vr.substr(0, tam - 9) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 13) && (tam <= 14))
       {
         strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
       }
       if ((tam >= 15) && (tam <= 17))
       {
         strCampo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + '-' + vr.substr(tam - 2, tam);
       }
    }
  }
  
function Formata(tipo,valor)
{
    var valor2;
    valor = LIMP(valor);
    if (tipo == 'cnpj')
    {
        valor2 = valor.substr(0,2) + "." + valor.substr(2,3) + "." + valor.substr(5,3) + "/" + valor.substr(8,4) + "-" + valor.substr(12,2);
    }
    if (tipo == 'cpf')
    {
        valor2 = valor.substr(0,3) + "." + valor.substr(3,3) + "." + valor.substr(6,3) + "-" + valor.substr(9,2);
    }
    return valor2;
}

function validaCPF(value)
{
    cpf = LIMP(value);//retira caracteres de formatação

    erro = new String;
    if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! ";
    var nonNumbers = /\D/;
    if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! ";
    if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
    {
        erro += "Numero de CPF invalido!"
    }
    var a = [];
    var b = new Number;
    var c = 11;
    for (i=0; i<11; i++)
    {
        a[i] = cpf.charAt(i);
        if (i < 9) b += (a[i] * --c);
    }
    if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
    b = 0;
    c = 11;
    for (y=0; y<10; y++) b += (a[y] * c--);
    if ((x = b % 11) < 2)
        { a[10] = 0; }
    else
        { a[10] = 11-x; }
    if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]))
    {
        erro +="Digito verificador com problema!";
    }
    if (erro.length > 0)
    {
        return false;
    }
    return true;
}
//--------------------------------

//validar entrada de datas
function FormataData(Campo)
{
		var Tecla = window.event.keyCode;
		event.cancelBubble = true;
		// Ctrl, Alt, Shift, Home, End, Insert, Delete, PageUp, PageDown, Left Arrow, Right Arrow, Up Arrow, Down Arrow, BackSpace
		if (Tecla != 8 && (Tecla < 33 || Tecla > 40) && Tecla != 45 && Tecla != 46 && (Tecla < 16 || Tecla > 18))
		{
			tam = Campo.value.length;
			str = "";
			for(x = 0;x <= tam;x++){
				if(Campo.value.substring(x,x+1) != '/'){
					str += Campo.value.substring(x,x+1);
				}
			}
			aux = "";
			var TesteBarra = true;
			if (Campo.value.substring(0,1) == '/' || Campo.value.substring(1,2) == '/' || Campo.value.substring(3,4) == '/' || Campo.value.substring(4,5) == '/')
				TesteBarra = false;
			if (TesteBarra)
			{
				s = str.substring(0,2);
				aux = aux + str.substring(0,2);
				if((s.length >= 2))
					aux = aux + "/";
				s = str.substring(2,4);
				aux = aux + s;
				if((s.length >= 2))
					aux = aux + "/";
				aux = aux + str.substring(4,8);
				if (Campo.value.length == 10)
					if (!fIsDate(Campo.value))
					{
						Campo.value = ""
						alert('Data Inválida!')
					}
					else
						Campo.value = aux;
				else
					Campo.value = aux;
			}
		}
}

function fIsDate(strData) {
var blnIsDate = true;
var Dia = "";
var Mes = "";
var Ano = "";
var Sep1 = "";
var Sep2 = "";

	if (strData.length == 10) {
		Dia = strData.substring(0, 2);
		Mes = strData.substring(3, 5);
		Ano = strData.substring(6, 10);
		Sep1 = strData.substring(2, 3);
		Sep2 = strData.substring(5, 6);

		if (isNaN(parseFloat(Ano)) == true || parseFloat(Ano) < 1){
			blnIsDate = false;
		}
		else
		{
			if (Sep1 != "/" || Sep2 != "/") {
				blnIsDate = false;
			}
			else
			{
				if (isNaN(parseFloat(Mes)) == true || parseFloat(Mes) < 1 || parseFloat(Mes) > 12){
					blnIsDate = false;
				}
				else
				{
					if (parseFloat(Mes) == 2) {
						if ((parseFloat(Ano)) / 4 == parseInt(parseFloat(Ano) / 4)) {
							if (parseFloat(Dia) < 1 || parseFloat(Dia) > 29) {
								blnIsDate = false;
							}
						}
						else
						{
							if (parseFloat(Dia) < 1 || parseFloat(Dia) > 28) {
								blnIsDate = false;
							}
						}
					}
					else
					{
						if (parseFloat(Mes) == 4 || parseFloat(Mes) == 6 || parseFloat(Mes) == 9 || parseFloat(Mes) == 11) {
							if (parseFloat(Dia) < 1 || parseFloat(Dia) > 30) {
								blnIsDate = false;
							}
						}
						else
						{
							if (parseFloat(Mes) == 1 || parseFloat(Mes) == 3 || parseFloat(Mes) == 5 || parseFloat(Mes) == 7 || parseFloat(Mes) == 8 || parseFloat(Mes) == 10 || parseFloat(Mes) == 12) {
								if (parseFloat(Dia) < 1 || parseFloat(Dia) > 31) {
									blnIsDate = false;
								}
							}
						}
					}
				}
			}
		}
	}
	else
	{
		blnIsDate = false;
	}

	return blnIsDate;
}


function ValidaData(campo)
{
	if (campo.value != "")
	{
		if (fIsDate(campo.value) && campo.value.length == 10)
		{
			return true;
		}
		else
		{
			alert('Data inválida!')
			campo.value = ""
			campo.focus();
			return false;
		}
	}
}

function NumericoReal(event, Campo)
{
    Tecla = event.which;
    if(Tecla == null)
        Tecla = event.keyCode;
	if ((Tecla > 47 && Tecla < 58) || (Tecla == 44 && Campo.value.indexOf(",") == -1) || Tecla == 8)
		event.returnValue = true;
	else
		event.returnValue = false;
}

function Numerico(event)
{
  var Tecla = window.event.keyCode;
  event.cancelBubble = true;
  if((Tecla > 47 && Tecla < 58))
    event.returnValue = true;
  else
    event.returnValue = false;
}

