/* Add here all your JS customizations */
// Commom Partials
(function($) {

    'use strict';

    $(window).afterResize(function() {
        $(".revslider-initialised").each(function() {
            try{
                $(this).revredraw();
            } catch(e) {}
        });
    });

    $(document).ready(function() {
        if($("#btn_sip").length>0) {
            $("#btn_sip").click(function (e) {
                e.preventDefault();
                if($(this).hasClass("disabled"))
                    return;
                $(this).addClass("disabled");
                $("#loading_sip").css("display","inline-block");
                window.location = $(this).attr("href");
            });
            $("#loading_sip").css("display","none");
        }

        if($("#btn_ip40").length>0) {
            $("#btn_ip40").click(function (e) {
                e.preventDefault();
                if($(this).hasClass("disabled"))
                    return;
                $(this).addClass("disabled");
                $("#loading_ip40").css("display","inline-block");
                window.location = $(this).attr("href");
            });
            $("#loading_ip40").css("display","none");
        }
    });

    // Sticky Header
    if (typeof theme.StickyHeader !== 'undefined') {
        theme.StickyHeader.initialize();
    }
    
    $('.dropdown-menu-toggle').on('click', function(e) {
        if ($(document).width() > 768) {
          e.preventDefault();
  
          var url = $(this).attr('href');
  
          if (url !== '#') {
              window.location.href = url;
          }
  
        }
  });

}).apply(this, [jQuery]);

function setLoaderFormBtn($_objBtn, img = '', $_idimg='') {
    if($_objBtn.hasClass("disabled"))
        return;
    $_objBtn.addClass("disabled");
    if(img!='')
        $_objBtn.prepend("<img src='"+img+"' width='16' id='loading_btn' style='margin-right: 10px; display: none;'>");

    if($_idimg=='') {
        $("#loading_btn").css("display","inline-block");
    } else {
        $_idimg.css("display","inline-block");
    }

}

function clearLoaderFormBtn($_objBtn, $_idimg='') {
    $_objBtn.removeClass("disabled");
    if($_idimg==''){
        $("#loading_btn").css("display","none");
    } else {
        $_idimg.css("display","none");
    }
}


/* CONTROLADOR DE COOKIES POR JS */
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    var expires = "";
    if(exdays==0) {
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        expires = "expires="+ d.toUTCString();
    }
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function checkCookie(cname) {
    if(getCookie(cname) != "")
        return true;
     else
        return false;
}

function deleteCookie(cname) {
    if(getCookie(cname) != "")
        setCookie(cname, "", 0)
    else
        return false;
}

/*------------------------------------*/
/* ENCUESTAS AGRESIVAS                */
/*------------------------------------*/
function openSurveyFunctions() {
    if($("#AggressiveSurveyModal").length>0)
        openSurveyAggressive();
}

function openSurveyFunctionsPopup($_obj='') {
    if($("#encuesta_agresiva_popup_show").length>0)
        openSurveyAggressivePopup();
    else if($_obj!='')
        $_obj.css("display","block");
}

function guardarDatos() {
    if ($('#rememberme').is(':checked')) {
        localStorage.username = $('#username').val();
        localStorage.password = $('#password').val();
        localStorage.checkBoxValidation = $('#rememberme').prop('checked');
    }else{
        localStorage.username = '';
        localStorage.password = '';
        localStorage.checkBoxValidation = false
    }
 }

/*$(document).ready(function() {
    if ((localStorage.username != undefined) && (localStorage.password != undefined)) {
        $('#username').val(localStorage.username);
        $('#password').val(localStorage.password);
        //$('#rememberme').val(localStorage.checkBoxValidation);
        $('#rememberme').prop('checked', localStorage.checkBoxValidation);
    }else {
        localStorage.username = '';
        localStorage.password = '';
        //$('#rememberme').prop('checked', false);
        localStorage.checkBoxValidation = '';
        $('#rememberme').prop('checked', false);
    }
});*/

$(".toggle-password").click(function() {

    $(this).toggleClass("fa-eye fa-eye-slash");
    var input = $($(this).attr("toggle"));
    if (input.attr("type") == "password") {
        input.attr("type", "text");
    } else {
        input.attr("type", "password");
    }
});

function equalHeightCard() {
    var height = 0;
    var $obj_list = $(".card_height");

    $obj_list.each(function() {
        if(height<$(this).outerHeight())
            height = $(this).outerHeight();
    });

    $obj_list.css("height", height + "px");
}

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}
