// JavaScript Document
// ===========================================================
// script: EUNANET - 08/2008
// http://www.eunanet.net
// ===========================================================
// Controle Número de Caracteres na Digitiação
// ===========================================================
contenido_textarea = ""
textovar = ""
function valida_longitud(num,varpost){
	var vpost       = document.getElementsByName(varpost);
	var num_caracteres_permitidos = num;
	var num_caracteres = vpost[0].value.length
	if (num_caracteres <= num_caracteres_permitidos){
		contenido_textarea = vpost[0].value	
	}else{
		vpost[0].value = contenido_textarea
	}
	
	if (num_caracteres >= num_caracteres_permitidos){

		// document.forms['formeditor'].caracteres.style.color="#ff0000";
	}else{
		// document.forms['formeditor'].caracteres.style.color="#000000";
	}
	cuenta(vpost[0].value.length)
}
function cuenta(vp){
	// document.forms['formeditor'].caracteres.value=vp
}
// Carrega Flash 
function carregaFlash(caminho,largura,altura) {
	document.write('<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="'+largura+'" height="'+altura+'">');
	document.write('<param name="movie" value="'+caminho+'">');
	document.write('<param name="quality" value="high">');
//	document.write('<param name="wmode" value="transparent">');
	document.write('<param name="menu" value="false">');
	document.write('<embed src="'+caminho+'" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+largura+'" height="'+altura+'"></embed>');
	document.write('</object>');
}
// Flash com Fundos Transparentes
function carregaFlashTransparente(caminho,largura,altura) {
	document.write('<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="'+largura+'" height="'+altura+'">');
	document.write('<param name="movie" value="'+caminho+'">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<param name="menu" value="false">');
	document.write('<embed src="'+caminho+'" wmode="transparent" quality="high"  menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+largura+'" height="'+altura+'"></embed>');
	document.write('</object>');
}

// ===========================================================
function calc_festa(num){
	var homem  = document.getElementsByName('homens');
	var mulher = document.getElementsByName('mulheres');
	var horas  = document.getElementsByName('tempo');
	var result = document.getElementsByName('resultado');
	if(homem[0].value == "" || mulher[0].value == "") {
		return false;
	}
	if (horas[0].value == 0) {
		percent = 0	
	}else{
		if (horas[0].value == 2) {
			percent = 1	
		}else{
			if (horas[0].value == 3) {
				percent = 1.2	
			}else{
				if (horas[0].value == 4) {
					percent = 1.3	
				}else{
					percent = 1.5
				}
			}
		}
	}	
	litros = parseInt((parseInt(homem[0].value) + parseInt(mulher[0].value)) * percent);
	result[0].value = litros+" Litros";
	return false;
}
// ===========================================================
function tabselect(tabela) {
	if (tabela == "pais") {
		var box    = document.getElementById("campo_bloco_pais");
		var vartab = document.getElementsByName('id_pai');
	} else {
		var box    = document.getElementById("campo_bloco_estado");
		var vartab = document.getElementsByName('id_est');
	}	
	box.style.display = (vartab[0].value == "999999" ? "inline" : "none" );
	return false; 
}
function tipocli(variavel) {
	var boxtipocli  = document.getElementsByName("tipo_cli");
	var varnome     = document.getElementsByName('nomerazao_cli');
	//var boxim    	= document.getElementById("campo_bloco_im");
	var boxou    	= document.getElementById("campo_bloco_ou");	
	var boxec    	= document.getElementById("campo_bloco_ec");
	var boxsx    	= document.getElementById("campo_bloco_sx");
	var boxnasc    	= document.getElementById("campo_bloco_nasc");	
	if(document.all) {
		if (boxtipocli[0].value == "J") {
			document.getElementById('tit_cnpjcpf_cli').innerText = "Nº CNPJ";
			document.getElementById('tit_rginsc_cli').innerText = "Nº Insc. Est.";
			document.getElementById('tit_nome_cli').innerText = "Razão Social";
			//document.getElementById('tit_fone1_cli').innerText = "Fone Contato";			
			//document.getElementById('tit_fone2_cli').innerText = "Fax";			
			boxec.style.display = "none";
			boxsx.style.display = "none";
			boxnasc.style.display = "none";	
			boxou.style.display = "none";				
			//boxim.style.display = "inline";
		} else {
			document.getElementById('tit_cnpjcpf_cli').innerText = "Nº CPF";
			document.getElementById('tit_rginsc_cli').innerText = "Nº do RG";		
			document.getElementById('tit_nome_cli').innerText = "Nome";		
			//document.getElementById('tit_fone1_cli').innerText = "Fone Contato";			
			//document.getElementById('tit_fone2_cli').innerText = "Fone Comercial";			
			boxec.style.display = "inline";
			boxsx.style.display = "inline";			
			boxnasc.style.display = "inline";
			boxou.style.display = "inline";				
			//boxim.style.display = "none";			
		}
	} else {
		if (boxtipocli[0].value == "J") {
			document.getElementById('tit_cnpjcpf_cli').textContent = "Nº CNPJ";
			document.getElementById('tit_rginsc_cli').textContent = "Nº Insc. Est.";
			document.getElementById('tit_nome_cli').textContent = "Razão Social";
			//document.getElementById('tit_fone1_cli').textContent = "Fone Contato";			
			//document.getElementById('tit_fone2_cli').textContent = "Fax";			
			boxec.style.display = "none";
			boxsx.style.display = "none";			
			boxnasc.style.display = "none";
			boxou.style.display = "none";			
			//boxim.style.display = "inline";				
		} else {
			document.getElementById('tit_cnpjcpf_cli').textContent = "Nº CPF";
			document.getElementById('tit_rginsc_cli').textContent = "Nº do RG";		
			document.getElementById('tit_nome_cli').textContent = "Nome";		
			//document.getElementById('tit_fone1_cli').textContent = "Fone Contato";			
			//document.getElementById('tit_fone2_cli').textContent = "Fone Comercial";
			boxec.style.display = "inline";
			boxsx.style.display = "inline";
			boxou.style.display = "inline";				
			boxnasc.style.display = "inline";						
			//boxim.style.display = "none";			
		}
	}	
	var vpesqfoc = varnome[0].focus();
	return false; 
}
// ===========================================================
function carrinho_mais(lista) {
   var lista_prd = lista;
   var varqde = "";
   var separa = "";
   var qde = document.getElementsByName('quant[]');
   for (i = 0; i < qde.length; i++) {
		varqde = varqde+separa+qde[i].value; 
		separa = "|";
	}
	exibeget('carrinho.php?quants='+varqde+'&'+lista_prd+'&acao=A','div1','L','D','D','D');	
	return false; 
}	
// ===========================================================
function exibedivs(cdiv1,cdiv2,cdiv3,cdiv4) {
	var box1 = document.getElementById('div1');
	var box2 = document.getElementById('div2');
	var box3 = document.getElementById('div3');
	var box4 = document.getElementById('div4');
	box1.style.display = (cdiv1 == "L" ? "inline" : "none" );
	box2.style.display = (cdiv2 == "L" ? "inline" : "none" );
	box3.style.display = (cdiv3 == "L" ? "inline" : "none" );
	box4.style.display = (cdiv4 == "L" ? "inline" : "none" );
	return false;
}
function esconde(divesconde) {
	var boxesconde = document.getElementById(divesconde);
	boxesconde.style.display="none";
	return false;
}
function exibe(divesconde,tipo) {
	var boxesconde = document.getElementById(divesconde);
	boxesconde.style.display=tipo;
	return false;
}
function nadafaz(nada) {
	return true;
}
function exibeget(varpag,vardiv,cd1,cd2,cd3,cd4) {
	var vcd1     = cd1;
	var vcd2     = cd2;
	var vcd3     = cd3;
	var vcd4     = cd4;
	exibedivs(vcd1,vcd2,vcd3,vcd4);
	var vpag     = varpag;
	var vdiv     = vardiv;
	var ajax     = new AJAX();
	ajax.Updater(vpag,vdiv,"GET","<img src='images/loading.gif' />");
	return false;
}
function exibepost(varform,varpag,vardiv,cd1,cd2,cd3,cd4) {
	var vcd1     = cd1;
	var vcd2     = cd2;
	var vcd3     = cd3;
	var vcd4     = cd4;	
	var vform    = varform;	
	var vpag     = varpag;
	var vdiv     = vardiv;
	exibedivs(vcd1,vcd2,vcd3,vcd4);
	micoxUpload(vform,vpag,vdiv,'images/loading.gif','Erro ao carregar');			
	return false;
}
function exibegetmenu(varpag,vardiv) {
	var vpag     = varpag;
	var vdiv     = vardiv;
	var ajax     = new AJAX();
	ajax.Updater(vpag,vdiv,"GET","<img src='images/loading.gif' />");
	return false;
}
function exibetab(vartab) {
	var vtabmenu        = vartab+".php";
	var vtablista       = vartab+"lista.php";
	exibedivs("L","D","D","D")
	var ajax = new AJAX();
	ajax.Updater(vtabmenu,'div1',"GET","<img src='images/loading.gif' />");
	setTimeout("exibeget(\'"+vtablista+"\',\'div2\',\'L\',\'L\',\'D\',\'D\')",1000);
}
function exibegettram(id) {
	var vtabmenu        = "processostramita.php?id="+id;
	var vtablista       = "processostramitalista.php?id="+id;
	exibedivs("L","D","D","D")
	var ajax = new AJAX();
	ajax.Updater(vtabmenu,'div1',"GET","<img src='images/loading.gif' />");
	setTimeout("exibeget(\'"+vtablista+"\',\'div2\',\'L\',\'L\',\'D\',\'D\')",1000);
}
function exibeoscli(vartab) {
	var vtablista       = vartab+".php";
	var os            = document.getElementsByName('id_os');
	var fone          = document.getElementsByName('fone');
	var vtablista     = vtablista+"?txt="+os[0].value+"&fone="+fone[0].value;	
	exibedivs("D","L","D","D")
	var ajax = new AJAX();
	ajax.Updater(vtablista,'div2',"GET","<img src='images/loading.gif' />");
}
function zaracache(aurl){
        var dt = new Date();
        if(aurl.indexOf("?")>=0){// já tem parametros
            return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
        }else{ return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
    }
function exibepesq(vartab) {
	var vtablista       = vartab+"lista.php";
	var pesq            = document.getElementsByName('pesquisa_tab');
	var vpesq           = pesq[0].value;
	if(vpesq.length <= 0) {
		alert("Digite algo para pesquisar!")
		return false
	}
	exibedivs("L","L","D","D");
	var ajax = new AJAX();
	ajax.Updater(vtablista+'?txt='+vpesq,'div2',"GET","<img src='images/loading.gif' />");
}
function exibepesqdup(vartab) {
	var vtablista       = vartab+"lista.php";
	var pag_out       = document.getElementsByName('filtra_out');	
	var vpag_out      = pag_out[0].value;	
	var pesq            = document.getElementsByName('pesquisa_tab');
	var vpesq           = pesq[0].value;
	if(vpesq.length <= 0) {
		alert("Digite algo para pesquisar!")
		return false
	}
	exibedivs("L","L","D","D");
	var ajax = new AJAX();
	ajax.Updater(vtablista+'?txt='+vpesq+'&out='+vpag_out,'div2',"GET","<img src='images/loading.gif' />");
}
function exibepaginacao(vartab) {
	var vtablista     = vartab+"lista.php";
	if (vartab == "estados" || vartab == "cidades" || vartab == "subcategorias") {
		var pag_out       = document.getElementsByName('filtra_out');	
		var vpag_out      = pag_out[0].value;
	}
	var pag_tab       = document.getElementsByName('numpag_tab');
	var vpag_tab      = pag_tab[0].value;
	var pesq          = document.getElementsByName('pesquisa_tab');
	var vpesq         = pesq[0].value;
	var vpesqfoc      = pesq[0].focus();
	if(vpesq.length > 0) {
		vpesq = "&txt="+vpesq
	} else {
	    vpesq = ""
		pesq[0].value = ""
	}
	exibedivs("L","L","D","D");
	var ajax = new AJAX();
	ajax.Updater(vtablista+'?nreg='+vpag_tab+vpesq+'&out='+vpag_out,'div2',"GET","<img src='images/loading.gif' />");
}
function exibetodos(vartab) {
	var varreg          = document.getElementsByName('numpag_tab');
	var vtablista     = vartab+"lista.php?nreg="+varreg[0].value;
	var pesq          = document.getElementsByName('pesquisa_tab');
	pesq[0].value     = "";
	exibedivs("L","L","D","D");
	var ajax = new AJAX();
	ajax.Updater(vtablista,'div2',"GET","<img src='images/loading.gif' />");
}
function exibetodosdup(vartab) {
	var vtablista     = vartab+"lista.php";
	var pesq          = document.getElementsByName('pesquisa_tab');
	pesq[0].value     = "";
	var pag_out       = document.getElementsByName('filtra_out');	
	pag_out[0].value  = "0";
	exibedivs("L","L","D","D");
	var ajax = new AJAX();
	ajax.Updater(vtablista,'div2',"GET","<img src='images/loading.gif' />");
}
function exibeselect(pag,div,psq,acao) {
	var vpag          = pag;
	var vdiv          = div;
	var vacao         = acao;
	var pesq          = document.getElementsByName(psq);
	var vpsq          = pesq[0].value;
	var ajax = new AJAX();
	ajax.Updater(vpag+"?var="+vpsq+"&acao="+vacao,vdiv,"GET","<img src='images/loading.gif' />");
}
function exibeselectcli(pag,div,psq,acao) {
	var vpag          = pag;
	var vdiv          = div;
	var vacao         = acao;
	var pesq          = document.getElementsByName(psq);
	var vpsq          = pesq[0].value;
	var ajax = new AJAX();
	ajax.Updater(vpag+"?var="+vpsq+"&acao="+vacao,vdiv,"GET","<img src='images/loading1.gif' />");
}
function replautos() {
   var mesmo = document.getElementsByName('mesmoprincipal');
   var nomemot = document.getElementsByName('nomemot_car');
   var nomecli = document.getElementsByName('nomerazao_cli');
   var tipocli = document.getElementsByName('tipo_cli');
   var cpfm    = document.getElementsByName('cpfmot_car');
   var rgm 	   = document.getElementsByName('rgmot_car');
   var sexm    = document.getElementsByName('sexomot_car');
   var nascm   = document.getElementsByName('nascmot_car');
   var ecivilm = document.getElementsByName('estadocivilmot_car');
   var cepm    = document.getElementsByName('ceppernoite_car');
   var cpfc    = document.getElementsByName('cnpjcpf_cli');
   var rgc 	   = document.getElementsByName('rginsc_cli');
   var sexc    = document.getElementsByName('sexo_cli');
   var nascc   = document.getElementsByName('nascimento_cli');
   var ecivilc = document.getElementsByName('estadocivil_cli');
   var cepc    = document.getElementsByName('cep_cli');
	if(mesmo[0].value=="Sim") { 
		tipocli[0].value ="F";
		nomecli[0].value = nomemot[0].value;	
		cpfc[0].value = cpfm[0].value;
		rgc[0].value = rgm[0].value;
		sexc[0].value = sexm[0].value;
		nascc[0].value = nascm[0].value;
		ecivilc[0].value = ecivilm[0].value;
		cepc[0].value = cepm[0].value;	
		return false;
	}
}
function tipomulher() {
	var boxtpresid = document.getElementsByName('sexo_cli');
	var boxesconde = document.getElementById('campo_bloco_ou');
	if(boxtpresid[0].value=="Masculino") {
		boxesconde.style.display="none";
	} else {
		boxesconde.style.display="inline";
	}
	return false;
}
function tiporesid() {
	var boxtpresid = document.getElementsByName('tiporesid_cli');
	var boxesconde = document.getElementById('campo_bloco_ou');
	if(boxtpresid[0].value=="Veraneio") {
		boxesconde.style.display="none";
	} else {
		boxesconde.style.display="inline";
	}
	return false;
}
function tipoempresa() {
	var boxtpresid = document.getElementsByName('tipo_cli');
	var boxesconde = document.getElementById('campo_bloco_ou');
	if(boxtpresid[0].value=="Pes. Física Prop. Imóvel") {
		boxesconde.style.display="none";
	} else {
		boxesconde.style.display="inline";
	}
	return false;
}
function marcaregtab() {
   var cks = document.getElementsByName('marcadel[]');
   for (i=0;i<cks.length;i++) 
	  if(cks[i].type == "checkbox") 
		if(cks[i].checked==1)
			cks[i].checked=0
		else	 
			cks[i].checked=1
		 return false;
}		
function delregtab(tab) {
   var tabela = tab;
   var varcks = "";
   var separa = "";
   var temreg = 0;
   var cks = document.getElementsByName('marcadel[]');
   for (i = 0; i < cks.length; i++) {
		if(cks[i].checked) {
			varcks = varcks+separa+cks[i].value; 
			separa = "|";
			temreg = 1;
		}
	}
	if (temreg == 1) { 
		if (confirm("Confirma a Exclusão dos Registros Marcados?")) {
			exibeget(tabela+".php?acao=D&cks="+varcks,'div2','L','L','D','D');
		} else {
			alert("Exclusão Concelada!");
		}
		return false; 
	}
	alert("Selecione pelo menos um Registro para apagar!!!"); 
	return false; 
}
function tirafocus(varfocus) {
	var vtab     = document.getElementsByName(varfocus);
	coloca_o_focus_em_ok_sis = vtab[0].focus();
	return false;
}
function colunastab(acao_sis) {
	var colunas = document.getElementsByName('numcol_ptb');
	exibedivs("D","L","D","D");
	var ajax = new AJAX();
	ajax.Updater('planostabelascol.php?nc='+colunas[0].value+'&acao='+acao_sis,'campo_bloco_ou',"GET","<img src='images/loading.gif' />");
}
/**  
 * Função para aplicar máscara em campos de texto
 * Copyright (c) 2008, Dirceu Bimonti Ivo - http://www.bimonti.net 
 * All rights reserved. 
 * @constructor  
 */ 
 
/* Version 0.27 */

/**  
  * Função Principal 
  * @param w - O elemento que será aplicado (normalmente this).
  * @param e - O evento para capturar a tecla e cancelar o backspace.
  * @param m - A máscara a ser aplicada.
  * @param r - Se a máscara deve ser aplicada da direita para a esquerda. Veja Exemplos.
  * @param a - 
  * @returns null  
  */
function maskIt(w,e,m,r,a){
    
    // Cancela se o evento for Backspace
    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    
    // Variáveis da função
    var txt  = (!r) ? w.value.replace(/[^\d]+/gi,'') : w.value.replace(/[^\d]+/gi,'').reverse();
    var mask = (!r) ? m : m.reverse();
    var pre  = (a ) ? a.pre : "";
    var pos  = (a ) ? a.pos : "";
    var ret  = "";

    if(code == 9 || code == 8 || txt.length == mask.replace(/[^#]+/g,'').length) return false;

    // Loop na máscara para aplicar os caracteres
    for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
        if(mask.charAt(x)!='#'){
            ret += mask.charAt(x); x++;
        } else{
            ret += txt.charAt(y); y++; x++;
        }
    }
    
    // Retorno da função
    ret = (!r) ? ret : ret.reverse()    
    w.value = pre+ret+pos;
}

// Novo método para o objeto 'String'
String.prototype.reverse = function(){
    return this.split('').reverse().join('');
};