protected void Page_Load(object sender, EventArgs e)
 {
     try
     {
         string user=Request.QueryString["user"].ToString();
         string pass = Request.QueryString["pass"].ToString();
         MD5 EncodeMDS = new MD5();
         DataSet ds = new DataSet();
         UserManagerSystem UserManage = new UserManagerSystem();
         ds=UserManage.GetUserAccount(user);
         if (ds.Tables.Count > 0)
         {
             if (ds.Tables[0].Rows.Count > 0)
             {
                 string PassUser = ds.Tables[0].Rows[0]["password"].ToString();
                 if (EncodeMDS.Verify(pass, PassUser))
                 {
                     isOk = "ok";
                     string[] userAcount = new string[3];
                     userAcount[0] = ds.Tables[0].Rows[0]["id"].ToString();
                     userAcount[1] = ds.Tables[0].Rows[0]["UserName"].ToString();
                     userAcount[2] = ds.Tables[0].Rows[0]["ContactName"].ToString();
                     Session["infoUser"] = userAcount;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Console.Write(ex.ToString());
     }
     Response.Write(isOk);
 }
    public CManageError ValidateForm(string username,string pass1,string pass2,string fullname,string jobtitle,string address,string email,string mobile,string homephone,string code)
    {
        CManageError errors = new CManageError();
        int numErr = 0;
        CValidate validate = new CValidate();
        if (username.Length > 0)
        {
            if (validate.TestUserName(username) > 0)
            {
                numErr++;
                errors.AddError(merruser);
                return errors;
            }
        }
        else
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (pass1.Length > 0)
        {
            if (pass1.Length < 4)
            {
                numErr++;
                errors.AddError(mpass);
                return errors;
            }
        }
        else
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (pass2.Length > 0)
        {
        }
        else
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (fullname.Length == 0)
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (address.Length == 0)
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (email.Length > 0)
        {

        }
        else
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (!pass1.Equals(pass2))
        {
            numErr++;
            errors.AddError(mpasserr);
            return errors;
        }
        if (code.Length > 0)
        {
            string subcode = Session["RamDomCodeRegister"].ToString();
            if (!subcode.Equals(code))
            {
                numErr++;
                errors.AddError(mcode);
                subcode = GetCodeRandom.CreateCodeRanDom(5);
                Session["RamDomCodeRegister"] = subcode;
                return errors;
            }
        }
        else
        {
            numErr++;
            errors.AddError(merr);
            return errors;
        }
        if (!validate.TestAddressEmail(email))
        {
            numErr++;
            errors.AddError(merremail);
            return errors;
        }
        //TestExsit Username, or Email:
        DataSet Ds = new UserManagerSystem().UserSelectUsernameandEmail(username, email);
        if (Ds.Tables.Count > 0)
        {
            if (Ds.Tables[0].Rows.Count > 0)
            {
                if (!Ds.Tables[0].Rows[0]["count"].ToString().Equals("0"))
                {
                    numErr++;
                    errors.AddError(muser);
                }
                if (!Ds.Tables[1].Rows[0]["count"].ToString().Equals("0"))
                {
                    numErr++;
                    errors.AddError(memail);
                }
            }
        }
        return errors;
    }
    protected void getpass_Click(object sender, EventArgs e)
    {
        try
        {
            string addressto = txtemail.Value.Trim();
            CValidate validate = new CValidate();
            UserManagerSystem usermanager=new UserManagerSystem();
            if (!validate.TestAddressEmail(addressto))
            {
                divErrors.InnerHtml = "<div class='diverror'>" + merrmail + "</div>";
                divErrors.Visible = true;
                return;
            }
            else
            {
                string code = txtcode.Value.Trim();
                if(!Session["RamDomCodeRegister"].ToString().Equals(code))
                {
                    divErrors.InnerHtml = "<div class='diverror'>" + mcodeerr + "</div>";
                    divErrors.Visible = true;
                    Session["RamDomCodeRegister"] = datacommon.CreateCodeRanDom(5);
                    return;
                }
            }
            DataSet ds=usermanager.UserSelecWithEmail(addressto);
            string username="";
            string name="";
            if(ds.Tables.Count>0)
            {
                if(ds.Tables[0].Rows.Count>0)
                {
                    username=ds.Tables[0].Rows[0]["userName"].ToString();
                    name=ds.Tables[0].Rows[0]["contactName"].ToString();
                }else
                {
                    divErrors.InnerHtml = "<div class='diverror'>" + mnothave + "</div>";
                    divErrors.Visible = true;
                    return;
                }
            }else
            {
                divErrors.InnerHtml = "<div class='diverror'>" + mnothave + "</div>";
                divErrors.Visible = true;
                return;
            }
            try
            {
                XmlDocument docservermail = new XmlDocument();
                string xpathfile = Server.MapPath("data/xml/configmailserver.xml");
                XmlTextReader readfile = new XmlTextReader(xpathfile);
                docservermail.Load(readfile);
                readfile.Close();
                XmlNode node = docservermail.SelectSingleNode("/server");
                XmlNodeList listnode = node.ChildNodes;
                int numnodes = listnode.Count;
                Hashtable hastServer = new Hashtable();
                for (int i = 0; i < numnodes; i++)
                {
                    hastServer.Add(listnode[i].Name, listnode[i].InnerText);
                }
                SmtpClient client = new SmtpClient();
                MailAddress addressfrom = new MailAddress(hastServer["from"].ToString(), hastServer["displayname"].ToString(), System.Text.Encoding.UTF8);
                MailAddress MailTo = new MailAddress(addressto);
                MailMessage message = new MailMessage();
                message.From = addressfrom;
                message.To.Add(MailTo);
                message.CC.Add(MailTo);
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.Subject = "YÊU CẦU LẤY LẠI MẬT KHẨU";
                message.IsBodyHtml = true;
                string codeGet=datacommon.CreateCodeRanDom(32);
                DateTime time = new DateTime();
                time = DateTime.Now;

                message.Body = BuildTable(username,name,hastServer["domain"].ToString(),hastServer["website"].ToString(),addressto,codeGet,time);
                client.Host = hastServer["host"].ToString();
                client.Port = int.Parse(hastServer["port"].ToString());
                Boolean testInsert = usermanager.UserInsertRequestPass(codeGet, name, addressto, time);
                if (testInsert)
                {
                    client.Credentials = new NetworkCredential(hastServer["username"].ToString(), hastServer["password"].ToString(), hastServer["domain"].ToString());
                    client.Send(message);
                    message.Dispose();
                    divErrors.Visible = true;
                    divErrors.InnerHtml = "<div class='diverror'>" + tyestosend + "</div>";
                    txtcode.Value = "";
                    txtemail.Value = "";
                    Session["RamDomCodeRegister"] = datacommon.CreateCodeRanDom(5);
                }
                else
                {
                    divErrors.Visible = true;
                    divErrors.InnerHtml = "<div class='diverror'>" + terrInsert + "</div>";
                }
            }
            catch(Exception ex)
            {
                Console.Write(ex.ToString());
                divErrors.Visible = true;
                divErrors.InnerHtml = "<div class='diverror'>" + tnotsend + "</div>";
            }
        }
        catch (Exception ex)
        {
            divErrors.InnerHtml = "<div class='diverror'>" + ex.ToString() + "</div>";
        }
    }
    protected void register_Click(object sender, EventArgs e)
    {
        string username = txtusername.Value.Trim();
        string pass1 = txtpass.Value.Trim();
        string pass2 = txtpass1.Value.Trim();
        string fullname = txtname.Value.Trim();
        string jobtitle = txtjobtitle.Value.Trim();
        string email = txtemail.Value.Trim();
        string mobile = txtmobile.Value.Trim();
        string homephone = txthomephone.Value.Trim();
        string address = txtaddress.Value.Trim();
        string code = txtcoderegister.Value.Trim();
        CManageError errors = ValidateForm(username, pass1, pass2, fullname, jobtitle, address, email, mobile, homephone, code);
        if (errors.GetNumberErr() > 0)
        {
            divErrors.Disabled = false;
            divErrors.InnerHtml = "<div class='diverror'>" + errors.GetAllError() + "</div>";
        }
        else
        {
            MD5 md5 = new MD5();
            pass1 = md5.Encrypt(pass1);
            UserManagerSystem managerUser = new UserManagerSystem();
            Boolean test = managerUser.UserInsert(username, pass1, fullname, "", jobtitle, address, "", "", "", address, "", "", "", mobile, "", homephone, "", "", email, "", "");
            if (test)
            {
                DataSet dsUser = managerUser.GetUserAccount(username);
                if (dsUser.Tables.Count > 0)
                {
                    if (dsUser.Tables[0].Rows.Count > 0)
                    {
                        string[] userAcount = new string[3];
                        userAcount[0] = dsUser.Tables[0].Rows[0]["id"].ToString();
                        userAcount[1] = dsUser.Tables[0].Rows[0]["UserName"].ToString();
                        userAcount[2] = dsUser.Tables[0].Rows[0]["ContactName"].ToString();
                        Session["infoUser"] = userAcount;
                        //Send email:

                        Response.Redirect("Default.aspx");
                    }
                }
            }
            else
            {
                divErrors.Disabled = false;
                divErrors.InnerHtml = "<div class='diverror'>" + terrConnect + "</div>";
            }
        }
    }