﻿// Key:Value 형태의 값을 가진 문자열로부터 Key값에 해당하는 Value값을 반환한다.
function GetKeyValue(arrayString, key, splitChar) {
	var arrayItems = arrayString.split(',');
	for (var i = 0; i < arrayItems.length; i++) {
		var colItem		= arrayItems[i];
		var colName		= (colItem.split(splitChar))[0];
		var colValue	= (colItem.split(splitChar))[1];
		if (colName == key)
			return colValue;
	}
		
	return '';
}

// SELECT 태그의 하위아이템을 제거한다.
function ClearSelectItems(selectObject) {
	if (selectObject != null) {
		if (selectObject.length > 0) {
			for (var i = selectObject.length; i >= 0; i--) {
				selectObject.remove(i);
			}			
		}
	}
}

// SELECT 태그의 하위아이템을 제거한다.
function ClearSelectItemsById(selectId) {
	var selectObject	= document.getElementById(selectId);
	if (selectObject != null) {
		ClearSelectItems(selectObject);
	}
}

// ChildControl의 Value를 초기화한다.
function ResetChildControls(parent) {
	if (parent.children != null) {
		for (var i = 0; i < parent.children.length; i++) {
			ResetChildControls(parent.children[i]);
			var child	= parent.children[i];
			
			if (child.readonly) {
				continue;
			}
			
			var childTagName	= child.tagName;
			if (childTagName == 'INPUT' && (child.type == 'checkbox' || child.type == 'radio')) {
				child.checked	= false;
			}
			else if (childTagName == 'INPUT' && child.type != 'button' && child.type != 'reset') {
				child.value = '';
			}
			else if (childTagName == 'SELECT') {
				child.value	= '';
			}
			else if (childTagName == 'TEXTAREA') {
				child.value	= '';
			}
		}
	}
}

// ChildControl의 Value를 초기화한다.
function ResetChildControlsById(parentID) {
	var parent	= document.getElementById(parentID);
	if (parent != null) {
		ResetChildControls(parent);
	}
}

// ChildControl의 disabled 속성을 제어한다.
function SetEnableChildControls(parent, status) {
	if (parent.children != null) {
		for (var i = 0; i < parent.children.length; i++) {
			SetEnableChildControls(parent.children[i], status);
			var child	= parent.children[i];
			var childTagName	= child.tagName;
			if (childTagName == 'INPUT' || childTagName == 'SELECT' || childTagName == 'TEXTAREA') {
				child.disabled	= status;
			}
		}
	}
}

// ChildControl의 disabled 속성을 제어한다.
function SetEnableChildControlsById(parentID, status) {
	var parent	= document.getElementById(parentID);
	if (parent != null) {
		SetEnableChildControls(parent, status);
	}
}

// Select 태그의 아이템을 바인딩한다.
function BindSelectItems(selectObject, dataSource) {
	for (var i = 0; i < dataSource.length; i++) {
		var listItem = dataSource[i].split(':');
		selectObject.add(new Option(listItem[0], listItem[1]));
	}
}

// Select 태그의 아이템을 바인딩한다.
function BindSelectItemsById(selectId, dataSource) {
	var selectObject = document.getElementById(selectId);
	if (selectObject != null) {
		BindSelectItems(selectObject, dataSource.split(';'));
	}
}

// 모달 팝업을 생성한다. (공통모달)
function openModalDialog(wndID, title, width, height, url) {

    openProtoTypeMWnd(wndID, title, width, height, url);

}

// 모달 팝업(ProtoType Alphacube)
function openProtoTypeMWnd(wndID, title, width, height, url) {
    // Window with scrollable text
    var optionValue = {className: 'alphacube',  width: width, height: height, zIndex: 100, resizable: false, title: title, showEffect:Element.show, hideEffect: Element.hide};

    win = new Window(wndID, optionValue);

	win.setURL(url);
	win.setDestroyOnClose();
	win.showCenter(true);	
}

// 모달 팝업을 종료한다.
function closeModalWindow() {
    Windows.close(Windows.focusedWindow.getId());
}

// 선택한 체크박스의 속성을 체크박스리스트에 적용시킨다.
function selectCheckBox(sender, cbxName) {
	var checkList = document.getElementsByName(cbxName);
	var checked = sender.checked;

	if (checkList != null) {
		for (var i = 0; i < checkList.length; i++) {
			checkList[i].checked = checked;
		}
	}
}

// 글자 수 제한
function textareacheck(aro_name,ari_max) // 텍스트박스, 제한글자수
{
   var ls_str     = aro_name.value; // 이벤트가 일어난 컨트롤의 value 값
   var li_str_len = ls_str.length;  // 전체길이

   // 변수초기화
   var li_max      = ari_max; // 제한할 글자수 크기
   var li_byte     = 0;  // 한글일경우는 2 그밗에는 1을 더함
   var li_len      = 0;  // substring하기 위해서 사용
   var ls_one_char = ""; // 한글자씩 검사한다
   var ls_str2     = ""; // 글자수를 초과하면 제한할수 글자전까지만 보여준다.

   for(i=0; i< li_str_len; i++)
   {
      // 한글자추출
      ls_one_char = ls_str.charAt(i);

      // 한글이면 2를 더한다.
      if (escape(ls_one_char).length > 4)
      {
         li_byte += 2;
      }
      // 그밗의 경우는 1을 더한다.
      else
      {
         li_byte++;
      }

      // 전체 크기가 li_max를 넘지않으면
      if(li_byte <= li_max)
      {
         li_len = i + 1;
      }
   }
   
   // 전체길이를 초과하면
   if(li_byte > li_max)
   {
      alert("내용은 한글 " + li_max/2 + "자 영문 " + li_max + "자 까지만 허용됩니다.");
      ls_str2 = ls_str.substr(0, li_len);
      aro_name.value = ls_str2;
   }
   aro_name.focus();   
}