<%@ page contentType="text/html; charset=UTF-8"
	import="java.net.URLEncoder"
	import="kr.co.kihyun.beans.user.HttpSSOLogin"
	import="kr.co.kihyun.text.html.ServletUtil"
	import="kr.co.kihyun.lang.MString"
	import="kr.co.kihyun.moumi.Moumi"
    import="kr.co.kihyun.lang.MLong"
	import="javax.jdo.PersistenceManager"
    import="javax.jdo.Transaction"
	import="kr.co.kihyun.beans.entity.util.*"
	import="kr.co.kihyun.beans.entity.*"
    import="kr.co.kihyun.beans.user.User"
    import="kr.co.kihyun.lang.Encoder"%>
<%@ 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 formName = clearXSS(Encoder.toJava(MString.checkNull(request.getParameter("formName"))),"");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="/totsys/common/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/totsys/common/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/totsys/common/js/jquery.hotkeys.js"></script>
<script type="text/javascript" src="/totsys/common/js/jquery.jstree.js"></script>
<TITLE>제출부서 선택</TITLE>
<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">
<style>
a:link {
	font-style: none;
	line-height: normal;
	color: #004080;
	text-decoration: none;
}

a:visited {
	font-style: none;
	line-height: normal;
	color: #3366CC;
	text-decoration: none;
}

a:active {
	font-style: none;
	line-height: normal;
	color: #535353;
	text-decoration: none;
}

a:hover {
	font-style: none;
	line-height: normal;
	color: #FF6600;;
	text-decoration: none;
}

img {
	border: none;
}
</style>
<script src="/totsys/common/js/org_search.js"></script>
<script Language="javascript">
	var tstate = false;
	var delstate = false;
	var userGroup = false; // 사용자정의 그룹인지 여부지정 변수

	var selid=''; // 지금 선택한 부서
	var selname='';
	var seltype='';
	var idList = ''; // 선택되어진 부서 리스트
	var typeList = '';
	var orgSearch = new OrgSearch(onNodeCallback);
	var focusedButton = null;

	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 setDepts(){
		document.list.idList.value = idList = "<%=deptIDs%>";
		idList=idList+"";
		document.list.typeList.value = typeList = "<%=deptTypes%>";

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

    }

	// 부서 추가
	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;
				idList=idList+"";
				document.list.typeList.value=typeList=type;
				typeList=typeList+"";
			}
			else
			{
				var isAdd=listAdd(idList,id,typeList,type);
				document.list.idList.value=idList;
				document.list.typeList.value=typeList;
			}
			document.list.submit();
		}
	}

	function getDeptId()
	{
		var $ff=$("#leftFrame");
		$ff[0].contentWindow.$("#checkbtn").click();
	}

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

	function deptConfirm2(id)
	{
		if(selid=='' ) alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptConfirm")%>");
    else if(seltype=="group") modSelect();
		else
		{
			id=selid;
			name=selname;
			//type=seltype;
			type='dept';
			if(idList=='')
			{
				document.list.idList.value=idList=id;
				idList=idList+"";
				document.list.typeList.value=typeList=type;
				typeList=typeList+"";
			}
			else
			{
				var isAdd=listAdd(idList,id,typeList,type);
				document.list.idList.value=idList;
				document.list.typeList.value=typeList;
			}
			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;
					idList=idList+"";
					document.list.typeList.value = typeList = type;
					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 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 deptDelete(){
		if(idList != '' ){
			deptFrame.movelist();
			document.list.submit();
		}
	}

	function leftFrameDel(name, id, type){
		name = lastStrDel(name, ",");
		id = lastStrDel(id, ",");
		type = lastStrDel(type, ",");
		if(idList == ''){
			document.list.idList.value = idList = id;
			document.list.typeList.value = typeList = type;

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

		tstate = true;

		document.list.submit();
		return;
	}

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

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

	// 부서 선택 시 유저 뽑아내기.
	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 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 deptComp(){
		if(document.list.idList.value=="")
		{
			alert("<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptComp")%>");
			return;
		}
		else{
			opener.<%= formName %>.deptIDs.value = document.list.idList.value;
			opener.<%= formName %>.deptTypes.value = document.list.typeList.value;
			var deptTemp = document.list.idList.value.split(",");
			var deptCounter = deptTemp.length;
			opener.<%= formName %>.deptCountText.value = deptCounter + "<%=Moumi.getMessageBundle().getString("moumi.message.popup.deptCounter")%>";
			window.close();
		}
	}

	function deptCancel(){
		window.close();
	}

	function searchWord()
	{
		var word=$('#search').val();
		if(word.length<2) { alert("검색어는 2글자 이상이어야 합니다."); return; }
		var $ff=$("#leftFrame");
		$ff[0].contentWindow.$("#searchWord").text(word);
		$ff[0].contentWindow.$("#search").click();
	}
</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="typeList">
    <input type="hidden" name="openeridList">
    <input type="hidden" name="openernameList">
    <input type="hidden" name="openertypeList">
</form>
<table width="100%" border="0" cellpadding="0" cellspacing="0" bordercolor="#00FFFF"><tr>
    <td >
      <table width="100%" border="0" cellpadding="0" cellspacing="0"><tr>
          <td bgcolor="728ab5" style="padding: 10 20 10 20"><strong><font color="#FFFFFF">제출기관지정</font></strong></td>
  </tr></table>
  <!--타이틀 //-->
	<!-- //본문 -->
<table  width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top:8px">
	<!-- 제목 -->
	<tr>
		<td colspan="3">
		<table width="100%" height="26" border="0" cellpadding="0" cellspacing="0" style="padding-top:10px ; padding-bottom:10px">
              <tr>
                <td width="300" ><img src="/totsys/common/images/ico_3.gif"  border="0" align="absmiddle">&nbsp;<b><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.deptMap")%></b></td>
                <td  align="left" width="100" ></td>
                <td width="470"><img src="/totsys/common/images/ico_3.gif"  border="0" align="absmiddle">&nbsp;<b><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.submitDept")%></b></td>
              </tr>
        </table>
	</td>
	</tr>
	<!-- 분류,부서 선택 -->
	<tr>
		<td width="300" valign="top"><!-- 분류 선택 아이프레임 -->
		<table width="300" height="21" border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td>
                   <iframe src="iframe/new_section_list_doc.html" width="300" height="21" frameborder="0" scrolling="no"></iframe>
                </td>
			</tr>
		</table>
		<!-- 부서 선택 아이프레임 기본 디폴트 페이지 선택 탭-->
		<table border="1px" bordercolor="#e3e3e3" class="kecttep_tdmenu" width="300" cellpadding="0" cellspacing="0">
			<tr>
				<td><iframe id="leftFrame" name="leftFrame" src="./iframe/newTreeApprove.jsp" width="300" height="360" frameborder="0"></iframe></td>
			</tr>
<!--			<tr class="toolbar">
				<td align="left" style="padding: 2px 3px;">
				<form name="searchForm" onsubmit="javascript: return false;">
				<table border="1" cellspacing="0" cellpadding="0">
					<tr>
						<td style="font-size: 12px; font-family: 돋움, serif;"><%=Moumi.getMessageBundle().getString("moumi.message.tot_doc.search")%>:</td>
						<td><input name="keyword" size="20" class="inputtxt"
							onkeydown="ProcKeyDown();" /></td>
						<td><a id="previousButton" class="simplebutton" href="#"
							onclick="previous();"
							onfocus="focusedButton = document.getElementById('previousButton');"
							onblur="focusedButton = null;"><span class="previous"><%=Moumi.getMessageBundle().getString("moumi.message.button_small.previous")%></span></a></td>
						<td><a id="nextButton" class="simplebutton" href="#"
							onclick="next();"
							onfocus="focusedButton = document.getElementById('nextButton');"
							onblur="focusedButton = null;"><span class="next"><%=Moumi.getMessageBundle().getString("moumi.message.button_small.next")%></span></a></td>
					</tr>
				</table>
				</form>
				</td>
			</tr>   -->
		</table>
		</td>
		<!-- 추가,삭제 -->
		<td width="80" align="center">
		<table border="0" cellspacing="0" cellpadding="0" width="50">
			<!--추가-->
			<tr>
				<td height="35" style="padding-left: 10px; padding-right: 10px"><a
					href="javascript:getDeptId();"><img
					src="./images/new_dept_add.gif"/></a></td>
			</tr>
			<!--삭제-->
			<tr>
				<td height="35" style="padding-left: 10px; padding-right: 10px"><a
					href="javascript:deptDelete();"><img
					src="./images/new_dept_del.gif"/></a></td>
			</tr>
			<!--구성원-->
			<tr>
			</tr>
		</table>
		</td>
		<!-- 선택 된 아이프레임 -->
		<td width="470" style="padding-top: 20px;">
		<table border="1px" bordercolor="#e3e3e3" class="kecttep_tdmenu"
			width="470" cellpadding="0" cellspacing="0">
			<tr>
				<td><iframe name="deptFrame" src="./iframe/selected_list.jsp" width="470" height="365" frameborder="0"></iframe></td>
			</tr>
		</table>
		</td>
	</tr>
</table>
<!-- 본문 //-->
<!--
<table border=0 cellspacing=0 cellpadding=0 width="100%" style="margin-top:8px">
	<tr><td height="1" bgcolor="728ab5"></td></tr>
	<tr>
		<td height="40" align="left" >
			<table width="200" height="0" border="1" cellpadding="0" cellspacing="0" >
			<tr>
				<td align="left"> 검색:<input name="docName" type="text" class="inputtxt" value="" size="55">
				</td>
				<td align="left">
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>
//-->
			<table height="0" border="0" cellpadding="0" cellspacing="0" >
			<tr>
							<td align="left"> 검 색 &nbsp;&nbsp;</td>
							<td><input id="search" name="search" type="text" class="inputtxt" value="" size="35"> </td>
							<td>&nbsp;&nbsp;<a href="#" onclick="searchWord()"><img src="/totsys/common/images/bt_search_inquiry.gif" border="0" align="absmiddle"></a></td>
				<td align="left">
				</td>
			</tr>
		</table>


	<!-- 확인, 취소 버튼 -->
<table border=0 cellspacing=0 cellpadding=0 width="100%" style="margin-top:8px">
	<tr><td height="1" bgcolor="728ab5"></td></tr>

	<tr>

		<td height="40" align="center" >
			<table width="200" height="0" border="0" cellpadding="0" cellspacing="0" >
			<tr>
				<td width="100" align="center"><a href="#" onclick="deptComp()"><img src="/totsys/common/images/bt_ok.gif" border="0" align="absmiddle"></a>
				</td>
				<td width="100" align="left"><a href="#" onClick="deptCancel()"><img src="/totsys/common/images/bt_cancle.gif" border="0" align="absmiddle"></a>
				</td>
			</tr>
		</table>
		</td>
	</tr>
</table>


  </tr></table>






</BODY>
</HTML>
