// Arboles de contenidos
var idRaiz;
var idCapa;
var idNodoPadre;
var imgLoading;
var realSrcImg;
function seleccionNodo(id,idPadre,url,nombreId) {
    idNodoPadre=idPadre;
    idCapa=id;
    var nodoPadre=findObj(nombreId+'_'+id+'_'+idNodoPadre);
    if(nodoPadre.getAttribute('tienehijos')=='true') {
        if(document.getElementById('ulHijoDe_'+id+'_'+idPadre).style.display=='none'){
            document.getElementById('ulHijoDe_'+id+'_'+idPadre).style.display='block';
            document.getElementById('ulHijoDe_'+id+'_'+idPadre).style.visibility='visible';
            var imgPadre=findObj('idImg_'+id+'_'+idNodoPadre);
            imgPadre.src = getImagenAbierta(imgPadre.src);
        }else{
            document.getElementById('ulHijoDe_'+id+'_'+idPadre).style.display='none';
            document.getElementById('ulHijoDe_'+id+'_'+idPadre).style.visibility='hidden';           
            var imgPadre=findObj('idImg_'+id+'_'+idNodoPadre);
            imgPadre.src = getImagenCerrada(imgPadre.src);
        }
    }else{
        imgLoading=document.getElementById('idImg_'+id+'_'+idPadre);
        realSrcImg=imgLoading.src;
        imgLoading.src=CONTEXTO+'/imagenes/ajax-loader.gif';
        if(window.XMLHttpRequest){xhttp=new XMLHttpRequest();}
        else if(window.ActiveXObject){xhttp=new ActiveXObject('Microsoft.XMLHTTP');}
        var params=new Array();
        params[0]='idPadre='+idPadre;
        params[1]='nombreId='+nombreId;
        if(findObj(nombreId)){
            var idNodoAModificar=findObj(nombreId).value;
            params[2]='idNodo='+idNodoAModificar;
        }
        var params=tratarParametros(params);
        var sendf=params.join('&');
        xhttp.open('POST',url,true);
        xhttp.onreadystatechange=cargarNodosHijo;
        xhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=ISO-8859-15');
        xhttp.setRequestHeader('Connection','close');
        xhttp.send(sendf);
    }
}
function cargarNodosHijo() {
    if(xhttp.readyState==4){
        if(xhttp.status!=200){
            sesionCaducada();
        } else {
            var respuesta=xhttp.responseXML; // responseText
            var doc=window.document;  
            var nombreId=(respuesta.getElementsByTagName('nombreId')[0]).childNodes[0].nodeValue;
            var nodoPadre=findObj(nombreId+'_'+idCapa+'_'+idNodoPadre);
            var elementos=(respuesta.getElementsByTagName('elementos')[0]).childNodes[0].nodeValue;
            if(elementos>0){
                var lista=doc.createElement('ul');
                lista.id='ulHijoDe_'+idCapa+'_'+idNodoPadre;
                lista.style.display='block';
                lista.style.visibility='visible';
                for(i=0;i<elementos;i++){
                    var elemento=doc.createElement('li');
                    var enlaceIcono=doc.createElement('a');
                    var icono=doc.createElement('img');
                    var value=(respuesta.getElementsByTagName('value')[i]).childNodes[0].nodeValue;
                    var texto=(respuesta.getElementsByTagName('texto')[i]).childNodes[0].nodeValue;
                    var titulo=(respuesta.getElementsByTagName('titulo')[i]).childNodes[0].nodeValue;
                    var ruta;
                    if(respuesta.getElementsByTagName('ruta')[i]){
                        ruta=(respuesta.getElementsByTagName('ruta')[i]).childNodes[0].nodeValue;
                    }
                    var rutaimg=(respuesta.getElementsByTagName('rutaimagen')[i]).childNodes[0].nodeValue;
                    var url=(respuesta.getElementsByTagName('url')[i]).childNodes[0].nodeValue;
                    var noseleccionable=false;
                    if(respuesta.getElementsByTagName('seleccionable')[i]){
                        noseleccionable=(respuesta.getElementsByTagName('seleccionable')[i]).childNodes[0].nodeValue=="true";
                    }
                    var tieneHijos;
                    if(respuesta.getElementsByTagName('tienehijos')[i]){
                        tieneHijos = (respuesta.getElementsByTagName('tienehijos')[i]).childNodes[0].nodeValue;
                    }
                    icono.id='idImg_'+idCapa+'_'+value;
                    icono.border=0;
                    if( tieneHijos == 'true' ){
                        icono.src=rutaimg;  
                        enlaceIcono.href="javascript:seleccionNodo('"+idCapa+"','"+value+"','"+url+"','"+nombreId+"')";
                        enlaceIcono.appendChild(icono);
                        elemento.appendChild(enlaceIcono);
                    } else{
                        icono.src=getImagenNoHijos(rutaimg);
                        elemento.appendChild(icono);
                    }
                    elemento.appendChild(doc.createTextNode(' '));
                    if(!noseleccionable){
                        var enlaceTitulo=doc.createElement('a');
                        if(ruta){
                            enlaceTitulo.href="javascript:seleccionPadre"+idCapa+"('"+idCapa+"','"+value+"','"+ruta+"')";
                        }else{
                            enlaceTitulo.href="javascript:seleccionPadre"+idCapa+"('"+idCapa+"','"+value+"')";
                        }
                        enlaceTitulo.appendChild(doc.createTextNode(texto));
                        elemento.appendChild(enlaceTitulo);
                    }else{
                        var enlaceTitulo=doc.createElement('a');
                        enlaceTitulo.href="javascript:seleccionNodo('"+idCapa+"','"+value+"','"+url+"','"+nombreId+"')";
                        enlaceTitulo.appendChild(doc.createTextNode(texto));
                        elemento.appendChild(enlaceTitulo);
                    }
                    elemento.id=nombreId+'_'+idCapa+'_'+value;
                    elemento.setAttribute('titulo',titulo);
                    elemento.setAttribute('tienehijos','false');
                    if(respuesta.getElementsByTagName('vigencia')[i]) {
                        var vigencia=(respuesta.getElementsByTagName('vigencia')[i]).childNodes[0].nodeValue;
                        elemento.setAttribute('vigencia',vigencia);
                    }
                    if(respuesta.getElementsByTagName('ruta')[i]){
                        var ruta = (respuesta.getElementsByTagName('ruta')[i]).childNodes[0].nodeValue;
                        elemento.setAttribute('ruta',ruta);
                    }
                    lista.appendChild(elemento);
                }
                nodoPadre.appendChild(lista);
                nodoPadre.setAttribute('tienehijos','true');
                
            }
            imgLoading.src=realSrcImg;
            imgLoading.src=getImagenAbierta(realSrcImg);
        }
    }
}


function cargaRaiz(id,url,nombreId) {
    idRaiz=id;
    idCapa=id;
    var indice =0;
    var nodoPadre=document.getElementById('raiz'+idRaiz);
    if(nodoPadre.getAttribute('tienehijos')=='true'){
        for (var h=nodoPadre.childNodes.length;h>0;h--){
            nodoPadre.removeChild(nodoPadre.childNodes.item(h-1));
        }
    } 
    if(window.XMLHttpRequest){xhttp=new XMLHttpRequest();}
    else if(window.ActiveXObject){xhttp=new ActiveXObject('Microsoft.XMLHTTP');}
    var params=new Array();
    params[indice]='nombreId='+nombreId;
    indice++;
    var c=findObj('idNodo');
    if (c){
        params[indice]='idNodo'+'='+findObj('idNodo').value;
        indice++;
    }
    var params=tratarParametros(params);
    var sendf=params.join('&');
    xhttp.open('POST',url,true);
    xhttp.onreadystatechange=cargarNodosRaiz;
    xhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=ISO-8859-15');
    xhttp.setRequestHeader('Connection','close');
    xhttp.send(sendf);
    mostrarArbol(id+'Capa');
}

function cargarNodosRaiz(){
    if(xhttp.readyState==4){
        if(xhttp.status!=200){
            sesionCaducada();
        } else {
            var respuesta=xhttp.responseXML; // responseText
            var nodoPadre=findObj('raiz'+idRaiz);  
            var doc=window.document;
            var elementos=(respuesta.getElementsByTagName('elementos')[0]).childNodes[0].nodeValue;
            var nombreId=(respuesta.getElementsByTagName('nombreId')[0]).childNodes[0].nodeValue;
            if(elementos>0){
                for(i=0;i<elementos;i++){
                    var elemento=doc.createElement('li');
                    var enlaceIcono=doc.createElement('a');
                    var icono=doc.createElement('img');
                    var value=(respuesta.getElementsByTagName('value')[i]).childNodes[0].nodeValue;
                    var texto=(respuesta.getElementsByTagName('texto')[i]).childNodes[0].nodeValue;
                    var titulo=(respuesta.getElementsByTagName('titulo')[i]).childNodes[0].nodeValue;
                    var rutaimg=(respuesta.getElementsByTagName('rutaimagen')[i]).childNodes[0].nodeValue;
                    var ruta;
                    if(respuesta.getElementsByTagName('ruta')[i]){
                        ruta=(respuesta.getElementsByTagName('ruta')[i]).childNodes[0].nodeValue;
                    }
                    var url=(respuesta.getElementsByTagName('url')[i]).childNodes[0].nodeValue;
                    var noseleccionable=false;
                    if(respuesta.getElementsByTagName('seleccionable')[i]){
                        noseleccionable=(respuesta.getElementsByTagName('seleccionable')[i]).childNodes[0].nodeValue=="true";
                    }
                    var tieneHijos;
                    if(respuesta.getElementsByTagName('tienehijos')[i]){
                        tieneHijos=(respuesta.getElementsByTagName('tienehijos')[i]).childNodes[0].nodeValue;
                    }
                   
                    icono.id='idImg_'+idCapa+'_'+value;
                    
                    icono.border=0;
                    if(tieneHijos == 'true'){
                        icono.src=rutaimg;
                        enlaceIcono.href="javascript:seleccionNodo('"+idCapa+"','"+value+"','"+url+"','"+nombreId+"')";
                        enlaceIcono.appendChild(icono);
                        elemento.appendChild(enlaceIcono);
                    }else{
                        icono.src=getImagenNoHijos(rutaimg);
                        elemento.appendChild(icono);
                    }
                    elemento.appendChild(doc.createTextNode(' '));
                    if(!noseleccionable){
                        var enlaceTitulo=doc.createElement('a');
                        if(ruta){
                            enlaceTitulo.href="javascript:seleccionPadre"+idCapa+"('"+idCapa+"','"+value+"','"+ruta+"')";
                        }else{
                            enlaceTitulo.href="javascript:seleccionPadre"+idCapa+"('"+idCapa+"','"+value+"')";
                        }
                        enlaceTitulo.appendChild(doc.createTextNode(texto));
                        elemento.appendChild(enlaceTitulo);
                    }else{
                        var enlaceTitulo=doc.createElement('a');
                        enlaceTitulo.href="javascript:seleccionNodo('"+idCapa+"','"+value+"','"+url+"','"+nombreId+"')";
                        enlaceTitulo.appendChild(doc.createTextNode(texto));
                        elemento.appendChild(enlaceTitulo);
                    }
                    elemento.id=nombreId+'_'+idCapa+'_'+value;
                    elemento.setAttribute('titulo',titulo);
                    elemento.setAttribute('nombreId',nombreId);
                    elemento.setAttribute('tienehijos','false');
                    if(respuesta.getElementsByTagName('vigencia')[i]) {
                        var vigencia=(respuesta.getElementsByTagName('vigencia')[i]).childNodes[0].nodeValue;
                        elemento.setAttribute('vigencia',vigencia);
                    }
                    nodoPadre.appendChild(elemento);
                }
                nodoPadre.setAttribute('tienehijos','true');
            }
        }
    }
}

var selectsHiddenArbol = new Array();
function mostrarArbol(id) {
    
    if (findObj(id).style.display=='block') {
        mostrarSelects();
        findObj(id).style.display='none';
        findObj(id).style.visibility='hidden';
    } else {
        ocultarSelects();
        var scrolTop = getPosicionScrollTop();
        var scrolLeft = getPosicionScrollLeft();
        
        findObj(id).style.top = scrolTop+"px";
        findObj(id).style.left = scrolLeft+"px";
        
        findObj(id).style.marginTop = '10%';
        findObj(id).style.marginLeft = '15%';
        findObj(id).style.display='block';
        findObj(id).style.visibility='visible';
    }
}


function borrarArbol(id) {
    findObj(id).value='';
    findObj(id+'Desc').value='';
}

function getImagenNoHijos(rutaimg){

  var ultimo = rutaimg.lastIndexOf(".");
    var subRuta;
    var ruta;
    if( ultimo > -1){
        ruta = rutaimg.substring(0, ultimo);
        subRuta = rutaimg.substring(ultimo);
    }
    
    rutaimg = ruta + subRuta.split(".").join("_vacio.");
    return rutaimg;
    
}


function getImagenAbierta(rutaimg){
    var ultimo = rutaimg.lastIndexOf(".");
    var subRuta;
    var ruta;
    if( ultimo > -1){
        ruta = rutaimg.substring(0, ultimo);
        subRuta = rutaimg.substring(ultimo);
    }
    
    rutaimg = ruta + subRuta.split(".").join("_up.");
    return rutaimg;
}
function getImagenCerrada(rutaimg){
    var ultimo = rutaimg.lastIndexOf(".");
    var ruta;
    if( ultimo > 3 ){
        ruta = rutaimg.substring(0, ultimo-3);
        subRuta = rutaimg.substring(ultimo-3);
    }
    
    rutaimg = ruta + subRuta.split("_up.").join(".");
    return rutaimg;
    
}