﻿var timeoutNoticia;
var timeoutSenha;
var idNoticia = 2;

$(document).ready(function () {
    $(document).keydown(function (event) {
        if (event.keyCode == 27) fecharPopUp();
    });

    $(":input").focus(function () {
        if ($(this).attr("type") == "text" || $(this).attr("type") == "textarea" || $(this).attr("type") == "select-one") {
            if ($.trim($(this).val()) == "" || $.trim($(this).val()) == $(this).attr("default")) {
                $(this).val("");
                $(this).css({ 'background': '#FFF' });
            }
            else
                $(this).css({ 'background': '#FFFFBF' });
        }

    });

    $(":input").blur(function () {
        if ($(this).attr("type") == "text" || $(this).attr("type") == "textarea" || $(this).attr("type") == "select-one") {
            if ($.trim($(this).val()) == "" || $.trim($(this).val()) == $(this).attr("default")) {
                $(this).val($(this).attr("default"));
                $(this).css({ 'background': '#FFF' });
            }
            else
                $(this).css({ 'background': '#FFFFBF' });
        }
    });

    timeoutNoticia = setTimeout("atualizaNoticia(" + idNoticia + ");", "10000");
});

function limparFormulario() {
    if (confirm('Tem certeza que deseja limpar todos os campos do formulário?')) {
        $(":input").each(function () {
            if ($(this).attr("email") == "true") {
                $(this).css({ 'background': '#FFF' });
                $(this).val($(this).attr("default"));
            }
        });
    }
}

function OnEnter(evt) {
    var key_code = evt.keyCode ? evt.keyCode :evt.charCode ? evt.charCode :evt.which ? evt.which : void 0;
    if (key_code == 13) {
        return true;
    }
}

function sair() {
    $.post("http://www.autopet.com.br/Controllers/Processa.aspx", { action: 'SAIR' }, function (retorno) {
        $(".bgTopoLogin_2").hide();
        $(".bgTopoLogin_1").show();
        window.open('http://www.autopet.com.br/', '_self');
    });
}


function donwloadSoftware(link) {    
    window.open(link, '_self');    
}



function verificaAtualizacao(download, mensagem, link) {
    $.post("http://www.autopet.com.br/Controllers/Processa.aspx", { action: 'VERIFICAATUALIZACAO', download: download, mensagem: mensagem }, function (retorno) {
        if (retorno == 'ok')
            window.open(link, '_self');
        else if (retorno == 'semautorizacao')
            alert('Download não autorizado!');
        else
            alert('Não foi possível efetuar o download, tente novamente!');
    });
}

function login() {
    var login = $("#txtLogin").val();
    var senha = $("#txtSenha").val();

    $.post("http://www.autopet.com.br/Controllers/Processa.aspx", { action: 'LOGIN', login: login, senha: senha }, function (retorno) {
        if (retorno == "erro") {
            exibirSenhaIncorreta();
        }
        else {
            $(".bgTopoLogin_2").html(retorno);
            $(".bgTopoLogin_1").hide();
            $(".bgTopoLogin_2").show();
            window.open('http://www.autopet.com.br/DadosUsuario', '_self');
        }
    });
}


function exibirSenhaIncorreta() {
    clearTimeout(timeoutSenha);
    $(".dadosIncorretos").fadeIn();
    timeoutSenha = setTimeout("$('.dadosIncorretos').fadeOut();", "4000");
}

function atualizaNoticia(id) {
    
    clearTimeout(timeoutNoticia);
    $(".noticia").hide();
    $("#noticia_" + id).show();

    $(".idNoticia_sel").attr("class", "idNoticia");
    $("#idNoticia_" + id).attr("class", "idNoticia_sel");

    idNoticia = parseInt(id) + 1;

    if (idNoticia > ($(".idNoticia").length + 1))
        idNoticia = 1;

    timeoutNoticia = setTimeout("atualizaNoticia(" + idNoticia + ");", "10000");
}

function rawurlencode(str) {
    var hex_chars = "0123456789ABCDEF";
    var noEncode = /^([a-zA-Z0-9\_\-\.])$/;
    var n, strCode, hex1, hex2, strEncode = "";

    for (n = 0; n < str.length; n++) {
        if (noEncode.test(str.charAt(n))) {
            strEncode += str.charAt(n);
        } else {
            strCode = str.charCodeAt(n);
            hex1 = hex_chars.charAt(Math.floor(strCode / 16));
            hex2 = hex_chars.charAt(strCode % 16);
            strEncode += "%" + (hex1 + hex2);
        }
    }
    return strEncode;
}


function enviarDownload(email) {
    if (enviarEmail(email)) {
        $(".passo_1").hide();
        $(".passo_2").show();
        $(".miolo").css({ height: '680px'})
    }
}

function enviarEmail(email) {

    v_conteudo = "";
    sair = false;

    v_campos = "";
    v_download = 0;
    $(":input").each(function () {
        if ($(this).attr("email") == "true") {
            if ($(this).val() == $(this).attr("default") && $(this).attr("obrigatorio") == "true") {
                $(this).css({ 'background': '#FFA6A6' });
                if (v_campos == "")
                    v_campos = $(this).attr("default");
                else
                    v_campos += "\n" + $(this).attr("default");
                sair = true;
            }
            else {
                if ($(this).attr("type") == "checkbox") {

                    if ($(this).attr("default") == "Deseja receber informativos do Autopet?")
                        v_download = 1;

                    if ($(this).attr("checked") == true)
                        v_conteudo = v_conteudo + $(this).attr("label") + ": Sim";
                    else
                        v_conteudo = v_conteudo + $(this).attr("label") + ": Não";
                }
                else {
                    if ($(this).val() == $(this).attr("default"))
                        v_conteudo = v_conteudo + $(this).attr("label") + ": <br>";
                    else
                        v_conteudo = v_conteudo + $(this).attr("label") + ": " + $(this).val() + "<br>";
                }
            }
        }
    });

    v_conteudo = $("#cabecalho_email").val() + "<br /><br />" + v_conteudo;
    v_conteudo = v_conteudo + "<br /><br />" + $("#rodape_email").val();

    
    v_conteudo = rawurlencode(v_conteudo);

    if (!sair) {

        v_informativo = "N";
        if ($("#informativo").attr("checked"))
            v_informativo = "S";


        $.post("http://www.autopet.com.br/Controllers/Processa.aspx", { action: 'INSERIRDOWNLOAD', conteudo: v_conteudo, assunto: $("#assunto_email").val(), destino: email, nome: $("#nome_completo").val(), empresa: $("#empresa").val(), cep: $("#cep").val(), ddd: $("#ddd").val(), email: $("#email").val(), telefone: $("#telefone").val(), newsletter: v_informativo, conhecimento: $("#conhecimento").val() }, function (retorno) { });


        $("#btnSubmit").html("Enviando...");
        $.post("http://www.autopet.com.br/Controllers/Processa.aspx", { action: 'ENVIAREMAIL', conteudo: v_conteudo, assunto: $("#assunto_email").val(), destino: email }, function (retorno) {
            if (v_download == 0)
                alert(retorno);

            $("#btnSubmit").html($("#btnSubmit").attr("default"));
        });
        return true;
    }
    else {
        alert("Preencha os seguintes campos para continuar:\n" + v_campos);
        return false;
    }
}


function carregarPopUp(tipo, conteudo, width, height) {
    height_2 = parseInt(height) + 36;


    $(".popup").css({ "position": "fixed", "left": "50%", "top": "50%", "marginLeft": "-374px", "marginTop": "-260px", "zIndex": "13", "background": "#FFF", "border": "solid 2px #333", "display": "none", "height": height_2 + "px", "width": width + "px" });
    width_2 = parseInt(width) - 8;

    width = width - 20;
    height = height - 20;

    if (tipo == "image")
        $(".popup").html("<div style='float: right; margin-bottom: 8px; margin-top: 8px; width: " + width_2 + "px; text-align: right; padding-right: 8px; '>Pressione ESC para <a href='#' style='color: #000;' onclick='fecharPopUp();'>Sair</a><img src='http://www.autopet.com.br/Images/" + conteudo + "' style='float: left; margin-top: 8px;'     /></div>");
    else
        $(".popup").html("<div style='float: right; margin-bottom: 8px; margin-top: 8px; width: " + width_2 + "px; text-align: right; padding-right: 8px; '>Pressione ESC para <a href='#' style='color: #000;' onclick='fecharPopUp();'>Sair</a><div style='float: left; padding: 10px; text-align: left; width: " + width + "px; height: " + height + "px; overflow: auto; margin-top: 8px;'>" + conteudo + "</div ></div>");

    $(".fundo").show();
    $(".popup").fadeIn();
    
}

function fecharPopUp() {
    $(".popup").fadeOut(function () {
        $(".fundo").hide();
    });
}

