// <![CDATA[

    var v_cookie = leerCookie("style");
    var v_title = v_cookie ? v_cookie : obtenerEstandarCSS();
    activarCSS(v_title);        
    
    var v_body_loaded = false;
    var v_a_clicked = null;

    function p_body_onload() {
        v_body_loaded = true;
        if (window.body_onload != undefined) {        
            body_onload();
        }
    }
    
    function p_body_onunload() {            
        var title = obtenerCSS();
        guardarCookie("style", title, 365);
        if (window.body_onunload != undefined) {       
            body_onunload();
        }
    }
    
    function a_onclick(str_obj) {   
        if (!v_body_loaded) {        
            alert("Por favor, espere a que la página se haya cargado por completo.");
            return false;
        }
        if (str_obj == v_a_clicked)  {
            alert("Acción ya ejecutandose. Por favor, espere ...");
            setTimeout("a_onclick_timeout()", 2000);
            return false;
        }
        else if (v_a_clicked != null)  {        
            return false;
        }       
        else  {        
            v_a_clicked = str_obj;
            setTimeout("a_onclick_timeout()", 2000);
            return false;
        }
    }
    
    function a_onclick_timeout()  {    
        v_a_clicked = null;
    }
    
    function activarCSS(title) {
        var i, a, main;
        var encontrado = false;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i ++) {
            if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
                a.disabled = true;
                if (a.getAttribute("title") == title) {
                    a.disabled = false;
                    encontrado = true;
                    v_title = title;
                }   
            }
        }
        if (!encontrado) {
            var title2 = obtenerEstandarCSS();
            for (i = 0; (a = document.getElementsByTagName("link")[i]); i ++) {
                if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
                    a.disabled = true;
                    if (a.getAttribute("title") == title2) {
                        a.disabled = false;
                        encontrado = true;
                        v_title = title2;
                    }   
                }
            }
        }
        return false;
    }

    function obtenerCSS() {
        var i, a;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i ++) {
            if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
                return a.getAttribute("title");
            }
        }
        return null;
    }

    function obtenerEstandarCSS() {
        var i, a;
        for (i = 0; (a = document.getElementsByTagName("link")[i]); i ++) {
            if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) { 
                return a.getAttribute("title");
            }
        }
        return null;
    }

    function guardarCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else {
            expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function leerCookie(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 null;
    }

    var v_foto = null;
    var v_mapa = null;

    function abrirFoto(url) {

        if (v_foto != null) {
            v_foto.close();
            v_foto = null;
        }
        v_foto = window.open(url, "FOTO", 
            "width=0,height=0,top=0,left=0,scrollbars=no,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=no");
    }
    
    function abrirMapa(url) {

        if (v_mapa != null) {
            v_mapa.close();
            v_mapa = null;
        }
        v_mapa = window.open(url, "MAPA", 
            "width=800,height=600,top=0,left=0,scrollbars=yes,location=yes,directories=yes,status=yes,menubar=yes,toolbar=yes,resizable=yes");
    }

    function provincia() {

        var obj = document.forms["f_alta"].elements["<c:out value='${FRM_USU.PRO}'/>"];
        if (obj.readonly != true) {
            return detalle_ini("PROVINCIA", obj.name, obj.value, 320); 
        }
        return false;
    }

    function poblacion() {
        
        var obj = document.forms["f_alta"].elements["<c:out value='${FRM_USU.POB}'/>"];
        var obj_padre = document.forms["f_alta"].elements["<c:out value='${FRM_USU.PRO}'/>"];
        if (obj.readonly != true && obj_padre.value != "") {
            return detalle_ini("MUNICIPIO", obj.name, obj.value, 420, obj_padre.value); 
        }
        return false;
    }
    
    function detalle_ini(tabla, campo, valor, ancho, padre) {
        detalle_fin();
        var aux = padre ? padre : "";
        v_detalle = open(
            "ListaValores.do?tabla=" + tabla + "&campo=" + campo + "&valor=" + valor + "&padre=" + aux + "&ancho=" + ancho, 
            "detalle",
            "location=no,menubar=no,toolbar=no,scrollbars=yes,status=yes,width=" + ancho + ",height=380",
            true);
        return false;
    }

    function detalle_fin() {
        if (v_detalle != null) {
            v_detalle.close();
            v_detalle = null;
        }
    }

// ]]>
