/**********************************************************************************/
////////////////////////////////////////////////////////////////////////////////////
//  RADIO BUTTON FUNCTIONS  ////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

//Recebe um array de radio buttons
//Retorna o índice do radio button ou null se não houver um selecionado
function selectedRadio(radioArray) {
	if (radioSize(radioArray) == 1) {
		if (radioArray.checked) {
			return 1;
		}
	} else {
		for(var i = 0;i < radioSize(radioArray) ; i++){
			if(radioArray[i].checked){
				return i;
				break;
			}
		}
	}
	return null;
}

//Recebe um array de radio buttons
//Retorna o valor do radio button selecionado ou null se não houver um selecionado
function selectedRadioValue(radioArray) {
	if (radioSize(radioArray) == 1) {
		if (radioArray.checked) {
			return radioArray.value
		}
	} else {
		for (var i = 0; i < radioSize(radioArray); i++){
			if (radioArray[i].checked) {
				return radioArray[i].value;
				break;
			}
		}
	}
	return null;
}

//Recebe um array de radio buttons e um array com valores, ambos obrigatoriamente com o mesmo tamanho
//Retorna o valor de um array (valueArray) do elemento de mesmo índice de um radio button selecionado (radioArray) ou null se não houver um radio selecionado ou os vetores não forem do mesmo tamanho
function selectedRadioFromValue(radioArray,valueArray){
	if(radioSize(radioArray) == radioSize(valueArray) && isSelected(radioArray)) {
		return valueArray[selectedRadio(radioArray)].value;
	} 
	return null;
}

//Recebe um array de radio buttons
//Seleciona o próximo radio button ou o primeiro se não houver um selecionado
function selectNextRadio(radioArray)
{
	var i = selectedRadio(radioArray);
	if(++i >= radioSize(radioArray)) i = 0;
	selectRadio(radioArray,i);
}

//Recebe um array de radio buttons
//Seleciona o radio button anterior ou o último se não houver um selecionado
function selectPreviousRadio(radioArray)
{
	var i = selectedRadio(radioArray);
	if(--i < 0) i = radioSize(radioArray) - 1;
	selectRadio(radioArray,i);
}

//Recebe um array de radio buttons e um inteiro
//Seleciona o radio button no índice 'i'
//Retorna false se não existir o radio button, senão retorna true
function selectRadio(radioArray,i){
	if(i < 0 || i >= radioSize(radioArray)) return false;
	if (radioSize(radioArray) == 1 && i == 1) {
		radioArray.checked = true;
		return true;
	} else {
		radioArray[i].checked = true;
		return true;
	}
	return false;
}

//Recebe um array de radio buttons
//Seleciona o radio button cujo valor é igual ao parâmetro value, nenhum radio se esse valor não existir
function selectRadioDefault(radioArray,value) {
	if(!selectRadioByValue(radioArray,value))
		unselectRadio(radioArray);
}

//Recebe um array de radio buttons
//Seleciona o radio button cujo valor é igual ao parâmetro value
function selectRadioByValue(radioArray,value) {
	var _radioButton = getRadioByValue(radioArray,value);

	if(_radioButton)
		return _radioButton.checked = true;

	return false;
}

//Recebe um array de radio buttons
//Retorna o radio button cujo valor é igual ao parâmetro value
function getRadioByValue(radioArray,value) {
	if(radioSize(radioArray) == 1)
		return radioArray.value == value ? radioArray : null;
	else
	{
		var _length = radioSize(radioArray);
		for(var i = 0;i < _length;i++)
			if(radioArray[i].value == value)
				return radioArray[i];
	}

	return null;
}

//Recebe um array de radio buttons
//De-seleciona todos os radio buttons do array
function unselectRadio(radioArray){
	if (radioSize(radioArray) == 1) {
		radioArray.checked = false;
	} else {
		for(var i = 0;i < radioSize(radioArray);i++){
			radioArray[i].checked = false;
		} 
	}
}

//Recebe um array de radio buttons
//Retorna true se há algum radio button selecionado, senão retorna false
function isSelected(radioArray)
{
	if (radioSize(radioArray) == 1) {
		if (radioArray.checked) {
			return true;
		}
	} else {
		for (var contador = 0; contador < radioSize(radioArray); contador++){
			if (radioArray[contador].checked) {
				return true;
				break
			}
		}
	}
	return false;
}

//Recebe um array de radio buttons
//Retorna o número de itens presentes no array de radio buttons
function radioSize(radioArray)
{
	if (isNaN(radioArray.length)){
		return 1;
	} else {
		return radioArray.length;
	}
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////
//  CHECK BOX FUNCTIONS  ///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

//Recebe um array de check boxes e um boolean
//Seta o estado de todos os check boxes para _toggleState
function toggleCheckArray(_checkBoxArray,_toggleState) {
	if(_checkBoxArray != undefined){
		if(_checkBoxArray.length == undefined)
			_checkBoxArray.checked = _toggleState;
		else
			for(var i = 0;i < _checkBoxArray.length;i++)
				_checkBoxArray[i].checked = _toggleState;
	}
}

//Recebe um array de check boxes
//habilita e desabilita todos os check boxes do array
function enableDisableCheckArray(_checkBoxArray,_toggleState) {
	if(_checkBoxArray != undefined){
		if(_checkBoxArray.length == undefined)
			_checkBoxArray.disabled = _toggleState;
		else
			for(var i = 0;i < _checkBoxArray.length;i++)
				_checkBoxArray[i].disabled = _toggleState;
	}
}

/*
function disableAllCheck(checkArray)
{
	if(checkArray!=undefined){
		if(checkArray.length==1 || checkArray.length== undefined){	
			checkArray.disabled = true;
		}else{
			for(var i = 0;i < checkArray.length;i++) checkArray[i].disabled = true;
		}
	}
}
*/


//Recebe um array de check boxes e um inteiro
//Seleciona o check box no índice 'i'
//Retorna false se não existir o check box, senão retorna true
function selectCheck(checkArray,i)
{
	if(i < 0 || i >= checkArray.length) return false;
	return checkArray[i].checked = true;
}

//Recebe um array de check boxes e um inteiro
//De-seleciona todos os check boxes do array
function unselectCheck(checkArray,i)
{
	if(checkArray!=undefined){
		if(i == null)
		{
			if(checkArray.length>1){
				for(var i = 0;i < checkArray.length;i++) checkArray[i].checked = false;
			}else{
				checkArray.checked = false;
			}
		}
		else
		{
			if(i < 0 || i >= checkArray.length) return false;
				return checkArray[i].checked = false;
		}
	}
}

//Recebe um array de check boxes
//seleciona todos os check boxes do array
function selectAllCheck(checkArray)
{
	if(checkArray!=undefined){
		if(checkArray.length==1 || checkArray.length== undefined){	
			checkArray.checked = true;
		}else{
			for(var i = 0;i < checkArray.length;i++) checkArray[i].checked = true;
		}
	}
}


//Recebe um array de check boxes
//Retorna a quantidade de check boxes selecionados
function nChecked(checkArray)
{
	var i,j = 0;
	if(checkArray==undefined){
		return 0;
	}
	
	if(checkArray.length ==1 || checkArray.length== undefined){
		if(checkArray.checked == true){
			j = 1;
		}
	}else{
		for(i = 0;i < checkArray.length;i++) if(checkArray[i].checked) j++;
	}
	return j;
}

//Recebe um array de check boxes
//Retorna o número de itens presentes no array de check boxes
function checkSize(checkArray)
{
	return checkArray.length;
}

//Recebe um array de check boxes
//Retorna true se todos os check boxes do array estão selecionados, senão retorna false
function isAllChecked(checkArray)
{
	for(var i = 0;i < checkArray.length;i++) if(!checkArray[i].checked) return false;
	return true;
}

//Recebe um array de check boxes
//Retorna true se todos os check boxes do array estão de-selecionados, senão retorna false
function isAllUnchecked(checkArray)
{
	for(var i = 0;i < checkArray.length;i++) if(checkArray[i].checked) return false;
	return true;
}

//Recebe um array de check boxes e um inteiro
//Retorna true se o check box no índice 'i' estiver selecionado, senão retorna false
//Se o check box não existir, também retorna false
function isChecked(checkArray,i)
{
	if(i < 0 || i >= checkArray.length) return false;
	return checkArray[i].checked == true;
}

//Recebe um array de check boxes e um inteiro
//Retorna o valor do check box no índice 'i'
function retornaValorChecked(checkArray,i){
	//var valor = 0;
	//alert('valor armazenado '+checkArray[i].value);
	//if(i < 0 || i >= checkArray.length){
		//valor =  checkArray[i].value;	
		
	//}	
	return checkArray[i].value;	
}
