   /*
   FUNÇÃO USADA EM CLIENTE_ABAS, GRUPO_ABAS, USUARIO_ABAS, TRANSACAO_ABAS. 
   RESPONSÁVEL PELO AUTO COMPLETE DO CAMPOS PASSADOS COMO PARAMETRO.
   */
   var retorno;
   function autoComplete(id, id2, div){
		//Instanciar objeto AutoComplete
		var AC = new dmsAutoComplete(id, id2, div);
		switch(id){
			case 'cliente_nome' : AC.ajaxTarget = 'cliente_nome';
			break;
			case 'cliente_cnpjcpf' : AC.ajaxTarget = 'cliente_cnpjcpf';
			break;
			case 'precliente_nome' : AC.ajaxTarget = 'precliente_nome';
			break;
			case 'usuario_nome' : AC.ajaxTarget = 'usuario_nome';
			break;
			case 'usuario_login' : AC.ajaxTarget = 'usuario_login';
			break;
			case 'grupo_nome' : AC.ajaxTarget = 'grupo_nome';
			break;
			case 'transacao_nome' : AC.ajaxTarget = 'transacao_nome';
			break;
			case 'cliente_prodoctor_nome' : AC.ajaxTarget = 'cliente_prodoctor_nome';
			break;
			case 'codigo_evolucao_erro' : AC.ajaxTarget = 'codigo_evolucao_erro';
			break;
			case 'descricao_evolucao_erro' : AC.ajaxTarget = 'descricao_evolucao_erro';
			break;
			case 'codigo_procedimento' : AC.ajaxTarget = 'codigo_procedimento';
			break;
			case 'nome_procedimento' : AC.ajaxTarget = 'nome_procedimento';
			break;
			
		}
		
		//Definir função de retorno
		//Esta função será executada ao se escolher a palavra
		AC.chooseFunc = function(id, id2, label){
		}
	}

  function gravaSessao(result) {
    window.location = window.location;
  }


function Verifica(numero) {

    var cp = new cpaint();
    cp.set_transfer_mode('POST');
    cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('ajax_lf.php', "getDadosByNumero", ComparaDados, numero);
  }
 
  
  
  function VerificaExiste(numero) {

    var cp = new cpaint();
    cp.set_transfer_mode('POST');
    cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('ajax_lf.php', "getDadosByNumero", VerificaSeExiste, numero);
  }
  
  
function VerificaResposta(numero, entrevistado) {
    var cp = new cpaint();
    cp.set_transfer_mode('POST');
    cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('ajax_lf.php', "getRespostasById", ComparaResposta, numero, entrevistado);
  }

  
  
 function VerificaDescricao(descricao, entrevistado) {
    var cp = new cpaint();
    cp.set_transfer_mode('POST');
    cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('ajax_lf.php', "getDescricaoById", ComparaDescricao, descricao, entrevistado);
  }
  
  function VerificaSeExiste(result) {
	  
    //pega a tag cidade
    var dataArray   = result.getElementsByTagName("entrevistado");
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
      //percorre o arquivo XML paara extrair os dados
		  alert('Este questionário já foi digitado! Verifique se a numeração está correta!');
		  
		  document.getElementById('numero').value = "";
		  document.getElementById('numero').focus();
	  }
  }
  
  function VerificaFim(id_entrevistado, questoes) {

	  var correto;
	  correto = true;
	  
	  var entrevistado = id_entrevistado;
	  var texto = 'OS DADOS ABAIXO NÃO CONFEREM. CONFIRMA A SUA OPÇÃO?\n';
	  
	 if((document.getElementById('nome').value == '') && (document.getElementById('nome_hidden').value != '')){
		  document.getElementById('nome').style.background = "#ba9a83";
	  	  correto = false;
		  texto = texto + '- NOME\n';
	  }
	  
	  if((document.getElementById('cidade').value == '') && (document.getElementById('cidade_hidden').value != '')){
		  document.getElementById('cidade').style.background = "#ba9a83";
	  		correto = false;
			texto = texto + '- CIDADE\n';
	  }
	  
	  if((document.getElementById('ins_estado').value == '') && (document.getElementById('ins_estado_hidden').value != '')){
	  	document.getElementById('ins_estado').style.background = "#ba9a83";
		correto = false;
		texto = texto + '- ESTADO\n';
	  }
	  
	  if((document.getElementById('ano_formatura').value == '') && (document.getElementById('ano_formatura_hidden').value != '')){
		document.getElementById('ano_formatura').style.background = "#ba9a83";
		correto = false;
		texto = texto + '- ANO DE FORMATURA\n';
	  }
	  
	  if((document.getElementById('especialidade').value == '') && (document.getElementById('especialidade_hidden').value != '')){
		  document.getElementById('especialidade').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- ESPECIALIDADE\n';
	  }
	  
	  if((document.getElementById('subespecialidade').value == '') && (document.getElementById('subespecialidade_hidden').value != '')){
		  document.getElementById('subespecialidade').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- SUBESPECIALIDADE\n';
	  }
	  
	  if((document.getElementById('clinica_ambulatorio_privada').checked == false) && (document.getElementById('clinica_ambulatorio_privada_hidden').checked == true)){ 
		 correto = false;
		 texto = texto + '- CLÍNICIA/AMBULATORIO PRIVADA\n';
	  }
	  
	  if((document.getElementById('clinica_ambulatorio_publica').checked == false) && (document.getElementById('clinica_ambulatorio_publica_hidden').checked == true)){
		document.getElementById('clinica_ambulatorio_publica').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- CLÍNICIA/AMBULATORIO PÚBLICA\n';
	  }
	  
	  if((document.getElementById('enfermaria_privada').checked == false) && (document.getElementById('enfermaria_privada_hidden').checked == true)){
		  document.getElementById('enfermaria_privada').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- ENFERMARIA PRIVADA\n';
	  }
	  
	  if((document.getElementById('enfermaria_publica').checked == false) && (document.getElementById('enfermaria_publica_hidden').checked == true)){
		 document.getElementById('enfermaria_publica').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- ENFERMARIA PÚBLICA\n';
	  }
	  
	  if((document.getElementById('ps_emergencia_privada').checked == false) && (document.getElementById('ps_emergencia_privada_hidden').checked == true)){
		   document.getElementById('ps_emergencia_privada').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- PS/EMERGÊNCIA PRIVADA\n';
	  }
	  
	  if((document.getElementById('ps_emergencia_publica').checked == false) && (document.getElementById('ps_emergencia_publica_hidden').checked == true)){
		  document.getElementById('ps_emergencia_publica').style.background = "#ba9a83";
	  	correto = false;
		texto = texto + '- PS/EMERGÊNCIA PÚBLICA\n';
	  }
	  
		if(correto == false){
			document.getElementById('numero').focus();
		}
		
		if(texto != 'OS DADOS ABAIXO NÃO CONFEREM. CONFIRMA A SUA OPÇÃO?\n'){
			if(!confirm(texto)){
				return false;
			}			
		}
	
		
		var questao2 = '';
		for (var a = 0; a < questoes.length; ++a){
			var inputs = document.avaliacao.elements[questoes[a]];
			var checado = false;
			var questao = '';
			for (var i = 0; i < inputs.length; ++i){
				if(inputs[i].checked == true){
					checado = true;
				}
				questao = inputs[i].name;
			}
			if(checado == false){
				questao2 = questao2 + '\n' + questao;
			}
			
		}
		
		if(questao2 != ''){
			if(!confirm('Confirma que as questões listadas abaixo não foram respondidas?\n' + questao2)){
				return false;
			}
		}
				
  }
  
  function VerificaRespostaFim(numero, entrevistado) {
    var cp = new cpaint();
    cp.set_transfer_mode('POST');
    cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('ajax_lf.php', "getRespostasById", ComparaRespostaFim, numero, entrevistado);
  }
  
 /*
 BUSCA DADOS DA CIDADE QUANDO O ESTADO JÁ FOI ESCOLHIDO!!
 */
  function ComparaDados(result) {
	  
    //pega a tag cidade
    var dataArray   = result.getElementsByTagName("entrevistados");
	
	 var dataArray2   = result.getElementsByTagName("entrevistado");
	 
      if(dataArray2.length == 0){
	  	alert('Este questionário ainda não foi digitado!');
		document.getElementById('numero').value = "";
		document.getElementById('numero').focus();
	  }else{
		  //total de elementos contidos na tag cidade
		  if(dataArray.length > 0) {
		  //percorre o arquivo XML paara extrair os dados
			  for(var i = 0 ; i < dataArray.length ; i++) {
				var item = dataArray[i];
		
				//contéudo dos campos no arquivo XML
				if(item.getElementsByTagName("id_entrevistado")[0].firstChild != null) {
					var id_entrevistado =  item.getElementsByTagName("id_entrevistado")[0].firstChild.nodeValue;
				} else {
				  var id_entrevistado = "";
				}
				
				if(item.getElementsByTagName("nome")[0].firstChild != null) {
					var nome =  item.getElementsByTagName("nome")[0].firstChild.nodeValue;
				} else {
					var nome = "";
				}
				
				if(item.getElementsByTagName("cidade")[0].firstChild != null) {
					var cidade =  item.getElementsByTagName("cidade")[0].firstChild.nodeValue;
				} else {
					var cidade = "";
				}
				
				/*if(item.getElementsByTagName("codigo_estado")[0].firstChild != null) {
					var codigo_estado =  item.getElementsByTagName("codigo_estado")[0].firstChild.nodeValue;
				} else {
					var codigo_estado = "";
				}*/
				
				if(item.getElementsByTagName("ano_formatura")[0].firstChild != null) {
					var ano_formatura =  item.getElementsByTagName("ano_formatura")[0].firstChild.nodeValue;
				} else {
					var ano_formatura = "";
				}
				
				if(item.getElementsByTagName("codigo_espec")[0].firstChild != null) {
					var codigo_espec =  item.getElementsByTagName("codigo_espec")[0].firstChild.nodeValue;
				} else {
					var codigo_espec = "";
				}
				
				if(item.getElementsByTagName("codigo_subespec")[0].firstChild != null) {
					var codigo_subespec =  item.getElementsByTagName("codigo_subespec")[0].firstChild.nodeValue;
				} else {
					var codigo_subespec = "";
				}
				
				if(item.getElementsByTagName("clinica_ambulatorio_privada")[0].firstChild != null) {			
					var clinica_ambulatorio_privada =  item.getElementsByTagName("clinica_ambulatorio_privada")[0].firstChild.nodeValue;
				} else {
					var clinica_ambulatorio_privada = "";
				}
				
				if(item.getElementsByTagName("clinica_ambulatorio_publica")[0].firstChild != null) {
					var clinica_ambulatorio_publica =  item.getElementsByTagName("clinica_ambulatorio_publica")[0].firstChild.nodeValue;
				} else {
					var clinica_ambulatorio_publica = "";
				}
				
				
				if(item.getElementsByTagName("enfermaria_privada")[0].firstChild != null) {			
					var enfermaria_privada =  item.getElementsByTagName("enfermaria_privada")[0].firstChild.nodeValue;
				} else {
					var enfermaria_privada = "";
				}
				
				if(item.getElementsByTagName("enfermaria_publica")[0].firstChild != null) {
					var enfermaria_publica =  item.getElementsByTagName("enfermaria_publica")[0].firstChild.nodeValue;
				} else {
					var enfermaria_publica = "";
				}
				
				
				if(item.getElementsByTagName("ps_emergencia_privada")[0].firstChild != null) {			
					var ps_emergencia_privada =  item.getElementsByTagName("ps_emergencia_privada")[0].firstChild.nodeValue;
				} else {
					var ps_emergencia_privada = "";
				}
				
				if(item.getElementsByTagName("ps_emergencia_publica")[0].firstChild != null) {
					var ps_emergencia_publica =  item.getElementsByTagName("ps_emergencia_publica")[0].firstChild.nodeValue;
				} else {
					var ps_emergencia_publica = "";
				}
				
				
				if(item.getElementsByTagName("instituicao_ensino")[0].firstChild != null) {			
					var instituicao_ensino =  item.getElementsByTagName("instituicao_ensino")[0].firstChild.nodeValue;
				} else {
					var instituicao_ensino = "";
				}
				
				document.avaliacao.id_entrevistado.value = id_entrevistado;
				document.getElementById('nome_hidden').value = nome;
				document.getElementById('cidade_hidden').value = cidade;
				//document.getElementById('ins_estado_hidden').value = codigo_estado;
				document.getElementById('ano_formatura_hidden').value = ano_formatura;
				document.getElementById('especialidade_hidden').value = codigo_espec;
				//document.forms[0].especialidade.value = codigo_espec;
				checkSelect(document.getElementById('especialidade_hidden'), codigo_espec);
				
				document.getElementById('subespecialidade_hidden').value = codigo_subespec;
				
				//document.forms[0].clinica_ambulatorio_privada.value = clinica_ambulatorio_privada;
				//document.forms[0].clinica_ambulatorio_publica.value = clinica_ambulatorio_publica;
				checkCheckBox(document.getElementById('clinica_ambulatorio_privada_hidden'), clinica_ambulatorio_privada);
				checkCheckBox(document.getElementById('clinica_ambulatorio_publica_hidden'), clinica_ambulatorio_publica);
				
				//document.forms[0].enfermaria_privada.value = enfermaria_privada;
				//document.forms[0].enfermaria_publica.value = enfermaria_publica;
				checkCheckBox(document.getElementById('enfermaria_privada_hidden'), enfermaria_privada);
				checkCheckBox(document.getElementById('enfermaria_publica_hidden'), enfermaria_publica);
				
				//document.forms[0].ps_emergencia_privada.value = ps_emergencia_privada;
				//document.forms[0].ps_emergencia_publica.value = ps_emergencia_publica;
				checkCheckBox(document.getElementById('ps_emergencia_privada_hidden'), ps_emergencia_privada);
				checkCheckBox(document.getElementById('ps_emergencia_publica_hidden'), ps_emergencia_publica);
				
				document.getElementById('instituicao_ensino_hidden').value = instituicao_ensino;
				checkRadio(document.getElementById('instituicao_ensino_hidden'), instituicao_ensino);
				
		
			  }
		  }else {
			//caso o XML volte vazio, printa a mensagem abaixo
			idOpcao.innerHTML = "--Primeiro selecione o Estado--";
		  } 
	  }
  }
  
  
  
   function ComparaResposta(result) {
	  
    //pega a tag resposta
    var dataArray   = result.getElementsByTagName("respostas");
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
      //percorre o arquivo XML paara extrair os dados
		  for(var i = 0 ; i < dataArray.length ; i++) {
				var item = dataArray[i];
	
				//contéudo dos campos no arquivo XML
				if(item.getElementsByTagName("id_opcao")[0].firstChild != null) {
					var id_opcao =  item.getElementsByTagName("id_opcao")[0].firstChild.nodeValue;
					var id_questao =  item.getElementsByTagName("id_questao")[0].firstChild.nodeValue;
					
					if(id_opcao == 0){
					  if(!confirm('As respostas estão diferentes. Confirma a sua opção?')){
						  var objeto = 'opcao['+id_questao+']';
						  
						  
						  var inputs = document.getElementsByTagName('input');
							if (inputs) {
							  for (var i = 0; i < inputs.length; ++i){
								if (inputs[i].type == 'radio'){
									if(inputs[i].name == objeto)
								  inputs[i].checked = false;
								}
							  }
							}
					  }
					}
				}
		  }
		}else {
	    //caso o XML volte vazio, printa a mensagem abaixo
  			alert('INDIQUE O NÚMERO DO QUESTIONÁRIO!');
		} 
  }
  
  function ComparaRespostaFim(result) {
	  
    //pega a tag resposta
    var dataArray   = result.getElementsByTagName("respostas");
	 var objeto = 'VERIFIQUE AS SEGUINTES QUESTÕES:'
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
      //percorre o arquivo XML paara extrair os dados
		  for(var i = 0 ; i < dataArray.length ; i++) {
				var item = dataArray[i];
	
				//contéudo dos campos no arquivo XML
				if(item.getElementsByTagName("id_opcao")[0].firstChild != null) {
					var id_opcao =  item.getElementsByTagName("id_opcao")[0].firstChild.nodeValue;
					var id_questao =  item.getElementsByTagName("id_questao")[0].firstChild.nodeValue;
					
					if(id_opcao != 0){
						  var objeto = objeto +"\n"+ id_questao;
						  

						 /* var inputs = document.getElementsByTagName('input');
							if (inputs) {
							  for (var i = 0; i < inputs.length; ++i){
								if (inputs[i].type == 'radio'){
									alert(inputs[i].name);
									if(inputs[i].name == objeto){
								  		alert(inputs[i].name);
									}
							    }
							  }
							}*/
					  }
				}
		  }
		}else {
	    //caso o XML volte vazio, printa a mensagem abaixo
  			alert('INDIQUE O NÚMERO DO QUESTIONÁRIO!');
		} 
		retorno = objeto;
  }
  
  
  function ComparaDescricao(result) {
	  
    //pega a tag resposta
    var dataArray   = result.getElementsByTagName("descricoes");
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
      //percorre o arquivo XML paara extrair os dados
	  
	  		for(var i = 0 ; i < dataArray.length ; i++) {
				var item = dataArray[i];
	
				//contéudo dos campos no arquivo XML
				if(item.getElementsByTagName("id_opcao")[0].firstChild != null) {
					var id_opcao =  item.getElementsByTagName("id_opcao")[0].firstChild.nodeValue;
					var descricao =  item.getElementsByTagName("nome")[0].firstChild.nodeValue;
					
					if(descricao == 0){
					  if(!confirm('As respostas estão diferentes. Confirma a sua opção?')){
						  document.getElementById('11_4_descricao').value = '';
					  }
					}
				}
			}
		}else {
	    //caso o XML volte vazio, printa a mensagem abaixo
  			alert('Entrou aqui');
		} 
  }
  


  
  function VerificaCampos(campo1, campo2, nome){
	  
	  var nome = nome;

  		if(campo1 !== campo2){
			if(!confirm('As respostas estão diferentes. Confirma a sua opção?')){
				document.getElementById(nome).value = '';
			}
		}
  }
  
   /*
 BUSCA DADOS DA CIDADE QUANDO O ESTADO JÁ FOI ESCOLHIDO!!
 */
  function BuscaCidade(valor) {
    var cp = new cpaint();
  	cp.set_transfer_mode('POST');
  	cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('questionario/ajax_lf.php', "getCidadesByEstado", CarregaCidades, valor);
    //deixa apenas o elemento 1 no option, os outros são excluídos
    document.forms[0].cidade.options.length = 1;
    idOpcao  = document.getElementById("opcoes");
    idOpcao.innerHTML = "Carregando...!";   
  }

 /*
 BUSCA DADOS DA CIDADE (que está na tabela entrevistado) QUANDO O ESTADO JÁ FOI ESCOLHIDO!!
 */
  function BuscaCidadeEntrevistado(valor) {
    var cp = new cpaint();
    cp.set_transfer_mode('POST');
    cp.set_response_type('XML');
    cp.set_debug(0);
    cp.call('ajax_lf.php', "getCidadesEntrevistadoByEstado", CarregaCidades, valor);
    //deixa apenas o elemento 1 no option, os outros são excluídos
    document.forms[0].cidade.options.length = 1;
    idOpcao  = document.getElementById("opcoes");
    idOpcao.innerHTML = "Carregando...!";   
  }

  function CarregaCidades(result){
    
    //pega a tag cidade
    var dataArray   = result.getElementsByTagName("cidade");
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
      //percorre o arquivo XML paara extrair os dados
      for(var i = 0 ; i < dataArray.length ; i++) {
        var item = dataArray[i];

        //contéudo dos campos no arquivo XML
        var codigo_estado    =  item.getElementsByTagName("codigo_estado")[0].firstChild.nodeValue;
        var codigo_cidade =  item.getElementsByTagName("codigo_cidade")[0].firstChild.nodeValue;
        var nome =  item.getElementsByTagName("nome")[0].firstChild.nodeValue;
        
        idOpcao.innerHTML = "--Selecione uma das opções abaixo--";
        
        //cria um novo option dinamicamente  
        var novo = document.createElement("option");
        //atribui um ID a esse elemento
        novo.setAttribute("id", "opcoes");
        //atribui um valor
        novo.value = codigo_cidade;
        //atribui um texto
        novo.text  = nome;
        //finalmente adiciona o novo elemento
        document.forms[0].cidade.options.add(novo);
      }
	  } else {
	    //caso o XML volte vazio, printa a mensagem abaixo
  		idOpcao.innerHTML = "--Primeiro selecione o Estado--";
	  } 
  }
  
  function checkSelect(objeto, texto){

	for (var i = 0; i < objeto.length; i++){
		if (objeto.options[i].value == texto){
			objeto.options[i].selected = true;
		}
	}
}

function checkRadio(objeto, texto) {

	for (var i = 0; i < objeto.length; i++){
		if (objeto[i].value == 't' || objeto[i].value == texto || objeto[i].value == 1){
			objeto[i].checked = true;
		}
	}
}

function checkCheckBox(objeto, texto) {
	if (texto == 1 || texto == 't'){
			objeto.checked = true;
		}
}

function Limpar(campo){
	var inputs = document.getElementsByTagName('input');
	if (inputs) {
	  for (var i = 0; i < inputs.length; ++i){
		if (inputs[i].type == 'radio'){
			if(inputs[i].name == campo){
				inputs[i].checked = false;
			}
		}
	  }
	}
}