﻿/*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 ValorPeso(v) {
    v = v.replace(/\D/g, "") //Remove tudo o que não é dígito
    v = v.replace(/(\d+)(\d{1})/, "$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) {

    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;");
    }


    return str;
}

function altera_status(id) {

    $.post('default.aspx', {
        ST: id
    },
						function(data) {
						    //alert(data);
						    if (data == 'S') {
						        var img = '<img src="../../img/bt_ativo.png" style="cursor:pointer;border:none" />';
						    } else {
						        var img = '<img src="../../img/bt_inativo.png" style="cursor:pointer;border:none" />';
						    }
						    $('.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) {

                $.post("edit.aspx",
                    {
                        cod: this.value,
                        excluir: true
                    },
                    function(data) {
                        alert(data);
                    }
                );

            }
        });

        document.location.reload();
    }
}

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 update_db() {

    if (confirm("Tem certeza que deseja atualizar os dados? \n Essa operação não poderá ser desfeita!")) {
        $("#mensagem").html("Processando atualização, aguarde, isso pode levar alguns minutos!");
        $.post("edit.aspx",
            {
                atualizar_database: true
            },
            function(data) {
                $("#mensagem").html("Processo concluído!");
                alert(data);
                location.href = ('default.aspx');
                
            }
    );

    }
}

function indisponivel(pag) {
    alert("Operação indisponível para esse módulo!");
    location.href=(pag);
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}

function erro(msgErro, path){
	if(path == ''){
		path = '../';
	}
	
	var html = '';
	html = '<p style="background-color:#F8D6C7;border:groove 4px #bb0000;padding:4px;text-align:center;color:#006600;margin:0 auto;margin-top:5px;width:70%;">';
	html += '<img src="'+path+'error.png" style="margin-right:5px; vertical-align:middle" />';
	html += msgErro;
	html += '</p>';
	return html;
}


function sucess(msg, path){
	if(path == ''){
		path = '../';
	}
	
	var html = '';
	html = '<p style="background-color:#bbFFbb;border:groove 4px #00bb00;padding:4px;text-align:center;color:#006600;margin:0 auto;margin-top:5px;width:70%;">';
	html += '<img src="'+path+'correct.png" style="margin-right:5px; vertical-align:middle" />';
	html += msg;
	html += '</p>';
	return html;
}

function message(msg, path){
	if(path == '' || path == 'undefined'){
		path = '../';
	}
	
	var html = '';
	html = '<p style="background-color:#FFFF88;border:groove 4px #FFFF88;padding:4px;text-align:center;color:#000;margin:0 auto;margin-top:5px;width:70%;">';
	html += '<img src="'+path+'atencion.png" style="margin-right:5px; vertical-align:middle" />';
	html += msg;
	html += '</p>';
	return html;
}

function slide(id){
	if($('.texto_'+id).is(':hidden')){
		$('.texto_'+id).slideDown();
	}else{
		$('.texto_'+id).slideUp();
	}
}

function verificaCaracteres(id){
	var carac = (Number(500) - Number($('#'+id+'').val().length));
	if(Number($('#'+id+'').val().length) >= 500){
		$('#'+id+'').val($('#'+id+'').val().substring(0,500));
	}
	$('#caracRestantes').html(carac);
}

function slideRevista(id){
	if($('#'+id+'').is(':hidden')){
		$('#'+id+'').slideDown();
		$('#'+id+'').prev('span').addClass('active');
		
	}else{
		$('#'+id+'').slideUp();
		$('#'+id+'').prev('span').removeClass('active');
	}
}


