/// <summary>
        /// Defines the compare criteria for two Contact List instances
        /// </summary>
        /// <param name="x">Contact List to be compared</param>
        /// <param name="y">Contact List to be compared</param>
        /// <returns></returns>
        public static int CompareContactListsBySortOrder(ContactList x, ContactList y)
        {
            if (x.SortOrder.HasValue && y.SortOrder.HasValue)
            {
                return x.SortOrder.Value.CompareTo(y.SortOrder.Value);
            }

            return 0;
        }
        /// <summary>
        /// Returns a contact lists from the xml data
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private static List<ContactList> GetContactListFromCampaignResponse(XPathNavigator node)
        {
            List<ContactList> result = new List<ContactList>();

            if (node.HasChildren)
            {
                node.MoveToFirstChild();
                {
                    do
                    {
                        switch (node.Name)
                        {
                            case EmailCampaignXmlNodeContactList:
                                ContactList contactList = new ContactList();
                                contactList.Id = GetContactListId(node);
                                result.Add(contactList);
                                break;
                        }
                    } while (node.MoveToNext());
                }
                node.MoveToParent();
            }
            return result;
        }
        /// <summary>
        /// Get Contact object with details from specified Xml data
        /// </summary>
        /// <param name="node">Xml data cursor model</param>
        /// <param name="resolver">Xml namespace resolver</param>
        /// <returns>Contact with details</returns>
        private static Contact GetContactDetail(XPathNavigator node, IXmlNamespaceResolver resolver)
        {
            Contact c = new Contact();
            c.Link = GetContactLink(node, resolver);

            const string xpathSelect = @"at:content/cc:Contact";

            XPathNodeIterator contactNodes = node.Select(xpathSelect, resolver);

            while (contactNodes.MoveNext())
            {
                XPathNavigator currentContactNode = contactNodes.Current;

                c.Id = GetContactId(currentContactNode);

                if (currentContactNode.HasChildren)
                {
                    currentContactNode.MoveToFirstChild();
                    do
                    {
                        switch (currentContactNode.Name)
                        {
                            case ContactXmlElementAddr1:
                                c.AddressLine1 = currentContactNode.Value;
                                break;
                            case ContactXmlElementAddr2:
                                c.AddressLine2 = currentContactNode.Value;
                                break;
                            case ContactXmlElementAddr3:
                                c.AddressLine3 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCity:
                                c.City = currentContactNode.Value;
                                break;
                            case ContactXmlElementCompanyName:
                                c.CompanyName = currentContactNode.Value;
                                break;
                            case ContactXmlElementConfirmed:
                                c.Confirmed = currentContactNode.ValueAsBoolean;
                                break;
                            case ContactXmlElementCountryCode:
                                c.CountryCode = currentContactNode.Value;
                                break;
                            case ContactXmlElementCountryName:
                                c.CountryName = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField1:
                                c.CustomField1 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField2:
                                c.CustomField2 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField3:
                                c.CustomField3 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField4:
                                c.CustomField4 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField5:
                                c.CustomField5 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField6:
                                c.CustomField6 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField7:
                                c.CustomField7 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField8:
                                c.CustomField8 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField9:
                                c.CustomField9 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField10:
                                c.CustomField10 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField11:
                                c.CustomField11 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField12:
                                c.CustomField12 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField13:
                                c.CustomField13 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField14:
                                c.CustomField14 = currentContactNode.Value;
                                break;
                            case ContactXmlElementCustomField15:
                                c.CustomField15 = currentContactNode.Value;
                                break;
                            case ContactXmlElementEmailAddresss:
                                c.EmailAddress = currentContactNode.Value;
                                break;
                            case ContactXmlElementEmailType:
                                c.EmailType = GetEmailType(currentContactNode.Value);
                                break;
                            case ContactXmlElementFirstName:
                                c.FirstName = currentContactNode.Value;
                                break;
                            case ContactXmlElementHomePhone:
                                c.HomePhone = currentContactNode.Value;
                                break;
                            case ContactXmlElementInsertTime:
                                c.InsertTime = currentContactNode.ValueAsDateTime;
                                break;
                            case ContactXmlElementJobTitle:
                                c.JobTitle = currentContactNode.Value;
                                break;
                            case ContactXmlElementLastName:
                                c.LastName = currentContactNode.Value;
                                break;
                            case ContactXmlElementLastUpdateTime:
                                c.LastUpdateTime = currentContactNode.ValueAsDateTime;
                                break;
                            case ContactXmlElementMiddleName:
                                c.MiddleName = currentContactNode.Value;
                                break;
                            case ContactXmlElementName:
                                c.Name = currentContactNode.Value;
                                break;
                            case ContactXmlElementNote:
                                c.Note = currentContactNode.Value;
                                break;
                            case ContactXmlElementPostalCode:
                                c.PostalCode = currentContactNode.Value;
                                break;
                            case ContactXmlElementStateCode:
                                c.StateCode = currentContactNode.Value;
                                break;
                            case ContactXmlElementStateName:
                                c.StateName = currentContactNode.Value;
                                break;
                            case ContactXmlElementStatus:
                                c.Status = GetContactStatus(currentContactNode.Value);
                                break;
                            case ContactXmlElementSubPostalCode:
                                c.SubPostalCode = currentContactNode.Value;
                                break;
                            case ContactXmlElementWorkPhone:
                                c.WorkPhone = currentContactNode.Value;
                                break;
                            case ContactXmlElementContactLists:
                                if (currentContactNode.HasChildren)
                                {
                                    // loop through all the Contact List
                                    XPathNavigator contactListsNode = currentContactNode.Clone();
                                    XPathNodeIterator lists = contactListsNode.Select("//cc:ContactList", resolver);
                                    while (lists.MoveNext())
                                    {
                                        // get current Contact List
                                        XPathNavigator list = lists.Current;

                                        ContactList contactList = new ContactList();
                                        // get the id of Contact List
                                        contactList.Id = GetContactListId(list);

                                        ContactOptInList optInList = new ContactOptInList();
                                        optInList.ContactList = contactList;

                                        if (list.HasChildren)
                                        {
                                            list.MoveToFirstChild();
                                            do
                                            {
                                                switch (list.Name)
                                                {
                                                    case ContactXmlElementOptInSource:
                                                        optInList.OptInSource = GetOptSource(list.Value);
                                                        break;
                                                    case ContactXmlElementOptInTime:
                                                        optInList.OptInTime = list.ValueAsDateTime;
                                                        break;
                                                }
                                            } while (list.MoveToNext());
                                        }

                                        // add the Contact List to current Contact
                                        c.ContactLists.Add(optInList);
                                    }
                                }
                                break;
                        }

                    } while (currentContactNode.MoveToNext());
                }

                break;
            }

            return c;
        }
 /// <summary>
 /// Get the Atom entry for newly Contact List to be send to Constant server
 /// </summary>
 /// <param name="list">Contact List to be created</param>
 /// <returns>Atom entry used to create new Contact List</returns>
 public static StringBuilder CreateNewContactList(ContactList list)
 {
     return CreateAtomEntry(list);
 }
        /// <summary>
        /// Get ContactList object from specified Xml data
        /// </summary>
        /// <param name="node">Xml data cursor model</param>
        /// <param name="resolver">Xml namespace resolver</param>
        /// <returns>Contact List</returns>
        private static ContactList GetContactList(XPathNavigator node, IXmlNamespaceResolver resolver)
        {
            ContactList cList = new ContactList();
            cList.Link = GetContactListLink(node, resolver);

            const string xpathSelect = @"at:content/cc:ContactList";

            XPathNodeIterator contactListNodes = node.Select(xpathSelect, resolver);

            while (contactListNodes.MoveNext())
            {
                XPathNavigator currentContactListNode = contactListNodes.Current;

                cList.Id = GetContactListId(currentContactListNode);
                cList.SystemList = IsPredefinedSystemList(cList.Id);

                if (currentContactListNode.HasChildren)
                {
                    currentContactListNode.MoveToFirstChild();
                    do
                    {
                        switch (currentContactListNode.Name)
                        {
                            case ContactListXmlElementName:
                                cList.Name = currentContactListNode.Value;
                                break;
                            case ContactListXmlElementShortName:
                                cList.ShortName = currentContactListNode.Value;
                                break;
                            case ContactListXmlElementOptInDefault:
                                cList.OptInDefault = currentContactListNode.ValueAsBoolean;
                                break;
                            case ContactListXmlElementSortOrder:
                                cList.SortOrder = currentContactListNode.ValueAsInt;
                                break;
                        }

                    } while (currentContactListNode.MoveToNext());
                }

                break;
            }

            return cList;
        }
        /// <summary>
        /// Create an Atom entry used to create a new Contact List
        /// </summary>
        /// <param name="list">Contact List to be created</param>
        /// <returns>Atom entry used to create new Contact List
        /// <example>
        ///     <entry xmlns="http://www.w3.org/2005/Atom">
        ///         <id>data:,</id>
        ///         <title/>
        ///         <author/>
        ///         <updated>2008-04-16</updated>
        ///         <content type="application/vnd.ctct+xml">
        ///             <ContactList xmlns="http://ws.constantcontact.com/ns/1.0/">
        ///                 <OptInDefault>false</OptInDefault>
        ///                 <Name>A New List</Name>
        ///                 <SortOrder>99</SortOrder>
        ///             </ContactList>
        ///         </content>
        ///     </entry>
        /// </example>
        /// </returns>
        private static StringBuilder CreateAtomEntry(ContactList list)
        {
            StringBuilder data = new StringBuilder();

            data.AppendFormat("<entry xmlns=\"{0}\">", AtomNamespace);
            data.Append("<id>data:,</id>");
            data.Append("<title/>");
            data.Append("<author/>");
            data.AppendFormat("<updated>{0}</updated>", DateTime.Now.ToShortDateString());
            data.Append("<content type=\"application/vnd.ctct+xml\">");
            data.AppendFormat("<ContactList xmlns=\"{0}\">", ConstantNamespace);
            data.AppendFormat("<OptInDefault>{0}</OptInDefault>", list.OptInDefault);
            data.AppendFormat("<Name>{0}</Name>", list.Name);
            data.AppendFormat("<SortOrder>{0}</SortOrder>", list.SortOrder);
            data.Append("</ContactList>");
            data.Append("</content>");
            data.Append("</entry>");

            return data;
        }