// buscareferenciado.js


// Armazena o nome do Navegador do cliente
var browser = navigator.appName;

function submitForm(url) {	
	
	var codigoRede = document.getElementById("idPlanos").value;
	var tipoServico = document.getElementById("idServicos").value;
	var especialidade = document.getElementById("especialidade").value;
	var nomeReferenciado = document.getElementById("idNome").value;
	
	var uf = document.getElementById("uf").value;
	var cidade = document.getElementById("cidade").value;

	//var pesquisa = document.getElementByName("pesquisaTipo").value;
	var pesquisa = getCheckedValue(document.forms['buscaReferenciadoForm'].elements['tipoPesquisa']);
	
	if(pesquisa == "porTipo"){
		pesquisa = "tipo" ;
		nomeReferenciado = "";
	}else if(pesquisa == "porNome"){
		pesquisa = "nome";
	} else if (pesquisa == ''){
		pesquisa = "tipo" ;
	}
	
	if(codigoRede == "") {
		alert("Selecione a Rede Referenciada.");
		
	} else if(uf == "") {
		alert("Selecione o Estado.");
		
	} else if(cidade == "") {
		alert("Selecione a Cidade");
	
	} else if(pesquisa == "nome" && nomeReferenciado == "") {
		alert("Preencha o Nome do Referenciado.");
	
	} else if(pesquisa == "tipo" && tipoServico == "") {
		alert("Selecione o Tipo de Serviço.");
		
	} else if(pesquisa == "tipo" && especialidade == "") {
		alert("Selecione a Especialidade.");
		
	} else {
		
		url += "PCBS-BuscaReferenciadoRAD/buscaReferenciadoPortal.do?NOVO_PORTAL_DENTAL=N";
		url += "&tpPesq=M";
		url += "&uf=" + uf;
		url += "&cidade=" + cidade; 
		url += "&pesquisa=" + pesquisa;
		url += "&tipoServico=" + tipoServico; 
		url += "&especialidade=" + especialidade;
		url += "&nomeReferenciado=" + nomeReferenciado; 
		url += "&cdRede=" + codigoRede;
		url += "&css=" + 'urlHttpServer';
				
		var width = 600;
		var height = 529;
		
		var left = 100;
		var top = 100;
		
		window.open(url, 'RedeReferenciada', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=yes,status=no,toolbar=no,location=no');
		
	}
}

function load() {
	//document.getElementById("idUF").value = '<c:out value="${estados}"/>';
}

function openPop1(url) {
	var width = 505;
	var height = 529;
	var left = 100;
	var top = 100;
	url = servidorURL(url,"outros");
	window.open(url, 'AcessoExclusivo', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=yes,status=no,toolbar=no,location=no');
}


function openPop(url) {
	var width = 505;
	var height = 540;
	var left = 100;
	var top = 100;
	url = servidorURL(url,"outros");
	window.open(url, 'AcessoExclusivo', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=no,status=no,toolbar=no,location=no');	
}

function openCorretor() {

	var url = "100Corretor/br/home/default.asp?site=bs";
	url = servidorURL(url,"corretor");

	var width = 765;
	var height = 585;
	
	var left = 100;
	var top = 100;
	
	window.open(url, 'AcessoExclusivo', '');
	
}

function openProdutor() {

	var url = "sprodutor/sprod_login.asp";
	url = servidorURL(url,"corretor");
	var width = 800;
	var height = 585;
	
	var left = 100;
	var top = 100;
	
	window.open(url, 'AcessoExclusivo', '');
}



function verificarEnter(evt,url) {	
    	
        var charCode = (evt.which) ? evt.which : event.keyCode;     
         
        if( charCode == "13" ){        	
            return submitForm(url);
        }else{
            return false;
        }
}



function carregarCombo(listaParametros){
	DWRUtil.useLoadingMessage("Carregando");
	DWRUtil.removeAllOptions(listaParametros["nomeCombo"]);
	// Executa o comando
	var comando = "BuscaReferenciadoDWR." + listaParametros["nomeMetodo"] + "('" + listaParametros["parametro"] + "',{ callback:function(listaRetorno){ DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", {\"\":\"Selecione...\"}); DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", listaRetorno, \"" + listaParametros["atributoValue"] + "\", \"" + listaParametros["atributoDescricao"] + "\");cursor_clear();}, errorHandler:function(errorString, exception) { setMsgError(errorString, 'block'); }, timeout:60000});";
	eval(comando);
}

function carregarComboEstadoPorPlano(listaParametros){
	DWRUtil.useLoadingMessage("Carregando");
	DWRUtil.removeAllOptions(listaParametros["nomeCombo"]);
	
	// Remove as cidades
	var comboCidades = document.getElementById("cidade");
	
	if(comboCidades != null){
		var i;
	    
		for (i = comboCidades.length - 1; i>=0; i--) {
			comboCidades.remove(i);
		}
		
		// Adiciona uma ciade temporaria
		
		var cidadeSelecione = document.createElement('option');
		cidadeSelecione.text = 'Selecione...';
		cidadeSelecione.value = ' ' ;
	    var cidadeTemporaria = comboCidades.options[0];  
	    try {
	    	comboCidades.add(cidadeSelecione, cidadeTemporaria); // padrao W3C; nao funciona no IE
	    }
	    catch(ex) {
	    	comboCidades.add(cidadeSelecione, 0); // apenas para o IE 
	    }

	}
    
	
	var strCodPlano = document.getElementById('idPlanos').value;
	 
	// Executa o comando
	var comando = "BuscaReferenciadoDWR." + listaParametros["nomeMetodo"] + "('" + listaParametros["parametro"] + "',{ callback:function(listaRetorno){ DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", {\"\":\"Selecione...\"}); DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", listaRetorno, \"" + listaParametros["atributoValue"] + "\", \"" + listaParametros["atributoDescricao"] + "\");cursor_clear();}, errorHandler:function(errorString, exception) { setMsgError(errorString, 'block'); }, timeout:60000});";
	eval(comando);
}

function carregarComboCidadePorPlano(listaParametros){
	DWRUtil.useLoadingMessage("Carregando");
	DWRUtil.removeAllOptions(listaParametros["nomeCombo"]);
	
	var strCodPlano = document.getElementById('idPlanos').value;
	
	// Executa o comando
	var comando = "BuscaReferenciadoDWR." + listaParametros["nomeMetodo"] + "('" + strCodPlano +  "','" + listaParametros["parametro"] + "',{ callback:function(listaRetorno){ DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", {\"\":\"Selecione...\"}); DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", listaRetorno, \"" + listaParametros["atributoValue"] + "\", \"" + listaParametros["atributoDescricao"] + "\");cursor_clear();}, errorHandler:function(errorString, exception) { setMsgError(errorString, 'block'); }, timeout:60000});";
	eval(comando);
}


function carregarComboTipoDeServico(listaParametros){
	DWRUtil.useLoadingMessage("Carregando");
	DWRUtil.removeAllOptions(listaParametros["nomeCombo"]);
	
	var strCodPlano = document.getElementById('idPlanos').value;
	
	// Executa o comando
	var comando = "BuscaReferenciadoDWR." + listaParametros["nomeMetodo"] + "('"+ strCodPlano +  "',{ callback:function(listaRetorno){ DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", {\"\":\"Selecione...\"}); DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", listaRetorno, \"" + listaParametros["atributoValue"] + "\", \"" + listaParametros["atributoDescricao"] + "\");cursor_clear();}, errorHandler:function(errorString, exception) { setMsgError(errorString, 'block'); }, timeout:60000});";
	eval(comando);
}


function carregarComboEspecialidade(listaParametros){
	DWRUtil.useLoadingMessage("Carregando");
	DWRUtil.removeAllOptions(listaParametros["nomeCombo"]);
	
	var rn167 = 'N';
	var strCodPlano = document.getElementById('idPlanos').value;
	
	// Executa o comando
	var comando = "BuscaReferenciadoDWR." + listaParametros["nomeMetodo"] + "('"+ rn167 +"','" + strCodPlano +  "','" + listaParametros["parametro"] + "',{ callback:function(listaRetorno){ DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", {\"\":\"Selecione...\"}); DWRUtil.addOptions(\"" + listaParametros["nomeCombo"] + "\", listaRetorno, \"" + listaParametros["atributoValue"] + "\", \"" + listaParametros["atributoDescricao"] + "\");cursor_clear();}, errorHandler:function(errorString, exception) { setMsgError(errorString, 'block'); }, timeout:60000});";
	eval(comando);
}

function clearForm() {
	
	var frmBusca = getFormBusca();
	if (frmBusca != null && frmBusca != 'undefined') {
		frmBusca.reset();
		frmBusca.uf[0].selected = "selected";
		if (frmBusca.nomeReferenciado != null) {
			frmBusca.nomeReferenciado.value = "";
		}
		DWRUtil.removeAllOptions('cidade');
	}
}

function handleFields(radioField) {

	if (radioField.value == "nome") {
		document.getElementById('tipoServico').options[0].selected = true;
		document.getElementById('especialidade').options[0].selected = true;
		document.getElementById('tipoServico').disabled = true;
		document.getElementById('especialidade').disabled = true;
		document.getElementById('nomeReferenciado').disabled = false;
		document.getElementById('nomeReferenciado').focus();
	} else {
		document.getElementById('tipoServico').disabled = false;
		document.getElementById('especialidade').disabled = false;
		document.getElementById('nomeReferenciado').value = "";
		document.getElementById('nomeReferenciado').disabled = true;
		document.getElementById('tipoServico').focus();
	}
}

function verificarCampos() {

	var radioField = document.getElementById('pesquisa');
	
        document.getElementById('tipoServico').options[0].selected = true;
		document.getElementById('especialidade').options[0].selected = true;
		document.getElementById('tipoServico').disabled = true;
		document.getElementById('especialidade').disabled = true;
		document.getElementById('nomeReferenciado').disabled = false;
		document.getElementById('nomeReferenciado').focus();
		document.buscaReferenciadoForm.pesquisa[0].checked = true;
	
	var uf = document.getElementById('uf');
	
	if (uf.value != "") {
		DWRUtil.useLoadingMessage("Carregando");
		DWRUtil.removeAllOptions('cidade');
	
		// Executa o comando
		var comando = "BuscaReferenciadoDWR.listarCidade" +  "('" + uf.value + "',{ callback:function(listaRetorno){ DWRUtil.addOptions(\"cidade\", {\"\":\"Selecione...\"}); DWRUtil.addOptions(\"cidade\", listaRetorno, \"descricaoCidade\", \"descricaoCidade\");cursor_clear();marcarCidade();}, errorHandler:function(errorString, exception) { setMsgError(errorString, 'block'); }, timeout:60000});";
		eval(comando);
	}
	uf.focus();
}

function marcarCidade() {

	var cidadeSelecionada = document.getElementById('cidadeSelecionada');
	var cidade = document.getElementById('cidade');
	cidade.selectedIndex = cidadeSelecionada.value;
}

/*
* Fun��o para controlar a cidade que foi selecioanda
*/
function marcarCidadeSelecionada(cidade) {
	var cidadeSelecionada = document.getElementById('cidadeSelecionada');
	cidadeSelecionada.value = cidade;
}

/*
* Fun��o que verifica qual ser� o pr�ximo campo a ser focado.
*/
function focarCampo() {

	var nome = document.getElementById('nomeReferenciado');
	var tipo = document.getElementById('tipoServico');
	
	if (!nome.disabled) {
		nome.focus();
	}
	if (!tipo.disabled) {
		tipo.focus();
	}
}

/**
* Fun��o que faz a verifica��o de referenciados e valida��es de telas 
*/
function verificarReferenciados(nomeBairro, nomeDiv, nomeImg, url) {

    
	var divObject = document.getElementById(nomeDiv);
	var imgObject = document.getElementById(nomeImg);
	var minus = location.protocol +"//"+ location.host + url + '/images/pt_BR/minus.gif';
	var plus = location.protocol +"//"+ location.host + url + '/images/pt_BR/plus.gif';
	
	if (divObject.innerHTML == "") {
		divObject.style.display = 'inline';
		//imgObject.src = 'images/pt_BR/minus.gif';
		imgObject.src = minus;
		listarReferenciados(nomeBairro, nomeDiv);
	} else if (divObject.style.display == 'inline' && divObject.innerHTML != "") {
		divObject.style.display = 'none';
		//imgObject.src = 'images/pt_BR/plus.gif';
		imgObject.src = plus;
	} else if (divObject.innerHTML != "" && divObject.style.display == 'none') {
		divObject.style.display = 'inline';
		//imgObject.src = 'images/pt_BR/minus.gif';
		imgObject.src = minus;
	}
}

/**
* Fun��o que obt�m a lista de referenciados conforme o bairro passado por par�metro
*/
function listarReferenciados(nomeBairro, nomeDiv) {
	DWRUtil.useLoadingMessage("Carregando");

	var nomeReferenciado = document.getElementById('nomeReferenciado').value;
	var uf = document.getElementById('uf').value;
	var cidade = document.getElementById('cidade').value;
	var tipoServico = document.getElementById('tipoServico').value;
	var especialidade = document.getElementById('especialidade').value;
	var tipoPesquisa = document.getElementById('pesquisa').value;
	var tipoReferenciado = document.getElementById('tpPesq').value;
	var codigoRede = document.getElementById('cdRede').value;
	var codigoRegiao = document.getElementById('cdRegiao').value;
	var rn167 = document.getElementById('rn167').value;
	var tipoAcomodacao = document.getElementById('cdAcomodacao').value;
	var nroCartao = document.getElementById('nroCartao').value;
	var strCodPlano = document.getElementById('strCodPlano').value;
	var numControle = document.getElementById('nroControle').value;
		
	if(document.getElementById('codUsuario').value == null){
	   codUsuario = "0";
	}else{
	   codUsuario = document.getElementById('codUsuario').value;
	}

	BuscaReferenciadoDWR.listarReferenciadosPorBairro(nroCartao, nomeBairro.toUpperCase(), nomeReferenciado, uf, cidade, tipoServico, especialidade, tipoPesquisa, tipoReferenciado, codigoRede, codigoRegiao, rn167, tipoAcomodacao, strCodPlano,numControle,codUsuario, function(listarReferenciadosPorBairro){
		DWRUtil.setValue(nomeDiv, listarReferenciadosPorBairro);
	});
}

function validaNome(event, keyRE) {
	
	if ((typeof(event.keyCode) != 'undefined' && event.keyCode == 8) || event.keyCode == 9 || event.keyCode == 13) {
		return true;
	}
	
    if (( typeof(event.keyCode) != 'undefined' && event.keyCode > 0 && String.fromCharCode(event.keyCode).search(keyRE) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode > 0 && String.fromCharCode(event.charCode).search(keyRE) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode != event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13|45|46|35|36|37|39)$/) != (-1) ) ||
        ( typeof(event.charCode) != 'undefined' && event.charCode == event.keyCode && typeof(event.keyCode) != 'undefined' && event.keyCode.toString().search(/^(8|9|13)$/) != (-1) ) ) {
        return true;
    } else {
        return false;
    }
}

function getFormBusca() {
	return document.getElementById('buscaReferenciadoForm');
}

function getObject(objectName) {
	return document.getElementById(objectName);
}

function cursor_wait() {
  document.body.style.cursor = 'wait';
}

function cursor_clear() {
  document.body.style.cursor = 'default';
}

function doInicio(url) {
	
	var form = document.getElementById('buscaReferenciadoForm');
	alert(form);
	form.action = location.protocol +"//"+ location.host + url + "/buscaReferenciado.do";
	location.href = location.protocol +"//"+ location.host + url + "/buscaReferenciado.do";
	//form.action = "buscaReferenciado.do";
	//alert(form.action);
	//form.submit();
}

function doExport(url) {
	
	var form = document.getElementById('buscaReferenciadoForm');
	form.action = url + "/solicitarRelatorio.do";
	form.submit();
}
function doNovaBusca(url) {
	
	var form = document.getElementById('buscaReferenciadoForm');
	form.action = url + "/buscaReferenciado.do";
	document.getElementById('pesquisa').value = "";
	form.submit();
}

/**
* Fun��o que faz o redirecionamento para a p�gina de listagem de download
*/
function redirectDownload(url) {
	window.setTimeout(function urlRedirect(){window.parent.location = url;}, 3000);
}

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 consultaRede(cdRede,cdRegiao,NmRede,Site,Servico,TipoServico,tpPesq,portal,rn167,url) {	
	
	//var url = '../../../../';
	
	url += "PCBS-BuscaReferenciadoRAD/buscaRedeReferenciada.do?NOVO_PORTAL_DENTAL=N";
	url += "&tpPesq=M";
	url += "&cdRede=" + cdRede;
	url += "&cdRegiao=" + cdRegiao; 
	url += "&NmRede=" + NmRede;
	url += "&site=" + Site; 
	url += "&Servico=" + Servico;
	url += "&TipoServico=" + TipoServico;
	url += "&portal=" + portal;
	url += "&rn167=" + rn167; 
			
	var width = 600;
	var height = 529;
	
	var left = 100;
	var top = 100;
	
	window.open(url, 'RedeReferenciada', 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=yes,status=no,toolbar=no,location=no');
	

}
