function Node(id, pid, name, url, title, target, isopen, img, chkName, chkId, chkUrl, chkValue,tipo) {
    this.id = id;
    this.pid = pid;
    this.name = name;
    this.url = url;
    this.title = title;
    this.target = target;
    this.img = img;
    this._io = isopen || false;
    this._ls = false;
    this._hc = false;
    this._is = false;
    this.chkName = chkName;
    this.chkId = chkId;
    this.chkUrl = chkUrl;
    this.chkValue = chkValue;
    this.tipo = tipo;//tipos: 0.- Raiz; 1.-Proyecto; 2.- Area; 3.-Carpeta; 4.- Inbox; 5.- Papelera; 6.- Catalogo; 7.-Borradores
}


function dTree(objName, imgFold, imgBase, imgOpen, imgClosed, imgSheet, styNode, styNodeSel,styNodeNoLink) {
    this.arrNodes = [];
    this.arrRecursed = [];
    this.arrIcons = [];
    this.rootNode = -1;
    this.strOutput = '';
    this.selectedNode = null;
    this.instanceName = objName;
    this.imgFolder = imgFold;
    this.target	= null;
    this.hasLines = true;
    this.clickSelect = true;
    this.folderLinks = true;
    this.useCookies = true;
	this.folderOpen = imgOpen;
	this.folderClosed = imgClosed;
	
	this.sheet = [];
	this.sheet[0] = imgSheet;
	this.sheet[1] = imgSheet;
	this.sheet[2] = imgSheet;
	this.sheet[3] = "sm_fold.gif";
	this.sheet[4] = "inboxpeq.gif";
	this.sheet[5] = "recycle_binpeq.gif";
	this.sheet[6] = imgSheet;
	this.sheet[7] = "drafts.gif";
	this.sheet[8] = "inboxpeqnews.gif";
	
	this.iOpen = [];
	this.iOpen[0] = imgOpen;
	this.iOpen[1] = imgOpen;
	this.iOpen[2] = imgOpen;
	this.iOpen[3] = "sm_fold.gif";
	this.iOpen[4] = "inboxpeq.gif";
	this.iOpen[5] = "recycle_binpeq.gif";
	this.iOpen[6] = imgOpen;
	this.iOpen[7] = "drafts.gif";
	this.iOpen[8] = "inboxpeqnews.gif";
	
	this.iClose = [];
	this.iClose[0] = imgClosed;
	this.iClose[1] = imgClosed;
	this.iClose[2] = imgClosed;
	this.iClose[3] = "sm_fold.gif";
	this.iClose[4] = "inboxpeq.gif";
	this.iClose[5] = "recycle_binpeq.gif";
	this.iClose[6] = imgClosed;
	this.iClose[7] = "drafts.gif";
	this.iClose[8] = "inboxpeqnews.gif";
	
	
	this.nodeStyle = styNode;
	this.nodeSelStyle = styNodeSel;
	this.base = imgBase;


    this.add = function(id, pid, name, url, title, target, isopen, img, chkName, chkId, chkUrl, chkValue,tipo) {
		this.arrNodes[this.arrNodes.length] = new Node (id, pid, name, url, title, 
                                              			target, isopen, img, chkName, chkId, chkUrl, chkValue,tipo);
    }

    this.addNodeList = function(nodesList) {
        var i;
        for (i = 0; i < nodesList.length; i++) {
            this.add (nodesList[i][0], nodesList[i][1], nodesList[i][2], nodesList[i][3], 
            		  nodesList[i][4], nodesList[i][5], nodesList[i][6], nodesList[i][7], nodesList[i][8],nodesList[i][9],nodesList[i][10],nodesList[i][11],nodesList[i][12]);
        }
    }


    this.draw = function() {    
        if (document.getElementById) {
            this.preloadIcons();

            if (this.useCookies) {
                this.selectedNode = this.getSelected();
            }

            this.addNode(this.rootNode);

            document.writeln(this.strOutput);
        }
        else {
            document.writeln('Navegador no soportado.');
        }
    }

    this.openAll = function() {
		this.oAll(true);
    }

    this.closeAll = function() {
		this.oAll(false);
    }


    this.preloadIcons = function() {
		if (this.hasLines) {
			this.arrIcons[0] = new Image();
			this.arrIcons[0].src = this.imgFolder + 'plus.gif';
			this.arrIcons[1] = new Image();
			this.arrIcons[1].src = this.imgFolder + 'plusbottom.gif';
			this.arrIcons[2] = new Image();
			this.arrIcons[2].src = this.imgFolder + 'minus.gif';
			this.arrIcons[3] = new Image();
			this.arrIcons[3].src = this.imgFolder + 'minusbottom.gif';
		}
		else {
			this.arrIcons[0] = new Image();
			this.arrIcons[0].src = this.imgFolder + 'nolines_plus.gif';
			this.arrIcons[1] = new Image();
			this.arrIcons[1].src = this.imgFolder + 'nolines_plus.gif';
			this.arrIcons[2] = new Image();
			this.arrIcons[2].src = this.imgFolder + 'nolines_minus.gif';
			this.arrIcons[3] = new Image();
			this.arrIcons[3].src = this.imgFolder + 'nolines_minus.gif';
		}

		//Por tipos
		this.arrIcons[4] = new Array();
		this.arrIcons[5] = new Array();
		//this.arrIcons[4] = new Image();
		for(j=0; j<7; j++){
			this.arrIcons[4][j] = new Image();
			this.arrIcons[5][j] = new Image();
			this.arrIcons[4][j].src = this.imgFolder + this.iClose[j];
			this.arrIcons[5][j].src = this.imgFolder + this.iOpen[j];
		}
		
		/*this.arrIcons[4][0].src = this.imgFolder + this.folderClosed;
		this.arrIcons[4][1].src = this.imgFolder + this.folderClosed;
		this.arrIcons[4][2].src = this.imgFolder + this.folderClosed;
		this.arrIcons[4][3].src = this.imgFolder + "sm_fold.gif";
		this.arrIcons[4][4].src = this.imgFolder + this.folderClosed;
		this.arrIcons[4][5].src = this.imgFolder + this.folderClosed;
		
		this.arrIcons[5][0].src = this.imgFolder + this.folderOpen;
		this.arrIcons[5][1].src = this.imgFolder + this.folderOpen;
		this.arrIcons[5][2].src = this.imgFolder + this.folderOpen;
		this.arrIcons[5][3].src = this.imgFolder + "sm_fold.gif";
		this.arrIcons[5][4].src = this.imgFolder + this.folderOpen;
		this.arrIcons[5][5].src = this.imgFolder + this.folderOpen;*/
    }


    this.addNode = function(pNode) {
        var n;
        var cn;
        var r;

		for (n = 0; n < this.arrNodes.length; n++) {
			if (this.arrNodes[n].pid == pNode) {
				cn = this.arrNodes[n];
				cn._hc = this.hasChildren(cn);
				cn._ls = (this.hasLines) ? this.lastSibling(cn) : false;
	
				if (cn._hc && !cn._io && this.useCookies) {
					cn._io = this.isOpen(cn.id);
				}
	
				if (this.clickSelect && cn.id == this.selectedNode) {
					cn._is = true;
					this.selectedNode = n;
				}
	
				if (!this.folderLinks && cn._hc) {
					cn.url = null;
				}
	
			
				if (this.rootNode != cn.pid) {
					for (r = 0; r < this.arrRecursed.length; r++) {
						this.strOutput += '<img src="' + this.imgFolder 
										  + ((this.arrRecursed[r] == 1 && this.hasLines) ? 'line' : 'empty') 
										  + '.gif" alt="" />';
					}
	
					(cn._ls) ? this.arrRecursed.push(0) : this.arrRecursed.push(1);
	
					if (cn._hc) {
						this.strOutput += '<a href="javascript: ' + this.instanceName 
										  + '.o(' + n + ');">'
										  + '<img id="j' + this.instanceName + n 
										  + '" src="' + this.imgFolder;
	
						if (!this.hasLines) {
							this.strOutput += 'nolines_';
						}
	
						this.strOutput += ((cn._io)
											? ((cn._ls && this.hasLines) ? 'minusbottom' : 'minus')
											: ((cn._ls && this.hasLines) ? 'plusbottom'  : 'plus' ))
										  + '.gif" alt="" /></a>';
					}
					else {
						this.strOutput += '<img src="' + this.imgFolder 
										  + ((this.hasLines) ? ( (cn._ls) ? 'joinbottom' : 'join' ) : 'empty') 
					                      + '.gif" alt="" />';
					}
				}
	

				if (cn.url != null){
					if (cn.url) {
						this.strOutput += '<a href="' + cn.url + '"';
		
						if (cn.title) {
							this.strOutput += ' title="' + cn.title + '"';
						}
		
						if (cn.target) {
							this.strOutput += ' target="' + cn.target + '"';
						}
		
						if (this.target && !cn.target) {
							this.strOutput += ' target="' + this.target + '"';
						}
		
						if (this.clickSelect) {
							if (cn._hc) {
								if (this.folderLinks) {
									this.strOutput += ' onclick="javascript: ' 
													  + this.instanceName + '.s(' + n + ');"';
								}
							}
							else {
								this.strOutput += ' onclick="javascript: ' 
												  + this.instanceName + '.s(' + n + ');"';
							}
						}
		
						this.strOutput += '>';
					}
		
					if (!this.folderLinks || !cn.url) {
						this.strOutput += '<a href="javascript: ' + this.instanceName 
										  + '.o(' + n + ');">';
					}
				} 
				this.strOutput += '<img id="i' + this.instanceName + n + '" src="' 
								  + this.imgFolder;
				this.strOutput += (cn.img) ? cn.img : ((this.rootNode == cn.pid) 
				                                       ? this.base : (cn._hc) 
													   ? ((cn._io) ? this.iOpen[cn.tipo]
													   : this.iClose[cn.tipo]) : this.sheet[cn.tipo]);
				this.strOutput += '" alt="" />';
				//Agregado para controlar el estilo de los nodos
				//sin enlace
				if(cn.url=='javascript:noLink()'){
					this.strOutput += '<span id="s' + this.instanceName + n + '" class="' 
								  + ((this.clickSelect) ? ((cn._is ? this.nodeSelStyle : this.nodeStyle)) 
								                        : this.nodeStyle) 
								  + '">';
				}else{
					this.strOutput += '<span id="s' + this.instanceName + n + '" class="' 
								  + this.styNodeNoLink 
								  + '">';				
				}
				if(cn.chkName != null){
					this.strOutput += '<input class="defChk" type=checkbox name="' + cn.chkName + '" id="' + cn.chkId + '" value =' + cn.chkValue + ' onclick="' + cn.chkUrl + '">';				
				}
				this.strOutput += cn.name;
				this.strOutput += '</span>';
	
				if (cn.url || (!this.folderLinks && cn._hc)) {
					this.strOutput += '</a>';
				}
	
				this.strOutput += '<br />\n';
	
				if (cn._hc) {
					this.strOutput += '<div id="d' + this.instanceName + n + '" style="display:'
					                  + ((this.rootNode == cn.pid || cn._io) ? '' : 'none')
					                  + ';">\n';
					this.addNode(cn.id);
					this.strOutput += '</div>\n';
				}
				this.arrRecursed.pop();
			}
		}
	}


	this.hasChildren = function(node) {
		var n;
		
		for (n = 0; n < this.arrNodes.length; n++) {
			if (this.arrNodes[n].pid == node.id) { 
				return true;
			}
		}
		
		return false;
	}

	
	this.lastSibling = function(node) {
		var lastId;
		var n;
		
		for (n = 0; n < this.arrNodes.length; n++) {
			if (this.arrNodes[n].pid == node.pid) {
				lastId = this.arrNodes[n].id;
			}
		}
		
		if (lastId == node.id) {
			return true;
		}
		
		return false;
	}


	this.isOpen = function(id) {
		var n;
		
		openNodes = this.getCookie('co' + this.instanceName).split ('.');
		
		for (n = 0; n < openNodes.length; n++) {
			if (openNodes[n] == id) {
				return true;
			}
		}
		
		return false;
	}


	this.isSelected = function(id) {
		selectedNode = this.getCookie('cs' + this.instanceName);
		
		if (selectedNode) {
			if (id == selectedNode) {
				this.selectedNode = id;
				return true;
			}
		}
		
		return false;
	}


	this.getSelected = function() {
		selectedNode = this.getCookie('cs' + this.instanceName);
		
		if (selectedNode) {
			return selectedNode;
		}
		
		return null;
	}


	this.s = function(id) {
		cn = this.arrNodes[id];
		if (this.selectedNode != id) {
			if (this.selectedNode) {
				eOldSpan = document.getElementById("s" + this.instanceName + this.selectedNode);
				if(cn.url!='javascript:noLink()'){	
					if(eOldSpan != null){
						eOldSpan.className = "node";
					}
				}
			}

			eNewSpan = document.getElementById("s" + this.instanceName + id);
			if(cn.url!='javascript:noLink()'){	
				eNewSpan.className = "nodeSel";
			}
			this.selectedNode = id;
			
			if (this.useCookies) { 
				this.setCookie('cs' + this.instanceName, cn.id);
			}
		}
	}


	this.o = function(id) {
		cn = this.arrNodes[id];

		 (cn._io) ? this.nodeClose(id,cn._ls) : this.nodeOpen(id,cn._ls);
		
		cn._io = !cn._io;

		if (this.useCookies) {
			this.updateCookie();
		}
	}


	this.oAll = function(open) {
		var n;
		
		for (n = 0; n < this.arrNodes.length; n++) {
			if (this.arrNodes[n]._hc && this.arrNodes[n].pid != this.rootNode) {
				if (open) {
					this.nodeOpen (n, this.arrNodes[n]._ls);
					this.arrNodes[n]._io = true;
				}
				else {
					this.nodeClose (n, this.arrNodes[n]._ls);
					this.arrNodes[n]._io = false;
				}
			}
		}
		
		if (this.useCookies) {
			this.updateCookie();
		}
	}


	this.nodeOpen = function(id, bottom) {
		var eDiv = document.getElementById('d' + this.instanceName + id);
		var eJoin = document.getElementById('j' + this.instanceName + id);
		eIcon = document.getElementById('i' + this.instanceName + id);
		eJoin.src = bottom ? this.arrIcons[3].src : this.arrIcons[2].src;
		
		if (!this.arrNodes[id].img) {
			eIcon.src = this.arrIcons[5][this.arrNodes[id].tipo].src;
		}
		
		eDiv.style.display = '';
	}


	this.nodeClose = function(id, bottom) {
		eDiv = document.getElementById('d' + this.instanceName + id);
		eJoin = document.getElementById('j' + this.instanceName + id);
		eIcon = document.getElementById('i' + this.instanceName + id);
		eJoin.src = (bottom) ? this.arrIcons[1].src : this.arrIcons[0].src;
		
		if (!this.arrNodes[id].img) {
			eIcon.src = this.arrIcons[4][this.arrNodes[id].tipo].src;
		}
		
		eDiv.style.display = 'none';
	}


	this.clearCookie = function() {
		var now = new Date();
		var yesterday = new Date (now.getTime() - 1000 * 60 * 60 * 24);
		
		this.setCookie('co'+this.instanceName, 'cookieValue', yesterday);
		this.setCookie('cs'+this.instanceName, 'cookieValue', yesterday);
	}

	
	this.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
		document.cookie = escape(cookieName) + '=' + escape(cookieValue)
						  + (expires ? '; expires=' + expires.toGMTString() : '')
			              + (path ? '; path=' + path : '')
			              + (domain ? '; domain=' + domain : '')
			              + (secure ? '; secure' : '');
	}


	this.getCookie = function(cookieName) {
		var cookieValue = '';
		var posName = document.cookie.indexOf(escape (cookieName) + '=');
		var posValue;
		var endPos;
		
		if (posName != -1) {
			posValue = posName + (escape (cookieName) + '=').length;
			endPos = document.cookie.indexOf (';', posValue);
			
			if (endPos != -1) {
				cookieValue = unescape(document.cookie.substring (posValue, endPos));
			}
			else {
				cookieValue = unescape(document.cookie.substring (posValue));
			}
		}
		
		return cookieValue;
	}


	this.updateCookie = function() {
		var n;
		var sReturn = '';
		
		for (n = 0; n < this.arrNodes.length; n++) {
			if (this.arrNodes[n]._io && this.arrNodes[n].pid != this.rootNode) {
				if (sReturn) {
					sReturn += '.';
				}
				
				sReturn += this.arrNodes[n].id;
			}
		}
		
		this.setCookie('co' + this.instanceName, sReturn);
	}

}



if (!Array.prototype.push) {
	Array.prototype.push = function array_push() {
		var i;
		
		for (i = 0; i < arguments.length; i++) {
			this[this.length] = arguments[i];
		}
		
		return this.length;
	}
}

if (!Array.prototype.pop) {
	Array.prototype.pop = function array_pop() {
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		
		return lastElement;
	}
}

