var isIE  = (navigator.appName.indexOf('Internet Explorer') != -1);
var title = ".:: Fundo de Desenvolvimento da Justiça - TJRN ::.";
window.status = title;

// Inicio da Funo Para Mudar a Barra de Status
function muda_status(var1){
  if (var1 == "") {
	  var1 = title;
	}
	window.status = var1;
}
// Fim da Funo Para Mudar a Barra de Status
function onLoadPage() {
  checkTipo('cpf', 'formGenerate');
}
// Inicio da Funo PROIBIR ENTER
function block() { 
  if(event.keyCode == 13) 
    return false;
  return true; 
} 
// Fim da block()

function LimparFormPrin(){
  window.opener.document.location='guias.do';
}

function HiddenManterInfo(){
  if (document.formGenerate.manterInformacoes.checked == true) {
    document.formConference.manterInformacoes.value = "1";
  } else {
    document.formConference.manterInformacoes.value = "";  
  }
}

function Ajuda(help){
  alert(help);
}

function enviarShowCustas() {
  document.formGenerate.action.value = "formShowCustas";
  abrirMyPopUp();
  document.formGenerate.submit();
  document.formGenerate.action.value = "generate";		
}

function validaNumGuia(formulario) {
	if (formulario == "formReceipt") {
  	if (formReceipt.numguiaReceipt.value == "") {
		  alert("Informe o número da guia!");
		  return false;
  	}
	  if (formReceipt.numguiaReceipt.value.length != 13) {
		  alert("O número da guia deve conter 13 dígitos!");
		  return false;
	  }
	  return true;
	}
	if (formulario == "formConference") {
	  if (formConference.numguiaVerify.value == "") {
		  alert("Informe o número da guia!");
		  return false;
	  }
	  if (formConference.numguiaVerify.value.length != 13) {
		  alert("O número da guia deve conter 13 dígitos!");
		  return false;
	  }
	  return true;
  }
  return true;
}
  
function onChangeForo() {
  document.formGenerate.target = "";
	document.formGenerate.action.value = "foro";
	document.formGenerate.submit();
}
  
function onChangeTipoSec() {
	document.formGenerate.target = "";
	document.formGenerate.action.value = "foro";
	document.formGenerate.submit();
}

function onChangeSecretaria() {
	document.formGenerate.target = "";
	document.formGenerate.action.value = "secretaria";
	document.formGenerate.submit();
}  
  
function showCustas() {
  document.formShowCustas.submit();
}

function processarForm(pForm) {	
	if ((formGenerate.action.value != "foro") && (formGenerate.action.value != "secretaria")) {
	  if (pForm != "formGenerate") {	 
		  if (validaNumGuia(pForm)) {
		    abrirMyPopUp();
		    return true;
	    }
	    return false;
	  }
  	abrirMyPopUp();
	  return true;
  }	
	return false;	
}

function PopUp(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+', resiable=0, menubar=1';
	win = window.open(mypage, myname, winprops);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	text = "Se a janela nao estava abrindo\ntalvez seja porque voce tenha um\nprograma bloqueador de pop-up!\nObservacao » O windows XP service pack 2\nbloqueia pop-ups!";
	if(win == null) { alert(text); return; }	
}

function openMyPopUpPage(mypage, myname) {
  w = screen.width;
  h = screen.height;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes, resizable=0';
  win = window.open(mypage, myname, winprops);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	text = "Se a janela nao estava abrindo\ntalvez seja porque voce tenha um\nprograma bloqueador de pop-up!\nObservacao » O windows XP service pack 2\nbloqueia pop-ups!";
	if(win == null) { alert(text); return; }	
}

  
function abrirMyPopUp() {
  w = screen.width;
  h = screen.height;
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes, resizable=0';
  win = window.open('aguarde.jsp', 'mypopup', winprops);
  if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function seleciona(campo, opcao, estado, nomeinput) {
	if (opcao) { 															//se opcao for true
		for ( a = 0; a < document.formGenerate.length; a++) {               //varre todos os checkbox
			if (document.formGenerate.elements[a].type == "checkbox") {
				if ((document.formGenerate.elements[a].name != campo) 
				 && (document.formGenerate.elements[a].name != "manterInformacoes")
				 && (document.formGenerate.elements[a].name != "cpf")				 
				 && (document.formGenerate.elements[a].name != "cnpj")				 				 
					 ) {
					document.formGenerate.elements[a].checked = false;           //desmarca o chb, seta zero no input, desabilita o chb
					document.formGenerate.elements[a].disabled = estado;
				}
			} else if (document.formGenerate.elements[a].type == "text") {
				if ((document.formGenerate.elements[a].name != nomeinput)
				&& (document.formGenerate.elements[a].name != "valor")
				&& (document.formGenerate.elements[a].name != "processo")
				&& (document.formGenerate.elements[a].name != "cpf_cnpj")
				&& (document.formGenerate.elements[a].name != "telefone")
				&& (document.formGenerate.elements[a].name != "email")) {          
						document.formGenerate.elements[a].value = 0;
						document.formGenerate.elements[a].disabled = true;
				} //|end nomeinput
			}   //|end else type
		}     //|end for
	}       //|end opcao
}         //|end function

function refreshField(obj){
	if (isIE) {
		obj.value = obj.value;
	}
}
  
function onlyNumbers(e) {
	var keyNumber = (isIE) ? event.keyCode : e.which;
	if (((keyNumber<48)||(keyNumber>57)) && (keyNumber!=13) && (keyNumber!="0") && (keyNumber!=8)) {
		if (isIE) event.keyCode=0;
		return false;
	}
}

function formatOnKeyUp(OBJ){
	var decimal,inteiro;
	var i,count;
	STR = new String(OBJ.value);
	STR = trimLeadingZeros(STR);
	inteiro='';
	if (isIE) {
	if (STR.length == 1){
			inteiro  = '0';
			decimal = '0' + STR;
		}
		else { 
			if (STR.length == 2){
				inteiro  = '0';
				decimal = STR;
			}
			else{
				decimal = STR.substring(STR.length-2,STR.length);
				i=3;
				count=0;
				while (i<=STR.length){
				if (count==3) {
					inteiro = '.' + inteiro;
					count = 0;
				}
					inteiro = STR.charAt(STR.length-i) + inteiro;
				count++;
				i++;
				}
			}
		}
	
	if (inteiro == '') {
		inteiro = '0';
	}

	if (decimal == '') {
		decimal = '00';
	}
	OBJ.value = inteiro+','+decimal;
	}
}

function formatOnKeyDown(OBJ){
    var decimal,inteiro;
    var i,count;
    STR = new String(OBJ.value);
  
    inteiro='';
  
    if (isIE) {	  
	  if (OBJ.maxLength > STR.length) {
	    STR = trimLeadingZeros(STR);
	  
	    if ( ((event.keyCode > 47) && (event.keyCode < 59)) || 
		     ((event.keyCode > 95) && (event.keyCode < 106)) ) {
	      
			if (STR.length == 0){
			  inteiro  = '0';
			  decimal = '0' + STR;
			}
			else { 
			  if (STR.length == 1){
			    inteiro  = '0';
			    decimal = STR;
			  }
			  else{
			    decimal = STR.substring(STR.length-1,STR.length);
			    i=2;
			    count=0;
			    while (i<=STR.length){
			 		if (count==3) {
			  	  inteiro = '.' + inteiro;
			  	  count = 0;
			  	}
			      inteiro = STR.charAt(STR.length-i) + inteiro;
			  	count++;
			  	i++;
			    }
			  }
			}   
	 
	  }
	  else{ 
	    if (event.keyCode == 8){

	      if (STR.length == 0){
			  inteiro  = '0';
			  decimal = '000';
			}
			else { 
			  if (STR.length == 1){
			    inteiro  = '0';
			    decimal = '00' + STR;
			  }
			  else { 
			    if (STR.length == 2){
		          inteiro  = '0';
			      decimal = '0' + STR;
			     }
		         else{
	 			   decimal = STR.substring(STR.length-3,STR.length);
				   i=4;
				   count=0;
				   while (i<=STR.length){
			 		 if (count==3) {
					   inteiro = '.' + inteiro;
					   count = 0;
					  }
				      inteiro = STR.charAt(STR.length-i) + inteiro;
					  count++;
					  i++;
				    }
			     }
			  } 
	      }
	    }
	    else {
	      
	      if (STR.length == 1){
		 	  inteiro  = '0';
		      decimal = '0' + STR;
			}
			else { 
			  if (STR.length == 2){
				  inteiro  = '0';
				  decimal = STR;
				}
				else{
				  decimal = STR.substring(STR.length-2,STR.length);
				  i=3;
				  count=0;
				  while (i<=STR.length){
			 		if (count==3) {
					  inteiro = '.' + inteiro;
					  count = 0;
					}
				    inteiro = STR.charAt(STR.length-i) + inteiro;
					count++;
					i++;
				  }
				}
			}        
	    }
	  }
	  
	  if (inteiro == '') {
	    inteiro = '0';
	  }
  
	  if (decimal == '') {
	    decimal = '00';
	  }
	  OBJ.value = inteiro+','+decimal;
	}
  }
}

function trimLeadingZeros(STR){
	var sAux='';
	var i=0;
	STR=new String(STR);

	while (i < STR.length ){
		if ((STR.charAt(i)!='.') && (STR.charAt(i)!=',') && (STR.charAt(i)!=':')){
			sAux += STR.charAt(i);
		}
		i++
	}

	STR = new String(sAux);
	sAux = '';
	i=0;

	while (i < STR.length ){
		if (STR.charAt(i) != '0'){
			sAux = STR.substring(i,STR.length)
		i = STR.length;
	}
		i++;
	}

	return  sAux;
}

//declarar div para as funções abaixo
//msgValida
//msgCPFCNPJ
//Funcoes de validacao do CPF-CNPJ
function checkTipo(opcao, frm) {
  document.forms[frm].tipo.value = opcao;
  document.forms[frm].cpf_cnpj.value = "";
  document.getElementById('msgValida').innerHTML = "";


	if (opcao == "cpf") {
    //msgCPFCNPJ.innerHTML = "Informe o CPF (000.000.000-00)"

		if (document.forms[frm].cpf.checked == true) {
   	  document.forms[frm].cnpj.checked = !document.forms[frm].cpf.checked;			
		} else {
  	  document.forms[frm].cpf.checked = true;			
		}

	} else {
    //msgCPFCNPJ.innerHTML = "Informe o CNPJ (00.000.000/0000-00)"
	
		if (document.forms[frm].cnpj.checked == true) {
   	  document.forms[frm].cpf.checked = !document.forms[frm].cnpj.checked;			
		} else {
  	  document.forms[frm].cnpj.checked = true;			
		}

	}

//  document.forms[frm].cpf_cnpj.focus();
}

function setLength(id, length){
   document.getElementById(id).setAttribute('maxLength', length);
}
function setSize(id, length){
   document.getElementById(id).setAttribute('size', length);
}

function formatCPFCNPJ(Campo, teclapres, frm){
  var maxLength;
  var tecla = teclapres.keyCode;
  var vr = new String(Campo.value);
  vr = vr.replace(".", "");
  vr = vr.replace(".", "");
  vr = vr.replace("-", "");
  vr = vr.replace("/", "");

  if (document.forms[frm].tipo.value == "cpf") {
	  maxLength = 14; //define o tamanho maximo do campo
		tam = vr.length + 1;
		if (tecla != 9 && tecla != 8){               
			if (tam > 3 && tam < 7)                                        
				Campo.value = vr.substr(0,3) + '.' + vr.substr(3, tam);
			if (tam >= 7 && tam <10)
				Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
			if (tam >= 10 && tam < 12)
				Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);
		}
	} else { //cnpj
	  maxLength = 18; //define o tamanho maximo do campo
		tam = vr.length + 1;
		if (tecla != 9 && tecla != 8){               
			if (tam > 2 && tam < 6)                                        
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2, tam);
			if (tam >= 6 && tam < 9)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5, tam-5);
			if (tam >= 9 && tam < 13)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);				
			if (tam >= 13 && tam < 15)
				Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4) + '-' + vr.substr(12,tam-12);
		}	
	} //fim do if tipo = cpf
  setLength('cpf_cnpj', maxLength);	

}

// formata a entrada de texto 
function formatInput(StrNL) {
  var evt = (window.event ? event : evt);
  var key = (window.event ? evt.keyCode : evt.which);
//  alert("tecla: "+evnt.keyCode);

  if (StrNL == 'n') {
    // Verifica se a tecla digitada está em [0-9]['.' ou '-']
		//(!((key > 47) && (key < 58)) && (key != 46) && (key != 45))
    if (!((key > 47) && (key < 58))) {
      evt.keyCode = 0;
	    evt.returnValue = false;
    }
  } else 
	if (StrNL == 'l') {
	// Verifica se a tecla digitada está em [0-9][A-Z][a-z]['.' ou '-' ou '/']
		if (!((key > 47) && (key < 58)) && !((key > 64) && (key < 91)) && !((key > 96) && (key < 123)) && (key != 45) && (key != 46) && (key != 47)) {
			evt.keyCode = 0;
			evt.returnValue = false;
		}
	}
	
}

function ValidarCPFCNPJ(frm, validar) {
  var vr = new String(document.forms[frm].cpf_cnpj.value);
  vr = vr.replace(".", "");
  vr = vr.replace(".", "");
  vr = vr.replace("-", "");
  vr = vr.replace("/", "");
  document.getElementById('msgValida').innerHTML = "";
	
	if (vr.length > 0) 	//so vai validar se tiver valor (maior que zero)
		if (document.forms[frm].tipo.value == "cpf") {
	
			if (validar)
				if (vr.length < 11) {
          document.getElementById('msgValida').innerHTML = "Quantidade de Caracteres Inválida!";
					return false;
				}
	
			if (vr.length == 11) 
				if (!valida_cpf(vr)) {
					document.getElementById('msgValida').innerHTML = "O número do CPF digitado não é válido.";
					return false;
				}			
				
		} else {
			
			if (validar)
				if (vr.length < 14) {
					document.getElementById('msgValida').innerHTML = "Quantidade de Caracteres Inválida!";
					return false;
				}	
	
			if (vr.length == 14) 		
				if (!valida_cnpj(vr)) {
					document.getElementById('msgValida').innerHTML = "O número do CNPJ digitado não é válido.";	
					return false;
				}
		}
	
	return true;
}

function valida_cpf(cpf) { 
	var numeros, digitos, soma, i, resultado, digitos_iguais; 
	digitos_iguais = 1; 
	if (cpf.length < 11) 
				return false; 
	for (i = 0; i < cpf.length - 1; i++) 
				if (cpf.charAt(i) != cpf.charAt(i + 1)) 
							{ 
							digitos_iguais = 0; 
							break; 
							} 
	if (!digitos_iguais) 
				{ 
				numeros = cpf.substring(0,9); 
				digitos = cpf.substring(9); 
				soma = 0; 
				for (i = 10; i > 1; i--) 
							soma += numeros.charAt(10 - i) * i; 
				resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
				if (resultado != digitos.charAt(0)) 
							return false; 
				numeros = cpf.substring(0,10); 
				soma = 0; 
				for (i = 11; i > 1; i--) 
							soma += numeros.charAt(11 - i) * i; 
				resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
				if (resultado != digitos.charAt(1)) 
							return false; 
				return true; 
				} 
	else 
				return false; 
	} 

function valida_cnpj(cnpj) { 
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais; 
	digitos_iguais = 1; 
	if (cnpj.length < 14 && cnpj.length < 15) 
				return false; 
	for (i = 0; i < cnpj.length - 1; i++) 
				if (cnpj.charAt(i) != cnpj.charAt(i + 1)) 
							{ 
							digitos_iguais = 0; 
							break; 
							} 
	if (!digitos_iguais) 
				{ 
				tamanho = cnpj.length - 2 
				numeros = cnpj.substring(0,tamanho); 
				digitos = cnpj.substring(tamanho); 
				soma = 0; 
				pos = tamanho - 7; 
				for (i = tamanho; i >= 1; i--) 
							{ 
							soma += numeros.charAt(tamanho - i) * pos--; 
							if (pos < 2) 
										pos = 9; 
							} 
				resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
				if (resultado != digitos.charAt(0)) 
							return false; 
				tamanho = tamanho + 1; 
				numeros = cnpj.substring(0,tamanho); 
				soma = 0; 
				pos = tamanho - 7; 
				for (i = tamanho; i >= 1; i--) 
							{ 
							soma += numeros.charAt(tamanho - i) * pos--; 
							if (pos < 2) 
										pos = 9; 
							} 
				resultado = soma % 11 < 2 ? 0 : 11 - soma % 11; 
				if (resultado != digitos.charAt(1)) 
							return false; 
				return true; 
				} 
	else 
				return false; 
	} 
//fim das funcoes de validação CPF_CNPJ