//FUNÇÕES DA DATA DO CABEÇALHO
function DataAtual()
{
	var day;
	var month;
	var now = new Date(); 
	var hours = now.getHours(); 
	var minutes = now.getMinutes(); 
	var timeValue = "" + ((hours >12) ? hours -12 :hours)
	timeValue += ((minutes < 10) ? ":0" : ":") + minutes
	timeValue += (hours >= 12) ? " PM" : " AM" 
	timerRunning = true; 
	
	mydate = new Date(); 
	myday = mydate.getDay(); 
	mymonth = mydate.getMonth();
	myweekday= mydate.getDate(); 
	weekday= myweekday; 
	myyear= mydate.getYear(); 
	  if (myyear < 2000)
      myyear = 1900 + myyear
	year = myyear

	switch(myday)
	{
		case 0 : day = " Domingo, " ; break;
		case 1 : day = " Segunda-feira, " ; break;
		case 2 : day = " Terça-feira, " ; break;
		case 3 : day = " Quarta-feira, " ; break;
		case 4 : day = " Quinta-feira, " ; break;
		case 5 : day = " Sexta-feira, " ; break;
		case 6 : day = " Sábado, " ;
	}

	switch(mymonth)
	{
		case 0 : month = " de Janeiro de " ; break;
		case 1 : month = " de Fevereiro de " ; break;
		case 2 : month = " de Março de " ; break;
		case 3 : month = " de Abril de " ; break;
		case 4 : month = " de Maio de " ; break;
		case 5 : month = " de Junho de "; break;
		case 6 : month = " de Julho de " ;break;
		case 7 : month = " de Agosto de " ; break;
		case 8 : month = " de Setembro de " ; break;
		case 9 : month = " de Outubro de " ; break;
		case 10 : month = " de Novembro de " ; break;
		case 11 : month = " de Dezembro de " ;break;
	}	
	
	document.write( day + myweekday + month + year); 
}

function currencyFormat(fld, milSep, decSep, e)
{
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if(whichCode == 13)
		return true; // Enter
	
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if(strCheck.indexOf(key) == -1)
		return false;  // Not a valid key
	
	len = fld.value.length;
	for(i = 0; i < len; i++)
		if((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep))
			break;
	
	aux = '';
	for(; i < len; i++)
		if(strCheck.indexOf(fld.value.charAt(i))!=-1)
			aux += fld.value.charAt(i);
	
	aux += key;
	len = aux.length;
	if(len == 0) fld.value = '';
	
	// Aqui tem que testar: se $Column->{decimals} for 3 coloca + '0' + '0' + senão, apenas + '0' +
	if(len == 1) fld.value = '0' + decSep + '0' + aux; // Alterei coloquei + '0' , decSep = , (vírgula)
	
	// Aqui tem que testar: se $Column->{decimals} for 3 coloca + '0' + senão, não coloca
	if(len == 2) fld.value = '0' + decSep + aux; // Alterei coloquei + '0'
	
	// Aqui tem que testar: se $Column->{decimals} for 3 coloca essa linha senão não coloca
	//if (len == 3) fld.value = '0' + decSep + aux + aux2; // Adicionei esta linha
	
	// Aqui tem que colocar a variável $decimal
	if(len > 2)
	{ // Alterei de 2 para 3
		aux2 = '';
		
		// A cada três caracteres adiciona um milSep (ponto ".")
		for (j = 0, i = len - 3; i >= 0; i--)
		{
			if (j == 3)
			{
				aux2 += milSep;
				j = 0;
	  		}
			
			aux2 += aux.charAt(i);
			j++;
		 }
	
		fld.value = '';
		len2 = aux2.length;
		
		// Aqui tem que testar: se $Column->{decimals} for 3 coloca i > 0 senão coloca i >= 0
		for (i = len2 - 1; i >= 0; i--) // Alterei de i >= 0 para i > 0
			fld.value += aux2.charAt(i);
		
		// Aqui tem que coloca a variável mo lugar do 3
		fld.value += decSep + aux.substr(len - 2, len); // O número dois é o valor chave (número de casas que vem depois da vírgula)
	}
	
	return false;
}

function showdiv(ativar,id1,id2)
{   
	var A=document.getElementById(id1);
	var B=document.getElementById(id2);
	
	if(ativar=='sim')
	{
	  B.style.display = 'block';
	  A.style.display= 'none';
	}
	else
	{
	  A.style.display = 'block';
	  B.style.display= 'none';
	}
}

//FUNÇÕES DA NOTICIA DA PAGINA PRINCIPAL
//Seta a velocidade do scroll.
var scrollspeed = cache = 2;
//Seta o tempo que o scroll vai ficar parado antes de começar a subir (em milisegundos)	
var initialdelay = 2000;

function initializeScroller()
{
	dataobj = document.all ? document.all.datacontainer : document.getElementById("datacontainer");
	dataobj.style.top = "5px";
	setTimeout("getdataheight()", initialdelay);
}
	
function getdataheight()
{
	thelength = dataobj.offsetHeight;
	if(thelength == 0)
	{
		setTimeout("getdataheight()", 10);
	}
	else
	{
		scrollDiv();
	}
}

function scrollDiv()
{
	dataobj.style.top = parseInt(dataobj.style.top) - scrollspeed + "px";
	if(parseInt(dataobj.style.top) < thelength * (-1))
	{
		dataobj.style.top = "320px";
	}
	setTimeout("scrollDiv()", 120);
}

if (window.addEventListener)
{
	window.addEventListener("load", initializeScroller, false);
}
else if(window.attachEvent)
{
	window.attachEvent("onload", initializeScroller);
} 
else
{
	window.onload = initializeScroller;
}

//FUNÇÃO DO MENU DO SITE
function Menu(id)
{
	var ulfilho = id.parentNode.getElementsByTagName("ul")[0];

	//FAZENDO A TROCA DO ESTADO ABERTO E FECHADO DA LISTA
	if(ulfilho.className.indexOf('aberto')>-1)
	{
		ulfilho.className = ulfilho.className.replace("aberto","fechado");
	}
	else
	{
		ulfilho.className = ulfilho.className.replace("fechado","aberto");
	}
	//FAZENDO A TROCA DA IMAGEM QUE REPRESENTA O ESTADO ABERTO E FECHADO DA LISTA
	if(ulfilho.className.indexOf('aberto')>-1)
	{
		id.className = "laberto";
	}
	else
	{
		id.className = "lfechado";
	}

	return false;
}

//FUNÇÃO QUE COLOCA A MASCARA DA DATA
function data(e,src,mask)
{
	if(window.event) 
	{ 
		_TXT = e.keyCode; 
	}
    else if(e.which) 
    { 
		_TXT = e.which; 
	}
	
	if(_TXT > 47 && _TXT < 58) 
    {
		var i = src.value.length; 
		var saida = mask.substring(0,1); 
		var texto = mask.substring(i)
		
		if (texto.substring(0,1) != saida)
		{ 
			src.value += texto.substring(0,1); 
		}
   		return true; 
	} 
	else 
	{ 
		if (_TXT != 8) 
		{ 
			return false; 
		}
		else 
		{ 
			return true; 
		}
	}
}

//FUNÇÃO QUE FORMATA NUMERO DE TELEFONE
function formatCelular(e,strField)
{
	if(window.event) 
	{ 
		_TXT = e.keyCode; 
	}
    else if(e.which) 
    { 
		_TXT = e.which; 
	}
	if(_TXT > 47 && _TXT < 58) 
    {
		var oLen = strField.value.length; 
		if(oLen == 0){strField.value += "(";}
		if(oLen == 3){strField.value += ") ";}
		if(oLen == 9){strField.value += "-";}
	} 
	else 
	{ 
		if (_TXT != 8) 
		{ 
			return false; 
		}
		else 
		{ 
			return true; 
		}
	}
}

//FUNÇÃO QUE COLOCA A MASCARA DO CEP
function MascaraCep(keypress, objeto)
{
	campo = eval (objeto);
	caracteres = '01234567890';

	if (document.all)
		eventoKey = window.event.keyCode;
	else if(document.layers || navigator.appName == "Netscape")
		eventoKey = keypress.which;
	else
		eventoKey = keypress.keyCode;
	
	if ((caracteres.search(String.fromCharCode(eventoKey))!=-1) || eventoKey == 8 || eventoKey == 0)
	{
		if (eventoKey != 8)
		{
			if (campo.value.length == 2)
				campo.value = campo.value + '.';
			if (campo.value.length == 6)
				campo.value = campo.value + '-';
		}
		return true	;
	}
	else
		return false;
}

//FUNÇÃO QUE CHAMA AS FUNÇÕES DE MASCARA DO CPF E CNPJ
function MascaraCnpjCpf(form, keypress, objeto)
{
	caracteres = '01234567890';
	
	if (document.all)
		eventoKey = window.event.keyCode;
	else if(document.layers || navigator.appName == "Netscape")
		eventoKey = keypress.which;
	else
		eventoKey = keypress.keyCode;
	
	if ((caracteres.search(String.fromCharCode(eventoKey))!=-1) || eventoKey == 8 || eventoKey == 0 )
	{
		if (eventoKey != 8)
		{
			if (form.cpfcnpj[0].checked)
			{
				MascaraCpf(keypress, objeto);
			}
			else
			{
				MascaraCnpj(keypress, objeto);
			}
		}
		return true	;
	}
	else
		return false;
}

//FUNÇÃO QUE COLOCA A MASCARA DO CPF
function MascaraCpf(keypress, objeto)
{
	campo = eval (objeto);
	
	if (campo.value.length == 3 || campo.value.length == 7)
		campo.value = campo.value + '.';
	if (campo.value.length == 11)
		campo.value = campo.value + '-';	
}

//FUNÇÃO QUE COLOCA A MASCARA DO CNPJ
function MascaraCnpj(keypress, objeto)
{
	campo = eval (objeto);
	
	if (campo.value.length == 2 || campo.value.length == 6)
		campo.value = campo.value + '.';
	if (campo.value.length == 10)
		campo.value = campo.value + '/';
	if (campo.value.length == 15)
		campo.value = campo.value + '-';
}

//FUNÇÃO QUE VALIDA CNPJ
function validarCnpj(cnpj)
{
	var
	   primeiroDigito, 
	   SegundoDigito,
	   n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12;
	   objCnpj = cnpj.replace(".", "").replace(".", "").replace("/", "").replace("-", "")
	
	   n1 = parseInt(objCnpj.charAt(0)); 
	   n2 = parseInt(objCnpj.charAt(1)); 
	   n3 = parseInt(objCnpj.charAt(2)); 
	   n4 = parseInt(objCnpj.charAt(3)); 
	   n5 = parseInt(objCnpj.charAt(4)); 
	   n6 = parseInt(objCnpj.charAt(5)); 
	   n7 = parseInt(objCnpj.charAt(6)); 
	   n8 = parseInt(objCnpj.charAt(7)); 
	   n9 = parseInt(objCnpj.charAt(8)); 
	   n10 = parseInt(objCnpj.charAt(9)); 
	   n11 = parseInt(objCnpj.charAt(10)); 
	   n12 = parseInt(objCnpj.charAt(11));
		
	  primeiroDigito = n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
	  primeiroDigito = 11 - (primeiroDigito % 11);
	  if(primeiroDigito >= 10)
	  	primeiroDigito = 0;
		
	   SegundoDigito = primeiroDigito*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
	   SegundoDigito = 11 - (SegundoDigito % 11);
	  if( SegundoDigito >= 10)
	  	SegundoDigito = 0;
	if (primeiroDigito+SegundoDigito != parseInt(objCnpj.charAt(12))+parseInt(objCnpj.charAt(13)))
		return true;
	else
		return false;
	
	
};

//FUNÇÃO QUE VALIDA O CPF
function validarCpf(cpf)
{
	var
	   primeiroDigito, 
	   SegundoDigito,
	   n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12;
	
	if (cpf.length > 14) return true;
	
	objCpf = cpf.replace(".", "").replace(".", "").replace("-", "")
	
	n1 = parseInt(objCpf.charAt(0)); 
	n2 = parseInt(objCpf.charAt(1)); 
	n3 = parseInt(objCpf.charAt(2)); 
	n4 = parseInt(objCpf.charAt(3)); 
	n5 = parseInt(objCpf.charAt(4)); 
	n6 = parseInt(objCpf.charAt(5)); 
	n7 = parseInt(objCpf.charAt(6)); 
	n8 = parseInt(objCpf.charAt(7)); 
	n9 = parseInt(objCpf.charAt(8)); 
	
	
	primeiroDigito = n9*2+n8*3+n7*4+n6*5+n5*6+n4*7+n3*8+n2*9+n1*10;
	primeiroDigito = 11 - (primeiroDigito % 11);
	if (primeiroDigito >= 10)
		primeiroDigito = 0;
		
	SegundoDigito = primeiroDigito*2+n9*3+n8*4+n7*5+n6*6+n5*7+n4*8+n3*9+n2*10+n1*11;
	SegundoDigito = 11 - (SegundoDigito % 11)
	if (SegundoDigito >= 10)
		SegundoDigito = 0;
	
	if (primeiroDigito+SegundoDigito != parseInt(objCpf.charAt(9))+parseInt(objCpf.charAt(10)))
		return true;
	else
		return false;
}

//FUNÇÃO QUE FORMATA NUMEROS (QUANTIDADE E VALOR)
function formatar_valor(valor)
{	
	obj = eval(valor);	
	valorItem = "" + obj.value;
	
	valorItem =valorItem.replace(".", "");
	if (valorItem.length == 0) exit;
	if (valorItem.charAt(0)==",")
	{
		resultado = "0" + valorItem;	
	}
		
	else
	{
		pos = valorItem.indexOf(',');
		
		if(pos > 0)
		{
			inteiro = substring(valorItem, 0, pos);
			fracao  = substring(valorItem, pos + 1);
		}
		else
		{
			inteiro = valorItem;
			fracao  = "00";
		}
		
		tamanho = inteiro.length;
		
		novoInteiro = "";
		
		for (i=0; i <  parseInt(tamanho); i++)
		{
			if (((i%3) == 0) && (i > 2))
				novoInteiro = "." + novoInteiro;
				novoInteiro = inteiro.charAt(parseInt(tamanho) - i -1) + novoInteiro;
		}
		
		if ( fracao.length == 1 )
			fracao +=  "0";
		if (!(novoInteiro) || novoInteiro == "")
			novoInteiro = "0";
		if (fracao.length > 2)
			fracao = substring(fracao,0,2);
					
		resultado = novoInteiro + "," + fracao;						 
	}
		obj.value =  resultado;	
}

/**
 *
 *	FUNÇÃO PARA SÓ PODER DIGITAR NUMEROS
 */
function somente_numeros(e) //recebe o evento
{
	if(window.event) 
	{
		_TXT = e.keyCode; 
	}
    else if(e.which) 
    { 
		_TXT = e.which;
	}
	
	/*
	* codigo asc dos numeros 0 a 9 (48 a 57), 8 codigo do backspace para apagar
	* compara se o evento esta no intervalo permitido se tiver retorna true e o 
	* valor e exibido na caixa de texto ou se podera apagar se o codigo recuperado 
	* for 8 do backspace retornando true também se podendo apagar os valores.
	*/ 
	if(_TXT > 47 && _TXT < 58 || _TXT == 8 || _TXT == 46)
    {
		return true; 
	} 
	else
	{
		return false; 
	}
}

//FUNÇÃO QUE SERA EXECUTADA QUANDO FOR TECLADO ENTER NA AUTENTICAÇÃO
function evento_autenticacao(e)
{	
	codigo_asc = e.keyCode;

	if(codigo_asc == 13)
		autenticacao_balcao();
}

//FUNÇÃO QUE ENVIA O FORMULARIO DE AUTENTICAÇÃO
function autenticacao_balcao()
{
	document.autenticacao.tipo.value = "autenticacao";
	document.getElementById('autenticacao').submit();
}

function enviaBusca()
{
	var Busca = document.cabecalho_busca.busca.value;
	document.cabecalho_busca.busca.value = Busca;
	document.getElementById('cabecalho_busca').submit();
}

//VALIDAR CPF
function valida_cpf(cpf)
{
	cpf = cpf.replace(/\./g, '');
	cpf = cpf.replace(/-/g, '');
	
	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;
}

//VALIDAR CNPJ
function valida_cnpj(cnpj)
{
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	
	digitos_iguais = 1;
	
	if(cnpj.length < 14)
		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);
}

function popup(){
	window.open('index.php','popup','width=200,height=100,scrolling=auto,top=0,left=0')
}

function fechar(){
    document.getElementById('popup').style.display = 'none';
}
    // Aqui definimos o tempo para fechar o pop-up automaticamente
function abrir(){
    document.getElementById('popup').style.display = 'block';
    setTimeout ('fechar()', 3000);
}