var oT = null;
function oT_Init(){
	//try{
		if(oT == null){
			oT = new Tom();
		}
		var o = oT;
		if(o.oForm==null){
			o.oForm = document.fmMainSearch;
		}
		if(o.sCollection==null){
			//o.sCollection = o.oForm.c.value;
			o.sCollection = "enuri";
		}
		if(o.oKeyword==null){
			o.oKeyword = o.oForm.keyword;//document.getElementById("keyword");
		}
		o.oKeyword.setAttribute("autocomplete","off"); 
		if(o.oAc_lyr==null){
			o.oAc_lyr = document.getElementById("ac_layer");
		}
		if(o.oAc_ifr==null){
			o.oAc_ifr = document.getElementById("ifr_ac");	
		}
		o.oAc_lyr.style.left  = (calculateOffsetLeft(o.oKeyword) - 4 )+ "px";
		var varAddHeight = 0;
		if (BrowserDetect.browser == "Explorer" && (BrowserDetect.version == 6 || BrowserDetect.version == 7)){
			varAddHeight = 0;
		}else{
			varAddHeight = 1;
		}
		o.oAc_lyr.style.top   = (calculateOffsetTop(o.oKeyword)+ o.oKeyword.offsetHeight + varAddHeight)+ "px";
		o.oAc_lyr.style.width = (o.oKeyword.offsetWidth+26) + "px";
		o.oAc_lyr.style.display = "none" ;
		o.oAc_ifr.style.width = (o.oKeyword.offsetWidth+26) + "px";
		o.oAc_ifr.style.display = "block";
		o.sSubmitFunctionName = "Cmd_MainSearchSubmit()";
	//}catch(e){
	//	setTimeout("oT_Init()", 1000);
	//}
}
function oT_search(objEvt){
	//try{
		if(oT==null){
			oT_Init();
		}

		//CmdHideMenu();
		var o = oT;
		var e = null;
		if (o.oAc_lyr == null){
			oT_Init();
		}		
		o.oAc_lyr.style.left  = (calculateOffsetLeft(o.oKeyword) - 4 )+ "px";

		if (typeof(objEvt) == "undefined"){
			e = window.event;
		}else{
			e = objEvt; 
		}
		var obj = o.oAc_ifr.contentWindow
		if(e.keyCode==40){
			obj.Jerry_next();
		}else if(e.keyCode==38){
			obj.Jerry_prev();
		}else if(e.keyCode==37){
		}else if(e.keyCode==39){
		}else if(e.keyCode==13){
		}else{
			setTimeout("oT.find()", o.TimerInterval);
		}
				
	//}catch(e){		
	//}	
}
function changeStyle(obj,onoff){
	if (onoff == 'on'){
		if (obj.value.trim().length == 0 ){
			obj.style.color ="#FF0000";
			obj.style.backgroundImage ='none';
		}
		if (document.getElementById("ac_layer").style.display == "none"){
			if (document.getElementById("div_search_01")){
				if (document.URL.indexOf("Searchlist.jsp") < 0 ){
					document.getElementById("div_search_01").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/l1_2.gif)';
				}else{
					document.getElementById("div_search_01").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/l2_2.gif)';
				}
				document.getElementById("div_search_02").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/bg2_2.gif)';
				document.getElementById("img_close_auto").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/c1_2.gif)';
				document.getElementById("img_close_auto").style.cursor="default";
			}
		}
	}else{
		if (obj.value.trim().length == 0 ){
			obj.style.backgroundImage ='url('+strRanSearchImage+')';
			obj.style.backgroundRepeat = 'no-repeat';
		}	
		if (document.getElementById("ac_layer").style.display == "none"){
			if (document.getElementById("div_search_01")){
				if (document.URL.indexOf("Searchlist.jsp") < 0 ){
					document.getElementById("div_search_01").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/l1.gif)';
				}else{
					document.getElementById("div_search_01").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/l2.gif)';
				}
				document.getElementById("div_search_02").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/bg2.gif)';
				document.getElementById("img_close_auto").style.backgroundImage ='url('+var_img_enuri_com+'/2010/images/search/c1.gif)';
				document.getElementById("img_close_auto").style.cursor="default";
			}
		}else{
		
		}
	}
}
function changeStyleNew(obj,onoff){
	if (onoff == 'on'){
		if (obj.value.trim().length == 0 ){
			obj.style.color ="#FF0000";
			obj.style.backgroundImage ='none';
		}
		if (document.getElementById("ac_layer").style.display == "none"){
			if (document.getElementById("n_search_5").src != (var_img_enuri_com + "/2010/images/search/20111117/search_02_ov.gif")){
				document.getElementById("n_search_5").src = var_img_enuri_com + "/2010/images/search/20111117/search_02_ov.gif";
				document.getElementById("n_search_6").style.backgroundImage="url("+var_img_enuri_com+"/2010/images/search/20111117/search_03_ov.gif)";
				document.getElementById("n_search_7").src = var_img_enuri_com + "/2010/images/search/20111117/search_06_ov.gif"			
				document.getElementById("n_search_7").style.cursor="default";		
			}
		}
	}else{
		if (obj.value.trim().length == 0 ){
			obj.style.backgroundImage ='url('+strRanSearchImage+')';
			obj.style.backgroundRepeat = 'no-repeat';
		}	
		if (document.getElementById("ac_layer").style.display == "none"){
			if (document.getElementById("n_search_5").src != (var_img_enuri_com + "/2010/images/search/20111117/search_02.gif")){
				document.getElementById("n_search_5").src = var_img_enuri_com + "/2010/images/search/20111117/search_02.gif";
				document.getElementById("n_search_6").style.backgroundImage="url("+var_img_enuri_com+"/2010/images/search/20111117/search_03.gif)";
				document.getElementById("n_search_7").src = var_img_enuri_com + "/2010/images/search/20111117/search_06.gif"	
			}
		}
	}
}
function Cmd_MainSearchSubmit(){

	var obj = document.fmMainSearch; 
	schWrd = obj.keyword.value;
	if (varRanSearch == 0 ){
			var return_a = Cmd_MainSearch1();
			if(return_a){
				document.fmMainSearch.submit();
			}	
	}else{
		if (document.fmMainSearch.keyword.style.backgroundImage == "none" || document.fmMainSearch.keyword.style.backgroundImage == ""){
			var return_a = Cmd_MainSearch1();
			if(return_a){
				if (document.getElementById("n_search_2")){	
					document.getElementById("n_search_2").src = var_img_enuri_com + "/2010/images/search/20111117/search_02.gif";
					document.getElementById("n_search_3").style.backgroundImage="url("+var_img_enuri_com+"/2010/images/search/20111117/search_03.gif)";
					document.getElementById("n_search_4").src = var_img_enuri_com + "/2010/images/search/20111117/search_04.gif";
					document.getElementById("n_search_7").src = var_img_enuri_com + "/2010/images/search/20111117/search_06.gif"
					document.getElementById("select_search_area").style.display = "none";
					document.getElementById("search_btn_go").style.display = "none";
					document.getElementById("sarea_arrow_btn").style.display = "";
				}
				document.fmMainSearch.submit();
			}
		}else{
            if (varRanSearch == 1 ){
				insertLog(7227);
				top.location.href = "/event/EventReview.jsp?event_idx=532";
			}else if (varRanSearch == 2 ){
				insertLog(7224);
				top.location.href = "/event/EventReview.jsp?event_idx=530";
			}else if (varRanSearch == 3 ){
				insertLog(7189);
				top.location.href = "/event/EventReview.jsp?event_idx=523";
			}else if (varRanSearch == 4 ){
				insertLog(7190);
				top.location.href = "/event/EventReview.jsp?event_idx=524";
			}else if (varRanSearch == 5 ){
				insertLog(7191);
				top.location.href = "/event/EventReview.jsp?event_idx=525";
			}else if (varRanSearch == 6 ){
				insertLog(7192);
				top.location.href = "/event/EventReview.jsp?event_idx=526";
			}else if (varRanSearch == 7 ){
				insertLog(7226);
				top.location.href = "/event/EventReview.jsp?event_idx=528";
			}else if (varRanSearch == 8 ){
				insertLog(7225);
				top.location.href = "/event/EventReview.jsp?event_idx=529";
			}else if (varRanSearch == 9 ){
				insertLog(7223);
				top.location.href = "/event/EventReview.jsp?event_idx=531";
			}else if (varRanSearch == 10 ){
				insertLog(7193);
				top.location.href = "/event/EventReview.jsp?event_idx=527";
			}else{
				insertLog(5860);
				top.location.href = "/event/Event_Guide_Travel.jsp";
			}

			insertLog(4986);
			return false;	
		}
	}
}
function schKeyword(schWrd){
	document.fmMainSearch.ismodelno.value = checkModelNoFormat(schWrd);
	var schWrdExp;
	schWrdExp = schWrd;
	
	schWrdExp = replace2(schWrdExp,"'");
	schWrdExp = replace2(schWrdExp,"\\");
	schWrdExp = replace2(schWrdExp,"^");
	schWrdExp = replace2(schWrdExp,"&");
	schWrdExp = replace2(schWrdExp,"~");
	schWrdExp = replace2(schWrdExp,"!");
	schWrdExp = replace2(schWrdExp,"@");
	schWrdExp = replace2(schWrdExp,"$");
	schWrdExp = replace2(schWrdExp,"%");
	schWrdExp = replace2(schWrdExp,"*");
	schWrdExp = replace2(schWrdExp,"+");
	schWrdExp = replace2(schWrdExp,"=");
	schWrdExp = replace2(schWrdExp,"\\");
	schWrdExp = replace2(schWrdExp,"{");
	schWrdExp = replace2(schWrdExp,"}");
	schWrdExp = replace2(schWrdExp,"[");
	schWrdExp = replace2(schWrdExp,"]");
	schWrdExp = replace2(schWrdExp,":");
	schWrdExp = replace2(schWrdExp,";");
	schWrdExp = replace2(schWrdExp,"/");
	schWrdExp = replace2(schWrdExp,"<");
	schWrdExp = replace2(schWrdExp,">");
	//schWrdExp = replace2(schWrdExp,".");
	schWrdExp = replace2(schWrdExp,",");
	schWrdExp = replace2(schWrdExp,"?");
	schWrdExp = replace2(schWrdExp,"(");
	schWrdExp = replace2(schWrdExp,")");
	schWrdExp = replace2(schWrdExp,"'");
	schWrdExp = replace2(schWrdExp,"_");
	schWrdExp = replace2(schWrdExp,"-");
	schWrdExp = replace2(schWrdExp,"`");
	schWrdExp = replace2(schWrdExp,"|");
    schWrdExp = schWrdExp.trim();
    
	function replace2(src, delWrd){
		var newSrc = "";
		for(var i=0;i<src.length;i++){
			if(src.charAt(i) == delWrd) {
				newSrc = newSrc + " ";
			}else{
				newSrc = newSrc + src.charAt(i);
			}			
		}
		return newSrc;
	}	    
    return schWrdExp;
}
function check2Hyphen(keyword){
	var bReturn = false;
	if (keyword.length >= 2){
		if (keyword.substring(1,2) == "-" || keyword.substring(1,2) == "_" || keyword.substring(1,2) == " "){
			bReturn = true;
		}
	}
	return bReturn;
}
function Cmd_MainSearch1(){
	if(oT != null){
		if (typeof(oT.lktype) == "function"){
			oT.lktype();
			return false;
		} 
	}
	var schWrd;
	var obj = document.fmMainSearch; 
	schWrd = obj.keyword.value;
	var schWrdExp = schKeyword(schWrd);
	schLen = schWrdExp.length;	
	if(schLen < 2 ){
		if (!(schLen == 1 && varExpKeyWord.indexOf(schWrd) >= 0 )){
			alert("2자 이상의 검색어를 넣으세요.\t\t\r\n\특수문자는 제외 됩니다.");
			obj.searchkind.value = "";
			obj.keyword.focus();
			return false;
		}
	}
	if (document.getElementById("img_close_auto")){
		document.getElementById("img_close_auto").src = var_img_enuri_com + "/2010/images/search/c1.gif"
		document.getElementById("img_close_auto").style.cursor="default";
	}
	if (document.getElementById("n_search_7")){
		if (selectSearchArea._f_area == 0){
			alert("1개 이상을 선택하셔야 합니다.")
			return false;
		}
		hideSearchSelectArea();
	}	
	obj.hyphen_2.value = check2Hyphen(obj.keyword.value);	
	obj.keyword.value = schWrdExp;

	if (obj.issearchpage.value == "Y"){
		document.getElementById("img_search_loading").style.display = "";
		obj.target = "ifrmMainSearch";
		obj.action = "/search/GetcountSearchAll.jsp";
	}else{
		obj.target = "_top";
		obj.action = "/search/Searchlist.jsp";
	}			
	insertLog(3231);
	return true;
	
	//alert("죄송합니다.\r\n\r\n현재 검색서버의 장애로 검색이 되지 않고 있습니다.\r\n\r\n불편하시더라도 메뉴를 이용하여 주시기 바랍니다.\r\n\r\n조속히 정상화 되도록 최선을 다하겠습니다.	");	
	//return false;																				
}	
function calculateOffsetLeft(field) {
  return calculateOffset(field, "offsetLeft");
}

function calculateOffsetTop(field) {
  return calculateOffset(field, "offsetTop");
}

function calculateOffset(field, attr) {
  var offset = 0;
  while(field) {
    offset += field[attr]; 
    field = field.offsetParent;
   }
  return offset;
}

//자동완성 찾기 및 이벤트 체크.
function Tom(){
	this.version="1.0";
	this.name = "AutoComplete Tom";
	this.oForm = null;							//검색어 입력 Form객체
	this.sCollection = null; //콜랙션 정보.		//자동완성 해당 콜랙션


	this.oKeyword = null;						//검색키워드 입력 Text객체
	this.sOldkwd = "";							//이전검색어
	this.sNewkwd = "";							//신규검색어
	this.oAc_lyr = null;						//자동완성 전체레이어객체


	this.oAc_ifr = null;						//자동완성처리 프레임객체


	this.oImg_Toggle = null;					//검색키워드 입력 Text객체의 우측 토글이미지객체
	this.sImg_Toggle_on = "http://img.enuri.gscdn.com/images/main_2007/search_arr_off.gif";
	this.sImg_Toggle_off = "http://img.enuri.gscdn.com/images/main_2007/search_arr_on.gif"; 
	this.sSkipWrd = "ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎ";	//자동완성 skip단어
	this.bSkipTF = false;						//자동완성 skip단어 기능을 사용할지 여부
	this.TimerInterval = 200; 					//자동완성 체크주기1000이 1초


	this.act =0; 								//자동완성 실행중인지 여부 (0:유휴, 1:실행)
	this.sSubmitFunctionName = "";
	this.isExistSkipwrd = function(){
		var ret = false;
		s = arguments[0]; 
		t = arguments[1];
		for(i=0;i<s.length;i++){
			if(t.indexOf(s.substring(i, i+1)) < 0){
				ret = false;
			}else{
				ret = true;
				break;
			}
		}
		return ret;
	};
	this.lktype="";
	this.lklink=null;
	this.toggle = function(){
		if(this.oAc_ifr!=null){
			obj = document.getElementById("ifr_ac").contentWindow;
			obj.Jerry_on_off();
		}
	};
	this.find = function(){
		if(this.act){
			return;
		}else{
			if(this.bSkipTF){
				if(this.isExistSkipwrd(this.oKeyword.value.trim(), this.sSkipWrd)){
					return;
				}
			}
			this.act=1;
			this.sNewkwd = this.oKeyword.value.trim();
			obj = document.getElementById("ifr_ac").contentWindow;
			obj.Jerry_AutoSearch(this);
			this.sOldkwd = this.sNewkwd;
			this.act=0;
		}		
	};
}
function checkModelNoFormat(schWord){
	if (schWord.length == 8 ){
		try{
			var f3Number = parseInt(schWord.substring(0,3));
			var l4Numver = parseInt(schWord.substring(4,8));			
			if (schWord.substring(3,4) == "-"){
				return true;
			}else{
				return false;
			}
		}catch(e){
			return false;
		}
	}else{
		return false;
	}
}
function fnGetCountSearchAll(varCount,varSearchkind){
	var obj = document.fmMainSearch;
	obj.target = "_top";	
	obj.searchkind.value = varSearchkind;
	if (varSearchkind == "3"){
		obj.es.value = "no";
	}
	obj.action = "/search/Searchlist.jsp";
	obj.submit();	
}
function hideAutoComLayer(){
	document.getElementById('ac_layer').style.display='none';
	document.getElementById("n_search_7").src = var_img_enuri_com + "/2010/images/search/20111117/search_06.gif"
	
}
