function getXMLHttpRequest() {
	if (window.ActiveXObject) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				return null;
			}
		}
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		return null;
	}
}

OrgSearch = function(onNodeCallback) {
	var currentNodeNum = -1;
	var keyword;
	var resultNodes;
	var current;

	var doSearch = function (k) {
		if(typeof(k) == "undefined" || k.length < 2) {
			throw "검색어는 2자이상 입력하세요.";
		}
		if(keyword != k) {
			keyword = k;
			var xmlHttpRequest = getXMLHttpRequest();
			xmlHttpRequest.open("GET", "/servlet/kr.co.kihyun.beans.user.HttpOrgSearch?keyword="+encodeURI(keyword), false);
			xmlHttpRequest.send(null);
			resultNodes = xmlHttpRequest.responseXML.getElementsByTagName('node');
		}
	};

	var getNode = function(nodeNum) {
		var node = new Object();
		node.id = resultNodes[nodeNum].getElementsByTagName("id")[0].childNodes[0].nodeValue;
		node.name = resultNodes[nodeNum].getElementsByTagName("name")[0].childNodes[0].nodeValue;
		node.type = resultNodes[nodeNum].getElementsByTagName("type")[0].childNodes[0].nodeValue;
		node.pathList = new Array();
		for(var i = 0; i < resultNodes[nodeNum].getElementsByTagName("path").length; i++)
			node.pathList[i] = resultNodes[nodeNum].getElementsByTagName("path")[i].childNodes[0].nodeValue;
		return node;
	}

	this.previous = function(k) {
		doSearch(k);
		if(currentNodeNum <= 0) {
			onNodeCallback(getNode(currentNodeNum = 0));
			throw "맨 앞까지 찾았습니다.";
		} else {
			onNodeCallback(getNode(--currentNodeNum));
		}
	};

	this.next = function(k) {
		doSearch(k);
		if(currentNodeNum >= resultNodes.length-1) {
			onNodeCallback(getNode(resultNodes.length-1));
			throw "맨 뒤까지 찾았습니다.";
		} else {
			onNodeCallback(getNode(++currentNodeNum));
		}
	};
};

