function checkboxList(instanceName, trackingElementId, containerElementId)
{
	this.instanceName = instanceName;
	this.add = addCheckboxNode;
	this.render = renderCheckboxList;
	this.allNodes = new Object;
	this.trackingElement = trackingElementId;
	this.containerElementId = containerElementId;
	this.statut = false;
	this.countChecked = 0;
	this.valueNames;
}

function radioboxList(instanceName, trackingElementId, containerElementId)
{
	this.instanceName = instanceName;
	this.add = addRadioboxNode;
	this.render = renderRadioboxList;
	this.allNodes = new Object;
	this.trackingElement = trackingElementId;
	this.containerElementId = containerElementId;
	this.statut = false;
	this.countChecked = 0;
}

function checkboxNode(id, text, checked, mainItem)
{
	this.id = id;
	this.text = text;
	this.checked = checked;
	this.show = expandParent;
	this.rootInstance = '';
	this.mainItem = mainItem != undefined ? mainItem : false;
}

function radioboxNode(id, text, checked)
{
	this.id = id;
	this.text = text;
	this.checked = checked;
	this.show = expandParent;
	this.rootInstance = '';
	this.mainItem = false;
}

function expandParent()
{
	var p = this.parent;
	while (p)
	{
		var el = document.getElementById(p.id);
		el.style.display = 'block';
		p = p.parent;
	}
}

function renderCheckboxList(name)
{
	var checkboxListString = '';
	for (var n in this.allNodes)
	{
		checkboxListString += this.allNodes[n].render(name);
	}

	var container = document.getElementById(this.containerElementId);

	if (container)
		container.innerHTML = checkboxListString;

	this.updateValue(true);
	this.valueNames = name;
}

function renderRadioboxList(name)
{
	var radioboxListString = '';
	for (var n in this.allNodes)
	{
		radioboxListString += this.allNodes[n].render(name);
	}

	var container = document.getElementById(this.containerElementId);

	if (container)
		container.innerHTML = radioboxListString;

	this.updateValue(true);
}

checkboxList.prototype.updateValue = function(display)
{
	this.countChecked = 0;
	var countCriterions = 0;
	for (var n in this.allNodes)
	{
		if (this.allNodes[n].checked)
		{
			if (display)
				this.allNodes[n].show();
			this.countChecked += 1
		}
		countCriterions += 1;
	}
	if (this.countChecked > 0)
	{
		document.getElementById(this.trackingElement).innerHTML = this.countChecked + " criterions selected";
	}
	else
	{
		document.getElementById(this.trackingElement).innerHTML = "Select up to " + countCriterions + " criterions";
	}
}

checkboxList.prototype.toggle = function(nodeId, checked)
{
	// If the user clicks a checkbox, the corresponding object in the associative array is updated

	var clickedNodeItem, n, mainItem;
	for (n in this.allNodes)
	{
		if (this.allNodes[n].id == nodeId)
		{
			clickedNodeItem = this.allNodes[n];
		}
		if (this.allNodes[n].mainItem)
		{
			mainItem = this.allNodes[n];
		}
	}
	for (n in this.allNodes)
	{
		if (this.allNodes[n].id == nodeId)
		{
			this.allNodes[n].checked = checked;
		}
		else
		{
			if (!this.allNodes[n].mainItem && clickedNodeItem.mainItem)
			{
			    this.allNodes[n].checked = false;
			}
		}
	}
	if (clickedNodeItem.mainItem)
	{
		var elems = document.getElementsByName(this.valueNames);
		for (n = 0 ; n < elems.length; n++)
		{
			if (elems[n].value != clickedNodeItem.id)
			{
				elems[n].checked = false;
			}
		}
	}
	else
	{
		var elem = document.getElementById('cb' + this.valueNames + '_' + mainItem.id);
		elem.checked = false;
	}
	// The tracking field is updated, but the nodes are not auto-expanded
	this.updateValue(false);

}

function addCheckboxNode(id, label, checked, mainItem)
{
	var n = new checkboxNode(id, label, checked, mainItem);
	n.rootInstance = this.instanceName;
	this.allNodes[n.id] = n;
}

function addRadioboxNode(id, label, checked, parentId)
{
	var n = new radioboxNode(id, label, checked);
	n.rootInstance = this.instanceName;
	this.allNodes[n.id] = n;
}

checkboxNode.prototype.render = function(name)
{
	// Renders a node to the browser
	
	var nodeString = '<span class="node' + '">';
	nodeString += '<input type="checkbox" name = "' + name + '" value=' + this.id + ' id="cb' + name + '_' + this.id + '" ';
	nodeString += 'onClick="' + this.rootInstance + '.toggle(\'' + this.id + '\', this.checked)"';
	nodeString += (this.checked ? ' checked' : '') + '> ';
	nodeString += '<span>' + this.text + '</span>';
	nodeString += '</span>';
	return nodeString;

}

radioboxNode.prototype.render = function(name)
{
	// Renders a node to the browser

	var nodeString = '<span class="node' + '">';
	nodeString += '<input type="radio" name = "' + name + '" value=' + this.id + ' id="cb' + name + '_' + this.id + '" ';
	nodeString += 'onClick="' + this.rootInstance + '.toggle(\'' + this.id + '\', this.checked)"';
	nodeString += (this.checked ? ' checked' : '') + '> ';
	nodeString += '<span>' + this.text + '</span>';
	nodeString += '</span>';
	return nodeString;

}

var currentClick;
var numberClickNode = 0;
var numberAllClick = 0;

function imgMouseDown(element)
{
	return function()
	{
		document.getElementById(element).src = "/portal/images/pointerAct.bmp";
	}
}
function imgMouseUp(element)
{
	return function()
	{
		document.getElementById(element).src = "/portal/images/pointerPas.bmp";
	}
}
function divMouseOver(element)
{
	return function()
	{
		document.getElementById(element).style.border = "1px solid blue";
	}
}
function divMouseOut(element)
{
	return function()
	{
		document.getElementById(element).style.pointer = "hand";
		document.getElementById(element).style.border = "1px solid #e0e0e0";
	}
}
function ClickOnList()
{
	return function()
	{
		numberClickNode = numberClickNode + 1;
	}
}
function hideAllScrollDiv(ArrayList)
{
	return function()
	{

		numberAllClick = numberAllClick + 1;

		for (var i = 0; i < ArrayList.length; i++)
		{
			if ((ArrayList[i] != currentClick) || (numberAllClick > numberClickNode))
			{
				document.getElementById(ArrayList[i]).style.display = "none";
			}
		}
		if (numberAllClick > numberClickNode)
		{
			numberAllClick = numberClickNode;
		}
	}
}
function showNode(node)
{
	return function()
	{
		currentClick = node;
		numberClickNode = numberClickNode + 1;
		var objNode = document.getElementById(node).style;

		if (objNode.display == "block")
			objNode.display = "none";
		else
		{
			objNode.display = "block";
		}
	}

}
function addEventsToElements(field, list, image)
{
	var Image = document.getElementById(image);
	Image.onmouseup = imgMouseUp(image);
	Image.onmousedown = imgMouseDown(image);
	var Field = document.getElementById(field);
	Field.onmouseover = divMouseOver(field);
	Field.onmouseout = divMouseOut(field)
	Field.onclick = showNode(list);
	var List = document.getElementById(list);
	List.onclick = ClickOnList();
}
