<%@ page contentType="text/html;charset=GBK" %>
<%
/**
 *	$RCSfile: account.jsp,v $
 *	$Revision: 1.3 $
 *	$Date: 2002/09/30 14:56:16 $
 */
%>

<%@ page import="com.jivesoftware.forum.*,
                 com.jivesoftware.forum.util.*"
	errorPage="error.jsp"
%>

<%@ include file="global.jsp" %>

<%	// Get parameters
    boolean create = ParamUtils.getBooleanParameter(request,"create");
	String username = ParamUtils.getParameter(request,"username");
	if(username!=null && !username.equals(""))
	username=username+"-user-web-"+web_account;
        
	String password = ParamUtils.getParameter(request,"password");
	String confirmPassword = ParamUtils.getParameter(request,"confirmPassword");
	String email = ParamUtils.getParameter(request,"email");
	String name = ParamUtils.getParameter(request,"name");
    String location = ParamUtils.getParameter(request,"location");
    String occupation = ParamUtils.getParameter(request,"occupation");
    String homepage = ParamUtils.getParameter(request,"homepage");
	boolean autoLogin = ParamUtils.getBooleanParameter(request,"autoLogin");
	String referrer = ParamUtils.getParameter(request,"referrer");

    if ("null".equals(referrer)) {
        referrer = null;
    }
    if (referrer == null) {
        referrer = request.getHeader("REFERER");
    }
    String createButton = ParamUtils.getParameter(request,"createButton");
    if ("取 消".equals(createButton)) {
        if (referrer == null) {
            referrer = "index.jsp";
        }
        response.sendRedirect(referrer);
        return;
    }

    // Error variables
    boolean errors = false;
	boolean errorUsername = false;
	boolean errorPassword = false;
	boolean errorConfirmPassword = false;
	boolean errorEmail = false;
	boolean errorName = false;
	boolean errorPasswordMatch = false;
    // User creation error vars
    boolean errorUserAlreadyExists = false;

    // Try creating a user account:
    if (create) {
        // Validate fields
        if (username == null || username.length() < 1) {
            errorUsername = true;
        }
        if (password == null || password.length() < 1) {
            errorPassword = true;
        }
        if (confirmPassword == null || confirmPassword.length() < 1) {
            errorConfirmPassword = true;
        }
        if (email == null) {
            errorEmail = true;
        }
        if (name == null) {
            errorName = true;
        }
        if (!errorPassword && !errorConfirmPassword) {
            if (!password.equals(confirmPassword)) {
                errorPasswordMatch = true;
            }
        }

        errors = errorUsername || errorPassword || errorConfirmPassword
                || errorEmail || errorName || errorPasswordMatch;

        // create an account if there were no errors
        if (!errors) {
		    try {
                UserManager userManager = myEnv.getForumFactory().getUserManager();
                User user = userManager.createUser(username, password, email);
                // Set the name
			    user.setName( name );
                // Set other properties
                if (location != null) {
                    user.setProperty("location", location);
                }
                if (occupation != null) {
                    user.setProperty("occupation", occupation);
                }
                if (homepage != null) {
                    user.setProperty("homepage", homepage);
                }
                // Log this user in
    			SkinUtils.login(request,response,username,password,autoLogin);
			    // redirect to main page
			    response.sendRedirect("accountSuccess.jsp");
			    return;
		    }
		    catch (UserAlreadyExistsException uaee) {
			    errors = true;
                errorUserAlreadyExists = true;
		    }
        }
    }
%>

<%  String title = "论坛 - 创建帐号"; %>
<%@ include file="header.jsp" %>

<% out.flush(); %>

<%-- Breadcrumbs --%>

<font face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>" color="<%= JiveGlobals.getJiveProperty("skin.default.linkColor") %>">
<b>
<a href="<%= JiveGlobals.getJiveProperty("skin.default.homeURL") %>"
>首页</a>
&raquo;
<a href="index.jsp" title="返回论坛列表"
>论坛</a>
&raquo;
<a href="account.jsp"
>创建帐号</a>
</b>
</font>
<p>

<font size="-1" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
您可以用下面的表单创建新帐号
<b>请注意：黑体标记的表格必须填写</b>
</font>

<p>

<%	if (create && errors) { %>
	<font size="-1" color="#ff0000" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    <%  if (errorUserAlreadyExists) { %>
        很抱歉，这个用户名已经存在，请重新选择.
    <%  } else if (errorPasswordMatch) { %>
        您输入的密码不匹配.
    <%  } else { %>
        对不起，注册出现错误，请更正黑体标名的表格
        (<font color="#FF0000"><b>*</b></font>).
    <%  } %>
    </font>
    <p>
<%	}
%>

<form action="account.jsp" method="post" name="userForm"  onsubmit="javaScript:return check_validate(this);">
<input type="hidden" name="create" value="true">
<input type="hidden" name="referrer" value="<%=referrer%>">

<table cellpadding="3" cellspacing="0" border="0" width="100%">
<tr>
    <td align="right">
    <font size="-1" face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    <b>您的姓名:</b>
    <%  if (create && errorName) { %>
    <font color="#ff0000"><b>*</b></font>
    <%  } %>
    </font>
    </td>
    <td>
    <input type="text" name="name" size="30" maxlength="50" value="<%= (name!=null)?name:"" %>">
    <%  if (create && errorName) { %>
    <br>
    <font color="#ff0000" class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
     >请输入姓名.</font>
    <%  } %>
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    <b>Email:</b>
    <%  if (create && errorEmail) { %>
    <font color="#ff0000"><b>*</b></font>
    <%  } %>
    </font>
    </td>
    <td>
    <input type="text" name="email" size="30" maxlength="50" value="<%= (email!=null)?email:"" %>">
    <%  if (create && errorEmail) { %>
    <br>
    <font color="#ff0000" class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
     >请输入一个有效的电子邮件地址.</font>
    <%  } %>
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    <b>用 户 名:</b>
    <%  if (create && errorUsername) { %>
    <font color="#ff0000"><b>*</b></font>
    <%  } %>
    </font>
    </td>
    <td>
    <input type="text" name="username" size="30" maxlength="50" value="<%= (username!=null)?username:"" %>">
    <%  if (create && errorUsername) { %>
    <br>
        <font color="#ff0000" class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
     >您的用户名至少2个字符.</font>
        <%  } %>
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    <b>密　　码:</b>
    <%  if (create && (errorPassword || errorPasswordMatch)) { %>
    <font color="#ff0000"><b>*</b></font>
    <%  } %>
    </font>
    </td>
    <td>
    <input type="password" name="password" size="15" maxlength="50" value="<%= (password!=null)?password:"" %>">
    <%  if (create && errorName) { %>
    <br>
        <font color="#ff0000" class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>"
     >您的密码至少2个字符.</font>
        <%  } %>
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    <b>确认密码:</b>
    <%  if (create && (errorConfirmPassword || errorPasswordMatch)) { %>
    <font color="#ff0000"><b>*</b></font>
    <%  } %>
    </font>
    </td>
    <td>
    <input type="password" name="confirmPassword" size="15" maxlength="50" value="<%= (confirmPassword!=null)?confirmPassword:"" %>">
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    您的地址:
    </font>
    </td>
    <td>
    <input type="text" name="location" size="30" maxlength="50" value="<%= (location!=null)?location:"" %>">
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    您的职业:
    </font>
    </td>
    <td>
    <input type="text" name="occupation" size="30" maxlength="50" value="<%= (occupation!=null)?occupation:"" %>">
    </td>
</tr>
<tr>
    <td align="right">
    <font class=p2 face="<%= JiveGlobals.getJiveProperty("skin.default.fontFace") %>">
    您的主页:
    </font>
    </td>
    <td>
    <input type="text" name="homepage" size="30" maxlength="50" value="<%= (homepage!=null)?homepage:"" %>">
    </td>
</tr>
<tr>
    <td align="center" colspan="2">
    <br>
    <input type="submit" name="createButton" value="创建帐号">
    <input type="submit" name="createButton" value="取 消">
    </td>
</tr>
</table>
<script language="JavaScript" type="text/javascript">
<!--
document.userForm.name.focus();
//-->
</script>
</form>
<Script Language="javaScript">
    function isSpace(str) {
      var len = str.length;

      for(var i=0; i< len; i++) {
        temp = str.substring(i,i+1);
        if (temp != " ") {
            return false;
        }
      }
    return true;
    }
    function isNum(str) {
    
    var len = 0;
    len = str.length;
    var i = 0;
    for( i=0; i< len; i++) {
        temp = str.substring(i,i+1);
        if ((temp >="0" && temp<="9") || (temp >="a" && temp<="z") || (temp >="A" && temp<="Z")) {
            continue;
        }
        else {
            return false;
        }
    }
    return true;
}
    function  check_validate(theForm) {
        if (isSpace(theForm.username.value)) {
            alert("用户名不能为空！");
            theForm.username.focus();
            return false;
        }
        if (!isNum(theForm.username.value)) {
            alert("用户名只能包含字母和数字！");
            theForm.username.focus();
            return false;
        }
        
        return true;
    }
    
</Script>
<br>

<%@ include file="footer.jsp" %>