function CheckList(){

	this.containerName = 'boxRaffinaZone';
	this.zoneSelectedCtlName = 'CurrentZoneSelected';
	this.groups = new Object();
	this.groupChecks = new Object();
	this.container = null;
	this.zoneSelectedCtl = null;
	this.selectedZones = new Array();
	this.onZoneOver = null;
	this.onZoneOut = null;
	this.onSelectZone = null;
	this.onUnselectZone = null;
	this.onSelectAll = null;
	this.onUnselectAll = null;
	this.onSelectGroup = null;
	this.onUnselectGroup = null;
	
	this.Init = function(){
	
		this.groups = new Object();
		this.groupChecks = new Object();
		this.selectedZones = new Array();
		
		if (document.getElementById(this.containerName)) {
			if (document.getElementById(this.containerName).getElementsByTagName('table')[0]) {
				this.container = document.getElementById(this.containerName).getElementsByTagName('table')[0];
				this.zoneSelectedCtl = document.getElementById(this.zoneSelectedCtlName);
				if (this.zoneSelectedCtl.value != '') 
					this.selectedZones = this.zoneSelectedCtl.value.split(',');
				var spans = this.container.getElementsByTagName('span');
				for (var i = 0; i < spans.length; i++) {
					var span = spans[i];
					var check = span.getElementsByTagName('input')[0];
					var id = span.attributes['id'].value;
					var idgroup = span.attributes['idgruppo'].value;
					if (id != idgroup) {
						// zone                        
						if (!this.groups[idgroup]) 
							this.groups[idgroup] = new Array();
						this.groups[idgroup].push(check);
						check.onclick = zone_click;
						span.onmouseover = this.onZoneOver;
						span.onmouseout = this.onZoneOut;
					}
					else {
						// group                        
						check.onclick = group_click;
						this.groupChecks[idgroup] = check;
					}
				}
			}
		}
	}
	
	this.selectZone = function(check){
		var span = check.parentNode;
		var id = span.attributes['id'].value;
		var idgroup = span.attributes['idgruppo'].value;
		if (!this.selectedZones.find(id)) 
			this.selectedZones.push(id);
		this.zoneSelectedCtl.value = this.selectedZones.join(',');
		check.checked = true;
		if (this.onSelectZone) 
			this.onSelectZone(id);
		if (this.isSelectedWholeGroup(idgroup)) {
			this.groupChecks[idgroup].checked = true;
			if (this.onSelectGroup) 
				this.onSelectGroup(idgroup);
			if (this.isAllGroupsSelected()) {
				this.selectAll();
			}
		}
	}
	
	this.unselectZone = function(check){
		var span = check.parentNode;
		var id = span.attributes['id'].value;
		var idgroup = span.attributes['idgruppo'].value;
		this.selectedZones.removeAll(id);
		this.zoneSelectedCtl.value = this.selectedZones.join(',');
		check.checked = false;
		this.groupChecks[idgroup].checked = false;
		if (this.onUnselectZone) 
			this.onUnselectZone(id);
		if (this.onUnselectGroup) 
			this.onUnselectGroup(idgroup);
	}
	
	this.isSelectedWholeGroup = function(idgroup){
		var group = this.groups[idgroup];
		for (var i = 0; i < group.length; i++) {
			if (!group[i].checked) 
				return false;
		}
		return true;
	}
	
	this.isAllGroupsSelected = function(){
		for (var idgroup in this.groupChecks) {
			if (!this.groupChecks[idgroup].checked) 
				return false;
		}
		return true;
	}
	
	this.selectGroup = function(check){
		var span = check.parentNode;
		var idgroup = span.attributes['idgruppo'].value;
		var group = this.groups[idgroup];
		if (group) {
			for (var i = 0; i < group.length; i++) {
				this.selectZone(group[i]);
			}
			if (this.isAllGroupsSelected()) {
				this.selectAll();
			}
			if (this.onSelectGroup) 
				this.onSelectGroup(idgroup)
		}
	}
	
	this.unselectGroup = function(check){
		var span = check.parentNode;
		var idgroup = span.attributes['idgruppo'].value;
		var group = this.groups[idgroup];
		for (var i = 0; i < group.length; i++) {
			this.unselectZone(group[i]);
		}
		if (this.onUnselectGroup) 
			this.onUnselectGroup(idgroup);
	}
	
	this.selectAll = function(){
		this.zoneSelectedCtl.value = "";
		if (this.onSelectAll) 
			this.onSelectAll();
	}
	
	this.unselectAll = function(){
		this.zoneSelectedCtl.value = "";
		this.selectedZones = new Array();
		if (this.onUnelectAll) 
			this.onUnelectAll();
	}
}
