using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using edu.jhu.Cas.Util;
using edu.jhu.Cas.Users;
namespace edu.jhu.Cas.Web {
    /// <summary>
    /// Summary description for login.
    /// </summary>
    public class Login : System.Web.UI.Page {
        protected string userid, pass, nexturl, lmessage;
        private void Page_Load(object sender, System.EventArgs E) {

        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs E) {
            lmessage = Request.Params["lmessage"];
            userid = Request.Params["userid"];
            pass = Request.Params["password"];
            nexturl = Request.Params["nexturl"];
            if (nexturl == null || nexturl.Length < 1) {
                nexturl = "SubmitJob.aspx";
            }
            if (userid != null && userid.Length > 0) {
                CasUsers cu = new CasUsers();
                long id = 0;
                try {
                    id = cu.GetWebServiceId(userid, pass);
                    if (id <= 0) {
                        lmessage = "Invalid User ID / Password combination, please try again.";
                    }
                    else {
                        Session["USER"] = id;
                        Session["USERID"] = userid;
                        Global.users.Add(userid);
                        try {
                            cu.HasPrivilege(id, PRIVS.SIGHT);
                            Session["ADMIN"] = true.ToString();
                        }
                        catch {
                            Session["ADMIN"] = false.ToString();
                        }
                        Response.Redirect(nexturl);
                    }
                }
                catch (Exception e) {
                    lmessage = e.Message;
                }
            }
            if (userid == null) {
                userid = "";
            }
            InitializeComponent();
            base.OnInit(E);

        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}