///// <summary>
        ///// Copy QPD segment
        ///// </summary>
        //internal static void CopyQPD(NHapi.Model.V25.Segment.QPD dest, NHapi.Model.V25.Segment.QPD source,  QueryData queryParms)
        //{
        //    dest.MessageQueryName.Identifier.Value = source.MessageQueryName.Identifier.Value;
        //    dest.MessageQueryName.Text.Value = source.MessageQueryName.Text.Value;
        //    dest.QueryTag.Value = source.QueryTag.Value;
        //    var qps = source.GetField(3);

        //    // Create the actual qparms
        //    Dictionary<String, Object> actualQParms = new Dictionary<string, object>();
        //    var qPerson = queryParms.QueryRequest.FindComponent(SVC.Core.ComponentModel.HealthServiceRecordSiteRoleType.SubjectOf);
        //    if (qPerson == null)
        //        return;


        //    //dest.UserParametersInsuccessivefields.Data = source.UserParametersInsuccessivefields.Data;
        //}

        /// <summary>
        /// XTN from telephone number
        /// </summary>
        public static void XTNFromTel(Everest.DataTypes.TEL tel, NHapi.Model.V231.Datatype.XTN instance)
        {
            NHapi.Model.V25.Datatype.XTN v25instance = new NHapi.Model.V25.Datatype.XTN(instance.Message);
            XTNFromTel(tel, v25instance);
            for (int i = 0; i < v25instance.Components.Length; i++)
            {
                if (v25instance.Components[i] is AbstractPrimitive && i < instance.Components.Length)
                {
                    (instance.Components[i] as AbstractPrimitive).Value = (v25instance.Components[i] as AbstractPrimitive).Value;
                }
            }
        }
        /// <summary>
        /// Transform a telephone number
        /// </summary>
        public static TelecommunicationsAddress TelFromXTN(NHapi.Model.V231.Datatype.XTN v2XTN)
        {
            Regex re = new Regex(@"([+0-9A-Za-z]{1,4})?\((\d{3})\)?(\d{3})\-(\d{4})X?(\d{1,6})?");

            MARC.Everest.DataTypes.TEL retVal = new Everest.DataTypes.TEL();

            if (v2XTN.Get9999999X99999CAnyText.Value == null)
            {
                StringBuilder sb = new StringBuilder("tel:");

                try
                {
                    if (v2XTN.CountryCode.Value != null)
                    {
                        sb.AppendFormat("{0}-", v2XTN.CountryCode);
                    }
                    if (v2XTN.PhoneNumber != null && v2XTN.PhoneNumber.Value != null && !v2XTN.PhoneNumber.Value.Contains("-"))
                    {
                        v2XTN.PhoneNumber.Value = v2XTN.PhoneNumber.Value.Insert(3, "-");
                    }
                    sb.AppendFormat("{0}-{1}", v2XTN.AreaCityCode, v2XTN.PhoneNumber);
                    if (v2XTN.Extension.Value != null)
                    {
                        sb.AppendFormat(";ext={0}", v2XTN.Extension);
                    }
                }
                catch { }

                if (sb.ToString().EndsWith("tel:") ||
                    sb.ToString() == "tel:-")
                {
                    retVal = "tel:" + v2XTN.AnyText.Value;
                }
                else
                {
                    retVal = sb.ToString();
                }
            }
            else
            {
                var           match = re.Match(v2XTN.Get9999999X99999CAnyText.Value);
                StringBuilder sb    = new StringBuilder("tel:");

                for (int i = 1; i < 5; i++)
                {
                    if (!String.IsNullOrEmpty(match.Groups[i].Value))
                    {
                        sb.AppendFormat("{0}{1}", match.Groups[i].Value, i == 4 ? "" : "-");
                    }
                }
                if (!string.IsNullOrEmpty(match.Groups[5].Value))
                {
                    sb.AppendFormat(";ext={0}", match.Groups[5].Value);
                }

                retVal = sb.ToString();
            }

            // Use code conversion
            MARC.Everest.DataTypes.Interfaces.TelecommunicationAddressUse?use = null;
            if (!String.IsNullOrEmpty(v2XTN.TelecommunicationUseCode.Value) && !s_v2TelUseConvert.TryGetValue(v2XTN.TelecommunicationUseCode.Value, out use))
            {
                throw new InvalidOperationException(string.Format("{0} is not a known use code", v2XTN.TelecommunicationUseCode.Value));
            }

            // Capability
            retVal.Capabilities = new Everest.DataTypes.SET <Everest.DataTypes.CS <Everest.DataTypes.Interfaces.TelecommunicationCabability> >();
            switch (v2XTN.TelecommunicationEquipmentType.Value)
            {
            case "BP":
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.Text);
                break;

            case "CP":
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.SMS);
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.Voice);
                break;

            case "FX":
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.Fax);
                break;

            case "PH":
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.Voice);
                break;

            case "X.400":
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.Text);
                retVal.Capabilities.Add(MARC.Everest.DataTypes.Interfaces.TelecommunicationCabability.Data);
                break;
            }

            return(new TelecommunicationsAddress()
            {
                Capability = Util.ToWireFormat(retVal.Capabilities),
                Use = Util.ToWireFormat(retVal.Use),
                Value = retVal.Value
            });
        }