URLBASE = document.getElementsByTagName ('base')[0].href;

/** Retorna un elemento por su ID
*/

function e( id ){
    return document.getElementById (id);
}

/** Crea un elemento
*/

function c( el ) {
    return document.createElement ( el );
}

/** Muestra el mensaje msg, y de aceptar, va a la url "url" :)
 */
function confirmar (msg, url) {
    if ( window.confirm (msg) ) {
        window.location.href = URLBASE + url;
    }
}

function cerrar_sesion () {
    if ( window.confirm ('Esto cerrará tu sesión en este sitio web')  )
        ir ( 'sesion/salir' );
}


/** Cambia la dirección a la direccion url
 */
function ir (url, openwindow, opts) {

    // Si la URL no tiene http:// , entonces le añadimos la ruta base.
    
    if ( url.substr (0, 7) != 'http://' )
        url = URLBASE + url;

    if ( openwindow ) 
        return window.open (url, openwindow, opts);
    else
        window.location.href = url;
}


/** Publica un mensaje, y da el foco al elemento
*/
function mensaje (msg, objeto, no_restaltar ) {
    if ( typeof (objeto) == 'string')  {
        o = e (objeto);
    }
    else {
        o = objeto;
    }
    
    o.focus();
    
    color_back = o.style.background;
    color_fore = o.style.color;
    
    o.style.color = 'white';
    o.style.background = 'red';

    window.alert ( msg );
    
    o.style.color = color_fore;
    o.style.background = color_back;
    o.focus()
}

/** Esta funcion muestra un popup bonito.
 */
function mostrar_busqueda (script, id, valor, busqueda) {

    id = window.open (script + "/" + "/id/" + id + "/valor/" + valor + "/busqueda/" + busqueda, "listado", "resizable=no,scrollbars=yes,width=320,height=480,toolbar=no,location=no,status=no");
    
   if ( !id  ) {
        window.alert ("El navegador no permite ventanas 'popup'. No puedo mostrar el listado mientras no permitas que este sitio muestre ventanas 'popup'.");
    }
}


function checkbox_muestra_oculta ( checkbox, control ) {

    if ( e(checkbox).checked )
        e(control).style.display='block';
    else
        e(control).style.display='none';        

}

// Prototipo de una funcion trim
String.prototype.trim = function(){
    return this.replace(/^\s+|\s+$/g,'')
}


