﻿function fnConfirmarExcluir()
{
    radconfirm("Deseja realmente excluir esse registro?",fnCallbackExcluir);
    return false;
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

function FormataCEP(e,obj)
{
//usar no evento keypress
//bloqueia caracteres alfa e coloca as barras nas posições

	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
    if(key != 8 && key != 0)
	{
	    goodChars = "0123456789";
	    if (goodChars.indexOf(keychar) != -1)	
	    {
		    if(obj.value.length == 5)
		        obj.value += "-";
    		    
		     vrRetorno = true;
	    }	    
	}
	else
	{
	     vrRetorno = true;
	}
	
	return vrRetorno;	
}

function FormataData(e,obj)
{
//usar no evento keypress
//bloqueia caracteres alfa e coloca as barras nas posições        
   	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
	//Backspace e Tab
	if(key != 8 && key != 0)
	{	 
	    goodChars = "0123456789";
	    if (goodChars.indexOf(keychar) != -1)	
	    {	    
		    if((obj.value.length == 2 || obj.value.length == 5) && key != 8)
		    {
		        obj.value += "/";		    
		    }
    		
		    vrRetorno = true;
	    }
	}
	else
	{
	    vrRetorno = true;
	}
	
	return vrRetorno;
}

function RequeridoData(sender, args) {
    var obj = document.getElementById(sender.controltovalidate);
    args.IsValid = isDate(trim(obj.value));
}

function RequeridoCPF(sender, args) {
    var txt = document.getElementById(sender.controltovalidate);
    args.IsValid = (validaCpfCnpj(txt) && (txt.value != ""));
}

function validaCpfCnpj(obj) {
    var i;
    var retorno = true;

    cpf = obj.value.replace(".", "");
    cpf = cpf.replace(".", "");
    cpf = cpf.replace("-", "");
    cpf = cpf.replace("/", "");

    if (cpf.length == 11) {
        if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
            retorno = false;

        add = 0;
        for (i = 0; i < 9; i++)
            add += parseInt(cpf.charAt(i)) * (10 - i);

        rev = 11 - (add % 11);

        if (rev == 10 || rev == 11)
            rev = 0;

        if (rev != parseInt(cpf.charAt(9)))
            retorno = false;

        add = 0;
        for (i = 0; i < 10; i++)
            add += parseInt(cpf.charAt(i)) * (11 - i);

        rev = 11 - (add % 11);

        if (rev == 10 || rev == 11)
            rev = 0;

        if (rev != parseInt(cpf.charAt(10)))
            retorno = false;

    }
    else {
        var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj;
        cnpj = cpf;
        digitos_iguais = 1;
        if (cnpj.length < 14 && cnpj.length < 15)
            retorno = 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))
                retorno = 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))
                retorno = false;
        }
        else
            retorno = false;
    }

    return retorno;
}

function desabilitaCtrlKeyCombinacao(e)
{
        //lista as combinações CTRL + key que queremos desabilitar:
        var forbiddenKeys = new Array('c','v');
        var key;
        var isCtrl;

        if(window.event)
        {
                key = window.event.keyCode;   //IE
                if(window.event.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }
        else
        {
                key = e.which;    //firefox
                if(e.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }

        // se ctrl é pressionado, verifica se outro key (tecla) é proibida
        if(isCtrl)
        {
                for(i=0; i<forbiddenKeys.length; i++)
                {
                        //case-insensitive comparation
                        if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
                        {
                                alert('Key combination CTRL + '
                                        +String.fromCharCode(key)
                                        + 'has been disabled.');
                                return false;
                        }
                }
        }
        return true;
}

function FormataCPF(e, obj) {
    //usar no evento keypress
    //bloqueia caracteres alfa e coloca as barras nas posições

    var key = window.event ? e.keyCode : e.which;
    var keychar = String.fromCharCode(key);
    var vrRetorno = false;

    if (key != 8 && key != 0) {
        goodChars = "0123456789";
        if (goodChars.indexOf(keychar) != -1) {

            if ((obj.value.length == 3 || obj.value.length == 7) && key != 8)
            { obj.value += "."; }
            if ((obj.value.length == 11) && key != 8)
            { obj.value += "-"; }

            vrRetorno = true;
        }
    }
    else {
        vrRetorno = true;
    }
    return vrRetorno;
}

function SomenteNumeros(e)
{
    //usar no evento onkeypress
    //se o caracter digitado não for pertencente ao goodChars, retorna falso (ascii = 0)

   	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);	
	var vrRetorno = false; 
	
	if(key != 8 && key != 0)
	{
	    goodChars = "0123456789";
	    if (goodChars.indexOf(keychar) != -1)	
	    {	    
		    vrRetorno = true;
	    }
	}
	else
	{
	    vrRetorno = true;   
	}
	return vrRetorno;
}

// ******************************************************************
// Limita o numero de caracteres de uma caixa de texto com multilinhas
// e apresenta a quantidade de caracteres digitados em uma label
//
// exemplo de uso:
// txtCaixa.Attributes.Add("onkeyup", "Count(this, 150, 'lblCountCaixa');");
//
function Count(text, long, msg) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
    }
    if (text.value.length == 0)
        document.getElementById(msg).innerHTML = '';
    else if (text.value.length == 1)
        document.getElementById(msg).innerHTML = '1 caracter';
    else
        document.getElementById(msg).innerHTML = text.value.length + ' caracteres';
    return true;
}

// ******************************************************************
// Retira acentos de uma string
//
function retira_acentos(palavra) {
    var com_acento = 'áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ.*[](){}|¹²³';
    var sem_acento = 'aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC------------';
    var nova = '';

    palavra = palavra.replace(/\*/g, '-');
    palavra = palavra.replace(/\[/g, '-');
    palavra = palavra.replace(/\]/g, '-');
    palavra = palavra.replace(/\(/g, '-');
    palavra = palavra.replace(/\)/g, '-');
    palavra = palavra.replace(/\{/g, '-');
    palavra = palavra.replace(/\}/g, '-');
    palavra = palavra.replace(/\|/g, '-');
    palavra = palavra.replace(/\¹/g, '-');
    palavra = palavra.replace(/\²/g, '-');
    palavra = palavra.replace(/\³/g, '-');

    for (i = 0; i < palavra.length; i++) {
        if (com_acento.search(palavra.substr(i, 1)) >= 0) {
            nova += sem_acento.substr(com_acento.search(palavra.substr(i, 1)), 1);
        }
        else {
            nova += palavra.substr(i, 1);
        }
    }
    return nova;
}

var min = 10;
var max = 17;
function increaseFontSize() {

    var p = document.getElementsByTagName("*");
    for (i = 0; i < p.length; i++) {

        var useClass = p[i].getAttribute('class');
        var useClassName = p[i].getAttribute('className');

        if (useClass != null || useClassName != null) {

            var isSizer = -1;

            if (useClass != null) isSizer = useClass.indexOf("sizer");
            if (useClassName != null) isSizer = useClassName.indexOf("sizer");

            if (isSizer >= 0) {

                if (p[i].style.fontSize) {
                    var s = parseInt(p[i].style.fontSize.replace("px", ""));
                } else {
                    var s = 15;
                }
                if (s != max) {
                    s += 1;
                }
                p[i].style.fontSize = s + "px"

            }

        }
    }

}
function decreaseFontSize() {
    var p = document.getElementsByTagName("*");
    for (i = 0; i < p.length; i++) {
        
        var useClass = p[i].getAttribute('class');
        var useClassName = p[i].getAttribute('className');

        if (useClass != null || useClassName != null) {
            
            var isSizer = -1;

            if (useClass != null) isSizer = useClass.indexOf("sizer");
            if (useClassName != null) isSizer = useClassName.indexOf("sizer");

            if (isSizer >= 0) {

                if (p[i].style.fontSize) {
                    var s = parseInt(p[i].style.fontSize.replace("px", ""));
                } else {
                    var s = 15;
                }
                if (s != min) {
                    s -= 1;
                }
                p[i].style.fontSize = s + "px";
            }
        }
    }
}



