
// JavaScript Document

function verificarLoginSenhaEnter(event)
{	
	var tecla = event.keyCode;
 
    if(tecla == 13)
		verificarLoginSenha();
}

function verificarExtensao(texto){
   if(!verificaEspacosEmBrancos(texto)){
      if(f.up.value.substring(texto.length - 3, texto.length) != 'pdf'){ // verificação do tipo do arquivo que sera feito o up load.
         alert('O arquivo deve ser um arquivo de extensao pdf. \n O arquivo a ser inserido não é desta extensao, insira um arquivo pdf!');
	     return false;
      }
   }
}

function verificarLoginSenha()
{
   	var user = $('user').value;
	var senha = $('senha').value;    
	if(user != null || senha != null)
	{
		if(user == '' || senha == '')
		{
			alert('Usuário ou senha em branco!');
		}
		else
		{
			document.form1.action = 'JAVASCRIPT:verificarLogin()';
			document.form1.submit();
		}
	}
	else
		alert('Usuário ou senha em branco!');
}
//Trata carcteres especiais
function escapeAll(string) {    
    var decHex = function(dec) {
        var chars = '0123456789ABCDEF';        
        return chars.charAt(Math.floor(dec / 16)) + chars.charAt(dec % 16);
    };

    var out = '';

    for(var i = 0; i < string.length; i++) {
        var code = string.charCodeAt(i);

        if(code > 255)
            out += '%3F'; //coloca uma interrogacao caso o caractere seja desconhecido
        else
            out += '%' + decHex(code);
    }

    return out;
}

function verificaEspacosEmBrancos(texto)
{
	var Branco = " ";
    var Posic, Carac;
    var Temp = texto.length;
    var Cont = 0;
    for (var i=0; i < Temp; i++)
	{
		Carac =  texto.charAt (i);
	    Posic  = Branco.indexOf (Carac);   
    	if(Posic == -1)
    		Cont++;  	
	}
	if (Cont <= 0)
		return false;
        else
          return true;
}

function checkaBrowser () {
	if (navigator.appName.indexOf("Microsoft") == -1) return false;
	else return true;
}

function mostraEmprestimo(nDocumento){     
	if(document.getElementById('respostaConsulta'+nDocumento+'').style.display == 'none'){	   
	   javascrip:carregaTelaEmprestimo(nDocumento);
	   document.getElementById('respostaConsulta'+nDocumento+'').style.display = 'block';	   
	}else{	   
	   //document.getElementById('respostaConsulta'+nDocumento+'').innerHTML='';
	   document.getElementById('respostaConsulta'+nDocumento+'').style.display = 'none';
	}
}

function mostraEmprestimoAdm(nDocumento){
	if(document.getElementById('respostaConsulta'+nDocumento+'').style.display == 'none'){	   
	   mostraDadosEmprestimo(nDocumento);
	   document.getElementById('respostaConsulta'+nDocumento+'').style.display = 'block';	   
	}else{	   
	   //document.getElementById('respostaConsulta'+nDocumento+'').innerHTML='';
	   document.getElementById('respostaConsulta'+nDocumento+'').style.display = 'none';
	}
}

function mostraSolicitacaoAdm(nDocumento){
	if(document.getElementById('respostaConsulta'+nDocumento+'').style.display == 'none'){	   
	   mostraDadosSolicitacao(nDocumento);
	   document.getElementById('respostaConsulta'+nDocumento+'').style.display = 'block';	   
	}else{	   
	   //document.getElementById('respostaConsulta'+nDocumento+'').innerHTML='';
	   document.getElementById('respostaConsulta'+nDocumento+'').style.display = 'none';
	}
}

function mostraDadosComplemConsAvancAdm(nDocumento){	
	if(document.getElementById('dadosCompletos'+nDocumento+'').style.display == 'none'){	   
	   dadosCompleto(nDocumento);
	   document.getElementById('dadosCompletos'+nDocumento+'').style.display = 'block';	   
	}else{	   
	   //document.getElementById('dadosCompletos'+nDocumento+'').innerHTML='';
	   document.getElementById('dadosCompletos'+nDocumento+'').style.display = 'none';
	}
}
function mostrarCaixas(){	
	if(document.getElementById('cx').style.display == 'none'){	   
	   exibirCxRelatorio();
	   document.getElementById('cx').style.display = 'block';	   
	}else{	   
	   //document.getElementById('cx').innerHTML='';
	   document.getElementById('cx').style.display = 'none';
	}
}

function validaEmprestimo(){    
	var desc = document.getElementById('descricaoDoc').value;	
	var formatDoc = document.getElementById('formatoDoc').value;	        
	var envBusca = document.getElementById('menu1').value;
	var tot = document.getElementById('todoc').value; 
	var verZero = document.getElementById('checkzero').value;
	var i, k;	
	if(verZero == 0){
		i = 1
		k = 1
	}else{
		i = 0;
		k = 0;
	}	
	var sentinela = '';     
        var docNum = new Array(tot);
		for(j = 0; j <= tot; j++){
			docNum[j]= 0;
		}
        for(i; i< tot; i++){		             
          if(document.getElementById('docchek'+i).checked){
		  	sentinela = 'ativo'             
             docNum[i] = document.getElementById('docchek'+i).value;                          
	  }        
       }
       if(sentinela != 'ativo'){
         alert('Marque algum documento');
         return false;
       }       	
       if(envBusca == "2"){
       var diaBusca = '', mesBusca = '', anoBusca = '', horarioBusca = '', rua = '', bairro = '', numero = '',
       cep = '', complemento = '', cidade = '', uf = '';	
       diaBusca = document.getElementById('dia').value;
       mesBusca = document.getElementById('mes').value;
       anoBusca = document.getElementById('ano').value
       horarioBusca = document.getElementById('horario').value;
       rua = document.getElementById('rua').value;
       bairro = document.getElementById('bairro').value;
       numero = document.getElementById('numero').value;
	   cep = document.getElementById('cep').value;
	   complemento = document.getElementById('cpl').value;
       cidade = document.getElementById('cidade').value;	  
       uf = document.getElementById('uf').value;	  	
	if(!verificaEspacosEmBrancos(diaBusca) ||!verificaEspacosEmBrancos(mesBusca)|| !verificaEspacosEmBrancos(anoBusca) || !verificaEspacosEmBrancos(horarioBusca) || 
       !verificaEspacosEmBrancos(rua) || !verificaEspacosEmBrancos(bairro) || !verificaEspacosEmBrancos(numero) || 
       !verificaEspacosEmBrancos(cidade) || !verificaEspacosEmBrancos(uf) || !verificaEspacosEmBrancos(cep)){			
       var msg="Preencha os campos abaixo:\n";
       if(!verificaEspacosEmBrancos(diaBusca))
          msg+="\n- dia !";	
       if(!verificaEspacosEmBrancos(mesBusca))
          msg+="\n- mes !";
       if(!verificaEspacosEmBrancos(anoBusca))
          msg+="\n- ano !";
       if(!verificaEspacosEmBrancos(horarioBusca))
          msg+="\n- horario !";
       if(!verificaEspacosEmBrancos(rua))
          msg+="\n- rua !";
       if(!verificaEspacosEmBrancos(bairro))
          msg+="\n- bairro !";
       if(!verificaEspacosEmBrancos(numero))
          msg+="\n- numero !";   	      	       	      	     	      	     	      	   
       if(!verificaEspacosEmBrancos(cidade))
          msg+="\n- cidade !";
       if(!verificaEspacosEmBrancos(uf))
          msg+="\n- uf !";   
       if(!verificaEspacosEmBrancos(cep))
          msg+="\n- cep !";
	   alert(msg);
       return false;
    }
 }	        
    var retornoSplit = formatDoc.split('|');    	
    var dataPedido = anoBusca + mesBusca + diaBusca;       
	if(retornoSplit[0] == 'Empréstimo'){	   
	   formatDoc = retornoSplit[0];	   
	   javascript:pedidoEmprestimo(desc, formatDoc, envBusca, 
	   dataPedido, horarioBusca, rua, bairro, numero, cep, complemento, cidade, uf, docNum);	
       return true;
	}else{	
	   formatDoc = retornoSplit[0];	   
	   javascript:pedidoSolicitacao(desc, formatDoc, envBusca, 
	   dataPedido, horarioBusca, rua, bairro, numero, cep, complemento, cidade, uf, docNum);
           return true;
	}
	
}



// EX:  <input type="text" name="cep" id="telefone" onKeyPress="return formata(this, '###.###.###-##', event)">

function formata(campo, mask, evt) {

 if(document.all) { // Internet Explorer

    key = evt.keyCode; }

    else{ // Nestcape

       key = evt.which;

     }

 if(key != 0 && key!=8){

	 string = campo.value;  

	 i = string.length;

	 if (i < mask.length) {

	  if (mask.charAt(i) == '#') {

		   return (key > 47 && key < 58);

		  } else {

			if (mask.charAt(i) == '!') {  return true;  }

			for (c = i; c < mask.length; c++) {

			   if (mask.charAt(c) != '#' && mask.charAt(c) != '!')

					 campo.value = campo.value + mask.charAt(c);

			   else if (mask.charAt(c) == '!'){

					 return true;

			   } else {

				 return (key > 47 && key < 58);

				  }

			   }
		}

	  } else return false;

 }else{

	return true; 

 }

}

function retiraEspacos(string) {
    var i = 0;
    var fim = '';
    while (i < string.length) {
        if (string.charAt(i) == ' ') {
            fim += string.substr(0, i);
            string = string.substr(i+1, string.length - (i+1));
            i = 0;
        }
        else {
            i++;
        }
    }
    return fim + string;
}

function validarEmail(e){   
   var re = /^[^@]+@[^@]+.[a-z]{2,}$/i;
   if(e.search(re) == -1){   	
      return false;
   }else{   	
   	  return true;
   }
}

function replaceAll(string, token, newtoken) {
        while (string.indexOf(token) != -1) {
                string = string.replace(token, newtoken);
        }
        return string;
}

// Depois alterar as funções dos relatórios que mexem com datas.
function VerificaData(cData) {
    
  var tam = cData.length;
  if(tam != 8){  	    	
      return false;
	}  
  var dt, dia, mes, ano;
  if(cData.substr(2,1) == '/'){
  	data = cData.split('/');
    dia = data[0];
	mes = data[1];
	ano = data[2];
  }else{
    dia = cData.substr(0,2)
    mes = cData.substr (3,2)
    ano = cData.substr (6,4)	
  }  
  switch (mes) {
    case '01':
      if  (dia <= 31)
        return (true);
      break;
    case '02':
      if  (dia <= 29)
        return (true);
      break;
    case '03':
      if  (dia <= 31)
        return (true);
      break;
    case '04':
      if  (dia <= 30)
        return (true);
      break;
    case '05':
      if  (dia <= 31)
        return (true);
      break;
    case '06':
       if  (dia <= 30)
      return (true);
      break;
    case '07':
      if  (dia <= 31)
      return (true);
      break;
   case '08':
  if  (dia <= 31)
   return (true);
  break;
 case '09':
  if  (dia <= 30)
   return (true);
  break;
 case '10':
  if  (dia <= 31)
   return (true);
  break;
 case '11':
  if  (dia <= 30)
   return (true);
  break;
 case '12':
  if  (dia <= 31)
   return (true);
  break;
}
{
 return false;
}
return true;
}

function dataMaiorMenor(dtIni, dtFim){			
	if(dtIni > dtFim){		
		return false;
	}else{		
		return true;
	}	
}







