var objImoveis=null;
var vFrmFiltro="";

function initImoveis(data){
   objImoveis=new Imoveis();
   vFrmFiltro=document.frmFiltro;
}

function Imoveis()
{
	this.BASEXML="inc/xml/";
	this.XML="getXMLUTILImoveis.asp";
	this.Tipo='';
	this.De='';
	this.Ate='';
	this.Cidade='';
	this.Lote='';
	this.Retorno=0;
}

Imoveis.prototype.getURL=function(opt)
{ 
	if(opt.toUpperCase()!="NULL")
	{
		var vUrl=this.BASEXML;
		vUrl=vUrl+this.XML;
		vUrl=vUrl+"?dv="+opt+"&"+this.getParametros();
		return vUrl;
	}
}

Imoveis.prototype.getParametros=function()
{
	var pImoveis="tipo="+this.Tipo+"&de="+this.De;
	pImoveis=pImoveis+"&ate="+this.Ate+"&cid="+this.Cidade+"&lote="+this.Lote+"&Retorno="+this.Retorno+"&rdn="+Math.random();	
	return pImoveis;
}

Imoveis.prototype.setTipo=function(value){
	this.Tipo=value;	
}

Imoveis.prototype.setDe=function(value){
	this.De=value;	
}

Imoveis.prototype.setAte=function(value){
	this.Ate=value;	
}

Imoveis.prototype.setCidade=function(value){
	this.Cidade=value;	
}

Imoveis.prototype.setLote=function(value){
	this.Lote=value;	
}

function fPesquisarImoveis(){
	var vForm=vFrmFiltro;
	var varTipo=vForm.txtTipo.options[vForm.txtTipo.selectedIndex].value;	
	var varCidade=vForm.txtCidade.options[vForm.txtCidade.selectedIndex].value;	
	var varLote=vForm.txtLote.options[vForm.txtLote.selectedIndex].value;	
	var varDe=vForm.txtDe.value;	
	var varAte=vForm.txtAte.value;	

	/*if(varCidade=="0"){
		varCidade="";
	}*/

	objImoveis.setTipo(varTipo);
	objImoveis.setDe(varDe);
	objImoveis.setAte(varAte);
	objImoveis.setCidade(varCidade);
	objImoveis.setLote(varLote);

	location="imoveis.asp?"+objImoveis.getParametros();
}


function fCombo(varCampoSelecionado,varCampoCarregar){
	var vObj=vFrmFiltro.item(varCampoSelecionado);
	var varValor=vObj.options[vObj.selectedIndex].value;
	
	if(vObj.name.toUpperCase()=="TXTTIPO"){
	   objImoveis.setTipo(varValor);
	}
	else if(vObj.name.toUpperCase()=="TXTCIDADE"){
		objImoveis.setCidade(varValor);
	}
	
	if(varCampoCarregar!="0"){
		var vObjLoading=getDiv('dvAguarde');				
		getDados(objImoveis.getURL(varCampoCarregar),copulaCombo,"xml",vObjLoading);
	}
}

function fComboSel(varCampoSelecionado,varCampoCarregar){
	if(varCampoCarregar!="0"){
		var vObjLoading=getDiv('dvAguarde');				
		//teste.innerHTML=objImoveis.getURL(varCampoCarregar);
		getDados(objImoveis.getURL(varCampoCarregar),copulaCombo,"xml",vObjLoading);
	}
}

function copulaCombo(strXml)
{
	  var varRetorno=strXml.getElementsByTagName("erro")[0].firstChild.nodeValue;
	  var vObj=strXml.getElementsByTagName("div")[0].firstChild.nodeValue;
	  
	  var bSel=false;
	  
	  if(varRetorno=="False") {
		  limpaCombo(vFrmFiltro.item(vObj));
		  addOption("","Todas",vFrmFiltro.item(vObj),"false");
		  
		  var dataArray=strXml.getElementsByTagName("registro");		  
		  if(dataArray.length > 0) {
			 for(var i = 0;i < dataArray.length ; i++)
			 {					
			    var varID = dataArray[i].attributes.getNamedItem("id").value;				
				var varValor = dataArray[i].attributes.getNamedItem("text").value;
				var varSel = dataArray[i].attributes.getNamedItem("sel").value;
				if(varSel=="true"){
					bSel=true;	
				}
				addOption(varID,varValor,vFrmFiltro.item(vObj),varSel); 
			 }
		 }		
	 }
	 setDiv("dvAguarde","");
	 
	 if(bSel==true){
		if(vObj.toUpperCase()=="TXTTIPO"){
		   fComboSel('txtTipo','txtCidade');
		}
	 }
}
