// 여행상품, 호텔 검색폼 Ajax 기본 XMLHTTP onreadyStateChange 액션 처리

function tourSearchXmlHttp(actionLink, actionObj, actionType, func) {
	var thisXmlHttpObj = {};
	var ajaxData;

	if (typeof(actionObj)=="string") var actionObj = getElement(actionObj);
	
	if (window.XMLHttpRequest) {thisXmlHttpObj = new XMLHttpRequest();}
	else if (window.ActiveXObject) {thisXmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");}

	thisXmlHttpObj.open("GET", actionLink, true);

	thisXmlHttpObj.onreadystatechange = function() {
		if (thisXmlHttpObj.readyState == 4) {
			switch (actionType) {
				case "T"	:	ajaxData = thisXmlHttpObj.responseText; break;
				case "X"	:	ajaxData = thisXmlHttpObj.responseXML; break;
				case "B"	:	ajaxData = thisXmlHttpObj.responseBody; break;
				default : 	ajaxData = thisXmlHttpObj.responseText;
			}
/*			
			var bindingAjaxData = function() {
				var thisObjTagName = actionObj.tagName.toLowerCase();
				var resultFlag = false;
				
				if (actionType=="T")  {resultFlag = setObjData(actionObj,thisObjTagName, ajaxData); }
				else {}
				
				if ((resultFlag)&(typeof(func) !=="undefined")) { try {	setTimeout(func,500); } catch(e) {} }
							
			}

			bindingAjaxData();
*/			
			bindingAjaxData(actionType, actionObj, ajaxData, func);
		}
		else {
			if(actionObj.tagName.toLowerCase()=="select") {
				actionObj.options[1]=new Option("DATA를 읽어오고 있습니다....",""); 
				actionObj.selectedIndex=1;
			}
		} 
	}
	thisXmlHttpObj.send(null);
}


// Ajax 데이타 바인딩
function bindingAjaxData(thisType, thisObj, thisData, thisFunc) {
	var thisObjTagName = thisObj.tagName.toLowerCase();
	var resultFlag = false;
	
	if (thisType=="T")  {resultFlag = setObjData(thisObj,thisObjTagName, thisData); }
	else {}
	
	if ((resultFlag)&(typeof(thisFunc) !=="undefined")) { try {	setTimeout(thisFunc,500); } catch(e) {} }
}

// 액션 오브젝트 태그에 따른 처리 (2007-05-22 현재 selectBox만 처리, span, div 등은 innerHTML로 처리)
function setObjData(actObj, actObjTag, actData) {
	var flag = true;
	
	try {
		if (actObjTag=="select") makeSelectBoxTour(actObj, eval('['+actData+']'));
		else actObj.innerHTML = actData;
	}
	catch (e) { flag = false;}
	
	return flag;
}

// select box 만들기(여행상품용)
	
function makeSelectBoxTour(selObj,selData) {
	var euFlag = false;
	selObj.selectedIndex=0;

	var j; 

	if (selObj==tourRegionObj) {
		for(var i= 0; i < selData.length;i++) {
			if (euFlag)	j=i+2;
			else j=i+1;
			
			if ((i < selData.length) & (selData[i][0].substring(0,1)=="E")&(euFlag==false)) {
				makeSelectObjOption(selObj, j, "유럽", "EU");
				euFlag = true;

				makeSelectObjOption(selObj, j+1, selData[i][1], selData[i][0]);
			}
			else {
				makeSelectObjOption(selObj, j, selData[i][1], selData[i][0]);
			}
		}
	}
	else if ((selObj==tourNationObj)||(selObj==tourCityObj)||(selObj==tourThemeObj)||(selObj==tourTypeObj)) {
		for(var i= 0; i < selData.length;i++) {
			makeSelectObjOption(selObj, i+1, selData[i][1], selData[i][0]);
		}
	}

	else {
		for(var i= 0; i < selData.length;i++) {
			selObj.options[i+1]=new Option(selData[i][1], selData[i][0]);
		}
	}
	
	if (euFlag) selObj.length = selData.length+2;
	else selObj.length = selData.length+1;
}


// Select Box 옵션 데이타 Binding (시작시 초기 값 처리 반영)-- 현재는 여행상품으로 고정

function makeSelectObjOption(obj, optNum, optName, optVal) {
	if(tourStartFlag) {
		/*
			if (areaInit=="N") {
				if(obj==tourRegionObj) 		beginSelectedChk(obj, tourRegionObj, reqRegion, optNum, optName, optVal);
				else if(obj==tourNationObj) beginSelectedChk(obj, tourNationObj, reqNation, optNum, optName, optVal);
				else if(obj==tourThemeObj) 	beginSelectedChk(obj, tourThemeObj, reqthem, optNum, optName, optVal);
				else if(obj==tourTypeObj) 	beginSelectedChk(obj, tourTypeObj, reqType, optNum, optName, optVal);
			}
			else if (areaInit=="C") {
				if(obj==tourRegionObj) 		beginSelectedChk(obj, tourRegionObj, reqRegion, optNum, optName, optVal);
				else if(obj==tourNationObj) beginSelectedChk(obj, tourNationObj, reqNation, optNum, optName, optVal);
				else if(obj==tourCityObj) 	beginSelectedChk(obj, tourCityObj, reqCity, optNum, optName, optVal);
				else if(obj==tourThemeObj) 	beginSelectedChk(obj, tourThemeObj, reqthem, optNum, optName, optVal);
				else if(obj==tourTypeObj) 	beginSelectedChk(obj, tourTypeObj, reqType, optNum, optName, optVal);
			}
		*/

		if(obj==tourRegionObj) 		beginSelectedChk(obj, tourRegionObj, reqRegion, optNum, optName, optVal);
		else if(obj==tourNationObj) beginSelectedChk(obj, tourNationObj, reqNation, optNum, optName, optVal);
		else if(obj==tourCityObj) 	beginSelectedChk(obj, tourCityObj, reqCity, optNum, optName, optVal);
		else if(obj==tourThemeObj) 	beginSelectedChk(obj, tourThemeObj, reqthem, optNum, optName, optVal);
		else if(obj==tourTypeObj) 	beginSelectedChk(obj, tourTypeObj, reqType, optNum, optName, optVal);

	}
	else {
		obj.options[optNum]=new Option(optName,optVal);
	}
}


// Select Box 옵션 데이타 Binding 처리(시작시 초기 값 처리 반영)

function beginSelectedChk(thisObj, compObj, reqVal, thisNum, thisName, thisVal) {

	if(thisObj==compObj) {
		if (reqVal==thisVal) thisObj.options[thisNum]=new Option(thisName,thisVal,true,true);
		else thisObj.options[thisNum]=new Option(thisName,thisVal);
	}
}


/* 

여행 상품 자체 처리 함수 모음

*/


// 현재 선택된 데이타 값 체크
function getFormStatusChk() {
	var thisArea="";
	var thisAreaCode="";

	if (tourCityObj.value !=="") {
		thisArea="C";
		thisAreaCode = tourCityObj.value;
	}
	else if (tourNationObj.value !=="") {
		thisArea="N";
		thisAreaCode = tourNationObj.value;
	}
	else if (tourRegionObj.value !=="") {
		thisArea="R";
		thisAreaCode = tourRegionObj.value;
	}
	
	return thisArea+":"+thisAreaCode;
}

//해당 대륙 불러오기
function getRegion_Search() {

	var link = tourAjaxDir+"area.asp?theme="+tourThemeTypeObj;
	//testFrame.location.href=link;
	tourSearchXmlHttp(link, tourRegionObj,'T', 'getNation_Search()');
}

// 해당 국가 불러오기
function getNation_Search() {
	var link = tourAjaxDir+"area.asp?status=N&region="+tourRegionObj.value;
	tourSearchXmlHttp(link, tourNationObj,'T', 'getCity_Search()');
}

//해당 도시 불러오기
function getCity_Search() {
	var link = tourAjaxDir+"area.asp?status=C&nation="+tourNationObj.value;
	
	//testFrame.location.href=link;
	tourSearchXmlHttp(link, tourCityObj, 'T', 'getTheme_Search()');
}


//해당 테마 불러오기
function getTheme_Search() {
	//tourStartFlag = false;
	if((typeof tourThemeObj) !="undefined")
	{	
		var thisCodeArr = getFormStatusChk().split(":");
		var param="&status="+thisCodeArr[0]+"&areaCode="+thisCodeArr[1];
		var link = tourAjaxDir+"theme.asp?act=TH"+param;
		
		tourSearchXmlHttp(link, tourThemeObj,'T', 'getType_Search()');
	}
	else
	{
		if((typeof tourTypeObj) !="undefined")
		{
			if(tourThemeTypeObj !=="") 
			{
				var thisCodeArr = getFormStatusChk().split(":");
				var param="&status="+thisCodeArr[0]+"&areaCode="+thisCodeArr[1]+"&theme="+tourThemeTypeObj;
				var link = tourAjaxDir+"theme.asp?act=TY"+param;
				//testFrame.location.href=link;
				tourSearchXmlHttp(link, tourTypeObj,'T');
			}
		}

	}
}

// 해당 타입 불러오기
function getType_Search() {
	if((typeof tourTypeObj) !="undefined")
	{
		if(tourThemeObj.value !=="") {
			var thisCodeArr = getFormStatusChk().split(":");
			var param="&status="+thisCodeArr[0]+"&areaCode="+thisCodeArr[1]+"&theme="+tourThemeObj.value;
			var link = tourAjaxDir+"theme.asp?act=TY"+param;
			//testFrame.location.href=link;
			tourSearchXmlHttp(link, tourTypeObj,'T');
		}
		
		else {
			tourTypeObj.length=1;
		}
	}
}

