/*--------------------------------------------------|
| pTree 2.05 | www.destroydrop.com/javascript/tree/ |
|---------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landrí°¹í¾ |*/

// Node object

function Pnode(id, pid, open, ls, chk, lokname) {
    this.id = id;
    this.pid = pid;
    this._io = open || false;
	 this._ls = ls;
	 this.chk = chk || false;
	 this.lokname = lokname;
}

function Cnode(id, pid, chk, lokname) {
    this.id = id;
    this.pid = pid;
    this.chk = chk || false;
	 this.lokname = lokname;
}
// Tree object

function pTree(objName) {
    this.icon = {      
	   root				: '/dtree/img/base.gif',	
		folder			: '/dtree/img/folder.gif',
		folderOpen	: '/dtree/img/folderopen.gif',
		node				: '/dtree/img/page.gif',
		empty				: '/dtree/img/empty.gif',
		line				: '/dtree/img/line.gif',
		join				: '/dtree/img/join.gif',
		joinBottom	: '/dtree/img/joinbottom.gif',
		plus				: '/dtree/img/plus.gif',
		plusBottom	: '/dtree/img/plusbottom.gif',
		minus				: '/dtree/img/minus.gif',
		minusBottom	: '/dtree/img/minusbottom.gif',
		nlPlus			: '/dtree/img/nolines_plus.gif',
		nlMinus			: '/dtree/img/nolines_minus.gif'
    };
    
    this.obj = objName;
    this.aNodes = [];
	 this.checkBoxes = [];
    this.rozwin = false;
	 this.nodesStr = '';
	 this.checksStr = '';
	 this.textareaStr = '';
}


// Adds a new node to the node array
pTree.prototype.add = function(id, pid, open, ls, chk, lokname) {
    this.aNodes[this.aNodes.length] = new Pnode(id, pid, open, ls, chk, lokname);
};

// Adds a new checkbox to the checkboxes array
pTree.prototype.addC = function(id, pid, chk, lokname) {
	 this.checkBoxes[this.checkBoxes.length] = new Cnode(id, pid, chk, lokname);
};


// Open/close all nodes
pTree.prototype.openAll = function() {
    this.rozwin = !this.rozwin;
    this.oAll(this.rozwin);
    //document.rozwin[0].value = (this.rozwin)?"zwiÅ„ wszystkie":"rozwiÅ„ wszystkie";
};

pTree.prototype.closeAll = function() {
    this.oAll(false);
};


// Toggle Open or close
pTree.prototype.o = function(ID) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].id == ID) {
            var cn = this.aNodes[n];
            this.nodeStatus(!cn._io, ID, this.aNodes[n]._ls);
            cn._io = !cn._io;
            this.closeLevel(cn);
				break;
        }
    }
};

// Toggle Open or close checkbox Node
pTree.prototype.ch = function(ID) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].id == ID) {
            var cn = this.aNodes[n];
            chInput = document.getElementById("ch" + ID);
            cn.chk = chInput.checked;
				this.toggleLevel(cn);
				//unchecks all parent and parentparent nodes if false
				if(!cn.chk && cn.pid) this.uncheckParents(cn.pid);
				break;
        }
    }
};

// Toggle Open or close checkbox
pTree.prototype.chC = function(ID) {
    for (var n=0; n<this.checkBoxes.length; n++) {
        if (this.checkBoxes[n].id == ID) {
            var cn = this.checkBoxes[n];
            chInput = document.getElementById("ch" + ID);
            cn.chk = chInput.checked;
				//unchecks all parent and parentparent nodes if false
				if(!cn.chk && cn.pid) this.uncheckParents(cn.pid);
				break;
        }
    }
};

// checks or unchecks all checkboxes under node level
pTree.prototype.toggleLevel = function(node) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node.id) {
            this.aNodes[n].chk = node.chk;
				chInput = document.getElementById("ch" + this.aNodes[n].id);
				chInput.checked = node.chk;
            this.toggleLevel(this.aNodes[n]);
        }
    }
	 //toggles checkboxes under certain node
	 for (var m=0; m<this.checkBoxes.length; m++) {
        if (this.checkBoxes[m].pid == node.id) {
            this.checkBoxes[m].chk = node.chk;
				chInput = document.getElementById("ch" + this.checkBoxes[m].id);
				chInput.checked = node.chk;
        }
    }
};

// unchecks all parents of the tree
pTree.prototype.uncheckParents = function(pid) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].id == pid) {
            this.aNodes[n].chk = false;
				chInput = document.getElementById("ch" + this.aNodes[n].id);
				chInput.checked = false;
				if(this.aNodes[n].pid) this.uncheckParents(this.aNodes[n].pid);
            break;
        }
    }
};

//unchecks all nodes and checkes
pTree.prototype.treeClear = function() {
    for (var n=0; n<this.aNodes.length; n++) {
        this.aNodes[n].chk = false;
        chInput = document.getElementById("ch" + this.aNodes[n].id);
        chInput.checked = false;
    }
    for (var m=0; m<this.checkBoxes.length; m++) {
        this.checkBoxes[m].chk = false;
        chInput = document.getElementById("ch" + this.checkBoxes[m].id);
        chInput.checked = false;
    }
}

pTree.prototype.apply_locations = function() {
    this.nodesStr = '';
    this.checksStr = '';
    this.textareaStr = '';
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].chk) {
            //wyÅ›wietla tylko wÄ™zÅ‚y, ktÃ³re nie majÄ… zaznaczonych rodzicÃ³w, zapisuje wszystkie zaznaczone wÄ™zÅ‚y
            if(this.aNodes[n].pid == '0') this.textareaStr += this.aNodes[n].lokname+'\n';
            else if(!this.isParCh(this.aNodes[n].pid)) this.textareaStr += this.aNodes[n].lokname+'\n';
            
            this.nodesStr += 'n'+this.aNodes[n].id+',';
        } else for (var m=0; m<this.checkBoxes.length; m++) {
            if (this.checkBoxes[m].pid == this.aNodes[n].id && this.checkBoxes[m].chk) {
                this.textareaStr += this.checkBoxes[m].lokname+'\n';
                this.nodesStr += this.checkBoxes[m].id+',';
            }
        }
    }
    /*
                for (var m=0; m<this.checkBoxes.length; m++) {
                        if (this.checkBoxes[m].chk) {
                            this.checksStr += this.checkBoxes[m].id+',';
                        }
                    }
                */
    var nodesStrObj = document.getElementById('nodesStr');
    this.nodesStr = this.nodesStr.slice(0, -1);
    nodesStrObj.value = this.nodesStr;
    //var checksStrObj = document.getElementById('checksStr');
    //this.checksStr = this.checksStr.slice(0, -1);
    //checksStrObj.value = this.checksStr;
    var lok_tree = getObject('lok_tree');
    lok_tree.style.display = 'none';
    var lok_textarea = getObject('lok_textarea');
    lok_textarea.value = this.textareaStr;
}
// checks if node is checked
pTree.prototype.isParCh = function(id) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].id == id) {
				if(this.aNodes[n].chk) return true;
            break;
        }
    }
	 return false;
};


// Open or close all nodes
pTree.prototype.oAll = function(status) {
    for (var n=0; n<this.aNodes.length; n++) {
        var ID = this.aNodes[n].id;
        this.nodeStatus(status, ID, this.aNodes[n]._ls);
        this.aNodes[n]._io = status;
    }
};



// Closes all nodes on the same level as certain node
pTree.prototype.closeLevel = function(node) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._io == true) {
            this.nodeStatus(false, this.aNodes[n].id, this.aNodes[n]._ls);
            this.aNodes[n]._io = false;
            this.closeAllChildren(this.aNodes[n]);
        }
    }
};

// Closes all children of a node
pTree.prototype.closeAllChildren = function(node) {
    for (var n=0; n<this.aNodes.length; n++) {
        if (this.aNodes[n].pid == node.id) {
            if (this.aNodes[n]._io) this.nodeStatus(false, this.aNodes[n].id, this.aNodes[n]._ls);
            this.aNodes[n]._io = false;
            this.closeAllChildren(this.aNodes[n]);
        }
    }
};

// Change the status of a node(open or closed)
pTree.prototype.nodeStatus = function(stan, ID, last) {
   eDiv	= document.getElementById("c" + ID);
  	eJoin	= document.getElementById("i" + ID);
	eFolder	= document.getElementById("j" + ID);
	eJoin.src = (last)?((stan)?this.icon.minusBottom:this.icon.plusBottom):((stan)?this.icon.minus:this.icon.plus);
	eFolder.src = (stan)?this.icon.folderOpen:this.icon.folder;
   eDiv.style.display = (stan) ? 'block': 'none';
};


