<!--#include file="../conn.asp"-->
<!--#include file="session.asp"-->
<HTML>
<HEAD>
<TITLE>添加投票</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<style>
.style1 {
	color: #FFFFFF;
	font-weight: bold;
}
</style>
<link href="css.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<%
dim act,title,optionNum,optionType,votestatus,expiresTime,currentTime,randomNum,catalogid
currentTime=now
act=request.QueryString("act")

if(act="add") then	
	title=trim(request.Form("title"))
	if(request.Form("catalogid")<>"") then
		catalogid=Clng(request.form("catalogid"))
	end if
	If  title="" then
	    Response.Write("<script>alert(""投票名称不能为空""); history.back();</script>")
		Response.End
	end if
	optionNum=Cint(request.Form("optionNum"))	
	if optionNum<1 then
	 	Response.Write("<script>alert(""选项个数必须为数字类型"");history.back();</script>")
		Response.End
	End if
	optionType=Cint(request.Form("optionType"))

	
	 For i=1 to optionNum
	     if Request.Form("Options"&i&"")="" or isnull(Request.Form("Options"&i&"")) then
		    Response.Write("<script>alert(""选项"&i&"内容不能为空"");history.back();</script>")
			Response.End
		 end if
	 Next
	'添加到库
	set rs=server.createobject("adodb.recordset")
	sql="select top 1 * from t_survey_vote order by id desc"
	rs.open sql,conn,3,3
	if not rs.eof then
		randomNum=Cint(rs("id"))+1
	else
		randomNum=1
	end if
	rs.addnew
	rs("id")=randomNum
	rs("title")=title
	rs("optionNum")=optionNum
	rs("optionType")=optionType
	rs("catalogid")=catalogid
	rs("modiTime")=now	
	rs.update
	rs.close
	set rs=nothing
	'================================添加选项======================================
	For i = 1 to optionNum
			Set rs = Server.CreateObject("Adodb.RecordSet")				 
			rs.Open "Select top 1 * from t_survey_item where 1=0",conn,3,3
			rs.AddNew
			rs("voteid") = randomNum
			rs("itemName") = Replace(Replace(Request.Form("Options"&i&""),"""",""),"'","")
			If Request.Form("Color"&i&"")<>"" then
				rs("colors") = Request.Form("Color"&i&"")
			End If
			rs("ords") = i
			rs.Update
			rs.Close
	Next
	set rs=nothing
	'=================================================================================
	response.Write("添加投票成功~!")
	response.Write("<br><p align=""center""><a href=""vote_add.asp?catalogid="&catalogid&""">继续添加==></a></p>")
else
	dim sele
	if(request.QueryString("catalogid")<>"") then
		catalogid=Clng(request.QueryString("catalogid"))
	end if
%>
<FORM method="POST" name="VoteForm" action="vote_add.asp?act=add" style="margin:0 0 0 0;">
  <TABLE width="100%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
<TR bgcolor="#FF9900">
  <TD height="24" colspan="2" bgcolor="#006699"><div align="center" class="style1">添加投票</div></TD>
  </TR>
<TR bgcolor="#F5F5F5">
  <TD><div align="center">投票项目：</div></TD>
  <TD>
	<select name="catalogid">
		<%
		set rs=server.createobject("adodb.recordset")
		rs.open "select id,title from t_survey_catalog order by id desc",conn,3,1
		while not rs.eof			
			if(catalogid=rs(0)) then
				sele=" selected"
			else
				sele=""
			end if
			response.Write("<option value="""&rs(0)&""" "&sele&">"&rs(1)&"</option>"&Chr(10))
			rs.movenext
		wend
		rs.close
		set rs=nothing
		%>		
	</select>	</TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD width="14%"><div align="center">投票标题：</div></TD>
  <TD width="86%"><input name="title" type="text" id="title" size="30"></TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD><div align="center">选项个数：</div></TD>
  <TD><div align="left">
    <input name="optionNum" type="text" id="optionNum" onBlur="ChooseOption()" onKeyPress="if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" value="4" size="10">
  </div></TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD><div align="center">选项类型：</div></TD>
  <TD><div align="left">
    <input name="optionType" type="radio" value="0" checked>
  单选　
  <input name="optionType" type="radio" value="1">
  多选</div></TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD align=right><div align="center">选项设置：</div></TD>
  <TD align=right><p align="left" id="Options">&nbsp;</p></TD>
</TR>
<TR bgcolor="#F5F5F5">
	<TD align=right>&nbsp;	  </TD>
    <TD align=right><div align="left"> 
        <input type=submit name=b1 value="提 交">&nbsp;&nbsp;
        <input type=reset name=b2 value="重 填">       
    </div></TD>
</TR>
</TABLE>
</FORM>
<script>
function ChooseOption()
 {
  var optionNum =parseInt(document.VoteForm.optionNum.value);
  var i,Optionstr;
	  Optionstr = '<table border="0" cellspacing="2" cellpadding="4" align="left">';
  for (i=1;i<=optionNum;i++)
      {
	   Optionstr = Optionstr+'<tr><td><div align="right">&nbsp;选&nbsp;项&nbsp;'+i+'：</div></td><td><div align="left"><input type="text" size="20" name="Options'+i+'" value=""></div></td></tr>';
	   }
	Optionstr = Optionstr+'</table>';  
    document.all.Options.innerHTML = Optionstr;
  }
  ChooseOption();
</script>
<%
end if
%>
</BODY>

</HTML>
<%
call connclose()
%>