protected void grdConditions_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                int conditionId = 0; conditionId = int.Parse(((DropDownList)e.Row.FindControl("ddlName")).SelectedValue);
                DropDownList ddlOperator = ((DropDownList)e.Row.FindControl("ddlOperator"));

                if (conditionId > 0)
                {
                    DropDownList ddlConditionNumber = ((DropDownList)e.Row.FindControl("ddlConditionNumber"));

                    FmViewTDS dataSet = new FmViewTDS();
                    dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
                    FmViewConditionNew model = new FmViewConditionNew(dataSet);
                    int conditionNumber = model.GetNewConditionNumber();
                    ddlConditionNumber.SelectedValue = conditionNumber.ToString();

                    FmTypeViewConditionGateway fmTypeViewConditionGateway = new FmTypeViewConditionGateway();
                    fmTypeViewConditionGateway.LoadByFmTypeConditionId(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                    string type = fmTypeViewConditionGateway.GetType(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);

                    if (ddlOperator.SelectedItem == null)
                    {
                        FmTypeViewOperatorList fmTypeViewOperatorList = new FmTypeViewOperatorList(new DataSet());
                        fmTypeViewOperatorList.LoadAndAddItem(type, Int32.Parse(hdfCompanyId.Value));
                        ddlOperator.DataSource = fmTypeViewOperatorList.Table;
                        ddlOperator.DataValueField = "Sign";
                        ddlOperator.DataTextField = "Operator";
                        ddlOperator.DataBind();
                        ddlOperator.SelectedIndex = 0;
                    }

                    if ((type == "String") || (type == "Date") || (type == "Int") || (type == "Decimal"))
                    {
                        ddlOperator.Enabled = true;

                        TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                        tbxValue.Visible = true;

                        DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                        ddlValue.Visible = false;

                        RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                        rbtnYes.Visible = false;

                        RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                        rbtnNo.Visible = false;
                    }

                    if ((type == "FixedItems") || (type == "DynamicItems") || (type == "Boolean"))
                    {
                        ddlOperator.Enabled = false;

                        if (type == "FixedItems")
                        {
                            DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                            ddlValue.Visible = true;

                            TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                            tbxValue.Visible = false;

                            RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                            rbtnYes.Visible = false;

                            RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                            rbtnNo.Visible = false;
                        }

                        if (type == "DynamicItems")
                        {
                            DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                            ddlValue.Visible = true;

                            TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                            tbxValue.Visible = false;

                            RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                            rbtnYes.Visible = false;

                            RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                            rbtnNo.Visible = false;
                        }

                        if (type == "Boolean")
                        {
                            DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                            ddlValue.Visible = false;

                            TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                            tbxValue.Visible = false;

                            RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                            rbtnYes.Visible = true;

                            RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                            rbtnNo.Visible = true;
                        }
                    }
                }
            }

            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                FmViewTDS dataSet = new FmViewTDS();
                dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
                FmViewConditionNewGateway gateway = new FmViewConditionNewGateway(dataSet);
                int id = int.Parse(((Label)e.Row.FindControl("lblId")).Text);

                int conditionId = gateway.GetConditionId(id);
                DropDownList ddlName = ((DropDownList)e.Row.FindControl("ddlName"));
                DropDownList ddlOperator = ((DropDownList)e.Row.FindControl("ddlOperator"));

                if (conditionId > 0)
                {
                    FmTypeViewConditionGateway fmTypeViewConditionGateway = new FmTypeViewConditionGateway();
                    fmTypeViewConditionGateway.LoadByFmTypeConditionId(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                    string type = fmTypeViewConditionGateway.GetType(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);

                    ddlName.DataBind();
                    ddlName.SelectedValue = conditionId.ToString();

                    FmTypeViewOperatorList fmTypeViewOperatorList = new FmTypeViewOperatorList(new DataSet());
                    fmTypeViewOperatorList.LoadAndAddItem(type, Int32.Parse(hdfCompanyId.Value));
                    ddlOperator.DataSource = fmTypeViewOperatorList.Table;
                    ddlOperator.DataValueField = "Sign";
                    ddlOperator.DataTextField = "Operator";
                    ddlOperator.DataBind();
                    ddlOperator.SelectedValue = gateway.GetSign(id);

                    if ((type == "String") || (type == "Date") || (type == "Int") || (type == "Decimal") || (type == "Distance"))
                    {
                        ddlOperator.Enabled = true;

                        TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                        tbxValue.Visible = true;
                        tbxValue.Text = gateway.GetValue_(id);

                        DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                        ddlValue.Visible = false;

                        RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                        rbtnYes.Visible = false;

                        RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                        rbtnNo.Visible = false;
                    }

                    if ((type == "FixedItems") || (type == "DynamicItems") || (type == "Boolean"))
                    {
                        ddlOperator.Enabled = false;

                        if (type == "FixedItems")
                        {
                            DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                            ddlValue.Visible = true;

                            // Prepare initial data
                            // ... For view type
                            FmTypeViewConditionItemList fmTypeViewConditionItemList = new FmTypeViewConditionItemList(new DataSet());
                            fmTypeViewConditionItemList.LoadAndAddItemInView(hdfFmType.Value, Int32.Parse(hdfCompanyId.Value), conditionId);
                            ddlValue.DataSource = fmTypeViewConditionItemList.Table;
                            ddlValue.DataValueField = "Name";
                            ddlValue.DataTextField = "Name";
                            ddlValue.DataBind();
                            ddlValue.SelectedValue = gateway.GetValue_(id);

                            TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                            tbxValue.Visible = false;

                            RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                            rbtnYes.Visible = false;

                            RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                            rbtnNo.Visible = false;
                        }

                        if (type == "DynamicItems")
                        {
                            DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                            ddlValue.Visible = true;

                            // Prepare initial data
                            // ... For view type
                            if (ddlName.SelectedItem.Text == "Category")
                            {
                                //Category for Units
                                Category category = new Category(categoriesTDS);
                                category.LoadAllUnitTypes(Int32.Parse(hdfCompanyId.Value));

                                Session["categoriesTDSForAddView"] = categoriesTDS;

                                if (category.Table.Rows.Count > 0)
                                {
                                    GetNodeForCategory(0, ddlValue);//Equipments
                                    level = -1;
                                    GetNodeForCategory(-1, ddlValue);//Vehicles

                                    ddlValue.DataBind();
                                    string categorySelected = gateway.GetValue_(id);

                                    foreach (ListItem li in ddlValue.Items)
                                    {
                                        if (li.Text.Contains(categorySelected))
                                        {
                                            li.Selected = true;
                                        }
                                    }
                                }
                                else
                                {
                                    ddlValue.Visible = false;
                                }
                            }
                            else
                            {
                                //State for Services
                                FmTypeViewStateList fmTypeViewStateList = new FmTypeViewStateList(new DataSet());
                                fmTypeViewStateList.LoadAndAddItem(Int32.Parse(hdfCompanyId.Value));

                                if (fmTypeViewStateList.Table.Rows.Count > 0)
                                {
                                    ddlValue.DataSource = fmTypeViewStateList.Table;
                                    ddlValue.DataValueField = "State";
                                    ddlValue.DataTextField = "State";
                                    ddlValue.DataBind();
                                    ddlValue.SelectedValue = gateway.GetValue_(id);
                                }
                                else
                                {
                                    ddlValue.Visible = false;
                                }
                            }

                            TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                            tbxValue.Visible = false;

                            RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                            rbtnYes.Visible = false;

                            RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                            rbtnNo.Visible = false;
                        }

                        if (type == "Boolean")
                        {
                            DropDownList ddlValue = ((DropDownList)e.Row.FindControl("ddlValue"));
                            ddlValue.Visible = false;

                            TextBox tbxValue = ((TextBox)e.Row.FindControl("tbxValue"));
                            tbxValue.Visible = false;

                            RadioButton rbtnYes = ((RadioButton)e.Row.FindControl("rbtnYes"));
                            rbtnYes.Visible = true;

                            RadioButton rbtnNo = ((RadioButton)e.Row.FindControl("rbtnNo"));
                            rbtnNo.Visible = true;

                            if (gateway.GetValue_(id) == "Yes")
                            {
                                rbtnYes.Checked = true;
                            }
                            else
                            {
                                rbtnNo.Checked = true;
                            }
                        }
                    }
                }
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP6 - SUMMARY
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP6 - SUMMARY - METHODS
        //
        private void StepSummaryIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Summary";

            // Tag Page
            hdfStep.Value = "StepSummary";
            hdfUpdate.Value = "yes";

            // Initialize summary
            FmView fmView = new FmView(fmViewTDS);

            // Process view
            FmViewTDS dataset = new FmViewTDS();
            dataset.FmViewDisplayTemp.Merge(fmViewDisplayTemp, true);
            dataset.FmViewConditionNew.Merge(fmViewConditionNew, true);
            dataset.FmViewSortTemp.Merge(fmViewSortTemp, true);

            FmViewDisplayTemp modelFmViewDisplayTemp = new FmViewDisplayTemp(dataset);
            FmViewConditionNew modelFmViewConditionNew = new FmViewConditionNew(dataset);
            FmViewSortTemp modelFmViewSortTemp = new FmViewSortTemp(dataset);

            string summary = "";
            summary = summary + "Name: " + tbxName.Text + "\n\n";
            summary = summary + "Columns To Display; " + modelFmViewDisplayTemp.GetColumnsToDisplay() + "\n\n";
            summary = summary + "Conditions: " + modelFmViewConditionNew.GetConditionsForSummary(hdfFmType.Value, int.Parse(hdfCompanyId.Value)) + "\n\n";
            summary = summary + "Logical Expression: " + tbxLogic.Text + "\n\n";
            summary = summary + "Order by: " + modelFmViewSortTemp.GetSortForSummary() + "\n\n";

            tbxSummary.Text = summary;
        }
        // ////////////////////////////////////////////////////////////////////////
        //
        // STEP4 - LOGIC
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP4 - LOGIC - AUXILIAR EVENTS
        //
        protected void cvLogic_ServerValidate(object source, ServerValidateEventArgs args)
        {
            LogicValidator logicValidator = new LogicValidator();
            string message;

            ArrayList conditions = new ArrayList();

            // Create dataset
            FmViewTDS dataSet = new FmViewTDS();
            dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);

            // Process new conditions
            FmViewConditionNew model = new FmViewConditionNew(dataSet);
            conditions = model.GetConditions();

            // Get changes
            fmViewConditionNew.Rows.Clear();
            fmViewConditionNew.Merge(model.Table);

            // Store tables
            Session["fmViewConditionNew"] = fmViewConditionNew;
            if (conditions.Count > 0)
            {
                bool bOk = logicValidator.Validate(tbxLogic.Text, conditions, out message);

                if (bOk)
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                    cvLogic.ErrorMessage = message;
                }
            }
            else
            {
                if (tbxLogic.Text == "")
                {
                    args.IsValid = true;
                }
                else
                {
                    args.IsValid = false;
                    cvLogic.ErrorMessage = "There are no conditions, The logical expression must be empty";
                }
            }
        }
        private void Save()
        {
            // Process view
            FmViewTDS dataset = new FmViewTDS();
            dataset.FmViewDisplayTemp.Merge(fmViewDisplayTemp, true);
            dataset.FmViewConditionNew.Merge(fmViewConditionNew, true);
            dataset.FmViewSortTemp.Merge(fmViewSortTemp, true);

            FmViewDisplayTemp modelFmViewDisplayTemp = new FmViewDisplayTemp(dataset);
            FmViewConditionNew modelFmViewConditionNew = new FmViewConditionNew(dataset);
            FmViewSortTemp modelFmViewSortTemp = new FmViewSortTemp(dataset);

            // Get parameters
            int viewId = int.Parse(hdfViewId.Value);
            int companyId = int.Parse(hdfCompanyId.Value);
            int loginId = Convert.ToInt32(Session["loginID"]);
            string name = tbxName.Text;
            string type = ddlType.SelectedValue;
            string logic = tbxLogic.Text;
            string sqlCommand = GetSqlCommand();
            string fmType = hdfFmType.Value;

            if (!(Convert.ToBoolean(Session["sgLFS_VIEWS_ADD"])) && ((string)Request.QueryString["fm_type"] == "Services"))
            {
                if ((Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_SERVICES_VIEW"])) && ((string)Request.QueryString["fm_type"] == "Services"))
                {
                    type = "Personal";
                }
            }

            if (!(Convert.ToBoolean(Session["sgLFS_VIEWS_ADD"])) && ((string)Request.QueryString["fm_type"] == "Units"))
            {
                if ((Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_UNITS_VIEW"])) && ((string)Request.QueryString["fm_type"] == "Units"))
                {
                    type = "Personal";
                }
            }

            // Save to database
            DB.Open();
            DB.BeginTransaction();

            try
            {
                FmView fmView = new FmView(null);
                fmView.InsertDirect(viewId, loginId, name, type, logic, sqlCommand, fmType, false, companyId);

                modelFmViewDisplayTemp.Save();
                modelFmViewConditionNew.Save(viewId, companyId, fmType);
                modelFmViewSortTemp.Save(viewId, fmType, companyId);

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private bool StepConditionsNext()
        {
            // Validate data
            bool validData = true;

            // Edit if there is data to save
            Page.Validate("ConditionsUpdate");
            if (!Page.IsValid) validData = false;

            if (validData)
            {
                // Conditions Gridview, if the gridview is edition mode
                if (grdConditions.EditIndex >= 0)
                {
                    grdConditions.UpdateRow(grdConditions.EditIndex, true);
                    grdConditions.DataBind();
                }
            }

            Page.Validate("ConditionsNext");
            if (!Page.IsValid) validData = false;

            if (validData)
            {
                // Add conditions if exists at footer
                if (ValidateFooterNext())
                {
                    GrdConditionsAdd();
                }

                // create dataset
                FmViewTDS dataSet = new FmViewTDS();
                dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);

                // Process new conditions
                FmViewConditionNew model = new FmViewConditionNew(dataSet);

                tbxLogic.Text = model.GetConditionsByDefault();

                // Get changes
                fmViewConditionNew.Rows.Clear();
                fmViewConditionNew.Merge(model.Table);

                // Store tables
                Session.Remove("fmViewConditionNewDummy");
                Session["fmViewConditionNew"] = fmViewConditionNew;
                hdfStep.Value = "StepConditions";

                return true;
            }
            else
            {
                return false;
            }
        }
        private string GetWhere()
        {
            // Process view
            FmViewTDS dataset = new FmViewTDS();
            dataset.FmViewConditionNew.Merge(fmViewConditionNew, true);

            FmViewConditionNew modelFmViewConditionNew = new FmViewConditionNew(dataset);

            string whereClause = "";
            whereClause = modelFmViewConditionNew.ParserLogic(tbxLogic.Text, hdfFmType.Value, int.Parse(hdfCompanyId.Value));

            return whereClause;
        }
        private void GrdConditionsAdd()
        {
            if (ValidateFooterAdd())
            {
                Page.Validate("Conditions");
                if (Page.IsValid)
                {
                    int conditionId = int.Parse(((DropDownList)grdConditions.FooterRow.FindControl("ddlName")).SelectedValue);
                    string name = ((DropDownList)grdConditions.FooterRow.FindControl("ddlName")).SelectedItem.Text;
                    string operator_ = ((DropDownList)grdConditions.FooterRow.FindControl("ddlOperator")).SelectedItem.Text;
                    string sign = ((DropDownList)grdConditions.FooterRow.FindControl("ddlOperator")).SelectedValue;
                    int conditionNumber = int.Parse(((DropDownList)grdConditions.FooterRow.FindControl("ddlConditionNumber")).SelectedValue);

                    FmTypeViewConditionGateway fmTypeViewConditionGateway = new FmTypeViewConditionGateway();
                    fmTypeViewConditionGateway.LoadByFmTypeConditionId(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                    string type = fmTypeViewConditionGateway.GetType(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                    string value = "";

                    if ((type == "String") || (type == "Date") || (type == "Int") || (type == "Decimal"))
                    {
                        value = ((TextBox)grdConditions.FooterRow.FindControl("tbxValue")).Text;
                    }

                    if ((type == "FixedItems") || (type == "DynamicItems"))
                    {
                        value = ((DropDownList)grdConditions.FooterRow.FindControl("ddlValue")).SelectedItem.Text.Replace("-", "");
                        value = value.Trim();
                    }

                    if (type == "Boolean")
                    {
                        if (((RadioButton)grdConditions.FooterRow.FindControl("rbtnYes")).Checked)
                            value = "Yes";
                        if (((RadioButton)grdConditions.FooterRow.FindControl("rbtnNo")).Checked)
                            value = "No";
                    }

                    FmViewTDS dataSet = new FmViewTDS();
                    dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
                    FmViewConditionNew model = new FmViewConditionNew(dataSet);

                    model.Insert(conditionId, name, operator_, sign, conditionNumber, value, false, false);

                    Session.Remove("fmViewConditionNewDummy");
                    fmViewConditionNew = dataSet.FmViewConditionNew;
                    Session["fmViewConditionNew"] = dataSet.FmViewConditionNew;

                    grdConditions.DataBind();
                    grdConditions.PageIndex = grdConditions.PageCount - 1;
                }
            }
        }
        protected void grdConditions_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("ConditionsUpdate");
            if (Page.IsValid)
            {
                int id = (int)e.Keys["ID"];

                int conditionId = int.Parse(((DropDownList)grdConditions.Rows[e.RowIndex].Cells[3].FindControl("ddlName")).SelectedValue);
                string name = ((DropDownList)grdConditions.Rows[e.RowIndex].Cells[3].FindControl("ddlName")).SelectedItem.Text;
                DropDownList ddlOperator = ((DropDownList)grdConditions.Rows[grdConditions.EditIndex].FindControl("ddlOperator"));
                string operator_ = ddlOperator.SelectedItem.Text;
                string sign = ddlOperator.SelectedValue;
                int conditionNumber = int.Parse(((DropDownList)grdConditions.Rows[e.RowIndex].Cells[2].FindControl("ddlConditionNumber")).SelectedValue);

                FmTypeViewConditionGateway fmTypeViewConditionGateway = new FmTypeViewConditionGateway();
                fmTypeViewConditionGateway.LoadByFmTypeConditionId(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                string type = fmTypeViewConditionGateway.GetType(hdfFmType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                string value = "";

                if ((type == "String") || (type == "Date") || (type == "Int") || (type == "Decimal") )
                {
                    value = ((TextBox)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("tbxValue")).Text;
                }

                if ((type == "FixedItems") || (type == "DynamicItems"))
                {
                    value = ((DropDownList)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("ddlValue")).SelectedItem.Text.Replace("-", "");
                    value = value.Trim();
                }

                if (type == "Boolean")
                {
                    if (((RadioButton)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("rbtnYes")).Checked)
                        value = "Yes";
                    if (((RadioButton)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("rbtnNo")).Checked)
                        value = "No";
                }

                FmViewTDS dataSet = new FmViewTDS();
                dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
                FmViewConditionNew model = new FmViewConditionNew(dataSet);

                model.Update(id, conditionId, name, operator_, sign, conditionNumber, value, false, false);
                Session["fmViewConditionNew"] = dataSet.FmViewConditionNew;
                fmViewConditionNew = dataSet.FmViewConditionNew;
            }
            else
            {
                e.Cancel = true;
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP3 - CONDITIONS
        //    
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        // STEP3 - CONDITIONS - EVENTS
        //
        protected void grdConditions_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Conditions Gridview, if the gridview is edition mode
            if (grdConditions.EditIndex >= 0)
            {
                grdConditions.UpdateRow(grdConditions.EditIndex, true);
            }

            // Delete conditions
            int id = (int)e.Keys["ID"];

            FmViewTDS dataSet = new FmViewTDS();
            dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
            FmViewConditionNew model = new FmViewConditionNew(dataSet);

            model.Delete(id);
            Session["fmViewConditionNew"] = dataSet.FmViewConditionNew;
        }
        private bool StepConditionsNext()
        {
            // Validate data
            bool validData = true;

            // Edit if there is data to save
            Page.Validate("ConditionsUpdate");
            if (!Page.IsValid) validData = false;

            if (validData)
            {
                // Conditions Gridview, if the gridview is edition mode
                if (grdConditions.EditIndex >= 0)
                {
                    grdConditions.UpdateRow(grdConditions.EditIndex, true);
                    grdConditions.DataBind();
                }
            }

            Page.Validate("ConditionsNext");
            if (!Page.IsValid) validData = false;

            if (validData)
            {
                // Add conditions if exists at footer
                if (ValidateFooterNext())
                {
                    GrdConditionsAdd();
                }
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int viewId = Int32.Parse(hdfViewId.Value);

                FmViewGateway fmViewGateway = new FmViewGateway();
                fmViewGateway.LoadByViewId(viewId, companyId );

                fmViewConditionTemp.Rows.Clear();

                // Create dataset
                FmViewTDS dataSet = new FmViewTDS();
                dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
                dataSet.FmViewConditionTemp.Merge(fmViewConditionTemp, true);

                // Process conditions
                FmViewConditionTemp model = new FmViewConditionTemp(dataSet);
                FmViewConditionNew modelLogic = new FmViewConditionNew(dataSet);

                string originalLogic = fmViewGateway.GetLogic(viewId);
                string newLogic = modelLogic.GetConditionsByDefault();

                ArrayList aConditions = new ArrayList();
                aConditions = modelLogic.GetConditions();

                bool isConditionsUpdate = false;

                if (originalLogic.Trim() != newLogic.Trim())
                {
                    foreach (string conditionNumber in aConditions)
                    {
                        if (!originalLogic.Contains(conditionNumber))
                        {
                            isConditionsUpdate = true;
                        }
                    }

                    if (!isConditionsUpdate)
                    {
                        if (Session["conditionsUpdate"] != null)
                        {
                            if (Session["conditionsUpdate"].ToString() == "true")
                            {
                                isConditionsUpdate = true;
                            }
                        }
                    }
                }
                else
                {
                    tbxLogic.Text = originalLogic.Trim();
                }

                if (isConditionsUpdate)
                {
                    tbxLogic.Text = newLogic.Trim();
                }
                else
                {
                    tbxLogic.Text = originalLogic.Trim();
                }

                // Get changes
                fmViewConditionNew.Rows.Clear();
                fmViewConditionNew.Merge(modelLogic.Table);
                fmViewConditionTemp.Rows.Clear();
                fmViewConditionTemp.Merge(model.Table);

                // Store tables
                Session.Remove("fmViewConditionNewDummy");
                Session.Remove("fmViewConditionTempDummy");
                Session["fmViewConditionNew"] = fmViewConditionNew;
                Session["fmViewConditionTemp"] = fmViewConditionTemp;

                Session["conditionsUpdate"] = null;

                return true;
            }
            else
            {
                return false;
            }
        }
        private bool StepColumnsToDisplayNext()
        {
            ViewState["columnsToDisplayPageIndex"] = grdColumnsToDisplay.PageIndex;

            Page.Validate("ColumsToDisplay");

            if (Page.IsValid)
            {
                hdfStep.Value = "StepColumnsToDisplay";
                StepColumnsToDisplayProcessGrid();
                fmViewDisplayTemp.Rows.Clear();

                // Create dataset
                FmViewTDS dataSet = new FmViewTDS();
                dataSet.LFS_FM_TYPE_VIEW_DISPLAY.Merge(fmTypeViewDisplay, true);
                dataSet.FmViewDisplayTemp.Merge(fmViewDisplayTemp, true);

                // Process
                int companyId = Int32.Parse(hdfCompanyId.Value);
                string fmType = hdfFmType.Value;
                int viewId = Int32.Parse(hdfViewId.Value);

                FmViewDisplayTemp model = new FmViewDisplayTemp(dataSet);
                model.ProcessForEdit(viewId, fmType, companyId);

                // get changes
                fmViewDisplayTemp.Rows.Clear();
                fmViewDisplayTemp.Merge(model.Table);

                // Store tables
                Session.Remove("fmTypeViewDisplayDummy");
                Session["fmTypeViewDisplay"] = fmTypeViewDisplay;
                Session["fmViewDisplayTemp"] = fmViewDisplayTemp;

                // Load conditions for next step
                dataSet.FmViewConditionNew.Merge(fmViewConditionNew, true);
                dataSet.FmViewConditionTemp.Merge(fmViewConditionTemp, true);

                FmViewConditionNew modelConditionNew = new FmViewConditionNew(dataSet);
                modelConditionNew.LoadByViewIdCompanyIdFmType(viewId, companyId, fmType);

                // Store tables
                fmViewConditionNew = (FmViewTDS.FmViewConditionNewDataTable)modelConditionNew.Table;
                Session["fmViewConditionNew"] = fmViewConditionNew;

                return true;
            }
            else
            {
                return false;
            }
        }
        private string GetWhere()
        {
            FmViewTDS dataset = new FmViewTDS();
            dataset.FmViewConditionNew.Merge(fmViewConditionNew, true);

            FmViewConditionNew modelFmViewConditionNew = new FmViewConditionNew(dataset);
            int companyId = Int32.Parse(hdfCompanyId.Value);
            string fmType = hdfFmType.Value;

            string whereClause = "";
            whereClause = modelFmViewConditionNew.ParserLogic(tbxLogic.Text, fmType, companyId);

            return whereClause;
        }