function getForm(formName) {
    // Test if is IE or Netscape (document.layers)
    if(document.all)
        return eval(formName);
    else
        return document.forms[formName];
}

function onChangeDistritoConcelho(strFirstOptionText) {

	if(document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].value == 0 || document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].value < 100) {
		populateSelect(document.getElementsByName("freguesia")[0], new Array(), new Array(), strFirstOptionText);
		document.getElementById("freguesia").disabled = true;
		
	}else {
		document.getElementById("freguesia").disabled = false;

		var head = document.getElementsByTagName("head").item(0);


		var old = document.getElementById("lastLoadedFreguesias");
		if(old)
			head.removeChild(old);

		script = document.createElement("script");
		script.src = "/js/freguesias/" + document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].value + ".js";
		script.type = "text/javascript";
		script.defer = true;
		script.id = "lastLoadedFreguesias";
		try{
			void(head.appendChild(script));
		}catch(e){
			alert(e.description);
		}
	}	
}

function onChangeTipo() {
	arrFormElements = document.getElementById('pesquisa_imoveis').elements;
	formElementsLength = arrFormElements.length;
	
	tipo = document.getElementById('tipo').options[document.getElementById('tipo').selectedIndex].value;

	document.getElementById('tipologia').disabled = false;
	if(tipo != 1 && tipo != 2){
		for(i=0; i!=formElementsLength; i++){
			if(arrFormElements[i].name == "tipologia"){
				arrFormElements[i].disabled = true;	
				arrFormElements[i].checked = false;	
			}
		}
	}else{
		for(i=0; i!=formElementsLength; i++){
			if(arrFormElements[i].name == "tipologia")
				arrFormElements[i].disabled = false;	
		}
	}
	
	try{
		if(tipo == 7){
			document.getElementById('estado').selectedIndex = 0;
			document.getElementById('estado').disabled = true;	
		}else{
			document.getElementById('estado').disabled = false;	
		}
	}catch(e){}
}

function validaPesquisa(){	
	var searchForm = getForm("pesquisa_imoveis");
	
	boolPesquisaValida = true;
	try{document.getElementById('tipo_erro').innerHTML = "";}catch(e){}
	try{document.getElementById('tipo').className = "";}catch(e){}
	try{document.getElementById('distrito_concelho_erro').innerHTML = "";	}catch(e){}
	try{document.getElementById('distrito_concelho').className = "";}catch(e){}
	try{document.getElementById('tipologia_erro').innerHTML = "";}catch(e){}
	try{document.getElementById('preco_erro').innerHTML = "";}catch(e){}
	try{document.getElementById('preco_min').className = "";}catch(e){}
	try{document.getElementById('preco_max').className = "";}catch(e){}
		
	try{	
		if(document.getElementById('tipo').selectedIndex == 0){
			document.getElementById('tipo_erro').innerHTML = "(Obrigatório)";
			document.getElementById('tipo').className = "inputs_erro";
			boolPesquisaValida = false;
		}
	}catch(e){}
	try{
		if(searchForm.distrito_concelho.selectedIndex == 0 ){
			document.getElementById('distrito_concelho_erro').innerHTML = "(Obrigatório)";
		//	document.getElementById('distrito_concelho').className = "inputs_erro";
			searchForm.distrito_concelho.className = "inputs_erro";
			boolPesquisaValida = false;
		}
	}catch(e){}
	
	try{
		preco_min = parseInt(document.getElementById('preco_min').options[document.getElementById('preco_min').selectedIndex].value);
		preco_max = parseInt(document.getElementById('preco_max').options[document.getElementById('preco_max').selectedIndex].value);
		
		if(preco_min > 0 && preco_max > 0 && ((preco_min < 25000 && preco_max >= 25000) || (preco_min >= 25000 && preco_max < 25000))){
			document.getElementById('preco_erro').innerHTML = "Preços escolhidos inválidos, escolha dois preços de compra ou dois preços de arrendamento.";
			document.getElementById('preco_min').className = "inputs_erro";
			document.getElementById('preco_max').className = "inputs_erro";
			boolPesquisaValida = false;
		}else if(preco_min > preco_max && preco_min > 0 && preco_max > 0){
			document.getElementById('preco_erro').innerHTML = "Preço mínimo superior ao preço máximo.";
			document.getElementById('preco_min').className = "inputs_erro";
			document.getElementById('preco_max').className = "inputs_erro";
			boolPesquisaValida = false;
		}
	}catch(e){}
	
	//try{document.getElementById('distrito_concelho_desc').value = document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].text;}catch(e){}
	try{
		searchForm.distrito_concelho_desc.value = searchForm.distrito_concelho.options[searchForm.distrito_concelho.selectedIndex].text;
	}catch(e){}
	
	try{document.getElementById('tipo_desc').value = document.getElementById('tipo').options[document.getElementById('tipo').selectedIndex].text;}catch(e){}
	try{document.getElementById('freguesia_desc').value = document.getElementById('freguesia').options[document.getElementById('freguesia').selectedIndex].text;}catch(e){}
	
	return boolPesquisaValida;
}

function validaPesquisaEscritorios(){	
	var searchForm = getForm("pesquisa_escritorios");
	
	boolPesquisaValida = true;
	try{document.getElementById('distrito_concelho_erro').innerHTML = "";	}catch(e){}
	try{document.getElementById('distrito_concelho').className = "";}catch(e){}
	try{document.getElementById('area_min').className = "";}catch(e){}
	try{document.getElementById('preco_max').className = "";}catch(e){}

	try{
		searchForm.distrito_concelho_desc.value = searchForm.distrito_concelho.options[searchForm.distrito_concelho.selectedIndex].text;
	}catch(e){}
	
	return boolPesquisaValida;
}

function cleanPesquisa(){
	document.getElementById('order_by').value = "data";
	document.getElementById('rs_offset').value = 0;
}

function orderByData(){
	document.getElementById('order_by').value = "data";
	document.getElementById('rs_offset').value = 0;
	try{document.getElementById('distrito_concelho_desc').value = document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].text;}catch(e){}
	try{document.getElementById('tipo_desc').value = document.getElementById('tipo').options[document.getElementById('tipo').selectedIndex].text;}catch(e){}
	try{document.getElementById('freguesia_desc').value = document.getElementById('freguesia').options[document.getElementById('freguesia').selectedIndex].text;}catch(e){}
	document.getElementById('pesquisa_imoveis').submit();
}

function orderByPreco(){
	document.getElementById('order_by').value = "preco";
	document.getElementById('rs_offset').value = 0;
	try{document.getElementById('distrito_concelho_desc').value = document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].text;}catch(e){}
	try{document.getElementById('tipo_desc').value = document.getElementById('tipo').options[document.getElementById('tipo').selectedIndex].text;}catch(e){}
	try{document.getElementById('freguesia_desc').value = document.getElementById('freguesia').options[document.getElementById('freguesia').selectedIndex].text;}catch(e){}
	document.getElementById('pesquisa_imoveis').submit();
}

function orderByArea(){
	document.getElementById('order_by').value = "area";
	document.getElementById('rs_offset').value = 0;
	try{document.getElementById('distrito_concelho_desc').value = document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].text;}catch(e){}
	try{document.getElementById('tipo_desc').value = document.getElementById('tipo').options[document.getElementById('tipo').selectedIndex].text;}catch(e){}
	try{document.getElementById('freguesia_desc').value = document.getElementById('freguesia').options[document.getElementById('freguesia').selectedIndex].text;}catch(e){}
	document.getElementById('pesquisa_imoveis').submit();
}

function orderByFreguesia(){
	document.getElementById('order_by').value = "freguesia";
	document.getElementById('rs_offset').value = 0;
try{	document.getElementById('distrito_concelho_desc').value = document.getElementById('distrito_concelho').options[document.getElementById('distrito_concelho').selectedIndex].text;}catch(e){}
	try{document.getElementById('tipo_desc').value = document.getElementById('tipo').options[document.getElementById('tipo').selectedIndex].text;}catch(e){}
	try{document.getElementById('freguesia_desc').value = document.getElementById('freguesia').options[document.getElementById('freguesia').selectedIndex].text;}catch(e){}
	document.getElementById('pesquisa_imoveis').submit();
}

function verImovel(imovel_id, rs_offset, nr_results, bool_foto) {
	var searchForm = "";
	
	try{
		if(document.getElementById('pesq').value == "renda")
			searchForm = getForm("pesquisa_imoveis");
		else if(document.getElementById('pesq').value == "com")
			searchForm = getForm("pesquisa_escritorios");
		else
			searchForm = getForm("pesquisa_imoveis");
	}catch(e){}

	try{
		if(document.getElementById('pesq_seg').value == "segmento"){
			searchForm = getForm("pesquisa_imoveis_segmento");
		}		
	}catch(e){}
	
	searchForm.nr_results.value = nr_results;	
	searchForm.rs_offset.value = rs_offset;	
	searchForm.id.value = imovel_id;

	if(bool_foto)
		document.getElementById('page').value = "detalhes";
	else	
		document.getElementById('page').value = "";	

		if(searchForm.pesq.value == "segmento"){
			document.getElementById('pesquisa_imoveis_segmento').action = "/venda-casas/comprar-casa.jsp";
			document.getElementById('pesquisa_imoveis_segmento').submit();
		}else if(searchForm.pesq.value == "renda"){
			document.getElementById('pesquisa_imoveis').action = "/venda-casas/alugar-casa.jsp";
			document.getElementById('pesquisa_imoveis').submit();
		}else if(searchForm.pesq.value == "com"){
			document.getElementById('pesquisa_escritorios').action = "/escritorios-lojas/comprar-alugar.jsp";
			document.getElementById('pesquisa_escritorios').submit();
		}else{
			document.getElementById('pesquisa_imoveis').action = "/venda-casas/comprar-casa.jsp";
			document.getElementById('pesquisa_imoveis').submit();
		}
}


function validaPesquisaReferencia(){	
	var searchForm = getForm("pesquisa_imoveis_ref");
	
	boolPesquisaValida = true;
	try{document.getElementById('ref_erro').innerHTML = "";}catch(e){}
	try{document.getElementById('tipo').className = "";}catch(e){}
	try{document.getElementById('distrito_concelho_erro').innerHTML = "";	}catch(e){}
	try{document.getElementById('distrito_concelho').className = "";}catch(e){}
	try{document.getElementById('tipologia_erro').innerHTML = "";}catch(e){}
	try{document.getElementById('preco_erro').innerHTML = "";}catch(e){}
	try{document.getElementById('preco_min').className = "";}catch(e){}
	try{document.getElementById('preco_max').className = "";}catch(e){}
		
	try{	
		if(document.getElementById('referencia').value == ''){
			document.getElementById('ref_erro').innerHTML = "(Obrigatório)";
			document.getElementById('referencia').className = "inputs_erro";
			boolPesquisaValida = false;
		}
	}catch(e){}

	return boolPesquisaValida;
}

