<%@ Page Language="c#" Inherits="Web.User.selectBigClass" CodeBehind="selectBigClass.aspx.cs" %>

<!DOCTYPE html>
<!--[if lt IE 9]><html class="lte-ie8"><![endif]-->
<!--[if gt IE 8]><!-->
<html>
<!--<![endif]-->
<head>
    <title>抄送栏目选择</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!--#include file="../Css/head.htm"-->

    <script src="../Js/jQuery.js" type="text/javascript"></script>
    <script  type="text/javascript" >
		    var arrays;
		    var array1;
		    // 加入已选
		    
			function Add() {
			    var selectFrom = document.getElementById("d_b1");
			    var selectTo = document.getElementById("d_b2");
				if (selectFrom.selectedIndex<0) {
					alert("请选择待选栏目！");
					return;
				}

				for(var loop=0;loop<selectFrom.options.length;loop++)
				{
				if(selectFrom.options[loop].selected==true)
					{                        
						var optionName = selectFrom.options[loop].text ;
						var optionValue = selectFrom.options[loop].value ;                        
						var flag = false;
							for(var i=0;i<selectTo.options.length;i++)
							{
								if(optionValue == selectTo.options[i].value)
									{
											flag = true;
											break;
									}
							}
			                        
						if (flag == true)
						{
							alert("“"+selectFrom.options[loop].text+"”已被选择!");
						}
							else 
							{
							selectTo.add(new Option(optionName,optionValue));
							}  
					} 
				}                       
				
			}
			function AddAll() 
			{

			    var selectFrom = document.getElementById('d_b1');
				var selectTo = document.getElementById('d_b2');

				for(var loop=0;loop<selectFrom.options.length;loop++)
				{			                           
						var optionName = selectFrom.options[loop].text ;
						var optionValue = selectFrom.options[loop].value ;                        
						var flag = false;
							for(var i=0;i<selectTo.options.length;i++)
							{
								if(optionValue == selectTo.options[i].value)
									{
											flag = true;
											break;
									}
							}
			                        
						if (flag == false)
						{
			              
							selectTo.add(new Option(optionName,optionValue));
							}  
			           
				}            
			}

			// 从已选中删除
			function Del() {
			    var selectTo = document.getElementById('d_b2');
				var nIndex = selectTo.selectedIndex;
				var nLen = selectTo.length;
				if (nLen<1) return;
				if (nIndex<0) {
					alert("请选择已选栏目！");
					return;
				}
				
				for(var loop=0;loop<selectTo.options.length;loop++)
				{
					if(selectTo.options[loop].selected){                        
               			selectTo.options[loop] = null; loop--;
					}
			     }
			}
			function DelAll() {
			    var selectTo = document.getElementById('d_b2');
				var nLen = selectTo.length;
				if (nLen<1) return;
				for(var loop=nLen;loop>=0;loop--)
				{ 
               		selectTo.options[loop] = null;
               	}			  
			}

			// 排序：向上移动
			function Up() {
				var sel=document.getElementById("d_b2");
				var nIndex = sel.selectedIndex;
				var nLen = sel.length;
				if ((nLen<1)||(nIndex==0)) return;
				if (nIndex<0) {
					alert("请选择一个要移动的已选栏目！");
					return;
				}
				var sValue=sel.options[nIndex].value;
				var sHTML=sel.options[nIndex].innerHTML;
				sel.options[nIndex].value=sel.options[nIndex-1].value;
				sel.options[nIndex].innerHTML=sel.options[nIndex-1].innerHTML;
				sel.options[nIndex-1].value=sValue;
				sel.options[nIndex-1].innerHTML=sHTML;
				sel.selectedIndex=nIndex-1;
			}

			// 排序：向下移动
			function Down() {
				var sel=document.getElementById("d_b2");
				var nIndex = sel.selectedIndex;
				var nLen = sel.length;
				if ((nLen<1)||(nIndex==nLen-1)) return;
				if (nIndex<0) {
					alert("请选择一个要移动的已选栏目！");
					return;
				}
				var sValue=sel.options[nIndex].value;
				var sHTML=sel.options[nIndex].innerHTML;
				sel.options[nIndex].value=sel.options[nIndex+1].value;
				sel.options[nIndex].innerHTML=sel.options[nIndex+1].innerHTML;
				sel.options[nIndex+1].value=sValue;
				sel.options[nIndex+1].innerHTML=sHTML;
				sel.selectedIndex=nIndex+1;
			}

			// 提交处理
			function checkform() {
			    var selectFrom=document.getElementById("d_b2");
			    var optionName="";
			    var optionValue="";
			   for(var loop=0;loop<selectFrom.options.length;loop++)
				{ 
					optionName = optionName + "|" + selectFrom.options[loop].text ;
					optionValue = optionValue + "|" + selectFrom.options[loop].value ;
				}  
				parent.checkform(optionName,optionValue);          	
				//parent.window.dialogArguments.TextBox1.value = optionName;
				//parent.window.dialogArguments.TextBox2.value = optionValue;      	
		    	parent.$.fancybox.close();
			}
			function getDuplicatedBigClass()
			{
			    var itemName = parent.document.getElementById("hiddenName").value;
			    var itemValue = parent.document.getElementById("hiddenValue").value;
			    InitSelectedBigClass(itemName, itemValue);
			}
			function InitSelectedBigClass(itemName, itemValue)
			{
			    //alert(itemName);alert(itemValue);
			    var arrayName = itemName.split('|') ; 
			    var arrayValue = itemValue.split('|') ;
			    var selectedFrom = document.getElementById('d_b2');
			    for(var loop=0; loop<arrayName.length; loop++)
			    {
			        if(arrayName[loop]!="")
			        {
			            selectedFrom.add(new Option(arrayName[loop], arrayValue[loop]));
			        }
			    }
			}

			function GetBigClassByS() {
			    var searchName = document.getElementById("searchText").value;
			    var lstStaffs = document.getElementById("d_b1");
			    if (arrays == null) {
			        arrays = new Array();
			        for (var j = 0; j < lstStaffs.options.length; j++) {			        
			            strStaffs = lstStaffs.options[j].text;
			            strValue = lstStaffs.options[j].value;
			            var optValue = { name: "", values: "" };
			            optValue.name = strStaffs;
			            optValue.values = strValue;
			            arrays.push(optValue);
			        }
			    }

			    if (array1 == null || array1.length == 0) {
			        array1 = new Array();
			        lstStaffs.length = 0;
			        if (arrays.length > 0) {
			            for (var m = 0; m < arrays.length; m++) {
			                lstStaffs.options.add(new Option(arrays[m].name, arrays[m].values));
			            }
			        }
			        for (var i = 0; i < lstStaffs.options.length; i++) {
			            strStaffs = lstStaffs.options[i].text;
			            strValue = lstStaffs.options[i].value;
			            if (strStaffs.indexOf(searchName) >= 0 && searchName != "") {			                
			                var optValues = { name: "", values: "" };
			                optValues.name = strStaffs;
			                optValues.values = strValue;
			                array1.push(optValues);
			            }

			        }
			    }
			    if (searchName != "") {
			        lstStaffs.length = 0;
			        if (array1.length > 0) {
			            for (var n = 0; n < array1.length; n++) {
			                lstStaffs.options.add(new Option(array1[n].name, array1[n].values));
			            }
			            array1.length = 0;
			        }
			    }
			    if (searchName == "") {
			        lstStaffs.length = 0;
			        for (var m = 0; m < arrays.length; m++) {
			            lstStaffs.options.add(new Option(arrays[m].name, arrays[m].values));
			        }

			    }

			}


			function GetBigClassByTemplate() {
			    var templateId = document.getElementById("<%=ddlTemplate.ClientID %>").value;
			    var searchName = document.getElementById("searchText").value;
			    var userId=document.getElementById("txt_userId").value;
			    var templates=document.getElementById("txt_templates").value;
			    var bigclassId=document.getElementById("txt_bigclassId").value;
			    //Web.Service.LoginService.GetBigClassBySX(templateId, searchName, templates, userId, bigclassId, onGetBigClassByTemplateSuccess, onError, null);
			    $.ajax({
                    type: "post",
                    url: "selectBigClass.aspx/GetBigClassBySX",
                    data: "{'templateId':'"+templateId+"','searchName':'"+searchName+"','templates':'"+templates+"','userId':'"+userId+"','bigclassId':'"+bigclassId+"'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data) {
                        //return data;
                        onGetBigClassByTemplateSuccess(data);
                    },
                    error: function(err) {
                        alert(err);
                    }
                });
			}

			function onGetBigClassByTemplateSuccess(result) {
			    var db = document.getElementById("<%=d_b1.ClientID %>");
			    db.length = 0;
			    if (result.length != 0) {
			        for (var i = 0; i < result.length; i++) {
			            var item = result[i];
			            db.options[i] = new Option(item.Name, item.PkId);
			        }
			    }
			}

    </script>

</head>
<body>
    <form id="myform" method="post" runat="server">
    <div class="edit-func-box func-box">
        <div class="bd">
            <table class="edit-table">
                <tr>
                    <td width="15%">
                    </td>
                    <td colspan="2">
                        根据模板筛选：<p><asp:DropDownList ID="ddlTemplate" runat="server" DataTextField="name" DataValueField="pkId" onchange="javascript:GetBigClassByTemplate();" >
                        </asp:DropDownList></p>
                    </td>
                    <td colspan="2">
                        栏目搜索：<p><input type="text" id="searchText" maxlength="20" onchange="javascript:GetBigClassByS();" onpropertychange="javascript:GetBigClassByS();" /></p>
                        <div style="display: none;">
                            <asp:TextBox ID="txt_templates" runat="server"></asp:TextBox>
                            <asp:TextBox ID="txt_userId" runat="server"></asp:TextBox>
                            <asp:TextBox ID="txt_bigclassId" runat="server"></asp:TextBox>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <div align="center">
                            可选栏目</div>
                    </td>
                    <td>
                    </td>
                    <td>
                        <div align="center">
                            已选栏目</div>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td width="15%">
                    </td>
                    <td>
                        <asp:ListBox ID="d_b1" runat="server" SelectionMode="Multiple" Height="225px" Width="168px">
                        </asp:ListBox>
                    </td>
                    <td width="15%" align="center">
                        <p>
                            <input onclick="Add()" type="button" value="→" name="b1" class="btn btn-mini btn-info" />
                        </p><br />
                        <p>
                            <input onclick="Del()" type="button" value="←" name="b1" class="btn btn-mini btn-info" />
                        </p><br />
                        <p>
                            <input onclick="AddAll()" type="button" value=">>" name="b1" class="btn btn-mini btn-info" />
                        </p><br />
                        <p>
                            <input onclick="DelAll()" type="button" value="<<" name="b1" class="btn btn-mini btn-info" />
                        </p>
                    </td>
                    <td>
                        <asp:ListBox ID="d_b2" runat="server" SelectionMode="Multiple" Height="225px" Width="168px">
                        </asp:ListBox>
                    </td>
                    <td width="15%">
                        <input onclick="Up()" type="button" value="↑" name="b3" class="btn btn-mini btn-info" />
                        <br />
                        <br />
                        <input onclick="Down()" type="button" value="↓" name="b4" class="btn btn-mini btn-info" />
                    </td>
                </tr>
            </table>
        </div>
        <div class="ft">
            <div class="action-field text-center">
                <input type="button" class="btn btn-sm btn-primary" value="确 定" onclick="checkform();" />&nbsp;&nbsp;
                <input type="button" class="btn btn-sm btn-primary" value="取 消" name="btnClose" onclick="parent.$.fancybox.close();" />
            </div>
        </div>
    </div>
    <input id="d_name" type="hidden" name="d_button" runat="server" />
    <input id="d_value" type="hidden" name="d_button" runat="server" />

    <script>getDuplicatedBigClass();</script>

    </form>
</body>
</html>
