function getSelectedCheckbox(buttonGroup)
{
    // Go through all the check boxes. return an array of all the ones
    // that are selected (their position numbers). if no boxes were checked,
    // returned array will be empty (length will be zero)
    var retArr = new Array();
    var lastElement = 0;
    if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
        for (var i = 0; i < buttonGroup . length; i++) {
            if (buttonGroup[i] . checked) {
                retArr . length = lastElement;
                retArr[lastElement] = i;
                lastElement++;
            }
        }
    } else { // There is only one check box (it's not an array)
        if (buttonGroup . checked) { // if the one check box is checked
            retArr . length = lastElement;
            retArr[lastElement] = 0; // return zero as the only array value
        }
    }
    return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup)
{
    // return an array of values selected in the check box group. if no boxes
    // were checked, returned array will be empty (length will be zero)
    var retArr = new Array(); // set up empty array for the return values
    var selectedItems = getSelectedCheckbox(buttonGroup);
    if (selectedItems . length != 0) { // if there was something selected
        retArr . length = selectedItems . length;
        for (var i = 0; i < selectedItems . length; i++) {
            if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
                retArr[i] = buttonGroup[selectedItems[i]] . value;
            } else { // It's not an array (there's just one check box and it's selected)
                retArr[i] = buttonGroup . value; // return that value
            }
        }
    }
    return retArr;
} // Ends the "getSelectedCheckBoxValue" function

/**
 * returnIndexCheckbox()
 * 
 * Retorna o índice do elemento selecionado de um objeto checkbox.
 * 
 * @author Luis Fernando Gomes
 * @copyright 07/2010
 * @version 1.0
 * 
 * Histórico:
 * 
 * Data			| Versão | Modificação
 * ------------------------------------------------------------------------------------------------
 * 				|   	 | ------------------------------------------------------------------------
 * ------------------------------------------------------------------------------------------------
 *  
 * @param object objCheck				Objeto input checkbox que contém os options.
 * @return
 */
function returnIndexCheckbox(objCheck)
{
    var objCheck = typeof(objCheck) != 'undefined' ? objCheck : document . createElement('INPUT');

    var myOption = -1;
    if (objCheck . length && objCheck . length > 0) {
        alert('len : ' + objCheck . length);
        for (var i = objCheck . length - 1; i > -1; i--) {
            if (objCheck[i] . checked) {
                myOption = i;
                break;
            }
        }
    } else {
        if (objCheckbox . checked) {
            myOption = 1;
        }
    }
    return myOption;
}

/**
 * returnValueCheckbox()
 * 
 * Retorna o valor do elemento selecionado de um objeto checkbox.
 * 
 * @author Luis Fernando Gomes
 * @copyright 07/2010
 * @version 1.0
 * 
 * Histórico:
 * 
 * Data			| Versão | Modificação
 * ------------------------------------------------------------------------------------------------
 * 				|   	 | ------------------------------------------------------------------------
 * ------------------------------------------------------------------------------------------------
 *  
 * @param object objCheck				Objeto input checkbox que contém os options.
 * @return
 
function returnValueCheckbox(objCheck)
{
	var i = -1;
	var value = '';
	var objCheck = typeof(objCheck) != 'undefined' ? objCheck : document . createElement('INPUT');

	i = returnIndexCheckbox(objCheck);
	if (i != -1) {
        if (objCheck . length && objCheckbox . length > 0) {
            value = objCheck[i] . value;
        } else {
            value = objCheck . value;
        }
  	}
	return value;
}
*/

function returnCheckedCheckbox(objCheck)
{
    var objCheck = typeof(objCheck) != 'undefined' ? objCheck : document . createElement('INPUT');

    var arChecked = new Array();

    objsCheck = new Array();
    if (typeof(objCheck) == 'string') {
        objsCheck = document . getElementsByName(objCheck);
    } else {
        if (typeof(objCheck) == 'object') {
            objsCheck[0] = objCheck;
        }
    }

    if (objsCheck . length && objsCheck . length > 0) {
        if (objsCheck . length > 1) {
            for (var i = objsCheck . length - 1; i > -1; i--) {
                var objCheck = objsCheck[i];
                if (objCheck . checked) {
                    arChecked . push(objCheck);
        	    }
            }
        } else {
            return objCheck[objsCheck . length];
        }
    }

    return arChecked;
}

function returnValueCheckbox(objCheck)
{
    var objCheck = typeof(objCheck) != 'undefined' ? objCheck : document . createElement('INPUT');

    var arChecked = new Array();

    objsCheck = new Array();
    if (typeof(objCheck) == 'string') {
        objsCheck = document . getElementsByName(objCheck);
    } else {
        if (typeof(objCheck) == 'object') {
            objsCheck[0] = objCheck;
        }
    }

    if (objsCheck . length && objsCheck . length > 0) {
        if (objsCheck . length > 1) {
            for (var i = objsCheck . length - 1; i > -1; i--) {
                var objCheck = objsCheck[i];
                if (objCheck . checked) {
                    arChecked . push(objCheck . value);
        	    }
            }
        } else {
            return objCheck[objsCheck . length] . value;
        }
    }

    return arChecked;
}

/**
 * setEventCheckBox()
 * 
 * Adiciona um evento a um objeto checkbox.
 * 
 * @author Luis Fernando Gomes
 * @copyright 07/2010
 * @version 1.0
 * 
 * Histórico:
 * 
 * Data			| Versão | Modificação
 * ------------------------------------------------------------------------------------------------
 * 				|   	 | ------------------------------------------------------------------------
 * ------------------------------------------------------------------------------------------------
 *  
 * @param object objCheck				Objeto input checkbox que contém os options.
 * @return
 */
function setEventCheckBox(objCheck, type, fn)
{
    var objCheck = typeof(objCheck) != 'undefined' ? objCheck : document . createElement('INPUT');
    var type = typeof(type) != 'undefined' ? type : 'click';
    var fn = typeof(fn) != 'undefined' ? fn : function(){};

    objsCheck = new Array();
    if (typeof(objCheck) == 'string') {
        objsCheck = document . getElementsByName(objCheck);
    } else {
        if (typeof(objCheck) == 'object') {
            objsCheck[0] = objCheck;
        }
    }

    if (objsCheck . length && objsCheck . length > 0) {
        for (var i = objsCheck . length - 1; i > -1; i--) {
            var obj = objsCheck[i];
            if (obj . attachEvent) {
    	        obj['e' + type + fn] = fn;
    	        obj[type + fn] = function ()
    	        {
    	            obj['e' + type + fn](window . event);
    	        }
    	        obj . attachEvent('on' + type, obj[type + fn]);
    	    } else {
    	    	obj . addEventListener(type, fn, false);
    	    }
        }
    }
}

// ##################### 1 #####################

/**
 * CheckAll()
 * 
 * Seleciona todos os checkbox do formulário.
 * 
 * @author Luis Fernando Gomes
 * @copyright 11/2009
 * @version 1.0
 * 
 * Histórico:
 * 
 * Data			| Versão | Modificação
 * ------------------------------------------------------------------------------------------------
 * 				|   	 | ------------------------------------------------------------------------
 * ------------------------------------------------------------------------------------------------
 * 
 * Ex:
 * ##################### xhtml #####################
 * <input type="checkbox" name="selAll" id="selAll" /><span id="checkar">Marcar todos</span>
 * <input type="checkbox" name="locais[]" value="" />
 * <input type="checkbox" name="locais[]" value="" />
 * <input type="checkbox" name="locais[]" value="" />
 * 
 * ################### javascript ##################
 * objForm = document . getElementById('cadastro');
 * objSelAll = document . getElementById('selAll');
 * objSpanCheckar = document . getElementById('checkar');
 * 
 * objSelAll . onclick = function()
 * {
 *		CheckAll(objForm, objSelAll, objSpanCheckar);
 * }
 *  
 * @param object objForm				Objeto formulário que contém os checkbox.
 * @param object objSelect				Objeto input checkbox que define a seleção dos outros checkbox.
 * @param object objSpan				Objeto span que exibe o texto "Marcar todos" ou "Desmarcar todos".
 * @return
 */
function CheckAll(objForm, objCheck, objSpan)
{
	objForm = typeof(objForm) != 'undefined' ? objForm : null;
	objSelAll = typeof(objCheck) != 'undefined' ? objCheck : null;
	objSpanCheckar = typeof(objSpan) != 'undefined' ? objSpan : null;

	for (var i = 0; i < objForm . elements . length; i++) {
        var x = objForm . elements[i];
        if((x . type == 'checkbox') && (x . id != objSelAll . id)) {
			x . checked = objSelAll . checked;
      	}
    }

	if (!objSelAll . checked) {
 		objSpanCheckar . innerHTML = 'Marcar todos';
        objSelAll . title = 'Marcar todos';
 	} else {
 		objSpanCheckar . innerHTML = 'Desmarcar todos';
        objSelAll . title = 'Desmarcar todos';
 	}
}

// ##################### 2 #####################

function selecionarTodosCheckboxPorId(qtd)
{
    for (i = 0; i < qtd; i++) {
        if (!document . getElementById(i) . checked) {
            document . getElementById(i) . checked = 1;
        }
    }
}

function deselecionarTodosCheckboxPorId(qtd)
{
    for (i = 0; i < qtd; i++) {
        if (document . getElementById(i) . checked) {
            document . getElementById(i) . checked = 0;
        }
    }
}

// ##################### 3 #####################

function selecionarTodosCheckbox()
{
    for (i = 0; i < document . cadastro . elements . length; i++) {
        if (document . cadastro . elements[i] . type == 'checkbox') {
            document . cadastro . elements[i] . checked = 1;
        }
    }
}

function deselecionarTodosCheckbox()
{
    for (i = 0; i < document . cadastro . elements . length; i++) {
        if (document . cadastro . elements[i] . type == 'checkbox') {
            document . cadastro . elements[i] . checked = 0;
        }
    }
}