private void ProcessHandler()
    {
        int i = 0;
        string sRulename = "New Rule";
        string sLogicalOperator = "and";
        string[] aParam = null;
        string sTemplateType = "";
        long iTemplateID = 0;
        int iOrder = 0;
        string sParam = "";
        string sValue = "";
        XmlDocument sXMLdoc;
        XmlElement xmlelem;
        XmlAttribute xmlatt;
        string stmpText = "";
        bool bWroteCondition = false;
        bool bWroteActionTrue = false;
        bool bNeedWriteActionTrue = false;
        bool bNeedWriteActionFalse = false;
        long icurTemplateID = -1;
        Ektron.Cms.RulesEngine.TemplateParam tpTemParam;

        if (!string.IsNullOrEmpty(Request.QueryString["rule_name"]))
            sRulename = Request.QueryString["rule_name"].ToString();
        if (sRulename.ToString().Length == 0)
        {
            throw (new Exception("You need a rule name."));
        }

        if (m_iRuleID > 0) //edit
        {
            m_rulesUI.ClearRuleParams(m_iRuleID); //clear any params for the rule
            m_rulesengineRule = new Ektron.Cms.RulesEngine.Rule(m_refContentApi.RequestInformationRef);
            m_rulesengineRule.load(m_iRuleID);
            m_rulesengineRule.SetName(sRulename);
        }
        else //add
        {
            if (m_iID == 0)
            {
                throw (new Exception("No associated Rule Set ID"));
            }
            m_rulesengineRule = new Ektron.Cms.RulesEngine.Rule(sRulename, "", true);
            m_rulesengineRule.Initialize(m_refContentApi.RequestInformationRef);
            m_rulesengineRule.AddAndAssociate(m_iID);
            m_iRuleID = m_rulesengineRule.RuleID;
        }

        sXMLdoc = new XmlDocument();
        for (i = 0; i <= (Request.QueryString.Count - 2); i++)
        {
            if (!string.IsNullOrEmpty(Request.QueryString[i]))
            {
                if ((string)(Request.QueryString.Keys[i].ToLower().Trim()) == "rule_name")
                {
                    xmlelem = sXMLdoc.CreateElement("rule");
                    xmlatt = sXMLdoc.CreateAttribute("name");
                    xmlatt.Value = sRulename;
                    xmlelem.Attributes.Append(xmlatt);
                    sXMLdoc.AppendChild(xmlelem);
                }
                else if ((string)(Request.QueryString.Keys[i].ToLower().Trim()) == "logical_operator")
                {
                    sLogicalOperator = Request.QueryString[i];
                    xmlelem = sXMLdoc.CreateElement("condition");
                    sXMLdoc.ChildNodes.Item(0).AppendChild(xmlelem);
                    xmlelem = sXMLdoc.CreateElement("predicate");
                    xmlatt = sXMLdoc.CreateAttribute("type");
                    xmlatt.Value = sLogicalOperator.ToLower();
                    xmlelem.Attributes.Append(xmlatt);
                    sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(0).AppendChild(xmlelem);

                    xmlelem = sXMLdoc.CreateElement("actions");
                    xmlatt = sXMLdoc.CreateAttribute("case");
                    xmlatt.Value = "true";
                    xmlelem.Attributes.Append(xmlatt);
                    sXMLdoc.ChildNodes.Item(0).AppendChild(xmlelem);

                    xmlelem = sXMLdoc.CreateElement("actions");
                    xmlatt = sXMLdoc.CreateAttribute("case");
                    xmlatt.Value = "false";
                    xmlelem.Attributes.Append(xmlatt);
                    sXMLdoc.ChildNodes.Item(0).AppendChild(xmlelem);
                }
                else
                {
                    aParam = Strings.Split(Request.QueryString.Keys[i], "_", -1, 0);
                    if (aParam.Length == 4)
                    {
                        sTemplateType = aParam[0].ToString();
                        iTemplateID = Convert.ToInt64(aParam[1].ToString());
                        iOrder = Convert.ToInt32(aParam[2].ToString());
                        sParam = aParam[3].ToString();
                        sValue = Strings.Replace(Request.QueryString[i], "&", "&amp;", 1, -1, 0);
                        if (ValidateValue(sTemplateType, iTemplateID, sParam, sValue))
                        {
                            switch (sTemplateType.ToLower())
                            {
                                case "condition":
                                    tpTemParam = new Ektron.Cms.RulesEngine.TemplateParam(iTemplateID, sParam, sValue, Ektron.Cms.Common.EkEnumeration.CustomAttributeValueTypes.String, Ektron.Cms.Common.EkEnumeration.RuleTemplateType.Condition);
                                    m_rulesUI.AddRuleParam(m_iRuleID, tpTemParam);
                                    if (icurTemplateID == -1 || (icurTemplateID == iTemplateID))
                                    {
                                        if (icurTemplateID == -1)
                                        {
                                            FindTemplateIndex(m_acontem, iTemplateID);
                                            stmpText = Strings.Replace(m_acontem[FindTemplateIndex(m_acontem, iTemplateID)].Predicate, "<predicate type=", "<predicate template=\"" + iTemplateID.ToString() + "\" type=", 1, -1, 0);
                                        }
                                        else
                                        {
                                            stmpText = stmpText.Replace("<predicate type=", "<predicate template=\"" + iTemplateID.ToString() + "\" type=");
                                        }
                                        stmpText = stmpText.Replace("[" + sParam.ToLower() + "]", sValue);
                                    }
                                    else if (!(icurTemplateID == iTemplateID))
                                    {
                                        //write out
                                        sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).InnerXml += stmpText;
                                        stmpText = ""; //reset
                                        //get values
                                        stmpText = Strings.Replace(m_acontem[FindTemplateIndex(m_acontem, iTemplateID)].Predicate, "<predicate type=", "<predicate template=\"" + iTemplateID.ToString() + "\" type=", 1, -1, 0);
                                        stmpText = stmpText.Replace("[" + sParam.ToLower() + "]", sValue);
                                    }
                                    icurTemplateID = iTemplateID;
                                    break;
                                case "actiontrue":
                                    tpTemParam = new Ektron.Cms.RulesEngine.TemplateParam(iTemplateID, sParam, sValue, Ektron.Cms.Common.EkEnumeration.CustomAttributeValueTypes.String, Ektron.Cms.Common.EkEnumeration.RuleTemplateType.ActionTrue);
                                    m_rulesUI.AddRuleParam(m_iRuleID, tpTemParam);
                                    bNeedWriteActionTrue = true;
                                    if (!(bWroteCondition)) //this is the first time we come to the actions
                                    {
                                        //Write out the conditions
                                        sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).InnerXml += stmpText;
                                        stmpText = ""; //reset
                                        icurTemplateID = -1;
                                        bWroteCondition = true;
                                    }
                                    if (icurTemplateID == -1 || (icurTemplateID == iTemplateID))
                                    {
                                        if (icurTemplateID == -1)
                                        {
                                            stmpText = Strings.Replace(m_aacttem[FindTemplateIndex(m_aacttem, iTemplateID)].Predicate, "<action type=", "<action template=\"" + iTemplateID.ToString() + "\" type=", 1, -1, 0);
                                        }
                                        else
                                        {
                                            stmpText = stmpText.Replace("<action type=", "<action template=\"" + iTemplateID.ToString() + "\" type=");
                                        }
                                        stmpText = stmpText.Replace("[" + sParam.ToLower() + "]", sValue);
                                    }
                                    else if (!(icurTemplateID == iTemplateID))
                                    {
                                        //write out
                                        sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(1).InnerXml += stmpText;
                                        //sXMLdoc.ChildNodes.Item(1).InnerXml += stmpText
                                        stmpText = ""; //reset
                                        //get values
                                        stmpText = Strings.Replace(m_aacttem[FindTemplateIndex(m_aacttem, iTemplateID)].Predicate, "<action type=", "<action template=\"" + iTemplateID.ToString() + "\" type=", 1, -1, 0);
                                        stmpText = stmpText.Replace("[" + sParam.ToLower() + "]", sValue);
                                    }
                                    icurTemplateID = iTemplateID;
                                    break;
                                case "actionfalse":
                                    tpTemParam = new Ektron.Cms.RulesEngine.TemplateParam(iTemplateID, sParam, sValue, Ektron.Cms.Common.EkEnumeration.CustomAttributeValueTypes.String, Ektron.Cms.Common.EkEnumeration.RuleTemplateType.ActionFalse);
                                    m_rulesUI.AddRuleParam(m_iRuleID, tpTemParam);
                                    bNeedWriteActionFalse = true;
                                    if (!(bWroteCondition))
                                    {
                                        sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).InnerXml += stmpText;
                                        stmpText = ""; //reset
                                        icurTemplateID = -1;
                                        bWroteCondition = true;
                                    }
                                    if ((!(bWroteActionTrue)) && bNeedWriteActionTrue)
                                    {
                                        sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(1).InnerXml += stmpText;
                                        stmpText = ""; //reset
                                        icurTemplateID = -1;
                                        bWroteActionTrue = true;
                                    }
                                    if (icurTemplateID == -1 || (icurTemplateID == iTemplateID))
                                    {
                                        if (icurTemplateID == -1)
                                        {
                                            stmpText = Strings.Replace(m_aacttem[FindTemplateIndex(m_aacttem, iTemplateID)].Predicate, "<action type=", "<action template=\"" + iTemplateID.ToString() + "\" type=", 1, -1, 0);
                                        }
                                        else
                                        {
                                            stmpText = stmpText.Replace("<action type=", "<action template=\"" + iTemplateID.ToString() + "\" type=");
                                        }
                                        stmpText = stmpText.Replace("[" + sParam.ToLower() + "]", sValue);
                                    }
                                    else if (!(icurTemplateID == iTemplateID))
                                    {
                                        //write out
                                        sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(2).InnerXml += stmpText;
                                        //sXMLdoc.ChildNodes.Item(1).InnerXml += stmpText
                                        stmpText = ""; //reset
                                        //get values
                                        stmpText = Strings.Replace(m_aacttem[FindTemplateIndex(m_aacttem, iTemplateID)].Predicate, "<action type=", "<action template=\"" + iTemplateID.ToString() + "\" type=", 1, -1, 0);
                                        stmpText = stmpText.Replace("[" + sParam.ToLower() + "]", sValue);
                                    }
                                    icurTemplateID = iTemplateID;
                                    break;
                            }
                        }
                    }
                }
            }
        }
        if (!(bWroteCondition))
        {
            //Write out the conditions
            sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(0).ChildNodes.Item(0).InnerXml += stmpText;
        }
        if ((!(bWroteActionTrue)) && bNeedWriteActionTrue)
        {
            //Write out the actiontrue
            sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(1).InnerXml += stmpText;
        }
        if (bNeedWriteActionFalse)
        {
            sXMLdoc.ChildNodes.Item(0).ChildNodes.Item(2).InnerXml += stmpText;
        }

        m_rulesengineRule.UpdateXML(sXMLdoc.InnerXml.ToString());

        Response.Redirect((string)("ruleset.aspx?action=View&id=" + m_iID.ToString()), false);
    }
    protected void Page_Load(object sender, System.EventArgs e)
    {
        m_refMsg = m_refContentApi.EkMsgRef;
        AppImgPath = m_refContentApi.AppImgPath;
        ltr_Title.Text = m_refMsg.GetMessage("lbl cms business rulesets");

        RegisterResources();
        try
        {
            Utilities.ValidateUserLogin();
            if (m_refContentApi.RequestInformationRef.IsMembershipUser == 1 || m_refContentApi.RequestInformationRef.UserId == 0)
            {
                Response.Redirect((string)("../reterror.aspx?info=" + m_refMsg.GetMessage("msg login cms user")), false);
                return;
            }

            if (!string.IsNullOrEmpty(Request.QueryString["action"]))
            {
                m_sPageAction = (string)(Request.QueryString["action"].ToLower());
            }
            else
            {
                m_sPageAction = "";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                m_iRuleID = Convert.ToInt64(Request.QueryString["id"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["rulesetid"]))
            {
                m_iID = Convert.ToInt64(Request.QueryString["rulesetid"].ToString());
            }

            m_rulesUI = new Ektron.Cms.RulesEngine.UI(m_refContentApi.RequestInformationRef);
            m_acontem = m_rulesUI.GetAllConditionTemplates();
            m_aacttem = m_rulesUI.GetAllActionTemplates();
            m_tpacondition = m_rulesUI.GetAllConditionTemplateParams();
            m_tpaaction = m_rulesUI.GetAllActionTemplateParams();

            OutputJS();
            ShowHidden();

            if (m_sPageAction == "edit")
            {
                m_aruleparam = m_rulesUI.GetRuleParams(m_iRuleID);
                m_rulesengineRule = new Ektron.Cms.RulesEngine.Rule(m_refContentApi.RequestInformationRef);
                m_rulesengineRule.load(m_iRuleID);
                ruleNameText.Value = m_rulesengineRule.RuleName;
                cmswizard ucWizard;
                ucWizard = (cmswizard)(LoadControl("../controls/wizard/wizard.ascx"));
                ucWizard.AllowSelect = true;
                ucWizard.ID = "ProgressSteps";
                pnlwizard.Controls.Add(ucWizard);
                BuildTemplateJS("edit");
                ShowRuleToolBar();
            }
            else if (m_sPageAction == "view")
            {
                m_aruleparam = m_rulesUI.GetRuleParams(m_iRuleID);
                m_rulesengineRule = new Ektron.Cms.RulesEngine.Rule(m_refContentApi.RequestInformationRef);
                m_rulesengineRule.load(m_iRuleID);
                ruleNameText.Value = m_rulesengineRule.RuleName;
                BuildTemplateJS("view");
                ShowRuleToolBar();
            }
            else if (m_sPageAction == "add")
            {
                BuildTemplateJS("add");
                cmswizard ucWizard;
                ucWizard = (cmswizard)(LoadControl("../controls/wizard/wizard.ascx"));
                ucWizard.AllowSelect = false; // do not allow skip for add
                ucWizard.ID = "ProgressSteps";
                pnlwizard.Controls.Add(ucWizard);
                ShowRuleToolBar();
            }
            else if (m_sPageAction == "process")
            {
                BuildTemplateJS("add");
                ProcessHandler();
            }
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message & ex.StackTrace)
            Utilities.ShowError(ex.Message);
        }
    }