/**
* Script contains common check-box functions that can handle
* the onclick of selectAll and other common checkbox functions.
*
* DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING!
*/

/**
 * Creates a class called CheckBoxUtils.
 */
var CheckBoxUtils = {
    version : "1.0.0"
};




/**
 * This function will handle the select and de-select ALL of all children
 * Example call: onClickSelectAll("frmMain", "isAllSelected", "chkChild");
 * NOTE: Implement method _handle_onClickSelectAll() on your JSP if you wish to implement additional
 * functionality after this method completes it's tasks.
 *  
 * @param formName The String nwl:form name that contains the elements you will be accessing
 * @param strChkIsAllSelectedName The String checkbox-select-all-name that was clicked-on.
 * @param strChkChildName The String checkbox-name of all the checkbox children
 */
CheckBoxUtils.onClickSelectAll = function(formName, strChkIsAllSelectedName, strChkChildName) {

    var objSelectAlLCheckBox = document.forms[formName].elements[strChkIsAllSelectedName];
	var objIsAllSelected = document.forms[formName].elements[strChkIsAllSelectedName];

    // Select/unselect all check boxes.
    var objChkChildren = document.forms[formName].elements[strChkChildName];


  	if ( objChkChildren !== null ) {
    	if  ( objChkChildren.length !== undefined ) {
      		for (var i=0; i < objChkChildren.length;i=i+1) {
      			objChkChildren[i].checked = objIsAllSelected.checked;
      		}
    	} else {
			objChkChildren.checked = objIsAllSelected.checked;
    	}
  	}

    // All pages that implement this [checkbox.js] should define this function on their JSP's
    // if they wish to do something after this method has been performed.
    if (window.CheckBoxUtils_handleOnClickSelectAll  != undefined) {
        CheckBoxUtils_handleOnClickSelectAll(objSelectAlLCheckBox, objIsAllSelected, objChkChildren);
    }

}

/**
 * This function is called when any of the child checkboxes are clicked upon. Implement any child checkbox's onclick
 * event to capture the click event.
 *
 * @param formName The String nwl:form name that contains the elements you will be accessing
 * @param chkChildObj The actual checkbox object reference that has just been clicked on by the user.
 * @param strChkIsAllSelectedName The String checkbox-select-all-name that was clicked-on.
 */
CheckBoxUtils.onClickChild = function(formName, chkChildObj, strChkIsAllSelectedName) {
    var objSelectAlLCheckBox = document.forms[formName].elements[strChkIsAllSelectedName];

    if (chkChildObj.checked === false) {
        objSelectAlLCheckBox.checked = false;
    }

    // All pages that implement this [checkbox.js] should define this function on their JSP's
    // if they wish to do something after this method has been performed.
    if (window.CheckBoxUtils_handleOnClickChild  != undefined) {
        CheckBoxUtils_handleOnClickChild(chkChildObj, objSelectAlLCheckBox);
    }

}

/**
 * Checks the incoming checkbox element for any checkboxes that may or may not be checked. Return <code>true</code>
 * if at least one child-element is checked; otherwise return <code>false</code>
 *
 * @param objChkChildren The reference to the checkbox-object-element to see if any of the children are currently checked.
  */
CheckBoxUtils.isAnyCheckBoxesSelected = function(objChkChildren) {
	if ( objChkChildren !== null ) {

    	if  ( objChkChildren.length != null ) {

      		for (var i=0; i < objChkChildren.length;i++) {
  				if (objChkChildren[i].checked) {
   					return true;
   				}
      		}
    	} else if ( objChkChildren.checked != null ) {
			if (objChkChildren.checked) {
   				return true;
   			}
    	}
  	}

	return false;
}

/**
 * Builds a comma-delimited list of each checkbox's values if they are currently (checked).
 *
 * @param objChkChildren The reference to the checkbox-object-element to gather all of it's values if they have been checked.
 */
CheckBoxUtils.getSelectedCheckBoxValues = function(objChkChildren) {
    var list = "";

	if ( objChkChildren !== null ) {
    	if  ( objChkChildren.length != null ) {
      		for (var i=0; i < objChkChildren.length;i++) {
  				if (objChkChildren[i].checked) {
   					list += objChkChildren[i].value + ",";
   				}
      		}
    	} else if ( objChkChildren.checked != null ) {
			if (objChkChildren.checked) {
   				list += objChkChildren.value + ",";
   			}
    	}
  	}

    return list;
}

/**
 * Unchecks all the checkboxes for the <code>objChkChildren</code>.
 *
 * @param objChkChildren Reference to the checkbox that may have 1-to-many children.
 */
CheckBoxUtils.deSelectAllCheckBoxes = function(objChkChildren) {
  	if ( objChkChildren !== null ) {
    	if  ( objChkChildren.length !== undefined ) {
      		for (var i=0; i < objChkChildren.length;i=i+1) {
      			objChkChildren[i].checked = false;
      		}
    	} else {
			objChkChildren.checked = false;
    	}
  	}
}

