<!--#include file="../inc/config.asp"-->
<!--#include file="../inc/conn.asp"-->
<!-- #include file="inc/Session.asp" -->
<!-- #include file="inc/checkrole.asp" -->
<HTML>
<HEAD>
<TITLE>添加投票</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<style>
body,td,input,textarea {font-size:9pt}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.style1 {
	color: #FFFFFF;
	font-weight: bold;
}
</style>

</HEAD>
<script language="JavaScript" src="../js/PublicJS.js"></script>
<BODY>

<%
'''''''''''''''''''''''''''''''''''''''''''''''
call checkPrevilege(Session("Name"),"p13")
'''''''''''''''''''''''''''''''''''''''''''''''
%>
<table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><iframe src="right_top.htm" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" height="30" width="100%"></iframe></td>
  </tr>
</table>
<%
dim id,act,title,optionNum,optionType,votestatus,expiresTime,currentTime,randomNum
id=cint(request("id"))
if(id<1) then
	response.Write("没有找到id="&id&"的记录！")
	response.End()
end if
currentTime=now
act=request.QueryString("act")

if(act="mod") then	
	title=trim(request.Form("title"))
	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"))
	votestatus=Cint(request.Form("status"))
	expiresTime=trim(request.Form("expiresTime"))	 
	if expiresTime<>"" and isdate(expiresTime)=false then
	 	Response.Write("<script>alert(""过期时间类型出错,如果不设置过期时间,请置空"");history.back();</script>")
		Response.End
	end if
	 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 vote where id="&id&" order by id desc"
	rs.open sql,conn,3,3	
	rs("title")=title
	rs("optionNum")=optionNum
	rs("optionType")=optionType
	if(votestatus=1) then
		If datediff("s",now(),formatdatetime(expiresTime))<0 then
			rs("status")=2
		else
			rs("status")=votestatus
		end if
	else
		rs("status")=votestatus
	end if	
	rs("expiresTime")=expiresTime
	rs("modiTime")=now	
	rs.update
	rs.close
	set rs=nothing
	
	redim clickArr(optionNum)
	i=0
	Set rs = Server.CreateObject("Adodb.RecordSet")				 
	rs.Open "Select clicks from voteitem where voteid="&id,conn,3,1
	if not rs.eof then
		tempClick=rs.GetRows()
		for i=0 to optionNum-1
			if(i>ubound(tempClick,2)) then
				clickArr(i)=0
			else
				clickArr(i)=tempClick(0,i)			
			end if
		next
	end if
	rs.close
	set rs=nothing
	conn.execute("delete from voteitem where voteid="&id)
	'================================添加选项======================================
	For i = 1 to optionNum
			Set rs = Server.CreateObject("Adodb.RecordSet")				 
			rs.Open "Select top 1 * from voteitem where 1=0",conn,3,3
			rs.addnew
			rs("voteid")=id
			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
			'if(isnull(clickArr(i-1)) or clickArr(i-1)<0) then
				'rs("clicks")=0
			'else
				rs("clicks")=clickArr(i-1)
			'end if
			rs.Update
			rs.Close
	Next
	set rs=nothing
	'=================================================================================
	response.Write("<p align=""center"">修改投票成功~!</p>")
	response.Write("<br><p align=""center""><a href=vote_manage.asp>继续修改==></a>")
	response.Write("&nbsp;&nbsp;<a href=vote_add.asp>添加新投票==></a></p>")
else
	dim voteid,tempArr
	Set rs = Server.CreateObject("Adodb.RecordSet")				 
	rs.Open "Select top 1 * from vote where id="&id,conn,3,1
	if not rs.eof then
		voteid=rs("id")
%>
<FORM method="POST" name="VoteForm" action="vote_mod.asp?act=mod" style="margin:0 0 0 0;">
  <TABLE width="100%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#666666">
<TR bgcolor="#FF9900">
  <TD height="24" colspan="2"><div align="center" class="style1">添加投票</div></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" value="<%=rs("title")%>"></TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD><div align="center">选项个数：</div></TD>
  <TD><div align="left">
    <input name="optionNum" type="text" id="optionNum" value="<%=rs("optionNum")%>" onBlur="ChooseOption()" onKeyPress="if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" 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" <%if(rs("optionType")=0) then response.Write("checked")%>>
  单选　
  <input name="optionType" type="radio" value="1" <%if(rs("optionType")=1) then response.Write("checked")%>>
  多选</div></TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD><div align="center">投票状态：</div></TD>
  <TD><div align="left">
    <input name="status" type="radio" value="0" <%if(rs("status")=0) then response.Write("checked")%>>
  关闭 
  <input name="status" type="radio" value="1"  <%if(rs("status")=1) then response.Write("checked")%>>
  开启 
  <input name="status" type="radio" value="2" <%if(rs("status")=2) then response.Write("checked")%>>
  过期</div></TD>
</TR>
<TR bgcolor="#F5F5F5">
  <TD><div align="center">过期时间：</div></TD>
  <TD><div align="left">
    <input name="expiresTime" type="text" id="rolename3" size="30" value="<%=rs("expiresTime")%>">
    <input name="sdaf" type="button" id="sdaf" onClick="OpenWindowAndSetValue('Inc/SelectDate.asp',280,120,window,document.VoteForm.expiresTime);document.VoteForm.expiresTime.focus();" value="选择日期">
  </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="hidden" name="id" value="<%=id%>">
		<%
			rs.close
			set rs=nothing			
		%>
        <input type=submit name=b1 value="提 交">&nbsp;&nbsp;
        <input type=reset name=b2 value="重 填">       
    </div></TD>
</TR>
</TABLE>
</FORM>
<script>
<%
	dim j
			Set rs = Server.CreateObject("Adodb.RecordSet")				 
			rs.Open "Select itemName,colors from voteitem where voteid="&id,conn,3,1
			if not rs.eof then
				tempArr=rs.GetRows()
				response.Write("var optionArray = new Array("&ubound(tempArr,2)&");"&Chr(13))
				response.Write("var colorArray = new Array("&ubound(tempArr,2)&");"&Chr(13))		
				for i=0 to ubound(tempArr,2)					
					response.Write("optionArray["&i&"]="""&tempArr(0,i)&""";"&Chr(13))	
					response.Write("colorArray["&i&"]="""&tempArr(1,i)&""";"&Chr(13))			
				next
			end if
			rs.close
			set rs=nothing
%>
function ChooseOption()
{
  var optionNum =parseInt(document.VoteForm.optionNum.value);
  var i,j,Optionstr;
  var optionT,colorV;
  Optionstr = '<table width="400px" border="0" cellspacing="2" cellpadding="4" align="left">';
  for (i=1;i<=optionNum;i++)
 {
 	j=i-1;
 	 if(j<optionArray.length){
	 	optionT=optionArray[j];
		colorV=colorArray[j];
	 }
	 else{
	 	optionT='';
		colorV='#ff0000';
	 }
	 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="'+optionT+'">&nbsp;色彩&nbsp;<input type="text" size="11" name="Color'+i+'" value="'+colorV+'">&nbsp;<input type="button" name="Submit'+i+'" value="选色" onClick="OpenWindowAndSetValue(\'editor/selcolor.htm\',230,190,window,document.VoteForm.Color'+i+');"></div></td></tr>';
  }
  Optionstr = Optionstr+'</table>';  
  document.all.Options.innerHTML = Optionstr;
}
ChooseOption();
</script>
<iframe src="vote_tojs.asp?id=<%=id%>" width="0" height="0"></iframe>
<%	
	end if
end if
%>
</BODY>
</HTML>
<%
call connclose()
%>
