
//===============================================================
// Funções Básicas

// Line Feed
CRLF = String.fromCharCode(13) + String.fromCharCode(10) ;

/**
 * author marcelo norio
 * Retorna o objeto pelo nome ou Id.
 */
	function getObj(obj){
		if( typeof(obj)!='object' ){
			try{
				oRetorno = document.getElementById(obj).id;
				oRetorno = document.getElementById(obj);
				if( typeof(oRetorno)!='object' ) throw obj;
			}catch(e){
				oRetorno=document.getElementsByName(obj)[0];
			}
		}
		return oRetorno;
	}

/**
 * author marcelo norio
 * Obtem valor de um objeto, seja ele text ou checkBox.
 * @PARAM obj String ID  ou String name ou Object
 * @TODO Falta obter o valor do TD
 */
	function getValue(obj){
		oTmp = obj;
		if(typeof(oTmp)!='object'){
			try{oTmp = document.getElementById(obj).id;
				// eh um <TD>
				if( document.getElementById(obj).tagName=="TD" ){
					TD_conteudo_ = document.getElementById(obj).innerText ; // IE
					if( TD_conteudo_ == null ){ // FF
						TD_conteudo_ = document.getElementById(obj).textContent;
					}
					if( TD_conteudo_ != null ){
						return TD_conteudo_;
					}
				}
				oTmp = document.getElementById(oTmp);
				if( typeof( oTmp )!='object' ) throw obj;
			}catch(e){
				oTmp = document.getElementsByName(obj)[0];
			}
		}
		obj = oTmp;
	
		switch (obj.type){
			case 'checkbox':{
				return obj.checked ? 1 : '';
				break;
			}case 'radio':{
				rd = document.getElementsByName(obj.name);
				for(i=0;i<rd.length;i++){
					if(rd[i].checked==true) return rd[i].value;
				}
				return '';
				break;
			}default:{
				return obj.value;
			}
		}
		
	}
	

// Marca/DES-Marca todos os checkBox
function setAllCheckbox(name, true_false){
	allCheck = document.getElementsByName(name);
	for(i=0 ; i < allCheck.length; i++){
		allCheck[i].checked = true_false ;
	}
}



/*************************************************
 * Retira os espacos em branco no final e no inicio
 *************************************************/
function ltrim(a){
	if(a.substr(0,1)==' '){
		return ltrim(a.substr(1));
	}
	return a;
}
function rtrim(a){
	if( a.substr( (a.length-1) , 1 ) ==' ' || a.substr( (a.length-1) , 1 ) == "	" ){ // retira espaço em branco e "Enter"
		return rtrim(a.substr(0,(a.length-1)));
	}
	return a;
}
function trim(a){
	return rtrim(ltrim(a));
}
//===============================================================	


//===============================================================	
/*
 * Seleciona todos os checkbox
 * Marca ou DES-Marca todos os checkBox epal inicial
 * @param inicial String inicial de identificacao
 */
function selecionaTodosCheckBoxes(inicial, true_false){
	if( inicial.length < 1) return;		
	tipo = "input";	
	var _arrayObj = new Array();
	var _arrayObj =retornaTipoPelaInicial(inicial, tipo);
				
	for(var i=0 ; i < _arrayObj.length ; i++){			
		_arrayObj[i].checked = true_false;
	}	
}


	
/*
 * Verifica se o controle <SELECT> tem <option  id='obj_opt_'> 
 * onde obj_opt_ é o exemplo de inicial de identificacao
 * @param inicial String inicial de identificacao
 * @param tipo String tag do controle html. <option>, <input> sem blaquets <>
 * @return quantidade de objetos que tem as iniciais Integer
 */
function qtdTipoPelaInicial(inicial, tipo){
	return retornaTipoPelaInicial(inicial, tipo).length;
}


/*
 * Obtem do documento todos os objetos que tem a inicial com o nome dado ex: 
 * <tagTipo id='obj_opt_1' /> <tagTipo id='obj_opt_2' /> 
 * onde obj_opt_ é a inicial de identificacao
 * @param inicial String inicial de identificacao
 * @param tipo String tag do controle html. <option>, <input> etc sem blaquets <>
 * @return Array Objetos
 */
function retornaTipoPelaInicial(inicial, tipo){
	var _arrayObj = new Array();
	var qtdObjetosRetorno = 0;
	if( tipo == null ){//tipo invalido
		return false;
	}
	if( inicial.length < 1 ) { //inicial invalida
		return false;
	}
	
	tipo = tipo.toUpperCase();
	var _todosDoTipo = document.getElementsByTagName(tipo);	// Obtem todos os controles que tem o mesmo tipo

	for(var i=0 ; i < _todosDoTipo.length ; i++){					
		if( _todosDoTipo[i].id.substring( 0, inicial.length ) == inicial ){ // somente os que tem a inicial dada
		_arrayObj[qtdObjetosRetorno] = _todosDoTipo[i];
		qtdObjetosRetorno++ ;			
		}
	}		
	return _arrayObj;
}
