
var selectors = new Array();

function addSelector(name,idString){
	selectors[selectors.length] = new Selector(name,idString);
}

function getSelectorIndex(name){
	for(var i=0;i<selectors.length;i++){
		if(selectors[i].groupName==name){
			return i;
		}
	}
	return -1;
}

function toggleAll(name){
	if(document.getElementById("cb_"+name).checked){
		selectAll(name);
	}else{
		unselectAll(name);
	}
}

function toggleSelected(name,selectedId){
	var ndx = getSelectorIndex(name);
	if(ndx >= 0){
		selectors[ndx].toggleSelected(selectedId);
	}
}

function getSelected(name){
	var ndx = getSelectorIndex(name);
	if(ndx >= 0){
		return selectors[ndx].getSelected();
	}
	return "";
}

function getSelectedCount(name){
	var ndx = getSelectorIndex(name);
	if(ndx >= 0){
		return selectors[ndx].getSelectedCount();
	}
	return 0;
}

function unselectAll(name){
	var ndx = getSelectorIndex(name);
	if(ndx >= 0){
		selectors[ndx].unselectAll();
	}
}

function selectAll(name){
	var ndx = getSelectorIndex(name);
	if(ndx >= 0){
		selectors[ndx].selectAll();
	}
}

function Selector(name,idString){
	
	this.groupName = name;
	this.possibleIds = idString;
	
	this.selectedArray = new Array();
	this.selectedCount = 0;
	
	this.toggleSelected = toggleSelected;
	
	function toggleSelected(selectedId){
		var alreadyAdded = false;
		for(var i=0;i<this.selectedCount;i++){
			if(this.selectedArray[i]==selectedId){
				alreadyAdded = true;
				while(i < this.selectedCount-1){
					this.selectedArray[i] = this.selectedArray[i+1];
					i++;
				}
			}
		}
		if(alreadyAdded){
			this.selectedCount--;
		}else{
			this.selectedArray[this.selectedCount++] = selectedId;
		}
	}
	
	this.getSelected = getSelected;
	
	function getSelected(){
		var str = "";
		for(var i=0;i<this.selectedCount;i++){
			str += this.selectedArray[i];
			if(i+1<this.selectedCount){
				str += ",";
			}
		}
		return str;
	}

	this.getSelectedCount = getSelectedCount;
	
	function getSelectedCount(){
		return this.selectedCount;
	}
	
	this.unselectAll = unselectAll;
	
	function unselectAll(){
		for(var i=0;i<this.selectedCount;i++){
			document.getElementById("cb_"+this.groupName+"_"+this.selectedArray[i]).checked = false;
		}
		this.selectedArray = new Array();
		this.selectedCount = 0;
	}
	
	this.selectAll = selectAll;
	
	function selectAll(){
		if(this.possibleIds.length!=""){
			this.selectedArray = this.possibleIds.split(",");
			this.selectedCount = this.selectedArray.length;
			for(var i=0;i<this.selectedCount;i++){
				document.getElementById("cb_"+this.groupName+"_"+this.selectedArray[i]).checked = true;
			}
		}
	}
	
}