<%@ Page Language="C#" MasterPageFile="~/Admin/MasterPage.master" AutoEventWireup="true"
    Inherits="PowerEasy.Module.General.WebSite.Admin.Vote.Vote" Title="投票管理" CodeBehind="Vote.aspx.cs" %>

<asp:Content ID="Content1" ContentPlaceHolderID="CphNavigation" runat="Server">
    <script language="javascript" src="<%=BasePath%>JS/jquery.pack.js" type="text/javascript"></script>
    <script src="<%=BasePath%>JS/jqDnR.js" type="text/javascript"></script>
    <script src="<%=BasePath%>JS/jquery.form.js" type="text/javascript"></script>
    <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
    </asp:ScriptManager>
    <pe:ExtendedSiteMapPath ID="SmpNavigator" SiteMapProvider="AdminMapProvider" runat="server" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CphContent" runat="Server">
    <table style="width: 100%;" cellpadding="2" cellspacing="1" class="border" id="Tabs0">
        <tr align="center">
            <td colspan="2" class="spacingtitle">
                <pe:AlternateLiteral ID="AltrTitle" Text="添加投票" AlternateText="修改投票" runat="Server" />
            </td>
        </tr>
        <tr class="tdbg">
            <td class="tdbgleft" style="width: 300px;">
                <strong>投票主题：&nbsp;</strong>
            </td>
            <td align="left">
                <asp:TextBox ID="TxtTitle" runat="server" Columns="50" MaxLength="50"></asp:TextBox>
                <pe:RequiredFieldValidator ID="ValrTitle" ControlToValidate="TxtTitle" runat="server"
                    ErrorMessage="投票主题不能为空！" Display="Dynamic"></pe:RequiredFieldValidator>
            </td>
        </tr>
        <tr class="tdbg">
            <td class="tdbgleft">
                <strong>投票项：&nbsp;</strong>
            </td>
            <td align="left" valign="bottom">
                <table style="width: 100%;" cellpadding="2" cellspacing="1" id="Table1">
                    <tr class="tdbg">
                        <td>
                            <div id="returnVoteItem">
                                <table id='searchinnet_Table_Title' cellspacing='1' cellpadding='2' style='width: 500px;'>
                                    <tr class='title'>
                                        <td width='46%' align='center'>
                                            标题
                                        </td>
                                        <td width='12%' align='center'>
                                            投票数
                                        </td>
                                        <td width='12%' align='center'>
                                            关联ID
                                        </td>
                                        <td width='30%' align='center'>
                                            操作
                                        </td>
                                    </tr>
                                </table>
                                <table id='searchinnet_Table' cellspacing='1' cellpadding='2' style='width: 500px;'>
                                    <pe:ExtendedLiteral ID="VoteItemId" runat="server" HtmlEncode="false"></pe:ExtendedLiteral>
                                </table>
                            </div>
                        </td>
                    </tr>
                    <tr class="tdbg">
                        <td>
                            <pec:FloatingDialog Title="选择投票项" runat="server" ID="FldVoteItemId" LoadUrl="VoteTreeIndex.aspx?ReturnJsFunction=returnVoteItem"
                                Width="800" Height="400" ShowType="2" Name="选择投票项" /> &nbsp;
                            <span onclick="addVoteItem(-1,0,'')" style='cursor: pointer;'>
                                <img border='0' alt='增加项目' src='<%=BasePath%>Admin/Images/icon_add.gif' align="absmiddle"
                                    title='增加项目' /></span> &nbsp;<span onclick="removeLastRow()" style='cursor: pointer;'><img
                                        border='0' align="absmiddle" alt='删除项目' src='<%=BasePath%>Admin/Images/icon_del.gif'
                                        title='删除项目' /></span>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr class="tdbg">
            <td class="tdbgleft">
                <strong>是否允许复选：&nbsp;</strong>
            </td>
            <td align="left">
                <asp:CheckBox ID="ChkIsCheck" runat="server" />
            </td>
        </tr>
        <tr class="tdbg">
            <td class="tdbgleft">
                <strong>截止日期：&nbsp;</strong>
            </td>
            <td align="left">
                <pe:DatePicker ID="DpkEndTime" runat="server" Width="115px"></pe:DatePicker>
                <pe:RequiredFieldValidator ID="ValrDpkEndTime" ControlToValidate="DpkEndTime" runat="server"
                    ErrorMessage="截止日期不能为空" Display="Dynamic"></pe:RequiredFieldValidator>
            </td>
        </tr>
        <tr class="tdbg">
            <td class="tdbgleft">
                <strong>投票限制：&nbsp;</strong>
            </td>
            <td align="left">
                <asp:UpdatePanel ID="UpPurviewType" runat="server">
                    <ContentTemplate>
                        <asp:RadioButtonList ID="RadioVoteLimit" runat="server" RepeatColumns="3" AutoPostBack="true"
                            OnSelectedIndexChanged="RadPurviewType_CheckedChanged">
                            <asp:ListItem Value="0" Selected="True">不限制</asp:ListItem>
                            <asp:ListItem Value="1">IP限制</asp:ListItem>
                            <asp:ListItem Value="2">用户限制</asp:ListItem>
                        </asp:RadioButtonList>
                    </ContentTemplate>
                </asp:UpdatePanel>
                <asp:UpdatePanel ID="UpVoteLimit" runat="server">
                    <ContentTemplate>
                        <table width="100%" border="0" cellpadding="2" cellspacing="1">
                            <tr id="VoteLimit" runat="server">
                                <td class="tdbgleft" style="width: 150px;">
                                    同一<asp:Label ID="LblLimitType" runat="server" Text=""></asp:Label>允许投票次数：&nbsp;
                                </td>
                                <td align="left">
                                    <asp:TextBox ID="TxtAllowVoteNumber" runat="server" Columns="5" Text="1" MaxLength="6"></asp:TextBox>
                                    <pe:RequiredFieldValidator ID="ValrVoteNumber" ControlToValidate="TxtAllowVoteNumber"
                                        runat="server" ErrorMessage="允许投票次数不能为空" Display="Dynamic"></pe:RequiredFieldValidator>
                                    <asp:RegularExpressionValidator ID="ValgVoteNumber" runat="server" ControlToValidate="TxtAllowVoteNumber"
                                        ErrorMessage="只能输入正整数" ValidationExpression="^([0-9])(\d{0,})(\d{0,})$" Display="Dynamic" />
                                </td>
                            </tr>
                        </table>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr class="tdbg">
            <td class="tdbgleft">
                <strong>状态：&nbsp;</strong>
            </td>
            <td align="left">
                <asp:RadioButtonList ID="RadioState" runat="server" RepeatColumns="3">
                    <asp:ListItem Value="1" Selected="True">启用</asp:ListItem>
                    <asp:ListItem Value="0">禁用</asp:ListItem>
                </asp:RadioButtonList>
            </td>
        </tr>
        <tr class="tdbgbottom">
            <td colspan="2">
                <asp:Button ID="EBtnSubmit" Text="保存" UseSubmitBehavior="false" OnClick="EBtnSubmit_Click"
                    runat="server" OnClientClick="if(!voteSubmit()) return" />&nbsp;&nbsp;
                <input name="Cancel" type="button" class="inputbutton" id="Cancel" value="取消" onclick="Redirect('VoteManage.aspx')" />
            </td>
        </tr>
    </table>
    <asp:HiddenField ID="HdnTitle" runat="server" />
    <asp:HiddenField ID="HdnVoteItemId" runat="server" />
    <asp:HiddenField ID="HdnVoteTitle" runat="server" />
    <asp:HiddenField ID="HdnTotalNumber" runat="server" />

    <script language="javascript" type="text/javascript">
        //--------------------------------------------------------
        // 李彬原创 --- JS控制HTMLDOM表格行上下移动(兼容多浏览器)
        //--------------------------------------------------------
        //返回浏览器类型
        function getOs() {
            if (navigator.userAgent.indexOf("MSIE") > 0) return 1;
            if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) return 2;
            if (isSafari = navigator.userAgent.indexOf("Safari") > 0) return 3;
            if (isCamino = navigator.userAgent.indexOf("Camino") > 0) return 4;
            if (isMozilla = navigator.userAgent.indexOf("Gecko") > 0) return 5;
            return 0;
        }
        function cleanWhitespace(element) {
            //遍历element的子结点
            for (var i = 0; i < element.childNodes.length; i++) {
                var node = element.childNodes[i];
                //判断是否是空白文本结点，如果是，则删除该结点
                if (node.nodeType == 3 && !/S/.test(node.nodeValue))
                    node.parentNode.removeChild(node);
            }
        }
        //获得表格对象
        var _table = document.getElementById("searchinnet_Table");
        cleanWhitespace(_table);
        var _id = <%=m_voteNum.ToString()%>;
        //增加节点
        function insertRow(node) {
            addVoteItem(node.parentNode.parentNode.rowIndex + 1,0,"");
        }
        //删除节点
        function removeRow(node) {
            _table.deleteRow(node.parentNode.parentNode.rowIndex);
        }
        function removeLastRow() {
            _table.deleteRow(-1);
        }
        //解决火狐浏览器问题递归查找节点  节点、类型 （1 = previousSibling，2 = nextSibling)
        function getChildNode(node, type) {
            if (type == 1) {
                //判断是否是空白文本结点，则继续选择下一个节点
                return (node.nodeType == 3 && !/S/.test(node.nodeValue)) ? getChildNode(node.previousSibling, 1) : node.previousSibling;
            }
            else {
                return (node.nodeType == 3 && !/S/.test(node.nodeValue)) ? getChildNode(node.nextSibling, 2) : node.nextSibling;
            }
        }
        //使表格行上移，接收参数为链接对象
        function moveUp(_a) {
            //通过链接对象获取表格行的引用
            var _row = _a.parentNode.parentNode;
            //如果不是第一行，则与上一行交换顺序
            if (_row.previousSibling) {
                var _previousSibling = (getOs() > 1) ? getChildNode(_row, 1) : _row.previousSibling;
                swapNode(_row, _previousSibling);
            }
        }
        //使表格行下移，接收参数为链接对象
        function moveDown(_a) {
            //通过链接对象获取表格行的引用
            var _row = _a.parentNode.parentNode;
            //如果不是最后一行，则与下一行交换顺序
            if (_row.nextSibling) {
                var _nextSibling = (getOs() > 1) ? getChildNode(_row, 2) : _row.nextSibling;
                swapNode(_row, _nextSibling);
            }
        }
        //定义通用的函数交换两个结点的位置
        function swapNode(node1, node2) {
            if (node2 != null) {
                //获取父结点
                var _parent = node1.parentNode;
                //获取两个结点的相对位置
                var _t1 = node1.nextSibling;
                var _t2 = node2.nextSibling;
                //将node2插入到原来node1的位置
                if (_t1) _parent.insertBefore(node2, _t1);
                else _parent.appendChild(node2);
                //将node1插入到原来node2的位置
                if (_t2) _parent.insertBefore(node1, _t2);
                else _parent.appendChild(node1);
            }
        }
        //获取表格内所有ID、Title 值
        function voteSubmit() {
            var rows = document.getElementById("searchinnet_Table").getElementsByTagName('input');
            var idvalue = "";
            var titlevalue = "";
            var totalNumber = "";
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].name == "_id") {
                    idvalue = (idvalue == "") ? rows[i].value : idvalue + "," + rows[i].value;
                }
                if (rows[i].name == "_totalNumber") {
                    totalNumber = (totalNumber == "") ? rows[i].value : totalNumber + "," + rows[i].value;
                }
                if (rows[i].name == "_title") {
                    if (rows[i].value == ""){
                        alert("标题不能为空！");
                        rows[i].focus();
                        return false;
                    }
                    titlevalue = (titlevalue == "") ? rows[i].value : titlevalue + "," + rows[i].value;
                }
            }
  
            if (idvalue == '') {
                alert("投票项不能为空！");
                return false;
            }
            $get('<%=HdnVoteItemId.ClientID%>').value = idvalue;
            $get('<%=HdnVoteTitle.ClientID%>').value = titlevalue;
            $get('<%=HdnTotalNumber.ClientID%>').value = totalNumber;    
            return true;
        }
        //添加新投票选项
        function addVoteItem(nodeAddress,id,title){
            var a = _table.insertRow(nodeAddress);
            var b = a.insertCell(0);
            var c = a.insertCell(1);
            var d = a.insertCell(2);
            var e = a.insertCell(3);
                         
            b.innerHTML = "<input type='text' class='inputtext' name='_title'  value='" + title + "' id='title_id_" + _id + "' size='35' title='标题' onclick='selectVoteItme(this);'/>";
            b.width = "46%";
            c.align ="center"; 
            c.innerHTML = "<input type='text' class='inputtext' name='_totalNumber'  value='0' id='totalNumber_id_" + _id + "' size='4' style='text-align: right' title='设置投票数'/>";
            c.width = "12%";
            d.align = "right";
            d.innerHTML = "<span id='span_id_" + _id + "' title='信息ID'>" + id + "</span><input type='hidden' id='hdn_id_" + _id + "' name='_id' value='" + id + "'/>";
            d.width = "12%";
            var setVote = "<span onclick='moveUp(this)'  style='cursor: pointer;'><img border='0' id='img_id_Up" + _id + "' alt='上移项目' src='<%=BasePath%>Admin/Images/icon_up.gif' title='上移项目' align='absmiddle' /></span>";
            setVote += "&nbsp;<span onclick='moveDown(this)'  style='cursor: pointer;'><img border='0' id='img_id_Down" + _id + "' alt='下移项目' src='<%=BasePath%>Admin/Images/icon_down.gif' title='下移项目' align='absmiddle'/></span>";
            setVote += "&nbsp;<span onclick='insertRow(this)'  style='cursor: pointer;'><img border='0' id='img_id_Insert" + _id + "' alt='增加项目' src='<%=BasePath%>Admin/Images/icon_add.gif' title='增加项目' align='absmiddle'/></span>";
            setVote += "&nbsp;<span onclick='removeRow(this)'  style='cursor: pointer;'><img border='0' id='img_id_Remove" + _id + "' alt='删除项目' src='<%=BasePath%>Admin/Images/icon_del.gif' title='删除项目' align='absmiddle' /></span>";
       
            e.align ="center";
            e.width = "30%";
            e.innerHTML = setVote; 
            _id += 1;
        }
        //返回内容值
        function returnVoteItem(str) {
             var arrstritem;
            if (str.indexOf("$")) {
                var arrstr = str.split("$");
                for (i = 0; i < arrstr.length; i++) {
                    arrstritem = arrstr[i].split(",");
                    addVoteItem(-1,arrstritem[0],arrstritem[1]);
                }
            }else{
                arrstritem = str.split(",");
                addVoteItem(-1,arrstritem[0],arrstritem[1]);
            }
        }
        function selectVoteItme(voteItem){
            if (voteItem.value == "投票项1" || voteItem.value == "投票项2"){
                voteItem.value = "";
            }
        }
    </script>

</asp:Content>
