	var tdAnterior = ""; 
	function contarChecados(nomeCampo){
		var	arrayCodigoAgenda = document.getElementsByName(nomeCampo);
		var j=0;
		for (var i=0;i<arrayCodigoAgenda.length;i++){
			if (arrayCodigoAgenda[i].checked){
				j++;
			}
		}
		return j;
	
	}

	/*
	Fun??o para abrir janela modal com os par?metros:
	url: paginal a ser aberta
	width: largura da janela a ser aberta
	height: altura da janela a ser aberta
	Veja tamb?m a fun??o:obterPaiModal
	*/    
	function abrirModal(url,width,height){
		if(window.showModalDialog){
			//Passa o formul?rio como par?metro para que seja chamado a partir da modal
			window.showModalDialog(url,window,'dialogWidth:'+width+'px;dialogHeight:'+height+'px');
		}else{
			window.open(url,'modal','height='+height+',width='+width+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
		}
	}
	/*  
	Fun??o complementar da abrirModal.
	Nela a janela que chamou a modal ? retornada.
	Estas fun??es foram criadas para encapsular as difer?n?as de funcionamento entre
	navegadores
	*/
	function obterPaiModal(){
		if (window.dialogArguments){
			return window.dialogArguments;
		}
		else{
			return window.opener;
		}
	}
	
	function ignorarEnter(e)
	{
		var keycode;
		if (window.event) 
			keycode = window.event.keyCode;
		else if (e) 
			keycode = e.which;
		else return true;
		if (keycode == 13)
		{
		   e.returnValue =false///and cancelBubble
		}
	}
	
	function debugMethod(method)
	{
		var str = "";
		var size = 0;
		try
		{	
			str = "<font face='verdana' size='1' color='red'><ol>";
			for(i in method)
			{
				str += "<li><b>"+i+"</b></li>" ;
				size++;
			}
			str += "</ol></font>";
			if(size>0)
				window.open("","DEBUG","dependent=yes,width=400,height=300,scrollbars=yes").document.write(str);
			else
				window.open("","DEBUG","dependent=yes,width=400,height=300,scrollbars=yes").document.write("N?o Foi encontrado nenhum m?todo para o mesmo ("+method+").");
				
		}catch(e)
		{
			alert("erro de debug-->"+e.message);
		}
	}
	
	var respostaDialogo=false;
	function chamaDialogoOk(mensagem,width,height)
	{
		chamaDialogo(mensagem,'ALERTA',width,height);
	}
	
	function chamaDialogoErro(mensagem,width,height)
	{
		chamaDialogo(mensagem,'ERRO',width,height);
	}
	
	function chamaDialogoConfirma(mensagem,width,height)
	{
		return chamaDialogo(mensagem,'CONFIRMA',width,height);
	}
	
	function chamaDialogo(mensagem,tipoDialogo,width,height)
	{
		if(!width){
			width = 320;
		}
		if(!height){
			height = 197;
		}
		respostaDialogo=false;
		if(!tipoDialogo){
			tipoDialogo = '';
		}
		abrirModal(contextPath+'/exibirDialogo.do?acao=exibir&mensagem='+mensagem+'&tipoDialogo='+tipoDialogo,width,height)
		return respostaDialogo;
	}
	
	function getCheckedValue(radioObj) 
	{
		if(!radioObj)
			return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
			if(radioObj.checked)
				return radioObj.value;
			else
				return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				return radioObj[i].value;
			}
		}
		return "";
	}


	function refreshArvore()
	{
	    window.opener.window.location.reload();
	    window.close();
	}

	function refreshSemReenvio(url)
	{
	   window.opener.window.location = url;
	   window.close();
	}

	function hideLayers(lay)
	{
		try{
			document.getElementById(lay).style.display = "none";
		}catch(e){
			alert(lay);
		}
	}

	function showLayers(lay)
	{
		try{
			document.getElementById(lay).style.display = "block";
		}catch(e){
			alert(lay);
		}
	}

	function showLayer(image,lay)
	{
		if (document.getElementById(lay).style.display == "none")
		{
			document.getElementById(lay).style.display = "block";
			image.src="imagens/grupoMinimizado.gif";
		}
		else
		{
			document.getElementById(lay).style.display = "none";
			image.src="imagens/grupoMaximizado.gif";
		}
	}
	
	function showRow(entidade,lay)
	{
		document.getElementById("td"+entidade+"_0").style.backgroundColor = "#FFFFFF";
		document.getElementById("td"+entidade+"_1").style.backgroundColor = "#FFFFFF";

		if (tdAnterior!=entidade && tdAnterior!="")
		{
			document.getElementById("td"+tdAnterior+"_0").style.backgroundColor = "";
			document.getElementById("td"+tdAnterior+"_1").style.backgroundColor = "";
			hideLayers("tr"+tdAnterior);
		}
		

		if (document.getElementById(lay).style.display == "none")
		{
			document.getElementById(lay).style.display = "block";
			document.getElementById("img"+entidade).src="/prontuario/geral/imagens/painel_"+entidade+"_menos.gif";
		}
		else
		{
			document.getElementById(lay).style.display = "none";
			document.getElementById("img"+entidade).src="/prontuario/geral/imagens/painel_"+entidade+"_mais.gif";
		}
			
						
			tdAnterior = entidade;
		
	}
	
	
	function addEntidade(entidade)
	{
		document.cookie = "td="+escape(entidade);
		
	}
	
	function pesquisarEntidade(entidade)
	{
		var cook = document.cookie; 
		var quebra_de_linha = cook.split("="); 
		var tdCookie = quebra_de_linha[1];  
		var tdCookie = unescape(tdCookie); 
		return tdCookie;
	}
	
	function redireciona(value)
	{
		document.forms[0].action = value;
		document.forms[0].submit();
	}			

	function redirecionaFrame(value)
	{
		top.location.href  = value;
	}			

	function fecharJanela(){
		window.close();
	}
	
	function trocaCorRecebeFoco(input){
		input.style.background = "lightyellow";	
	}
	
	function trocaCorPerdeFoco(input){
		input.style.background = "#FFFFFF";	
	}
	
	
		/*Script do cadastro de unidade de atendimento*/
		function salvaGrupoPermissao(){
			redireciona("manterGrupoPermissao.do?acao=salvarGrupoPermissao");
		}

		function novoGrupoPermissao(){
			redireciona("manterGrupoPermissao.do?acao=novoGrupoPermissao");
		}
		
		function adicionarPermissao(codigoPermissao){
		
			if(codigoPermissao!="")
				redireciona("manterGrupoPermissao.do?acao=adicionarGrupoPermissao&codigoPermissao="+codigoPermissao);
			else
				alert("Adicione alguma Permiss?o");			
		}
	
		function removerPermissao(codigoPermissao){
			redireciona("manterGrupoPermissao.do?acao=removerGrupoPermissao&codigoPermissao="+codigoPermissao);
		}
	
		function pesquisarGrupoPermissao(){
			redireciona("manterGrupoPermissao.do?acao=pesquisarPerfil");
		}
		
		
		/*Script Unidade atendimento*/
		function salvaUnidadeAtendimento()
		{
			redireciona("manterUnidadeAtendimento.do?acao=salvarUnidadeAtendimento");
		}
	
		function novoUnidadeAtendimento(evt)
		{
			redireciona("manterUnidadeAtendimento.do?acao=novoUnidadeAtendimento");
		}
		
		function pesquisaUnidadeAtendimento()
		{
			redireciona("manterUnidadeAtendimento.do?acao=pesquisarUnidadeAtendimento");
		}
		
		
		function validaForm()
		{
			if(!validarCadastroUsuario(document.forms[0])){
				return;
			}
		
			salvaUnidadeAtendimento();
		}
		
		/*Script do cadastro de perfil*/
		function salvarPerfil()
		{
			redireciona("manterPerfil.do?acao=salvarPerfil");
		}
	
		function pesquisarPerfil(codigoPermissao)
		{
			redireciona("manterPerfil.do?acao=pesquisarPerfil");
		}
		
		
			
		
		/*Pesquisar Grupo Permiss?o */
		function consultarGrupoPermissao()
		{
			redireciona("manterGrupoPermissao.do?acao=pesquisarGrupoPermissao");
		}
		
		function novoGrupoPermissao()
		{
			redireciona("manterGrupoPermissao.do?acao=novoGrupoPermissao");
		}

		function alterarGrupoPermissao(codigoGrupoPermissao)
		{
			redireciona("manterGrupoPermissao.do?acao=alterarGrupoPermissao&codigoGrupoPermissao="+codigoGrupoPermissao);
		}
		
		function excluirGrupoPermissao(codigoGrupoPermissao)
		{
			redireciona("manterGrupoPermissao.do?acao=excluirGrupoPermissao&codigoGrupoPermissao="+codigoGrupoPermissao);
		}
		
		
		function checaOpc(chkbox)
		{
			var chk = chkbox.name;
			
			frm = document.forms[0];
			for(i=0;i<frm.length;i++){
				if(frm.elements[i].name.indexOf(chk)!=-1)
				{
					frm.elements[i].checked = true;
				}
			}	
		}			
		
		
		
		/*Pesquisar permissao*/
		function consultarPermissao()
		{
			redireciona("manterPermissao.do?acao=pesquisarPermissao");
		}
		

		function listarPermissoes()
		{
			redireciona("manterPermissao.do?acao=obterPermissoes");
		}
		
		/*Pesquisar Unidade Atendimento*/		
		function consultarUnidadeAtendimento()
		{
			redireciona("manterUnidadeAtendimento.do?acao=pesquisarUnidadeAtendimento");
		}
		
		function novoUnidadeAtendimento()
		{
			redireciona("manterUnidadeAtendimento.do?acao=novaUnidadeAtendimento");
		}

		function alterarUnidadeAtendimento(codigoUnidadeAtendimento)
		{
			redireciona("manterUnidadeAtendimento.do?acao=alterarUnidadeAtendimento&codigoUnidadeAtendimento="+codigoUnidadeAtendimento);
		}
		
		/*Salvar Perfil*/
		

		
		/*Pesquisa Grupo Permissao*/
		function alterarGrpPer()
		{
			codigoPerfil = document.forms[0].codigoPerfil.value;
			redireciona("manterPerfil.do?acao=alterarPerfil&codigoPerfil="+codigoPerfil);
		}
		
		
		function procurarGrupoPermissao()
		{
			redireciona("manterPerfil.do?acao=pesquisarGrupoPermissaoAgrupado");
		}
		
		function salvaPermissoesPerfil()
		{
			redireciona("manterPerfil.do?acao=salvarPermissaoPerfil");
		}
		
		
		function setaFoco(elem){
			document.forms[0].elements[elem].focus();
		}

		function abrirJanelaCentro(uri, nomeJanela, largura, altura)
		{
			var larguraTela = window.screen.width;
			var alturaTela  = window.screen.height;
			window.open(uri,nomeJanela,"dependent=yes,width="+largura+",height="+altura+",left="+(larguraTela/2-largura/2)+",top="+(alturaTela/2-altura/2)+",scrollbars=yes");
		}
		
		function abrirJanelaCentroPortlet(uri, nomeJanela, largura, altura)
		{
			var larguraTela = window.screen.width;
			var alturaTela  = window.screen.height;
			window.open(uri,nomeJanela,"dependent=yes,width="+largura+",height="+altura+",left="+(larguraTela/2-largura/2)+",top="+(alturaTela/2-altura/2)+",scrollbars=no");
		}

		function trim(STRING)
		{
			STRING = LTrim(STRING);
			return RTrim(STRING);
		}
		
		function RTrim(STRING)
		{
			while(STRING.charAt((STRING.length -1))==" ")
			{
				STRING = STRING.substring(0,STRING.length-1);
			}
			return STRING;
		}
		
		
		function LTrim(STRING)
		{
			while(STRING.charAt(0)==" ")
			{
				STRING = STRING.replace(STRING.charAt(0),"");
			}
			return STRING;
		}
		function fMostraEsconde(valueId)
		{
			var target = document.getElementById(valueId);
			if (target)
			{
				if (target.style.display == 'block')
					target.style.display = 'none';
				else
					target.style.display = 'block';
			}
		}
		function upperCase(campo) { 
			campo.value = campo.value.toUpperCase(); 

		}
		
		function lowerCase(campo,e) { 
			var keycode;
			if (window.event) 
				keycode = window.event.keyCode;
			else if (e) 
			keycode = e.which;
		
			campo.value = (campo.value+String.fromCharCode(keycode)).toLowerCase(); 
			e.returnValue =false///and cancelBubble
		}
		function setComboValue(campoCombo,value)
		{
			for (i=0;i<campoCombo.length;i++)
			{
				if (campoCombo.options[i].value == value)
				{
			    	campoCombo.options[i].selected = true;
			    	return;
			    }

			}
		}

		function verificaInputVazio(formulario)
		{
			var isVazio = true;
			var tipo = "";
			for(i=0; i<formulario.elements.length; i++)
			{
				if(formulario.elements[i].type == "text" || formulario.elements[i].type == "textarea" )
				{
					if(trim(formulario.elements[i].value) != '')
					{
						isVazio = false;
						break;
					}
				}
			}
			return isVazio;
		}
		
		function abrirNode(identificadorNode)
		{
			if(identificadorNode!='')	
			{
				window.parent.frames[1].jsTree.abrirNodeSelecionado(identificadorNode);
			}
		}
		
		/* Gravar Informacao Relevante. 
			Esta funcao sera utilizada apenas nas telas do prontuario,
			pois necessita-se ter um hidden com o codigoRegistroAtendimento. */
		function registrarInformacaoRelevante(className,identificador,path,informacao)
		{
			var codigoRegistroAtendimento = document.getElementById("codigoRegistroAtendimento").value;
			
			if(informacao == undefined || informacao == null)
			{
				// pegando o texto selecionado
				var textoRelevante = document.selection.createRange().text;
				
				if(trim(textoRelevante) == '')
				{
					alert('Selecione um texto!');
					return;
				}
				else
				{
					informacao = textoRelevante;
					abrirJanelaCentro("registrarInformacaoRelevante.do?acao=exibirInformacaoRelevante&codigoRegistroAtendimento="+codigoRegistroAtendimento+"&className="+className+"&identificador="+identificador+"&path="+path+"&informacao="+informacao+"&tipo=textoRelevante","InformacaoRelevante",500,450);
				}
			}
			else
			{
				abrirJanelaCentro("registrarInformacaoRelevante.do?acao=exibirInformacaoRelevante&codigoRegistroAtendimento="+codigoRegistroAtendimento+"&className="+className+"&identificador="+identificador+"&path="+path+"&informacao="+informacao,"InformacaoRelevante",500,450);
			}
		}
		
		function limparForm()
		{
			var formulario = document.forms[0];
			
			for(i=0;i<formulario.length;i++)
			{
				if(formulario.elements[i].type=='text')
				{
					formulario.elements[i].value = '';
				}
				if(formulario.elements[i].type=='checkbox')
				{
					formulario.elements[i].checked = false;
				}
				if(formulario.elements[i].type=='select-one')
				{
					formulario.elements[i].options[0].selected = true;
				}
			}
		}
		