﻿//----- TRATAMENTO DO CPF ---------
function eDigito (c){   
	    return ((c >= "0") && (c <= "9"))
}

function eNulo(s){
	return ((s == null) || (s.length == 0))
}

function retiraCaracteresInvalidos(strCampo,tam) {
	nTamanho = strCampo.length;
	szCampo = "";
	j=0;
	for (i = nTamanho-1;i>=0;i--) {
		if (eDigito(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
			j++;
			if (j > tam) break;
		}
	}
    return szCampo;
}


function ajustarCPF(input, evento){
	var BACKSPACE=  8; 
	var PONTO=  46; 
	var FRENTE=  39; 
	var TRAS=  37;          
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (( tecla == BACKSPACE )||(tecla == PONTO)||(tecla == FRENTE)||(tecla == TRAS)) {
	    return true; 
    }
    if ( tecla == 13 )     return false; 

	if ((tecla<48)||(tecla>57)){
		evento.returnValue =false;
		return false;
	} else { 
		if((input.value.length==3)||(input.value.length==7)) {
		    input.value=input.value + "." ;
		}  else {
		      if(input.value.length==11) {
		           input.value=input.value + "-" ;
		         }
		}
	}
 return true;		
}

//-------------------------------
function editaCPF(strCampo,tam) {
    var i;
    var j;
    var nTamanho;
    var szCampo;
    j=0;
	nTamanho = strCampo.length;
	szCampo = "";
	for (i = nTamanho-1;i>=0;i--) 
	{
		if (eDigito(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
			j++;
			if (j > tam) break;
        }
	}
	if (szCampo.length < tam) {
		for (i = szCampo.length;i<tam;i++) 
		{
			szCampo = "0" + szCampo;
		}
	}
	if (j==0) return "";
    return (szCampo.substr(0,3)+"."+szCampo.substr(3,3)+"."+szCampo.substr(6,3)+"-"+szCampo.substr(9,2));
}

//-------------------------------
function validaCPF(obj, proximo) {
	valido=true;
	ncpf= retiraCaracteresInvalidos(obj.value,11)
	rcpf1 = ncpf.substr(0,9);
	rcpf2 = ncpf.substr(9,2);
	
	d1 = 0;
	for (i=0;i<9;i++) 
		d1 += rcpf1.charAt(i)*(10-i);
	d1 = 11 - (d1 % 11);
	if (d1>9) d1 = 0;
  
	if (rcpf2.charAt(0) != d1) {
		valido= false;
	}	  
	d1 *= 2;
	for (i=0;i<9;i++) 
		d1 += rcpf1.charAt(i)*(11-i);
	d1 = 11 - (d1 % 11);
	if (d1>9) d1 = 0;
	
	if (rcpf2.charAt(1) != d1){
		valido= false
	}
	if (!valido){
		obj.value=""
		alert('CPF Invalido')
		obj.focus()
	} else {
		buscaProfessor(retiraCaracteresInvalidos(obj.value,11));
		document.getElementById('cpfLimpo').value=retiraCaracteresInvalidos(obj.value,11);
		document.getElementById(proximo).focus();
	}
}


function enviar(form){
	var valido=true;
	valido = true;
	for (var i=0; i<form.elements.length; i++){
   		form.elements[i].style.backgroundColor="#FFFFFF";
   		//campos do tipo text
   		if((form.elements[i].type=='text')&
   		   (eNulo(form.elements[i].value))&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
    	//campos do tipo Select
    	if((form.elements[i].type=='select-one')&
   		   (form.elements[i].value==0)&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
    	//campos tipo password
    	if((form.elements[i].type=='password')&
   		   (eNulo(form.elements[i].value))&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
    	
	}
	if (!valido){
		alert('Observe os campos em destaque!\nEles devem estar preenchidos!');
	} else {
		form.submit()	    
	}
}



function atualizarSenha(){
	var valido=true;

//validação do senha	
	if (document.getElementById("senha").value==""){
		valido = false;
		document.getElementById("senha").style.backgroundColor="#FFFFCC";
		document.getElementById("senha").focus();
	}else{
		document.getElementById("senha").style.backgroundColor="#FFFFFF";
		document.getElementById("confsenha").style.backgroundColor="#FFFFFF";
		if(document.getElementById("senha").value!=document.getElementById("confsenha").value){
			valido = false;
			alert('As senhas informadas nos respectivos campos estão diferentes.');
			document.getElementById("senha").value="";
			document.getElementById("confsenha").value="";
			document.getElementById("senha").style.backgroundColor="#FFFFCC";
			document.getElementById("confsenha").style.backgroundColor="#FFFFCC";
			document.getElementById("senha").focus();
		}
	}

//validação do codigo do Professor
	if (document.getElementById("codprof").value==""){
		valido = false;
		document.getElementById("codprof").style.backgroundColor="#FFFFCC";
		document.getElementById("codprof").focus();
	}else{
		document.getElementById("codprof").style.backgroundColor="#FFFFFF";
	}

//validação do CPF	
	if (document.getElementById("cpf").value==""){
		valido = false;
		document.getElementById("cpf").style.backgroundColor="#FFFFCC";
		document.getElementById("cpf").focus();
	}else{
		document.getElementById("cpf").style.backgroundColor="#FFFFFF";
	}
	
	if(valido){
		document.getElementById("cpf").value=retiraCaracteresInvalidos(document.getElementById('cpf').value)
		document.getElementById("fatualiza").submit();
	} else {
		alert('Os campos em destaque são OBRIGATÓRIOS e DEVEM ser PREENCHIDOS');
	}
}
function atualizarSenhaAluno(){
	var valido=true;

//validação do senha	
	if (document.getElementById("senha").value==""){
		valido = false;
		document.getElementById("senha").style.backgroundColor="#FFFFCC";
		document.getElementById("senha").focus();
	}else{
		document.getElementById("senha").style.backgroundColor="#FFFFFF";
		document.getElementById("confsenha").style.backgroundColor="#FFFFFF";
		if(document.getElementById("senha").value!=document.getElementById("confsenha").value){
			valido = false;
			alert('As senhas informadas nos respectivos campos estão diferentes.');
			document.getElementById("senha").value="";
			document.getElementById("confsenha").value="";
			document.getElementById("senha").style.backgroundColor="#FFFFCC";
			document.getElementById("confsenha").style.backgroundColor="#FFFFCC";
			document.getElementById("senha").focus();
		}
	}

//validação do rg do aluno
	if (document.getElementById("rg").value==""){
		valido = false;
		document.getElementById("rg").style.backgroundColor="#FFFFCC";
		document.getElementById("rg").focus();
	}else{
		document.getElementById("rg").style.backgroundColor="#FFFFFF";
	}

//validação do RA do aluno	
	if (document.getElementById("ra").value==""){
		valido = false;
		document.getElementById("ra").style.backgroundColor="#FFFFCC";
		document.getElementById("ra").focus();
	}else{
		document.getElementById("ra").style.backgroundColor="#FFFFFF";
	}
	
	if(valido){
		document.getElementById("rg").value=retiraCaracteresInvalidos(document.getElementById('rg').value)
		document.getElementById("fatualiza").submit();
	} else {
		alert('Os campos em destaque são OBRIGATÓRIOS e DEVEM ser PREENCHIDOS');
	}
}

//******** PARA A TELA DE CADASTRO DE NOTAS **********//////


function apenasNumeros(input, evento){
	var BACKSPACE=  8; 
	var PONTO=  46; 
	var FRENTE=  39; 
	var TRAS=  37;   
	var TAB = 9;        
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (( tecla == BACKSPACE )||(tecla == PONTO)||(tecla == FRENTE)||(tecla == TRAS)||(tecla==TAB)) {
	    return true; 
    }
    if ( tecla == 13 )     return false; 

	if ((tecla<48)||(tecla>57)){
		evento.returnValue =false;
		return false;
	}
 return true;		

}

function apenasNumerosInteiros(input, evento){
	var BACKSPACE=  8; 
//	var PONTO=  46; 
	var FRENTE=  39; 
	var TRAS=  37;         
	var TAB = 9; 
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (( tecla == BACKSPACE )||(tecla == FRENTE)||(tecla == TRAS)||(tecla==TAB)) {
	    return true; 
    }
    if ( tecla == 13 )     return false; 

	if ((tecla<48)||(tecla>57)){
		evento.returnValue =false;
		return false;
	}
 return true;		

}


function validaNota(input){
	if (parseFloat(input.value)>10){
		alert('A nota informada não pode ser superior a 10.0');
		input.focus();
		input.value="";
	} else {
		if((input.value.length==2)&&(parseFloat(input.value)>=10)){
		    input.value="10.0" ;
		}else {
			if ((input.value.length==1) && ( parseFloat(input.value) < 10)) {
				input.value="0"+input.value+".0";
			} else 	if ((input.value.length==3) && ( parseFloat(input.value) < 10)) {
				input.value="0"+input.value;
			} else 	if ((input.value.length==2) && ( input.value.substring(1)=='.')) {
				input.value="0"+input.value+"0";
			} else 	if ((input.value.length==2) && ( input.value.substring(0)=='.')) {
				input.value="00"+input.value;
			}
		}
	}
	if ((input.value.substring(input.value.length-1)!=0)&&
    	(input.value.substring(input.value.length-1)!=5)) {
		alert('Informar as notas apenas com variações de 0.5 pontos!');
		input.focus();
		input.value="";
    }
}

function validaCamposNota(form1){
	for (var j=0; j<form1.elements.length; j++){
		if ((form1.elements[j].name="notas[]")&&(form1.elements[j].value=="")){
			return false;
		}
	}
	return true;
}

function mudaFigura(obj, novaFigura){
	obj.src=novaFigura
}

function contaCaracteres(tamanho, obj, evento){
	var BACKSPACE=  8; 
	var FRENTE=  39; 
	var TRAS=  37;      
	var TAB = 118;    
    var tecla= (evento.keyCode ? evento.keyCode: evento.which ? evento.which : evento.charCode)
    if (( tecla == BACKSPACE )||(tecla == FRENTE)||(tecla == TAB)||(tecla == TRAS)) {
		return true;
	}else if (obj.value.length > tamanho-1){
		alert('Você não pode exceder a quantidade máxima de '+tamanho+' caracteres permitida para este campo.');
		obj.value = obj.value.substr(0,tamanho);
		return false;
	}
	return true;
}

function limparReferencias(indice){
	getElmBI("inicioTitulo"+indice).value = "";
	getElmBI("titulo"+indice).value = "";
    getElmBI("subtitulo"+indice).value = "";
	getElmBI("cidade"+indice).value = "";
	getElmBI("edicao"+indice).value = "";
	getElmBI("anoEdicao"+indice).value = "";
	getElmBI("editora"+indice).value = "";
    getElmBI("sobrenome1"+indice).value = "";
    getElmBI("nome1"+indice).value = "";    
    getElmBI("nome2"+indice).value = "";   
    getElmBI("sobrenome2"+indice).value = ""; 
    getElmBI("nome3"+indice).value = "";   
    getElmBI("sobrenome3"+indice).value = ""; 
}


function validaFormPlano(nomeForm){
	form = getElmBI(nomeForm);
	valido = true;
	for (var i=0; i<form.elements.length; i++){
   		//form.elements[i].style.backgroundColor="#FFFFFF";
   		
   		//campos do tipo text
   		if(((form.elements[i].name=='titulo2')||
   			(form.elements[i].name=='titulo3')||
   			(form.elements[i].name=='titulo5')||
   			(form.elements[i].name=='titulo6')||
   			(form.elements[i].name=='titulo7')||
   			(form.elements[i].name=='titulo8'))&
    		(!eNulo(form.elements[i].value))){
			var indice = form.elements[i].name.substring(form.elements[i].name.length-1, form.elements[i].name.length);
			if (eNulo(document.getElementById('edicao'+indice).value)){
				document.getElementById('edicao'+indice).style.backgroundColor="#FFFFCC";
				valido=false;
			}
			if (eNulo(document.getElementById('anoEdicao'+indice).value)){
				document.getElementById('anoEdicao'+indice).style.backgroundColor="#FFFFCC";
				valido=false;
			}
			if(eNulo(document.getElementById('editora'+indice).value)){
				document.getElementById('editora'+indice).style.backgroundColor="#FFFFCC";
				valido=false;
			}
			if(eNulo(document.getElementById('sobrenome1'+indice).value)){
				document.getElementById('sobrenome1'+indice).style.backgroundColor="#FFFFCC";
				valido=false;
			}
			if(eNulo(document.getElementById('nome1'+indice).value)){
				document.getElementById('nome1'+indice).style.backgroundColor="#FFFFCC";			
				valido=false;
			}
   		}
   		
   		if((form.elements[i].type=='text')&
   		   (eNulo(form.elements[i].value))&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
    	//campos do tipo Select
    	if((form.elements[i].type=='select-one')&
   		   (form.elements[i].value==0)&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
    	//campos tipo password
    	if((form.elements[i].type=='password')&
   		   (eNulo(form.elements[i].value))&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
   		if((form.elements[i].type=='textarea')&
   		   (eNulo(form.elements[i].value))&
   		   (form.elements[i].className=="obrigatorio")){
   		   		form.elements[i].style.backgroundColor="#FFFFCC";
	   			valido=false;
    	} 
    	if (form.elements[i].type=='checkbox')
    		if (form.elements[i].checked){
    			form.elements[i].value='S'
    		}else {
    			form.elements[i].value='N'    		
    		}
	}
	if (!valido){
		alert('Observe os campos em destaque!\nEles devem estar preenchidos!');
	} else {
	   salvarPlanoEnsino(form);
	   
	   // form.name defini o nome do mapeamento a ser enviado (SERVLET)
	   // acao: SALVAR, EXCLUIR, PESQUISAR, LOGAR, ...
	   // if (acao=='login'){ // utilizado especificamente na tela de login
	   //     validarLogin(form.name, acao, getDados(form));
	   // } else { // tratador genérico para a maioria das telas
	   // 	manipularDados(form.name, acao, getDados(form));
	   // }
	    
	}
}

function validaSair(idDiv,idForm) {
	var resultado = confirm("Prezado Professor(a).\nVocê deseja salvar as alterações realizadas no Plano de Ensino?");
	if (resultado) {
		validaFormPlano(idForm);
	} else {
		getElmBI(idDiv).style.display='none'
	}
}