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']
];

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);
		}
		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 crtlDivPlanta = document.getElementById( 'divPlanta' );	
			var crtlPlanta = document.getElementById( 'cbxPlanta' );
			var crtlPronto = document.getElementById( 'cbxPronto' );

			if ( crtlDivPlanta != null ) {
				crtlDivPlanta.style.visibility = ( lPlanta ) ? "visible" : "hidden";
				if ( lPlanta ) {
					crtlPlanta.checked = vPlantaPadrao;
					crtlPronto.checked = vProntoPadrao;
				} else {
					crtlPlanta.checked = true;
					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 mudarCidade( indexCidade ) {
	var Uf = vUfPadrao;	
	if ( document.fPesquisa.cbxUf )
		Uf = document.fPesquisa.cbxUf.value;
	var Negocio = vNegocioPadrao;
	if ( document.fPesquisa.cbxNegocio )
		Negocio = document.fPesquisa.cbxNegocio.value;
	inicializarOutros( Uf, Negocio, indexCidade );
}

function mudarTipo( indexTipo ) {


	var crtlDivPlanta = document.getElementById( 'divPlanta' );	
	if ( crtlDivPlanta != 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 ] )
				crtlDivPlanta .style.visibility = "visible";
			else
				crtlDivPlanta.style.visibility = "hidden";
		}
	}
}
