// CheckBoxGroup allows a master checkbox to be set that when clicked, will check other boxes in the group.
// Group is specified by all checkboxes having the same class name, and this is set on the CheckBoxGroup object.

// Code adapted by Amy from http://www.mattkruse.com/javascript/checkboxgroup/
// Original code author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/

function CheckBoxGroup() {
		this.controlBox = null;
		this.checkboxes = new Array();
		this.formRef = null;
		this.masterBehaviour = null;
		this.totalSelected = 0;
		this.setControlBox = CBG_setControlBox;
		this.setChkClassName = CBG_setChkClassName;
		this.setFormRef = CBG_setFormRef;
		this.getCheckboxes = CBG_getCheckboxes;
		this.check = CBG_check;
}

function CBG_setControlBox(name) { this.controlBox = name; }
function CBG_setChkClassName(ccn) { this.chkClassName = ccn; }
function CBG_setFormRef(formId) { this.formRef = document.getElementById(formId); }

function CBG_getCheckboxes() {
	this.checkboxes = document.getElementsByClassName(this.chkClassName);
}

function CBG_check(obj) {
var checked=obj.checked;
	//determine whether control box or group box was clicked
	if(this.controlBox==obj.name) {
		for(i=0; i<this.checkboxes.length; i++){
			this.checkboxes[i].checked=checked;
		}
			this.totalSelected=(checked)?this.checkboxes.length:0;
	}
	else {
		if(checked==true) { 
			this.totalSelected++;
			if(this.totalSelected==this.checkboxes.length) { this.formRef[this.controlBox].checked=true; }
		}	
		else { 
			this.totalSelected--;
			this.formRef[this.controlBox].checked=false;
		}	
	}		
}

