<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dictionarySelect.aspx.cs"
    Inherits="Czou2012.UI.Manager.Dictionary.dictionarySelect" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>字典管理</title>
    <link type="text/css" rel="stylesheet" href="../Styles/style.css" />
    <link type="text/css" rel="stylesheet" href="../Styles/pagination.css" />
    <link type="text/css" rel="stylesheet" href="../Styles/popdiv.css" />
    <link rel="stylesheet" type="text/css" href="../Scripts/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../Scripts/themes/icon.css" />
    <link type="text/css" rel="stylesheet" href="../Scripts/Tips/tip-yellowsimple/tip-yellowsimple.css" />

    <script type="text/javascript" src="../Scripts/jquery.min.js"></script>
    <script type="text/javascript" src="../Scripts/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../Scripts/jquery.url.js"></script>
    <script type="text/javascript" src="../Scripts/jquery.validate.field.js"></script>
    <script type="text/javascript">
        //文本框改变事件--------------------------------------------------------------/
        function btnConfirm() {
            var nOpenObjID = "";  //打开弹出层的对象ID
            var nSourceCode = "";
            var nSourceName = "";
            var nReturnCodeID = $.getUrlVar("ReturnCodeID");
            var nReturnNameID = $.getUrlVar("ReturnNameID");
            var nReturnSelectType = $.getUrlVar("ReturnSelectType");

            var nReturnCode = ""; //返回代码
            var nReturnName = ""; //返回名称
            nReturnCode = $("#txtSelectCode").val();
            nReturnName = $("#txtSelectName").val();

            if (nReturnCodeID == "") {
                if (nReturnNameID == "") {
                }
                else {
                    $("#" + nReturnNameID, parent.document.body).val(nReturnName);

                }
                nOpenObjID = nReturnNameID;
            }
            else {
                if (nReturnNameID == "") {
                    $("#" + nReturnCodeID, parent.document.body).val(nReturnCode);
                    nOpenObjID = nReturnCodeID;
                }
                else {
                    $("#" + nReturnNameID, parent.document.body).val(nReturnName);
                    $("#" + nReturnCodeID, parent.document.body).val(nReturnCode);
                    nOpenObjID = nReturnNameID;
                }
            }

            var objExist = "";
            if (nOpenObjID != "") {
                objExist = jQuery("#" + nOpenObjID, parent.document.body);
            }
            // alert(nOpenObjID);

            if (objExist.length > 0) {
                objExist.focus();
                parent.$.popup.close(); //通过这个对象关闭层
            }
            else
            { $('#lblInfo').html("对象有误！"); }
        }

        function addSel(obj, objValue) {
            var nReturnCode = ""; //返回代码
            var nReturnName = ""; //返回名称
            var ifExist = false;

            var nReturnSelectType = $.getUrlVar("ReturnSelectType");
            var nDicLayers = $.getUrlVar("DicLayers");
          

            //如果为单选类型，先清除原来的值
            if (nReturnSelectType == "single") {
                $("#btnConfirm").attr("style", "display:none");
                $("#txtSelectCode").val("");
                $("#txtSelectName").val("");
            }

            var checkValue = obj.innerHTML;
            var scBox = $(obj).parent().parent().attr("id");
            if ($.trim($("#txtSelectCode").val()) == "") {

                //如果为二级单选类型，则显示上次类型
                var nLast = $("#txtSelectFirstName").val();
                if (nDicLayers == "2" && nReturnSelectType == "single" && nLast != checkValue) {                    
                    checkValue = nLast + ">" + checkValue;
                }

                nReturnCode = objValue;
                nReturnName = checkValue;
                if (nDicLayers == "1") {
                    $(".checkedBox").append("<dd><a href='#' onclick='delSel(this,\"" + objValue + "\",\"" + scBox + "\")'>" + checkValue + "</a></dd>");
                }
                else {
                    $(".checkedBox").append("<dd><a href='#' onclick='delSel(this,\"" + objValue + "\",\"" + scBox + "\")'>" + checkValue + "</a></dd>");
                 }
            }
            else {
                
                var nTempCode = $.trim($("#txtSelectCode").val()).split(",");
                var nTempName = $.trim($("#txtSelectName").val()).split(",");
                $.each(nTempCode, function (n) {
                    if (nTempCode[n] == objValue) {
                        ifExist = true;
                    }
                })
                if (ifExist) {
                    nReturnCode = $("#txtSelectCode").val();
                    nReturnName = $("#txtSelectName").val();
                }
                else {
                    nReturnCode = $("#txtSelectCode").val() + "," + objValue;
                    nReturnName = $("#txtSelectName").val() + "," + checkValue;
                    $(".checkedBox").append("<dd><a href='#' onclick='delSel(this,\"" + objValue + "\",\"" + scBox + "\")'>" + checkValue + "</a></dd>");
                }
            }

            $("#txtSelectCode").val(nReturnCode);
            $("#txtSelectName").val(nReturnName);
            
            //如果为单选，则激活确认函数
            if (nReturnSelectType == "single") {
                btnConfirm();
            }
            //$(obj).parent().remove();//移除选择源对象
        }


        function delSel(obj, objValue, parentId) {
            var nReturnCode = ""; //返回代码
            var nReturnName = ""; //返回名称
            var ifExist = false;

            var checkValue = obj.innerHTML;
            //获取新的值
            var nTempCode = $.trim($("#txtSelectCode").val()).split(",");
            var nTempName = $.trim($("#txtSelectName").val()).split(",");
            $.each(nTempCode, function (n) {
                if (nTempCode[n] != objValue) {
                    nReturnCode = nReturnCode + nTempCode[n] + ",";
                    nReturnName = nReturnName + nTempName[n] + ",";
                }
            })

            //去除最后的","
            var nLastChar = "";
            nLastChar = nReturnCode.substring(nReturnCode.length - 1, nReturnCode.length);
            if (nLastChar == ",") { nReturnCode = nReturnCode.substring(0, nReturnCode.length - 1); }

            nLastChar = nReturnName.substring(nReturnName.length - 1, nReturnName.length);
            if (nLastChar == ",") { nReturnName = nReturnName.substring(0, nReturnName.length - 1); }
            //填充新选择值
            $("#txtSelectCode").val(nReturnCode);
            $("#txtSelectName").val(nReturnName);
            //不再向原选择处添加此选项
            //$("#" + parentId + " div").before("<dd><a href='#' onclick='addSel(this,\"" + objValue + "\")'>" + checkValue + "</a></dd>");
            $(obj).parent().remove();
        }

        function subSel(obj, objCode, objName) {
            $("#txtSelectFirstName").val(objName);
            $.ajax({
                type: "GET",
                url: "dictionarySelect.aspx?type=sublayer&DicCode=" + objCode + "&number=" + Math.random(),
                beforeSend: function () {
                    $("#item1").html("<dd>loading...</dd>");
                }, //执行ajax前执行loading函数.直到success 
                success: function (msg) {
                    $("#item1").html(msg);
                } //成功时执行Response函数 
            });
            
        }

        $(function () {
           // $(".screenItem dd:last").attr("style", "color:orange");
            var nSourceCode = "";
            var nSourceName = "";
            var nReturnCodeID = $.getUrlVar("ReturnCodeID");
            var nReturnNameID = $.getUrlVar("ReturnNameID");
            var nReturnSelectType = $.getUrlVar("ReturnSelectType");

            if (nReturnCodeID != "") {
                nSourceCode = $("#" + nReturnCodeID, parent.document.body).val();
            }
            if (nReturnNameID != "") {
                nSourceName = $("#" + nReturnNameID, parent.document.body).val();
            }

            $("#txtSelectCode").val(nSourceCode);
            $("#txtSelectName").val(nSourceName);


            //如果为单选，清空原有值           
            if (nReturnSelectType == "single") {
                $("#btnConfirm").attr("style", "display:none");
            }

            //获取新的值
            var nTempCode = $.trim($("#txtSelectCode").val()).split(",");
            var nTempName = $.trim($("#txtSelectName").val()).split(",");
            if ($.trim($("#txtSelectCode").val()) != "") {
                if ($.trim($("#txtSelectName").val()) != "") {
                    $.each(nTempCode, function (n) {
                        $(".checkedBox").append("<dd><a href='#' onclick='delSel(this,\"" + nTempCode[n] + "\",\"item1\")'>" + nTempName[n] + "</a></dd>");
                    })
                }
                else {
                    $.each(nTempCode, function (n) {
                        $(".checkedBox").append("<dd><a href='#' onclick='delSel(this,\"" + nTempCode[n] + "\",\"item1\")'>" + nTempCode[n] + "</a></dd>");
                    })
                }
            }
            else {
                //如果代码为空，则取值名称
                if ($.trim($("#txtSelectName").val()) != "") {
                    $.each(nTempCode, function (n) {
                        $(".checkedBox").append("<dd><a href='#' onclick='delSel(this,\"" + nTempName[n] + "\",\"item1\")'>" + nTempName[n] + "</a></dd>");
                    })
                }
            }

            $("#lblClear").click(function () {
                $("#txtSelectCode").val("");
                $("#txtSelectName").val("");
                btnConfirm();
            })
            $("#lblClose").click(function () {
                parent.$.popup.close(); //通过这个对象关闭层
            })

        });
	
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="screen">
        <dl class="screenTitle" id="sTitle">
            <dt>选择<asp:Label ID="lblDicType" runat="server"></asp:Label>：<span id="lblInfo" style="color: #ff0000;"
                    class="lblInfo"></span> </dt>
            <dd id="lblClose">
                <a href="#">[关闭]</a></dd><dd id="lblClear"><a href="#">[清除]</a></dd>
        </dl>
        <asp:Literal  ID="LblContent" runat="server" ></asp:Literal>       
       
       <table style="clear:both;float:left;width:100%;border:0px;padding-top:4px;"><tr>
       <td style="width:60px;padding-left:4px;">您已选择：</td>
       <td><dl class="checkedBox"></dl></td>
       <td style="width:100px;"><div style="clear: both; width: 100%; text-align: center;">
            <a id="btnConfirm" class="easyui-linkbutton" href="javascript:btnConfirm();" style="margin-top: 2px;"
                tabindex="5">确认</a>
            <input type="hidden" id="txtSelectCode" value="" size="20" />
            <input type="hidden" id="txtSelectName" value="" size="20" />
            <input type="hidden" id="txtSelectFirstName" value="" size="20" />
        </div></td>
        </tr></table>
        
        
    </div>
    </form>
</body>
</html>
