//	*****	UTIL FUNCTIONS
/*
> removeAtt - remove um atributo [1. id do objeto, 2. atributo a ser removido]
> $$ - substitui o 'document.getElementById();' [1. id do objeto]
> toBlur - retira o foco de um objeto quando alcançado o número maximo de caracteres (maxlength), normalmente utilizado com o evento onkeyup [1. objeto , destino de foco]
> removeValue - remove um valor de um elemento deixando-o nulo [1. id]
> getCidade - resgata uma lista de cidades para um objeto select [1. código do estado , 2. id do objeto select cidade]
> getCidadeR - funçao de resposta a funçao getCidade, é responsável por inserir as cidades no objeto select
> windowOpen - abre uma nova janela [1. url , 2. nome da janela , 3. outros parametros ]
> newWindow - abre uma nova janela semelhante ao target _blank [1. url]
> onlyLower - determina apenas os caracteres aceitos no campo de login e senha (usar com evento 'onkeyup') [1. objeto input]
*/
function onlyLower(onlyLowerObj){
	var $obj = onlyLowerObj;
	var $not = /[^a-z|^0-9]/g;
	var $value = $obj.value;
	$obj.value = $value.replace($not,'');
	return null;
}
function removeAtt(setAttId,setAttType){
	var $id = document.getElementById(setAttId);
	var $type = setAttType;
	if($type=='class'){
		$id.removeAttribute('class');
		$id.removeAttribute('className');
	}else{
		$id.removeAttribute($type);
	}
}

function $$(id) {
	return document.getElementById(id);
}

function toBlur(toBlurObj,toBlurTo){
	var $obj = toBlurObj;
	var $value = $obj.value;
	var $limit = $obj.getAttribute('maxlength');
	var $to = document.getElementById(toBlurTo);
	if($value.length>=$limit){
		$to.focus();
	}
}

function removeValue(getId){
	var $element = document.getElementById(getId);
	$element.value = "";
}

function getCidade(getCidadeEstado,getCidadeId){
	var $estado = getCidadeEstado;
	if($estado=='-1'||$estado==''){
		return null;
	}else{
		getCidadeObj = document.getElementById(getCidadeId);
		getCidadeObj.setAttribute('disabled','disabled');
		while(getCidadeObj.hasChildNodes()){
			getCidadeObj.removeChild(getCidadeObj[0]);
		}
		var $dom = new Dom();
		var $loading = $dom.option('Carregando . . .','-1');
		getCidadeObj.appendChild($loading);
		
		getCidadeAjax = ajaxRequest();
		ajaxCon(getCidadeAjax,getCidadeR,ROOT_HTTP+'atributos/bibliotecas/ajaxPages/getCidade.php','codEstado='+$estado,'get');
	}
}

function getCidadeR(){
	var $dom = new Dom();
	if(getCidadeAjax.readyState==4){
		if(getCidadeAjax.status==200){
			var $docxml = getCidadeAjax.responseXML;
			if($docxml.hasChildNodes()){
				while(getCidadeObj.hasChildNodes()){
					getCidadeObj.removeChild(getCidadeObj[0]);
				}
				var $cidades = $docxml.getElementsByTagName('cidade');
				for(var $i=0;$i<$cidades.length;$i++){
					var $node = $cidades[$i];
					$option = $dom.option($node.childNodes[0].nodeValue,$node.getAttribute('value'));
					getCidadeObj.appendChild($option);
				}
				getCidadeObj.removeAttribute('disabled');
				getCidadeObj.focus();
			}else{
				while(getCidadeObj.hasChildNodes()){
					getCidadeObj.removeChild(getCidadeObj[0]);
				}
				var $error = $dom.option('Erro ao listar cidades.','-1');
				getCidadeObj.appendChild($error);
			}
		}else{
			while(getCidadeObj.hasChildNodes()){
				getCidadeObj.removeChild(getCidadeObj[0]);
			}
			var $error = $dom.option('Página nao encontrada, contate o adminstrador para mais informaçoes.','-1');
			getCidadeObj.appendChild($error);
		}
	}
}

function windowOpen(windowOpenURL, windowOpenName, windowOpenParam){
	var $url = windowOpenURL;
	var $name = windowOpenName;
	var $param = windowOpenParam;
	window.open($url,$name,$param);
}

function newWindow(newWindowURL){
	var $url = newWindowURL;
	window.open($url);
}
//	REDIRECIONAMENTO
function redirect(redirectFolder){
	var $folder = redirectFolder;
	self.location = ROOT_HTTP_ADMIN+$folder+'/';
}

//	ATRIBUI UM ESTILO
function setStyle(setStyleId,setStyleType,setStyleValue){
	var $obj = document.getElementById(setStyleId);
	var $type = setStyleType;
	var $value = setStyleValue;
	$obj.style[$type] = $value;
}

function informacao(informacaoId){
	var $obj = document.getElementById(informacaoId);
	var $h = $obj.style.height;
	if($h=='auto'){
		var $nH = '25px';
	}else{
		var $nH = 'auto';
	}
	$obj.style.height = $nH;
}

function setLoading(setLoadingObj){
	var $obj = setLoadingObj;
	$obj.innerHTML = '<img src="'+ROOT_HTTP+'atributos/imagens/global/loading.gif" />';
}
function hideShow(hideShowId){
	var $obj = document.getElementById(hideShowId);
	if($obj.style.display=='' || $obj.style.display=='none'){
		$obj.style.display = 'block';
	}else{
		$obj.style.display = 'none';
	}
}
function openInformativo(openInformativoLink,openInformativoId){
	var $link = openInformativoLink;
	var $img = $link.firstChild;
	var $path = $img.getAttribute('src');
	var $partName = $path.substr($path.length-5,1);
	var $partPath = $path.substr(0,$path.length-5);
	if($partName=='0'){
		$partPath+= '1.jpg';
	}else{
		$partPath+= '0.jpg';
	}
	$img.setAttribute('src',$partPath);
	hideShow(openInformativoId);
}
function disableButton(disableButtonForm){
	var $form = disableButtonForm;
	var $inputs = $form.getElementsByTagName('input');
	for(var $i=0;$i<$inputs.length;$i++){
		$input = $inputs[$i];
		$att = $input.getAttribute('type');
		if($att=='submit' || $att=='button' || $att=='reset'){
			$input.setAttribute('disabled','disabled');
		}
	}
}
function legendIcon(legendIconImg){
	var $obj = legendIconImg;
	var $cell = $obj.parentNode.parentNode;
	
	//	VERIFICANDO SE JA EXISTE UM TEXTBOX
	var $divs = $cell.getElementsByTagName('div');
	var $dom = new Dom();
	if($divs.length==0){
		var $div = $dom.div();
		$div.setAttribute('class','legendIcon');
		$div.setAttribute('className','legendIcon');
		var $text = $dom.text($obj.title);
		$div.appendChild($text);
		$cell.appendChild($div);
	}else{
		$divs[0].style.display = 'block';
		$divs[0].removeChild($divs[0].childNodes[0]);
		var $text = $dom.text($obj.title);
		$divs[0].appendChild($text);
	}
	
}

function legendIconHide(legendIconHideImg){
	var $obj = legendIconHideImg;
	var $cell = $obj.parentNode.parentNode;
	var $divs = $cell.getElementsByTagName('div');
	for(var $i=0;$i<$divs.length;$i++){
		$divs[$i].style.display = 'none';
	}
}

function disableEnableInput(disableEnableInputCheckbox,disableEnableInputId){
	var $checkbox = disableEnableInputCheckbox;
	var $obj = document.getElementById(disableEnableInputId);
	if($checkbox.checked===true){
		$obj.value = '';
		$obj.setAttribute('disabled','disabled');
	}else{
		$obj.removeAttribute('disabled');
	}
}

function redirectAction(redirectActionSelect){
	var $obj = redirectActionSelect;
	if($obj.value!='-1'){
		self.location = $obj.value;
	}
}

function getURL(getURLAddress){
	var $address = getURLAddress;
	var $compile = PHP_LINK===true ? '' : 'compile/';
	var $type = PHP_LINK===true ? 'php' : 'html';
	self.location = ROOT_HTTP+$compile+$address+'.'+$type;
}

function getParamURL(getParamURLDefault){
	var $default = getParamURLDefault;
	var $link = window.location;
	var $all = $link.search.substr($link.search.indexOf('?')+1);
	return $all=='' ? $default : $all.substr(6);
}

function resetInput(resetInputObj, resetInputClass){
	var $obj = resetInputObj;
	var $class = resetInputClass;
	var $value = $obj.value;
	var $default = $obj.defaultValue;
	if($value==$default){
		$obj.value = '';
	}else if($value==''){
		$obj.value= $default;
	}
	if($value==$default||$value==''){
		if($class!=''){
			$obj.setAttribute('class',$class);
			$obj.setAttribute('className',$class);
		}
	}
}

function addFavorite(addFavoriteLink /* link absoluto*/, addFavoriteTitle){
	var $link = addFavoriteLink;
	var $title = addFavoriteTitle;
	if(document.all){
		window.external.AddFavorite($link, $title);
	}else if(window.sidebar){
		window.sidebar.addPanel($title, $link, "");
	}
}
