function getForm(formName) {
    // Test if is IE or Netscape (document.layers)
    if(document.all)
        return eval(formName);
    else
        return document.forms[formName];
}

function submitPos(pos) {
    var searchForm = getForm("imoPesq");

    searchForm.pos.value = pos;
    searchForm.submit();
}

function submitlastinsertmin(lastinsertmin, lastinsertmax) {
	var searchForm = getForm("imoPesq");
	
	//if(lastinsertmin != 0)
	searchForm.lastinsert.value = lastinsertmax;	
	searchForm.lastinsertmin.value = lastinsertmin;

	searchForm.pos.value = 0;
	//return;
}

function submitlastinsertmin_0(lastinsertmin, lastinsertmax) {
	var searchForm = getForm("imoPesq");
	
	//if(lastinsertmin != 0)
	searchForm.lastinsert.value = lastinsertmax;	
	searchForm.lastinsertmin.value = lastinsertmin;
	
	searchForm.pos.value = 0;
	if(ValidaPesq())
		searchForm.submit();
		
	//return;
}



function populateSelect(updateSelect, arrVal, arrTxt) {
    for(var i = updateSelect.options.length; i >= 0; i--)
        updateSelect.options[i] = null;

    var optElem = document.createElement("option");
    optElem.value = -1;
    optElem.text = ":: seleccionar";
    updateSelect.options[0] = optElem;

    if(arrTxt != null && arrVal != null) {
        for(var i = 0; i < arrTxt.length; i++) {
            var optElem = document.createElement("option");
            optElem.value = arrVal[i];
            optElem.text = arrTxt[i];
            updateSelect.options[i + 1] = optElem;
        }
    }
}


function populateSelectWithNM(updateSelect, arrVal, arrTxt, strSelec,boolSemlimite) {
    for(var i = updateSelect.options.length; i >= 0; i--)
        updateSelect.options[i] = null;

    var optElem = document.createElement("option");
    optElem.value = -1;
    optElem.text = strSelec;
    updateSelect.options[0] = optElem;
		
    if(arrTxt != null && arrVal != null) {
        for(var i = 0; i < arrTxt.length; i++) {
            var optElem = document.createElement("option");
            optElem.value = arrVal[i];
            optElem.text = arrTxt[i];
            updateSelect.options[i + 1] = optElem;
            
            if(boolSemlimite == true && i == (arrTxt.length-1)){
            	var optElem = document.createElement("option");
            	optElem.value = "0";
            	optElem.text = "sem limite";
            	updateSelect.options[i + 2] = optElem;
            }
        }
    }
}


function SetConcelho(count) {
    var searchForm = getForm("imoPesq");

    if(searchForm.concelho_id.length <= 1) {
        if(count < 50)
            setTimeout("SetConcelho(" + (count + 1) + ")", 100);
        return;
    }
    if(searchForm["idconcelho"] != undefined)
        concelho_id = searchForm.idconcelho.value;

    for(i = 0; i < searchForm.concelho_id.length; i++) {
        if((searchForm.concelho_id.options[i].value) == (concelho_id)) {
            searchForm.concelho_id.options[i].selected = true;
            OnChangeConcelho();
            setTimeout("SetFreguesia(0)", 100);
        }
    }
}

function SetFreguesia(count) {
    var searchForm = getForm("imoPesq");

    if(searchForm.freguesia_id.length <= 1) {
        if(count < 50)
            setTimeout("SetFreguesia(" + (count + 1) + ")", 100);
        return;
    }

    if(searchForm["idfreguesia"] != undefined)
        freguesia_id = searchForm.idfreguesia.value;

    for(i = 0; i < searchForm.freguesia_id.length; i++) {
        if((searchForm.freguesia_id.options[i].value) == (freguesia_id)) {
            searchForm.freguesia_id.options[i].selected = true;
        }
    }
}


function ValidaPesq () {
    var searchForm = getForm("imoPesq");
	searchForm.action = "/servlet/PesquisaResult";    	 
	searchForm.target = "";

    if(searchForm.distrito_id.selectedIndex == 0) {
        alert("Por favor, escolha o Distrito onde pretende fazer a sua pesquisa.");
        searchForm.distrito_id.focus();
        return false;
    }
    if(searchForm.concelho_id.selectedIndex == 0) {
        alert("Por favor, escolha o Concelho onde pretende fazer a sua pesquisa.");
        searchForm.concelho_id.focus();
        return false;
    }
    if(searchForm.tipologia_id1.selectedIndex > searchForm.tipologia_id2.selectedIndex &&
       searchForm.tipologia_id2.selectedIndex != 0) {
        alert("Seleccionar tipologia máxima igual ou superior\nà tipologia mínima.");
        searchForm.tipologia_id2.focus();
        return false;
    }
    if(searchForm.preco1.selectedIndex > searchForm.preco2.selectedIndex &&
       searchForm.preco2.selectedIndex != 0) {
        alert("Seleccionar preço máximo igual ou superior\nao preço mínimo.");
        searchForm.preco2.focus();
        return false;
    }
    
    if(searchForm.area1.selectedIndex > searchForm.area2.selectedIndex &&
       searchForm.area2.selectedIndex != 0) {
        alert("Seleccionar área máxima igual ou superior\nà área mínima.");
        searchForm.area2.focus();
        return false;
    }
    refno = /[^0-9]/;
    if(refno.test(searchForm.lastinsert.value)){
        alert("O campo dos 'últimos imóveis inseridos' tem de ser numérico.");
        searchForm.lastinsert.value = 90;
        searchForm.lastinsert.focus();
        return false;
    }
    searchForm.pos.value = 0;

    intTipoId = searchForm.tipo_id.options[searchForm.tipo_id.selectedIndex].value;
    if(intTipoId == 3) {
    	objWindow = window.open("", "wdOpenEscritorios");
    	searchForm.action = "/imobiliario/redirect_to_escritorios.jsp";    	 
    	searchForm.target = "wdOpenEscritorios";
    } else if(intTipoId == 5) {
    	objWindow = window.open("", "wdOpenArmazens");
    	searchForm.action = "/imobiliario/redirect_to_armazens.jsp";    	 
    	searchForm.target = "wdOpenArmazens";
    } else if(intTipoId == 6) {
    	objWindow = window.open("", "wdOpenLojas");
    	searchForm.action = "/imobiliario/redirect_to_lojas.jsp";    	 
    	searchForm.target = "wdOpenLojas";
    }
    
    return true;
}

function OnChangeDistrito() {
    var searchForm = getForm("imoPesq");

    populateSelect(document.getElementsByName("freguesia_id")[0], new Array(), new Array());
    if(searchForm.distrito_id.options[searchForm.distrito_id.selectedIndex].value == -1) {
        populateSelect(document.getElementsByName("concelho_id")[0], new Array(), new Array());
    }
    else {
        var head    = document.getElementsByTagName("head").item(0);
        var old     = document.getElementById("lastLoadedConcelhos");
        if(old)
            head.removeChild(old);

        script = document.createElement("script");
        script.src = "/js/concelhos/" + searchForm.distrito_id.options[searchForm.distrito_id.selectedIndex].value+ ".js";
        script.type = "text/javascript";
        script.defer = true;
        script.id = "lastLoadedConcelhos";
        void(head.appendChild(script));
    }
}

function OnChangeConcelho() {
    var searchForm = getForm("imoPesq");

    if(searchForm["idconcelho"] != undefined)
       searchForm.idconcelho.value = searchForm.concelho_id.options[searchForm.concelho_id.selectedIndex].value;
    if(searchForm.concelho_id.options[searchForm.concelho_id.selectedIndex].value == -1) {
        populateSelect(document.getElementsByName("freguesia_id")[0], new Array(), new Array());
    }
    else {
        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/" + searchForm.concelho_id.options[searchForm.concelho_id.selectedIndex].value + ".js";
        script.type = "text/javascript";
        script.defer = true;
        script.id = "lastLoadedFreguesias";
        void(head.appendChild(script));
    }
}

function OnChangeFreguesia() {
    var searchForm = getForm("imoPesq");

    if(searchForm["idfreguesia"] != undefined)
        searchForm.idfreguesia.value = searchForm.freguesia_id.options[searchForm.freguesia_id.selectedIndex].value;
}

function OnChangeClassTipo() {
	var searchForm = getForm("imoPesq");
	
	if(searchForm.tipo_id.selectedIndex != 1 && searchForm.tipo_id.selectedIndex != 2) {
		searchForm.tipologia_id1.disabled = true;
		searchForm.tipologia_id2.disabled = true;
	}else {
		searchForm.tipologia_id1.disabled = false;
		searchForm.tipologia_id2.disabled = false;
	}
}

function OnChangeTipo() {
	var searchForm = getForm("imoPesq");	
	
	if(searchForm.tipo_id.selectedIndex > 1) {
		searchForm.tipologia_id1.disabled = true;
		searchForm.tipologia_id2.disabled = true;
	}
	else {
		searchForm.tipologia_id1.disabled = false;
		searchForm.tipologia_id2.disabled = false;
	}	
}

function OnChangeOper() {
    var searchForm = getForm("imoPesq");
		var formPesqAvancada = document.getElementById("pesqav").value;    
	  var valor = parseInt(searchForm.operacao.value);	  
	  
    var precoTxt = new Array("250","500","750","1.000","1.250","1.500","1.750","2.000","2.500");
    var precoVal = new Array("250","500","750","1000","1250","1500","1750","2000","2500");
    
  
    if(formPesqAvancada == 1){
	    var precoTxt = new Array("250 €","500 €","750 €","1.000 €","1.250 €","1.500 €","1.750 €","2.000 €","2.500 €");
    	var precoVendTxt = new Array("25.000 €","50.000 €","75.000 €","100.000 €","125.000 €","150.000 €","175.000 €","200.000 €","225.000 €","250.000 €","275.000 €","300.000 €","325.000 €","350.000 €","375.000 €","400.000 €","425.000 €","450.000 €","475.000 €","500.000 €","525.000 €","550.000 €","575.000 €","600.000 €","625.000 €","650.000 €","675.000 €","700.000 €","725.000 €","750.000 €","775.000 €","800.000 €","825.000 €","850.000 €","875.000 €","900.000 €","925.000 €","950.000 €","975.000 €","1.000.000 €");
    }else{
			var precoVendTxt = new Array("25","50","75","100","125","150","175","200","225","250","275","300","325","350","375","400","425","450","475","500","525","550","575","600","625","650","675","700","725","750","775","800","825","850","875","900","925","950","975","1.000");
		}	
    var precoVendVal = new Array("25000","50000","75000","100000","125000","150000","175000","200000","225000","250000","275000","300000","325000","350000","375000","400000","425000","450000","475000","500000","525000","550000","575000","600000","625000","650000","675000","700000","725000","750000","775000","800000","825000","850000","875000","900000","925000","950000","975000","1000000");
    
    switch (valor){
        case 0:
            (document.getElementsByName("preco1")[0], new Array(), new Array(),"mín.");
            populateSelectWithNM(document.getElementsByName("preco2")[0], new Array(), new Array(),"máx.,",true);
            if(formPesqAvancada != 1)
	            document.getElementById("mil_euros").innerHTML = "mil";
            searchForm.preco1.disabled = true;
            searchForm.preco2.disabled = true;
            break;
        case 1:     //Compra
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máx.",true);
            if(formPesqAvancada != 1)
	            document.getElementById("mil_euros").innerHTML = "mil";
	            
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 2:     //Arredamento
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVal, precoTxt,"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVal, precoTxt,"máx.",true);
            
            if(formPesqAvancada != 1)
	            document.getElementById("mil_euros").innerHTML = "";
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 3:     //Trespasse
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máx.",true);
            if(formPesqAvancada != 1)
							document.getElementById("mil_euros").innerHTML = "mil";
							
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
    }
}


function loadPrecos(){

		var searchForm = getForm("imoPesq");
  	var valor = parseInt(searchForm.operacao.value);
  alert(valor);
	  var precoTxt = new Array("250","500","750","1.000","1.250","1.500","1.750",">2.000");
  	var precoVal = new Array("250","500","750","1000","1250","1500","1750","2000");
  	var precoVendTxt = new Array("25","50","75","100","125","150","175","200","225","250","275","300","325","350","375","400","425","450","475","500","525","550","575","600","625","650","675","700","725","750","775","800","825","850","875","900","925","950","975","1.000");
  	var precoVendVal = new Array("25000","50000","75000","100000","125000","150000","175000","200000","225000","250000","275000","300000","325000","350000","375000","400000","425000","450000","475000","500000","525000","550000","575000","600000","625000","650000","675000","700000","725000","750000","775000","800000","825000","850000","875000","900000","925000","950000","975000","1000000");
  
    	switch (valor){
        case 0:
            populateSelectWithNM(document.getElementsByName("preco1")[0], new Array(), new Array(),"mín.");
            populateSelectWithNM(document.getElementsByName("preco2")[0], new Array(), new Array(),"máx.,");
            searchForm.preco1.disabled = true;
            searchForm.preco2.disabled = true;
            break;
        case 1:     //Compra
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mín.");
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máx.");
            
            alert(searchForm.h_preco2.value);
            val1 = searchForm.h_preco1.value;
            val2 = searchForm.h_preco2.value;
            
            setTimeout('moveToOption("preco1",val1)', 200);
            setTimeout('moveToOption("preco2",val2)', 250);
//					moveToOption("preco2", searchForm.h_preco2.value);
						
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 2:     //Arredamento
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVal, precoTxt,"mín.");
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVal, precoTxt,"máx.");
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 3:     //Trespasse
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mín.");
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máx.");
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;        	    
    	}
} 



function SetConcelhoUK(count) {
    var searchForm = getForm("imoPesqUK");

    if(searchForm.concelho_id.length <= 1) {
        if(count < 50)
            setTimeout("SetConcelhoUK(" + (count + 1) + ")", 100);
        return;
    }
    if(searchForm["idconcelho"] != undefined)
        concelho_id = searchForm.idconcelho.value;

    for(i = 0; i < searchForm.concelho_id.length; i++) {
        if((searchForm.concelho_id.options[i].value) == (concelho_id)) {
            searchForm.concelho_id.options[i].selected = true;
            OnChangeConcelhoUK();
            setTimeout("SetFreguesiaUK(0)", 100);
        }
    }
}

function SetFreguesiaUK(count) {
    var searchForm = getForm("imoPesqUK");

    if(searchForm.freguesia_id.length <= 1) {
        if(count < 50)
            setTimeout("SetFreguesiaUK(" + (count + 1) + ")", 100);
        return;
    }

    if(searchForm["idfreguesia"] != undefined)
        freguesia_id = searchForm.idfreguesia.value;

    for(i = 0; i < searchForm.freguesia_id.length; i++) {
        if((searchForm.freguesia_id.options[i].value) == (freguesia_id)) {
            searchForm.freguesia_id.options[i].selected = true;
        }
    }
}


function ValidaPesqUK () {
    var searchForm = getForm("imoPesqUK");

    if(searchForm.distrito_id.selectedIndex == 0) {
        alert("Por favor, escolha o Distrito onde pretende fazer a sua pesquisa.");
        searchForm.distrito_id.focus();
        return false;
    }
    if(searchForm.concelho_id.selectedIndex == 0) {
        alert("Por favor, escolha o Concelho onde pretende fazer a sua pesquisa.");
        searchForm.concelho_id.focus();
        return false;
    }
    if(searchForm.tipologia_id1.selectedIndex > searchForm.tipologia_id2.selectedIndex &&
       searchForm.tipologia_id2.selectedIndex != 0) {
        alert("Seleccionar tipologia máxima igual ou superior\nà tipologia mínima.");
        searchForm.tipologia_id2.focus();
        return false;
    }
    if(searchForm.preco1.selectedIndex > searchForm.preco2.selectedIndex &&
       searchForm.preco2.selectedIndex != 0) {
        alert("Seleccionar preço máximo igual ou superior\nao preço mínimo.");
        searchForm.preco2.focus();
        return false;
    }
    if(searchForm.area1.selectedIndex > searchForm.area2.selectedIndex &&
       searchForm.area2.selectedIndex != 0) {
        alert("Seleccionar área máxima igual ou superior\nà área mínima.");
        searchForm.area2.focus();
        return false;
    }
    refno = /[^0-9]/;
    if(refno.test(searchForm.lastinsert.value)){
        alert("O campo dos 'últimos imóveis inseridos' tem de ser numérico.");
        searchForm.lastinsert.value = 90;
        searchForm.lastinsert.focus();
        return false;
    }
    searchForm.pos.value = 0;
    return true;
}

function OnChangeDistritoUK() {
    var searchForm = getForm("imoPesqUK");

    populateSelectWithNM(document.getElementsByName("freguesia_id")[0], new Array(), new Array(),"Select");
    if(searchForm.distrito_id.options[searchForm.distrito_id.selectedIndex].value == -1) {
        populateSelectWithNM(document.getElementsByName("concelho_id")[0], new Array(), new Array(),"Select");
    }
    else {
        var head    = document.getElementsByTagName("head").item(0);
        var old     = document.getElementById("lastLoadedConcelhos");
        if(old)
            head.removeChild(old);

        script = document.createElement("script");
        script.src = "/js/concelhos/" + searchForm.distrito_id.options[searchForm.distrito_id.selectedIndex].value+ ".js";
        script.type = "text/javascript";
        script.defer = true;
        script.id = "lastLoadedConcelhos";
        void(head.appendChild(script));
    }
}





//ANGELA
function populateSelectEmp(updateSelect, arrVal, arrTxt) {
    for(var i = updateSelect.options.length; i >= 0; i--)
        updateSelect.options[i] = null;

    var optElem = document.createElement("option");
    optElem.value = -1;
    optElem.text = ":: seleccionar";
    updateSelect.options[0] = optElem;

    if(arrTxt != null && arrVal != null) {
        for(var i = 0; i < arrTxt.length; i++) {
            var optElem = document.createElement("option");
            optElem.value = arrVal[i];
            optElem.text = arrTxt[i];
            updateSelect.options[i + 1] = optElem;
        }
    }
}



function SetConcelhoEmp(count) {
    var searchForm = getForm("imoPesqEmp");

    if(searchForm.concelho_id.length <= 1) {
        if(count < 50)
            setTimeout("SetConcelhoEmp(" + (count + 1) + ")", 100);
        return;
    }
    if(searchForm["idconcelho"] != undefined)
        concelho_id = searchForm.idconcelho.value;

    for(i = 0; i < searchForm.concelho_id.length; i++) {
        if((searchForm.concelho_id.options[i].value) == (concelho_id)) {
            searchForm.concelho_id.options[i].selected = true;
            OnChangeConcelhoEmp();    
        }
    }
}



function ValidaPesqEmp() {
    var searchForm = getForm("imoPesqEmp");
    
	s = Trim(searchForm.nome_empreend.value);
    
    if(searchForm.distrito_id.selectedIndex == 0 && s.length == 0) {
        alert("Por favor, terá de preencher o Distrito ou o nome do Empreendimento a pesquisar.");
        searchForm.distrito_id.focus();
        return false;
    }
    
	if(searchForm.distrito_id.selectedIndex != 0 && s.length == 0) {
		searchForm.nome_empreend.value = '';
		return true;
	}
		
	//if(searchForm.concelho_id.selectedIndex == 0) {
    //    alert("Por favor, escolha o Concelho onde pretende fazer a sua pesquisa.");
    //    searchForm.concelho_id.focus();
    //    return false;
    //}
  
    searchForm.pos.value = 0;
    return true;
}

function ValidaPesqEmpNome() {
	var searchForm = getForm("imoPesqEmpNome");

	if(searchForm.nome_emoreend.value.length == 0) {
		alert("Por favor, preencha o campo com o nome do empreendimento.");
		searchForm.nome_emoreend.focus();
		return false;
	}
    
	return true;
}

function ValidaPesqNomeEmp () {
	
    if(document.getElementById("nomeEmp").value == "" || document.getElementById("nomeEmp").value == ":: Empreendimento") {
		alert("Tem de preencher o nome do empreendimento");
		document.getElementById("nomeEmp").focus();
		return false;
	}	
}



function OnChangeDistritoEmp() {
    var searchForm = getForm("imoPesqEmp");	
    
    populateSelectEmp(document.getElementById("emp_concelho_id"), new Array(), new Array());
    
    if(searchForm.distrito_id.options[searchForm.distrito_id.selectedIndex].value != -1) {    	
        var head    = document.getElementsByTagName("head").item(0);
        var old     = document.getElementById("lastLoadedConcelhos");

        if(old)
        	head.removeChild(old);

        script = document.createElement("script");
        script.src = "/js/empreendimentos/concelhos/" + searchForm.distrito_id.options[searchForm.distrito_id.selectedIndex].value+ ".js";
        script.type = "text/javascript";
        script.defer = true;
        script.id = "lastLoadedConcelhos";
        
        void(head.appendChild(script));
    }
}

function OnChangeConcelhoEmp() {
    var searchForm = getForm("imoPesqEmp");

    if(searchForm["idconcelho"] != undefined)
        searchForm.idconcelho.value = searchForm.concelho_id.options[searchForm.concelho_id.selectedIndex].value;  
}

function submitPosEmp(pos) {
    var searchForm = getForm("imoPesqEmp");

    searchForm.pos.value = pos;
    searchForm.submit();
}

function OnChangeOperation() {
    var searchForm = getForm("imoPesq");
    var valor = parseInt(searchForm.operacao.value);
    
    var precoTxt = new Array("250 €","500 €","750 €","1.000 €","1.250 €","1.500 €","1.750 €","2.000 €","2.500 €");
    var precoVal = new Array("250","500","750","1000","1250","1500","1750","2000","2500");
    var precoVendTxt = new Array("25.000 €","50.000 €","75.000 €","100.000 €","125.000 €","150.000 €","175.000 €","200.000 €","225.000 €","250.000 €","275.000 €","300.000 €","325.000 €","350.000 €","375.000 €","400.000 €","425.000 €","450.000 €","475.000 €","500.000 €","525.000 €","550.000 €","575.000 €","600.000 €","625.000 €","650.000 €","675.000 €","700.000 €","725.000 €","750.000 €","775.000 €","800.000 €","825.000 €","850.000 €","875.000 €","900.000 €","925.000 €","950.000 €","975.000 €","1.000.000 €");
    var precoVendVal = new Array("25000","50000","75000","100000","125000","150000","175000","200000","225000","250000","275000","300000","325000","350000","375000","400000","425000","450000","475000","500000","525000","550000","575000","600000","625000","650000","675000","700000","725000","750000","775000","800000","825000","850000","875000","900000","925000","950000","975000","1000000");
    switch (valor){
        case 0:
            populateSelectWithNM(document.getElementsByName("preco1")[0], new Array(), new Array(),"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], new Array(), new Array(),"máx.,",true);
            searchForm.preco1.disabled = true;
            searchForm.preco2.disabled = true;
            break;
        case 1:     //Compra
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máx.",true);
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 2:     //Arredamento
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVal, precoTxt,"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVal, precoTxt,"máx.",true);
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 3:     //Trespasse
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mín.",false);
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máx.",true);
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        	    
    }
}   



function ValidaPesqClass () {
    var searchForm = getForm("imoPesq");
		searchForm.action = "/imobiliario/classificados_resultados.jsp";    	 
		searchForm.target = "";

    if(searchForm.distrito_id.selectedIndex == 0) {
        alert("Por favor, escolha o Distrito onde pretende fazer a sua pesquisa.");
        searchForm.distrito_id.focus();
        return false;
    }
  /**
    if(searchForm.concelho_id.selectedIndex == 0) {
        alert("Por favor, escolha o Concelho onde pretende fazer a sua pesquisa.");
        searchForm.concelho_id.focus();
        return false;
    }
  **/
    if(searchForm.tipo_id.selectedIndex == 0) {
        alert("Por favor, escolha o tipo do imóvel que pretende fazer a sua pesquisa.");
        searchForm.tipo_id.focus();
        return false;
    }

    intTipoId = searchForm.tipo_id.options[searchForm.tipo_id.selectedIndex].value;
    if(intTipoId == 3) {
    	objWindow = window.open("", "wdOpenEscritorios");
    	searchForm.action = "/imobiliario/redirect_to_escritorios.jsp";    	 
    	searchForm.target = "wdOpenEscritorios";
    } else if(intTipoId == 5) {
    	objWindow = window.open("", "wdOpenArmazens");
    	searchForm.action = "/imobiliario/redirect_to_armazens.jsp";    	 
    	searchForm.target = "wdOpenArmazens";
    } else if(intTipoId == 6) {
    	objWindow = window.open("", "wdOpenLojas");
    	searchForm.action = "/imobiliario/redirect_to_lojas.jsp";    	 
    	searchForm.target = "wdOpenLojas";
    }
    
    if(searchForm.tipologia_id1.selectedIndex > searchForm.tipologia_id2.selectedIndex &&
       searchForm.tipologia_id2.selectedIndex != 0) {
        alert("Seleccionar tipologia máxima igual ou superior\nà tipologia mínima.");
        searchForm.tipologia_id2.focus();
        return false;
    }
    if(searchForm.preco1.selectedIndex > searchForm.preco2.selectedIndex &&
       searchForm.preco2.selectedIndex != 0) {
        alert("Seleccionar preço máximo igual ou superior\nao preço mínimo.");
        searchForm.preco2.focus();
        return false;
    }
    searchForm.pos.value = 0;
    return true;
}
	    
    
    
