protected void btnImportFromCsv_Click(object sender, EventArgs e)
        {
            if ( !csvFile.HasFile )
            {
                Utils.AppendScript( "openPopUp( 'importCsv' );" );
                Utils.AppendScript( "location.href='#agencies'" );
                Utils.AppendScript( string.Format( "alert( '{0}' );", Resources.Messages.err_no_file_uploaded ) );
                return;
            }

            IAgencySchemeMutableObject agencyScheme = GetAgencySchemeFromSession();
            if (agencyScheme == null) return;
            List<csvAgency> agencies = new List<csvAgency>();
            bool errorInUploading = false;

            try
            {
                string filenameWithoutExtension = string.Format("{0}_{1}_{2}", Path.GetFileName(csvFile.FileName).Substring(0, csvFile.FileName.Length - 4), Session.SessionID, DateTime.Now.ToString().Replace('/', '_').Replace(':', '_').Replace(' ', '_'));
                string filename = string.Format("{0}.csv", filenameWithoutExtension);
                string logFilename = string.Format("{0}.log", filenameWithoutExtension);
                csvFile.SaveAs(Server.MapPath("~/csv_agencyschemes_files/") + filename);

                StreamReader reader = new StreamReader(Server.MapPath("~/csv_agencyschemes_files/") + filename);
                StreamWriter logWriter = new StreamWriter(Server.MapPath("~/csv_agencyschemes_import_logs/") + logFilename, true);
                logWriter.WriteLine(string.Format("LOG RELATIVO A CARICAMENTO DEL AGENCY SCHEME [ ID => \"{0}\"  AGENCY_ID => \"{1}\"  VERSION => \"{2}\" ]  |  LINGUA SELEZIONATA: {3}\n", agencyScheme.Id.ToString(), agencyScheme.AgencyId.ToString(), agencyScheme.Version.ToString(), cmbLanguageForCsv.SelectedValue.ToString()));
                logWriter.WriteLine("-----------------------------------------------------------------------------------------------------------------------------\n");
                reader.ReadLine();
                string wrongRowsMessage = string.Empty;
                string wrongRowsMessageForUser = string.Empty;
                string wrongFileLines = string.Empty;
                int currentRow = 1;

                char separator = txtSeparator.Text.Trim().Equals( string.Empty ) ? ';' : txtSeparator.Text.Trim().ElementAt( 0 );

                while (!reader.EndOfStream)
                {
                    string  currentFileLine = reader.ReadLine();
                    string[] fields = currentFileLine.Split( separator );
                    if (fields.Length != 3)
                    {
                        errorInUploading = true;
                        wrongRowsMessage += string.Format(Resources.Messages.err_csv_import_line_bad_format, currentRow + 1);
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_line_bad_format_gui, currentRow + 1);
                        wrongFileLines += string.Format( "{0}\n", currentFileLine );
                        logWriter.WriteLine(string.Format(Resources.Messages.err_csv_import_line_bad_format, currentRow + 1));
                        logWriter.Flush();
                        currentRow++;
                        continue;
                    }
                    if (fields[0].Trim().Equals("\"\"") || fields[0].Trim().Equals(string.Empty))
                    {
                        errorInUploading = true;
                        wrongRowsMessage += string.Format(Resources.Messages.err_csv_import_id_missing, currentRow + 1);
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_id_missing_gui, currentRow + 1);
                        wrongFileLines += string.Format( "{0}\n", currentFileLine );
                        logWriter.WriteLine(string.Format(Resources.Messages.err_csv_import_id_missing, currentRow + 1));
                        logWriter.Flush();
                        currentRow++;
                        continue;
                    }
                    if (fields[1].Trim().Equals("\"\"") || fields[1].Trim().Equals(string.Empty))
                    {
                        errorInUploading = true;
                        wrongRowsMessage += string.Format(Resources.Messages.err_csv_import_name_missing, currentRow + 1);
                        wrongRowsMessageForUser += string.Format(Resources.Messages.err_csv_import_name_missing_gui, currentRow + 1);
                        wrongFileLines += string.Format( "{0}\n", currentFileLine );
                        logWriter.WriteLine(string.Format(Resources.Messages.err_csv_import_name_missing, currentRow + 1));
                        logWriter.Flush();
                        currentRow++;
                        continue;
                    }
                    agencies.Add(new csvAgency(fields[0].ToString().Replace("\"", string.Empty ), fields[1].ToString().Replace("\"", string.Empty ), fields[2].ToString().Replace("\"", string.Empty )));
                    currentRow++;
                }
                if ( !errorInUploading )
                {
                    logWriter.WriteLine("Andato tutto a buon fine con questo file!");
                }
                else
                {
                    lblImportCsvErrors.Text = wrongRowsMessageForUser;
                    lblImportCsvWrongLines.Text = wrongFileLines;
                    Utils.AppendScript("openP('importCsvErrors',500);");
                }
                logWriter.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                Utils.AppendScript(string.Format("Upload status: The file could not be uploaded. The following error occured: {0}", ex.Message));
            }

            foreach (csvAgency agency in agencies)
            {

                IEnumerable<IAgencyMutableObject> tmpAgencies = (from agen in agencyScheme.Items where agen.Id == agency.agency select agen).OfType<IAgencyMutableObject>();

                IAgencyMutableObject tmpAgency;

                if (!(tmpAgencies.Count() > 0))
                {
                    tmpAgency = new AgencyMutableCore();
                    tmpAgency.Id = agency.agency;
                    tmpAgency.AddName(cmbLanguageForCsv.SelectedValue.ToString(), agency.name);
                    tmpAgency.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), agency.description);
                    agencyScheme.AddItem(tmpAgency);
                }
                else
                {
                    tmpAgency = tmpAgencies.First();
                    tmpAgency.Id = agency.agency;
                    tmpAgency.AddName(cmbLanguageForCsv.SelectedValue.ToString(), agency.name);
                    tmpAgency.AddDescription(cmbLanguageForCsv.SelectedValue.ToString(), agency.description);
                }
            }

            if (!SaveInMemory(agencyScheme))
                return;
            BindData();
            if ( !errorInUploading )
            {
                Utils.ShowDialog( Resources.Messages.succ_operation );
            }
            Utils.AppendScript("location.href='#agencies';");
        }
        protected void btnUpdateAgency_Click(object sender, EventArgs e)
        {
            // Get Input field
            string agency_id = txt_id_update.Text.Trim();
            IList<ITextTypeWrapperMutableObject> agency_names = AddTextName_Update.TextObjectList;
            IList<ITextTypeWrapperMutableObject> agency_descs = AddTextDescription_Update.TextObjectList;
            // string code_order_str = txtUpdateCodeOrder.Text.Trim();

            // Get Current Object Session
            IAgencySchemeMutableObject agencyScheme = GetAgencySchemeFromSession();
            IEnumerable<IAgencyMutableObject> _rc = (from x in agencyScheme.Items where x.Id == agency_id select x).OfType<IAgencyMutableObject>();
            if (_rc.Count() == 0) return;

            IAgencyMutableObject agency = _rc.First();

            IAgencyMutableObject _bAgency = new AgencyMutableCore();

            int indexAgency = agencyScheme.Items.IndexOf(agency);
            int indexOrder = 0;
            try
            {

                #region CONCEPT ID
                if (!agency_id.Equals(string.Empty) && ValidationUtils.CheckIdFormat(agency_id))
                {
                    _bAgency.Id = agency_id;
                }
                else
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_id_format;
                    Utils.AppendScript( "openPopUp('df-Agency-update', 600 );" );
                    Utils.AppendScript("location.href= '#concepts';");
                    return;
                }
                #endregion

                #region AGENCY NAMES
                if (agency_names != null)
                {
                    foreach (var tmpName in agency_names)
                    {
                        _bAgency.AddName(tmpName.Locale, tmpName.Value);
                    }
                }
                else
                {
                    lblErrorOnUpdate.Text = Resources.Messages.err_list_name_format;
                    Utils.AppendScript( "openPopUp('df-Agency-update', 600 );" );
                    Utils.AppendScript("location.href= '#concepts';");
                    return;
                }
                #endregion

                #region AGENCY DESCRIPTIONS
                if (agency_descs != null)
                {
                    foreach (var tmpDescription in agency_descs)
                    {
                        _bAgency.AddDescription(tmpDescription.Locale, tmpDescription.Value);
                    }
                }
                #endregion

                agencyScheme.Items.Remove(agency);
                agencyScheme.Items.Insert(indexAgency, _bAgency);

                var canRead = agencyScheme.ImmutableInstance;

            }
            catch (Exception ex) // ERRORE GENERICO!
            {
                agencyScheme.Items.Remove(_bAgency);
                agencyScheme.Items.Insert(indexAgency, agency);

                Utils.ShowDialog(Resources.Messages.err_agency_update, 300, Resources.Messages.err_title);
                Utils.AppendScript("location.href='#agencies';");

            }

            if (!SaveInMemory(agencyScheme))
                return;

            BindData();
            lblErrorOnUpdate.Text = string.Empty;
            Utils.AppendScript("location.href='#agencies';");
        }
        private IAgencySchemeMutableObject InsertAgencyInAgencyscheme(IAgencySchemeMutableObject agencyScheme)
        {
            if (agencyScheme == null) return null;

            IAgencyMutableObject agency = new AgencyMutableCore();

            string agency_id = txt_id_new.Text.Trim();

            IList<ITextTypeWrapperMutableObject> agency_names = AddTextName_new.TextObjectList;
            IList<ITextTypeWrapperMutableObject> agency_descs = AddTextDescription_new.TextObjectList;
            // string code_order_str = txtOrderNewCode.Text.Trim();     ----- ORDINE

            #region AGENCYSCHEME ID
            if (ValidationUtils.CheckIdFormat(agency_id))
            {
                agency.Id = agency_id;
            }
            else
            {
                //lblErrorOnNewInsert.Text = Resources.Messages.err_id_format;
                Utils.AppendScript("location.href= '#agencies';");
                OpenAddAgencyPopUp();
                Utils.ShowDialog(Resources.Messages.err_id_format, 300);
                return null;
            }

            IEnumerable<IAgencyMutableObject> agencies = (from c in agencyScheme.Items where c.Id == agency_id select c).OfType<IAgencyMutableObject>();
            if (agencies.Count() > 0)
            {
                //lblErrorOnNewInsert.Text = Resources.Messages.err_id_exist;
                Utils.AppendScript("location.href= '#agencies';");
                OpenAddAgencyPopUp();
                Utils.ShowDialog(Resources.Messages.err_id_exist, 300);
                return null;
            }
            #endregion

            #region AGENCY NAMES
            if (agency_names != null)
            {
                foreach (var tmpName in agency_names)
                {
                    agency.AddName(tmpName.Locale, tmpName.Value);
                }
            }
            else
            {
                //lblErrorOnNewInsert.Text = Resources.Messages.err_list_name_format;
                Utils.AppendScript("location.href= '#agencies';");
                OpenAddAgencyPopUp();
                Utils.ShowDialog(Resources.Messages.err_list_name_format, 300);
                return null;
            }
            #endregion

            #region AGENCY DESCRIPTIONS
            if (agency_descs != null)
            {
                foreach (var tmpDescription in agency_descs)
                {
                    agency.AddDescription(tmpDescription.Locale, tmpDescription.Value);
                }
            }
            #endregion

            agencyScheme.Items.Add(agency);

            try
            {
                // Ultimo controllo se ottengo Immutable istanze validazione completa
                var canRead = agencyScheme.ImmutableInstance;
            }
            catch (Exception ex)
            {
                agencyScheme.Items.Remove(agency);

                return null;

            }
            return agencyScheme;
        }