<%@ Page Language="c#" %>

<%@ Register TagPrefix="uc1" TagName="Trailer" Src="Trailer.ascx" %>
<%@ Register TagPrefix="uc2" TagName="Header" Src="top.ascx" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="edu.jhu.Cas.Users" %>
<%@ Import Namespace="edu.jhu.Cas.Util" %>
<%@ Import Namespace="edu.jhu.Cas.Web" %>
<%@ Import Namespace="System.IO" %>
<%

    WebUtils.CheckLoggedIn(Session, Response);
    long wsid = (long)Session["USER"];
    string lmessage = Request.Params["lmessage"];
    string pmessage = Request.Params["pmessage"];

    edu.jhu.Cas.Util.User user = null;

    CasUsers cu = new CasUsers();


    if (Request.Params["Name"] == null) { // not submitted yet
        // lookup other details
        try {
            User[] users = cu.FindUser(edu.jhu.Cas.Util.ID.SERVICE_ID, "WEBSERVICESID=" + Session["USER"]);
            if (users == null) {
                lmessage = "There is a problem with your session - please login and try again.";
            }
            else {
                user = users[0];
                Session["THEUSER"] = user;
            }
        }
        catch (Exception e) {
            lmessage = e.Message;
        }
        if (user == null) {
            Response.Redirect("ChangeDetails.aspx?lmessage=" + lmessage);
        }
    }
    else {
        string pass = Request.Params["newpassword"];
        string cpass = Request.Params["confirmpassword"];
        if (pass != null && cpass != null) {
            if (pass != cpass) {
                Response.Redirect("ChangeDetails.aspx?pmessage=The password fields are not the same.");
            }
            else {
                //do the change
                try {
                    // need to pack user
                    user = (User)Session["THEUSER"];
                    bool isvis = user.GVisible;
                    string privMes = "";
                    if (isvis) privMes = "Your name remains visible to all other users in the group screen.";
                    else privMes = "Your name remains invisible to all other users in the group screen.";

                    if (Request.Params["gvisible"] == null && isvis) {// get rid of it
                        user.GVisible = false;
                        privMes = "Your name will no longer be visible to all other users in the group screen.";
                    }
                    if (Request.Params["gvisible"] != null && !isvis) { // add it 
                        user.GVisible = true;
                        privMes = "Your name will now be visible to all other users in the group screen.";
                    }
                    user.UserID = Request.Params["userid"];
                    user.Name = Request.Params["name"];
                    user.Email = Request.Params["email"];
                    if (Request.Params["newpassword"] != null && Request.Params["newpassword"].Length > 0) {
                        user.PWEncrypted = Request.Params["newpassword"];
                    }
                    else user.PWEncrypted = null;
                    user.EmailLevel = Convert.ToInt32(Request.Params["emaillevel"]);
                    user.LastModified = DateTime.Now;
                    bool ok = cu.UpdateAccount(wsid, user);
                    if (ok) {
                        Response.Redirect("default.aspx?lmessage=Your account was updated sucessfully.");
                    }
                    else {
                        lmessage = "There was a problem updating your account.";
                    }
                }catch (Exception e) {
                    lmessage = e.Message;
                }
            }
        }
    }

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Change Details</title>
    <link href="cjobs.css" rel="stylesheet" />
</head>
<body>
    <uc2:Header ID="Header" runat="server" />
    <p>
        <%=lmessage%>
    </p>
    <form name="updateAccount" action="ChangeDetails.aspx" method="post">
        <table class="uform">
            <tr>
                <th class="bx">
                    WSID</th>
                <td>
                    <%=wsid%>
                </td>
            </tr>
            <tr>
                <th class="bx">
                    Userid</th>
                <td>
                    <input type="text" name="userid" value="<%=user == null ? Request.Params["userid"] : user.UserID%>"
                        size="32" />
                </td>
                <% if (Request.Params["userid"] != null && Request.Params["userid"].Length == 0) {%>
                <td>
                    <p>
                        Please enter a userid
                    </p>
                </td>
                <%}%>
            </tr>
            <tr>
                <th class="bx">
                    Full Name</th>
                <td>
                    <input type="text" name="name" value="<%=user == null ? Request.Params["name"] : user.Name%> "
                        size="50" />
                </td>
                <% if (Request.Params["name"] != null && Request.Params["name"].Length == 0) {%>
                <td>
                    <p>
                        Please enter your name
                    </p>
                </td>
                <%}%>
            </tr>
            <tr>
                <th class="bx">
                    Email Address</th>
                <td>
                    <input type="text" name="email" value="<%=user == null ? Request.Params["email"] : user.Email%>"
                        size="50" />
                </td>
                <% if (Request.Params["email"] != null && Request.Params["email"].Length == 0) {%>
                <td>
                    <p>
                        Please enter your Email address
                    </p>
                </td>
                <%}%>
            </tr>
            <tr>
                <th class="bx">
                    Level of Emails</th>
                <td>
                    <% int el = user == null ? Convert.ToInt32(Request.Params["emaillevel"]) : user.EmailLevel;%>
                    <select name="emaillevel">
                        <option value="0" <%= (el == EMAIL.ADMIN_ONLY) ? "selected=t" : "" %>>Admin Only </option>
		                <option value="1" <%= (el == EMAIL.ON_BATCH_FAILURE) ? "selected=t" : "" %>>On Failures </option>
		                <option value="2" <%= (el == EMAIL.ON_BATCH_COMPLETION) ? "selected=t" : "" %>>On Completion </option>
		                <option value="3" <%= (el == EMAIL.ON_BATCH_FAILURE_OR_COMPLETION) ? "selected=t" : "" %>>On Fail or Complete</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th class="bx">
                    New Password</th>
                <td>
                    <input name="newpassword" type="password" value="" size="20" />
                </td>
                <td>
                    <p>
                        <%=pmessage%>
                    </p>
                </td>
            </tr>
            <tr>
                <th class="bx">
                    Confirm New Password</th>
                <td>
                    <input name="confirmpassword" value="" type="password" size="20" />
                </td>
            </tr>
            <tr>
                <th class="bx">
                    Group Visibility</th>
                <td colspan="2">
                    <% bool checkgv = (Request.Params["gvisible"] != null || (user != null && user.GVisible));%>
                    <input name="gvisible" type="CheckBox" <%=checkgv?"checked":" "%> />
                    <%if (checkgv) {%>
                    Uncheck this if you do not wish
                    <%}
  else {%>
                    Check this to have
                    <%}%>
                    your name to appear in the Group Member Selection list of other users.
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input type="submit" value="Update Account" /></td>
            </tr>
        </table>
    </form>
    <uc1:Trailer ID="Trailer1" runat="server" cvsTag="$Name: v3_5_18 $" cvsRev="$Revision: 1.10 $">
    </uc1:Trailer>
</body>
</html>
