//Generate the Terms collection (of Term) based on an existing Template
		public static List<Term> Create(XmlDocument xmlTemplateDoc, Template template)
		{
			XmlNodeList nodeComplexLists = xmlTemplateDoc.SelectNodes(Utility.XMLHelper.GetXPath(true, XMLNames._E_TemplateDef, XMLNames._E_ComplexLists, XMLNames._E_ComplexList));
			if (nodeComplexLists == null)
			{
				return new List<Term>();
			}

			List<Term> rtn = new List<Term>(nodeComplexLists.Count);
			foreach (XmlNode nodeComplexList in nodeComplexLists)
			{
                ComplexList complexList = new ComplexList(nodeComplexList, template, false);
				rtn.Add(complexList);
			}

			return rtn;
		}
        public ComplexListItem(XmlNode node, ComplexList complexList)
		{
			_selectable = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_Selectable);
			_selected = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_Selected);
			_editable = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_Editable);
			_deletable = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_Deletable);
			_default = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_Default);

            _id = Guid.Empty;
            string id = Utility.XMLHelper.GetAttributeString(node, XMLNames._A_ID);
            if (!string.IsNullOrEmpty(id))
            {
                try
                {
                    _id = new Guid(id);
                }
                catch
                {
                }
            }
            //If an ID was not assigned before, assign it now....
            if (_id == Guid.Empty)
                _id = Guid.NewGuid();

			XmlNodeList nodeItemValues = node.SelectNodes(XMLNames._E_ItemValue);

			if (nodeItemValues != null)
			{
				_itemValues = new List<ComplexListItemValue>(nodeItemValues.Count);
				foreach (XmlNode nodeItemValue in nodeItemValues)
				{
					ComplexListItemValue itemValue = new ComplexListItemValue(nodeItemValue, complexList);
					_itemValues.Add(itemValue);
				}
			}
		}
        public ComplexListItemValue(XmlNode node, ComplexList complexList)
		{
            _complexList = complexList;

            try
            {
                string fieldID = Utility.XMLHelper.GetAttributeString(node, XMLNames._A_FieldID);
                _fieldID = new Guid(fieldID);
                _fieldIDDefined = true;
            }
            catch
            {
                _fieldIDDefined = false;
                try
                {
                    _fieldName = Utility.XMLHelper.GetAttributeString(node, XMLNames._A_FieldName);
                }
                catch
                {
                }
            }

            _fieldFilterTerm = complexList.FindField(_fieldID).FilterTerm;
            _bigText = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_BigText) ?? false;
            _fieldValue = Utility.XMLHelper.GetText(node);
            _removeBlank = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_RemoveBlank) ?? false;
		}
        //This call is used to display the ComplexListItemValue in the Summary page.
        public ComplexListItemValue(Guid fieldID, string fieldValue, ComplexList complexList, Term fieldFilterTerm, bool guiOverride)
		{
            _fieldID = fieldID;
            _fieldIDDefined = true;
            _complexList = complexList;
            _fieldFilterTerm = fieldFilterTerm;
            _guiOverride = guiOverride;
            if (_guiOverride)
                _guiDisplay = fieldValue;
            else
                _fieldValue = fieldValue;
        }
        public ComplexListItemValue(ComplexList complexList, Term fieldFilterTerm)
		{
            _complexList = complexList;
            _fieldFilterTerm = fieldFilterTerm;
        }
 //For each term in the text, replace the FieldID (if found) with the FieldName
 public static string EmbedFieldNames(ComplexList complexList, string sText)
 {
     //match anything of the form <img...src="TextImage.ashx?text="...".../> in _text
     if (!string.IsNullOrEmpty(sText))
     {
         MatchCollection matches = Regex.Matches(sText, XMLNames._M_FieldImageTemplate);
         foreach (Match match in matches)
         {
             string sFieldName = match.Groups[1].Value;
             ComplexListField field = null;
             if (IsEmbeddedID(sFieldName))
             {
                 field = complexList.FindField(new Guid(FilterEmbeddedID(sFieldName)));
                 if (field != null)
                 {
                     //Replace <img ... > with field value
                     string matchedText = match.Value;
                     string replacementText = "";
                     replacementText = matchedText.Replace(sFieldName, field.Name);
                     sText = sText.Replace(matchedText, replacementText);
                 }
             }
         }
     }
     return sText;
 }
 //For each term in the text, replace the FieldName (if found) with the FieldID
 public static string EmbedFieldIDs(ComplexList complexList, string sText)
 {
     //match anything of the form <img...src="TextImage.ashx?text="...".../> in _text
     if (!string.IsNullOrEmpty(sText))
     {
         MatchCollection matches = Regex.Matches(sText, XMLNames._M_FieldImageTemplate);
         foreach (Match match in matches)
         {
             string fieldName = match.Groups[1].Value;
             ComplexListField field = null;
             string sFieldName = fieldName.Replace("&quot;", "\"");
             if (!IsEmbeddedID(sFieldName))
             {
                 field = complexList.FindField(sFieldName);
                 if (field != null)
                 {
                     //Replace <img ... > with term value
                     string matchedText = match.Value;
                     string replacementText = "";
                     replacementText = matchedText.Replace(fieldName, field.EmbeddedID);
                     sText = sText.Replace(matchedText, replacementText);
                 }
                 else
                     //Need to check for 'reserved term names'
                     if (!sFieldName.StartsWith("*"))
                         throw new Exception(string.Format("Unable to locate term named '{0}' when substituting terms", sFieldName));
             }
         }
     }
     return sText;
 }
        public ComplexListField(XmlNode node, ComplexList complexList)
		{
			_name = Utility.XMLHelper.GetAttributeString(node, XMLNames._A_Name);
            _complexList = complexList;

            _id = Guid.Empty;
            string id = Utility.XMLHelper.GetAttributeString(node, XMLNames._A_ID);
            if (!string.IsNullOrEmpty(id))
            {
                try
                {
                    _id = new Guid(id);
                }
                catch
                {
                }
            }
            //If an ID was not assigned before, assign it now....
            if (_id == Guid.Empty)
                _id = Guid.NewGuid();

			_bigText = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_BigText);
			_summary = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_Summary);
			_removeBlank = Utility.XMLHelper.GetAttributeBool(node, XMLNames._A_RemoveBlank);

            //Try to retrieve the Term info, if there.  If not, assign a Text Term by default.
            _filterTerm = null;
            if (node.HasChildNodes)
            {
                XmlNode termNode = node.FirstChild;
                TermType termType = (TermType)Enum.Parse(typeof(TermType), termNode.Name.ToString());
                CheckTermType(termType);
                switch (termType)
                {
                    case TermType.Text:
                        _filterTerm = new TextTerm(termNode, null, true);
                        break;

                    case TermType.Date:
                        _filterTerm = new DateTerm(termNode, null, true);
                        break;

                    case TermType.PickList:
                        _filterTerm = new PickListTerm(termNode, null, true);
                        break;
                }
            }
            else
            {
                _filterTerm = new TextTerm(false, null, true);
                TextTerm textTerm = _filterTerm as TextTerm;
                textTerm.Format = TextTermFormat.Plain;
            }
		}
        public ComplexListField(ComplexList complexList)
		{
            _id = Guid.NewGuid();
            _complexList = complexList;
		}
 public static bool ModifyItems(ComplexList sourceTerm, ComplexList destTerm)
 {
     return destTerm.Items.Count == 0 && sourceTerm.Items.Count > 0;
 }
        public override Term RetroCopy(bool systemTerm, Template template)
        {
            ComplexList complexList = new ComplexList(systemTerm, template, true, false);
            CopyBase(complexList, template);

            complexList._columnCount = _columnCount;
            complexList._rendering = _rendering;
            complexList._showOnItemSummary = _showOnItemSummary;
            complexList._standardHeader = _standardHeader;
            complexList._alternateHeader = _alternateHeader;
            complexList._fields = new List<ComplexListField>(_fields);
            complexList._items = new List<ComplexListItem>();

            return complexList;
        }