/*------------------------------------------------------------------------------
 * 1. 파일명: form.js
 * 2. 설  명: Form 요소 처리와 관련된 함수를 정의한다.
 * 3. 의존성: 없음
 * 4. 작성일: 2006.10.11.
 -----------------------------------------------------------------------------*/


/**
 * Select 객체의 현재 선택되어 있는 항목의 value를 돌려 준다.
 *
 * @param selectObj 선택된 값을 얻으려는 Select 객체 참조 혹은 ID.
 */
function getSelectedValue(selectObj) {
	var selectObj = ref(selectObj);
	var selectedIndex = selectObj.selectedIndex;

	return selectObj.options[selectedIndex].value;
}


/**
 * Select 객체에 배열에 있는 값으로 Option 객체를
 * 새로 생성한다. 이 때 주어진 insertIndex 값부터
 * 채워 나가므로 insertIndex 값 이전 항목들은
 * 살아 남는다.
 *
 * @param selectObj Option 항목을 세팅할 Select 객체 참조 혹은 ID.
 * @param optionArr Option 항목을 담고 있는 2차원 배열 객체.
 *                  이 배열 요소의 각 배열 중 index가 0인건 value로
 *                  1인 건 text로 사용한다.
 * @param includeValue text 값에 value에 해당하는 값도 포함하는지 여부. 기본값은 false.
 * @param insertIndex Option을 추가할 Index.
 *                    그 전에 있던 이 Index 이하 항목들은 모두 삭제된다.
 */
function setOption(selectObj, optionArr, insertIndex, includeValue) {
	if ( typeof(selectObj) == "string" ) selectObj = document.getElementById(selectObj);

	if ( insertIndex == undefined ) insertIndex = 0;

	if ( includeValue == undefined ) includeValue = false;

	selectObj.options.length = insertIndex ;

	if ( optionArr == undefined || optionArr == null || optionArr.length == 0 ) return;

	for ( var i = 0; i < optionArr.length; i++ ) {
		var text = (includeValue ? "(" + optionArr[i][0] + ") " : "") + optionArr[i][1];

		selectObj.options[i + 1] = new Option(text, optionArr[i][0]);
	}
}


/**
 * Select 객체 내에서 주어진 값이 있는지 찾아
 * Index를 돌려 준다. 없으면 -1을 돌려 준다.
 *
 * @param selectObj Select 객체 참조 혹은 ID.
 * @param value     Index를 찾으려고 하는 값.
 *
 * @return selectObj의 Option 항목 중 value 속성이 주어진 값인 항목의 Index.
 *         없으면 -1.
 */
function indexOfSelect(selectObj, value) {
	if ( typeof(selectObj) == "string" ) selectObj = document.getElementById(selectObj);

	var index = -1;

	for ( var i = 0; i < selectObj.length; i++ ) {
		if ( selectObj.options[i].value == value ) {
			index = i;
			break;
		}
	}

	return index;
}


/**
 * Select의 option중 value 항목의 값을 가지는
 * option이 선택된 것으로 설정한다.
 *
 * @param selectObj Select 객체 참조 혹은 ID.
 * @param value     선택된 것으로 하려는 값.
 */
function setSelect(selectObj, value) {
	if ( typeof(selectObj) == "string" ) selectObj = document.getElementById(selectObj);

	if ( value == undefined ) value = "";

	selectObj.selectedIndex = indexOfSelect(selectObj, value);
}


/**
 * Radio 버튼에서 주어진 값의 index를 돌려 준다.
 * 값이 없으면 -1을 돌려 준다.
 */
function indexByValue(checkedObj, value) {
	var length = objLength(checkedObj);

	for ( var i = 0; i < length; i++ ) {
		if ( ref(checkedObj, i).value == value ) {
			return i;
		}
	}

	return -1;
}


/**
 * Radio나 CheckBox의 체크된 값을 돌려 준다.
 * Radio의 경우 체크되는 값이 하나이므로 그 값을,
 * CheckBox의 경우 여러 개일 수 있으므로 배열로 돌려 준다.
 */
function getCheckedValue(checkedObj) {
	if ( typeof(checkedObj) == "string" ) checkedObj = document.getElementByName(checkedObj);

	var res = null;

	var length = objLength(checkedObj);

	var type = ref(checkedObj, 0).type;

	/* Radio인지 CheckBox인지에 따라 처리를 한다. */
	if ( "radio" == type ) {
		/* Radio인 경우. */
		for ( var i = 0; i < length; i++ ) {
			if ( ref(checkedObj, i).checked ) {
				res = ref(checkedObj, i).value;
				break;
			}
		}
	}
	else if ( "checkbox" == type ) {
		/* CheckBox인 경우. */
		var array = new Array();
		for ( var i = 0; i < length; i++ ) {
			if ( ref(checkedObj, i).checked ) {
				array.push(ref(checkedObj, i).value);
			}
		}

		res = array;
	}

	return res;
}


/**
 * Radio나 CheckBox의 체크된 Index를 돌려 준다.
 * Radio의 경우 체크되는 값이 하나이므로 그 값을,
 * CheckBox의 경우 여러 개일 수 있으므로 배열로 돌려 준다.
 */
function getCheckedIndex(checkedObj) {
	if ( typeof(checkedObj) == "string" ) checkedObj = document.getElementByName(checkedObj);

	var res = null;

	var length = objLength(checkedObj);

	var type = ref(checkedObj, 0).type;

	/* Radio인지 CheckBox인지에 따라 처리를 한다. */
	if ( "radio" == type ) {
		/* Radio인 경우. */
		for ( var i = 0; i < length; i++ ) {
			if ( ref(checkedObj, i).checked ) {
				res = i;
				break;
			}
		}
	}
	else if ( "checkbox" == type ) {
		/* CheckBox인 경우. */
		var array = new Array();
		for ( var i = 0; i < length; i++ ) {
			if ( ref(checkedObj, i).checked ) {
				array.push(i);
			}
		}

		res = array;
	}

	return res;
}




var ENABLED_BGCOLOR  = "";
var DISABLED_BGCOLOR = "#F7F7F7";

/**
 * 주어진 객체 Disable 여부를 disabled 값으로 설정하고
 * 배경색을 조절한다.
 *
 * @param obj Disabled 여부를 세팅할 객체 참조 혹은 ID.
 * @param disabled Disabled 여부. Disable 시키려면 true, Enable 시키려면 false.
 */
function setDisabled(obj, disabled) {
	if ( typeof(obj) == "string" ) obj = document.getElementById(obj);

	obj.disabled = disabled;

	obj.style.backgroundColor = disabled ? DISABLED_BGCOLOR : "";
}


var READONLY_BGCOLOR = "#EFEFEF";

/**
 * 주어진 객체 ReadOnly 여부를 disabled 값으로 설정하고
 * 배경색을 조절한다.
 *
 * @param obj readOnly 여부를 세팅할 객체 참조 혹은 ID.
 * @param readOnly ReadOnly 여부. readOnly 시키려면 true, Enable 시키려면 false.
 */
function setReadOnly(obj, readonly) {
	if ( typeof(obj) == "string" ) obj = document.getElementById(obj);

	obj.readOnly = readonly;

	obj.style.backgroundColor = readonly ? READONLY_BGCOLOR : "";
}

