function getFormEmpresa(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 SetConcelhoEmpresa(count) {
    var searchForm = getFormEmpresa("imoPesqEmpresa");

    if(searchForm.concelho_id.length <= 1) {
        if(count < 50)
            setTimeout("SetConcelhoEmpresa(" + (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;
            OnChangeConcelhoEmpresa();    
        }
    }
}

function ValidaPesqEmpresa () {
    var searchForm = getFormEmpresa("imoPesqEmpresa");
    
	s = Trim(searchForm.nome_empresa.value);
    
    if(searchForm.distrito_id.selectedIndex == 0 && s.length == 0) {
        alert("Por favor, terá de preencher o Distrito ou o nome do Empresa a pesquisar.");
        searchForm.distrito_id.focus();
        return false;
    }
    
	if(searchForm.distrito_id.selectedIndex != 0 && s.length == 0) {
		searchForm.nome_empresa.value = '';
		return true;
	}
    
    
	
    
	/*if(searchForm.categoria_id.selectedIndex == 0) {
			alert("Por favor, escolha a Categoria onde pretende fazer a sua pesquisa.");
			searchForm.categoria_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;
    }*/
  
    return true;
}


function ValidaPesqEmpresaNome () {
	var searchForm = getFormEmpresa("PesqEmpresaNome");

	if(searchForm.nome_empresa.value.length == 0) {
		alert("Por favor, preencha o campo com o nome da empresa.");
		searchForm.nome_empresa.focus();
		return false;
	}
    
    return true;
}

function OnChangeDistritoEmpresa() {
    var searchForm = getFormEmpresa("imoPesqEmpresa");	
    
    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 OnChangeConcelhoEmpresa() {
    var searchForm = getFormEmpresa("imoPesqEmpresa");

    if(searchForm["idconcelho"] != undefined)
        searchForm.idconcelho.value = searchForm.concelho_id.options[searchForm.concelho_id.selectedIndex].value;  
}
