﻿
	<!-- 위치값을 찾기 위한 함수 모음 -->
	function getAnchorPosition(anchorname)
	{
		// This function will return an Object with x and y properties
		var useWindow = false;
		var coordinates = new Object();
		var x = 0, y = 0;
		// Browser capability sniffing
		var use_gebi = false, use_css = false, use_layers = false;
		if (document.getElementById) { use_gebi = true; }
		else if (document.all) { use_css = true; }
		else if (document.layers) { use_layers = true; }
		// Logic to find position
		if (use_gebi && document.all)
		{
			x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
			y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
		else if (use_gebi)
		{
			var o = document.getElementById(anchorname);
			x = AnchorPosition_getPageOffsetLeft(o);
			y = AnchorPosition_getPageOffsetTop(o);
		}
		else if (use_css)
		{
			x = AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
			y = AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
		else if (use_layers)
		{
			var found = 0;
			for (var i = 0; i < document.anchors.length; i++)
			{
				if (document.anchors[i].name == anchorname) { found = 1; break; }
			}
			if (found == 0)
			{
				coordinates.x = 0; coordinates.y = 0; return coordinates;
			}
			x = document.anchors[i].x;
			y = document.anchors[i].y;
		}
		else
		{
			coordinates.x = 0; coordinates.y = 0; return coordinates;
		}
		coordinates.x = x;
		coordinates.y = y;
		return coordinates;
	}

	// getAnchorWindowPosition(anchorname)
	//   This function returns an object having .x and .y properties which are the coordinates
	//   of the named anchor, relative to the window
	function getAnchorWindowPosition(anchorname)
	{
		var coordinates = getAnchorPosition(anchorname);
		var x = 0;
		var y = 0;
		if (document.getElementById)
		{
			if (isNaN(window.screenX))
			{
				x = coordinates.x - document.body.scrollLeft + window.screenLeft;
				y = coordinates.y - document.body.scrollTop + window.screenTop;
			}
			else
			{
				x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
				y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
			}
		}
		else if (document.all)
		{
			x = coordinates.x - document.body.scrollLeft + window.screenLeft;
			y = coordinates.y - document.body.scrollTop + window.screenTop;
		}
		else if (document.layers)
		{
			x = coordinates.x + window.screenX + (window.outerWidth - window.innerWidth) - window.pageXOffset;
			y = coordinates.y + window.screenY + (window.outerHeight - 24 - window.innerHeight) - window.pageYOffset;
		}
		coordinates.x = x;
		coordinates.y = y;
		return coordinates;
	}

	// Functions for IE to get position of an object
	function AnchorPosition_getPageOffsetLeft(el)
	{
		var ol = el.offsetLeft;
		while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
		return ol;
	}
	function AnchorPosition_getWindowOffsetLeft(el)
	{
		return AnchorPosition_getPageOffsetLeft(el) - document.body.scrollLeft;
	}
	function AnchorPosition_getPageOffsetTop(el)
	{
		var ot = el.offsetTop;
		while ((el = el.offsetParent) != null) { ot += el.offsetTop; }
		return ot;
	}
	function AnchorPosition_getWindowOffsetTop(el)
	{
		return AnchorPosition_getPageOffsetTop(el) - document.body.scrollTop;	
	}
	
	
	// 방향 이동함수
	function _WinSelectPopUpOnKeyUp()
	{
		var keycode = window.event.keyCode ;
		if (_WinSelectPopUp.isOpen)
		{
			if (window.event.altKey) _onWindDDLClose();
			
			if (keycode == 38)
			{
				// 위쪽 방향키를 눌렀을 때
				if (_WindDDL.nowMoveCount > 0)
				{
					_WindDDLChangeDiv(_WindDDL.nowMoveCount, _WindDDL.nowMoveCount - 1);
					_WindDDL.nowMoveCount = _WindDDL.nowMoveCount - 1;
					_WinSelectPopUp.document.body.scrollTop = _WindDDL.nowMoveCount * 20;
				}					
				return false;
			} else if (keycode == 40)
			{
				if (_WindDDL.nowMoveCount < _WindDDLItems[_WindDDL.NowControl.id].Items.length - 1)
				{
					_WindDDLChangeDiv(_WindDDL.nowMoveCount, _WindDDL.nowMoveCount + 1);
					_WindDDL.nowMoveCount = _WindDDL.nowMoveCount + 1;
					_WinSelectPopUp.document.body.scrollTop = _WindDDL.nowMoveCount * 20;
				}
				return false;
				
			} else if (keycode == 13)
			{
				_onWindDDLClick(_WindDDLItems[_WindDDL.NowControl.id], _WindDDL.nowMoveCount);
				return false;
			}
		}
	}
	
	// 리스트박스의 오브젝트를 등록 한다.
	var _WindDDL = new Object();
	_WindDDL.isOpen = false;
	_WindDDL.NowControl = null;
	_WindDDL.keyCount = 0;
	_WindDDL.nowMoveCount = null;
	
	// 리스트의 내용을 담을 배열의 객체
	var _WindDDLItems = new Array();
	
	// 리트 팝업
	var _WinSelectPopUp = window.createPopup();
	window.document.onkeydown = _WinSelectPopUpOnKeyUp;
	
	// Css를 등록 한다.
	_WinSelectPopUp.document.createStyleSheet("/Common/Resource/Css/style.css");
	// 팝업창을 설정 한다.
	_WinSelectPopUp.document.body.className = "WinSelectBoxBody";
	_WinSelectPopUp.document.body.style.border = "solid 1px #9b9b9b";
	
	// 내용을 설정 한다.
	var _WinSelectBoxList = _WinSelectPopUp.document.createElement("div");
	_WinSelectBoxList.className = "WinSelectBoxList";
	
	// ViewType설정
	var _ViewType = "Search";

	// div형식을 바꾼다.
	function _WindDDLChangeDiv(beforKeyCount, afterKeyCount)
	{
		var breakCont = 0;
		
		for (i = 0; i < _WinSelectBoxList.childNodes.length; i++)
		{
			if (_WinSelectBoxList.childNodes[i].keyCount == beforKeyCount.toString())
			{	
				_WindDDLOnMouseOut(_WinSelectBoxList.childNodes[i], _WindDDLItems[_WindDDL.NowControl.id].Items[i].IsSelected);				
			}

			if (_WinSelectBoxList.childNodes[i].keyCount == afterKeyCount.toString())
			{
				_WindDDLOnMouseOver(_WinSelectBoxList.childNodes[i], _WindDDLItems[_WindDDL.NowControl.id].Items[i].IsSelected);
			}

			if (breakCont == 2)
			{
				break;
			}
		}
	}
	
	// 문서의 스크롤링을 불가능하도록 설정
	function disableScroll()
	{
		window.execScript("document.onmousewheel = function(){return _WinSelectPopUp.isOpen ? false : true ;}");		
	}
	
	// 팝업 오픈 함수
	function _onWindDDLSet(thisObject, HeightList, isEnabled, textAlign, ViewType)
	{	
		_ViewType = ViewType;
		if(ViewType == "Search")
		{
			_WinSelectPopUp.document.body.className = "WinSelectBoxBody";
			_WinSelectPopUp.document.body.style.border = "solid 1px #9b9b9b";
			_WinSelectBoxList.className = "WinSelectBoxList";
		}
		else if(ViewType == "Contents")
		{
			_WinSelectPopUp.document.body.className = "WinSelectBoxBody_01";
			_WinSelectPopUp.document.body.style.border = "solid 1px #9b9b9b";
			_WinSelectBoxList.className = "WinSelectBoxList_01";
		}
		
		if (isEnabled)
		{
			var obj = document.getElementById(thisObject);
			
			if (_WindDDL.NowControl == null || _WindDDL.NowControl.id != obj.id)
			{
				_WindDDL.NowControl = obj;
				_WindDDL.nowMoveCount = null;
				_WindDDL.keyCount = 0;
				_WindDDL.isOpen = false;
			}
			_WindDDL.isOpen = true;
			
			// 리스트 내용을 만든다.
			var listCount = _WindDDLMakeItem(_WindDDL.NowControl.id, ViewType);

			// 팝업에 리스트 내용을 ADD시킨다.
			_WinSelectPopUp.document.body.style.overflow = "auto";
				
			_WinSelectPopUp.document.body.appendChild(_WinSelectBoxList);
			_WinSelectBoxList.style.textAlign = textAlign ;
			
			var bx = 0;
			var bt = 0;
			var bb = 0;
			try
			{
				bx = parseInt(obj.parentElement.currentStyle.borderLeftWidth);
				if(bx == null || bx.toString() == 'NaN' || bx == '')
				{
					bx = 0 ;
				}
			}
			catch(e){}
			
			try
			{
				var bt = parseInt(obj.parentElement.currentStyle.borderTopWidth);				
				if(bt == null || bt.toString() == 'NaN' || bt == '')
				{
					bt = 0 ;
				}
			}
			catch(e){}
			
			try
			{
				var bb = parseInt(obj.parentElement.currentStyle.borderBottomWidth);				
				if(bb == null || bb.toString() == 'NaN' || bb == '')
				{
					bb = 0 ;
				}
			}
			catch(e){}
			
			var x = parseInt(AnchorPosition_getWindowOffsetLeft(obj)) + bx ;
			var y = parseInt(AnchorPosition_getWindowOffsetTop(obj)) + parseInt(obj.offsetHeight) - bb + bt ;
			
			var w = obj.offsetWidth - 1 + bx;
			var h = HeightList;
			if (HeightList == null || HeightList == "")
			{
				h = 200;
			}

			var contentsHeight = ( listCount * 20) + 12 ;
			if (h > contentsHeight)
			{
				h = contentsHeight;
				if (contentsHeight < 32)
				{
					h = 32;
				}
			}
			
			_WinSelectPopUp.show(x, y, w, h, document.body);
			_WindDDL.nowMoveCount = _WindDDL.keyCount;			
			
			disableScroll();
		}		
	}
	
	
	
	// 리스트 내용을 닫는다.
	function _onWindDDLClose()
	{	
		if(_WinSelectPopUp.isOpen)
		{
			_WinSelectPopUp.hide();
		}		
		_WindDDL.isOpen = false;
	}
	
	// 리스트 내용을 만든다.
	function _WindDDLMakeItem(idName, ViewType)
	{
		_ViewType = ViewType;
		_WinSelectBoxList.innerHTML = "";
		
		var _List = _WindDDLItems[idName];
		
		if (_List != null)
		{	
			for (i = 0; i < _List.Items.length; i++)
			{	
				_WinSelectBoxList.appendChild(_WindGetDDLDiv(_List, i, ViewType))
			}
		}
		return _List.Items.length;
	}
	
	// 아이템을 만든다.
	function _WindGetDDLDiv(inList, inCount, ViewType)
	{	
		_ViewType = ViewType;
		
		var retVal = _WinSelectPopUp.document.createElement("div");		
		if (inList.Items[inCount].IsSelected)
		{
			if(ViewType == "Search")
			{
				retVal.className = "WindDDLCssChoice";
			}
			else
			{
				retVal.className = "WindDDLCssChoice_01";
			}
			retVal.onmouseover = function() { _WindDDLOnMouseOver(retVal, true, inCount); };
			retVal.onmouseout = function() { _WindDDLOnMouseOut(retVal, true); };
			_WindDDL.keyCount = inCount;
		}
		else
		{
			if(ViewType == "Search")
			{
				retVal.className = "WindDDLCssNone";
			}
			else
			{
				retVal.className = "WindDDLCssNone_01";
			}
			retVal.onmouseover = function() { _WindDDLOnMouseOver(retVal, false, inCount); };
			retVal.onmouseout = function() { _WindDDLOnMouseOut(retVal, false); };			
		}
		retVal.innerText = "  " + inList.Items[inCount].Text + " " ;
		retVal.onclick = function() { _onWindDDLClick(inList, inCount); };
		retVal.keyCount = inCount;		
		return retVal;
		
	}

	function _WindDDLOnMouseOver(obj, isChoice, inKeyCount)
	{	
		if (isChoice)
		{
			if(_ViewType == "Search")
			{
				obj.style.backgroundColor = "#FFFFFF";
				obj.style.color = "#000";
			}
			else
			{
				obj.style.backgroundColor = "#FFFFFF";
				obj.style.color = "#fd9800";
			}
		}
		else
		{
			if(_ViewType == "Search")
			{
				obj.style.backgroundColor = "#FFFFFF";
				obj.style.color = "#000";
			}
			else
			{
				obj.style.backgroundColor = "#FFFFFF";
				obj.style.color = "#fd9800";
			}
		}

		if (inKeyCount != null)
		{
			_WindDDL.nowMoveCount = inKeyCount;
		}
	}

	function _WindDDLOnMouseOut(obj, isChoice)
	{
		if (isChoice)
		{	
			if(_ViewType == "Search")
			{
				obj.style.backgroundColor = "#99CD63";
				obj.style.color = "#FFFFFF";
			}
			else
			{
				obj.style.backgroundColor = "#FFFFFF";
				obj.style.color = "#fd9800";
			}
		}
		else
		{
			if(_ViewType == "Search")
			{
				obj.style.backgroundColor = "#99CD63";
				obj.style.color = "#000";
			}
			else
			{
				obj.style.backgroundColor = "#FFFFFF";
				obj.style.color = "#000";
			}
		}
	}
	
	// 리스트 내용을 선택 하면 처리 하는 함수
	function _onWindDDLClick(inList, inCount)
	{
		// 리스트에 값을 설정 한다.	
		inList.SelectedText = inList.Items[inCount].Text;
		inList.SelectedValue = inList.Items[inCount].Value;
		
		inList.IsSelected = true;
		inList.keyCount = inCount;				
		for (i = 0; i < inList.Items.length; i++)
		{
			inList.Items[i].IsSelected = false;
		}
		
		inList.Items[inCount].IsSelected = true;

		_WindDDL.NowControl.selectedValue = inList.SelectedValue;
		_WindDDL.NowControl.selectedText = inList.SelectedText;
		_WindDDL.keyCount = inCount;
		
		var SelectedValueId = document.getElementById(_WindDDL.NowControl.id + '_SelectedValue');
		var SelectedTextId = document.getElementById(_WindDDL.NowControl.id + '_SelectedText');		
		SelectedValueId.value = inList.SelectedValue;
		SelectedTextId.value = inList.SelectedText;
		
		// 선택된 내용을 화면에 보여준다.
		_WindDDLSetText(_WindDDL.NowControl, inList.SelectedText);
		
		_WindDDL.isOpen = false;
		
		_onWindDDLClose();
		
		if (_WindDDL.NowControl.onEvent != "")
		{
			eval(_WindDDL.NowControl.onEvent);
		}
		
	}

	// 선택된 내용을 화면에 보여준다.
	function _WindDDLSetText(inControls, inValue)
	{	
		for (i = 0; i < inControls.childNodes.length; i++)
		{	
			if (inControls.childNodes[i].id == "_WindDDLText")
			{
				inControls.childNodes[i].innerHTML = inValue;
				return true;
			}
			else
			{
				if (inControls.childNodes[i].hasChildNodes)
				{
					if (_WindDDLSetText(inControls.childNodes[i], inValue))
					{
						return true;
					}
				}
			}
		}
	}	
	
	// 리스트의 배열 내용을 저장 한다.
	function _onSetDDLItems(inArrText, inArrValue, inDiffParam, inSelectValue, inStyle)
	{
		// 리스트 내용을 만든다.
		var _WindDDLItem = new Object();
		_WindDDLItem.Items = new Array();
		_WindDDLItem.SelectedValue = "";
		_WindDDLItem.SelectedText = "";
		_WindDDLItem.IsSelected = false;
		_WindDDLItem.style = inStyle ;
		_WindDDLItem.keyCount = 0;

		if (inArrValue != null && inArrText.length > 0)
		{
			if (inArrValue.indexOf(inDiffParam) > -1)
			{
				var textArr = inArrText.split(inDiffParam);
				var valueArr = inArrValue.split(inDiffParam);

				for (i = 0; i < valueArr.length; i++)
				{
					var _Item = new Object();

					_Item.Text = textArr[i];
					_Item.Value = valueArr[i];
					_Item.IsSelected = false;
					_Item.keyCount = i;
					
					if (inSelectValue != null)
					{
						if (inSelectValue.toString() == valueArr[i])
						{
							_WindDDLItem.SelectedText = textArr[i];
							_WindDDLItem.SelectedValue = valueArr[i];
							_WindDDLItem.IsSelected = true;
							_WindDDLItem.keyCount = i;							
							_Item.IsSelected = true;
						}
					}
					_WindDDLItem.Items[i] = _Item;
				}
			}
			else
			{
				var _Item = new Object();
				_Item.Text = inArrText;
				_Item.Value = inArrValue;
				_Item.isSelected = true;
				
				_WindDDLItem.SelectedText = inArrText;
				_WindDDLItem.SelectedValue = inArrValue;
				_WindDDLItem.IsSelected = true;
				_WindDDLItem.Items[0] = _Item;
				_WindDDLItem.keyCount = 0; 			
			}

			if (!_WindDDLItem.IsSelected)
			{
				_WindDDLItem.SelectedText = _WindDDLItem.Items[0].Text;
				_WindDDLItem.SelectedValue = _WindDDLItem.Items[0].Value;
				_WindDDLItem.IsSelected = true;
				_WindDDLItem.Items[0].IsSelected = true;
				_WindDDLItem.keyCount = 0; 
			}
		}
		
		return _WindDDLItem
	}
