var linkBusca = '../library/Others/Ajax.php';
var codempresa = 'C00812';

function mostraItens(codsistema) 
{
	if($('secaoU').checked){
		if(codsistema=="cpv"){
			$('tiposimovel').length = 9;
			$('tiposimovel').options[1] = new Option ("Casas e Sobrados","1");
			$('tiposimovel').options[2] = new Option ("Apartamento","9");
			$('tiposimovel').options[3] = new Option ("Prédio Comercial","14");
			$('tiposimovel').options[4] = new Option ("Prédio Residencial","2");
			$('tiposimovel').options[5] = new Option ("Sala Comercial","6");
			$('tiposimovel').options[6] = new Option ("Galpão","5");
			$('tiposimovel').options[7] = new Option ("Terreno Comercial","3");
			$('tiposimovel').options[8] = new Option ("Terreno Residencial","2");
			$('tiposimovel').options[9] = new Option ("Negócios","17");
		}else{
			$('tiposimovel').length = 10;
			$('tiposimovel').options[1] = new Option ("Casas e Sobrados","4");
			$('tiposimovel').options[2] = new Option ("Apartamento","8");
			$('tiposimovel').options[3] = new Option ("Kitinete","9");
			$('tiposimovel').options[4] = new Option ("Prédio Comercial","5");
			$('tiposimovel').options[5] = new Option ("Sala Comercial","7");
			$('tiposimovel').options[6] = new Option ("Galpão","21");
			$('tiposimovel').options[7] = new Option ("Quiosque","23");
			$('tiposimovel').options[8] = new Option ("Terreno Comercial","3");
			$('tiposimovel').options[9] = new Option ("Chácaras","50");
			$('tiposimovel').options[10] = new Option ("Garagem","49");
		}
		$('sistema').style.visibility='visible';
		$('bairros').style.visibility='visible';
	}else if($('secaoR').checked){
		$('tiposimovel').length = 2;
		$('tiposimovel').options[1] = new Option ("Chácaras","7");
		$('tiposimovel').options[2] = new Option ("Fazenda","8");
		$('sistema').style.visibility='hidden';
		$('bairros').style.visibility='hidden';
	}
	if(codsistema=="cpv") codsistemaSigla = 'V';
	else codsistemaSigla = 'A';
	new Ajax.Request(linkBusca, getOpt('cidades', codsistemaSigla, 'cidade', "codcidade", "nomecidade"));
	new Ajax.Request(linkBusca, getOpt('bairros', codsistemaSigla, 'bairro', "codbairro", "nomebairro"));

	$('dormitorios').selectedIndex = 0;
	$('sistema').value = codsistema;
}

function getOpt(nomeCampo, codsistema, acao, campocodigo, campovalor)
{
	var opt = {	
		// Use POST
		method: 'get',
		// Send this lovely data
		parameters: "codempresa="+codempresa+"&codsistema="+codsistema+"&acao="+acao,
		onLoading: function(t) {
			$(nomeCampo).length = 1;
			$(nomeCampo).options[1] = new Option ("Carregando...");
			setSelectDefault(nomeCampo, "Carregando...");
			$(nomeCampo).disabled='disabled';
		},
		// Handle successful response
		onSuccess: function(t) {
			results = t.responseXML;
			$(nomeCampo).length = 1;

			var root  = results.getElementsByTagName('DataGrid')[0];
			var items = root.getElementsByTagName("Row");

			for (var i = 0 ; i < items.length ; i++)
			{
				// get one item after another
				var item = items[i];
				// now we have the item object, time to get the contents
				// get the name of the item
				var codigo = item.getElementsByTagName(campocodigo)[0].firstChild.nodeValue;
				// get the quantity
				var valor = item.getElementsByTagName(campovalor)[0].firstChild.nodeValue;

				$(nomeCampo).options[i+1] = new Option( valor, codigo, false, false );
			}

			if (acao == "cidade" && !$('secaoR').checked)
			{
				setSelectDefault(nomeCampo, "Palmas");
			}
			$(nomeCampo).disabled='';
		},
		// Handle 404
		on404: function(t) {
			alert('Erro 404: local "' + t.statusText + '" não foi encontrado.');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Erro ' + t.status + ' -- ' + t.statusText);
		}
	}
	
	return opt;
}


function enviaForm(f)
{
	f.submit();

	return false;
}


function setSelectDefault(nomeCampo, valorPadrao)
{
	campo = $(nomeCampo);
	selectedItem = 0;

	for (var i = 0; i < campo.length; i++)
	{
		if (campo.options[i].text.toUpperCase() == valorPadrao.toUpperCase())
		{
			selectedItem = i;
		}
    }
	
	campo.options[selectedItem].selected = true;
}

