////---------------------------------------------------------------------------------//
// COMMONS
//---------------------------------------------------------------------------------//
/*
/*
* Funções para grid antigo
*/

function HL(){
    $("input[type=text],input[type=password],select,textarea").focus(function () {
        $("input[type=text],input[type=password],select,textarea").removeClass("focus");
        $(this).addClass("focus");
    });

    $("input[type=text],input[type=password],select,textarea").blur(function () {
        $(this).removeClass("focus");
    });
};

function security(name) {
    $('iframe:not(.redeFacebook):not(.localizacao iframe):not(.ifVideo iframe)').remove();
    $('body').append('<iframe name="'+name+'" id="'+name+'" src="about:blank" style="top: 0px;display:none; background-color: #fff; height:200px; margin-left: 5%; width:90%; position: fixed; z-index: 50000;"></iframe>');
}


/*
 * Função para para por determinado tempo
 * @param milliseconds milisegundos
 */
function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
            break;
        }
    }
}

//-----------------------------------------------------------------------------//
// VALIDA E-MAIL
//-----------------------------------------------------------------------------//

function validaEmail(email)
{
    if(email == "")
    {
        alert('Você precisa digitar um endereço de e-mail! ');
        return false;
    }

    if(email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
    {
        alert('Você precisa digitar um endereço de e-mail valido! ');
        return false;
    }

    return true;
}

function formataMoeda(campo,evt) {
   
    //para evitar caracteres alfas.
    if(((evt.keyCode < 96) || (evt.keyCode > 105)) && ((evt.keyCode < 48) || (evt.keyCode > 57)) ){
        campo.value = campo.value.replace(String.fromCharCode(evt.keyCode).toLowerCase(),"");
    }
    str = campo.value;

    while(str.search(",") != -1)
        str = str.replace(",","");
   
    var i = 0;

    while(i< str.length){
        if(str.substr(i,1) == ".")
            str = str.replace(".","");
        i++;
    }

    part1 = str.substr(0,str.length - 2);
    while(part1.search(" ") != -1)
        part1 = part1.replace(" ","");

    part2 = str.substr(str.length - 2,2);
    res = "";
    i = part1.length;
    sob = i % 3;
    if((sob != 0) && (i > 2))
        res = part1.substr(0,sob) + ".";
    else
        res = part1.substr(0,sob);
    j = 1;
    part1 = part1.substr(sob);
    i = 0;
    while(i < part1.length){
        if(j == 3){
            if(i + 1 == part1.length)
                res = res + part1.substr(i-2,3);
            else res = res + part1.substr(i-2,3) + ".";
        }
        i++;
        j = j<3?j+1:1;
    }
    campo.value = res + "," + part2;
	   
    if (campo.value == ',')
        campo.value = '';

}


//Formata moeda
// ex: number_format(result,2, ',', '.');

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}


function valida_cpf(cpf)
{
    var numeros, digitos, soma, i, resultado, digitos_iguais;
    digitos_iguais = 1;
    if (cpf.length < 11)
        return false;
    for (i = 0; i < cpf.length - 1; i++)
        if (cpf.charAt(i) != cpf.charAt(i + 1))
        {
            digitos_iguais = 0;
            break;
        }
    if (!digitos_iguais)
    {
        numeros = cpf.substring(0,9);
        digitos = cpf.substring(9);
        soma = 0;
        for (i = 10; i > 1; i--)
            soma += numeros.charAt(10 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(0))
            return false;
        numeros = cpf.substring(0,10);
        soma = 0;
        for (i = 11; i > 1; i--)
            soma += numeros.charAt(11 - i) * i;
        resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
        if (resultado != digitos.charAt(1))
            return false;
        
        return true;
    }
    else
        return false;
}



function valida_cpf(cpf) 
{
    cpf = cpf.replace('.', '');  
    cpf = cpf.replace('.', '');  
    cpf = cpf.replace('-', '');              
            
    if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
        return 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)))
        return 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)))
        return false;
    
    return true;
}




function valida_cnpj(cnpj)
{
    cnpj = cnpj.replace('.', '');  
    cnpj = cnpj.replace('.', '');  
    cnpj = cnpj.replace('/', '');  
    cnpj = cnpj.replace('-', '');    
    
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais = 1;
    if (cnpj.length < 14 && cnpj.length < 15)
        return 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))
            return 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))
            return false;
        return true;
    }
    else
        return false;
} 
