﻿function checkTotalSelected(selectedCheckBox, fieldSetContainer, totalAllowedChecked){
	totalChecked=0;
	checkBoxToUncheck="";
	childDivs = fieldSetContainer.childNodes
	for (var j=0;j<childDivs.length;j++){
		if ((childDivs[j].nodeType==1) && (childDivs[j].childNodes[0].childNodes[0].childNodes[0].checked)){ 
			totalChecked++;
			if (totalChecked==1) checkBoxToUncheck=childDivs[j].childNodes[0].childNodes[0].childNodes[0]
		}
	}
	if (totalChecked>totalAllowedChecked) {
		//if the checkbox we are about to uncheck is the one the user just checked, we must find the next box selected and uncheck it instead
		if (checkBoxToUncheck==selectedCheckBox){
			for (j=0;j<childDivs.length;j++){
				if ((childDivs[j].nodeType==1) && (childDivs[j].childNodes[0].childNodes[0].childNodes[0].checked) && (childDivs[j].childNodes[0].childNodes[0].childNodes[0]!=selectedCheckBox)) 
					checkBoxToUncheck=childDivs[j].childNodes[0].childNodes[0].childNodes[0];
			}
		}
		checkBoxToUncheck.parentNode.className="off";
		checkBoxToUncheck.checked=false;
	}
}

function changeCheck(nodeToChange){
	currentClass=nodeToChange.className;
	if ((nodeToChange.className=="on") || (nodeToChange.className=="on")){
		nodeToChange.className=currentClass.replace(/on/, "off");
	}	
	else{
		nodeToChange.className=currentClass.replace(/off/, "on");
	}
	
}
function checkUpKey(chk, n, index){
	if(chk) {
		changeCheck(n);
	}
	else {
		changeCheck(n);	
	}
}

function circleChecks(){
	pageInputs = document.getElementsByTagName("input");
	for(currentInput=0;currentInput<pageInputs.length;currentInput++){
		if ((pageInputs[currentInput].type=="checkbox") && (pageInputs[currentInput].checked)){
			pageInputs[currentInput].parentNode.className="on";
		}
	}
}

function initialize(){
	circleChecks();
}

window.onload=initialize;
