namespace Employee_Directory
{
	
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

	public class AdminMenu : System.Web.UI.Page
	
    { 

//登录窗口定义控件的声名
		protected CCUtility Utility;
		
		//登录表单、变量等的声名
		protected System.Web.UI.WebControls.TextBox Login_name;
		protected System.Web.UI.WebControls.TextBox Login_password;
		protected System.Web.UI.WebControls.Button Login_login;
		protected System.Web.UI.HtmlControls.HtmlTableRow Login_trname;
		protected System.Web.UI.HtmlControls.HtmlTableRow Login_trpassword;
		protected System.Web.UI.WebControls.Label Login_labelname;
		protected System.Web.UI.WebControls.Label Login_message;
		protected System.Web.UI.HtmlControls.HtmlInputHidden Login_querystring;
		protected System.Web.UI.HtmlControls.HtmlInputHidden Login_ret_page; 
	
		//定义各表单事件保护字符串
		protected string Form_FormAction=".aspx?";
		protected string Login_FormAction="AdminMenu.aspx?";
		

//初始化事件
	public AdminMenu()
	{
	this.Init += new System.EventHandler(Page_Init);
	}
//AdminMenu中的自定义包含控件结束

	public void ValidateNumeric(object source, ServerValidateEventArgs args) {
			try{
				Decimal temp=Decimal.Parse(args.Value);
				args.IsValid=true;
		        }catch{
				args.IsValid=false;	}
		}


//定义登录窗口显示控件过程
//初始化页面过程，创建一个CCUtility实例，并调用其相应的各方法
        protected void Page_Load(object sender, EventArgs e)
        {	
		Utility=new CCUtility(this);
		Utility.CheckSecurity(3);
		// 完成窗口安全验证
		if (Session["UserID"] != null && Int16.Parse(Session["UserID"].ToString()) > 0)
		Login_logged = true;

		if (!IsPostBack){
			Page_Show(sender, e);
		}
	}

//页面关闭过程
	protected void Page_Unload(object sender, EventArgs e)
	{
		if(Utility!=null) Utility.DBClose();
	}

//窗口中控件定义事件处理过程
	protected void Page_Init(object sender, EventArgs e)
	{
		InitializeComponent();
	}
        private void InitializeComponent()
        {
		this.Load += new System.EventHandler(this.Page_Load);
		this.Unload += new System.EventHandler(this.Page_Unload);
Login_login.Click += new System.EventHandler (this.Login_login_Click);
		
        }

//定义整体显示页面过程
        protected void Page_Show(object sender, EventArgs e)
        {
		Login_Show();
        }



//AdminMenu Show end
//完成表单初始化


//登录表单显示过程
protected bool Login_logged = false;
void Login_Show() {

	if (Login_logged) {
		//用户登录成功时		
		Login_login.Text = "退出登录";
		Login_trpassword.Visible = false;
		Login_trname.Visible = false;
		Login_labelname.Visible = true;
		Login_labelname.Text ="用户" + Utility.Dlookup("emps", "emp_login", "emp_id=" + Session["UserID"]) + "&nbsp;&nbsp;&nbsp;";
	} else {
		//用户登录失败时或没有登录时
		Login_login.Text = "登录";
		Login_trpassword.Visible = true;
		Login_trname.Visible = true;
		Login_labelname.Visible = false;
	}
}



void Login_login_Click(Object Src, EventArgs E) {
	if (Login_logged) {
		//如果登录成功，则退出登录，同时进行相应处理
		Login_logged = false;
		Session["UserID"] = 0;
		Session["UserRights"] = 0;
		Login_Show();
		//重新登录
		Response.Redirect("login.aspx");
		
	} else {
		
		//进行登录验证
		int iPassed = Convert.ToInt32(Utility.Dlookup("emps", "count(*)", "emp_login ='" + Login_name.Text + "' and emp_password='" + CCUtility.Quote(Login_password.Text) + "'"));
		if (iPassed > 0) {
			
		Login_message.Visible = false;
			Session["UserID"] = Convert.ToInt32(Utility.Dlookup("emps", "emp_id", "emp_login ='" + Login_name.Text + "' and emp_password='" + CCUtility.Quote(Login_password.Text) +"'"));
			Login_logged = true;
			
			Session["UserRights"] = Convert.ToInt32(Utility.Dlookup("emps", "emp_level", "emp_login ='" + Login_name.Text + "' and emp_password='" + CCUtility.Quote(Login_password.Text) + "'"));
			
			string sQueryString = Utility.GetParam("querystring");
			string sPage = Utility.GetParam("ret_page");
			if (! sPage.Equals(Request.ServerVariables["SCRIPT_NAME"]) && sPage.Length > 0) {
				Response.Redirect(sPage + "?" + sQueryString);
			} else {
				Login_Show();
			}
		} else {
			Login_message.Visible = true;
		}
	
	}
}

    }
}