﻿
var htmBtn;
var ticks = 0;
var totalSegundos;
var interval;

function startClock() {
    return setInterval(Tick, 1000);
}

function Tick() {
    var segundos = totalSegundos--;

    if (segundos < 0)
        segundos = 0;

    var horas = Math.floor(segundos / 3600);
    segundos -= horas * 3600;
    var minutos = Math.floor(segundos / 60);

    segundos -= minutos * 60;

    document.getElementById('horas').innerHTML = horas >= 0 ? horas : 0;
    document.getElementById('minutos').innerHTML = minutos >= 0 ? minutos : 0;
    document.getElementById('segundos').innerHTML = segundos >= 0 ? segundos : 0;

    if (totalSegundos == 0) {
        window.location.href = window.location.href;
        clearInterval(interval);
    }
}


function OpenOverlayEmail() {
    if ($('#overlayCidades:visible').length == 0 && $('#indique:visible').length == 0) {
        $("#CadastroEmail").overlay().load();
    } else { setTimeout('OpenOverlayEmail();', 10000); }
};

function FecharOverlayCadastro() { $('#CadastroEmail').overlay().close(); };


function Indicar() {

    var enviar = false;

    if ($('#txtNomeUser').val() == '') {
        $('#txtNomeUser').focus();
        alert('Digite seu nome.');
        return;
    }

    var cookieEmail= '';
    if (LerCookie('ClienteOfertas').indexOf('Email=') == -1) {
        if (!isEmail('txtEmailUser')) {
            return;
        }
    } else {
        cookieEmail = LerCookie('ClienteOfertas');
        var ckEmail = cookieEmail.split('Email=');
        if (ckEmail.length == 2) {
            cookieEmail = ckEmail[1];
        }
    }

    if ($('#txtNome').val() != '' || $('#txtEmail').val() != '') {
        if ($('#txtEmail').val() == '' || $('#txtNome').val() == '') {
            alert('Informe o nome e o email da pessoa a indicar.');
            return;
        } else { enviar = true; }
    }

    if ($('#txtNome2').val() != '' || $('#txtEmail2').val() != '') {
        if ($('#txtEmail2').val() == '' || $('#txtNome2').val() == '') {
            alert('Informe o nome e o email da pessoa a indicar.');
            return;
        } else { enviar = true; }
    }

    if ($('#txtNome3').val() != '' || $('#txtEmail3').val() != '') {
        if ($('#txtEmail3').val() == '' || $('#txtNome3').val() == '') {
            alert('Informe o nome e o email da pessoa a indicar.');
            return;
        } else { enviar = true; }
    }

    if ($('#txtNome4').val() != '' || $('#txtEmail4').val() != '') {
        if ($('#txtEmail4').val() == '' || $('#txtNome4').val() == '') {
            alert('Informe o nome e o email da pessoa a indicar.');
            return;
        } else { enviar = true; }
    }

    if ($('#txtNome5').val() != '' || $('#txtEmail5').val() != '') {
        if ($('#txtEmail5').val() == '' || $('#txtNome5').val() == '') {
            alert('Informe o nome e o email da pessoa a indicar.');
            return;
        } else { enviar = true; }
    }

    if (enviar) {

        htmBtn = document.getElementById('btnEnviarIndique').innerHTML;
        document.getElementById('btnEnviarIndique').innerHTML = '<span class="AguardeRecupera">&nbsp;</span>';

        $('#msg').hide();

        var params = [
                    'codOferta', document.getElementById('hidCodOferta').value,
                    'nomeSender', $('#txtNomeUser').val(),
                    'emailSender', cookieEmail == '' ? $('#txtEmailUser').val() : cookieEmail,
                    'nomeIndicacao', $('#txtNome').val(),
                    'emailIndicacao', $('#txtEmail').val(),
                    'nomeIndicacao2', $('#txtNome2').val(),
                    'emailIndicacao2', $('#txtEmail2').val(),
                    'nomeIndicacao3', $('#txtNome3').val(),
                    'emailIndicacao3', $('#txtEmail3').val(),
                    'nomeIndicacao4', $('#txtNome4').val(),
                    'emailIndicacao4', $('#txtEmail4').val(),
                    'nomeIndicacao5', $('#txtNome5').val(),
                    'emailIndicacao5', $('#txtEmail5').val(),
                    'titulOferta', $('.tituloOfertaDoDia').html()
                    ];

        ajaxCall("../oferta-do-dia.aspx.htm", "IndicarAmigos", params, callBack_Indicar, null, null);

    } else { alert('Informe o nome e o email da pessoa a indicar.'); }

    return false;
};

function callBack_Indicar(res) {
    if (res.d) {
        $('#msg').show();
        ClearFieldsIndicar();
    }
    else {
        $('#msg').html('Ocorreu um erro no processo. Tente novamente mais tarde.');
        $('#msg').show();
    }

    document.getElementById('btnEnviarIndique').innerHTML = htmBtn;
};

function ClearFieldsIndicar() {
    $('#txtNomeUser').val('');
    $('#txtEmailUser').val('');
    $('#txtNome').val('');
    $('#txtEmail').val('');
    $('#txtNome2').val('');
    $('#txtEmail2').val('');
    $('#txtNome3').val('');
    $('#txtEmail3').val('');
    $('#txtNome4').val('');
    $('#txtEmail4').val('');
    $('#txtNome5').val('');
    $('#txtEmail5').val('');
};

function openIndiqueAmigo() {
    if ($('#overlayCidades:visible').length == 0) {

        if (LerCookie('ClienteOfertas').indexOf('Email=') > -1) {
            document.getElementById('spanEmailUser').style.display = 'none';
        } else {
            document.getElementById('spanEmailUser').style.display = 'block';
        }


        $('#msg').hide();
        $('#indique').overlay().load();
        _gaq.push(['_trackPageview', '/indique.vhtm']);
    } else { setTimeout('openIndiqueAmigo();', 10000); }
};

function closeIndiqueAmigo() {
    $('#indique').overlay().close();
};


$(function () {
    $('.OfertaDoDia').addClass('OfertaDoDiaAtivo');
    $('.OfertaDoDia .Seta').css('display', 'block');

    $('#LastTweet').twitterMentions('qpechincha', {
        maximum: 4
    });

    totalSegundos = document.getElementById('hidCronometro').value;

    if (document.getElementById('horas')) {
        interval = startClock();
    }

    $("#indique").overlay({
        top: 160,
        masc: {
            color: '#000',
            loadSpeed: 200,
            opacity: 0.8
        },
        closeOnClick: false
    });

    //
    $(document).ready(function () {
        $("#CadastroEmail").overlay({

            // custom top position
            top: 160,
            // some mask tweaks suitable for recuperaSenha-looking dialogs
            masc: {
                // you might also consider a "transparent" color for the mask
                color: '#000',
                // load mask a little faster
                loadSpeed: 800,
                // very transparent
                opacity: 0.8
            },
            closeOnClick: false
        });
    });
});

function CadastroOverlayOk() {

    if (!isEmail('txtNomeOverlayEmail')) { return; }


    document.getElementById('btnOverlayCadastroEmail').style.display = 'none';
    var dropCidades = document.getElementById('dropCidades');

    var params = ["email", document.getElementById('txtNomeOverlayEmail').value, "codCidade", dropCidades.options[dropCidades.selectedIndex].value]

    ajaxCall("../oferta-do-dia.aspx.htm", "InsertEmail",params,
                    callback_cadastroOverlay,
                     callback_cadastroOverlay,
                     callback_cadastroOverlay
             );
};

function callback_cadastroOverlay() { $('#CadastroEmail').overlay().close(); }


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;
};
  

