﻿/*************************************************************************************************************
**********************************   Funciones Comunes   ****************************************************
*************************************************************************************************************/

//Devuelve el objeto del DOM con id pasado por parámetro
if (!$) var $ = function(id) {
    return (document.getElementById) ? document.getElementById(id) : (document.all) ? document.all[id] : null;
};

//Vacía los estilos
if (!clearElementStyle) var clearElementStyle = function(id) {
    $(id).setAttribute("style", "");
};

//Muestra un elemento estableciendo la propiedad css display a block
if (!showElement) var showElement = function(id) {
    $(id).style.display = "block";
};

//Oculta un elemento estableciendo la propiedad css display a none
if (!hideElement) var hideElement = function(id) {
    $(id).style.display = "none";
};

//Si el modo debug está activado lanza un alert con información de la excepción
if (!isDebug) var isDebug = true;
if (!manageException) var manageException = function(ex) {

    if (isDebug) {
        alert(ex.name + ":\n\t" + ex.message);
    }

};

//Cancela la propagación del evento recibido por parámetro
if (!cancelEvent) var cancelEvent = function(objEvento) {

    if (objEvento.stopPropagation) {
        objEvento.stopPropagation();
    }

    if (objEvento.preventDefault) {
        objEvento.preventDefault();
    }

    objEvento.cancelBubble = true;
    objEvento.cancel = true;
    objEvento.returnValue = false;

    return false;
};

//Función left trim para String
String.prototype.lTrim = function() {
    return this.replace(/^\s\s*/, "");
};

//Función right trim para String
String.prototype.rTrim = function() {
    return this.replace(/\s\s*$/, "");
};

//Función trim para String
String.prototype.trim = function() {
    return this.lTrim().rTrim();
};

//Hace un trim a un string y lo pone en minúsculas
String.prototype.trimAndLowerCase = function() {
    return this.trim().toLowerCase();
};

//Si se pulsa la tecla enter se cancela la propagación del evento
cancelarEnter = function(objEvento) {

    try {

        objEvento = objEvento ? objEvento : window.event;
        var tecla = (document.all) ? objEvento.keyCode : objEvento.which;
        if (tecla == 13) {
            cancelEvent(objEvento);
        }
        else {
            return true;
        }
    }
    catch (ex) {
        manageException(ex);
    }
};

//Agrega a favoritos
if (!AddToFavoritos) var AddToFavoritos = function(titulo, url) {

    //Mozilla Firefox
    if (window.sidebar) {
        window.sidebar.addPanel(titulo, url, "");
    }
    //Internet Explorer
    else if (window.external) {
        window.external.AddFavorite(url, titulo);
    }

};
