public IContactImporter CreateContactImporter(string username, string password, string type)
        {
            var contactImporter = new ContactImporter(username, password, type);

            return new ContactImporterWrapper(contactImporter);
        }
    protected void lbtnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string[] nameArray;
            string[] emailArray;
            string emailsite = GetEmailService();

            ContactImporter importer = new ContactImporter(txtUserName.Text, txtPassword.Text, emailsite);
            importer.login();

            bool isLogin = importer.logged_in;

            if (isLogin)
            {
                importer.getcontacts();

                lblErrMsg.InnerHtml = "";
                lblErrMsg.Visible = false;
            }
            else
            {
                lblErrMsg.InnerHtml = ShowMessage("<h2>Oops - there was a problem with your login detail..</h2>", "Invalid Login", 1);
                lblErrMsg.Visible = true;
                return;
            }

            nameArray = importer.nameArray;
            emailArray = importer.emailArray;

            if ((emailArray.Length > 0) && (nameArray.Length > 0))
            {
                IList<Contacts> lstContacts = new List<Contacts>();

                for (int i = 0; i < emailArray.Length; i++)
                {
                    Contacts tmpContacts = new Contacts();

                    tmpContacts.ConatctName = nameArray[i];
                    tmpContacts.Email = emailArray[i];

                    lstContacts.Add(tmpContacts);
                    tmpContacts = null;
                }

                lbtnInviteGuest.Enabled = true;
                lbtnRemove.Enabled = true;

                // close the popup
                ScriptManager.RegisterClientScriptBlock(lbtnLogin, GetType(), "ClosePopup", "modalClose();", true);

                repContactList.DataSource = lstContacts;
                repContactList.DataBind();

                lblContactMsg1.Text = emailArray.Length + " contacts have been imported from " + emailsite.Substring(0, emailsite.IndexOf('.'));

                // open the contact list popup
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "ContactPopup", "doModalContact();", true);
            }
            else
            {
                // close the popup
                ScriptManager.RegisterClientScriptBlock(lbtnLogin, GetType(), "ClosePopup", "modalClose();", true);

                lblErrMsg.InnerHtml = ShowMessage("No contacts found", 0);
                lblErrMsg.Visible = true;
            }
        }
        catch //(Exception ex) // commented by Ud to remove warning
        {
            // close the popup
            ScriptManager.RegisterClientScriptBlock(lbtnLogin, GetType(), "ClosePopup", "modalClose();", true);

            lblErrMsg.InnerHtml = ShowMessage("<h2>Oops - there was a problem with your login detail..</h2>", "can't connect to server", 1);
            lblErrMsg.Visible = true;
        }
        finally
        {
            // close the popup
            ScriptManager.RegisterClientScriptBlock(lbtnLogin, GetType(), "ClosePopup", "modalClose();", true);
        }
    }
        protected void btnWebMailLogin_Click(object sender, System.EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ImportEmails));

            member = (Member)Session["Member"];

            if (member == null)
            {
                Response.Redirect("/signup");
            }

            if (this.txtUserID.Text == "" || this.txtPassword.Text == "")
                return;

            Regex isNumeric = new Regex(@"^\d+$");

            //strip email address
            string Login = txtUserID.Text;

            int AtSignIndex = Login.IndexOf("@");

            if (AtSignIndex > 0)
            {
                if (emailCat.SelectedValue.ToLower() != "linkedin.com")
                {
                    Login = Login.Substring(0, AtSignIndex);
                }
            }

            DateTime start = DateTime.Now;

            contactImporter = new ContactImporter(Login, txtPassword.Text, emailCat.SelectedValue);

            contactImporter.login();

            LoginOkay = contactImporter.logged_in;

            if (LoginOkay)
            {
                try
                {
                    contactImporter.getcontacts();
                    fetch = DateTime.Now - start;
                }
                catch { }

                this.nameArray = contactImporter.nameArray;
                this.emailArray = contactImporter.emailArray;

                for (int i = 0; i < emailArray.Length; i++)
                {
                    emailArray[i] = emailArray[i].ToLower();
                }

                string ImportToken = Next2Friends.Misc.UniqueID.NewWebID();

                //insert into the db so they can be referenced by JoinEmailsWithMembers
                for (int i = 0; i < emailArray.Length; i++)
                {
                    ContactImport contact = new ContactImport();

                    contact.WebID = Next2Friends.Misc.UniqueID.NewWebID();
                    contact.ImporterMemberID = member.MemberID;
                    contact.Email = emailArray[i];
                    contact.Name = nameArray[i];
                    contact.FriendState = (int)FriendState.None;
                    contact.InviteState = (int)InviteState.None;
                    contact.ImportToken = ImportToken;
                    contact.IsInitialImport = 1;

                    contact.SaveWithCheck();
                }

                FullContacts = new List<ContactImport>();
                List<ContactImport> MemberList = new List<ContactImport>();

                //List<ContactImport> ExistingContactList = ContactImport.GetAllContactImportByMemberID(member.MemberID);

                if (emailArray.Length > 0)
                {
                    MemberList = ContactImport.JoinEmailsWithMembers(member.MemberID, emailArray, ImportToken);
                }

                List<Member> FriendList = Member.GetAllFriendsByMemberID(member.MemberID);

                for (int i = 0; i < emailArray.Length; i++)
                {
                    int friendState = (int)FriendState.None;
                    int inviteState = (int)InviteState.None;

                    ContactImport Associcate = MemberList.FirstOrDefault(f => f.Email == emailArray[i]);

                    if (Associcate != null)
                    {
                        friendState = Associcate.FriendState;
                        inviteState = Associcate.InviteState;
                    }

                    ContactImport contact = new ContactImport();

                    contact.WebID = Next2Friends.Misc.UniqueID.NewWebID();
                    contact.ImporterMemberID = member.MemberID;
                    contact.Email = emailArray[i];
                    contact.Name = nameArray[i];
                    contact.FriendState = friendState;
                    contact.InviteState = inviteState;

                    FullContacts.Add(contact);
                }

                var sortedContacts =
                    from c in FullContacts
                    where c.Email != string.Empty
                    orderby c.Name
                    select c;

                FullContacts = sortedContacts.ToList();

                Imported = true;
                ImportCount = FullContacts.Count;

                Session["Contacts"] = FullContacts;

                BuildContactList();

                ImportStage = true;

                HelperMessage = "<p style='color:#0257AE'>You may now choose your contacts to invite</p>";
                
            }
            else
            {
                BuildLetterIndexList();
                Session["Contacts"] = null;
                HelperMessage = "<p style='color:red;'>Login details are incorrect, please try again</p>";
            }
        }
 internal ContactImporterWrapper(ContactImporter importer)
 {
     _importer = importer;
 }