

function teste(){
    alert("alerta de teste");
}


function alternaFiltro(valor){
    if (valor == 'data'){
        document.getElementById("opData").style.visibility = 'visible';
        document.getElementById("opAIDF").style.visibility = 'hidden';
    }else if (valor == 'aidf'){
        document.getElementById("opData").style.visibility = 'hidden';
        document.getElementById("opAIDF").style.visibility = 'visible';
    }
}



function mostraCarregando(){
    top.principal.document.getElementById("carregando").style.visibility = 'visible'; 
}


function mostraCarregandoPesquisaGrafica(){
    document.getElementById("carregando").style.visibility = "visible";
}



function alteraLabel(check){
    if (check == 'contador'){
        document.getElementById("labelUsuario").value='CPF/CNPJ:';
    }else if (check == 'legal'){
        document.getElementById("labelUsuario").value='CPF:';
    }else if (check == 'grafica'){
        document.getElementById("labelUsuario").value='CPF/CNPJ:';
    }else if (check == 'prefeitura'){
        document.getElementById("labelUsuario").value='E-mail:';
        document.getElementById("usuario").value='';
    }
}

function abreFecha(div, img){
	if(document.getElementById(div).style.position == 'absolute'){
	    document.getElementById(div).style.visibility = 'visible';
    	document.getElementById(div).style.position = 'relative';
	    document.getElementById(img).src = "imagens/setas_up.gif";
	}else{
	    document.getElementById(div).style.visibility = 'hidden';
	    document.getElementById(div).style.position = 'absolute';
	    document.getElementById(img).src = "imagens/setas_down.gif";
	}

} 


function buscaProcesso(aidf){
    top.rodape.document.location = 'action/alteracaoGrafica.php?acao=consultaAIDF&aidf='+aidf;
}


function mouseOverMenu(menu){
	menu.style.color = "#990000";
	menu.style.cursor = "pointer";
}


function mouseOutMenu(menu){
	menu.style.color = "#333333";
	menu.style.cursor = "default"
}



function buscaGraficaCaixa(cnpj){
    top.rodape.document.location = 'action/buscaGrafica.php?cnpjInscricao='+cnpj+'&origem=caixa';
    window.close();
}

function buscaGraficaRelCaixa(grafica){
    top.rodape.document.location = 'action/buscaGraficaRel.php?cnpjInscricao='+grafica+'&origem=caixa';
    window.close();
}

function buscaEmpresaCaixa(empresa){
    top.rodape.document.location = 'action/buscaEmpresa.php?cnpjInscricao='+empresa+'&origem=caixa';
    window.close();
}




function buscaGraficaCredenciamento(cnpj){
    top.rodape.document.location = '../action/buscaGrafica.php?cnpj='+cnpj;
    window.close();
}


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;
}

function sobeFormulario(nomeComponente){
    document.frmPasso4.action = "action/workflow/passo4.php?nomeComponente="+nomeComponente; 
    document.frmPasso4.submit();
}



function limpaQuantidade(caixa){
    if (caixa.value == "TB" || caixa.value == "0"){
        document.getElementById("quantidade").value = "";
    }
}



			function check_date(field) {
				var checkstr = "0123456789"; 
				var DateField = field; 
				var Datevalue = ""; 
				var DateTemp = ""; 
				var seperator = "/"; 
				var day; 
				var month; 
				var year; 
				var leap = 0; 
				var err = 0; 
				var i; 
				err = 0; 
				DateValue = DateField.value; 
				
				/* Deletando todos os caracteres exceto o 0..9 */ 
				for (i = 0; i < DateValue.length; i++){ 
					if (checkstr.indexOf(DateValue.substr(i,1)) >= 0)
						DateTemp = DateTemp + DateValue.substr(i,1); 
				}
				DateValue = DateTemp; 
				
				/* Exectutando a data para 8 digitos - string*/ 
				/* if entrada do ano com 2-digitos / exemplo 20xx */ 
				if (DateValue.length == 6)
					DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); 
				
				if (DateValue.length != 8)
					err = 19;
					
				/* Se o ano for errado = 0000 */ 
				year = DateValue.substr(4,4); 
				if (year == 0)
					year = 2001;
				
				/* Validando o mês*/ 
				month = DateValue.substr(2,2); 
				if (month < 1)
					month = "01";

				if (month > 12)
					month = 12;
				
				/* Validando o dia*/ 
				day = DateValue.substr(0,2); 
				if (day < 1)
					day = "01";
				
				/* Validando ano Bissexto / fevereiro / dia */
				if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
					leap = 1;
				if ((month == 2) && (leap == 1) && (day > 29))
					day = 29;
				if ((month == 2) && (leap != 1) && (day > 28))
					day = 28;
				
				
				/* Validando o mês */ 
				if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12")))
					day = 31; 
				if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) 
					day = 30; 
					
				/* if 00 houvendo entrada, com erros */ 
				if ((day == 0) && (month == 0) && (year == 00)){
					err = 18;
					day = ""; 
					month = ""; 
					year = ""; 
					seperator = "";
				}
				
				/* if sem erros, escrevo a data completa no Input-Field (e.x. 13/12/2001) */ 
				if (err == 0) 
					DateField.value = day + seperator + month + seperator + year; 
			}
function validaData(e){
  // Definimos o divisor de nossa nova data
  var divisor = '/'
  // Recebemos a data digitada pelo usuario
  var data = e.value
  // Setamos a nova data para vazia
  var dataAtual = ''
  // Verificamos se o camarada digitou uma data válida
  if (data.match (/^(0[1-9]|[12][0-9]|3[01]).?(0[1-9]|1[012]).?([12][0-9]{3}|[0-9]{2})$/)) {
    // Precisamos trabalhar apenas com numeros
    data = data.replace (/[^0-9]/g, '')
    // adicionamos os dois primeiros pares
    dataAtual = data.substr(0,2)+divisor+data.substr(2,2)+divisor
    // Agora, os quatro ultimos
    // Se o usuário entrar com quatro caracteres, então ele entrou com uma data inteira
    if ( data.substr (4).length == 4 ) dataAtual += data.substr (4)
    // Senão, verificamos se os dois caracteres finais baseiam-se em 30 (lembrem-se do que disse no post)
    else dataAtual += (data.substr (4) > 30 ? '19' : '20') + data.substr (4)
  }
  // Por ultimo, reescrevemos o campo
  e.value = dataAtual
}	


function max_day(mn, yr)
{
   var mDay;
	if((mn == 4) || (mn == 6) || (mn == 9) || (mn == 11))
		mDay = 30;
	else if(mn == 2)
		mDay = isLeapYear(yr) ? 29 : 28;    
	else
		mDay = 31;
	return mDay; 
}



function checkDate(field) {
	var checkstr = "0123456789"; 
	var DateField = field; 
	var Datevalue = ""; 
	var DateTemp = ""; 
	var seperator = "/"; 
	var day; 
	var month; 
	var year; 
	var leap = 0; 
	var err = 0; 
	var i; 
	err = 0; 
	DateValue = DateField.value; 
	
	/* Deletando todos os caracteres exceto o 0..9 */ 
	for (i = 0; i < DateValue.length; i++){ 
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0)
			DateTemp = DateTemp + DateValue.substr(i,1); 
	}
	DateValue = DateTemp; 
	
	if(DateValue.length == 0)
		err = 20;
	
	if (DateValue.length > 8)
		err = 19;
	
	/* Exectutando a data para 8 digitos - string*/ 
	/* if entrada do ano com 2-digitos / exemplo 20xx */ 
	if (DateValue.length == 6)
		DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); 
		
		
	/* Se o ano for errado = 0000 */ 
	year = DateValue.substr(4,4); 
	if (year == 0)
		year = 2007;
	
	/* Validando o mes*/ 
	month = DateValue.substr(2,2); 
	if (month < 1)
		month = "01";

	if (month > 12)
		month = 12;
	
	/* Validando o dia*/ 
	day = DateValue.substr(0,2); 
	if (day < 1)
		day = "01";
	
	/* Validando ano Bissexto / fevereiro / dia */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
		leap = 1;
	if ((month == 2) && (leap == 1) && (day > 29))
		day = 29;
	if ((month == 2) && (leap != 1) && (day > 28))
		day = 28;
	
	
	/* Validando o mes */ 
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12")))
		day = 31; 
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) 
		day = 30; 
		
	/* if 00 houvendo entrada, com erros */ 
	if ((day == 0) && (month == 0) && (year == 00)){
		err = 18;
		day = ""; 
		month = ""; 
		year = ""; 
		seperator = "";
	}
	
	/* if sem erros, escrevo a data completa no Input-Field (e.x. 13/12/2001) */ 
	if (err == 0) 
		DateField.value = day + seperator + month + seperator + year; 
	else
		DateField.value = "";
}



function mascara(e, objeto, mask){ 
	//Todos funcionam com onkeypress apenas valor tem que ser com onkeydown
	var BS = 8;

	if(window.event){
	  var ev = e.keyCode;
    } else if(e.which){
	  var ev = e.which;		
    }else{
        return true;
    }
    if ( (ev == BS) && mask != "VALOR")
	    return true;
    
	campo = document.getElementById(objeto); 
	if(mask == "DATA"){
		separador = '/'; 	
		conjunto1 = 2; 
		conjunto2 = 5; 
		if (ev >= 48 && ev <= 57){ 
			if (campo.value.length == conjunto1)campo.value = campo.value + separador; 
			if (campo.value.length == conjunto2)campo.value = campo.value + separador; 
		}else return false; 
	}else if(mask == "CPF"){
		separador1 = '.'; 
		separador3 = '-';
		conjunto1 = 3; 
		conjunto2 = 7; 
		conjunto3 = 11; 	
		if (ev >= 48 && ev <= 57){ 
			if (campo.value.length == conjunto1)campo.value = campo.value + separador1; 
			if (campo.value.length == conjunto2)campo.value = campo.value + separador1; 
			if (campo.value.length == conjunto3)campo.value = campo.value + separador3; 
		}else return false;		
	}else if(mask == "CEP"){
		separador1 = '-'; 
		conjunto1 = 5;  
		if (ev >= 48 && ev <= 57)
			if (campo.value.length == conjunto1)campo.value = campo.value + separador1; 
		else return false;		
	}else if(mask == "FONE"){
		separador = '-';
		conjunto = 4;
		if (ev >= 48 && ev <= 57)
			if (campo.value.length == conjunto)campo.value = campo.value + separador; 
		else return false;			
	}else if(mask == "VALOR"){
		if(campo.value == "") campo.value = "0.00";	
		var str = campo.value;
		if (((ev >= 48 && ev <= 57) || (ev >= 96 && ev <= 105))&& campo.value.length < 8){
			str = str.slice(0, -3) + str.slice(str.length-2);
			if(str.substr(0,1) == "0") str = str.substr(1,str.length);
			str = str.slice(0, -1)+"."+str.slice(str.length-1);
			campo.value = str;
		}else if(ev == BS && str != "0.00"){
			str = str.slice(0, -3) + str.slice(str.length-2);
			str = str.slice(0, -3)+"."+str.slice(str.length-3);
			if(str.substr(0,1) == ".") str = "0"+str;
			campo.value = str;
		}else return false;
	}
}


function blNumero(event){
    if (navigator.appName == "Microsoft Internet Explorer")
        var tecla = window.event.keyCode;
    else
        var tecla = event.which;

    if ( (tecla < 48 || tecla > 57) && tecla != 8 && tecla != 0) 
        return false;
    return true;
}


function blMoeda(event){
    if (navigator.appName == "Microsoft Internet Explorer")
        var tecla = window.event.keyCode;
    else
        var tecla = event.which;

    if (!this.blNumero(event)){
        if (tecla == 46){
		    return true;
        }else{ 
            return false;        
        }
    }else
        return true;
}



function validaMoeda(caixa) {
    var qtd_virgulas=0;
    var valor= caixa.value; 
   
    if (Trim(valor) == ""){
        caixa.value= "0.00";
        return;
    }

    for (var i=0; i<valor.length; i++){
        if (valor.charAt(i) == "."){
           qtd_virgulas++;
           pos_virgula= i;
        }
    }

    if (qtd_virgulas == 0)
        valor += ".00";

    if (qtd_virgulas == 1){
        arr_valor= valor.split(".");

        if (!arr_valor[0])
            arr_valor[0]= "0";           	
        else
            arr_valor[0]= Number(arr_valor[0]);

            
        if (!arr_valor[1])
            arr_valor[1]= "00";           	
        else if (arr_valor[1].length == 1)
            arr_valor[1] += "0";
        else if (arr_valor[1].length >2){
            arr_valor[1]= arr_valor[1].slice(0, 2);
        }
        valor= arr_valor[0]+'.'+arr_valor[1];
    }

    if (qtd_virgulas > 1){
        alert (unescape('Valor inv%E1lido!'));
<!--        caixa.focus(); -->
<!--        caixa.select(); -->
    }else{
        if (valor > 999999999999.99){
            alert (unescape('Valor inv%E1lido!'));
<!--            caixa.focus(); -->
<!--            caixa.select(); -->
        }
        caixa.value= valor;
    }
}










function cpfcnpj2(obj, evento){
	var valida;
	soNumero(obj);
	if(obj.value.replace(/[^0-9]/gi, '').length == 11){
		valida = validaCPF(obj.value);
		if(valida){
			alert(unescape(valida));
			obj.value='';
		}else{
			FormataCPFDSF(obj, evento);
            return 'OK';
		}
	}
	else {
		if(obj.value.replace(/[^0-9]/gi, '').length == 14){
			valida = validaCNPJ(obj.value);
			if(valida){
				alert(unescape(valida));
				obj.value='';
			}else{
				FormataCNPJDSF(obj, evento);
                return 'OK';
			}
		}
		else {
			/*if(obj.value.replace(/[^0-9]/gi, '').length > 0){
				alert("CPF/CNPJ com formato invalido.");
			}*/
			obj.value = '';
		}
	}
}



function validaCNPJ(cnpj){
	digito =  (parseInt(cnpj.charAt(0)) * 5);
	digito += (parseInt(cnpj.charAt(1)) * 4);
	digito += (parseInt(cnpj.charAt(2)) * 3);
	digito += (parseInt(cnpj.charAt(3)) * 2);
	digito += (parseInt(cnpj.charAt(4)) * 9);
	digito += (parseInt(cnpj.charAt(5)) * 8);
	digito += (parseInt(cnpj.charAt(6)) * 7);
	digito += (parseInt(cnpj.charAt(7)) * 6);
	digito += (parseInt(cnpj.charAt(8)) * 5);
	digito += (parseInt(cnpj.charAt(9)) * 4);
	digito += (parseInt(cnpj.charAt(10))* 3);
	digito += (parseInt(cnpj.charAt(11))* 2);
		digito = (digito*10) % 11;
	if(digito == 10)
		digito = 0;
		if(digito == cnpj.charAt(12)){
		digito =  (parseInt(cnpj.charAt(0)) * 6);
		digito += (parseInt(cnpj.charAt(1)) * 5);
		digito += (parseInt(cnpj.charAt(2)) * 4);
		digito += (parseInt(cnpj.charAt(3)) * 3);
		digito += (parseInt(cnpj.charAt(4)) * 2);
		digito += (parseInt(cnpj.charAt(5)) * 9);
		digito += (parseInt(cnpj.charAt(6)) * 8);
		digito += (parseInt(cnpj.charAt(7)) * 7);
		digito += (parseInt(cnpj.charAt(8)) * 6);
		digito += (parseInt(cnpj.charAt(9)) * 5);
		digito += (parseInt(cnpj.charAt(10))* 4);
		digito += (parseInt(cnpj.charAt(11))* 3);
		digito += (parseInt(cnpj.charAt(12))* 2);
			digito = (digito*10) % 11;
		if(digito == 10)
			digito = 0;
			if(digito == cnpj.charAt(13)){
			return "";
		}
		else{
			return "CNPJ Inv%E1lido";
		}
	}
	else{
		return "CNPJ Inv%E1lido";
	}
}

function validaCPF(cpf){
	digito =  (parseInt(cpf.charAt(0)) * 10);
	digito += (parseInt(cpf.charAt(1)) * 9);
	digito += (parseInt(cpf.charAt(2)) * 8);
	digito += (parseInt(cpf.charAt(3)) * 7);
	digito += (parseInt(cpf.charAt(4)) * 6);
	digito += (parseInt(cpf.charAt(5)) * 5);
	digito += (parseInt(cpf.charAt(6)) * 4);
	digito += (parseInt(cpf.charAt(7)) * 3);
	digito += (parseInt(cpf.charAt(8)) * 2);

	digito = (digito*10) % 11;

	if(digito == 10)
		digito = 0;
		if(digito == cpf.charAt(9)){
		digito =  (parseInt(cpf.charAt(0)) * 11);
		digito += (parseInt(cpf.charAt(1)) * 10);
		digito += (parseInt(cpf.charAt(2)) * 9);
		digito += (parseInt(cpf.charAt(3)) * 8);
		digito += (parseInt(cpf.charAt(4)) * 7);
		digito += (parseInt(cpf.charAt(5)) * 6);
		digito += (parseInt(cpf.charAt(6)) * 5);
		digito += (parseInt(cpf.charAt(7)) * 4);
		digito += (parseInt(cpf.charAt(8)) * 3);
		digito += (parseInt(cpf.charAt(9)) * 2);

		digito = (digito*10) % 11;
		if(digito == 10)
			digito = 0;


		if(digito == cpf.charAt(10)){
			return "";
		}
		else{
			return "CPF Inv%E1lido";
		}
	}
	else{
		return "CPF Inv%E1lido";
	}
}


function FormataCPFDSF(objeto, evento){
	evita_letra2(evento);
	cpf = objeto.value.replace(/[^0-9]/gi, '');
	tamanho = cpf.length;
	
	if(tamanho >= 9)
		cpf = cpf.substr(0, 3)+"."+cpf.substr(3, 3)+"."+cpf.substr(6, 3)+"-"+cpf.substr(9, 3);
	else if(tamanho >= 6)
		cpf = cpf.substr(0, 3)+"."+cpf.substr(3, 3)+"."+cpf.substr(6, 3);
	else if(tamanho >= 3)
		cpf = cpf.substr(0, 3)+"."+cpf.substr(3, 3);
	else 
		cpf = cpf;
		
	objeto.value = cpf;
}


function FormataCNPJDSF(objeto, evento){
	evita_letra2(evento);
	
	cnpj = objeto.value.replace(/[^0-9]/gi, '');
	tamanho = cnpj.length;
	
	if(tamanho >= 12)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3)+"."+cnpj.substr(5, 3)+"/"+cnpj.substr(8, 4)+"-"+cnpj.substr(12, 2);
	else if(tamanho >= 8)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3)+"."+cnpj.substr(5, 3)+"/"+cnpj.substr(8, 4);
	else if(tamanho >= 5)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3)+"."+cnpj.substr(5, 3);
	else if(tamanho >= 2)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3);
	else
		objeto.value = cnpj;

}


function soNumero(obj){
	qtd = obj.value.length;
	valor = obj.value.replace(/[^0-9$]/gi, '');
	if(qtd > 0 && valor == ""){
		alert(unescape('Digite um valor v%E1lido.'));
		obj.value = '';
		obj.focus();
		return (false);
	}
	else{
		obj.value = valor;
	}
}

function evita_letra2(tecla) {
	if (tecla.keyCode < 48 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}

function cpf(obj, evento){
	var valida;
	soNumero(obj);
	if(obj.value.replace(/[^0-9]/gi, '').length == 11){
		valida = validaCPF(obj.value);
		if(valida){
			alert(unescape(valida));
			obj.value='';
		}else{
			FormataCPFDSF(obj, evento);
            return 'OK';
		}
	}
	else
		obj.value = '';
}	



