/*|---------------------------------->>  <<----------------------------------|

Include para ser usado na estrutura padrão dos sites. deve chamado 
no arquivo head.inc.php.

Sr. Programador, todas as funções em javascript que serão utilizadas no site
deverão serem declaradas apenas neste arquivo.

|---------------------------------->>  <<----------------------------------|*/
// Função abre nova janela
// utilização: 											PROPRIEDADES
//abrejanela('PAGINA.htm','TITULO','scrollbars=yes,resizable=no,width=500,height=400')"

function dropdown(hidden,field,hidden_value,field_value)
{
	hidden.value = hidden_value;
	field.innerHTML = field_value;
	document.getElementById('dropdown').style.display='none';
	document.getElementById('iframe').style.display='none';
}

function fechar()
{
	document.getElementById('popup').style.display='none';
}

function refresh()
{
 location = 'ajaxcontador.php';
}
	function valida_login(fcad)
{
	if(fcad.usuario.value=='')
	{
		 alert('Campo login vazio.');
		 fcad.usuario.focus();
		 return false;
	}
	if(fcad.senha.value=='')
	{
		 alert('Campo senha vazio.');
		 fcad.senha.focus();
		 return false;
	}
 fcad.submit();
}
function janela(www,h,w)
{
var height = window.screen.height - 160;
var width = window.screen.width;
var top = (height- h)/2;
var left = (width - w)/2;
window.open(www,"nova","top=" + top + ",left="+ left +",width="+w+",height="+h+",toolbar=no,location=no,status=no,menubar=no,scrollbars=no,scrolling=no,resizable=no")
} 

function valida_form(ff)
						{
						  if(ff.nome.value=='')
						  {
						    alert('Campo nome vazio.');
						    ff.nome.focus();
							  return false;
						  }
						
						   if(ff.telefone.value=='')
						  {
						    alert('Campo telefone vazio.');
						    ff.telefone.focus();
							return false;
						  }
						  if(ff.email.value=='')
						  {
						    alert('Campo email vazio.');
						    ff.email.focus();
							return false;
						  }
						  else
						  {
						  	if(!verifica_mail(ff.email.value))
							{
								alert('Email inválido.');
						    	ff.email.focus();
								return false;
							
							}
						  
						  }
						 
						   if(ff.texto.value=='')
						  {
						    alert('Campo texto vazio.');
						    ff.texto.focus();
							return false;
						  }
}

function mostrar_texto(tblObj)
		{ 
		  var x = document.getElementById('teste');
		  if(tblObj.style.display=="none") {
			tblObj.style.display = "block";
			
		  }
		  else {
			tblObj.style.display = "none";
		  }
		 return false;
		}

								function createRequestObject(){
	var request_;
	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer"){
	 	request_ = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	 	request_ = new XMLHttpRequest();
	}

	return request_;
}

var http = createRequestObject();
function getInfo2(url){
	http.open('get', url);
	http.onreadystatechange = handleInfo;
	http.send(null);
}

function getInfo(){
   
	http.open('get','ajaxcontador.php');
	http.onreadystatechange = handleInfo;
	http.send(null);
}

function handleInfo(){
	/*if(http.readyState == 1){
	 	document.getElementById('cont').innerHTML = 'Loading...';
	}*/
	if(http.readyState == 4){
		var response = http.responseText;
		document.getElementById('contador').innerHTML = response;
	}
}
var xmlhttp = createRequestObject();
function getPage(url, campo){
    //Exibe o texto carregando no div conte?do
    var conteudo=document.getElementById(campo);
    conteudo.innerHTML='<br><br><br><br><center>carregando...</center><br><br><br><br><br><br>';
    //Abre a url



    xmlhttp.open("GET", url,true);

    //Executada quando o navegador obtiver o c?digo

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            //L? o texto
            var texto=xmlhttp.responseText;
            texto = (texto);
            //Desfaz o urlencode
          //  texto=texto.replace(/\+/g," ");
           // texto=unescape(texto);
            //texto = (texto);
            //Exibe o texto no div conte?do
            var conteudo=document.getElementById(campo);

            conteudo.innerHTML=texto;
        }
    }
    xmlhttp.send(null)
}
function evita_letra3(tecla) {
	if (tecla.keyCode < 49 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}
function FormataValor3(campo,tammax,teclapres) 
	{

		var tecla = teclapres.keyCode;
		var vr = campo.value;
		vr = vr.replace( "/", "" );
		vr = vr.replace( "/", "" );
		vr = vr.replace( ",", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		tam = vr.length;
	
		if (tam < tammax && tecla != 8){ tam = vr.length + 2 ; }
	
		if (tecla == 8 ){	tam = tam - 1 ; }
			
		if ( tecla == 8 || (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) ){
			if ( tam <= 2 ){ 
				campo.value = vr ; }
			tam = tam - 1;
			if ( (tam > 2) && (tam <= 5) ){
				campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam) ; }
			if ( (tam >= 6) && (tam <= 8) ){
				campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 9) && (tam <= 11) ){
				campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 12) && (tam <= 14) ){
				campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 15) && (tam <= 17) ){
				campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam) ;}
		}
	}

	function valida_numero(ConteudoCampo)
	{
 		if ((((event.keyCode) > 47) && ((event.keyCode) < 58)))
		{
     		return(true);
		}
 		else 
		{
     		return(false);
		}
	}
	
	function FormataNumero2(Conteudo)
	{
		var teste;
		teste=String(Conteudo);
		
		Conteudo=String(Conteudo);
		var trocaP = /\./g;
		//var trocaP =".";
		var Flag=0;
		
		for (i = 0; (i < Conteudo.length); i++)
		{
			if(Conteudo.substr(i,1)==".")
			{
				Flag=1;
				
				var fim;
				
				fim=Conteudo.substr(i+1,Conteudo.length-i-1)
				Conteudo=Conteudo.substr(0,i+3);
			}
		}
		
	   	if(Flag==0)
		{
			Conteudo=String(Conteudo+".00");
		}

		NumDig = String(Conteudo);
	   	TamDig = NumDig.length;
	   	Contador = 0;
		Flag2=0;
		
	   if (TamDig > 0)
		{
			numer = "";
		  	for (i = TamDig; (i >= 0); i--)
			{
				
			  if ((parseInt(NumDig.substr(i,1))>=0) && (parseInt(NumDig.substr(i, 1))<=9))
				{
				 	Contador++;
				 	if ((Contador == 3) && Flag2==0)
				  	{
						numer = ","+numer;
				   		Contador = 0;
						Flag2=1;
				   	}
				 	else if ((Contador == 3) && Flag2==1)
				  	{
						numer = "."+numer;
				   		Contador = 0;
				  	}
				 	numer = NumDig.substr(i, 1)+numer;
				}
			}
			
		  	return numer;
			
		}
	}
	
function trimAll(sString) 
{
  while (sString.substring(0,1) == ' ')
   {
     sString = sString.substring(1, sString.length);
   }
  while (sString.substring(sString.length-1, sString.length) == ' ')
   {
     sString = sString.substring(0,sString.length-1);
   }
  return sString;
}


function rightTrim(sString) 
{
   while (sString.substring(sString.length-1, sString.length) == ' ')
    {
      sString = sString.substring(0,sString.length-1);
    }
      return sString;
}



function leftTrim(sString) 
{
   while (sString.substring(0,1) == ' ')
     {
       sString = sString.substring(1, sString.length);
     }
   return sString;
}


function formatar(src, mask) 
{
  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);
  }
}

function abrejanela(url,nome,propriedades){ 
   window.open(url,nome,propriedades);
}

//Checa o campo E-Mail
function verifica_mail(email){
	var campo = email;
	//se a "@" não existe e é o primeiro caractere 
	if (campo.indexOf("@") < 1) return false
	//se o caracter antes da "@" é "." 
	if(campo.charAt(campo.indexOf("@")-1) == ".") return false;
	//se a última incidência de "." está antes da @ 
	if (campo.lastIndexOf(".") <= campo.indexOf("@")) return false;
	//se o último caracter é ponto,
	if (campo.lastIndexOf(".")  == (campo.length-1)) return false; 
	
	return true;
}

function val_cnpj(numero) {
	//numero = numero1.value;
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 5;
	controle_2 = 6;

if ( (numero.length != 18)  || (numero.substring(2, 3) != ".") || (numero.substring(6, 7) != ".") || (numero.substring(10, 11) != "/") || (numero.substring(15, 16) != "-") ) {
     return false;
}else{ 
  	   numero = (numero.substring(0, 2)) + "" + (numero.substring(3, 6)) + "" + (numero.substring(7, 10))+ "" + (numero.substring(11, 15)) + "" + (numero.substring(16, 18)) 
	   for ( i=0 ; i < 12 ; i++) {
	        dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);
	          controle_1 = controle_1 - 1;
	        if (i == 3) {
	           controle_1 = 9;
	        }
	   }
	   
	   resto = dig_1 % 11;
	   dig_1 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_1 = 0;
	   }
	   
	   for ( i=0 ; i < 12 ; i++) {
	        dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);
	          controle_2 = controle_2 - 1;
	        if (i == 4) {
	           controle_2 = 9;
	        }
	   }
	   
	   dig_2 = dig_2 + (2 * dig_1);
	   resto = dig_2 %11;
	   dig_2 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_2 = 0;
	   }
	   
	   dig_ver = (dig_1 * 10) + dig_2;
	   
	   if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
	          return false;
	   }
 }
 return true;
}

function FormataCPF(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8){
		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 if (tam >= 7 && tam < 10) {
			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
		} else if (tam > 3 && tam < 7) {
			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
		}
	}
}

function FormataCNPJ(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	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);
		}
}

//Valida o cpf 
function val_cpf(numero2) {
  dig_1 = 0;
  dig_2 = 0;
  controle_1 = 10;
  controle_2 = 11;
  lsucesso = 1;
  
  var numero = numero2.substr(0,3) + numero2.substr(4,3) + numero2.substr(8,3) + numero2.substr(12,2);
  
  if (numero == "00000000000" || numero == "11111111111" || numero == "22222222222" || numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999") {
  	return false;
  }
  
  if ((numero.length != 11) && (numero.length != 0)) {
     return false;
  }
  else {
     for ( i=0 ; i < 9 ; i++) {
        dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
          controle_1 = controle_1 - 1;
     }
    
     resto = dig_1 % 11;
     dig_1 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
          dig_1 = 0;
     }
     for ( i=0 ; i < 9 ; i++) {
          dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
        controle_2 = controle_2 - 1;
     }
     dig_2 = dig_2 + 2 * dig_1;
     resto = dig_2 % 11;
     dig_2 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
        dig_2 = 0;
     }
     dig_ver = (dig_1 * 10) + dig_2;
     if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
          return false;
        }
  }

return true;
}

function abre_impressao(url) {
	window.open(url, "impressao", "width=600,height=450");
}
//Utilização: onkeypress="evita_letra(event)" onKeydown="FormataHora('NOMEDOCAMPO','NOMEDOFORM',event)" maxlength="5"
function FormataHora(campo,formname,teclapres){
	var tecla = teclapres.keyCode;
	vr = document[formname][campo].value;
	vr = vr.replace( ":", "" );
	tam = vr.length + 1;
	
	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 ){
			document[formname][campo].value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );
		}
	}
}
// FUNÇÃO QUE AUTOCOMPLETA DATA COM '/'
// COMO UTILIZAR:    onkeydown="FormataData(this.name,this.form.name,event);" 
function FormataData(campo,formname,teclapres) { // Máscara para os campos de data
	var tecla = teclapres.keyCode;
	vr = document[formname][campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document[formname][campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document[formname][campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
}
// COMO UTILIZAR: onkeypress="evita_letra2(event)"
// Não permite digitar letras em um input type=text
function evita_letra2(tecla) {
	if (tecla.keyCode < 48 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function ToggleAll(checked) {
    len = document.listagem.elements.length;
    var i = 0;
    for(i = 0; i < len; i++) {
        document.listagem.elements[i].checked = checked;
    }
}

function in_array(s,a) {
  for (i = 0; i < a.lenght; i++) {
	  /*if (a[i] == s) {
	  	return true;
	  }*/
	  alert(a[i]+'=='+s.name);
  }
  //return false;
}
//Checa o campo E-Mail
function verifica_mail(email){
	var campo = email;
	//se a "@" não existe e é o primeiro caractere 
	if (campo.indexOf("@") < 1) return false
	//se o caracter antes da "@" é "." 
	if(campo.charAt(campo.indexOf("@")-1) == ".") return false;
	//se a última incidência de "." está antes da @ 
	if (campo.lastIndexOf(".") <= campo.indexOf("@")) return false;
	//se o último caracter é ponto,
	if (campo.lastIndexOf(".")  == (campo.length-1)) return false; 
	
	return true;
}
function valida(ffff,ele)
{//começa valida
  
  var c=ele.split(',');
  for(x = 0; x<ffff.elements.length; x++)//varre o formulário
  {  
    //alert(c[x]);//atriz com os elementos
	//e = document.getElementById(c[x]);
	
	for(j=0;j<c.length;j++)//varre os obrigatórios
	{
	   var formul=ffff.elements[x];
	   //alert(formul);
       if(formul.name==c[j] &&  leftTrim(formul.value)=='')//se tem o elemento no formulário
	   {
	      alert('O campo '+document.getElementById(formul.name+"_label").innerHTML+' é obrigatorio');
		  document.getElementById(formul.name).focus();
		  return;
		  
	      
	   }
	}//fim for
	
  }//fim for
  ffff.submit();
}//fim valida

function writeTime()
{
	var meses = new Array('Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
	var semana = new Array('Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado');
	
	var data = new Date();
	
	document.write(
		semana[data.getDay()] +', '+
		data.getDate() +' de '+
		meses[data.getMonth()] +' &middot; '+
		(data.getHours()<10?'0'+data.getHours():data.getHours()) +':'+
		(data.getMinutes()<10?'0'+data.getMinutes():data.getMinutes()));
}

function exibeFlash(swf, width, height, wmode, cache)
{
noCache = cache || cache == undefined ? "" : "?" + new Date();
wmode = wmode || wmode == undefined ? "opaque" : "transparent";

monta_swf = "";
monta_swf += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\""+ width +"\" height=\""+ height +"\" title=\"\">";
monta_swf += "<param name=\"movie\" value=\""+ swf + noCache +"\" />";
monta_swf += "<param name=\"quality\" value=\"high\" />";
monta_swf += "<param name=\"menu\" value=\"0\" />";
monta_swf += "<param name=\"scale\" value=\"noscale\" />";
monta_swf += "<param name=\"wmode\" value=\""+ wmode +"\" />";
monta_swf += "<embed src=\""+ swf + noCache +"\" quality=\"high\" wmode=\""+ wmode +"\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+ width +"\" height=\""+ height +"\"></embed>";
monta_swf += "</object>";

document.write(monta_swf);
}