/* da_date: 5-4-2007 14:24 */
/* da_client: yyy */
/* da_module: xxx */
/* da_version: 0000 */

/*
2Do:
	- testen checkbox versie, doorgevoerd 12-9-2007 17:07 (Remco)
	- xml als bron van boom? Nuttig?
*/

/*
Boom:

- Init (boomname, boomidx)			: initialiseren boom (boomname = boomname.dat, boomidx = boomnummer)
- ProductBoom (node)				: haal boomtak op
- addBoom (index,mother,omschrijving,
	document,child,next,oldbookmark,icon)	: voeg teruggekomen boom data toe aan arrays
- isOpenFol (omId)				: is folder open (true/false)
- doLoadBranch (branchId)
- setActiveTocNr (nr)				: actieve node
- BoomLoadDoc (deel, nr)			: laad boomtak of als geen document, laad doc ('deel'-argument overbodig geworden)
- ZetZnummer (node)				: ?? controleer of correct ?? wordt gebruikt om gemarkeerd zoekgebied aan te geven. misschien array van maken, zodat makkelijk uit te breiden als boom vinkjes heeft.
- addOpenFol (node)				: folder toevoegen aan de open folder lijst
- remOpenFol (node)				: folder weghalen uit open folder lijst

- isChkFull (node)				: heeft node volle checkbox (true/false)
- isChkHalf (node)				: heeft node halve checkbox (true/false)
- isChkMother (node)				: heeft node volle checkbox (true/false)
- addChk (node)					: voeg node toe aan volle checkbox lijst
- addHalf (node)				: voeg node toe aan halve checkbox lijst
- remChk (node)					: verwijder node uit volle checkbox lijst
- remHalf (node)				: verwijder node uit halve checkbox lijst
*/

function Boom (boomname, boomidx) {
	this.Init(boomname, boomidx);
}

Boom.prototype = {
	Init: function (boomname, boomidx) {
		this.boomname = boomname;	// bestandsnaam boom zonder extensie (bijv. boom.dat)
		this.boomidx = boomidx;		// boomnr
		this.mo = new Array();		// mo staat voor moeder de moeder array
		this.om = new Array();		// om staat voor Omschrijving de omschrijvingen array
		this.md = new Array();		// md staat voor document
		this.ch = new Array();		// ch staat voor child, menu diper verwijzing (array)
		this.nx = new Array();		// nx staat voor Next, vervolgpointer naar volgende entrie (array)
		this.ob = new Array();		// ob staat voor OldBookmark
		this.bi = new Array();		// bi staat voor boomIcon
		this.openMaps = new Array();	// de array met daarin alle open mapjes...
		this.Znummer = 0;
		this.loadbranch = "";
		this.loadbranchrest = "";
		this.boomhtmlfile = '/boom.htm';
		this.url = this.boomhtmlfile + '?boomnr=' + this.boomidx
		this.activeTocNr = 0;

		this.ischeckbox = false; 	// hebben de boomnodes checkboxen
		this.chkMapsF = new Array();	// array met alle volle gevinkte nodes
		this.chkMapsH = new Array();	// array met alle half gevinkte nodes
		
		this.doHead = true;		// toon header na scrollen
		this.doZ = false; //true;
	},
	ProductBoom: function (node) {
		var boom = this.boomname;
		var ret = '';
		var boomnr = this.boomidx == 2 ? this.boomidx : '';
		var dom = this.boomidx == 2 ? 'dom' : '';
		var sr = this.boomidx == 2 ? ' -SUBROOT' : '';
		if(doCGI){
			if(doCD)	ret = gCGIPath+'bos'+dom+'.cgi?boom='+boom+'&node='+node+'&appc='+gAppCode+'&boomnr=' + boomnr;
			else		ret = gCGIPath+'bos.exe?'+boom+sr+' '+node+' '+boomnr;
		}else{
			if(node==0)	ret = gCGIPath+boom+'/boomdata.js';
			else		ret = gCGIPath+boom+'/node'+node+'.htm';
		}
		return ret;
	},
	addBoom: function (index,mother,omschrijving,document,child,next,oldbookmark,icon) {
//		top.Debug ('index : ' + index,'mother : ' +  mother,'omschrijving : ' +  omschrijving,'document : ' +  document,'child : ' +  child,'next : ' +  next,'oldbookmark : ' +  oldbookmark,'icon : ' + icon);
		this.mo[index] = mother;
		this.om[index] = omschrijving;
		this.md[index] = document;
		this.ch[index] = child;
		this.nx[index] = next;
		this.ob[index] = oldbookmark;
		this.bi[index] = icon;
	},
	isOpenFol: function (node) {
		for(var i in this.openMaps){
			if(this.openMaps[i]==node) return true;
		}
		return false;
	},
	doLoadBranch: function (branchId) {
		this.loadbranch = branchId;
		if (!top.init) return false;
		if (this.boomidx == 2) { myFindex2.location.replace(this.url); }
		else { myFindex.location.replace(this.url); }
	},
	setActiveTocNr: function (nr) {
		this.activeTocNr = nr;
	},
	BoomLoadDoc: function (deel, nr) {
//		top.Debug ('~~BoomLoadDoc ' + deel + ' ' + nr)
		if (!top.init) return false;

		this.setActiveTocNr(nr);
		
		if(this.md[nr]<0){
			var newnode = (-this.md[nr]);
			top.myFnode.location.replace(this.ProductBoom(newnode));
		}else{
			if (top.gZoekMasjien) {
				top.gZoekMasjien.formvars['foboomid'] = this.ob[nr];
			}
			if (this.md[nr]!=""){
				top.LoadPage(this.md[nr]);
			}
		}
	},
	ZetZnummer: function (node) {
		this.Znummer = node;
		
		if (!top.init) return false;

		if (top.gZoekMasjien) {
			top.gZoekMasjien.formvars['foboomid'] =	this.ob[node];
		}
	},
	addOpenFol: function (node) {
//		top.Debug ('!! addOpenFol : ' + node)
		if(node>0){
			var isopen = false;
			for(var i in this.openMaps.length) {
				if(this.openMaps[i] == node) isopen = true;
			}
			if (!isopen) this.openMaps[this.openMaps.length] = node;
			this.openMaps.sort(mysortfn);
		}
	},
	remOpenFol: function(node) {
//		top.Debug('remOpenFol : ' + node);
		var nextNode;
		var i,j;
	
		nextNode = this.nx[node];
		if(nextNode == -1) {
			nextNode = this.mo[node];
			while(this.nx[nextNode] == -1 && nextNode != 0) {
				nextNode = this.mo[nextNode];
			}
			nextNode = this.nx[nextNode];
			if (nextNode <= 0) {
				nextNode = 999999;
			}
		}

		j = 0;
		while(j < this.openMaps.length) {
			if(node <= this.openMaps[j] && this.openMaps[j] < nextNode) {
				if(this.openMaps[j] <= this.Znummer && this.Znummer < nextNode){
					this.Znummer = -1;
				}
				i = j;
				while(i+1 < this.openMaps.length) {
					this.openMaps[i] = this.openMaps[i+1];
					i++;
				}
				this.openMaps.length -= 1;
			} else {
				j++;
			}
		}
	},
	refreshTree: function () {
		this.activeTocNr = 0;	//	top.Znummer = 0;
		this.mo = [];	// mo staat voor moeder de moeder array
		this.om = [];	// om staat voor Omschrijving de omschrijvingen array
		this.md = [];	// md staat voor document
		this.ch = [];	// ch staat voor child, menu diper verwijzing (array)
		this.nx = [];	// nx staat voor Next, vervolgpointer naar volgende entrie (array)
		this.ob = [];	// ob staat voor OldBookmark
		this.bi = [];	// bi staat voor boomIcon
		this.Znummers = 0;
		this.openMaps = []; // de array met daarin alle open mapjes...
	},
	// checkbox functionaliteit //
	isChkFull: function(node) {
		for(var i in this.chkMapsF) {
			if(this.chkMapsF[i] == node) return true;
		}
		return false;
	},
	isChkHalf: function(node) {
		for(var i in this.chkMapsH) {
			if(this.chkMapsH[i] == node) return true;
		}
		return false;
	},
	isChkMother: function(node) {
		var ret = this.isChkFull(node);
		while(!ret && node!=0 && node != undefined){
			node = this.mo[node];
			ret = this.isChkFull(node);
		}
		return ret;
	},
	addChk: function(node) {
		this.chkMapsF.push(node);
	},
	addHalf: function(node) {
		this.chkMapsH.push(node);
	},
	remChk: function (node) {	
		var i, j = 0;
		while(j<this.chkMapsF.length){
			if(node == this.chkMapsF[j]){
				i = j;
				while(i+1<this.chkMapsF.length){
					this.chkMapsF[i] = this.chkMapsF[i+1];
					i++;
				}
				this.chkMapsF.length -= 1;
			}else{
				j++;
			}
		}
	},
	remHalf: function(node) {
		var i, j = 0;
		while(j<this.chkMapsH.length){
			if(node == this.chkMapsH[j]){
				i = j;
				while(i+1<this.chkMapsH.length){
					this.chkMapsH[i] = this.chkMapsH[i+1];
					i++;
				}
				this.chkMapsH.length -= 1;
			}else{
				j++;
			}
		}
	}
}

/*
 * Sorteer functie
 */
function mysortfn(a,b) {
	if (a<b) return -1;
	if (a>b) return 1;
	return 0;
}

/*
 * deze functie wordt aangeroepen in return document van cgi-call. Moet dus in deze vorm blijven bestaan
 */
function addBoom(index,mother,omschrijving,document,child,next,oldbookmark,icon) {
//	var fra = top.myFindex;
//	if (fra) fra.currBoom.addBoom(index,mother,omschrijving,document,child,next,oldbookmark,icon);
	gBoom.addBoom(index,mother,omschrijving,document,child,next,oldbookmark,icon);
}

function addBoom2(index,mother,omschrijving,document,child,next,oldbookmark,icon) {
//	var fra = top.myFindex2;
//	if (fra) fra.currBoom.addBoom(index,mother,omschrijving,document,child,next,oldbookmark,icon);
	gBoom2.addBoom(index,mother,omschrijving,document,child,next,oldbookmark,icon);
}

/*
 * is de actieve tab een boom tab. aangepast aangezien er meerder frames worden gebruikt (niet alleen myFindex)
 */
function isBoomTab() {
	var activeTabIndex = GetActiveTab('tabsIndex');
	return (activeTabIndex.win == 'frIndexMain')
//	return (getCurrBoomHandle());
}

/*
 * geeft handle terug naar huidige boom
 */
function getCurrBoomHandle() {
	return top.myFindex.currBoom;
}

