﻿$(function () {

    $('a.desce_menu').click(function () {
        $('div.layercidades').slideToggle('medium');
    });

    $('#btnCadastroEmailTopo').click(CadastroNewsLetter);

    // Cria a Modal
    $('a[rel=modal]').click(function (e) {
        e.preventDefault();
        var id = $(this).attr('href');
        evModal(id);
    });

    // Fecha modal
    $('.close').click(function (e) {
        e.preventDefault();
        if ($('#indique:visible').length == 1) {
            $('#indique').fadeOut(1000, function () {
                $('#mask').hide();
                $('#msg').hide();
            });
        }

        if ($('#CadastroEmail:visible').length == 1) {
            $('#CadastroEmail').fadeOut(1000, function () {
                $('#mask').hide();
            });
        }

        if ($('#recuperaSenha:visible').length == 1) {
            $('#recuperaSenha').fadeOut(1000, function () {
                $('#mask').hide();
                $('#msg').hide();
            });
        }
    });
});

function CadastroNewsLetter() {
    if (!isEmail('header_txtEmailTopo')) { return; }
    var params = ["email", $('#header_txtEmailTopo').val()];

    ajaxCall("oferta-do-dia.aspx", "CadastroNewsLetter", params, CallBack_CadastroNewsLetter, null, null);

};

function CallBack_CadastroNewsLetter(res) {
    if (res.d != null) {
        $('#header_txtEmailTopo').val(res.d);
    } else {
        $('#header_txtEmailTopo').val('Erro ao cadastrar o email.');
    }
};

function LerCookie(name) {
    var nameE = name + "=";
    var cookies = document.cookie.split(";");
    for (var i = 0, Cookie; Cookie = cookies[i]; i++) {
        while (Cookie.charAt(0) == " ") {
            Cookie = Cookie.substring(1, Cookie.length);
        }
        if (Cookie.indexOf(nameE) == 0) {
            return Cookie.substring(nameE.length, Cookie.length);
        }
    }
    return false;
};

function evModal(objModal) {
    // Indique um amigo
    if (objModal == '#indique') {
        if ($('#overlayCidades:visible').length == 0) {
            if (LerCookie('ClienteOfertas').indexOf('Email=') > -1) {
                $('#spanEmailUser').css('display', 'none');
            } else {
                $('#spanEmailUser').css('display', 'block');
            }

            $('#msg').hide();
        } else {
            setTimeout('evModal("#indique");', 30000);
        }
    }

    var maskHeight = $(document).height();
    var maskWidth = $(window).width();

    $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
    $('#mask').fadeIn(1000);
    $('#mask').fadeTo("slow", 0.8);

    var winH = $(window).height();
    var winW = $(window).width();

    $(objModal).css('top', winH / 2 - $(objModal).height() / 2);
    $(objModal).css('left', winW / 2 - $(objModal).width() / 2);

    $(objModal).fadeIn(2000);
};

function validaData() {

    var hoje = new Date();
    var anoAtual = hoje.getFullYear();
    var barras = document.getElementById('inputData').value.split("/");
    if (barras.length == 3) {
        dia = barras[0];
        mes = barras[1];
        ano = barras[2];
        resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) && (ano <= anoAtual && ano >= 1900));
        if (!resultado) {
            alert("Formato de data invalido!");
            document.getElementById('inputData').focus();
            return false;
        }
    } else {
        alert("Formato de data invalido!");
        document.getElementById('inputData').focus();
        return false;
    }
    return true;
}
