private string CreateResponseXml(Epi.Web.Enter.Common.Message.PreFilledAnswerRequest request, List <SurveyInfoBO> SurveyBOList)
        {
            string ResponseXml;

            XDocument SurveyXml = new XDocument();

            foreach (var item in SurveyBOList)
            {
                SurveyXml = XDocument.Parse(item.XML);
            }
            SurveyResponseXML Implementation = new SurveyResponseXML(request, SurveyXml);

            ResponseXml = Implementation.CreateResponseDocument(SurveyXml).ToString();


            return(ResponseXml);
        }
        public ActionResult Index(string surveyid, string AddNewFormId, string EditForm)
        {
            int  UserId       = SurveyHelper.GetDecryptUserId(Session["UserId"].ToString());
            int  CuurentOrgId = int.Parse(Session["SelectedOrgId"].ToString());
            Guid ResponseID   = Guid.NewGuid();

            Session["FormValuesHasChanged"] = "";

            TempData[Epi.Web.MVC.Constants.Constant.RESPONSE_ID] = Session["RootResponseId"] = ResponseID.ToString();


            if (string.IsNullOrEmpty(EditForm) && Session["EditForm"] != null)
            {
                EditForm = Session["EditForm"].ToString();
            }

            if (!string.IsNullOrEmpty(EditForm) && string.IsNullOrEmpty(AddNewFormId))
            {
                Session["RootResponseId"] = EditForm.ToLower();

                Session["IsEditMode"] = true;
                Epi.Web.Enter.Common.DTO.SurveyAnswerDTO surveyAnswerDTO = GetSurveyAnswer(EditForm, Session["RootFormId"].ToString());


                Session["RequestedViewId"] = surveyAnswerDTO.ViewId;
                if (Session["RecoverLastRecordVersion"] != null)
                {
                    surveyAnswerDTO.RecoverLastRecordVersion = bool.Parse(Session["RecoverLastRecordVersion"].ToString());
                }
                string ChildRecordId = GetChildRecordId(surveyAnswerDTO);
                Session["RecoverLastRecordVersion"] = false;
                return(RedirectToAction(Epi.Web.MVC.Constants.Constant.INDEX, Epi.Web.MVC.Constants.Constant.SURVEY_CONTROLLER, new { responseid = ChildRecordId, PageNumber = 1, Edit = "Edit" }));
            }
            else
            {
                Session["IsEditMode"] = false;
            }
            bool IsMobileDevice = this.Request.Browser.IsMobileDevice;


            if (IsMobileDevice == false)
            {
                IsMobileDevice = Epi.Web.MVC.Utility.SurveyHelper.IsMobileDevice(this.Request.UserAgent.ToString());
            }

            FormsAuthentication.SetAuthCookie("BeginSurvey", false);
            Session["RootFormId"] = AddNewFormId;
            //create the responseid
            Epi.Web.Enter.Common.DTO.SurveyAnswerDTO SurveyAnswer = _isurveyFacade.CreateSurveyAnswer(AddNewFormId, ResponseID.ToString(), UserId, false, "", false, CuurentOrgId);
            MvcDynamicForms.Form form = _isurveyFacade.GetSurveyFormData(SurveyAnswer.SurveyId, 1, SurveyAnswer, IsMobileDevice, null, null, false, false);
            TempData["Width"] = form.Width + 100;
            SurveyInfoModel surveyInfoModel = Mapper.ToFormInfoModel(form.SurveyInfo);

            // set the survey answer to be production or test
            SurveyAnswer.IsDraftMode = form.SurveyInfo.IsDraftMode;
            XDocument xdoc = XDocument.Parse(form.SurveyInfo.XML);

            ///////////////////////////// Execute - Record Before - start//////////////////////
            Dictionary <string, string> ContextDetailList = new Dictionary <string, string>();
            EnterRule         FunctionObject_B            = (EnterRule)form.FormCheckCodeObj.GetCommand("level=record&event=before&identifier=");
            SurveyResponseXML SurveyResponseXML           = new SurveyResponseXML(PageFields, RequiredList);

            if (FunctionObject_B != null && !FunctionObject_B.IsNull())
            {
                try
                {
                    SurveyAnswer.XML        = SurveyResponseXML.CreateResponseDocument(xdoc, SurveyAnswer.XML);
                    Session["RequiredList"] = SurveyResponseXML._RequiredList;
                    //SurveyAnswer.XML = Epi.Web.MVC.Utility.SurveyHelper.CreateResponseDocument(xdoc, SurveyAnswer.XML, RequiredList);
                    this.RequiredList       = SurveyResponseXML._RequiredList;
                    form.RequiredFieldsList = this.RequiredList;
                    FunctionObject_B.Context.HiddenFieldList      = form.HiddenFieldsList;
                    FunctionObject_B.Context.HighlightedFieldList = form.HighlightedFieldsList;
                    FunctionObject_B.Context.DisabledFieldList    = form.DisabledFieldsList;
                    FunctionObject_B.Context.RequiredFieldList    = form.RequiredFieldsList;

                    FunctionObject_B.Execute();

                    // field list
                    form.HiddenFieldsList      = FunctionObject_B.Context.HiddenFieldList;
                    form.HighlightedFieldsList = FunctionObject_B.Context.HighlightedFieldList;
                    form.DisabledFieldsList    = FunctionObject_B.Context.DisabledFieldList;
                    form.RequiredFieldsList    = FunctionObject_B.Context.RequiredFieldList;


                    ContextDetailList = Epi.Web.MVC.Utility.SurveyHelper.GetContextDetailList(FunctionObject_B);
                    form = Epi.Web.MVC.Utility.SurveyHelper.UpdateControlsValuesFromContext(form, ContextDetailList);

                    _isurveyFacade.UpdateSurveyResponse(surveyInfoModel, ResponseID.ToString(), form, SurveyAnswer, false, false, 0, SurveyHelper.GetDecryptUserId(Session["UserId"].ToString()));
                }
                catch (Exception ex)
                {
                    // do nothing so that processing
                    // can continue
                }
            }
            else
            {
                SurveyAnswer.XML        = SurveyResponseXML.CreateResponseDocument(xdoc, SurveyAnswer.XML);//, RequiredList);
                this.RequiredList       = SurveyResponseXML._RequiredList;
                Session["RequiredList"] = SurveyResponseXML._RequiredList;
                form.RequiredFieldsList = RequiredList;
                _isurveyFacade.UpdateSurveyResponse(surveyInfoModel, SurveyAnswer.ResponseId, form, SurveyAnswer, false, false, 0, SurveyHelper.GetDecryptUserId(Session["UserId"].ToString()));
            }


            return(RedirectToAction(Epi.Web.MVC.Constants.Constant.INDEX, Epi.Web.MVC.Constants.Constant.SURVEY_CONTROLLER, new { responseid = ResponseID, PageNumber = 1, surveyid = surveyInfoModel.SurveyId }));
        }