<!--
페이지 설명 : 제출기관지정시 보여지는 팝업 메뉴
수정작업 :  2013년 6월 25일
수정자 : jskim
-->

<%@ page contentType="text/html; charset=UTF-8"
         import="java.net.URLDecoder"
         import="kr.co.kihyun.lang.MString"
         import="kr.co.kihyun.lang.MInteger"
         import="kr.co.kihyun.lang.Encoder"
         import="kr.co.kihyun.beans.user.User"
         import="kr.co.kihyun.moumi.Moumi"%>
<%@ include file="/totsys/common/inc/sec/secure.inc.jsp"%>
<%
    String deptIDs = clearXSS(Encoder.toJava(MString.checkNull(request.getParameter("deptIDs"))), "");
    String deptTypes = clearXSS(Encoder.toJava(MString.checkNull(request.getParameter("deptTypes"))), "");
    //String deptNames = clearXSS(Encoder.toJava(MString.checkNull(request.getParameter("deptNames"))), "");
    String deptNames = clearXSS(URLDecoder.decode(MString.checkNull(request.getParameter("deptNames")), "UTF-8"),"");
    String formName = clearXSS(Encoder.toJava(MString.checkNull(request.getParameter("formName"))), "");

    int totCount = 0;

    String[] deptIDsList = deptIDs.split(",");
    if(deptIDs == null  || deptIDs == "") {
        totCount = 0;
    } else {
        totCount = deptIDsList.length;
    }

    //2014.11.07 : Add by KWON,HAN
    int deptCount = MInteger.parseInt(request.getParameter("deptCount"), 0);
    if(deptCount > 0) {
        totCount = deptCount;
    }
    //++++++++++++++++++++++++++++

%>
<!DOCTYPE html>
<html lang="ko">
	<head>
        <meta charset="UTF-8">
        <title>제출부서 선택</title>
        <script src="/totsys/common/js/jquery-1.12.4.min.js"></script>
        <script src="/totsys/common/js/jquery.cookie.js"></script>
		<script src="/totsys/common/js/jquery.hotkeys.js"></script>
		<script src="/totsys/common/js/jquery.jstree.js"></script>
        <link rel="stylesheet" href="/totsys/common/css/text.css" type="text/css">
        <link rel="stylesheet" href="/totsys/common/css/kecttep.css" type="text/css">
        <link rel="stylesheet" href="/totsys/common/css/SquareButtons.css" type="text/css">
        <link rel="stylesheet" href="/totsys/common/css/popup.css" type="text/css">
        <link rel="stylesheet" href="/totsys/common/css/basic.css" type="text/css">
        <script src="/totsys/common/js/org_search.js"></script>
        <script>
            var delstate = false;
            var userGroup = false; // 사용자정의 그룹인지 여부지정 변수

            var selid = ''; // 지금 선택한 부서
            var seltype = '';
            var selname = '';
            var idList = ''; // 선택되어진 부서 리스트
            var typeList = '';
            var nameList = '';
            var orgSearch = new OrgSearch(onNodeCallback);
            var focusedButton = null;
            $(document).ready(function() {
            	$("#search").focus();
            });

            // 부서 추가
            function setDepts() {
                //2014.11.07 : Update by KWON,HAN
                //document.list.idList.value = idList = "<%=deptIDs%>";
                //document.list.typeList.value = typeList = "<%=deptTypes%>";
                //document.list.nameList.value = nameList = "<%=deptNames%>";

                document.list.idList.value  =  idList   = opener.<%= formName%>.deptIDs.value;
                document.list.typeList.value = typeList = opener.<%= formName%>.deptTypes.value ;
                document.list.nameList.value = nameList = opener.<%= formName%>.deptNames.value;
                //===============================

                idList = idList + "";
                typeList = typeList + "";
                nameList = nameList + "";

                if (idList != "")
                    document.list.submit();

                /* @@@ test
                 document.getElementById('search').value = "강원도교육청";
                */
            }

            //추가 버튼
            function getDeptId() {
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#checkbtn").click();
                //top.document.leftFrame.location.reload(); //부서가 추가된후 체크박스들을 초기화 시켜준다.2012.09.28
            }

            function putSelId(id) {
                selid = id;
                deptConfirm2();
            }

            function putSelId2(sId, sDeptName, sOrganDeptName, sUpperDeptName) {
                //alert("id = " + sId + ", deptName = " + sDeptName + ", OrganDeptName = " + sOrganDeptName + ", UpperDeptName = " + sUpperDeptName)

                selid = sId;
                //selname = sOrganDeptName;
                //if(!sOrganDeptName.equals(sUpperDeptName)) {
                //    selname = selname + "-" + sUpperDeptName;
                //}
                //selname = selname + "-" + sDeptName;
                selname = sOrganDeptName + "|" + sUpperDeptName + "|" + sDeptName;

                deptConfirm2();
            }

            // 부서 추가
            function deptConfirm() {
                if (selid == '')
                    alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>");
                else if (seltype == "group")
                    modSelect();
                else {
                    id = selid;
                    name = selname;
                    type = seltype;
                    if (idList == '') {
                        document.list.idList.value = idList = id;
                        document.list.typeList.value = typeList = type;
                        idList = idList + "";
                        typeList = typeList + "";
                    } else {
                        var isAdd = listAdd(idList, id, typeList, type);
                        document.list.idList.value = idList;
                        document.list.typeList.value = typeList;
                    }
                    document.list.submit();
                }
            }

            function deptConfirm2(id) {
                if (selid == '')
                    alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>");
                else if (seltype == "group")
                    modSelect();
                else {
                    var id = selid;
                    //type=seltype;
                    var type = 'dept';
                    var name = selname;

                    if (idList == '') {
                        document.list.idList.value = idList = id;
                        document.list.typeList.value = typeList = type;
                        document.list.nameList.value = nameList = name;
                        idList = idList + "";
                        typeList = typeList + "";
                        nameList = nameList + "";
                    } else {
                        var isAdd = listAdd(idList, id, typeList, type);
                        document.list.idList.value = idList;
                        document.list.typeList.value = typeList;
                        if (isAdd) {
                            listAddn(nameList, name);
                        }
                        document.list.nameList.value = nameList;
                    }
                    //document.list.submit();
                }
            }

            function openerdeptConfirm() { //구성원으로 추가
                var gid = document.list.openeridList.value;
                var gname = document.list.openernameList.value;
                var gtype = document.list.openertypeList.value;

                var arrayID = gid.split(",");
                var arrayName = gname.split(",");
                var arrayType = gtype.split(",");

                var isChange = false;

                for (var i = 0; i < arrayID.length; i++) {
                    selid = arrayID[i];
                    selname = arrayName[i];
                    seltype = arrayType[i];

                    if (selid == '') {
                        alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>");
                    } else {
                        id = selid;
                        name = selname;
                        type = seltype;
                        if (idList == '') {
                            document.list.idList.value = idList = id;
                            document.list.typeList.value = typeList = type;
                            idList = idList + "";
                            typeList = typeList + "";

                        } else {
                            var isAdd = listAdd(idList, id, typeList, type);
                            document.list.idList.value = idList;
                            document.list.typeList.value = typeList;
                        }

                        isChange = true;
                    }
                }

                if (isChange) {
                    document.list.submit();
                }
            }

            function modSelect() {
                var height = screen.height;
                var width = screen.width;
                var leftpos = width / 2 - 235;
                var toppos = height / 2 - 235;

                if (seltype != "group") {
                    alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.groupSelect")%>");
                    return;
                }

                var urlname = "/totsys/common/web/report_dept/modifier.jsp?groupID=" + selid;
                post2 = window.open(urlname, "post2", "width=450, height=465, toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=" + leftpos + ",top=" + toppos);
                post2.focus();
            }

            function listAdd(idList1, idList2, tList, tItem) {
                var arrayID1 = idList1.split(",");
                var arrayType = tList.split(",");

                var isEquals = true;

                if (idList1 == '') {
                    idList = idList1;
                    typeList = tList;
                    return false;
                }
                for (var i = 0; i < arrayID1.length; i++) {
                    if (idList2 == arrayID1[i] && tItem == arrayType[i]) {
                        isEquals = false;
                        break;
                    }
                }
                if (isEquals) {
                    idList1 += "," + idList2;
                    tList += "," + tItem;
                }
                idList = idList1;
                typeList = tList;
                return isEquals;
            }

            //부서명을 더한다.
            function listAddn(nameList1, nameList2) {
                //var arrayName1 = nameList1.split(",");

                if (nameList1 == '') {
                    nameList = nameList1;
                    return;
                }

                nameList1 += "," + nameList2;
                nameList = nameList1;
                return;
            }


            // 삭제 버튼
            function deptDelete() {
                if (idList != '') {
                    deptFrame.movelist();
                    document.list.submit();
                }
            }

            function leftFrameDel(name, id, type) {
                id = lastStrDel(id, ",");
                type = lastStrDel(type, ",");
                name = lastStrDel(name, ",");

                if (idList == '') {
                    document.list.idList.value = idList = id;
                    document.list.typeList.value = typeList = type;
                    document.list.nameList.value = nameList = name;
                } else {
                    delList(id, type, name);
                    document.list.idList.value = idList;
                    document.list.typeList.value = typeList;
                    document.list.nameList.value = nameList;
                }

                document.list.submit();
            }

            function lastStrDel(str, delStr) {
                var lastStr = str.lastIndexOf(delStr);
                return str.substring(0, lastStr);
            }

            function delList(idList1, tyList1, naList1) {
                idList = idList1;
                typeList = tyList1;
                nameList = naList1;
            }

            // 부서 선택 시 유저 뽑아내기.
            function userList(id, name, type) {
                if (type != '') {
                    idAll = id;
                    selid = id;
                    selname = name;
                } else {
                    idAll = '';
                    selid = '';
                    selname = '';
                }
                seltype = type;
                idUpper = false;
                userGroup = false;
            }

            function groupList(id, name, type) {
                idAll = id;
                selid = id;
                selname = name;
                seltype = type;
                idUpper = false;
                userGroup = false;
            }

            function ProcKeyDown() {
                keyPressed = String.fromCharCode(window.event.keyCode);
                if ((keyPressed == '\r') || (keyPressed == '\n')) {
                    if (focusedButton == null) {
                        document.getElementById('nextButton').focus();
                    }
                }
            }

            function previous() {
                alert('prev');
                try {
                    var locationString = document.leftFrame.location + '';
                    if (locationString.indexOf('groupTree.jsp') !== -1)		// onNodeCallback does not works for group_list.jsp yet.
                        return;
                    orgSearch.previous(document.searchForm.keyword.value);
                } catch (e) {
                    alert(e);
                    return;
                }
            }

            function next() {
                alert('next');
                try {
                    var locationString = document.leftFrame.location + '';
                    if (locationString.indexOf('groupTree.jsp') !== -1)		// onNodeCallback does not works for group_list.jsp yet.
                        return;
                    orgSearch.next(document.searchForm.keyword.value);
                } catch (e) {
                    alert(e);
                    return;
                }
            }

            function onNodeCallback(node) {
                for (i = 0; i < node.pathList.length; i++)
                    document.leftFrame.openF(node.pathList[i]);
                document.leftFrame.selectItem(node.id, node.type);
                userList(node.id, node.name, node.type);
            }

            function searchWord() {
                var word = $('#search').val();
                word = $.trim(word);
                $('#search').val(word);
                if (word.length < 2) {
                    alert("검색어는 2글자 이상이어야 합니다.");
                    return;
                }
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#searchWord").text(word);
                $ff[0].contentWindow.$("#search").click();
            }

            //조직도에서 검색된 노드로 포커스를 이동시킨다.  strType : down, up
            function focusMove(strType) {
                //alert("strType = " + strType);
                var $ff = $("#leftFrame");
                $ff[0].contentWindow.$("#moveType").text(strType);      //포커스이동타입
                $ff[0].contentWindow.$("#focusMove").click();
            }

            //확인 버튼
            function deptComp() {
                if (<%=totCount%>==0 && document.list.idList.value == "") {
                    alert('<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptComp")%>');        //제출요청 부서를 추가 해 주세요
                    return false;

                } else {
                    //전체 삭제를 한 경우에 확인메세지를 띄운다.
                    if (document.list.idList.value == "") {
                        //정말로 삭제 하시겠습니까?
                        if (!confirm('<%=Moumi.getMessageBundle().getString("moumi.message.popup.deletion")%>')) {
                            return false;
                        }
                    }

                    //*** 선택된 부서는 부모창에 변수에 저장되지만 DB에는 저장하지 않는다. 즉, 문서생성 완료시에 DB에 저장된다.
                    opener.<%= formName%>.deptIDs.value = document.list.idList.value;
                    opener.<%= formName%>.deptTypes.value = document.list.typeList.value;
                    opener.<%= formName%>.deptNames.value = document.list.nameList.value;
//                    var deptTemp = document.list.idList.value.split(",");
//                    deptCounter = deptTemp.length;
//
//                    opener.<%= formName%>.deptCountText.value = deptCounter + "<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptCounter")%>";
//
                    var deptTemp = '';
                    var deptCounter = 0;

                    if (document.list.idList.value != null && document.list.idList.value != "") {
                        deptTemp = document.list.idList.value.split(",");
                    }

                    if (deptTemp != null && deptTemp != '') {
                        deptCounter = deptTemp.length;
                    }

                    //2014.11.07 : Add by KWON,HAN
                    opener.<%= formName%>.deptCount.value = deptCounter;
                    //+++++++++++++++++++++++++++++++

                    if(deptCounter <= 0) {
                        opener.<%= formName%>.deptCountText.value = '<%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.submitDevision")%>';           //제출기관을 선택해주세요.
                    } else {
                        opener.<%= formName%>.deptCountText.value = deptCounter + '<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptCounter")%>';  //개의 부서를 선택하였습니다.
                    }
                    window.close();
                }
            }

            //취소 버튼
            function deptCancel() {
                window.close();
            }
        </script>
    </head>
    <body onload="setDepts();">
        <form name="list" method="post" action="/servlet/kr.co.kihyun.beans.user.HttpDeptList" target="deptFrame">
            <input type="hidden" name="idList">
            <input type="hidden" name="seltype">
            <input type="hidden" name="typeList">
            <input type="hidden" name="nameList">
            <input type="hidden" name="openeridList">
            <input type="hidden" name="openernameList">
            <input type="hidden" name="openertypeList">
        </form>
                    <!--타이틀 //-->
                    <div class="title">
                    	<div><%=Moumi.getMessageBundle().getString("moumi.message.button_large.submitDept")%></div>
                    </div>
                    <!--타이틀 //-->
                    <div class="disFlex lv1">
                    	<div class="deptListSide">
	                    	<img alt="말머리" src="/totsys/common/images/ico_3.gif">
	                    	<b><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.deptMap")%></b>
                    	</div>
                    	<div class="deptListCenter"></div>
                    	<div class="deptListSide"><img alt="말머리" src="/totsys/common/images/ico_3.gif"><b>제출기관(부서)<!--%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.submitDept")%--></b></div>
                    </div>
                    <div class="disFlex ml_5">
                    	<!-- 분류 선택 아이프레임 -->
                    	<iframe src="iframe/new_section_list3.html" width="300" height="21"></iframe>
                    </div>
                    <div class="disFlex">
                    	<div class="deptListSide">
                    		<!-- 부서 선택 아이프레임 -->
                    		<iframe id="leftFrame" name="leftFrame" src="./iframe/groupCommon.jsp"></iframe>
                    	</div>
                    	<div class="deptListCenter center deptPt">
                            <div>
	                    		<!--추가-->
                            	<a href="javascript:getDeptId();"><img alt="추가" src="./images/new_dept_add.gif"/></a>
                            </div>
                            <div class="mt_5">
	                        	<!--삭제-->
	                            <a href="javascript:deptDelete();"><img alt="삭제" src="./images/new_dept_del.gif"/></a>
                            </div>
                    	</div>
                    	<div class="deptListSide">
                    		<!-- 선택된 부서 리스트 아이프레임 -->
                    		<iframe name="deptFrame" src="./iframe/selected_list.jsp"></iframe>
                    	</div>
                    </div>
                    <table>
                        <tr>
                            <td> 검 색 &nbsp;</td>
                            <td><input id="search" name="search" type="text" class="inputtxt" value="" size="35" onkeyDown="if(event.keyCode === 13) { searchWord(); }" style="ime-mode:active;">&nbsp;</td>
                            <td><a href="#" onclick="searchWord();"><img alt="찾기" src="/totsys/common/images/bt_search_inquiry2.gif"></a>&nbsp;</td>
                            <td><a href="#" onclick="focusMove('down');"><img alt="다음" src="/totsys/common/images/bt_down.jpg"></a>&nbsp;</td>
                            <td><a href="#" onclick="focusMove('up');"><img alt="이전" src="/totsys/common/images/bt_up.jpg"></a></td>
                            <td></td>
                        </tr>
                    </table>
                    <!-- 확인, 취소 버튼 -->
                    <div class="center mt_10" style="border-top: 1px solid #8ea6d6">
                    	<div class="mt_10">
	                    	<a href="#" onclick="deptComp()"><img alt="확인" src="/totsys/common/images/bt_ok.gif"></a>
	                    	<a href="#" onClick="deptCancel()"><img alt="취소" class="ml_5" src="/totsys/common/images/bt_cancle.gif"></a>
                    	</div>
					</div>
    </body>
</HTML>
