private void ok_button_Click(object sender, RoutedEventArgs e)
        {
            if (!isTextChanged) // if text is not changed
            {
                ValueLabelsSubDialog.GetWindow(this).Close();
            }
            else
            {
                if (factormap != null && factormap.Count < 1)//if list is empty. just close the dialog
                {
                    ValueLabelsSubDialog.GetWindow(this).Close();
                    return;
                }
                bool isEmpty            = false;
                bool isDuplicateLvlName = false;
                ////bool isDuplicateLvlNum = false;
                int i   = 0;
                int len = factormap.Count;

                #region if user enters more than one levels separated by comma
                //The last item in the list is the one that has the new level(s)
                FactorMap lastitem = factormap.Last();
                if (lastitem.textbox.Contains(','))
                {
                    char[]   sepa        = { ',' };
                    string[] newlevels   = lastitem.textbox.Split(sepa);
                    bool     isfirstItem = true;

                    //add new levels but remember to modify the 'lastitem' that was having all the new levels.
                    //First new level can be fed to 'lasitem'. Second onwards create new item per new level.
                    for (int newi = 0; newi < newlevels.Length; newi++)
                    {
                        //ignore blanks
                        if (newlevels[newi].Trim() == null || newlevels[newi].Trim().Length < 1)
                        {
                            continue;
                        }

                        //ignore duplicates
                        {
                            isDuplicateLvlName = false;
                            for (int j = 0; j < len; j++)
                            {
                                //duplicate level names
                                if ((newlevels[newi].Trim().Length > 0) && (newlevels[newi].Trim().Equals(factormap.ElementAt(j).textbox.Trim())))//blank fields should not be checked
                                {
                                    isDuplicateLvlName = true;
                                    break;
                                }
                            }
                            if (isDuplicateLvlName)//ignore duplicates. When multiple new levels are entered by user with duplicates
                            {
                                continue;
                            }
                        }

                        if (isfirstItem)//first item goes to 'lastitem' object
                        {
                            lastitem.textbox = newlevels[newi].Trim();
                            isfirstItem      = false;
                        }
                        else
                        {
                            FactorMap newitemNewlevel = new FactorMap();
                            newitemNewlevel.textbox = newlevels[newi].Trim();
                            newitemNewlevel.labels  = "";
                            factormap.Add(newitemNewlevel);
                        }
                    }
                }
                #endregion
                foreach (FactorMap m in factormap)
                {
                    string s = m.labels + ":" + m.textbox;
                    //MessageBox.Show(s);

                    ////checking duplicates ////
                    i++;
                    for (int j = i; j < len; j++)
                    {
                        //duplicate level names
                        if ((m.textbox.Trim().Length > 0) && (m.textbox == factormap.ElementAt(j).textbox.Trim()))//blank fields should not be checked
                        {
                            isDuplicateLvlName = true;
                            break;
                        }

                        //////Duplicate level number
                        ////if ((m.numlevel.ToString().Trim().Length > 0) && (m.numlevel.ToString() == factormap.ElementAt(j).textbox.Trim()))//blank fields should not be checked
                        ////{
                        ////    isDuplicateLvlNum = true;
                        ////    break;
                        ////}
                    }

                    ////checking empty for all except the blank field(which is for new level)///
                    if (m.textbox.Trim().Length == 0 &&
                        !m.labels.Trim().Equals(BSky.GlobalResources.Properties.UICtrlResources.AddFactorLevelMsg.Trim()))
                    {
                        isEmpty = true;
                    }
                }
                OKclicked = true;
                if (isDuplicateLvlName)  //// || isDuplicateLvlNum)
                {
                    ////MessageBox.Show("Duplicate Level Names(or level numbers) are not allowed.", "Error! Duplicate not allowed.", MessageBoxButton.OK, MessageBoxImage.Error);
                    MessageBox.Show("Duplicate Level Names are not allowed.", "Error! Duplicate not allowed.", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                if (isEmpty)
                {
                    if (MessageBox.Show("Empty values not allowed.", "Warning.", MessageBoxButton.OK, MessageBoxImage.Warning) == MessageBoxResult.OK)
                    {
                        //restore cells with blank values
                        foreach (FactorMap m in factormap)
                        {
                            if (m.textbox == null || m.textbox.Trim().Length == 0)
                            {
                                m.textbox = m.labels;
                            }
                        }
                        Listbox.ItemsSource = null;
                        Listbox.ItemsSource = factormap;
                        return;
                    }
                    else if (MessageBox.Show("Empty factors will be converted to NAs.", "Warning.", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
                    {
                        ValueLabelsSubDialog.GetWindow(this).Close();
                    }
                    else
                    {
                        //try to put back original value that was replaced with space.
                        //If there are multiple levels(fields) made as blank, it will be trickier to get all of them back in UI dialog.
                        foreach (FactorMap m in factormap)
                        {
                            if (m.textbox == null || m.textbox.Trim().Length == 0)
                            {
                                m.textbox = m.labels;
                            }
                        }
                        Listbox.ItemsSource = null;
                        Listbox.ItemsSource = factormap;
                    }
                }
                else
                {
                    ValueLabelsSubDialog.GetWindow(this).Close();
                }
            }
        }
        private bool checkMaxFactorsAndLoadFMap()
        {
            bool success = true;
            IAnalyticsService analyticServ = LifetimeService.Instance.Container.Resolve <IAnalyticsService>();

            string fromTo     = "from " + changeFrom + " to " + changeTo;
            object numfactors = null;

            string[] factors;
            if (changeFrom.Equals("Scale") && (changeTo.Equals("Nominal") || changeTo.Equals("Ordinal")))   // S to N/O
            {
                //get the list of factors(numeric)
                numfactors = analyticServ.GetColNumFactors(colName, datasetName);

                if (numfactors != null && (numfactors as UAReturn).SimpleTypeData.GetType().Name == "String[]")
                {
                    factors = (string[])(numfactors as UAReturn).SimpleTypeData;
                    if (factors.Length > maxfactors)
                    {
                        string msg = "Conversion " + fromTo + " cannot be done, as number of factors are greater than " + maxfactors + ".";
                        if (changeMeasureCombo.SelectedIndex != 2)
                        {
                            MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        changeMeasureCombo.SelectedIndex = 2;
                        success = false;
                    }
                    else
                    {
                        _factormapList = new List <FactorMap>();
                        FactorMap fm = null;
                        foreach (string str in factors)
                        {
                            if (!str.Trim().Equals("."))//exclude '.'
                            {
                                fm         = new FactorMap();
                                fm.labels  = str;
                                fm.textbox = str;
                                _factormapList.Add(fm);
                            }
                        }
                    }
                }
                else//if empty levels/factors
                {
                    string msg = "Conversion " + fromTo + " cannot be done, as number of values are less than 1. Please enter some values in data grid.";
                    if (changeMeasureCombo.SelectedIndex != 2)
                    {
                        MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    changeMeasureCombo.SelectedIndex = 2;
                    success = false;
                }
            }
            else if (changeTo.Equals("Scale") && (changeFrom.Equals("Nominal") || changeFrom.Equals("Ordinal")))// N/O to S
            {
                //get list of level names.
                _factormapList = analyticServ.GetColumnFactormap(colName, datasetName);
            }
            //else
            //{
            //    success = false;
            //}
            //MessageBox.Show("Selection changed"+s);
            return(success);
        }