<!--#INCLUDE FILE="setup.asp" -->
<!--#INCLUDE FILE="checkuser.asp" -->
<!--#INCLUDE FILE="theme.asp" -->
<!--#INCLUDE FILE="include/char.asp" -->
<!--#INCLUDE FILE="include/email.asp" -->
<%locat="班级注册"
Call top(1,1)
if membername="" Then
  	ErrMsg=ErrMsg+"<br>"+"<li>您还没有<a href=login.asp>登录</a>。"
	Call Error("Information",ErrMsg)
Else
	Dim classname,enyear
	Select Case Request("action")
	Case "Join"
		Dim scID
		Call joinclass()
	Case "Create"
		Call createclass()
	Case "passwordjoin"
		Call chkclspwd()
	Case "exitpass"
		Call ExitPass()
	Case Else
		Call selclsform()
	End Select
End if


Sub joinclass()

	Dim rs,sql

	sql="Select classcount from student where userid='"&membername&"'"
	Set rs=conn.execute(sql)
	if not rs.eof and rs(0)>=Cint(IntVar(0)) Then
		ErrMsg=ErrMsg+"<br>"+"<li>对不起！每个用户只能注册"&IntVar(0)&"个班级!"
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	scID=Clng(Request("scID"))
	sql="Select classid from class where classid="&scID
	Set rs=conn.execute(sql)

	if  rs.eof and rs.bof Then
		ErrMsg=ErrMsg+"<br>"+"<li>"+"您选择的班级不存在！"
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	sql="Select openlevel from class where classid="&scID
	Set rs=conn.execute(sql)

	openlevel=rs(0)

	if SysAdmin Then openlevel=0

	Select Case openlevel
	Case 0
		Call add2class()
	Case 1
		Call pswform()
	Case 2
		Call waitpass()
	Case 3
		ErrMsg=ErrMsg+"<br><li>班级管理员已经锁定了这个班级，禁止其他用户加入！"
		Call Error("Information",ErrMsg)
	End Select

End Sub


Sub CreateClass()

	Dim rs,sql
	Dim FoundErr,ErrMsg
	Dim RestrictWord,i

	if chkpost=False Then
		ErrMsg=ErrMsg+"<Br>"+"<li>您提交的数据不合法，请不要从同学录外部提交信息。"
		Call Error("Critical",ErrMsg)
		Exit Sub
	End if

	if BoolVar(2)=False Then
		ErrMsg=ErrMsg+"<br>"+"<li>对不起！系统已设置不允许创建新的班级!"
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	sql="Select classcount from student where userid='"&membername&"'"
	Set rs=conn.execute(sql)
	if not rs.eof and rs(0)>=Cint(IntVar(0)) Then
		ErrMsg=ErrMsg+"<br>"+"<li>对不起！每个用户只能注册"&IntVar(0)&"个班级!"
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	classname=replace(trim(Request.form("classname")),"'","")
	if classname="" Then
		ErrMsg=ErrMsg+"<br>"+"<li>"+"请输入您要创建的班级名称！"
		founderr=True
	ElseIf strlength(classname)<5 or strlength(classname)>25 Then
		ErrMsg=ErrMsg+"<br>"+"<li>"+"请输入班级名称5-25个字符！"
		founderr=True
	Else
		if StrVar(3)<>"" then
			RestrictWord=split(StrVar(3),"|")
			for i=0 to ubound(RestrictWord)
				if instr(classname,RestrictWord(i))=0 then
					ErrMsg=ErrMsg+"<br>"+"<li>"+"班级名称必须包含指定的限定词！"
					founderr=True
					exit for
				end if
			next
		else
			sql="Select classid from class where classname='"&classname&"'"
			Set rs=conn.execute(sql)
			if not (rs.eof and rs.bof) Then
				ErrMsg=ErrMsg+"<br>"+"<li>"+"您输入的班级名已经存在，请在直接在班级列表中选择或重新输入其他班级名！"
				founderr=True
			End if
			Set rs=Nothing
		end if
	End if

	enyear=trim(Request.form("enyear"))

	thisyear=cint(year(date()))
	startyear=thisyear-50

	if not isinteger(enyear) or not (cint(enyear)>=startyear and cint(enyear)<=thisyear ) Then
		ErrMsg=ErrMsg+"<br>"+"<li>"+"您输入的入学年份不正确！"
		founderr=True
	End if

	if founderr=True Then
		Call Error("Exclamation",ErrMsg)
		Exit Sub
	End if

	'rights="11010010110000000011001000010000001"

	sql="insert into class (classname,enyear,creator,openlevel) values('"&classname&"','"&_
		enyear&"','"&membername&"',"&IntVar(6)&")"
	conn.execute(sql)

	sql = "select max(classid) from class"
	Set rs=conn.execute(sql)
	ncid=rs(0)


	VoteTitle="您在使用"&StrVar(1)&"后感觉如何？"
	VoteItem="非常好，我很满意"&vbCrLf&"不错，还可以吧"&vbCrLf&"太一般了"

	sql="insert into VoteBox (UserName,VoteTitle,VoteItem,VoteType,"&_
		"ClassID,Vote) values('"&StrVar(1)&"','"&VoteTitle&"','"&VoteItem&"',False,"&ncid&",'0,0,0')"
	conn.execute(sql)

	sql="update config set ClassNum=ClassNum+1"
	conn.execute(sql)

	sql="update student set classcount=classcount+1,point=point+"&PointSet(10)&" where userid='"&membername&"'"
	conn.execute(sql)

	sql="insert into joinclass (userid,classid,jointime,lastvisit,"&_
		"isauditing) values('"&membername&"',"&ncid&",now(),now(),False)"
	conn.execute(sql)

	Set rs=Nothing

	rURL="MyClass.asp?ClassID="&ncid
	rtitle="创建班级成功"
	rmsg="您已经成功的创建了班级：<font color=brown>"&htmlencode(classname)&"</font>，并成为该班级的成员。<br>"&_
			"<ul><li><a href="&rURL&">进入班级页面</a>"&_
			"</li></ul>"
	Redirect 3,rurl,rtitle,rmsg

End Sub



Sub ExitPass()

	Dim sql,rs
	Dim FoundErr,ErrMsg

	pclassid=Request.querystring("classid")
	if pclassid<>"" Then pclassid=Clng(pclassid)

	sql="Select isauditing from joinclass where userid='"&membername&"' and classid="&pclassid
	Set rs=conn.execute(sql)
	if rs.eof and rs.bof Then
		ErrMsg=ErrMsg+"<br>"+"<li>你没有向班级管理员提出申请加入这个班级!"
		founderr=True
	ElseIf rs(0)=False Then
		ErrMsg=ErrMsg+"<br>"+"<li>你已经通过班级管理员的审核并成为这个班级的正式成员了！"
		founderr=True
	End if
	Set rs=Nothing

	if founderr=True Then
		Call Error("Information",ErrMsg)
	Else
		sql="update student Set classcount=classcount-1 where userid='"&membername&"'"
		conn.execute(sql)

		sql="delete from joinclass where isauditing=true and classid="&pclassid&" and userid='"&membername&"'"
		conn.execute(sql)	

		stitle="退出审核"
		smsg="你已经成功的退出班级管理员的审核状态！"
		Call success(stitle,smsg)
	End if

End Sub


Sub selclsform()

Dim enyearRs
Dim enyear
%>
<!--#INCLUDE FILE="script/popup.inc" -->
	<BR>
	<form name="selsc" action="RegClass.asp" method="POST" onsubmit="return submitonce(this)">
	<input type=hidden name=action value=Join>

	<TABLE width=95% align=center>
	<TR><TD align=left><IMG SRC="Images/no1.jpg" WIDTH="14" HEIGHT="14" hspace=3><span style="FONT-SIZE: 10pt">你的班级是下面班级列表中的哪一个？(点击班级名浏览班级信息)</span></TD>
	</TR></TABLE>

	<table border="0" width="95%" Class=TableBorder cellspacing="1" cellpadding="2" align=center>
	<tr height=25>
		<th align="center" width=6%>选择</th>
		<th align="center" width=36%>班&nbsp;级&nbsp;名&nbsp;称</th>
		<th align="center" width=8%>人&nbsp;数</th>
		 <th align="center" colspan=2 width=26%>管&nbsp;理&nbsp;员</th>
		 <th align="center" width=10%>开放等级</th>
		 <th align="center" width=14%>		 
<%
if ClassNum<=50 Then
	Response.write "入学年份"
Else
	Set enyearRs=conn.execute("select distinct enyear from class order by enyear")
	Response.write "<Select	name=enyear size=1 onchange=""location.href='RegClass.asp?enyear='+options[this.selectedIndex].value"">"
	Response.write "<option value=''>入学年份</option>"
	do until enyearRs.eof
		Response.write "<option value="&enyearRs(0)
		if cstr(enyearRs(0))=Request("enyear") Then Response.write " selected"
		Response.write ">&nbsp;&nbsp;"&enyearRs(0)&"&nbsp;&nbsp;</option>"
	enyearRs.movenext
	loop	
	Response.write "</Select>"
	Set enyearRs=Nothing
End if


Response.write "</th></tr>"

enyear=Request("enyear")
if enyear="" Then wherecond="" Else wherecond="where enyear="&cint(enyear)&" "

Set rs = Server.CreateObject("ADODB.Recordset")
sql="Select classid,classname,admin1,admin2,studcount,openlevel,enyear from class "&wherecond&"order by enyear,classname"
rs.Open sql,conn,1,1

if rs.eof and rs.bof Then 
	Response.write "<tr Class=LightTableBody><td colspan=7 height=30>&nbsp;&nbsp;目前还没有任何班级噢:(</td></tr>"
Else

	rs.pagesize=20
	maxpages=rs.pagecount

	if isInteger(Request("page")) Then
		currentpage=cint(Request("page"))
	Else 
		CurrentPage=1
	End if

	if currentpage>maxpages Then currentpage=maxpages
	rs.absolutepage=CurrentPage

	for i=1 to 20
		response.write "<tr Class=LightTableBody><td align=center>"&_
			"<input type=Radio name=scId value="&rs(0)&"></td><td>&nbsp;"&_
			"<a target=_blank href=""BrowseClass.asp?ClassID="&rs(0)&""" title=""浏览"&rs(1)&"班级信息"">"&htmlencode(rs(1))&"</a></td>"&_
			"<td align=center>"&rs(4)&"</td><td width=""13%"">&nbsp;"
		if rs(2)<>"" Then 
			Response.write "<a target=_blank href=""Profile.asp?Name="&rs(2)&""" onmouseout=""kill()"" onmouseover=""popup('查看"&rs(2)&"的个人信息！','"&LightTBColor&"')"">"&htmlencode(rs(2))&"</a>"
		end if
		response.write "</td><td>&nbsp;"
		if rs(3)<>"" Then 
			Response.write "<a target=_blank href=""Profile.asp?Name="&rs(3)&" onmouseout=""kill()"" onmouseover=""popup('查看"&rs(3)&"的个人信息！','"&LightTBColor&"')"">"&htmlencode(rs(3))&"</a>"
		end if
		response.write "</td><td align=center>"
		select case rs(5)
		case 0
			  Response.write "<img src=images/open.gif width=16 onmouseout=kill() onmouseover=""popup('该班级完全开放，可以自由加入！','"&LightTBColor&"')"">"
		case 1
			  Response.write "<img src=images/passwd.gif onmouseout=kill() onmouseover=""popup('加入该班级需要向班级管理员索取班级密码！','"&LightTBColor&"')"">"
		case 2
			  Response.write "<img src=images/auditing.gif onmouseout=kill() onmouseover=""popup('你需要通过班级管理员的验证才能加入这个班级！','"&LightTBColor&"')"">"
		case 3
			  Response.write "<img src=images/lock1.gif width=14 onmouseout=kill() onmouseover=""popup('该班级已被管理员锁定，不允许其他人加入！','"&LightTBColor&"')"">"
		End select
		response.write "</td><td align=center>"&rs(6)&"</td></tr>"
		rs.movenext
		if currentpage=maxpages then
			if rs.eof then exit for
		end if
	next

End if
rs.close
Set rs=Nothing
%>
</table>

<TABLE width=95% align=center>
<TR><TD width=100%><table width=100% border=0><tr><td align=left width=140 nowrap><%Response.write "页次: "&currentpage&"/"&maxpages&"&nbsp;&nbsp;&nbsp;去第"%><input type=hidden value="<%=enyear%>" name="selenyear">&nbsp;<input type=text name=targetPage size=2 style="height:16px;width:20px;">&nbsp;页</td><td width=15 align=right style="cursor:hand" onclick="document.location.href='RegClass.asp?enyear='+selenyear.value+'&page='+targetPage.value;"><img src=images/btngo.gif width=13 height=15></td><td align=center><% if currentpage=0 Then currentpage=1
if not  (currentpage=1 or maxpages=0) Then
	Response.write "　<a href=""?page="&currentpage-1&"&enyear="&enyear&"""><img src=images/left.gif border=0>&nbsp;上一页</a>"
End if
		
if not (currentpage=maxpages or maxpages=0) Then
	Response.write "　<a href=""?page="&currentpage+1&"&enyear="&enyear&"""><img src=images/right.gif border=0>&nbsp;下一页</a>"
End if
%></td><td align=right width=75><input type="submit" value="加入班级(J)" name="sbmtj" accesskey="j" class=button></td></tr></table></TD>
</TR></TABLE>
</form>

<BR>
<form name="selsc" action="RegClass.asp" method="POST" onsubmit="return submitonce(this)">
<input type=hidden name=action value=Create>

<TABLE width=95% align=center>
<TR><TD align=left><IMG SRC="Images/no2.jpg" WIDTH="16" HEIGHT="16" hspace=3><span style="FONT-SIZE: 10pt">如果上表中还没有你想要加入的班级，请在下面创建班级：</span></TD>
</TR></TABLE>

<table border="0" width="95%" Class=TableBorder cellspacing="1" cellpadding="7" align=center>
<tr Class=LightTableBody>
	<td width="60%" align="center" valign=center>
	&nbsp;&nbsp;班级名：<input name="classname" size="28" maxlength="28">&nbsp;*
	</td>
	<td width="40%" align="center" valign=center>
入学年份：<Select
		      name="enyear" size="1"><%
				thisyear=cint(year(date()))
				startyear=thisyear-50
				for i=startyear to thisyear%>
				<option value="<%=i%>" <%if i=thisyear-4 Then Response.write "selected"%>><%=i%></option>
				<%next%>
				</Select>&nbsp;*<br>
	</td>
</tr>

<tr Class=LightTableBody>
<td colspan=2><table border=0 cellpadding=3><tr><td class=mp>为避免出现同一班级多个名称的现象,班级名必须遵守以下规范！<br>
				(1)在班级名中尽量写明入学年份,让人一目了然,如"99级1班"<br>
				(2)请用中文,文字之间不要有空格,班级名称长度在5至25个字符之间.<BR>
				(3)如果发现班级名不规范或者重复，请速与<a href=mailto:<%=StrVar(2)%>>管理员联系</a><br>
				<%if not isnull(StrVar(3)) then%>(4)系统已设定班级名称必须包含以下限定词：<B><%=replace(StrVar(3),"|","</b>,<b>")%></B>。<%end if%></td></tr></table></td></tr>
</table>

<TABLE width=95% align=center>
<TR><TD align=right><input type="submit" value="创建班级(C)" accesskey="c" class=button></TD>
</TR></TABLE>
</form>

 <%
End Sub


Sub add2class()

	Dim rs,sql
	sql="Select classid from joinclass where userid='"&membername&"' and classid="&scID
	Set rs=conn.execute(sql)
	if not (rs.eof and rs.bof) Then
		ErrMsg=ErrMsg+"<br><li>您已经加入这个班级了！"
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	sql="update class Set studcount=studcount+1 where classid="&scID
	conn.execute(sql)

	sql = "Select classname from class where classid="&scID
	Set rs=conn.execute(sql)
	scname=rs(0)
	Set rs=Nothing

	sql="update student Set classcount=classcount+1,point=point+"&PointSet(10)&" where userid='"&membername&"'"
	conn.execute(sql)

	sql="insert into joinclass (userid,classid,jointime,lastvisit,"&_
	"isauditing) values('"&membername&"',"&scID&",now(),now(),False)"
	conn.execute(sql)

	'if sendmailoption=True Then
	sql = "Select realname,email from student where userid='"&membername&"'"
	Set rs=conn.execute(sql)
	studname=rs(0)
	studmail=rs(1)

	sql="select distinct S.email from student S inner join joinclass J on S.userid=J.userid where J.classid="&scID&"and J.userid<>'"&membername&"'"
	Set rs=conn.execute(sql)

	if not rs.eof and IntVar(5)<>0 Then
	mailto=rs("email")
	rs.movenext
	do until rs.eof
		mailto=mailto&","&rs("email")
		rs.movenext
	loop
	Set rs=Nothing

	mailfrom=StrVar(2)
	MailFromName=StrVar(1)
	mailtopic="好消息，贵班又增新成员!--"&StrVar(1)
	mailbody="亲爱的同学，您好！"&vbCrLf&vbCrLf&_
	"   贵班（"&scname&"）又有新成员加入："&vbCrLf&vbCrLf&_
	"   姓名："&studname&vbCrLf&_
	"   EMAIL："&studmail&vbCrLf&vbCrLf&_
	"   快快去信给他（她）一声问候吧！"&vbCrLf&vbCrLf&_
	string(44,"-")&vbCrLf&space(4)&StrVar(1)&vbCrLf&_
	space(4)&SchoolmateURL&vbCrLf
		select case Cint(IntVar(5))
			case 1
				Call jmail_smtp()		
			case 2
				Call Cdonts()		
			case 3
				Call aspemail()
			case 4
				Call jmail_msg()		
		End select
	End if
	'End if

	rURL="MyClass.asp?ClassID="&scID
	rtitle="注册班级成功"
	rmsg="您已经成功的注册了班级：<font color=brown>"&htmlencode(scname)&"</font>。<br><ul>"&_
			"<li><a href="""&rURL&""">进入班级页面</a></li>"&_
			"</ul>"
	Redirect 3,rurl,rtitle,rmsg

End Sub


Sub WaitPass()

	sql="Select isauditing from joinclass where userid='"&membername&"' and classid="&scID
	Set rs=conn.execute(sql)
	if not rs.eof Then
		if rs(0)=False Then
			ErrMsg=ErrMsg+"<br><li>您已经加入这个班级，无需再申请加入！"
			founderr=True
		Else
			ErrMsg=ErrMsg+"<br><li>您已经向班级管理员发出加入这个班级的请求，目前仍处于班长审核之中！"
			founderr=True
		End if
	End if

	if founderr=True Then
		Call Error("Information",ErrMsg)
		Exit Sub
	End if

	sql="update student set classcount=classcount+1 where userid='"&membername&"'"
	conn.execute(sql)

	SQL = "select classname from class where classid="&scID
	Set rs=conn.execute(sql)
	scname=rs(0)

	sql="insert into joinclass (userid,classid,isauditing) values('"&membername&"',"&scID&",true)"
	conn.execute(sql)

	stitle="发出请求"
	smsg="<li>您已经成功的向<font color=brown>"&htmlencode(scname)&"</font>的班级管理员发出加入班级的请求。"
	smsg=smsg&"<br>但是在没有通过班级管理员的审核前，你暂时还不能访问这个班级"
	Call success(stitle,smsg)

End Sub


Sub pswform()
%><br><br>
<form name="selsc"
    action="RegClass.asp" method="POST">
	<input type=hidden name=Action value=passwordjoin>
	<input type=hidden name="scID" value=<%=scID%>>
    <table cellpadding=0 cellspacing=0 border=0 width=90% Class=TableBorder align=center>
        <tr>
            <td>
                <table cellpadding=5 cellspacing=1 border=0 width=100%>
    <tr align="center"> 
      <th width="100%" colspan=2><b>班级注册密码验证</b></td>
    </tr>
    <tr Class=LightTableBody> 
      <td width="40%"  class=mp align=center>请输入班级密码：</font>
      </td><td>&nbsp;&nbsp;<input type=password name=clspwd size=30> </td>
    </tr>
    <tr align="center"> 
      <td width="100%" colspan=2 Class=DarkTableBody>
<input type=submit name=submit value="提交">
      </td>
    </tr>  
    </table>   </td></tr></table></form>
<%
End Sub


Sub chkclspwd()

	if chkpost=False Then
		ErrMsg=ErrMsg+"<Br>"+"<li>您提交的数据不合法。"
		Call Error("Critical",ErrMsg)
		Exit Sub
	End if

	clspwd=Request.form("clspwd")
	clspwd=replace(clspwd,"'","")

	scID=Clng(Request.form("scID"))
	sql="Select classpwd from class where classid="&scID
	Set rs=conn.execute(sql)

	if clspwd<>rs("classpwd") Then
		ErrMsg=ErrMsg+"<br><li>班级密码错误!"
		Call Error("Exclamation",ErrMsg)
		Exit Sub
	End if

	Call add2class()

End Sub
%>

<%Call footer%>


