//if(browser.indexOf('Mozilla Firefox')>=0) {
	//theDiv = document.getElementById('imTree');
	//theDiv.style.height = "97px";
//}

for(i=0; i < imParents.length; i++) {
	curParent					= document.getElementById(imParents[i]);
	curParent.onclick = updateParent;

	for(j=0; j < imChilds[i].length; j++) {
		curChild					= document.getElementById(imChilds[i][j]);
		curChild.onclick	= updateChild;
	}
}

function updateParent() { 
	updateTree(this, 'parent');
}


function updateChild() { 
	updateTree(this, 'child');
}

function updateTreeStartFunctie(obj) {

	theParent = document.getElementById(obj.id)

	if (theParent.checked)
	{
		theParent.checked = true;
		highlightParent(obj.id, 'parsf');
	}
	else if (!theParent.checked)
	{
		theParent.checked = false;
		unHighlightParent(obj.id, 'parsf');
	}
}

function updateTree(obj, type) {

	if(type=='parent') {

		theParent = document.getElementById(obj.id)

		children = getChildren(obj);

		if (children.length == 0)
		{
			if (theParent.checked)
			{
				theParent.checked = true;
				document.getElementById(obj.id).value = 1;
				highlightParent(obj.id, 'par');
			}
			else if (!theParent.checked)
			{
				theParent.checked = false;
				document.getElementById(obj.id).value = "";
				unHighlightParent(obj.id, 'par');
			}
		}
		else
		{

			for(k=0; k < children.length; k++) {
				curCb = document.getElementById(children[k]);
				toggleChildren(obj.checked, curCb);
			}
			
			if(allBrothersChecked(children)) {
				checkParent(obj.id);
				document.getElementById(obj.id).value = 1;
				highlightParent(obj.id, 'par');
			} 
			else if(noBrothersChecked(children))	{
				unCheckParent(obj.id);
				document.getElementById(obj.id).value = "";
				unHighlightParent(obj.id, 'par');
			}
		}

	}
	if(type=='child') {
		
		parentCb		= getParent(obj);
		brothers	= getBrothers(obj);

		if(allBrothersChecked(brothers)) {
			checkParent(parentCb);
			document.getElementById(parentCb).value = 1;
			highlightParent(parentCb, 'par');
		} 
		else if(noBrothersChecked(brothers))	{
			unCheckParent(parentCb);
			document.getElementById(parentCb).value = "";
			unHighlightParent(parentCb, 'par');
		}
		else {
			checkParent(parentCb);
			document.getElementById(parentCb).value = "";
			semiHighlightParent(parentCb, 'par');
		}
	}
}


function unHighlightParent(objId, parId) {

	parentDiv = getParentDiv(objId, parId);
	
	parentDiv.style.fontWeight = 'normal';
	parentDiv.style.backgroundImage = "url(/images/tree/white.gif)";
	parentDiv.style.backgroundColor = "white";
}


function highlightParent(objId, parId) {

	parentDiv = getParentDiv(objId, parId);
	
	parentDiv.style.fontWeight = 'bold';
	parentDiv.style.backgroundImage = "url(/images/tree/subSelected.gif)";
	parentDiv.style.backgroundColor = "#E2F4FB";
}


function semiHighlightParent(objId, parId) {

	parentDiv = getParentDiv(objId, 'par');
	
	parentDiv.style.fontWeight = 'normal';
	parentDiv.style.backgroundImage = "url(/images/tree/subSelectedGrey.gif)";
	parentDiv.style.backgroundColor = "#E5E4E4";
}

function getParentDiv(objId, parId) {
	result = objId.split('_');
	parentId = document.getElementById(parId+result[1]);
	return parentId;
}


function unCheckParent(objId) {
	theParent = document.getElementById(objId)

	if(theParent.checked) {
		theParent.checked = false;
	}
}


function checkParent(objId) {
	theParent = document.getElementById(objId)
	if(!theParent.checked) {
		theParent.checked = true;
	}
}


function noBrothersChecked(brothersArr) {
	cntChecked = 0;
	for(r=0; r < brothersArr.length; r++) {
		curEl = document.getElementById(brothersArr[r]);
		if(curEl.checked) {
			cntChecked++;
		}
	}
	if(cntChecked==0) {
		return true;
	} else {
		return false;	
	}

}


function allBrothersChecked(brothersArr) {
	cntChecked = 0;
	for(r=0; r < brothersArr.length; r++) {
		curEl = document.getElementById(brothersArr[r]);
		if(curEl.checked) {
			cntChecked++;
		}
	}

	if(cntChecked==brothersArr.length) {
		return true;
	} else {
		return false;
	}

}


function getChildren(obj) {
	for(p=0; p < imParents.length; p++) {
		if(obj.id == imParents[p]) {
			return imChilds[p];	
		}
	}
}


function getParent(obj) {
	for(l=0; l < imChilds.length; l++) {
		for(m=0; m < imChilds[l].length; m++) {
			if(imChilds[l][m]==obj.id) {
				return imParents[l];
			}
		}
	}
}

function getBrothers(obj) {
	for(n=0; n < imChilds.length; n++) {
		for(q=0; q < imChilds.length; q++) {
			if(imChilds[n][q]==obj.id) {
				return imChilds[n];
			}
		}
	}
}


function toggleChildren(parentChecked,obj) {
	if(parentChecked) {
		obj.checked = true;
	} else {
		obj.checked = false;
	}
}

