var timerMenuOff = undefined, objMenuOff = undefined;
var setMenuOff = function(obj) {
    if (timerMenuOff) {
        clearTimeout(timerMenuOff);
    }
    objMenuOff = obj;
    timerMenuOff = setTimeout(callMenuOff, 200, "JavaScript");
}
var resetMenuOff = function() {
    if (timerMenuOff) {
        clearTimeout(timerMenuOff);
    }
    timerMenuOff = setTimeout(callMenuOff, 200, "JavaScript");
}
var clearMenuOff = function() {
    if (timerMenuOff) {
        clearTimeout(timerMenuOff);
        timerMenuOff = undefined;
    }
}
var callMenuOff = function() {
    if (timerMenuOff) {
        clearTimeout(timerMenuOff);
        timerMenuOff = undefined;
    }
    if (objMenuOff)
        funcMenuOff();
}
var funcMenuOff = function() {
    $(".menuSup",objMenuOff).add(objMenuOff).removeClass("menuSupOn");
    $("DIV.despMenu#"+$(objMenuOff).attr("menu")).hide().css("left", 0);
}
var masterLoad = function(IDSel) {
    var tdCont = $("TD#texto");
    tdCont.css("height",$(window).height()-$("TABLE#principal").height()+tdCont.height());
    if (IDSel) {
        $("TD.menuSup[menu='m"+IDSel+"']").add("TD.menuSup[menu='m"+IDSel+"'] .menuSup").addClass("menuSupOn");
    }
    $("TD.menuSup:not(.menuSupOn)").hover(
        function() {
            callMenuOff();
            $(".menuSup",this).add(this).addClass("menuSupOn");
        },
        function() {
            setMenuOff(this);
        }
    );
    $("TD.menuSup").css("cursor","pointer").click(
        function(event) {
            var l = $("A",this);
            var URL = l.attr("href"), Target = l.attr("target");
            event.preventDefault();
            if (URL && URL!="#")
                window.open(URL, Target);
            return (false);
        }
    );
    var menu = $("DIV.despMenu");
    $("*",menu).addClass("despMenu");
    $("UL LI:first-child",menu).addClass("despMenuPrimero");
    $("UL LI",menu).hover(
        function() {
            $(".despMenu",this).add(this).addClass("despMenuOn");
        },
        function() {
            $(".despMenu",this).add(this).removeClass("despMenuOn");
        }
    ).click(
        function(event) {
            var l = $("A",this);
            var URL = l.attr("href"), Target = l.attr("target");
            event.preventDefault();
            if (URL && URL!="#")
                window.open(URL, Target);
            return (false);
        }
    );
    $("TD.menuSup:not(.menuSupOn)").hover(
        function() {
            var ancho = $(this).width(), menu = $("DIV.despMenu#"+$(this).attr("menu")).show();
            if (menu.length==0) return;
            if (menu.width() > ancho) {
                var limite = $("TABLE.menuSup"), posX = $(this).offset().left - Math.floor((menu.width()-ancho)/2);
                limite = limite.offset().left + limite.width() - menu.width();
                if (posX>limite) posX = limite;
                menu.css("left", posX);
            } else {
                menu.css("left", $(this).offset().left).find("UL").width(ancho-28);
            }
        },
        function() {
            return;
        }
    );
    $(menu).hover(
        function() {
            clearMenuOff();
        },
        function() {
            resetMenuOff();
        }
    );
};

var sacaVentana = function(doc) {
	var v;

	if (screen.height == 600) {
		v = window.open(doc,'vent','width=790,height=520,left=0,top=0,resizable=yes,status=no,toolbar=yes,scrollbars=yes');
	} else if (screen.height == 768) {
		v = window.open(doc,'vent','width=1014,height=680,left=0,top=0,resizable=yes,status=no,toolbar=yes,scrollbars=yes');
	} else {
		v = window.open(doc,'vent','width=1142,height=780,left=0,top=0,resizable=yes,status=no,toolbar=yes,scrollbars=yes');
	}
	v.focus();
}
function vent(doc,id) {
    sacaVentana(doc);
} 
var Disclaimer = function(id) {
    var v;
    
    v = window.open('/aspx/Comun/Disclaimer.aspx?id='+id,'Disclaimer','width=450,height=365,left=350,top=200,resizable=yes,status=no,toolbar=no,scrollbars=no');
	v.focus();
}

var Disclaimer_Tipos = function(id) {
    var v;
    
    v = window.open('/aspx/Comun/Disclaimer_Tipos.aspx?id='+id,'Disclaimer','width=450,height=450,left=350,top=200,resizable=yes,status=no,toolbar=no,scrollbars=no');
	v.focus();
}
/*var Grafico = function(ticker,id) {
    var v;
    
    v = window.open('/aspx/Financiero/verGrafico.aspx?ticker='+ticker+'&id='+id,'GraficoMEFF','width=475,height=440,left=350,top=150,resizable=yes,status=no,toolbar=no,scrollbars=no');
	if (v)
		v.focus();
}*/

var NotaPrensa = function(id,tipo,fecha,nombre) {
    var v, l;
	
	if (nombre.indexOf('.') >= 0) {
		l = '/xml/prensa/'+tipo+'/'+fecha.substr(0,4)+'/'+fecha.substr(4,2)+'/'+nombre;
	} else {
		l = '/aspx/Prensa/NotaPrensa.aspx?id='+id+'&fecha='+fecha+'&tipo='+tipo+'&fich='+nombre;
	}
   	v = window.open(l,"NotaPrensa", "width=700,height=600,left=150,top=50,resizable=yes,status=no,toolbar=no,scrollbars=yes");
	v.focus();
}
function formatDate(ticks){
    var fecha;
    fecha=new Date(ticks);
    return fecha.getDate() + "/" + eval(fecha.getMonth()+1) + "/" + fecha.getFullYear();
}

/* Esta función requiere importar las librerías: jshashtable-2.1.js y jquery.numberformatter-1.2.1.min.js*/
function formatNumber(Numero, idioma){
   var numAux = $.formatNumber(Numero, {format:'0,000.00', locale:idioma});
   return numAux;

}
function getResults(pagina, capaResultados, mensajeError) {
    if (capaResultados == null) capaResultados = "capaResultados";
    $.ajax({
        url: pagina,
        async: true,
        beforeSend: function (objeto) {
            $("#" + capaResultados).html("<p><img src='/images/loading80x60.gif'></p>");
        },
        complete: function (objeto, exito) {
        },
        contentType: "application/x-www-form-urlencoded",
        dataType: "html",
        error: function (objeto, quepaso, otroobj) {
            if (mensajeError == null) mensajeError = quepaso;
            $("#" + capaResultados).html(mensajeError);
        },
        global: true,
        ifModified: false,
        processData: true,
        success: function (datos) {
            $("#" + capaResultados).html(datos);
        },
        timeout: 30000,
        type: "GET"
    });
}
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
function createCookie(name, value, minutes) {
    if (minutes) {
        var date = new Date();
        //date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        date.setTime(date.getTime() + (minutes * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return "";
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}
