protected void PopulateNode(Object source, TreeNodeEventArgs e)
    {
        modelos = (SortedList<int, ModeloCompetencias>)Session["modelos"];
        TreeNode node = e.Node;
        String[] paths = node.ValuePath.Split('|');

        if (paths.Length == 4)
        {
            //expandir uma prática
            currModelo = modelos[int.Parse(paths[0])];
            Pratica currPratica = currModelo.getFamilia(paths[1]).getCompetencia(paths[2]).getPratica(paths[3]);
            currPratica.loadPerguntas("JC");

            foreach (Pergunta perg in currPratica.Perguntas.Values)
            {
                TreeNode n1 = new TreeNode(perg.Name, perg.PerguntaID.ToString());
                node.ChildNodes.Add(n1);
            }
        }
        else if (paths.Length == 3)
        {
            //expandir uma competência
            currModelo = modelos[int.Parse(paths[0])];
            Competencia currCompetencia = currModelo.getFamilia(paths[1]).getCompetencia(paths[2]);
            currCompetencia.loadPraticas("JC");
            foreach (Pratica prat in currCompetencia.Praticas.Values)
            {
                TreeNode n1 = new TreeNode(prat.Name, prat.PraticaID.ToString());
                //n1.PopulateOnDemand = true;
                node.ChildNodes.Add(n1);
                prat.loadPerguntas("JC");
                if (prat.Perguntas.Count > 0) n1.PopulateOnDemand = true;
            }

        }
        else if (paths.Length == 2)
        {
            //expandir uma família
            currModelo = modelos[int.Parse(paths[0])];
            FamiliaCompetencias currFamilia = currModelo.getFamilia(paths[1]);
            currFamilia.loadCompetencias("JC",null);
            foreach(Competencia comp in currFamilia.Competencias.Values)
            {
                TreeNode n1 = new TreeNode(comp.Name, comp.CompetenciaID.ToString());
                //n1.PopulateOnDemand = true;
                node.ChildNodes.Add(n1);
                comp.loadPraticas("JC");
                if (comp.Praticas.Count > 0)
                    n1.PopulateOnDemand = true;
            }
        }
        else if (paths.Length == 1)
        {
            currModelo = modelos[int.Parse(node.Value)];
            currModelo.loadFamilias("JC");

            foreach (FamiliaCompetencias fam in currModelo.Familias.Values)
            {
                TreeNode n1 = new TreeNode(fam.Name, fam.FamiliaID.ToString());
                //n1.PopulateOnDemand = true;
                node.ChildNodes.Add(n1);
                fam.loadCompetencias("JC",null);
                if (fam.Competencias.Count > 0)
                {
                    n1.PopulateOnDemand = true;
                }
            }

        }
        Session["currModelo"] = currModelo;
    }
    protected void ModelTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        if(modelos == null)
            modelos = (SortedList<int, ModeloCompetencias>)Session["modelos"];

        Session.Remove("mode");

        TreeView tv;
        TreeNode node;
        View selView = null;
        FamiliaCompetencias currFamilia;
        Competencia currCompetencia;
        Pratica currPratica;
        Pergunta currPergunta;

        tv = (TreeView)sender;
        node = tv.SelectedNode;

        String[] paths = node.ValuePath.Split('|');

        switch(paths.Length)
        {
            case 1:
                selView = ModeloView;
                currModelo = modelos[int.Parse(paths[0])];
                selectedObject = currModelo;
                // alterar texto dos botões de acção
                link1.Text = "Novo Modelo";
                link1.ToolTip = "Insere um novo modelo de competências";
                lblIDObjecto.Text = "";
                break;
            case 2:
                selView = FamiliaView;
                currModelo = modelos[int.Parse(paths[0])];
                currFamilia = currModelo.getFamilia(paths[1]);
                selectedObject = currFamilia;
                if (currFamilia.Competencias.Count > 0)
                    botRemoveFamilia.Visible = false;
                else
                    botRemoveFamilia.Visible = true;
                lblIDObjecto.Text = "";
                break;
            case 3:
                selView = CompetenciaView;
                currModelo = modelos[int.Parse(paths[0])];
                currFamilia = currModelo.getFamilia(paths[1]);
                currCompetencia = currFamilia.getCompetencia(paths[2]);
                selectedObject = currCompetencia;
                /*if (currCompetencia.Praticas.Count > 0)
                    botRemoveCompetencia.Visible = false;
                else*/
                    botRemoveCompetencia.Visible = true;
                    lblIDObjecto.Text = "C" + currCompetencia.CompetenciaID.ToString();
                break;
            case 4:
                selView = PraticaView;
                currModelo = modelos[int.Parse(paths[0])];
                currFamilia = currModelo.getFamilia(paths[1]);
                currCompetencia = currFamilia.getCompetencia(paths[2]);
                currPratica = currCompetencia.getPratica(paths[3]);
                selectedObject = currPratica;
                if (currPratica.Perguntas.Count > 0)
                    botRemovePratica.Visible = false;
                else
                    botRemovePratica.Visible = true;
                lblIDObjecto.Text = "R" + currPratica.PraticaID.ToString();
                break;

            case 5:
                selView = PerguntaView;
                currModelo = modelos[int.Parse(paths[0])];
                currFamilia = currModelo.getFamilia(paths[1]);
                currCompetencia = currFamilia.getCompetencia(paths[2]);
                currPratica = currCompetencia.getPratica(paths[3]);
                currPergunta = currPratica.getPergunta(paths[4]);
                selectedObject = currPergunta;
                lblIDObjectoP.Text = "P" + currPergunta.PerguntaID.ToString();
                break;
        }

        if (selView != null)
        {
            MView.SetActiveView(selView);
            Session["selectedObject"] = selectedObject;
            Session["currModelo"] = currModelo;
        }
        else
        {
            MView.Visible = false;
            commonDataFieldsTable.Visible = false;
        }
        updateCommonFields();
        updateSpecialFields();
    }