/*Função que padroniza valor monétario*/
function maskValor(v){
	v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
	v=v.replace(/^([0-9]{3}\.?){3}-[0-9]{2}$/,"$1.$2");
	//v=v.replace(/(\d{3})(\d)/g,"$1,$2")
	v=v.replace(/(\d)(\d{2})$/,"$1.$2") //Coloca ponto antes dos 2 últimos digitos
	return v
}

/*Função que padroniza CEP*/
function maskCep(v){
	v = v.replace(/\D/g,"")
	if(v.length >= 5){
		v = v.substr(0, 5) +"-"+v.substr(5, 3);	
	}
	if(v.length > 9){
		v = v.substr(0, 9);	
	}
	return v
}

/*Função que padroniza telefone (11) 41841241*/
function maskTelefone(v){
	
	v=v.replace(/\D/g,"")                 
	v=v.replace(/^(\d\d)(\d)/g,"($1) $2")
	if(v.length > 13){
		v = v.substr(0, 13);	
	}
	return v
}

/*Função que permite apenas numeros*/
function maskNum(v){
	return v.replace(/\D/g,"")
}

function maskData(v){        
	v = v.replace(/\D/g,"");         
	v = v.replace(/(\d{2})(\d)/,"$1/$2");         
	v = v.replace(/(\d{2})(\d)/,"$1/$2");         
	if(v.length > 10){
		v = v.substr(0, 10);	
	}
	return v    
}

function ValorComPonto(v){
	v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
	v=v.replace(/(\d+)(\d{2})/,"$1.$2")	//Insere a vírgula
	//v=v.replace(/^(0+)(\d)/g,"$2"); //remove "0" à esquerda
	return v
}

function ValorComVirgula(v){
	v=v.replace(/\D/g,"") //Remove tudo o que não é dígito
	v=v.replace(/(\d+)(\d{2})/,"$1,$2")	//Insere a vírgula
	v=v.replace(/^(0+)(\d)/g,"$2"); //remove "0" à esquerda
	return v
}

function maskHora(v){        
	v = v.replace(/\D/g,"");         
	v = v.replace(/(\d{2})(\d)/,"$1:$2");         
	v = v.replace(/(\d{2})(\d)/,"$1:$2");         
	if(v.length > 8){
		v = v.substr(0, 8);	
	}
	return v    
}

function limpa(str){
	
	try{
		while(str.indexOf('Á') !== -1){
			str = str.replace("Á", "&Aacute;");
		}	
		
		while(str.indexOf('á') !== -1){
			str = str.replace("á", ";aacute;");
		}	
		
		while(str.indexOf('é') !== -1){
			str = str.replace("é", ";eacute;");
		}	
		
		while(str.indexOf('É') !== -1){
			str = str.replace("É", ";Eacute;");
		}	
		
		while(str.indexOf('Í') !== -1){
			str = str.replace("Í", ";Iacute;");
		}	
		
		while(str.indexOf('í') !== -1){
			str = str.replace("í", ";iacute;");
		}	
		
		while(str.indexOf('Ó') !== -1){
			str = str.replace("Ó", ";Oacute;");
		}	
		
		while(str.indexOf('ó') !== -1){
			str = str.replace("ó", ";oacute;");
		}	
		
		while(str.indexOf('Ú') !== -1){
			str = str.replace("Ú", ";Uacute;");
		}	
		
		while(str.indexOf('ú') !== -1){
			str = str.replace("ú", ";uacute;");
		}	
		
		while(str.indexOf('Â') !== -1){
			str = str.replace("Â", ";Acirc;");
		}	
		
		while(str.indexOf('â') !== -1){
			str = str.replace("â", ";acirc;");
		}	
		
		while(str.indexOf('Ê') !== -1){
			str = str.replace("Ê", ";Ecirc;");
		}	
		
		while(str.indexOf('ê') !== -1){
			str = str.replace("ê", ";ecirc;");
		}	
		
		while(str.indexOf('Ô') !== -1){
			str = str.replace("Ô", ";Ocirc;");
		}	
		
		while(str.indexOf('ô') !== -1){
			str = str.replace("ô", ";ocirc;");
		}	
		
		while(str.indexOf('Ã') !== -1){
			str = str.replace("Ã", ";Atilde;");
		}	
		
		while(str.indexOf('ã') !== -1){
			str = str.replace("ã", ";atilde;");
		}	
		
		while(str.indexOf('Õ') !== -1){
			str = str.replace("Õ", ";Otilde;");
		}	
		
		while(str.indexOf('õ') !== -1){
			str = str.replace("õ", ";otilde;");
		}	
		
		while(str.indexOf('Ç') !== -1){
			str = str.replace("Ç", ";Ccedil;");
		}	
		
		while(str.indexOf('ç') !== -1){
			str = str.replace("ç", ";ccedil;");
		}	
		
		while(str.indexOf('À') !== -1){
			str = str.replace("À", ";Agrave;");
		}	
		
		while(str.indexOf('à') !== -1){
			str = str.replace("à", ";agrave;");
		}	
	
	}catch(e){
		str = '';	
	}
	
	return str;
}

function altera_status(id){

	$.post('index.php',{ 
							ST: id
						},
						function(data){
							//alert(data);
							if(data == 'S'){
								var img = '<img src="../../private/imagens/bt_ativo.png" style="cursor:pointer;" />';	
							}else{
								var img = '<img src="../../private/imagens/bt_inativo.png" style="cursor:pointer;" />';	
							}

							id = id.replace("&&", "");
							
							$('.status_'+id).html(img);
						}

	);
}

/*Roberto da Silva*/
function define_status(id,codStatus){

	$.post('index.php',{ 
							ST: id, cod: codST
						},
						function(data){
							alert(data);
							if(data == 'S'){
								var img = '<img src="../../private/imagens/bt_ativo.png" style="cursor:pointer;" />';	
							}else{
								var img = '<img src="../../private/imagens/bt_inativo.png" style="cursor:pointer;" />';	
							}

							id = id.replace("&&", "");
							
							$('.status_'+id).html(img);
						}

	);
}

function excluir() {
    if (confirm("Esta operação não poderá ser desfeita! \n Deseja continuar?")) {
        $("input[name=excluir]").each(function() {
            if (this.checked) {
				var cod = this.value;
                $.post("index.php",
                    {
                        ID: cod,
                        excluir: true
                    },
                    function(data) {
						//alert(data);	
						if(data.length > 3){
	                        alert(data);
						}else{
							$("#linha_"+cod).hide();
						}
                    }
                );

            }
        });
    }
}

function seleciona(campo, cod) {
    if (campo.checked) {
        $("#tr_" + cod + " td").css("border-bottom", "#FF6600 solid 2px");
        $("#tr_" + cod + " td").css("font-style", "italic");
    } else {
        $("#tr_" + cod + " td").css("border-bottom", "none");
        $("#tr_" + cod + " td").css("font-style", "normal");
    }
}

function janela(pag){
	var width  = 920;
	var height = 510;
	window.open(pag,"",'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',top='+((screen.height-height)/2)+',left='+((screen.width-width)/2));
}

function novaPesquisa(){
	$("#i").attr({value: "0"});
}

function imprimirMalaDireta(){
	
	$.get("index.php", {
		//$busca		: $('#busca').text(),
		$codAno		: $('#codAno').val(),
		$busca		: $('#busca').val(),
		$estado		: $('#estado').val(),
		$i			: $('#i').val(),
		$limit		: $('#limit').val(),
		$status		: $('#status').val(),
		$titulo		: $('#titulo').val(),
		$imprimir	: true
	},function(data){
		//alert("Data Loaded: " + data);
		form = $("#pesquisar");
		$("#imprimir").attr({value: "true"});
		$("#impressaoConfirmada").attr({value: "true"});
        form.submit();
	});
}

