controle    = 0;
controlePg  = 0;
itens		= 0;
lista		= 0;
total 		= 0;
 
// Seleciona todos os chackbox em que o ID case com a string passada por parmetro
function selecionaTodos(strId) {
	var a = 0;
	var n = document.form.length;
	
	for(i = 0; i < n; i++) {
		str = document.form.elements[i].id;
		if(str.indexOf(strId) >= 0 && document.form.elements[i].disabled === false && document.form.elements[i].checked === false) {
			document.form.elements[i].checked = true;
			a++;
		}
	}
	
	if(a == 0) {
		for(i = 0; i < n; i++) {
			str = document.form.elements[i].id;
			if(str.indexOf(strId) >= 0 && document.form.elements[i].disabled === false) {
				document.form.elements[i].checked = false;
			}
		}
	}
}

// Verifica se os checkbox DELETAR esto checados e confirma a deleo
function verificaLista(valor) {
	var a = 0;
	var n = document.form.length;
	for(i = 0; i < n; i++) {
		var str = document.form.elements[i].id;
		if(str == valor && document.form.elements[i].checked === true) {
			a++;
		}
	}
	
	if (a > 0) {
		if (confirm("Tem certeza que deseja " + valor + " os itens selecionados?")) {
			document.form.it_acao.value = 'deletar';
			return true;
		} else {
			return false;
		}
	} else {
		alert("Selecione qual ítem você deseja " + valor + "!");
		return false;
	}
}

// Impede a digitao de caracteres que no sejam NMEROS
function numeros(event)
{
    if (event.keyCode) {
        if (event.keyCode < 44 || event.keyCode > 58) {
            return false;
        } else {
            return true;
        }
    } else {
        if (event.which < 44 || event.which > 58) {
            return false;
        } else {
            return true;
        }
    }
}

// Verifica a sintaxe padro de e-mail
function alertaEmail(campo) {
	apos   = campo.value.indexOf("@");
	dotpos = campo.value.lastIndexOf(".");
	len    = campo.value.length;
	if (campo.value != '' && (apos < 1 || dotpos-apos < 2 || dotpos == len-1)) {
		alert('Digite um endereço de e-mail válido!');
		campo.focus();
		campo.select();
		return false;
	} else {
		return true;
	}
}

// Seta a data atual nos combos DIA / MS / ANO
function setaData(comboDia,comboMes,comboAno){
	data = new Date();
	
	dia  = new String(data.getDate());
	if(dia.length == 1) { dia = '0' + dia; }
	
	mes  = new String(data.getMonth() + 1);
	if(mes.length == 1) { mes = '0' + mes; }
	
	ano  = new String(data.getFullYear());
	
	eval('document.form.' +  comboDia + '.value = "' + dia + '";'); 
	eval('document.form.' +  comboMes + '.value = "' + mes + '";');
	eval('document.form.' +  comboAno + '.value = "' + ano + '";');
}

// Seta a hora atual nos combos HORA / MINUTO
function setaHora(comboHora,comboMinuto){
	data   = new Date();
	
	hora   = new String(data.getHours());
	if(hora.length   == 1) { hora   = '0' + hora;   }
	
	minuto = new String(data.getMinutes());
	if(minuto.length == 1) { minuto = '0' + minuto; }
	
	
	eval('document.form.' +  comboHora   + '.value = "' + hora   + '";'); 
	eval('document.form.' +  comboMinuto + '.value = "' + minuto + '";');
}

// O BLOCO DE FUNES ABAIXO VALIDA AS DATAS SETADAS NOS COMBOS DIA / MS / ANO
var obData	= new Date();
var focoDia = new String(obData.getDate());
var focoMes = new String(obData.getMonth() + 1);
var focoAno = new String(obData.getFullYear());

function focusData(campo) {
	focoDia = eval('document.form.' + campo + '_dia.value;');
	focoMes = eval('document.form.' + campo + '_mes.value;');
	focoAno = eval('document.form.' + campo + '_ano.value;');
}

function verificaData(campo) {
	dia = eval('document.form.' + campo + '_dia.value;');
	mes = eval('document.form.' + campo + '_mes.value;');
	ano = eval('document.form.' + campo + '_ano.value;');
	controle = true;
	
	switch(mes) {
		case '2':
			ano = ano % 4;
			if (ano > 0) { // se no for ano bissexto
				if (dia >= 29) { controle = false; }
			} else {
				if (dia >= 30) { controle = false; }			
			}
		break;

		case '4':
		case '6':
		case '9':
		case '11':
			if (dia == 31) { controle = false; }
		break;
	}
	
	if(controle === false) {
		alert('A data selecionada não é válida!');
		eval('document.form.' + campo + '_dia.value = ' + focoDia);
		eval('document.form.' + campo + '_mes.value = ' + focoMes);
		eval('document.form.' + campo + '_ano.value = ' + focoAno);
		return false;
	} else { return true; }
}
// AQUI TERMINA O BLOCO DE FUNES QUE VALIDAM DATA

function validar_cnpj( sender ) {
	var temp
	var cont
	var dg1
	var dg1_1
	var dg2
	var dg2_2
	var sValue
	var digits="0123456789"

	/*
	if (sender.value == "") {
		alert("Por Favor Preencha o Campo CNPJ.");
		sender.focus();
	}
	*/
	sValue = sender.value;

	// Limpa todos os caracteres de formatao que
	// j estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	
	for (var i=0;i<sValue.length;i++){
		temp=sValue.substring(i,i+1);
		
		if (digits.indexOf(temp)==-1){
			alert("Por Favor, o Campo CNPJ deve ser preenchido apenas com números!");
			sender.focus();
			return;
		}
	}
	
	cont=0;
	
	for (var i = 0; i < sValue.length; i++) {
		temp=sValue.substring(i,i+1);
		cont=cont+1;
	}
	
	if (cont!=0) {
		if (cont!=14) {
			alert("Número de CPNJ Inválido");
			sender.focus();
		}
		else {
			cont=1;
			dg1=0;
			dg2=0;
			
			for (var i=0;i<sValue.length;i++){
				temp = Math.abs( sValue.substring(i,i+1) );

				if (cont==1)
						dg1=dg1+Math.abs(temp*5);

				if (cont==2)
					dg1=dg1+Math.abs(temp*4);
				
				if (cont==3)
					dg1=dg1+Math.abs(temp*3);
				
				if (cont==4)
					dg1=dg1+Math.abs(temp*2);

				if (cont==5)
					dg1=dg1+Math.abs(temp*9);
				
				if (cont==6)
					dg1=dg1+Math.abs(temp*8);
				
				if (cont==7)
					dg1=dg1+Math.abs(temp*7);
				
				if (cont==8)
					dg1=dg1+Math.abs(temp*6);

				if (cont==9)
					dg1=dg1+Math.abs(temp*5);
					
				if (cont==10)
					dg1=dg1+Math.abs(temp*4);
				
				if (cont==11)
					dg1=dg1+Math.abs(temp*3);
				
				if (cont==12)
					dg1=dg1+Math.abs(temp*2);
					
				if (cont==13)
					dg1_1=temp;
				
				if (cont==14)
					dg2_2=temp;
				
				cont=cont+1;
			}

			dg1=11-(dg1-((Math.floor(dg1/11))*11))

			if (dg1==10 || dg1==11)
				dg1=0;

			if (dg1!=dg1_1){
				alert("Número de CPNJ Inválido");
				sender.focus();
			}
			else {
				cont=1;

				for (var i=0;i<sValue.length;i++){
					temp=Math.abs(sValue.substring(i,i+1));
					
					if (cont==1)
						dg2=dg2+Math.abs(temp*6);
					if (cont==2)
						dg2=dg2+Math.abs(temp*5);
					if (cont==3)
						dg2=dg2+Math.abs(temp*4);
					if (cont==4)
						dg2=dg2+Math.abs(temp*3);
					if (cont==5)
						dg2=dg2+Math.abs(temp*2);
					if (cont==6)
						dg2=dg2+Math.abs(temp*9);
					if (cont==7)
						dg2=dg2+Math.abs(temp*8);
					if (cont==8)
						dg2=dg2+Math.abs(temp*7);
					if (cont==9)
						dg2=dg2+Math.abs(temp*6);
					if (cont==10)
						dg2=dg2+Math.abs(temp*5);
					if (cont==11)
						dg2=dg2+Math.abs(temp*4);
					if (cont==12)
						dg2=dg2+Math.abs(temp*3);
					if (cont==13)
						dg2=dg2+Math.abs(temp*2);

					cont=cont+1;
				}
				
				dg2=11-(dg2-((Math.floor(dg2/11))*11));
				
				if (dg2==10 || dg2==11)
					dg2=0;
				
				if (dg2!=dg2_2) {
					alert("Número de CPNJ Inválido");
					sender.focus();
				}
			}
		}
	}
}

function validar_cpf( sender ){
	var conf=0
	var temp
	var temp1
	var cont
	var dg1
	var dg1_1
	var dg2
	var dg2_2
	var sValue
	var digits="0123456789"
	
	/*
	if (sender.value == ""){
		alert("Por Favor Preencha o Campo CPF.")
		sender.focus()
	}
	*/
	sValue = sender.value;

	// Limpa todos os caracteres de formatao que
	// j estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
      
	for (var i = 0; i < sValue.length; i++) {
		temp1=temp
		temp=sValue.substring(i,i+1)
		
		if (temp==temp1){
			conf=conf+1
		}
    
		if (digits.indexOf(temp)==-1){
			alert("Por Favor, o Campo CPF deve ser preenchido apenas com números!")
      sender.focus()
      return
		}
	}

	if (conf==10){
		alert("Número do CPF Inválido")
		sender.focus()
		return
	}

	cont=0

	for (var i=0;i<sValue.length;i++){
		temp=sValue.substring(i,i+1)
		cont=cont+1
	}

	if (cont!=0){
		if (cont!=11){
			alert("Número do CPF Inválido")
			sender.focus()
			return
		}
    else {
			cont=1
			dg1=0
			dg2=0
			
			for (var i=0;i<sValue.length;i++){
				temp=Math.abs(sValue.substring(i,i+1))
        
				if (cont==1) {
					dg1=dg1+Math.abs(temp*10)
				}
               
				if (cont==2) {
					dg1=dg1+Math.abs(temp*9)
				}

				if (cont==3) {
					dg1=dg1+Math.abs(temp*8)
				}

				if (cont==4) {
					dg1=dg1+Math.abs(temp*7)
				}

				if (cont==5) {
					dg1=dg1+Math.abs(temp*6)
				}

				if (cont==6) {
					dg1=dg1+Math.abs(temp*5)
				}

				if (cont==7) {
					dg1=dg1+Math.abs(temp*4)
				}

				if (cont==8) {
					dg1=dg1+Math.abs(temp*3)
				}

				if (cont==9){
					dg1=dg1+Math.abs(temp*2)
				}

				if (cont==10) {
					dg1_1=temp
				}
					
				if (cont==11) {
					dg2_2=temp
				}
				
				cont=cont+1
			}

			dg1=11-(dg1-((Math.floor(dg1/11))*11))

			if (dg1==10 || dg1==11) {
				dg1=0
			}

			if (dg1!=dg1_1) {
				alert("Número do CPF Inválido")
				sender.focus()
				return
			}
			else {
				cont=1

				for (var i=0;i<sValue.length;i++) {
					temp=Math.abs(sValue.substring(i,i+1))

					if (cont==1){
						dg2=dg2+Math.abs(temp*11)
					}

					if (cont==2){
						dg2=dg2+Math.abs(temp*10)
					}

					if (cont==3){
						dg2=dg2+Math.abs(temp*9)
					}

					if (cont==4){
						dg2=dg2+Math.abs(temp*8)
					}

					if (cont==5){
						dg2=dg2+Math.abs(temp*7)
					}

					if (cont==6){
						dg2=dg2+Math.abs(temp*6)
					}

					if (cont==7){
						dg2=dg2+Math.abs(temp*5)
					}

					if (cont==8){
						dg2=dg2+Math.abs(temp*4)
					}

					if (cont==9){
						dg2=dg2+Math.abs(temp*3)
					}

					if (cont==10){
						 dg2=dg2+Math.abs(temp*2)
					}
					
					cont=cont+1
				}
        
				dg2=11-(dg2-((Math.floor(dg2/11))*11))
        
				if (dg2==10 || dg2==11){
        	dg2=0
				}
        
				if (dg2!=dg2_2) {
					alert("Número do CPF Inválido")
					sender.focus()
					return
				}
			}
		}
	}
}

// Habilita o campo "Publicar" nas permisses de sistemas e sees
function setaPublicar(objeto,bnId) {
	if(objeto.checked === true) {
		eval("document.getElementById('ib_secao_publicar_" + bnId + "').disabled = false;");
	} else {
		eval("document.getElementById('ib_secao_publicar_" + bnId + "').disabled = true;");
		eval("document.getElementById('ib_secao_publicar_" + bnId + "').checked  = false;");
	}
}

// Abre o pop-up para visualizao de imagens no CMS
function popupImagem(arquivo) {
	altura  = 400;
	largura = 500;
	x = (screen.width  - largura) / 2;
	y = (screen.height - altura)  / 2;
	window.open(arquivo,'imagem','left=' + x + ',top=' + y + ',height=' + altura + ',width=' + largura + ',menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no');
}

// Abre o pop-up da Galeria de Imagens no CMS
function popupGaleria(secao,idioma,campo) {
	altura  = 450;
	largura = 800;
	x = (screen.width  - largura) / 2;
	y = (screen.height - altura)  / 2;
	window.open('../secao-conteudo/popup-galeria.php?in_secao=' + secao + '&in_idioma=' + idioma + '&it_campo=' + campo,'galeria','left=' + x + ',top=' + y + ',height=' + altura + ',width=' + largura + ',menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no');
}

// Abre o pop-up da Galeria de Imagens no CMS
function popupGaleriaSistema(categoria,campo) {
	altura  = 450;
	largura = 800;
	x = (screen.width  - largura) / 2;
	y = (screen.height - altura)  / 2;
	window.open('../secao-conteudo/popup-galeria.php?tb_imagem_categoria_bn_id=' + categoria + '&it_campo=' + campo,'galeria','left=' + x + ',top=' + y + ',height=' + altura + ',width=' + largura + ',menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no');
}

// Abre o pop-up de Relacionamento de Matrias no CMS
function popupMateria(secao,idioma,campo) {
	altura  = 190;
	largura = 500;
	x = (screen.width  - largura) / 2;
	y = (screen.height - altura)  / 2;
	window.open('../secao-conteudo/popup-materia.php?in_secao=' + secao + '&in_idioma=' + idioma + '&it_campo=' + campo,'materia','left=' + x + ',top=' + y + ',height=' + altura + ',width=' + largura + ',menubar=no,location=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,toolbar=no');
}

// Abre o pop-up de Relacionamento de Matrias no CMS
function popupJanela(url, nome, altura, largura) {
	x = (screen.width  - largura) / 2;
	y = (screen.height - altura)  / 2;
	window.open(url,nome,'left=' + x + ',top=' + y + ',height=' + altura + ',width=' + largura + ',menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no');
}

// Visualiza a imagem selecionada na GALERIA DE IMAGENS
function visualizaGaleria(campo,diretorio) {
	valor = eval('document.form.' + campo + '.value;');
	if(valor != '') {
		popupImagem(diretorio + valor);
	} else {
		return;
	}
}

// Exibe ou esconde os itens de menu filhos do parametro VALOR
function ffMenu(valor) {
	if(document.getElementById(valor).style.display == 'none') {
		document.getElementById(valor).style.display = 'block';
	} else {
		document.getElementById(valor).style.display = 'none';
	}
}

// Faz a busca nas listas de SISTEMAS e SEES
function buscaLista() {
	document.form.it_acao.value = '';
	document.form.action  		= 'lista.php';
	document.form.submit();
}

// Ordena as listas
function ordenaLista(tabela,campo) {
	document.form.it_acao.value  = '';
	if(tabela != '') {
		document.form.it_ordem.value = tabela + '.' + campo;
	} else {
		document.form.it_ordem.value = campo;
	}
	 
	switch(document.form.ib_ordem.value) {
		case '0':
			document.form.ib_ordem.value = 1;
		break;
		
		case '1':
			document.form.ib_ordem.value = 0;
		break;
	}
	
	document.form.submit();
}

// Define o valor do campo de relacionamento de matérias
function relacionamentoURL(campo) {
	str = eval('document.form.' + campo + '.value;');
	if(str.indexOf('http://') == 0) {
		eval('document.form.' + campo + '_id.value = document.form.' + campo + '.value;');
	}
	if(str == '') {
		eval('document.form.' + campo + '_id.value = "";');
	}
}

function fnExpiraData(status) {
	document.form.bd_expiracao_dia.value 	= 0;
	document.form.bd_expiracao_mes.value 	= 0;
	document.form.bd_expiracao_ano.value 	= 0;
	
	if(document.form.bd_expiracao_hora && document.form.bd_expiracao_minuto) {
		document.form.bd_expiracao_hora.value 	= '';
		document.form.bd_expiracao_minuto.value = '';
	}
	
	if(status == true) {
		document.form.bd_expiracao_dia.disabled 	 = true;
		document.form.bd_expiracao_mes.disabled 	 = true;
		document.form.bd_expiracao_ano.disabled		 = true;
		
		if(document.form.ib_data_bd_expiracao) {
			document.form.ib_data_bd_expiracao.disabled  = true;
		}
		
		if(document.form.bd_expiracao_hora && document.form.bd_expiracao_minuto) {
			document.form.bd_expiracao_hora.disabled 	 = true;
			document.form.bd_expiracao_minuto.disabled 	 = true;
			
			if(document.form.ib_hora_bd_expiracao) {
				document.form.ib_hora_bd_expiracao.disabled  = true;
			}
		}
	} else {
		document.form.bd_expiracao_dia.disabled 	 = false;
		document.form.bd_expiracao_mes.disabled 	 = false;
		document.form.bd_expiracao_ano.disabled 	 = false;
		
		if(document.form.ib_data_bd_expiracao) {
			document.form.ib_data_bd_expiracao.disabled  = false;
		}
		
		if(document.form.bd_expiracao_hora && document.form.bd_expiracao_minuto) {
			document.form.bd_expiracao_hora.disabled 	 = false;
			document.form.bd_expiracao_minuto.disabled 	 = false;
			
			if(document.form.ib_hora_bd_expiracao) {
				document.form.ib_hora_bd_expiracao.disabled  = false;
			}
		}
	}
}

function fnSetaImagem(valor) {
	controle = valor;
	fnExibePaginacaoImagem();
	
	if(document.getElementById('galeria_img')) {
		document.getElementById('galeria_img').src 		  = arrImagem[controle];
	}
	
	if(document.getElementById('galeria_nome')) {
		document.getElementById('galeria_nome').innerHTML = arrImgNome[controle];
	}
	
	if(document.getElementById('galeria_txt')) {
		document.getElementById('galeria_txt').innerHTML  = arrImgTxt[controle];
	}
}

function fnExibePaginacaoImagem() {
	if(document.getElementById('pg_voltar')) {
		if(controle == 0) {
			document.getElementById('pg_voltar').style.visibility = 'hidden';
		} else {
			document.getElementById('pg_voltar').style.visibility = 'visible';
		}
	}
	
	if(document.getElementById('pg_avancar')) {
		if(controle == (total -1)) {
			document.getElementById('pg_avancar').style.visibility = 'hidden';
		} else {
			document.getElementById('pg_avancar').style.visibility = 'visible';
		}
	}
}

function fnPaginacaoImagem(direcao) {
	switch(direcao) {
		case 0:
			if(controle > 0) {
				controle--;
				fnSetaImagem(controle);
				
				if( controle == ((lista * (controlePg-1)) - 1) ) {
					controlePg--;
					fnSetaLista(controlePg);
				}
			}
		break;
		
		case 1:
		default:
			if(controle < (total - 1)) {
				controle++;
				fnSetaImagem(controle);
				
				if( controle == (lista * controlePg) ) {
					controlePg++;
					fnSetaLista(controlePg);
				}
			}
		break;
	}
}

function fnSetaLista(pagina) {
	controlePg = pagina;
	fnExibePaginacaoLista(pagina);
	
	var final  = (lista * pagina) - 1;
	var inicio = (lista * pagina) - lista;
	
	var a = 0;
	for(i = inicio; i <= final; i++) {
		if (document.getElementById('thumb_img' + a)) {
			if(arrImgThumb[i]) {
				eval("document.getElementById('thumb_img"  + a + "').style.visibility = 'visible';");
				eval("document.getElementById('thumb_img"  + a + "').src  = arrImgThumb["  + i + "];");
				eval("document.getElementById('thumb_img"  + a + "').alt  = arrImgNome["  + i + "];");
				eval("document.getElementById('thumb_link" + a + "').href = 'javascript:fnSetaImagem(\"" + i + "\");';");
			} else {
				eval("document.getElementById('thumb_img"  + a + "').style.visibility = 'hidden';");
				eval("document.getElementById('thumb_link" + a + "').href = 'javascript:;';");
			}
		}
		a++;
	}
}

function fnExibePaginacaoLista(pagina) {
	var p = Math.ceil(total / lista); 	// Total de páginas
	var a = Math.ceil(p / itens);		// Total de listas de páginas
	
	for(i = 1; i <= a; i++) {
		if(document.getElementById('paginacao_'  + i)) {
			eval("document.getElementById('paginacao_"  + i + "').style.display = 'none';");
			if(pagina <= (i * itens) && pagina >= ((i * itens) - (itens - 1))) {
				eval("document.getElementById('paginacao_"  + i + "').style.display = 'inline';");
			}
		}
	}
	
	if(document.getElementById('pg_avancar_paginacao')) {
		if(controlePg < p) {
			document.getElementById('pg_avancar_paginacao').style.visibility = 'visible';
		} else {
			document.getElementById('pg_avancar_paginacao').style.visibility = 'hidden';
		}
	}
	
	if(document.getElementById('pg_voltar_lista')) {
		if(controlePg > 1) {
			document.getElementById('pg_voltar_lista').style.visibility  	 = 'visible';
		} else {
			document.getElementById('pg_voltar_lista').style.visibility		 = 'hidden';
		}
	}
	
	if(document.getElementById('pg_avancar_lista')) {
		if(controlePg < p) {
			document.getElementById('pg_avancar_lista').style.visibility 	 = 'visible';
		} else {
			document.getElementById('pg_avancar_lista').style.visibility	 = 'hidden';
		}
	}
	
	if(document.getElementById('pg_div')) {
		if(controlePg > 1 && controlePg < p && document.getElementById('pg_div')) {
			document.getElementById('pg_div').style.visibility   			 = 'visible';
		} else {
			document.getElementById('pg_div').style.visibility				 = 'hidden';
		}
	}
}

function fnPaginacaoLista(direcao) {
	switch(direcao) {
		case 0:
			if(controlePg > 1) {
				controlePg--;
				fnSetaLista(controlePg);
			}
		break;
		
		case 1:
		default:
			if(controlePg < lista) {
				controlePg++;
				fnSetaLista(controlePg);
			}
		break;
	}
}