


//ITISSelect selectInfo 멤버 정보
/*	
	selectElement							//select element (type select_element_object)
	requestURL								//요청page url (type string)
	firstValue								//첫번째 option tag의 value. null이면 "" (type string)
	firstText								//첫번째 option tag의 text. null이면 "" (type string)
	value									//option tag의 value = 수신 xml의 element 명 (type string)
	text									//option tag의 text = 수신 xml의 element 명 (type string 또는 string array)
	selectedValue							//자동선택 될 option의 value. null이면 처리 안함 (type string 또는  string array)
	ajaxRunning								//readonly, 작업 처리 중 여부 (type bool)
	onAfterFillSelect						//fillSelect호출에 의해 서버로부터 수신하여 select를 구성한 직후 호출됨.
*/

//ITISSelect 클래스 정의
function ITISSelect(selectInfo) {
	this.selectInfo = selectInfo;

	selectInfo.ajaxRunning = false;

	var saved_disabled_value = this.selectInfo.selectElement.disabled;
	this.selectInfo.selectElement.disabled = false;
	
	//공백 option 추가
	if (this.selectInfo.firstValue || this.selectInfo.firstText) {
		var first_value, first_text;

		//기존의 option들 지우기
		while (this.selectInfo.selectElement.length > 0) {
			this.selectInfo.selectElement.options[0] = null;
		}
		
		if (!this.selectInfo.firstValue) {
			first_value = "";
		} else {
			first_value = this.selectInfo.firstValue;
		}
		
		if (!this.selectInfo.firstText) {
			first_text = "";
		} else {
			first_text = this.selectInfo.firstText;
		}
		
		this.selectInfo.selectElement.options[0] = new Option(first_text, first_value);
	}

	this.selectInfo.selectElement.disabled = saved_disabled_value;

	//fillSelect 멤버 함수 정의	
	this.fillSelect = function() {
	
		if (this.selectInfo.ajaxRunning == true)
			return;
		
		if (!this.selectInfo.requestURL) {
			this.clearSelect();
			return;
		}

		if (this.selectInfo.requestURL == "") {
			this.clearSelect();
			return;
		}
		
		this.selectInfo.ajaxRunning = true;
		
		var thisObj = this;
	
		var xmlHttp = new Ajax.Request(thisObj.selectInfo.requestURL, 
			{
			method: "get", 
		    parameters: "",
		    onComplete: function(request) {
		    	//web server error
				if (xmlHttp.responseIsFailure()) {
					alert("웹서버에서 에러가 발생하였습니다.\r\n상태코드=" 
							+ xmlHttp.transport.status + "\r\n요청URL=" + thisObj.selectInfo.requestURL);
					thisObj.selectInfo.ajaxRunning = false;
					return;
				} 
				//web server success
				else {
					var xDoc = request.responseXML;
					
//					alert(xDoc.xml);

					//서버처리 중 exception 발생
					if (xDoc.getElementsByTagName("error").length > 0) {
						doError(xDoc);						
						thisObj.selectInfo.ajaxRunning = false;
						return;
					}
					var saved_disabled_value = thisObj.selectInfo.selectElement.disabled;
					thisObj.selectInfo.selectElement.disabled = false;

					//기존의 option들 지우기
					while (thisObj.selectInfo.selectElement.length > 0) {
						thisObj.selectInfo.selectElement.options[0] = null;
					}

					//공백 option 추가
					if (thisObj.selectInfo.firstValue || thisObj.selectInfo.firstText) {
						var first_value, first_text;
						
						if (!thisObj.selectInfo.firstValue) {
							first_value = "";
						} else {
							first_value = thisObj.selectInfo.firstValue;
						}
						
						if (!thisObj.selectInfo.firstText) {
							first_text = "";
						} else {
							first_text = thisObj.selectInfo.firstText;
						}
						
						thisObj.selectInfo.selectElement.options[0] = new Option(first_text, first_value);
					}

					var value = "", text = "";
					var received_cnt = 0;
					
					//수신받은 레코드 수 얻기
					if (typeof(thisObj.selectInfo.value) == "string")
						received_cnt = xDoc.getElementsByTagName(thisObj.selectInfo.value).length;
					else
						received_cnt = xDoc.getElementsByTagName(thisObj.selectInfo.value[0]).length;

					//조회 결과를 option으로 추가
					for (var i = 0; i < received_cnt; i++) {
						value = "";
						text = "";
						
						//value가 string인  경우의 처리 (xDoc의 단일 elements가  value 임)
						if (typeof(thisObj.selectInfo.value) == "string") {
							var tagObject = xDoc.getElementsByTagName(thisObj.selectInfo.value)[i].firstChild;
							if (tagObject != null) {
								if (tagObject.nodeValue != null)
									value = tagObject.nodeValue;
							}
						} 
						//value가 array인 경우의 처리 (xDoc 의 여러 elements의 연결값이 value 임) 
						else {
							for (var k = 0; k < thisObj.selectInfo.value.length; k++) {
								var tagObject = xDoc.getElementsByTagName(thisObj.selectInfo.value[k])[i].firstChild;
								if (tagObject != null) {
									if (tagObject.nodeValue != null)
										value += tagObject.nodeValue;
								}
							}
						}
						
						//value가 string인  경우의 처리 (xDoc의 단일 elements가  value 임)
						if (typeof(thisObj.selectInfo.text) == "string") {
							var tagObject = xDoc.getElementsByTagName(thisObj.selectInfo.text)[i].firstChild;
							if (tagObject != null) {
								if (tagObject.nodeValue != null)
									text = tagObject.nodeValue;
							}
						}
						//value가 array인 경우의 처리 (xDoc 의 여러 elements의 연결값이 value 임) 
						else {
							for (var k = 0; k < thisObj.selectInfo.text.length; k++) {
								var tagObject = xDoc.getElementsByTagName(thisObj.selectInfo.text[k])[i].firstChild;
								if (tagObject != null) {
									if (tagObject.nodeValue != null)
										text += tagObject.nodeValue;
								}
							}
						}
						
						thisObj.selectInfo.selectElement.options[thisObj.selectInfo.selectElement.options.length] 
									= new Option(text, value); 
					}
			
					//자동선택 처리
					if (thisObj.selectInfo.selectedValue) {
						thisObj.selectInfo.selectElement.value = thisObj.selectInfo.selectedValue;
					}
	
					thisObj.selectInfo.selectElement.disabled = saved_disabled_value;
					
					if (thisObj.selectInfo.onAfterFillSelect)
						thisObj.selectInfo.onAfterFillSelect();

					//수신데이터 처리 완료.
					thisObj.selectInfo.ajaxRunning = false;
				}
			}
			}
		);
	}

	//fillSelect 멤버 함수 정의	
	this.clearSelect = function() {
		
		if (this.selectInfo.ajaxRunning == true)
			return;
		
		var thisObj = this;
		
		//기존의 option들 지우기
		while (thisObj.selectInfo.selectElement.length > 0) {
			thisObj.selectInfo.selectElement.options[0] = null;
		}

		//공백 option 추가
		if (thisObj.selectInfo.firstValue || thisObj.selectInfo.firstText) {
			var first_value, first_text;
			
			if (!thisObj.selectInfo.firstValue) {
				first_value = "";
			} else {
				first_value = thisObj.selectInfo.firstValue;
			}
			
			if (!thisObj.selectInfo.firstText) {
				first_text = "";
			} else {
				first_text = thisObj.selectInfo.firstText;
			}
			
			thisObj.selectInfo.selectElement.options[0] = new Option(first_text, first_value);
		}
	}
}

