﻿<!--
var vNegocioPadrao = 'V';
var vUfPadrao = 'DF';
var vCidadePadrao = 0;
var vTipoPadrao = 0;
var vQuartoPadrao = 0;
var vBairroPadrao = '';
var vPalavraPadrao = '';
var vPrecoPadrao = '';
var vOrdemPadrao = 'F';
var vPlantaPadrao = true;
var vProntoPadrao = true;

var arrayNegocio = [
	 ['A', 'ALUGUEL']
	,['V', 'VENDA']
	,['L', 'LANCAMENTO']
	,['T', 'TEMPORADA']
	,['R', 'RURAL']
];
var arrayBairroBrasilia = [
    "ASA NORTE",
    "ASA SUL",
    "LAGO NORTE",
    "LAGO SUL",
    "OCTOGONAL",
    "SUDOESTE",    
    "PARK WAY",
    "SIA",
    "SIG",
    "SOF",
    "VILA PLANALTO"
];

function showText(ob, txt){
	if (ob.value == "") {
		ob.value = txt;
		}
	}

function clearText(ob, txt){
	if (ob.value == txt) {
		ob.value = "";
	}
}

function limpaLista( ObjLst ) {
   for (i = ObjLst.length; i >= 0; i--) {
	  ObjLst[i] = null;
   }
}

function carregarArray( Uf, Negocio ) {
	var arrayTmp;
	var nomeVar = 'array' + Uf + '_' + Negocio;

	eval( 'arrayTmp = (' + nomeVar + ') ? ' + nomeVar + ' : null;' );
	return arrayTmp;
}

function inicializarUf(){
	if ( document.fPesquisa.cbxUf ) {

		limpaLista( document.fPesquisa.cbxUf );	
		for ( i = 0; i < arrayUFs.length; i++ ) {
			document.fPesquisa.cbxUf.options[i] = new Option( arrayUFs[i][1], arrayUFs[i][0] );
			if ( document.fPesquisa.cbxUf.options[i].value == vUfPadrao ) {
				document.fPesquisa.cbxUf.selectedIndex = (i);
			} 
		}
	}

	inicializarNegocio( vUfPadrao );
	if ( document.fPesquisa.cbxNegocio )
		inicializarCidade( vUfPadrao, document.fPesquisa.cbxNegocio.value );
	else
		inicializarCidade( vUfPadrao, vNegocioPadrao );

	if ( document.fPesquisa.bairro )
		document.fPesquisa.bairro.value = vBairroPadrao;
	if ( document.fPesquisa.palavra )
		document.fPesquisa.palavra.value = vPalavraPadrao;
	if ( document.fPesquisa.preco )
		document.fPesquisa.preco.value = vPrecoPadrao;

	if ( document.fPesquisa.ordem ) {
		for ( i = 0; i < document.fPesquisa.ordem.options.length; i++ ) {
			if ( document.fPesquisa.ordem.options[ i ].value == vOrdemPadrao ) {
				document.fPesquisa.ordem.selectedIndex = i;
				break;
			}
		}
	}
}

function inicializarNegocio( Uf ) {
	var existe;
	var c = 0;
	var nomeVar = false;
	
	if ( document.fPesquisa.cbxNegocio ) {
		limpaLista( document.fPesquisa.cbxNegocio );
		for ( i = 0; i < arrayNegocio.length; i++ ){
			nomeVar = 'array' + Uf + '_' + arrayNegocio[i][0];
			eval( 'existe = (window.' + nomeVar + ') ? true : false;' );
			if ( existe ) {
				document.fPesquisa.cbxNegocio.options[c] = new Option( arrayNegocio[i][1], arrayNegocio[i][0] );
				if ( document.fPesquisa.cbxNegocio.options[c].value == vNegocioPadrao ) 
					document.fPesquisa.cbxNegocio.selectedIndex = (c);
				c++;
			}
		}
	}
}

function inicializarCidade( Uf, Negocio ){
	var arrayTmp = carregarArray( Uf, Negocio );
	var lCidadePadrao = vCidadePadrao;
	if ( vUfPadrao != Uf ) {
		lCidadePadrao = arrayTmp[0];
	}

	if ( document.fPesquisa.cbxCidade ) {
		limpaLista( document.fPesquisa.cbxCidade ); 
		for ( i = 1; i < arrayTmp.length; i++ ) {
			document.fPesquisa.cbxCidade.options[i-1] = new Option( arrayTmp[i][1] , arrayTmp[i][0] );
			if ( document.fPesquisa.cbxCidade.options[i-1].value == lCidadePadrao ) 
				document.fPesquisa.cbxCidade.selectedIndex = (i-1);
		}
		carregarBairro(document.fPesquisa.cbxCidade.value);
		inicializarOutros( Uf, Negocio, document.fPesquisa.cbxCidade.selectedIndex );
	}
	else {
		var indexCidade = 0;
		for ( i = 1; i < arrayTmp.length; i++ ) {
			if ( arrayTmp[i][0] == lCidadePadrao ) {
				indexCidade = (i-1);
				break;
			}
		}
		inicializarOutros( Uf, Negocio, indexCidade );
	}
}

function pegarPaginaAtual() {
	var dir = location.href.substring( 0, location.href.lastIndexOf( '/' ) + 1 );
	var url = location.href.substring( dir.length, location.href.length + 1 );
	if ( url.indexOf( '?' ) >= 0 )
		return url.substring( 0, url.indexOf( '?' ) )
	else
		return url;
}

function inicializarOutros( Uf, Negocio, indexCidade ) {

	var arrayUf = carregarArray( Uf, Negocio );
	var arrayCidade = arrayUf[ indexCidade+1 ];

	inicializarTipo( Negocio, arrayCidade );

	var arrayQuarto = arrayCidade[ 4 ].split( "-" );	
	var c = 1;
	if ( document.fPesquisa.cbxQuarto ) {
		limpaLista( document.fPesquisa.cbxQuarto );
		document.fPesquisa.cbxQuarto.options[0] = new Option( 'TODOS', '0' );
		for ( i = 0; i < arrayQuarto.length; i++ ) {
			if ( arrayQuarto[i] > 0 && arrayQuarto[i] < 4 ) {
				document.fPesquisa.cbxQuarto.options[c] = new Option( arrayQuarto[i], arrayQuarto[i] );
				if ( document.fPesquisa.cbxQuarto.options[i].value == vQuartoPadrao ) 
					document.fPesquisa.cbxQuarto.selectedIndex = (i);
				c++;
			}
			else if ( arrayQuarto[i] >= 4 ) {
				document.fPesquisa.cbxQuarto.options[c] = new Option( '4 ou +', '4' );
				if ( document.fPesquisa.cbxQuarto.options[i].value == vQuartoPadrao ) 
					document.fPesquisa.cbxQuarto.selectedIndex = (i);
				c++;
			}
		}	
	}
}

function inicializarTipo( Negocio, arrayCidade ) {
	var arrayTipo = arrayCidade[ 3 ].split( "-" );
	
	limpaLista( document.fPesquisa.cbxTipo );

	for ( i = 0; i < arrayTipo.length; i++ ) {

		var lId = arrayTipos[ arrayTipo[ i ] ][ 0 ];
		var lDesc = arrayTipos[ arrayTipo[ i ] ][ 1 ];
		var lPlanta = arrayTipos[ arrayTipo[ i ] ][ 2 ];

		if ( Negocio != "V" )
			lPlanta = false;

		document.fPesquisa.cbxTipo.options[i] = new Option( lDesc, lId );
		if ( document.fPesquisa.cbxTipo.options[i].value == vTipoPadrao ) {
			document.fPesquisa.cbxTipo.selectedIndex = (i);

			var crtlLiPlanta = document.getElementById( 'liPlanta' );
			var crtlLiPronto = document.getElementById( 'liPronto' );
			var crtlPlanta = document.getElementById( 'cbxPlanta' );
			var crtlPronto = document.getElementById( 'cbxPronto' );

			if ( crtlLiPlanta != null ) {
				crtlLiPlanta.style.visibility = ( lPlanta ) ? "visible" : "hidden";
				if ( lPlanta )
					crtlPlanta.checked = vPlantaPadrao;
				else
					crtlPlanta.checked = true;
			}
			if ( crtlLiPronto != null ) {
				crtlLiPronto.style.visibility = ( lPlanta ) ? "visible" : "hidden";
				if ( lPlanta )
					crtlPronto.checked = vProntoPadrao;
				else
					crtlPronto.checked = true;
			}			
		}
	}
}

function mudarNegocio( Negocio ) {
	inicializarCidade( document.fPesquisa.cbxUf.value, Negocio );
}

function mudarUf( Uf ){
	for ( i = 0; i < arrayUFs.length; i++ ){
		if ( arrayUFs[ i ][ 2 ] == true && arrayUFs[ i ][ 0 ] == Uf ) {
			var url = 'default.aspx?n=' + vNegocioPadrao + '&uf=' + Uf;
			location.href = url;
			return;
		} 
	}
	inicializarNegocio( Uf );
	if ( document.fPesquisa.cbxNegocio )
		inicializarCidade( Uf, document.fPesquisa.cbxNegocio.value );
	else
		inicializarCidade( Uf, vNegocioPadrao );
}

function carregarBairro( indexCidade ) {
    var liBairro = document.getElementById( 'liBairro' );
    if ( liBairro != null ) {
        var version = 0;
        if (navigator.appVersion.indexOf("MSIE") != -1) {
            temp = navigator.appVersion.split("MSIE");
            version = parseFloat(temp[1]);
        }
        if ( indexCidade == 1 && version > 5.5 ) {
            liBairro.innerHTML = '<select name="bairro" style="height:16px"></select>';
            var ctrlBairro = document.getElementById( 'bairro' );
            if ( ctrlBairro != null ) {
                ctrlBairro.options[0] = new Option( "--Todos os bairros--", "" );
                for ( i = 0; i < arrayBairroBrasilia.length; i++ )
                    ctrlBairro.options[i+1] = new Option( arrayBairroBrasilia[i], arrayBairroBrasilia[i] );
            }
        } else
            liBairro.innerHTML = '<input id="bairro" name="bairro" type="text" onfocus="clearText(this,\'bairro ** opcional\')" onblur="showText(this,\'bairro ** opcional\');" value="bairro ** opcional">';
    }
}

function mudarCidade( indexCidade ) {
	var Uf = vUfPadrao;	
	if ( document.fPesquisa.cbxUf )
		Uf = document.fPesquisa.cbxUf.value;
    carregarBairro( indexCidade );
    var Negocio = vNegocioPadrao;
	if ( document.fPesquisa.cbxNegocio )
		Negocio = document.fPesquisa.cbxNegocio.value;
	inicializarOutros( Uf, Negocio, indexCidade );
}

function mudarTipo( indexTipo ) {

	var crtlLiPlanta = document.getElementById( 'liPlanta' );
	var crtlLiPronto = document.getElementById( 'liPronto' );
	if ( crtlLiPlanta != null && crtlLiPronto != null ) {


		var Uf = document.fPesquisa.cbxUf.value;
		var Negocio = vNegocioPadrao;
		if ( document.fPesquisa.cbxNegocio )
			Negocio = document.fPesquisa.cbxNegocio.value;

		if ( Negocio == 'V' ) {
			var arrayUf = carregarArray( Uf, Negocio );

			var indexCidade = 0;
			if ( document.fPesquisa.cbxCidade ) {
				indexCidade = document.fPesquisa.cbxCidade.selectedIndex;
			} else {
				for ( i = 1; i < arrayUf.length; i++ ) {
					if ( arrayUf[i][0] == vCidadePadrao ) {
						indexCidade = (i-1);
						break;
					}
				}
			}

			var arrayCidade = arrayUf[ indexCidade + 1 ];
			var arrayTipo = arrayCidade[ 3 ].split( "-" );

			var arrTipo = arrayTipos[ arrayTipo[ indexTipo ] ];
			if ( arrTipo[ 2 ] ) {
				crtlLiPlanta.style.visibility = "visible";
				crtlLiPronto.style.visibility = "visible";
			} else {
				crtlLiPlanta.style.visibility = "hidden";
				crtlLiPronto.style.visibility = "hidden";
		    }
		}
	}
}

function pesquisar() {
    var url = "";
    var negocio = "R";
    if ( document.fPesquisa.cbxNegocio != null )
        negocio = document.fPesquisa.cbxNegocio.value;
    var tipoUrl = document.fPesquisa.tipoUrl.value;
    if ( negocio == "R" || negocio == "r" ) {
        if ( tipoUrl == "lista" )
            url += "rural.aspx";
        else if ( tipoUrl == "galeria" )
            url += "rural_galeria.aspx";
        else if ( tipoUrl == "mapa" )
            url += "rural_mapa.aspx";
    }    
    else if ( negocio == "V" || negocio == "v" ) {
        if ( tipoUrl == "lista" )
            url += "venda.aspx";
        else if ( tipoUrl == "galeria" )
            url += "venda_galeria.aspx";
        else if ( tipoUrl == "mapa" )
            url += "venda_mapa.aspx";
    }
    else if ( negocio == "A" || negocio == "a" ) {
        if ( tipoUrl == "lista" )
            url += "aluguel.aspx";
        else if ( tipoUrl == "galeria" )
            url += "aluguel_galeria.aspx";
        else if ( tipoUrl == "mapa" )
            url += "aluguel_mapa.aspx";
    }
    else if ( negocio == "L" || negocio == "l" ) {
        if ( tipoUrl == "lista" )
            url += "lancamento.aspx";
        else if ( tipoUrl == "galeria" )
            url += "lancamento_galeria.aspx";
        else if ( tipoUrl == "mapa" )
            url += "lancamento_mapa.aspx";
    }
    else if ( negocio == "T" || negocio == "t" ) {
        if ( tipoUrl == "lista" )
            url += "temporada.aspx";
        else if ( tipoUrl == "galeria" )
            url += "temporada_galeria.aspx";
        else if ( tipoUrl == "mapa" )
            url += "temporada_mapa.aspx";
    }
    url += "?uf=" + escape(document.fPesquisa.cbxUf.value);
    if ( document.fPesquisa.cbxNegocio != null )
        url += "&n=" + escape(document.fPesquisa.cbxNegocio.value);
    else
        url += "&n=R";
    url += "&c=" + escape(document.fPesquisa.cbxCidade.value);
    url += "&t=" + escape(document.fPesquisa.cbxTipo.value);
    if ( document.fPesquisa.cbxQuarto != null )
        url += "&q=" + escape(document.fPesquisa.cbxQuarto.value);
    if ( document.fPesquisa.bairro != null && 
         document.fPesquisa.bairro.value != '--Todos os bairros--' &&
         document.fPesquisa.bairro.value != 'bairro ** opcional' )
        url += "&b=" + escape(document.fPesquisa.bairro.value);
    url += "&p=" + escape(document.fPesquisa.palavra.value);
    url += "&o=" + escape(document.fPesquisa.ordem.value);
    if ( document.fPesquisa.preco != null )
        url += "&v=" + escape(document.fPesquisa.preco.value);
    if ( document.fPesquisa.cbxPlanta != null && document.fPesquisa.cbxPlanta.checked )
        url += "&planta=1";
    if ( document.fPesquisa.cbxPronto != null && document.fPesquisa.cbxPronto.checked )
        url += "&pronto=1";
    location.href = url;
}
//-->
