function getForm(formName) {
    // Test if is IE or Netscape (document.layers)
    if(document.all)
        return eval(formName);
    else
        return document.forms[formName];
}

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) {
    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;
        }
    }
}


function SetConcelho(count) {
    var searchForm = getForm("formPesqCanal");

    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("formPesqCanal");

    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 checkPesqParams () {
    var searchForm = getForm("formPesqCanal");

    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 de imóvel a considerar.");
		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.rs_offset.value = 0;
    return true;
}

function OnChangeDistrito() {
    var searchForm = getForm("formPesqCanal");

    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("formPesqCanal");

    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("formPesqCanal");

    if(searchForm["idfreguesia"] != undefined)
        searchForm.idfreguesia.value = searchForm.freguesia_id.options[searchForm.freguesia_id.selectedIndex].value;
}

function OnChangeClassTipo() {
	var searchForm = getForm("formPesqCanal");
	
	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("formPesqCanal");	
	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("formPesqCanal");
    var valor = parseInt(searchForm.operacao.value);
    var precoTxt = new Array("250 €","500 €","750 €","1.000 €","1.250 €","1.500 €","1.750 €",">2.000 €");
    var precoVal = new Array("250","500","750","100","1250","1500","1750","2000");
    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ínimo");
            populateSelectWithNM(document.getElementsByName("preco2")[0], new Array(), new Array(),"máximo,");
            searchForm.preco1.disabled = true;
            searchForm.preco2.disabled = true;
            break;
        case 1:     //Compra
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mínimo");
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máximo");
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
        case 2:     //Arredamento
            //populateSelectWithNM(document.getElementsByName("preco1")[0], precoVal, precoTxt,"mínimo");
            //populateSelectWithNM(document.getElementsByName("preco2")[0], precoVal, precoTxt,"máximo");
            searchForm.preco1.disabled = true;
            searchForm.preco2.disabled = true;
            break;
        case 3:     //Trespasse
            populateSelectWithNM(document.getElementsByName("preco1")[0], precoVendVal, precoVendTxt,"mínimo");
            populateSelectWithNM(document.getElementsByName("preco2")[0], precoVendVal, precoVendTxt,"máximo");
            searchForm.preco1.disabled = false;
            searchForm.preco2.disabled = false;
            break;
    }
}

function OnChangeOperation() {
    var searchForm = getForm("formPesqCanal");
    var valor = parseInt(searchForm.operacao.value);
    var precoTxt = new Array("250 €","500 €","750 €","1.000 €","1.250 €","1.500 €","1.750 €",">2.000 €");
    var precoVal = new Array("250","500","750","100","1250","1500","1750","2000");
    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.");
            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.");
            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 ValidaPesqClass () {
    var searchForm = getForm("formPesqCanal");

    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.canal.value == "CUP"){
    	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.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;
    }
    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;
}
	    
    
    

