function buscarDadosUCFAQ(objTxt){
	document.getElementById('txtNomeSolic').value = '';
	document.getElementById('txtEmail').value = '';
	document.getElementById('txtDddTelefone').value = '';
	document.getElementById('txtTelefone').value = '';
	if(objTxt.value.length > 0){
		if(!isNaN(objTxt.value)){
			DadosUCFAQ.getDadosUC(objTxt.value, function(dados){
				var nome;
				var email;
				var fone;
				var ddd;
				
				if(dados){
					nome = (dados.nomCli != null) ? dados.nomCli : '' ;
					email = (dados.codEmailCli != null) ? dados.codEmailCli : '';
					fone = (dados.numFoneCli != null) ? dados.numFoneCli : '';
					ddd = fone;
					
					if(fone != ''){
						ddd = fone.substring(0,2);
						fone = fone.substring(2,fone.length);
					}
					
					document.getElementById('txtNomeSolic').value = nome;
					document.getElementById('txtEmail').value = email;
					document.getElementById('txtDddTelefone').value = ddd;
					document.getElementById('txtTelefone').value = fone;
				}
				else{
					alert('Verifique se o número da Unidade Consumidora está correto.');
					objTxt.select();
				}
			});
		}
	}
}

function buscarPergunta(objSelAssunto,qtdePerg){
	var codOri = objSelAssunto.options[objSelAssunto.selectedIndex].value;
	
	PerguntasFAQ.getPerguntas(codOri,true,function(dados){
		var container = document.getElementById('containerPerguntas');
		var strHtml = '';
		
		
		if(dados.length > 0){
			//Se nao passar qtde todas as perguntas serao exibidas
			if(qtdePerg == null || qtdePerg == 0){
				qtdePerg = dados.length;
			}
			
			for(i = 0; i < dados.length && i < qtdePerg; i++){
				strHtml += '<div class="divPerguntaFAQ textoGeralNegrito" id="perg_'+dados[i].codPergunta+'">';
				strHtml += '	<a onclick="JavaScript:exibirRespostaFAQ(\''+dados[i].codPergunta+'\');">'+dados[i].pergunta+'</a>';
				strHtml += '	<div class="divRespostaFAQ textoGeral">'
				strHtml += 				dados[i].resposta.replace(/\r/g,'<br />');
				strHtml += '	</div></div>';
			}
			
			if(dados.length > qtdePerg){
				strHtml += '<div id="divMaisPerguntasFAQ" style="display:none">';
				for(i = qtdePerg; i < dados.length; i++){
					strHtml += '<div class="divPerguntaFAQ textoGeralNegrito" id="perg_'+dados[i].codPergunta+'">';
					strHtml += '	<a onclick="JavaScript:exibirRespostaFAQ(\''+dados[i].codPergunta+'\');">'+dados[i].pergunta+'</a>';
					strHtml += '	<div class="divRespostaFAQ textoGeral">'
					strHtml += 				dados[i].resposta.replace(/\r/g,'<br />');
					strHtml += '	</div></div>';
				}
				strHtml += '</div>';
				
				exibirLinkMaisPerg(true);
			}
			else{
				exibirLinkMaisPerg(false);
			}
		}
		else{
			exibirLinkMaisPerg(false);
		}
		
		container.innerHTML = strHtml;
	});
}

function exibirLinkMaisPerg(exibir){
	var linkMaisPerg = document.getElementById('linkMaisPerg');
	
	if(linkMaisPerg){
		if(!exibir){
			linkMaisPerg.style.visibility = 'hidden';
		}
		else{
			linkMaisPerg.style.visibility = 'visible';
		}
	}
}

function exibirRespostaFAQ(idDivPerg, contar){
	var objDivPerg = document.getElementById('perg_'+idDivPerg);
	var objResp = objDivPerg.getElementsByTagName('div')[0];
	
	if(objResp){
		if(objResp.style.display == 'block'){
			objResp.style.display = 'none';
		}
		else{
			//Adicionando no contador
			PerguntasFAQ.incrementarClick(idDivPerg, function(){
				objResp.style.display = 'block';
			});
		}
	}
}

function exibirMaisPerguntas(objOri){
	var divMais = document.getElementById('divMaisPerguntasFAQ');
	
	if(divMais){
		divMais.style.display = 'block';
		objOri.style.visibility = 'hidden';
	}
}

function buscarPerguntaAdmin(objSelAssunto){
	var codOri = objSelAssunto.options[objSelAssunto.selectedIndex].value;
	
	PerguntasFAQ.getPerguntas(codOri,false,function(dados){
		var container = document.getElementById('containerPerguntas');
		var strHtml = '';
		
		
		if(dados.length > 0){
			for(i = 0; i < dados.length; i++){
				strHtml += '<div class="divPerguntaFAQ textoGeralNegrito" id="perg_'+dados[i].codPergunta+'">';
				strHtml += '	<a onclick="JavaScript:editarPergunta(\''+dados[i].codPergunta+'\');">'+dados[i].pergunta+'</a>';
				strHtml += '	<div class="divRespostaFAQ textoGeral">'
				strHtml += 				dados[i].resposta;
				strHtml += '	</div></div>';
			}
		}
		container.innerHTML = strHtml;
		cadastrarPerguntas();
	});
}

function editarPergunta(idPerg){
	var objSelAssunto = document.getElementById('selAssunto')
	var codOri = objSelAssunto.options[objSelAssunto.selectedIndex].value;
	var perg = document.getElementById('txtPergunta');
	var resp = document.getElementById('txtResposta');
	var codPerg = document.getElementById('hdnPergId');
	var status = document.getElementsByName('habilita');
	
	PerguntasFAQ.getPergunta(codOri,idPerg,false,function(dado){
		perg.value = dado.pergunta;
		resp.value = dado.resposta;
		codPerg.value = dado.codPergunta;
		
		if(status[0].value == dado.statusPergunta){
			status[0].checked="checked";
		}
		else{
			status[1].checked="checked";
		}
	});
}

function cadastrarPerguntas(){
	var perg = document.getElementById('txtPergunta');
	
	document.getElementById('txtResposta').value = '';
	document.getElementById('hdnPergId'),value = 0;
	
	perg.value = '';
	perg.focus();
}
