/**
 * author: mson@idealistics.org 
 * USAGE:
 *
<script language="javascript">	
	var theArray = new Array();	
	// Add the master control
	theArray.push("select_all");
	// Add sub items   	
	theArray.push("item1");
	theArray.push("item2");
	theArray.push("item3");   	
</script>

<input type="checkbox" name="select_all" onclick="selectAll(theArray);">Select All
<input type="checkbox" name="item1" onclick="setMasterCheckBox(this.checked, theArray);">Item1<br>
<input type="checkbox" name="item2" onclick="setMasterCheckBox(this.checked, theArray);">Item2<br>
<input type="checkbox" name="item3" onclick="setMasterCheckBox(this.checked, theArray);">Item3<br>
 * 
 **/

/**
 * @author mson@idealistics.org
 * Select all the checkboxes are in the group. 
 * @param optionArray			the element names
 *								the first element is the master 
 *								control of all checkboxes
 **/
function selectAll(optionArray) {	
	// Loop through entire elements inside a form.				
	var checked = false; 
	for(var i=0;i<document.forms[0].elements.length;i++){
		myElement = document.forms[0].elements[i];
		if (myElement.type == "checkbox") {
			// is this a master control?  If so, set the checked value to all sub items.
			if (myElement.name == optionArray[0]) {
				checked = myElement.checked;
			}		
			else { 
				 if (isNameInArray(myElement.name, optionArray)) {
					myElement.checked = checked;
				 }
			} 				
		}					
	}					
}

/**
 * @author: mson@idealistics.org
 * Make sure to check the "Select All" if all checkboxes are checked or vice versa. 
 * @param checked			value of the checkbox when a user clicked.  
 * @param optionArray		all other elements belong to the group 
 *							if the param "checked" equals true then we want to check for all sub items. 				
 **/
function setMasterCheckBox(checked, optionArray) {
	// If one checkbox is not checked then set "Select All"
	// to uncheck, otherwise we need to loop through all				
	if (checked) {					
		for(var i=0; i<document.forms[0].elements.length; i++){
			myElement = document.forms[0].elements[i];
			if (myElement.type != "checkbox") { continue; }
			if (isNameInArray(myElement.name, optionArray)) {
				// At least one element is uncheck
				if (myElement.checked == false) { 							
					checked = false;
					break; // break the loop
				}
			}
		}					
	}
	setSelectAll(optionArray[0], checked);		
}	

/**
 * @author: mson@idealistics.org
 * Set the select all checkbox status.
 * @param name			the master group name
 * @param checked		the value can be true / false.
 **/
function setSelectAll(name, checked) {
	for(var i=0;i<document.forms[0].elements.length;i++) {				
		myElement = document.forms[0].elements[i];
		if (myElement.name == name) {
			myElement.checked = checked;
			return;
		}
	}
}

/**
 * @author: mson@idealistics.org
 * We want to know if the element name is in the array.
 * Since the Select All is the index of 0, we can begin with index at 1.
 * @param name			the name that we want to search for
 * @param optionArray	the name of all check boxes belong to the group 
 **/ 
function isNameInArray(name, optionArray) {								
	for(var i=1;i<optionArray.length;i++){
		if (name == optionArray[i]) {						
			return true; 
		}
	}
	return false;
}

