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

<%@ Register TagPrefix="uc1" TagName="Trailer" Src="Trailer.ascx" %>
<%@ Register TagPrefix="uc2" TagName="Header" Src="top.ascx" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="edu.jhu.Cas.Users" %>
<%@ Import Namespace="edu.jhu.Cas.Util" %>
<%
    string def = Request.Params["default"];
    if (def == null || def.Length < 1) {
        def = "default.aspx";
    }

    Session["MENU"] = MENU.CREATE;
    long serviceID = long.Parse(
        ConfigurationSettings.AppSettings["ServiceID"]);
    string lmessage = "";
    string pmessage = "";
    string pass = Request.Params["password"];
    string cpass = Request.Params["confirmpassword"];
    string userid = Request.Params["userid"];
    string email = Request.Params["email"];
    short gvisible = 0;

    if (pass != null && cpass != null) {
        if (pass != cpass) {
            pmessage = "The password fields do not agree.";
        }
        else {
            // attempt create the account
            if (userid != null && userid.Length > 0 && email != null && email.Length > 0)
                try {
                    CasUsers cu = new CasUsers();
                    string privs = null;
                    string privMes = "Your account will NOT be visible to other users in the group screen.";
                    if (Request.Params["gvisible"] != null) {
                        privMes = "Your account will be visible to other users in the group screen.";
                        gvisible = 1;
                    }
                    long nwsid = cu.CreateAccount(serviceID, userid,
                                            Request.Params["name"],
                                            email,
                                            Request.Params["password"],
                                            privs,
                                            Convert.ToInt32(Request.Params["emaillevel"]),gvisible);
                    if (nwsid > 0) {// ok created 
                        Session["USER"] = nwsid;
                        Session["USERID"] = Request.Params["userid"];
                        Response.Redirect(def + "?lmessage=Your new account has sucessfully been created!  " + privMes);

                    }
                    else {
                        lmessage = "There was a problem creating 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>Create Account</title>
    <link href="cjobs.css" rel="stylesheet" />
</head>
<body>
    <uc2:Header ID="Header" runat="server" />
    <p>
        <%=lmessage%>
    </p>
    <form name="createAccount" action="CreateAccount.aspx" method="post">
        <table>
            <tr>
                <th class="bx">
                    Userid</th>
                <td>
                    <input type="text" name="userid" value="<%=Request.Params["userid"]%>" size="32" />
                </td>
                <% if (Request.Params["userid"] != null && Request.Params["userid"].Length == 0) {%>
                <td class="message">
                    Please enter a userid
                </td>
                <%}%>
            </tr>
            <tr>
                <th class="bx">
                    Full Name</th>
                <td>
                    <input type="text" name="name" value="<%=Request.Params["name"]%>" size="50" />
                </td>
                <% if (Request.Params["name"] != null && Request.Params["name"].Length == 0) {%>
                <td class="message">
                    Please enter your name
                </td>
                <%}%>
            </tr>
            <tr>
                <th class="bx">
                    Email Address</th>
                <td>
                    <input type="text" name="email" value="<%=Request.Params["email"]%>" size="50" />
                </td>
                <% if (Request.Params["email"] != null && Request.Params["email"].Length == 0) {%>
                <td class="message">
                    Please enter your Email address
                </td>
                <%}%>
            </tr>
            <tr>
                <th class="bx">
                    Level of Emails</th>
                <td>
                    <% int el = Convert.ToInt32(Request.Params["emaillevel"]);%>
                    <select name="emaillevel">
                        <option value="0" <%= (el == 0) ? "selected=t" : "" %>>Admin Only </option>
                        <option value="1" <%= (el == 1) ? "selected=t" : "" %>>On Failures </option>
                        <option value="2" <%= (el == 2) ? "selected=t" : "" %>>On Completion </option>
                    </select>
                </td>
            </tr>
            <tr>
                <th class="bx">
                    Password</th>
                <td>
                    <input name="password" type="password" value="<%=Request.Params["password"]%>" size="20" />
                </td>
                <td class="message">
                    <%=pmessage%>
                </td>
            </tr>
            <tr>
                <th class="bx">
                    Confirm Password</th>
                <td>
                    <input name="confirmpassword" value="<%=Request.Params["confirmpassword"]%>" type="password"
                        size="20" />
                </td>
            </tr>
            <tr>
                <th class="bx">
                    Group Visibility</th>
                <td colspan="2">
                    <% bool checkgv = (Request.Params["gvisible"] != null || Request.Params["password"] == null);%>
                    <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 all other users.
                </td>
            </tr>
        </table>
        <p>
            <input type="submit" value="Create Account" />
        </p>
        <input type="hidden" name="default" value="<%=def%>" />
    </form>
    <uc1:Trailer ID="Trailer1" runat="server" cvsTag="$Name: v3_5_18 $" cvsRev="$Revision: 1.10 $">
    </uc1:Trailer>
</body>
</html>
