var gDS_ALERTA_UF_RECUSADO_JS1 = "";
var gDS_ALERTA_UF_RECUSADO_JS2 = "";
var gufrecusado = "";

// temporario para o Canavial
var gpagreg = "";

function Confirma(msg)
{
	return confirm(msg);
}

function CheckLength(TargetObject, tam)
{			
	LenString = TargetObject.value.length;
	if (LenString > tam)
	{
		//alert("Ultrapassou tamanho máximo.");
		TargetObject.value = TargetObject.value.substring(0,tam);
	}
}

function MostrarTooltip(idObject, offsetX, offsetY)
{
	MostrarTooltip(idObject, offsetX, offsetY, 0);
}

function MostrarTooltip(idObject, offsetX, offsetY, width)
{ 			
	var obj = document.getElementById(idObject);
	
	if (obj.disabled)
		return;
	
	document.getElementById("tooltip").innerHTML = obj.options[obj.selectedIndex].text;

	if(obj.options[obj.selectedIndex].value !="") 
	{
		var lobjCurrentDropdown = window.event.srcElement;
		posX = getX(lobjCurrentDropdown);
		posY = getY(lobjCurrentDropdown);
 		document.getElementById("tooltip").style.zIndex = 9999999; 
  		document.getElementById("tooltip").style.display = "inline"; 
  		document.getElementById("tooltip").style.position = "absolute"; 
 		document.getElementById("tooltip").style.border = "ActiveBorder 1px solid"; 
		document.getElementById("tooltip").style.padding = "3px";
		document.getElementById("tooltip").style.fontSize = "10px";
		document.getElementById("tooltip").style.fontFamily = "Verdana";
		document.getElementById("tooltip").style.backgroundColor = "LemonChiffon";
		document.getElementById("tooltip").style.top = posY + offsetY;
		document.getElementById("tooltip").style.left =	posX + offsetX;
		if (width>0)
			document.getElementById("tooltip").style.width = width;
	} 
} 

function OcultarTooltip()
{ 
	document.getElementById("tooltip").style.display = "none"; 
}

function getX(obj)
{
	return( obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft+getX(obj.offsetParent) );
}

function getY(obj)
{
	return( obj.offsetParent==null ? obj.offsetTop : obj.offsetTop+getY(obj.offsetParent) );
}

function UpperCase()
{
	key = window.event.keyCode;
	if ((key > 96 && key < 123) || (key > 223 && key < 247) || (key > 248 && key < 254))
		window.event.keyCode = key - 32;
		
	if (key==255 || key==124)
		window.event.keyCode = ''; // para não dar problema no Opus			
}

function ObtemNomeProponente(msg, errodoc, pagreg)
{
	var doc = document.getElementById("txtDs_CPF_CNPJ").value;
	var tpF = document.getElementById("optDs_tp_pessoa_0").checked;
	var tpJ = document.getElementById("optDs_tp_pessoa_1").checked;
	
	if (doc!="" && tpF!=undefined && tpJ!=undefined)
	{
		if (!VerificaCpfCnpj(doc, tpF?1:2))
		{
			document.getElementById("lblMsgConsultaCPFCNPJ").innerHTML = errodoc;
			return;
		}				
		
		document.getElementById("lblMsgConsultaCPFCNPJ").innerHTML = msg;
		document.body.style.cursor = 'wait';
		eval(pagreg + ".ObtemNomeProponente(tpF?'F':'J', doc, ObtemNomeProponente_CallBack)");
	}
}
function ValidaGPS(pagreg)
{
	var Cd_municipio_prop_rural = document.getElementById("cboCd_municipio_prop_rural").value;
	var GPS_propriedade_graus_S = document.getElementById("txtNr_GPS_propriedade_graus_S").value;
	var GPS_propriedade_minutos_S = document.getElementById("txtNr_GPS_propriedade_minutos_S").value;
	var GPS_propriedade_segundos_S = document.getElementById("txtNr_GPS_propriedade_segundos_S").value;
	var GPS_propriedade_graus_W = document.getElementById("txtNr_GPS_propriedade_graus_W").value;
	var GPS_propriedade_minutos_W = document.getElementById("txtNr_GPS_propriedade_minutos_W").value;
	var GPS_propriedade_segundos_W = document.getElementById("txtNr_GPS_propriedade_segundos_W").value;
	var GPS = document.getElementById("txtGPS").value;
	if (Cd_municipio_prop_rural!="" && GPS_propriedade_graus_S!="" && GPS_propriedade_minutos_S!="" && GPS_propriedade_segundos_S!="" && GPS_propriedade_graus_W!="" && GPS_propriedade_minutos_W!="" && GPS_propriedade_segundos_W!="" && GPS_propriedade_graus_S!="")
	{
		eval(pagreg + ".ValidaGPS(Cd_municipio_prop_rural,GPS_propriedade_graus_S, GPS_propriedade_minutos_S, GPS_propriedade_segundos_S,GPS_propriedade_graus_W, GPS_propriedade_minutos_W,GPS_propriedade_segundos_W, ValidaGPS_CallBack)");
	}
}

function ValidaGPS_CallBack(response)
{
	document.body.style.cursor = 'default';
	document.getElementById("lblMsgGPS").innerHTML = "";

	if (response.error != null)
	{
		//alert(response.error);
		document.getElementById("lblMsgGPS").innerHTML = response.error;
	
		return;
	}

	document.getElementById("txtGPS").value = response.value;
	
	return;

}
function ObtemNomeBeneficiario(msg, errodoc, pagreg, i)
{
	var doc = eval("document.getElementById('txtDs_CPF_CNPJ_beneficiario" + i + "').value");
	var tpF = eval("document.getElementById('optDs_tp_pessoa_beneficiario" + i + "_0').checked");
	var tpJ = eval("document.getElementById('optDs_tp_pessoa_beneficiario" + i + "_1').checked");
	var lblMsgConsultaCPFCNPJ = eval("document.getElementById('lblMsgConsultaCPFCNPJBeneficiario" + i + "')");
	
	if (doc!="" && tpF!=undefined && tpJ!=undefined)
	{
		if (!VerificaCpfCnpj(doc, tpF?1:2))
		{
			lblMsgConsultaCPFCNPJ.innerHTML = errodoc;
			return;
		}				
		
		lblMsgConsultaCPFCNPJ.innerHTML = msg;
		document.body.style.cursor = 'wait';
		eval(pagreg + ".ObtemNomeProponente(tpF?'F':'J', doc, ObtemNomeBeneficiario" + i + "_CallBack)");
	}
}

function ObtemNomeBeneficiario1_CallBack(response)
{
	ObtemNomeBeneficiario_CallBack(response, 1);
}
function ObtemNomeBeneficiario2_CallBack(response)
{
	ObtemNomeBeneficiario_CallBack(response, 2);
}

function ObtemNomeBeneficiario_CallBack(response, i)
{
	var lblMsgConsultaCPFCNPJ = eval("document.getElementById('lblMsgConsultaCPFCNPJBeneficiario" + i + "')");
	var txtDs_nome_beneficiario = eval("document.getElementById('txtDs_nome_beneficiario" + i + "')");
	var txtDs_CPF_CNPJ = eval("document.getElementById('txtDs_CPF_CNPJ_beneficiario" + i + "')");
	
	document.body.style.cursor = 'default';
	lblMsgConsultaCPFCNPJ.innerHTML = "";

	if (response.error != null)
	{
		//alert(response.error);
		lblMsgConsultaCPFCNPJ.innerHTML = response.error;
		
		// erro no ajax, habilita o campo para preenchimento manual
		if (txtDs_nome_proponente.readOnly)
		{
			txtDs_nome_beneficiario.value = "";
			txtDs_nome_beneficiario.readOnly = false;
			txtDs_nome_beneficiario.focus();
			
			txtDs_nome_beneficiario.style.backgroundColor = txtDs_CPF_CNPJ.style.backgroundColor;
			txtDs_nome_beneficiario.style.borderStyle = txtDs_CPF_CNPJ.style.borderStyle;
			txtDs_nome_beneficiario.style.zIndex = "1";
			txtDs_nome_beneficiario.tabIndex = "0";
		}
		
		if (txtDs_nome_beneficiario.value == "")
			txtDs_nome_beneficiario.focus();
		
		return;
	}
		
	var resp = response.value;
	if (resp[0]=="") // Não deu erro na consulta ao documento
	{
		if (resp[1]!="")
		{
			txtDs_nome_beneficiario.value = resp[1];
			txtDs_nome_beneficiario.readOnly = true;
			
			txtDs_nome_beneficiario.style.backgroundColor = "Transparent";
			txtDs_nome_beneficiario.style.borderStyle = "None";
			txtDs_nome_beneficiario.style.zIndex = "100";
			txtDs_nome_beneficiario.tabIndex = "-1";
		}
		else // Não retornou valor, habilita o campo para preenchimento manual
		{
			if (txtDs_nome_beneficiario.readOnly)
			{
				txtDs_nome_beneficiario.value = "";
				txtDs_nome_beneficiario.readOnly = false;
				txtDs_nome_beneficiario.focus();
				
				txtDs_nome_beneficiario.style.backgroundColor = txtDs_CPF_CNPJ.style.backgroundColor;
				txtDs_nome_beneficiario.style.borderStyle = txtDs_CPF_CNPJ.style.borderStyle;
				txtDs_nome_beneficiario.style.zIndex = "1";
				txtDs_nome_beneficiario.tabIndex = "0";
			}
			
			if (txtDs_nome_beneficiario.value == "")
				txtDs_nome_beneficiario.focus();
		}
	}
	else // Erro
	{
		if (txtDs_nome_beneficiario.readOnly)
		{
			txtDs_nome_beneficiario.value = "";
			txtDs_nome_beneficiario.readOnly = false;
			txtDs_nome_beneficiario.focus();
			
			txtDs_nome_beneficiario.style.backgroundColor = txtDs_CPF_CNPJ.style.backgroundColor;
			txtDs_nome_beneficiario.style.borderStyle = txtDs_CPF_CNPJ.style.borderStyle;
			txtDs_nome_beneficiario.style.zIndex = "1";
			txtDs_nome_beneficiario.tabIndex = "0";
		}
		
		if (txtDs_nome_beneficiario.value == "")
			txtDs_nome_beneficiario.focus();
				
		lblMsgConsultaCPFCNPJ.innerHTML = resp[1];
	}
}


function ObtemNomeProponente_CallBack(response)
{
	document.body.style.cursor = 'default';
	document.getElementById("lblMsgConsultaCPFCNPJ").innerHTML = "";

	if (response.error != null)
	{
		//alert(response.error);
		document.getElementById("lblMsgConsultaCPFCNPJ").innerHTML = response.error;
		
		// erro no ajax, habilita o campo para preenchimento manual
		if (document.getElementById("txtDs_nome_proponente").readOnly)
		{
			document.getElementById("txtDs_nome_proponente").value = "";
			document.getElementById("txtDs_nome_proponente").readOnly = false;
			document.getElementById("txtDs_nome_proponente").focus();
			
			document.getElementById("txtDs_nome_proponente").style.backgroundColor = document.getElementById("txtDs_CPF_CNPJ").style.backgroundColor;
			document.getElementById("txtDs_nome_proponente").style.borderStyle = document.getElementById("txtDs_CPF_CNPJ").style.borderStyle;
			document.getElementById("txtDs_nome_proponente").style.zIndex = "1";
			document.getElementById("txtDs_nome_proponente").tabIndex = "0";
		}
		
		if (document.getElementById("txtDs_nome_proponente").value == "")
			document.getElementById("txtDs_nome_proponente").focus();
		
		return;
	}
		
	var resp = response.value;
	if (resp[0]=="") // Não deu erro na consulta ao documento
	{
		if (resp[1]!="")
		{
			document.getElementById("txtDs_nome_proponente").value = resp[1];
			document.getElementById("txtDs_nome_proponente").readOnly = true;
			
			document.getElementById("txtDs_nome_proponente").style.backgroundColor = "Transparent";
			document.getElementById("txtDs_nome_proponente").style.borderStyle = "None";
			document.getElementById("txtDs_nome_proponente").style.zIndex = "100";
			document.getElementById("txtDs_nome_proponente").tabIndex = "-1";
		}
		else // Não retornou valor, habilita o campo para preenchimento manual
		{
			if (document.getElementById("txtDs_nome_proponente").readOnly)
			{
				document.getElementById("txtDs_nome_proponente").value = "";
				document.getElementById("txtDs_nome_proponente").readOnly = false;
				document.getElementById("txtDs_nome_proponente").focus();
				
				document.getElementById("txtDs_nome_proponente").style.backgroundColor = document.getElementById("txtDs_CPF_CNPJ").style.backgroundColor;
				document.getElementById("txtDs_nome_proponente").style.borderStyle = document.getElementById("txtDs_CPF_CNPJ").style.borderStyle;
				document.getElementById("txtDs_nome_proponente").style.zIndex = "1";
				document.getElementById("txtDs_nome_proponente").tabIndex = "0";
			}
			
			if (document.getElementById("txtDs_nome_proponente").value == "")
				document.getElementById("txtDs_nome_proponente").focus();
		}
	}
	else // Erro
	{
		if (document.getElementById("txtDs_nome_proponente").readOnly)
		{
			document.getElementById("txtDs_nome_proponente").value = "";
			document.getElementById("txtDs_nome_proponente").readOnly = false;
			document.getElementById("txtDs_nome_proponente").focus();
			
			document.getElementById("txtDs_nome_proponente").style.backgroundColor = document.getElementById("txtDs_CPF_CNPJ").style.backgroundColor;
			document.getElementById("txtDs_nome_proponente").style.borderStyle = document.getElementById("txtDs_CPF_CNPJ").style.borderStyle;
			document.getElementById("txtDs_nome_proponente").style.zIndex = "1";
			document.getElementById("txtDs_nome_proponente").tabIndex = "0";
		}
		
		if (document.getElementById("txtDs_nome_proponente").value == "")
			document.getElementById("txtDs_nome_proponente").focus();
				
		document.getElementById("lblMsgConsultaCPFCNPJ").innerHTML = resp[1];
	}
}

function ObtemLogradouroPeloCEPBeneficiario(msg, i, pagreg)
{
	var lblMsgConsultaCEP = eval("document.getElementById('lblMsgConsultaCEPBeneficiario" + i + "')");
	var txtNr_CEP = eval("document.getElementById('txtNr_CEP_beneficiario" + i + "')");

	var cep = txtNr_CEP.value;
	
	if (cep!="")
	{
		if (cep.length>=8)
		{
			document.body.style.cursor = 'wait';

			lblMsgConsultaCEP.innerHTML = msg;
			eval(pagreg + ".ObtemLogradouroPeloCEP(cep, ObtemLogradouroPeloCEPBeneficiario" + i + "_CallBack)");
		}
	}
}

function ObtemLogradouroPeloCEP(msg, qual, pagreg, msgrecusado_parte1, msgrecusado_parte2, ufrecusado)
{
	var cep = "";
	
	if (qual==1)
		cep = document.getElementById("txtNr_CEP").value;
	else if (qual==2)
		cep = document.getElementById("txtNr_CEP_prop_rural").value;
	
	if (cep!="")
	{
		if (cep.length>=8)
		{
			document.body.style.cursor = 'wait';
			if (qual==1)
			{
				// temporario para o Canavial, mudar depois que migrar para o VS2005
				gpagreg = pagreg;
				
				document.getElementById("lblMsgConsultaCEP").innerHTML = msg;
				eval(pagreg + ".ObtemLogradouroPeloCEP(cep, ObtemLogradouroPeloCEP_CallBack)");
			}
			else if (qual==2)
			{
				gDS_ALERTA_UF_RECUSADO_JS1 = msgrecusado_parte1;
				gDS_ALERTA_UF_RECUSADO_JS2 = msgrecusado_parte2;
				gufrecusado = ufrecusado;
			
				document.getElementById("lblMsgConsultaCEP_prop_rural").innerHTML = msg;
				eval(pagreg + ".ObtemLogradouroPeloCEP(cep, ObtemLogradouroPeloCEP_prop_rural_CallBack)");
			}
		}
	}
}

function ObtemLogradouroPeloCEP_CallBack(response)
{
	document.getElementById("txtDs_logradouro").value = "";
	document.getElementById("txtDs_bairro").value = "";
	document.getElementById("cboCd_UF").selectedIndex = 0;
	document.getElementById("cboCd_Municipio").innerHTML = "";

	if (response.error != null)
	{
		document.body.style.cursor = 'default';
		document.getElementById("lblMsgConsultaCEP").innerHTML = response.error;
		document.getElementById("txtDs_logradouro").focus();

		return;
	}

	var ds = response.value;
	if (ds!=null && typeof(ds)=="object" && ds.Tables!=null)
	{
		var TPO_LOGRADOURO = "";
		var NME_LOGRADOURO = "";
		var NME_LOCALIDADE = "";
		var BAIRRO_INICIAL = "";
		var SGA_UF = "";
		
		if (ds.Tables[0].Rows.length>0)
		{
			TPO_LOGRADOURO = ds.Tables[0].Rows[0].TPO_LOGRADOURO==null?"":ds.Tables[0].Rows[0].TPO_LOGRADOURO;
			NME_LOGRADOURO = ds.Tables[0].Rows[0].NME_LOGRADOURO==null?"":ds.Tables[0].Rows[0].NME_LOGRADOURO;
			NME_LOCALIDADE = ds.Tables[0].Rows[0].NME_LOCALIDADE==null?"":ds.Tables[0].Rows[0].NME_LOCALIDADE;
			BAIRRO_INICIAL = ds.Tables[0].Rows[0].BAIRRO_INICIAL==null?"":ds.Tables[0].Rows[0].BAIRRO_INICIAL;
			SGA_UF = ds.Tables[0].Rows[0].SGA_UF==null?"":ds.Tables[0].Rows[0].SGA_UF;
		
			document.getElementById("txtDs_logradouro").value = TPO_LOGRADOURO + " " + NME_LOGRADOURO;
			document.getElementById("txtDs_bairro").value = BAIRRO_INICIAL;
			// { seleciona a UF
			for (j=0;j<document.getElementById("cboCd_UF").options.length;j++)
			{
				if (document.getElementById("cboCd_UF").options[j].value==SGA_UF)
				{
					document.getElementById("cboCd_UF").options[j].selected = true;	
					break;
				}
			}
			// }
			
			// temporario para o Canavial
			if (gpagreg=="frmQARCanaOpFin")
				__doPostBack('lnk_cdUF_postback', '');
		}

		document.getElementById("cboCd_Municipio").innerHTML = "";
		if (ds.Tables.length>1)
		{
			var ID_seleciona = 0;
			for(var i=0; i<ds.Tables[1].Rows.length; i++)
			{
				document.getElementById("cboCd_Municipio").options[i] = new Option(ds.Tables[1].Rows[i].Ds_municipio, ds.Tables[1].Rows[i].Cd_municipio);
				if (ds.Tables[1].Rows[i].Ds_municipio == NME_LOCALIDADE)
					ID_seleciona = i;
			}
			document.getElementById("cboCd_Municipio").selectedIndex = ID_seleciona;
		}
		
		document.getElementById("txtDs_logradouro").focus();
		if (TPO_LOGRADOURO!="" || NME_LOGRADOURO!="")
			document.getElementById("txtDs_nr_lograd").focus();
	}

	document.body.style.cursor = 'default';
	document.getElementById("lblMsgConsultaCEP").innerHTML = "";
}

function ObtemLogradouroPeloCEPBeneficiario1_CallBack(response)
{
	ObtemLogradouroPeloCEPBeneficiario_CallBack(response, 1);
}
function ObtemLogradouroPeloCEPBeneficiario2_CallBack(response)
{
	ObtemLogradouroPeloCEPBeneficiario_CallBack(response, 2);
}

function ObtemLogradouroPeloCEPBeneficiario_CallBack(response, i)
{
	var txtDs_logradouro = eval("document.getElementById('txtDs_logradouro_beneficiario" + i + "')");
	var txtDs_nr_lograd = eval("document.getElementById('txtDs_nr_lograd_beneficiario" + i + "')");
	var txtDs_bairro = eval("document.getElementById('txtDs_bairro_beneficiario" + i + "')");
	var cboCd_UF = eval("document.getElementById('cboCd_UF_beneficiario" + i + "')");
	var cboCd_Municipio = eval("document.getElementById('cboCd_Municipio_beneficiario" + i + "')");
	var lblMsgConsultaCEP = eval("document.getElementById('lblMsgConsultaCEPBeneficiario" + i + "')");
	
	txtDs_logradouro.value = "";
	txtDs_bairro.value = "";
	cboCd_UF.selectedIndex = 0;
	cboCd_Municipio.innerHTML = "";

	if (response.error != null)
	{
		document.body.style.cursor = 'default';
		lblMsgConsultaCEP.innerHTML = response.error;
		txtDs_logradouro.focus();

		return;
	}

	var ds = response.value;
	if (ds!=null && typeof(ds)=="object" && ds.Tables!=null)
	{
		var TPO_LOGRADOURO = "";
		var NME_LOGRADOURO = "";
		var NME_LOCALIDADE = "";
		var BAIRRO_INICIAL = "";
		var SGA_UF = "";
		
		if (ds.Tables[0].Rows.length>0)
		{
			TPO_LOGRADOURO = ds.Tables[0].Rows[0].TPO_LOGRADOURO==null?"":ds.Tables[0].Rows[0].TPO_LOGRADOURO;
			NME_LOGRADOURO = ds.Tables[0].Rows[0].NME_LOGRADOURO==null?"":ds.Tables[0].Rows[0].NME_LOGRADOURO;
			NME_LOCALIDADE = ds.Tables[0].Rows[0].NME_LOCALIDADE==null?"":ds.Tables[0].Rows[0].NME_LOCALIDADE;
			BAIRRO_INICIAL = ds.Tables[0].Rows[0].BAIRRO_INICIAL==null?"":ds.Tables[0].Rows[0].BAIRRO_INICIAL;
			SGA_UF = ds.Tables[0].Rows[0].SGA_UF==null?"":ds.Tables[0].Rows[0].SGA_UF;
		
			txtDs_logradouro.value = TPO_LOGRADOURO + " " + NME_LOGRADOURO;
			txtDs_bairro.value = BAIRRO_INICIAL;
			// { seleciona a UF
			for (j=0;j<cboCd_UF.options.length;j++)
			{
				if (cboCd_UF.options[j].value==SGA_UF)
				{
					cboCd_UF.options[j].selected = true;	
					break;
				}
			}
			// }
		}

		cboCd_Municipio.innerHTML = "";
		if (ds.Tables.length>1)
		{
			var ID_seleciona = 0;
			for(var i=0; i<ds.Tables[1].Rows.length; i++)
			{
				cboCd_Municipio.options[i] = new Option(ds.Tables[1].Rows[i].Ds_municipio, ds.Tables[1].Rows[i].Cd_municipio);
				if (ds.Tables[1].Rows[i].Ds_municipio == NME_LOCALIDADE)
					ID_seleciona = i;
			}
			cboCd_Municipio.selectedIndex = ID_seleciona;
		}
		
		txtDs_logradouro.focus();
		if (TPO_LOGRADOURO!="" || NME_LOGRADOURO!="")
			txtDs_nr_lograd.focus();
	}

	document.body.style.cursor = 'default';
	lblMsgConsultaCEP.innerHTML = "";
}

function ObtemLogradouroPeloCEP_prop_rural_CallBack(response)
{
	document.getElementById("txtDs_lograd_prop_rural").value = "";
	document.getElementById("txtDs_bairro_prop_rural").value = "";
	document.getElementById("cboCd_UF_prop_rural").selectedIndex = 0;
	document.getElementById("cboCd_municipio_prop_rural").innerHTML = "";

	if (response.error != null)
	{
		document.body.style.cursor = 'default';
		document.getElementById("lblMsgConsultaCEP_prop_rural").innerHTML = response.error;
		document.getElementById("txtDs_lograd_prop_rural").focus();
	
		return;
	}

	var ds = response.value;
	if (ds!=null && typeof(ds)=="object" && ds.Tables!=null)
	{
		var TPO_LOGRADOURO = "";
		var NME_LOGRADOURO = "";
		var NME_LOCALIDADE = "";
		var BAIRRO_INICIAL = "";
		var SGA_UF = "";
		
		var mostra_alerta_uf_recusado = false;
		
		if (ds.Tables[0].Rows.length>0)
		{
			TPO_LOGRADOURO = ds.Tables[0].Rows[0].TPO_LOGRADOURO==null?"":ds.Tables[0].Rows[0].TPO_LOGRADOURO;
			NME_LOGRADOURO = ds.Tables[0].Rows[0].NME_LOGRADOURO==null?"":ds.Tables[0].Rows[0].NME_LOGRADOURO;
			NME_LOCALIDADE = ds.Tables[0].Rows[0].NME_LOCALIDADE==null?"":ds.Tables[0].Rows[0].NME_LOCALIDADE;
			BAIRRO_INICIAL = ds.Tables[0].Rows[0].BAIRRO_INICIAL==null?"":ds.Tables[0].Rows[0].BAIRRO_INICIAL;
			SGA_UF = ds.Tables[0].Rows[0].SGA_UF==null?"":ds.Tables[0].Rows[0].SGA_UF;
		
			if (gufrecusado!=null)
			{
				for (i=0; i<gufrecusado.length; i++)
				{
					if (SGA_UF == gufrecusado[i])
					{
						mostra_alerta_uf_recusado = true;
						break;
					}
				}
			}
		
			document.getElementById("txtDs_lograd_prop_rural").value = TPO_LOGRADOURO + " " + NME_LOGRADOURO;
			document.getElementById("txtDs_bairro_prop_rural").value = BAIRRO_INICIAL;
			// { seleciona a UF
			for (j=0;j<document.getElementById("cboCd_UF_prop_rural").options.length;j++)
			{
				if (document.getElementById("cboCd_UF_prop_rural").options[j].value==SGA_UF)
				{
					document.getElementById("cboCd_UF_prop_rural").options[j].selected = true;
					break;
				}
			}
			// }
		}
		
		document.getElementById("cboCd_Municipio_prop_rural").innerHTML = "";		
		if (ds.Tables.length>1)
		{
			var ID_seleciona = 0;
			for(var i=0; i<ds.Tables[1].Rows.length; i++)
			{
				document.getElementById("cboCd_Municipio_prop_rural").options[i] = new Option(ds.Tables[1].Rows[i].Ds_municipio, ds.Tables[1].Rows[i].Cd_municipio);
				if (ds.Tables[1].Rows[i].Ds_municipio == NME_LOCALIDADE)
					ID_seleciona = i;
			}
			document.getElementById("cboCd_Municipio_prop_rural").selectedIndex = ID_seleciona;
		}
		
		document.getElementById("txtDs_lograd_prop_rural").focus();
		if (TPO_LOGRADOURO!="" || NME_LOGRADOURO!="")
			document.getElementById("txtDs_nr_prop_rural").focus();
			
		if (mostra_alerta_uf_recusado)
			alert(gDS_ALERTA_UF_RECUSADO_JS1 + " '" + SGA_UF + "'." + gDS_ALERTA_UF_RECUSADO_JS2);
	}
	
	document.body.style.cursor = 'default';
	document.getElementById("lblMsgConsultaCEP_prop_rural").innerHTML = "";
}

function FillMunicipioBeneficiario(msg, i, pagreg)
{
	var uf;
	var cboCd_UF = eval("document.getElementById('cboCd_UF_beneficiario" + i + "')");
	var cboCd_Municipio = eval("document.getElementById('cboCd_Municipio_beneficiario" + i + "')");
	var lblMsgProcuraMunicipio = eval("document.getElementById('lblMsgProcuraMunicipioBeneficiario" + i + "')");
	
	if (cboCd_UF.selectedIndex==0)
	{
		cboCd_Municipio.innerHTML = "";
		return;
	}

	uf = cboCd_UF.options[cboCd_UF.selectedIndex].value;

	document.body.style.cursor = 'wait';
	lblMsgProcuraMunicipio.innerHTML = msg;
	cboCd_Municipio.disabled = true;
		
	try
	{
		eval(pagreg + ".FillMunicipio('" + uf + "', FillMunicipioBeneficiario" + i + "_CallBack);");
	}
	catch(err)
	{
		document.body.style.cursor = 'default';
		lblMsgProcuraMunicipio.innerHTML = "";
		cboCd_Municipio.disabled = false;
		//__doPostBack('lnk_cdUF_postback', '');
	}
}

function FillMunicipio(msg, qual, pagreg, msgrecusado_parte1, msgrecusado_parte2, ufrecusado)
{
	var uf;
	
	if (qual==1)
	{	
		if (document.getElementById("cboCd_UF").selectedIndex==0)
		{
			document.getElementById("cboCd_Municipio").innerHTML = "";
			return;
		}

		uf = document.getElementById("cboCd_UF").options[document.getElementById("cboCd_UF").selectedIndex].value;

		document.body.style.cursor = 'wait';
		document.getElementById("lblMsgProcuraMunicipio").innerHTML = msg;
		document.getElementById("cboCd_Municipio").disabled = true;
			
		try
		{
			eval(pagreg + ".FillMunicipio('" + uf + "', FillMunicipio_CallBack);");
		}
		catch(err)
		{
			document.body.style.cursor = 'default';
			document.getElementById("lblMsgProcuraMunicipio").innerHTML = "";
			document.getElementById("cboCd_Municipio").disabled = false;
			__doPostBack('lnk_cdUF_postback', '');
		}
	}
	else if (qual==2)
	{
		if (document.getElementById("cboCd_UF_prop_rural").selectedIndex==0)
		{
			document.getElementById("cboCd_municipio_prop_rural").innerHTML = "";
			
			return;
		}

		uf = document.getElementById("cboCd_UF_prop_rural").options[document.getElementById("cboCd_UF_prop_rural").selectedIndex].value;		

		if (ufrecusado!=null)
		{
			for (i=0; i<ufrecusado.length; i++)
			{
				if (uf == ufrecusado[i])
				{
					alert(msgrecusado_parte1 + " '" + uf + "'." + msgrecusado_parte2);
			
					break;
				}
			}
		}

		document.body.style.cursor = 'wait';
		document.getElementById("lblMsgProcuraMunicipioPropRural").innerHTML = msg;
		document.getElementById("cboCd_Municipio_prop_rural").disabled = true;
			
		try
		{
			eval(pagreg + ".FillMunicipio('" + uf + "', FillMunicipioPropRural_CallBack);");
		}
		catch(err)
		{
			document.body.style.cursor = 'default';
			document.getElementById("lblMsgProcuraMunicipioPropRural").innerHTML = "";
			document.getElementById("cboCd_municipio_prop_rural").disabled = false;
			__doPostBack('lnk_cdUF_prop_rural_postback','');
		}
	}
}

function FillMunicipioBeneficiario1_CallBack(response)
{
	FillMunicipioBeneficiario_CallBack(response, 1);
}
function FillMunicipioBeneficiario2_CallBack(response)
{
	FillMunicipioBeneficiario_CallBack(response, 2);
}

function FillMunicipioBeneficiario_CallBack(response, i)
{
	var lblMsgConsultaCEP = eval("document.getElementById('lblMsgConsultaCEPBeneficiario" + i + "')");
	var cboCd_Municipio = eval("document.getElementById('cboCd_Municipio_beneficiario" + i + "')");
	var lblMsgProcuraMunicipio = eval("document.getElementById('lblMsgProcuraMunicipioBeneficiario" + i + "')");

	if (response.error != null)
	{
		document.body.style.cursor = 'default';
		lblMsgProcuraMunicipio.innerHTML = "";
		cboCd_Municipio.disabled = false;
		
		return;
	}
	var ds = response.value;
	
	if (ds!=null && typeof(ds)=="object" && ds.Tables!=null)
	{
		cboCd_Municipio.innerHTML = "";
		
		for(var i=0; i<ds.Tables[0].Rows.length; i++)
		{
			cboCd_Municipio.options[i] = new Option(ds.Tables[0].Rows[i].Ds_municipio, ds.Tables[0].Rows[i].Cd_municipio);
		}
	}
	
	document.body.style.cursor = 'default';
	lblMsgProcuraMunicipio.innerHTML = "";
	lblMsgConsultaCEP.innerHTML = "";
	cboCd_Municipio.disabled = false;
}


function FillMunicipio_CallBack(response)
{
	if (response.error != null)
	{
		document.body.style.cursor = 'default';
		document.getElementById("lblMsgProcuraMunicipio").innerHTML = "";
		document.getElementById("cboCd_Municipio").disabled = false;
		
		return;
	}
	var ds = response.value;
	
	if (ds!=null && typeof(ds)=="object" && ds.Tables!=null)
	{
		document.getElementById("cboCd_Municipio").innerHTML = "";
		
		for(var i=0; i<ds.Tables[0].Rows.length; i++)
		{
			document.getElementById("cboCd_Municipio").options[i] = new Option(ds.Tables[0].Rows[i].Ds_municipio, ds.Tables[0].Rows[i].Cd_municipio);
		}
	}
	
	document.body.style.cursor = 'default';
	document.getElementById("lblMsgProcuraMunicipio").innerHTML = "";
	document.getElementById("lblMsgConsultaCEP").innerHTML = "";
	document.getElementById("cboCd_Municipio").disabled = false;
}

function FillMunicipioPropRural_CallBack(response)
{
	if (response.error != null)
	{
		alert(response.error);
		
		document.body.style.cursor = 'default';
		document.getElementById("lblMsgProcuraMunicipioPropRural").innerHTML = "";
		document.getElementById("cboCd_municipio_prop_rural").disabled = false;
		
		return;
	}
	var ds = response.value;
	
	if (ds!=null && typeof(ds)=="object" && ds.Tables!=null)
	{
		document.getElementById("cboCd_municipio_prop_rural").innerHTML = "";
		
		for(var i=0; i<ds.Tables[0].Rows.length; i++)
		{
			document.getElementById("cboCd_municipio_prop_rural").options[i] = new Option(ds.Tables[0].Rows[i].Ds_municipio, ds.Tables[0].Rows[i].Cd_municipio);
		}
	}
	
	document.body.style.cursor = 'default';
	document.getElementById("lblMsgProcuraMunicipioPropRural").innerHTML = "";
	document.getElementById("lblMsgConsultaCEP_prop_rural").innerHTML = "";
	document.getElementById("cboCd_municipio_prop_rural").disabled = false;
}