//verifica se o email informada eh valido
function validaEmail(email){
    ER = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]{2,64}(\.[a-z0-9-]{2,64})*\.[a-z]{2,4}$");
    if (ER.test(email)){
        return true;
    }
    else{
        return false;
    }
}

function validaData(data){
    if(!(/^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/.test(data))){
        return false;
    } else {
        return true;
    }
}

function validaCEP(cep){
    if(!(/^[0-9]{2}\.[0-9]{3}\-[0-9]{3}$/.test(cep))){
        return false;
    } else {
        return true;
    }
}


function validaCPF(cpf)
{
    cpf = cpf.replace('.','');
    cpf = cpf.replace('.','');
    cpf = cpf.replace('.','');
    cpf = cpf.replace('.','');
    cpf = cpf.replace('.','');
    cpf = cpf.replace('-','');

    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 isNum(campo){
    var valor = campo.value;
    var er = /[^0-9()-]+/;

    while(er.test(valor))
    {
        valor = valor.replace( er, "");
    }

    campo.value = valor;
}

function varificaMax(campo, max)
{
    if(campo.value > max)
    {
        campo.value = max;
    }
}




function isValor(valor)
{
    var er = /[^0-9,]/;
    var texto = valor;

    while(er.test(texto))
    {
        texto = texto.replace(er, '');
    }

    return texto;
}

var pathSite = "";

function validaFrmCadNewsletter(form)
{


    if(form.nome.value == "")
    {
        mostraMensagem("Informe seu nome");
        form.nome.focus();
        return false;
    }

    if(form.email.value == "")
    {
        mostraMensagem("Informe seu e-mail");
        form.email.focus();
        return false;
    }
    else
    {
        if(!validaEmail(form.email.value))
        {
            mostraMensagem("Informe um e-mail válido");
            form.email.focus();
            return false;
        }
    }
    
    mostraMensagem('Aguarde um instante...', 'infinity');
    
    $.ajax({
        url: pathSite + 'ctrl.php?acao=cadastra-news',
        dataType: 'json',
        type: 'POST',
        data: $('#frmCadContato').serialize(),


        success: function(obj){

            if(obj.situacao=="sucess"){
                //form.email.value = "";
                mostraMensagem(obj.msg,3);
            } else if(obj.situacao=="error"){
                mostraMensagem(obj.msg,2);
            }


        },

        error : function (XMLHttpRequest, textStatus, errorThrown) {
        },

        beforeSend : function(requisicao){
            clearTimeout($idTimeoutMostraMensagem);
            $idTimeoutMostraMensagem = setTimeout(function(){
                var obj = {
                    situacao : 'error',
                    msg : 'O servidor demorou a responder. Tente novamente mais tarde'
                }
                mostraMensagem(obj.msg,5);
                requisicao.abort();
            },60*1000);
        }

    });

    return false;

}


function mostrarCarregando(){
    
    $('#promocao_carregando').css('visibility','visible');
    
//piscar('promocao_carregando',.4);
    
    
}


function ocultarCarregando(){
    $('#promocao_carregando').css('visibility','hidden');
}










function base64_encode(decStr){
    var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var bits;
    var dual;
    var i = 0;
    var encOut = "";
    while(decStr.length >= i + 3){
        bits = (decStr.charCodeAt(i++) & 0xff) <<16 | (decStr.charCodeAt(i++) & 0xff) <<8 | decStr.charCodeAt(i++) & 0xff;
        encOut += base64s.charAt((bits & 0x00fc0000) >>18) + base64s.charAt((bits & 0x0003f000) >>12) + base64s.charAt((bits & 0x00000fc0) >> 6) + base64s.charAt((bits & 0x0000003f));
    }
    if(decStr.length -i > 0 && decStr.length -i < 3){
        dual = Boolean(decStr.length -i -1);
        bits = ((decStr.charCodeAt(i++) & 0xff) <<16) |    (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
        encOut += base64s.charAt((bits & 0x00fc0000) >>18) + base64s.charAt((bits & 0x0003f000) >>12) + (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') + '=';
    }
    return(encOut);
}

function base64_decode(encStr){
    var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var bits;
    var decOut = "";
    var i = 0;
    for(; i<encStr.length; i += 4){
        bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
        decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
    }
    if(encStr.charCodeAt(i -2) == 61){
        return(decOut.substring(0, decOut.length -2));
    }
    else if(encStr.charCodeAt(i -1) == 61){
        return(decOut.substring(0, decOut.length -1));
    }
    else {
        return(decOut);
    }
}


function urlencode( str ) {

    var histogram = {}, tmp_arr = [];
    var ret = (str+'').toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    //histogram['%20'] = '+';
    histogram['+'] = '%20';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}


function urldecode( str ) {

    var histogram = {};
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';

    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}


var $idTimeoutMostraMensagem;
function mostraMensagem(msg,tempo){
    $('#tela').css('height','28px');
    clearTimeout($idTimeoutMostraMensagem);
    $('#tela p.title').html(msg);
    $('#tela').show();
    if(tempo!='infinity'){
        $idTimeoutMostraMensagem = setTimeout(function(){
            $('#tela').css('height','100%');
            $('#tela').hide();
            $('#tela p.title').html('');
        }, (tempo>0?tempo:3)*1000);
    }
}



