/**
 * XMLElement에서 노드 값을 반환한다. 노드에 자식이 있는 경우 자식이 없는 노드까지 탐색하여 값을 가져온다.
 * @param node XMLNode
 * @return 노드 값
 */
function getXmlNodeValue(node) {
	if (node.childNodes.length > 0)
		return getXmlNodeValue(node.childNodes[0]);
	else {
		var objValue = node.nodeValue;
		return objValue;
	}	
}

/**
 * XMLElement의 속성들을 JSON 타입으로 반환한다.
 * @param node XMLNode
 * @return JSON Object로 표현된 속성들
 */
function getXmlAttributeToJSON(node) {
	var jsonString = '{';
	
	for (var i = 0; i < node.attributes.length; i++) {
		var nodeValue = node.attributes[i].nodeValue; 
		jsonString += "'" + node.attributes[i].nodeName + "': " + nodeValue.toJSON();
		if (i + 1 != node.attributes.length)
			jsonString += ', ';
	}
	jsonString += '}';

	return jsonString.evalJSON();
}

/**
 * 모든 자식 노드들을 삭제한다.
 * @param parent 삭제할 자식 노드를 갖는 부모 노드
 */
function removeAllChildren(parent) {
	var children = parent.childElements();
	
	for (var i = 0; i < children.length; i++) {
		if (children[i].childElements().length > 0)
			removeAllChildren(children[i]);
		parent.removeChild(children[i]);
	}
}

/**
 * 자식 노드들을 JSON Object로 변환한다.
 * @param obj XMLNode
 * @return JSON Object로 표현된 값
 */
function nodeToJSON(obj) {
	var jsonString = '{';
	
	for (var i = 0; i < obj.childNodes.length; i++) {
		var nodeValue = getXmlNodeValue(obj.childNodes[i]);
		jsonString += "'" + obj.childNodes[i].nodeName + "':" + (nodeValue == null ? "''" : nodeValue.toJSON());
		if (i + 1 != obj.childNodes.length)
			jsonString += ",";
	}

	jsonString += '}';
	return jsonString.evalJSON();
}

/**
 * 객체를 화면 중앙에 표시한다.
 * @param obj 표시할 객체
 */
function centerPosition(obj) {
	var dimensions = obj.getDimensions();

	var fullWidth, fullHeight;
	var docSize = document.viewport.getDimensions();
	if(docSize.width == 0)
	{
		fullWidth = document.body.clientWidth;
		fullHeight = document.body.clientHeight;
	}
	else
	{
		fullWidth = docSize.width;
		fullHeight = docSize.height;
	}
	
	var scrollOffset = document.viewport.getScrollOffsets();

	var leftpos = (fullWidth / 2) - (dimensions.width / 2) + scrollOffset.left;
	var toppos = (fullHeight / 2) - (dimensions.height / 2) + scrollOffset.top;

	obj.setStyle({'left':leftpos+'px','top':toppos+'px'});
}

/**
 * 객체를 화면 중앙에서 입력한 값만큼 이동시킨다 표시한다.
 * @param obj 표시할 객체
 */
function inputMoveCenterPosition(obj, width, height) {
	var dimensions = obj.getDimensions();

	var fullWidth, fullHeight;
	var docSize = document.viewport.getDimensions();
	if(docSize.width == 0)
	{
		fullWidth = document.body.clientWidth;
		fullHeight = document.body.clientHeight;
	}
	else
	{
		fullWidth = docSize.width;
		fullHeight = docSize.height;
	}
	
	var scrollOffset = document.viewport.getScrollOffsets();

	var leftpos = (fullWidth / 2) - (dimensions.width / 2) + scrollOffset.left + width;
	var toppos = (fullHeight / 2) - (dimensions.height / 2) + scrollOffset.top + height;

	obj.setStyle({'left':leftpos+'px','top':toppos+'px'});
}

