/// <summary>
 /// Creates a new answer item instance from the db answer data
 /// </summary>
 /// <param name="answer">The data source to create the instance from</param>
 /// <param name="question">The question container</param>
 /// <param name="defaultSelectionMode">The default selection mode for the 
 /// item (Radio, checkbox etc...)</param>
 /// <param name="parentControlUniqueID">
 /// Unique ID required to identify global selection groups 
 /// like radiobutton groups
 /// </param>
 /// <param name="showAnswerText">
 /// Assigns the text to the answeritem control ?
 /// </param>
 /// <param name="enableDefaults">
 /// Does the answer set the user default values of fields to the answer web controls
 /// </param>
 public static AnswerItem Create(AnswerData.AnswersRow answer, QuestionItem question, Section section, AnswerSelectionMode defaultSelectionMode, Style answerStyle, ControlRenderMode renderMode, string languageCode, string parentControlUniqueID, bool showAnswerText, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableDefaults)
 {
     AnswerItem item = null;
     try
     {
         if (answer.TypeAssembly != null)
         {
             item = (AnswerItem) Assembly.Load(answer.TypeAssembly).CreateInstance(answer.TypeNameSpace);
             item.AnswerId = answer.AnswerId;
             item.ImageUrl = answer.IsImageURLNull() ? null : answer.ImageURL;
             item.ID = (section == null) ? (GlobalConfig.AnswerItemName + answer.AnswerId) : string.Concat(new object[] { GlobalConfig.AnswerItemName, answer.AnswerId, GlobalConfig.AnswerSectionName, section.SectionUid });
             item.AnswerStyle = answerStyle;
             item.Question = question;
             item.QuestionId = answer.QuestionId;
             item.RenderMode = renderMode;
             item.Text = new PipeManager().PipeValuesInText(answer.QuestionId, answer.AnswerText, voterAnswersState, languageCode);
             item.ShowAnswerText = showAnswerText;
             item.SectionContainer = section;
             item.LanguageCode = languageCode;
             
            
         }
     }
     catch (FileNotFoundException)
     {
         item = new AnswerTextItem();
         item.AnswerId = -1;
         item.Text = string.Format(ResourceManager.GetString("AnswerTypeAssemblyNotFoundMessage"), answer.TypeAssembly);
         return item;
     }
     catch (NullReferenceException)
     {
         item = new AnswerTextItem();
         item.AnswerId = -1;
         item.Text = string.Format(ResourceManager.GetString("AnswerTypeNotFoundMessage"), answer.TypeNameSpace, answer.TypeAssembly);
         return item;
     }
     catch (InvalidCastException)
     {
         item = new AnswerTextItem();
         item.AnswerId = -1;
         item.Text = string.Format(ResourceManager.GetString("AnswerTypeInvalidMessage"), answer.TypeNameSpace, answer.TypeAssembly);
         return item;
     }
     if (item is IClientScriptValidator)
     {
         IClientScriptValidator validator = (IClientScriptValidator) item;
         validator.JavascriptCode = answer.JavascriptCode;
         validator.JavascriptFunctionName = answer.JavascriptFunctionName;
         if (answer.JavascriptErrorMessage != null)
         {
             validator.JavascriptErrorMessage = (ResourceManager.GetString(answer.JavascriptErrorMessage, languageCode) == null) ? answer.JavascriptErrorMessage : ResourceManager.GetString(answer.JavascriptErrorMessage, languageCode);
         }
         else
         {
             validator.JavascriptErrorMessage = null;
         }
         validator.EnableValidation = true;
     }
     if (item is IMandatoryAnswer)
     {
         ((IMandatoryAnswer) item).Mandatory = answer.Mandatory;
     }
     if ((item is IRegExValidator) && (answer.RegExpression != null))
     {
         ((IRegExValidator) item).RegExpression = answer.RegExpression;
         ((IRegExValidator) item).RegExpressionErrorMessage = answer.RegExMessage;
     }
     AnswerSelectionItem item2 = item as AnswerSelectionItem;
     if (item2 != null)
     {
         item2.UniqueGroupId = parentControlUniqueID;
         item2.AnswerId = answer.AnswerId;
         item2.TypeMode = (AnswerTypeMode) answer.TypeMode;
         item2.SelectionMode = defaultSelectionMode;
         bool flag = (section == null) ? IsUserSelected(answer.AnswerId, 0, voterAnswersState) : IsUserSelected(answer.AnswerId, section.SectionNumber, voterAnswersState);
         if (flag)
         {
             item2.Selected = flag;
         }
         else
         {
             item2.Selected = enableDefaults ? answer.Selected : false;
         }
     }
     if (item is IFieldItem)
     {
         IFieldItem item3 = (IFieldItem) item;
         item3.FieldHeight = answer.FieldHeight;
         item3.FieldWidth = answer.FieldWidth;
         item3.FieldLength = answer.FieldLength;
     }
     else if (item is AnswerDataSourceItem)
     {
         AnswerDataSourceItem item4 = (AnswerDataSourceItem) item;
         item4.QuestionId = answer.QuestionId;
         item4.AnswerId = answer.AnswerId;
         if (item4 is AnswerXmlItem)
         {
             item4.DataSource = answer.XmlDatasource;
         }
         else if (answer.DataSource != null)
         {
             item4.DataSource = ParseDefaultAnswerText(answer.QuestionId, answer.DataSource, voterAnswersState, languageCode);
         }
         item4.ImageUrl = answer.IsImageURLNull() ? null : answer.ImageURL;
     }
     string str = (section == null) ? GetUserText(answer.AnswerId, 0, voterAnswersState) : GetUserText(answer.AnswerId, section.SectionNumber, voterAnswersState);
     if (str != null)
     {
         item.DefaultText = str;
     }
     else if ((enableDefaults && !answer.IsDefaultTextNull()) && (answer.DefaultText.Length > 0))
     {
         item.DefaultText = ParseDefaultAnswerText(answer.QuestionId, answer.DefaultText, voterAnswersState, languageCode);
     }
     if (item is ExtendedAnswerItem)
     {
         ((ExtendedAnswerItem) item).RestoreProperties();
     }
     return item;
 }
        /// <summary>
        /// Converts a stronlgy typed MatrixChildQuestionData dataset
        /// to a webcontrol collection
        /// </summary>
        /// <param name="childQuestions">The questions / answers data</param>
        /// <param name="parentControlUniqueID">
        /// Unique ID required to identify global groups 
        /// like radiobutton groups
        /// </param>
        /// <returns>a web control collection of MatrixChildQuestion</returns>
        public static MatrixChildCollection CreateQuestionChildCollection(MatrixChildQuestionData childQuestions, Section sectionContainer, string languageCode, string parentControlUniqueID, AnswerSelectionMode selectionMode, Style answerStyle, ControlRenderMode renderMode, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableAnswersDefault)
        {
            MatrixChildCollection childs = new MatrixChildCollection();
            
            foreach (MatrixChildQuestionData.ChildQuestionsRow row in childQuestions.ChildQuestions.Rows)
            {
                MatrixChildQuestion question = new MatrixChildQuestion();
                question.QuestionId = row.QuestionId;
                question.Text = new PipeManager().PipeValuesInText(row.QuestionId, row.QuestionText, voterAnswersState, languageCode);
                AnswerData answers = new AnswerData();

//                answers.Merge(row.GetAnswersRows());

				MatrixChildQuestionData.AnswersRow[] ar = row.GetAnswersRows();
					foreach (MatrixChildQuestionData.AnswersRow r in ar) {
								r.Table.Namespace = answers.Namespace; }
					answers.Merge(ar);
				
                question.Answers = AnswerItemFactory.CreateAnswerItemCollection(answers, question, sectionContainer, selectionMode, answerStyle, renderMode, languageCode, parentControlUniqueID, false, voterAnswersState, enableAnswersDefault);
                childs.Add(question);
            }
            return childs;
        }
 /// <summary>
 /// Parse an AnswerDataCollection, converts the data and
 /// returns an AnswerItemCollection filled with the correct 
 /// child controls
 /// </summary>
 /// <param name="answers">A collection of answerdata entities</param>
 /// <param name="defaultSelectionMode">The default selection mode for the 
 /// item (Radio, checkbox etc...)</param>
 /// <param name="parentcontrolUniqueID">
 /// Unique ID required to identify global groups 
 /// like radiobutton groups
 /// </param>
 /// <param name="showAnswerText">
 /// Assigns the text to the answeritem control ?
 /// </param>
 /// <param name="voterAnswersState">
 /// Current state of answers, used for piping. If not available
 /// leave it to null 
 /// </param>
 /// <param name="enableDefaults">
 /// Does the answer set the user default values of fields to the answer web controls
 /// </param>
 /// <returns>A collection of answeritem web controls</returns>
 public static AnswerItemCollection CreateAnswerItemCollection(AnswerData answers, QuestionItem question, Section section, AnswerSelectionMode defaultSelectionMode, Style answerStyle, ControlRenderMode renderMode, string languageCode, string parentControlUniqueID, bool showAnswerText, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableDefaults)
 {
     AnswerItemCollection items = new AnswerItemCollection();
     foreach (AnswerData.AnswersRow row in answers.Answers.Rows)
     {
         items.Add(Create(row, question, section, defaultSelectionMode, answerStyle, renderMode, languageCode, parentControlUniqueID, showAnswerText, voterAnswersState, enableDefaults));
     }
     return items;
 }
 public void Remove(Section section)
 {
     base.Remove(section);
 }
 public void Insert(int index, Section section)
 {
     base.Insert(index, section);
 }
 public bool Contains(Section section)
 {
     return base.Contains(section);
 }
 public int Add(Section section)
 {
     return base.Add(section);
 }