﻿//
//  OnLoad Event
//
$(document).ready(function () {

    loggedIn = false;
    giftToPick = 0;


    $("#fideli").click(function () {
        if (!loggedIn) {
            $(".loginDropDown").slideToggle(500);
            $(this).toggleClass("menuNodeFideliOn");
            $("#TextLoginEmail").focus();
        }
        else {
            getProfile();
            $(".profileDropDown").slideToggle(500);
            // fillAreaInteresse();

            $(this).toggleClass("menuNodeFideliOn");
        }
        return false;
    });

    $(".menuNode").click(function () {
        $(".menuNodeActive").toggleClass("menuNodeActive")
        $(this).toggleClass("menuNodeActive");
        switchDiv($(this).attr("id"));
        return false;
    });

    $(".navItem").click(function () {
        switchNode($(this).attr("id"));
        return false;
    });

    $("#ButtonLogin").click(function () {
        $("#ButtonLogin").hide();
        $(".loadingLogin").show();
        login();

        // $(".loginWarning").fadeIn();
        return false;
    });
});

// END OF OnLoad Event

function showVoucherPanel() {
    $(".profileForm").hide();
    $(".profilePoints").hide();
    $(".profileRules").hide();
    $(".profileVouchers").fadeIn();
    VouchersGrid.Refresh();
}

function showPointsPanel() {
    $(".profileForm").hide();
    $(".profileVouchers").hide();
    $(".profileRules").hide();
    $(".profilePoints").fadeIn();
    PointsGrid.Refresh();
}


function showProfileForm() {
    $(".profilePoints").hide();
    $(".profileVouchers").hide();
    $(".profileRules").hide();
    $(".profileForm").fadeIn();
}

function showRulesPanel() {
    $(".profileForm").hide();
    $(".profilePoints").hide();
    $(".profileVouchers").hide();
    $(".profileRules").fadeIn();
}

function renderExtrato(codigo, situacao) {
    if (situacao == "False") {
        alert("Este voucher já foi dado baixa.");
        return;
    }
    window.open('renderVoucher.aspx?id=' + codigo, 'voucher', 'status=0,toolbar=0,width=580,height=550');
    return;
}


function submitContactForm() {

    if (TextBoxContactNome.GetText() == "") {
        $("#ASPxTextBoxContactNomee").css("border", "1px solid #FF0000");
        $(".contactValidation").text("Por favor, digite o seu nome.");
        TextBoxContactNome.SetFocus();
        $(".contactValidation").fadeIn();

        return false;
    }

    if (!(validateEmail(TextBoxContactEmail.GetText()))) {
        $("#ASPxTextBoxContactEmail").css("border", "1px solid #FF0000");
        $(".contactValidation").text("É necessário um Endereço de Email válido.");
        TextBoxContactEmail.SetFocus();
        $(".contactValidation").fadeIn();

        return false;
    }

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            alert("Mesagem Enviada Com Sucesso. Entraremos em contato em breve.");
            window.location = "http://zilda.fidelimais.com.br/";

        }


    }
    xmlhttp.open("GET", "ajax/sendContactForm.aspx?nome=" + TextBoxContactNome.GetText() + "&telefone=" + TextBoxContactTelefone.GetText() + "&email=" + TextBoxContactEmail.GetText() + "&horarios=" + TextBoxHorarios.GetText() + "&dias=" + TextBoxDias.GetText() + "&expectativas=" + MemoExpectativas.GetText() + "&comentarios=" + MemoComentario.GetText(), true);
    xmlhttp.send();
}
function switchDiv(divId) {
    $("#contentnode1").hide();
    $("#contentnode2").hide();
    $("#contentnode3").hide();
    $("#contentnode4").hide();
    $("#content" + divId).fadeIn();

}

function switchNode(divId) {
    $("#holderTab1").hide();
    $("#holderTab2").hide();
    $("#holderTab3").hide();
    $("#holderTab4").hide();
    $("#holderTab5").hide();
    $("#holderTab6").hide();
    $("#holder" + divId).fadeIn();

}

function clearWarnings() {
    $(".registerValidation").fadeOut();
    $("#ASPxTextBoxRegisterEmail").css("border", "1px solid #afc185");
    $("#ASPxTextBoxRegisterNome").css("border", "1px solid #afc185");
}


function registerCustomer() {

    clearWarnings();

    if (TextBoxRegisterNome.GetText() == "") {
        $("#ASPxTextBoxRegisterNome").css("border", "1px solid #FF0000");
        $(".registerValidation").text("Por favor, digite o seu nome.");
        TextBoxRegisterNome.SetFocus();
        $(".registerValidation").fadeIn();

        return false;
    }

    if (!(validateEmail(TextBoxRegisterEmail.GetText()))) {
        $("#ASPxTextBoxRegisterEmail").css("border", "1px solid #FF0000");
        $(".registerValidation").text("É necessário um Endereço de Email válido.");
        TextBoxRegisterEmail.SetFocus();
        $(".registerValidation").fadeIn();

        return false;
    }


    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            switch (xmlhttp.responseText) {
                case "0":
                    alert("Cadastro efetuado com sucesso. Cheque o seu email para escolher sua senha e ativar seu cadastro.");
                    window.location = "http://zilda.fidelimais.com.br/";
                    break;
                case "1":
                    $("#ASPxTextBoxRegisterEmail").css("border", "1px solid #FF0000");
                    $(".registerValidation").text("Este Endereço de Email já está cadastrado no sistema.");
                    $(".registerValidation").fadeIn();
                    break;
                case "2":
                    $("#ASPxComboBoxRegisterDiaAniversario").css("border", "1px solid #FF0000");
                    $("#ASPxComboBoxRegisterMesAniversario").css("border", "1px solid #FF0000");
                    $(".registerValidation").text("Dia e mês de aniversário são obrigatórios.");
                    $(".registerValidation").fadeIn();
                    break;
                case "3":
                    $("#ASPxTextBoxRegisterCpf").css("border", "1px solid #FF0000");
                    $(".registerValidation").text("CPF Inválido. Verifique se os números estão corretos.");
                    $(".registerValidation").fadeIn();
                    break;
                case "4":
                    $("#ASPxTextBoxRegisterCpf").css("border", "1px solid #FF0000");
                    $(".registerValidation").text("Este CPF já está cadastrado no sistema.");
                    $(".registerValidation").fadeIn();
                    break;
            }

        }


    }
        sexo = "";
        if (RadioButtonListRegisterSexo.GetSelectedIndex() == 0) {
            sexo = "M";
        }
        if (RadioButtonListRegisterSexo.GetSelectedIndex() == 1) {
            sexo = "F";
        }
        if (ComboBoxRegisterProfissao.GetValue() == null) {
            profissao = 1;
        } else {
            profissao = ComboBoxRegisterProfissao.GetValue();
        }
        dataString = TextBoxRegisterNome.GetText() + "|" + TextBoxRegisterTratamento.GetText() + "|" + profissao + "|" + ComboBoxRegisterNaturalidade.GetValue() + "|";
        dataString += sexo + "|" + TextBoxRegisterCpf.GetText() + "|" + ComboBoxRegisterDiaAniversario.GetValue() + "/" + ComboBoxRegisterMesAniversario.GetValue() + "/" + ComboBoxRegisterAnoAniversario.GetValue() + " 00:00:00 AM|" + TextBoxRegisterEndereco.GetText() + "|";
        dataString += TextBoxRegisterBairro.GetText() + "|" + TextBoxRegisterCep.GetText() + "|" + TextBoxRegisterCidade.GetText() + "|" + TextBoxRegisterEmail.GetText() + "|";
        dataString += TextBoxRegisterTelefone.GetText() + "|1234|1|interesses";
        xmlhttp.open("GET", "ajax/insertProfileData.aspx?data=" + dataString, true);
        xmlhttp.send();

}

function issueVoucher(voucherId) {


    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function () {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            switch (xmlhttp.responseText) {
                case "0":
                    alert("Voucher emitido com sucesso. Mais informações na caixa de entrada do seu email.");
                    getProfile();
                    $('.close-reveal-modal').click();
                    break;
                case "1":
                    alert("Você não tem pontos suficientes para emitir este voucher.");
                    break;
            }

        }


    }
    xmlhttp.open("GET", "ajax/issueVoucher.aspx?bonusItem=" + giftToPick, true);
    xmlhttp.send();

}



function updateProfile() {

    if (TextBoxSenha.GetText() != TextBoxConfirmeSenha.GetText()) {
        $("#ASPxTextBoxSenha").css("border", "1px solid #FF0000");
        $("#ASPxTextBoxConfirmeSenha").css("border", "1px solid #FF0000");
        $(".profileValidation").text("Senha não confere. Confirme a sua senha.");
        $(".profileValidation").fadeIn();
        //$("#ASPxTextBoxSenha_I").hide();

        return false;
    }
    if (CheckBoxAniversarioObrigatorio.GetChecked()) {
        if ((ComboBoxDiaAniversario.GetValue() == "00") || (ComboBoxMesAniversario.GetValue() == "00")) {
            $(".profileValidation").text("Data de Aniversário Obrigatório.");
            $(".profileValidation").fadeIn();
            if (ComboBoxDiaAniversario.GetValue() == "00") {
                $("#ASPxComboBoxDiaAniversario").css("border", "1px solid #FF0000");
            }
            if (ComboBoxMesAniversario.GetValue() == "00") {
                $("#ASPxComboBoxMesAniversario").css("border", "1px solid #FF0000");
            }
            return false;
        }
    }

    var anoAniversario = "";

    if (ComboBoxAnoAniversario.GetValue() == "00") {
        anoAniversario = "1889";
    } else {
        anoAniversario = ComboBoxAnoAniversario.GetValue();
    }
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            $("#clientName").text(TextBoxNome.GetText());
            $("#ASPxComboBoxMesAniversario").css("border", "1px solid #afc185");
            $("#ASPxComboBoxDiaAniversario").css("border", "1px solid #afc185");
            $("#ASPxTextBoxSenha").css("border", "1px solid #afc185");
            $("#ASPxTextBoxConfirmeSenha").css("border", "1px solid #afc185");
            $(".profileValidation").hide();
            alert("Dados alterados com sucesso.");
            $(".profileDropDown").slideToggle(500)
            $("#fideli").toggleClass("menuNodeFideliOn");

        }

    }

    profissao = ComboBoxProfissao.GetValue();
    loja = ComboBoxLoja.GetValue();
    sexo = RadioButtonListSexo.GetValue();

    if (ComboBoxProfissao.GetValue() == null) {
        profissao = 1;
    }
    if (ComboBoxLoja.GetValue() == null) {
        loja = 1;
    }

    if (RadioButtonListSexo.GetValue() == null) {
        sexo = "";
    }


    dataString = TextBoxNome.GetText() + "|" + TextBoxTratamento.GetText() + "|" + profissao + "|" + ComboBoxNaturalidade.GetValue() + "|";
    dataString += sexo + "|" + TextBoxCpf.GetText() + "|" + ComboBoxMesAniversario.GetValue() + "/" + ComboBoxDiaAniversario.GetValue() + "/" + anoAniversario + " 00:00:00 AM|" + TextBoxEndereco.GetText() + "|";
    dataString += TextBoxBairro.GetText() + "|" + TextBoxCep.GetText() + "|" + TextBoxCidade.GetText() + "|" + TextBoxEmail.GetText() + "|";
    dataString += TextBoxTelefone.GetText() + "|" + TextBoxSenha.GetText() + "|" + loja + "|interesses";

    xmlhttp.open("GET", "ajax/updateProfileData.aspx?data=" + dataString, true);
    xmlhttp.send();
}

function getProfile() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            response = xmlhttp.responseText.split("|");
            TextBoxNome.SetText(response[0]);
            TextBoxTratamento.SetText(response[1]);
            TextBoxCpf.SetText(response[5]);
            TextBoxEndereco.SetText(response[7]);
            TextBoxBairro.SetText(response[8]);
            TextBoxCep.SetText(response[9]);
            TextBoxCidade.SetText(response[10]);
            TextBoxEmail.SetText(response[11]);
            TextBoxTelefone.SetText(response[12]);
            TextBoxSenha.SetText(response[13]);

            if (response[4] == "F") {
                RadioButtonListSexo.SetSelectedIndex(1);
            } else {
                RadioButtonListSexo.SetSelectedIndex(0);
            }

            dtNascimento = response[6].split('/');

            dtDia = dtNascimento[0][0] + dtNascimento[0][1];
            dtMes = dtNascimento[1][0] + dtNascimento[1][1];
            dtAno = dtNascimento[2][0] + dtNascimento[2][1] + dtNascimento[2][2] + dtNascimento[2][3];

            ComboBoxDiaAniversario.SetSelectedItem(ComboBoxDiaAniversario.FindItemByValue(dtDia));
            ComboBoxMesAniversario.SetSelectedItem(ComboBoxMesAniversario.FindItemByValue(dtMes));
            ComboBoxAnoAniversario.SetSelectedItem(ComboBoxAnoAniversario.FindItemByValue(dtAno));

            ComboBoxProfissao.SetSelectedItem(ComboBoxProfissao.FindItemByValue(response[2]));
            ComboBoxNaturalidade.SetSelectedItem(ComboBoxNaturalidade.FindItemByValue(response[3]));
            ComboBoxLoja.SetSelectedItem(ComboBoxLoja.FindItemByValue(response[14]));
            $('#clientPontos').text(response[16] + " pontos");
        }

    }
    xmlhttp.open("GET", "ajax/getProfileData.aspx", true);
    xmlhttp.send();
}

function fillAreaInteresse() {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //var response = new Array();
            response = xmlhttp.responseText.split("|");
            alert(response.length);
            for (i = 0; i < response.length; i++) {
                infos = response[i].split(";");
                interesseIndex = CheckBoxListAreaInteresse.AddItem(infos[0], infos[1]);
                alert(CheckBoxListAreaInteresse.AddItem(infos[0], infos[1]));
                if (info[3] == "1") {
                    CheckBoxListAreaInteresse.selectItems(interesseIndex);
                }
            }
        }

    }
    xmlhttp.open("GET", "ajax/getAreaInteresse.aspx", true);
    xmlhttp.send();
}

function getGift(giftId) {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            $("#modalRegistration").hide();
            $("#modalVoucher").show();

            response = xmlhttp.responseText.split("|");
            document.getElementById("modalGiftName").innerHTML = response[0];
            document.getElementById("modalGiftDescription").innerHTML = response[1];
            document.getElementById("modalGiftPoints").innerHTML = response[2];
            document.getElementById("modalGiftPicture").src = response[3];
        }

    }
    giftToPick = giftId;
    xmlhttp.open("GET", "ajax/getGift.aspx?id=" + giftId, true);
    xmlhttp.send();
}

function checkCpf(cpf) {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            return xmlhttp.responseText;
        }

    }
    xmlhttp.open("GET", "ajax/checkCpf.aspx?cpf=" + cpf, true);
    xmlhttp.send();
}

function registrationForm() {
    TextBoxRegisterCpf.SetVisible(true);
    $("#modalRegistration").show();
    $("#modalVoucher").hide();
}

function getGiftItems() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            response = xmlhttp.responseText;
            document.getElementById("bonus").innerHTML = response;
        }

    }
    xmlhttp.open("GET", "ajax/getGiftItems.aspx", true);
    xmlhttp.send();
}

function login() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        responseCartao = "";
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            responseCartao = xmlhttp.responseText.split("|");

            if (responseCartao[0] == "False") {
                $(".loginWarning").fadeIn();
                $("#ButtonLogin").show();
                $(".loadingLogin").hide();
            } else {
                loggedIn = true;
                $(".home").fadeOut();
                $(".bonusPanel").fadeIn();
                $(".loginDropDown").slideToggle(500)
                $("#fideli").toggleClass("menuNodeFideliOn");
                $("#clientName").text(responseCartao[1]);
                $("#clientPontos").text(responseCartao[2] + " pontos");
                getGiftItems();
            }
        }

    }
    xmlhttp.open("GET", "ajax/performLogin.aspx?email=" + $("#TextLoginEmail").val() + "&pass=" + $("#TextLoginPassword").val(), true);
    xmlhttp.send();


}

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 validateEmail(email) {

    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);

}
