function CartCheckBoxGroup() {
	this.controlBox=null;
	this.checkBoxes=new Array();
	this.totalBoxes=0;
	this.totalSelected=0;
	// Public methods
	this.setControlBox=CCBG_setControlBox;
	this.addGroupCheckboxes=CCBG_addGroupCheckboxes;
	this.setFormVal=CCBG_setFormVal;
	// Private methods
	this.cartSelection=CCBG_cartSelection;
}

// Set the master control checkbox name
function CCBG_setControlBox(name) { 
	var controlBoxList = document.getElementsByName(name);
	if (controlBoxList.length != 1) {
		//alert("There should only be one control element, instead there are: " + controlBoxList.length);
	} else if (controlBoxList[0].type != "checkbox") {
		//alert("The control element should be a checkbox, instead it is: " + controlBoxList[0].type);
	} else {
		this.controlBox=controlBoxList[0];	
	} 
}

function addFormInput(formID, paramVal, paramName) {
    var formRef = document.getElementById(formID);
	if (formRef != null && paramName != null && paramVal != null) {
		var inp = document.createElement("input");
		inp.type = "hidden";
		inp.name = paramName;
		inp.value = paramVal;
		formRef.appendChild(inp);	
	} else {
		//alert("Null value! Product not passed to cart.");
	}
}


function CCBG_setFormVal(formID, paramName, paramName2, paramName3, cartArray) {
    //alert("CartArray - " + cartArray);
	var newParams = function addInput() {
						if (paramVal != null) {
							var inp = document.createElement("input");
							inp.type = "hidden";
							inp.name = paramName;
							inp.value = paramVal;
							formRef.appendChild(inp);	
						} else {
							//alert("Null value! Product not passed to cart.");
						}
					}

	var formRef = document.getElementById(formID);
	var paramVal = null;
	if (formRef != null) {
		if (this.controlBox != null && this.controlBox.checked == true) {
			paramVal = this.controlBox.value;
			this.controlBox.checked = false;
			newParams();
			paramName=paramName2;
			for(i=0; i<cartArray.length; i++) {
			     //alert("CartArray["+ i + "] - " + cartArray[i]);
			     paramVal = cartArray[i];
			     newParams();
			}
			paramName=paramName3;
			paramVal=cartArray.length;
			newParams();
		} else {
			for(i=0; i<this.checkBoxes.length; i++) {
				if (this.checkBoxes[i].checked == true) {
					paramVal = this.checkBoxes[i].value;
					this.checkBoxes[i].checked = false;
					newParams();
				}
			}
		}
	} 
}


function CCBG_cartSelection(obj) {
	if (this.controlBox != null && obj.name == this.controlBox.name) {
		for(i=0; i<this.checkBoxes.length; i++) {
			this.checkBoxes[i].checked = false;
		}
		this.totalSelected=1;
	} else {
		if (this.controlBox != null && this.controlBox.checked == true) {
				this.controlBox.checked=false;
				this.totalSelected--;
		}
		if (obj.checked == false) {
			this.totalSelected--;
		} else {
			this.totalSelected++;
		}
	}
}


// Add checkboxes to the group which all have the same name
function CCBG_addGroupCheckboxes(name) {
	this.checkBoxes = document.getElementsByName(name);
}

function CCBG_nameIsArray(obj) {
	return ((typeof obj.type!="string")&&(obj.length>0)&&(obj[0]!=null)&&(obj[0].type=="checkbox"));
}
