/**
Contains all utiltites such as form managements.  
This should never contain code specific to our interfaces, behaviors or brands.
*/
function checkChildBoxes(parentCheckboxName, childCheckboxClassName, checkValue){
	/* When parentCheckbox is checked/unchecked, find all of its childCheckboxes and check/uncheck them also */
	/* ChildCheckboxes have class= the value of parentCheckboxName and an id that begins with the value of parentName  i.e.: Dallas12345 */
	if(checkValue){
		$jq("input:checkbox[class='"+ childCheckboxClassName +"'][id^='"+ parentCheckboxName +"']").each(function(){this.checked=true;});
	}else{
		$jq("input:checkbox[class='"+ childCheckboxClassName +"'][id^='"+ parentCheckboxName +"']").each(function(){this.checked=false;});
	}
}

function checkParentBox(parentCheckboxName, childCheckboxClassName, checkValue){
	/* ParentCheckbox should have an id of the value of parentCheckboxName + "ParentCheckbox"  i.e.: DallasParentCheckbox */
	/* ChildCheckboxes have class= the value of parentCheckboxName and an id that begins with the value of parentName  i.e.: Dallas12345 */
	var parentCheckboxId = parentCheckboxName + 'ParentCheckbox';
	if(!checkValue){
		/* When childCheckbox is unchecked, find its parentCheckbox and uncheck it because all childCheckboxes are no longer checked */
		document.getElementById(parentCheckboxId).checked=false;
	
	}else if(checkValue){
		/* When childCheckbox is checked, find all of its sibling checkboxes and check their values. */
		/* If all the sibling checkboxes have been checked, find the parentCheckbox and check it also */
		if(isAllChildCheckboxesChecked(parentCheckboxName, childCheckboxClassName)){
			document.getElementById(parentCheckboxId).checked=true;
		}
	}
	
}

function isAllChildCheckboxesChecked(parentCheckboxName, childCheckboxClassName){
	/* Find all of the childCheckboxes of the given parentCheckbox and check their values. */
	/* If all the childCheckboxes checkboxes have been checked, return "true" */
	var allChildCheckboxesChecked = true;
	$jq("input:checkbox[class='"+ childCheckboxClassName +"'][id^='"+ parentCheckboxName +"']").each(function(){
		if(!(this.checked)){
			allChildCheckboxesChecked = false;
		}
	});
	return allChildCheckboxesChecked;
}

function precheckAllParentBoxes(parentIdEndsWith, childCheckboxClassName){
	/* Find all parent checkboxes - their id should end with the value of parentIdEndsWith */
	$jq("input:checkbox[id$='"+parentIdEndsWith+"']").each(function(){
		var parentCheckboxId = this.id;
		
		/*Need to split out the unique part of the parent name -- same part that is used in each childbox id */
		var splitResult = parentCheckboxId.split(parentIdEndsWith);
		var parentIdStartsWith = splitResult[0];
		
		/* For each parent checkbox, first check to see if they have childCheckboxes */
		/* All childCheckboxes should have the same class name and their id should begin with a common reference to the parentCheckbox */
		var childCheckboxes = $jq("input:checkbox[class='"+childCheckboxClassName+"'][id^='"+ parentIdStartsWith +"']");
		
		if(childCheckboxes.length > 0){
		/*check to see if all of their childCheckboxes are selected, if true select the parentCheckbox also, otherwise uncheck the parent */
			if(isAllChildCheckboxesChecked(parentIdStartsWith, childCheckboxClassName)){
				document.getElementById(parentCheckboxId).checked=true;
			}else{
				document.getElementById(parentCheckboxId).checked=false;
			}
		}
	});
}

function checkAll(checkboxName, checkValue){
	/* Find all the checkboxes with the given checkboxName and set their value to the checkValue */
	if(checkValue){
		$jq("input:checkbox[name='" + checkboxName +"']").each(function(){this.checked=true;});
	}else if(!checkValue){
		$jq("input:checkbox[name='" + checkboxName +"']").each(function(){this.checked=false;});
	}
}


var previousToggleValue = false;
function toggleCheckAll(checkboxName){
	/* Find all the checkboxes with the given checkboxName and set their value to the opposite value */
	if(previousToggleValue){
		$jq("input:checkbox[name='" + checkboxName +"']").each(function(){this.checked=false;});
		previousToggleValue = false;
	}else if(!previousToggleValue){
		$jq("input:checkbox[name='" + checkboxName +"']").each(function(){this.checked=true;});
		previousToggleValue = true;
	}

}

