<!--#include file="sdcms_check.asp"-->
<%
set sdcms=new Sdcms_Admin
sdcms.Check_admin
sdcms.Check_lever 8
set sdcms=nothing
select case action
case "add":title="添加投票"
case "edit":title="修改投票"
case "show":title="查看投票"
case else:title="投票管理"
end select
sd_table="sd_vote"
sdcms_head
%>
<div class="sdcms_notice"><span>管理操作：</span><a href="?action=add">添加投票</a>　┊　<a href="?">投票管理</a></div>
<br>
<ul id="sdcms_sub_title">
	<li class="sub"><%=title%></li>
	
</ul>
<div id="sdcms_right_b">
<%
select case action
case "add":add
case "edit":edit
case "save":save
case "del":del
case "show":shows
case else:main
end select
closedb
sub main
%>
  <table border="0" align="center" cellpadding="3" cellspacing="1" class="table_b" id="tagContent0">
    <tr>
      <td width="30" class="title_bg">ID</td>
      <td class="title_bg">投票名称</td>
      <td width="160" class="title_bg">日期</td>
      <td width="160" class="title_bg">管理</td>
    </tr>
	<%
	set rs=conn.execute("select id,title,adddate  from "&sd_table&"   order by id desc")
	while not rs.eof
	%>
    <tr onmouseover=this.bgColor='#EBFFDC'; onmouseout=this.bgColor='#ffffff';  bgcolor='#ffffff'>
	<%for i=0 to 2%>
      <td <%if i<>1 then%>align="center"<%end if%>><%=rs(i)%></td>
	  <%next%>
      <td align="center"><a href='?action=show&id=<%=rs(0)%>'>查看</a> <a href="?action=edit&id=<%=rs(0)%>">编辑</a> <a href="?action=del&id=<%=rs(0)%>" onclick='return confirm("真的要删除?不可恢复!");'>删除</a></td>
    </tr>
	<%rs.movenext
	wend%>
  </table>
  
<%
end sub
sub add
check_info
%>
  <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1">
  <form name="add" method="post" action="?action=save&act=add" onSubmit='return checkadd()'>
    <tr>
      <td width="9%" align="center" class="tdbg">投票名称：      </td>
      <td width="91%" class="tdbg"><input name="t0" type="text" class="input" id="t0" size="40"></td>
    </tr>
    <tr class="tdbg">
      <td align="center">投票选项：      </td>
      <td><input name='t1' type='radio' value='1'  checked />单选 <input  name='t1' type='radio' value='0' />多选</td>
    </tr>
    <tr class="tdbg">
      <td align="center">项目内容：</td>
      <td><input type='button' class="bnt" name='addvote' value='添加项目' onclick='Addvote();'> <input type='button' class="bnt" name='modifyvote' value='修改项目' onclick='return Modifyvote();'> <input type='button' class="bnt" name='delvote' value='删除项目' onclick='Delvote();'> <input class="bnt" onClick='changepos(content,-1)'  value='上 移' type='button'> <input class="bnt" onClick='changepos(content,1)' type='button' value='下 移'><br><input type='hidden' name='votes' value='' ><select class="inputs" name='content' style='width:400px;height:200px;margin-top:5px;' size='2' ondblclick='return Modifyvote();' ></select>    
        </td>
    </tr>
 
    <tr class="tdbg">
	  <td>&nbsp;</td>
      <td><input name="Submit" type="submit" class="bnt" value="保 存">
        <input name="Submit23" type="button" onClick="history.go(-1)" class="bnt" value="返 回"></td>
    </tr>
	</form>
  </table>
<%
end sub
sub edit
set rs=conn.execute("select id,title,stype,vote,result from "&sd_table&" where id="&id&"")
if rs.eof then
echo "请勿非法提交参数":Died
end if
check_info
%>
  <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1">
  <form name="add" method="post" action="?action=save&act=save&id=<%=id%>" onSubmit='return checkadd()'>
    <tr>
      <td width="9%" align="center" class="tdbg">投票名称：      </td>
      <td width="91%" class="tdbg"><input name="t0" type="text" class="input" value="<%=rs(1)%>" id="t0" size="30"></td>
    </tr>
    <tr class="tdbg">
      <td align="center">投票选项：      </td>
      <td><input name='t1' type='radio' value='1' <%=w_check(rs(2),1)%> />单选 <input  name='t1' type='radio' value='0' <%=w_check(rs(2),0)%> />多选</td>
    </tr>
    <tr class="tdbg">
      <td align="center">项目内容：</td>
      <td><input type='button' class="bnt" name='addvote' value='添加项目' onclick='Addvote();'> <input type='button' class="bnt" name='modifyvote' value='修改项目' onclick='return Modifyvote();'> <input type='button' class="bnt" name='delvote' value='删除项目' onclick='Delvote();'> <input class="bnt" onClick='changepos(content,-1)'  value='上 移' type='button'> <input class="bnt" onClick='changepos(content,1)' type='button' value='下 移'><br><input type='hidden' name='votes' value='' ><select class="inputs" name='content' style='width:400px;height:200px;margin-top:5px;' size='2' ondblclick='return Modifyvote();' ><%result=split(rs(4),"|")
for i=0 to ubound(result)
next
vote=split(rs(3),"|")
for i=0 to ubound(vote)-1
echo "<option value="&vote(i)&"|"&result(i)&">"&vote(i)&"|"&result(i)&"</option>"
next%></select></td>
    </tr>
	 
    <tr class="tdbg">
	  <td>&nbsp;</td>
      <td><input name="Submit" type="submit" class="bnt" value="保 存">
        <input name="Submit22" type="button" onClick="history.go(-1)" class="bnt" value="返 回"></td>
    </tr>
	</form>
  </table>
<%
end sub
sub shows
set rs=conn.execute("select id,title,vote,result from sd_vote where id="&id&"")
if rs.eof then
response.Redirect("?"):Died
else
vote=rs(2)
result=rs(3)
total_vote=0
vote=split(vote,"|")
result=split(result,"|")
for i=0 to ubound(result)
if not result(i)="" then total_vote=result(i)+total_vote
next
end if
%>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" class="table_b">
     <tr>
      <td width="45%" align="center" class="title_bg" >选项      </td>
      <td width="45%" class="title_bg" >比例 </td>
      <td width="10%" class="title_bg" >票数</td>
    </tr>
<%for i=0 to (ubound(vote)-1)%>
    <tr class="tdbg">
	  <td><%echo ""&i+1&". "&vote(i)&""%></td>
      <td><div style='border:1px solid #ccc;'>
<%echo "<div style='width:"
if result(i)=0 then
echo "0"
else
echo ""&Formatpercent(result(i)/total_vote,0)&""
end if
echo ";background:#f00;'>"
echo "</div>"
echo "</div>"
%></td>
      <td align="center"><%echo ""&result(i)&" ("
if total_vote<>0 then
echo ""&Formatpercent(result(i)/total_vote,0)&""
else
echo "0"
end if%>)</td>
    </tr>
<%next%>
<tr>
      <td colspan="3" align="center" class="tdbg" ><input name="Submit2" type="button" onClick="history.go(-1)" class="bnt" value="返 回"></td>
    </tr>
  </table>
<%
end sub

sub save

t0=trim(request("t0"))
t1=trim(request("t1"))
votes=trim(request("votes"))

if votes="" then
echo "<script>alert('项目内容不能为空');location.href='javascript:history.go(-1)';</script>":Died
end if
 
votes=split(votes,",")
for i=0 to (ubound(votes)-1)
if not instr(votes(i),"|")>0 then
echo "<script>alert('投票选项"&i+1&"有错误');location.href='javascript:history.go(-1)';</script>":Died
end if
 somevote=split(votes(i),"|")
   for j=0 to ubound(somevote)
   s=somevote(0)
   s1=somevote(1)
   if not isnumeric(s1) then
   echo "<script>alert('投票选项"&i+1&"值错误');location.href='javascript:history.go(-1)';</script>":Died
   end if
   next  
   vote=vote&s&"|"
   result=result&s1&"|"
next


set rs=server.CreateObject("adodb.recordset")
sql="select id,vote,result,title,stype from "&sd_table&""
if act="save" then 
 sql=sql&" where id="&id&""
end if
rs.open sql,conn,1,3

if act="add" then 
  rs.addnew
else
  rs.update
end if
rs(1)=vote
rs(2)=result
rs(3)=t0
rs(4)=t1
rs.update
if act="add" then
conn.execute("Insert into sd_log (sdcms_name,ip,content) values('"&sdcms_adminname&"','"&sdcms_adminip&"','添加投票："&rs(3)&"')")
else
conn.execute("Insert into sd_log (sdcms_name,ip,content) values('"&sdcms_adminname&"','"&sdcms_adminip&"','修改投票："&rs(3)&"')")
end if
response.Redirect("?")
end sub
sub del 
conn.execute("Insert into sd_log (sdcms_name,ip,content) values('"&sdcms_adminname&"','"&sdcms_adminip&"','删除投票："&conn.execute("select title from "&sd_table&" where id="&id&"")(0)&"')")
  conn.execute("delete from "&sd_table&" where id="&id&"")
  response.Redirect("?") 
end sub

sub pass
  conn.execute("update   "&sd_table&" set ispass="&t&" where id="&id&"")
  response.Redirect("?") 
end sub

sub check_info
with response
.write("	<script>")
.write("function changepos(obj,index)")
.write("{")
.write("if(index==-1){")
.write("if (obj.selectedIndex>0){")
.write("obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex-1))")
.write("}")
.write("}")
.write("else if(index==1){")
.write("if (obj.selectedIndex<obj.options.length-1){")
.write("obj.options(obj.selectedIndex).swapNode(obj.options(obj.selectedIndex+1))")
.write("}")
.write("}")
.write("}")

.write("function Addvote(){")
.write("  var thisvote='投票名称'+(document.add.content.length+1)+'|0'; ")
.write("  var vote=prompt('请输入投票名称和初始值，中间用“|”隔开：',thisvote);")
.write("  if(vote!=null&&vote!=''){document.add.content.options[document.add.content.length]=new Option(vote,vote);}")
.write("}")
.write("function Modifyvote(){")
.write("  if(document.add.content.length==0) return false;")
.write("  var thisvote=document.add.content.value; ")
.write("  if (thisvote=='') {alert('请先选择一个投票项目，再点修改按钮！');return false;}")
.write("  var vote=prompt('请输入投票名称和初始值，中间用“|”隔开：',thisvote);")
.write("  if(vote!=thisvote&&vote!=null&&vote!=''){document.add.content.options[document.add.content.selectedIndex]=new Option(vote,vote);}")
.write("}")
.write("function Delvote(){")
.write("  if(document.add.content.length==0) return false;")
.write("  var thisvote=document.add.content.value; ")
.write("  if (thisvote=='') {alert('请先选择一个投票项目，再点删除按钮！');return false;}")
.write("  document.add.content.options[document.add.content.selectedIndex]=null;")
.write("}")
.write("	function checkadd()")
.write("	{")
.write("	if (document.add.t0.value=='')")
.write("	{")
.write("	alert('投票名称不能为空');")
.write("	document.add.t0.focus();")
.write("	return false")
.write("	}")
 
.write("	if (document.add.content.length==0)")
.write("	{")
.write("	alert('投票项目不能为空');")
.write("	document.add.content.focus();")
.write("	return false")
.write("	}")
.write("	if (document.add.content.length<2)")
.write("	{")
.write("	alert('投票项目内容至少两个以上');")
.write("	document.add.content.focus();")
.write("	return false")
.write("	}")
.write("var s=""""; ")
.write("for(i=0;i<=document.all(""content"").length-1;i++) ")
.write("{ ")
.write("var s=s+document.add.content.options(i).value+"",""; ")
.write("} ")
.write("document.add.votes.value=s")
.write("	}")
.write("	</script>")
end with
end sub
%>  
</div>
</body>
</html>