
var db = "";
var evt = null; //trick

var oTom = null;
var oJerry = null;

function autocom_Init(frm, keyObj, acLayer, acLayer, toggleImg, searchFunction){
	
    try{
        if(oTom == null){
        	oTom = new Tom();
        }

        if(oTom.frm==null){
        	oTom.frm = frm;
        }
        if(oTom.keyObj==null){
        	oTom.keyObj = keyObj;
        }
        oTom.keyObj.setAttribute("autocomplete","off");
        
        if(oTom.acLayer==null){
        	oTom.acLayer = acLayer;
        }
        if(oTom.toggleImg == null){
        	oTom.toggleImg = toggleImg;
        }
        
        oTom.acLayer.style.width = (oTom.keyObj.offsetWidth)+"px";
        oTom.acLayer.style.display = "none" ;
        
        var leftSize = oTom.acLayer.style.left;
        oTom.toggleImg.style.left = (parseInt(leftSize) + oTom.keyObj.offsetWidth - 18) +"px";
        oTom.toggleImg.style.display = "block";
        
        oTom.sSubmitFunctionName = searchFunction;

    	Jerry_Init();
    }catch(e){
       setTimeout("autocom_Init()", 1000);
    }
}

//자동완성창 초기화
function Jerry_Init() {
	try {
		if (oJerry == null) {
			oJerry = new Jerry();
			oJerry.oBox_lyr = document.getElementById("autocom_box")
			oJerry.oAcList_lyr = document.getElementById("autocom_list");
			oJerry.oAcTop_lyr = document.getElementById("autocom_top");
		}
	} catch (e) {
	}
}

function autocom_search(e){
    try{
        var event = window.event || e;
        evt = event; //trick
		
		if (event.keyCode == 40) {
			autocom_next();
		} else if (event.keyCode == 38) {
			autocom_prev();
		} else if (event.keyCode == 37) {
		} else if (event.keyCode == 39) {
		} else if (event.keyCode == 13) {
//			search();
		} else {
			oJerry.use = true;
			if(window.event)
			{
				setTimeout("oTom.find()", 100);
			} else {
				trick();
			}
		}
	} catch (ee) {
	}
}

function trick() {
	if (db != oTom.keyObj.value) {
		if(evt.keyCode==40){
        }else if(evt.keyCode==38){
        }else if(evt.keyCode==37){
        }else if(evt.keyCode==39){
        }else if(evt.keyCode==13){
        }else{
        	
			db = oTom.keyObj.value;
			oTom.find();
        }
	} else {
	}
	
	setTimeout("trick()", 1000);
}


function Tom(){
	
	this.version="1.0";
	this.name = "AutoComplete Tom";
	this.sLang = "KOR";
	this.frm = null;						
	this.keyObj = null;						
	this.sOldkwd = "";							
	this.sNewkwd = "";							
	this.acLayer = null;						
	this.toggleImg = null;					
	this.sImg_Toggle_on =  "/images/en/how/btn_ac_on.gif"; 
	this.sImg_Toggle_off = "/images/en/how/btn_ac_off.gif";
	this.sSkipWrd = "ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎ";	
	this.bSkipTF = false;						
	this.act =0; 								
	this.sSubmitFunctionName = "";
	this.isExistSkipwrd = function(){
		var i;
		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.toggle = function(){
		Jerry_on_off2();
	};
	
	this.find = function(){
		if(this.sOldkwd == this.keyObj.value.trim()){
			return;
		}else{
			if(this.act){
				return;
			}else{
				if(this.bSkipTF){
					if(this.isExistSkipwrd(this.keyObj.value.trim(), this.sSkipWrd)){
						return;
					}
				}
				this.act=1;
				this.sNewkwd = this.keyObj.value.trim();

				Jerry_AutoSearch(this);
				this.sOldkwd = this.sNewkwd;
				this.act=0;
			}
		}
	};
}

function Jerry_ShowAjax(strFaq) {
	try {
		oJerry.aList = new Array();
		if (strFaq.trim().length > 0) {
			oJerry.aList = decodeURIComponent(strFaq).trim().split("|");
		}
		Jerry_print();
	} catch (e) {
	}
}

function Jerry_print() {
	try {
		var s = "";
		var xiTmp = "";
		var ii = 0;

		if (oJerry.aList != null && oTom != null) {
			var sTmps = oTom.sNewkwd;
			if (sTmps.length > 0 && oJerry.aList.length > 0) {
				ii = 0;
				s += "<table width=\"95%\" style=\"margin-top:5px;\" cellpadding=\"0\" cellspacing=\"0\" >\n";
				for ( var i = 0; i < oJerry.aList.length; i++) {
					xiTmp = oJerry.aList[i].trim();
					xiTmp = xiTmp.replace(eval("/\\+/gi"), " ");
					if (xiTmp != "") {
						s += "<tr id='ac" + (i + 1)
								+ "' bgcolor='"
								+ oJerry.list_bg + "' ";
						s += " onmouseover=\"this.style.backgroundColor='"
								+ oJerry.list_over + "';\"  ";
						s += " onmouseout=\"this.style.backgroundColor='"
								+ oJerry.list_out + "';\" >\n";
						s += "<td style=\"height:18px;padding:0 0 5px 15px; cursor:pointer;\" onclick='Jerry_RealSearch("
								+ (i + 1) + ");' >\n";
						s += "<span id='auckey_" + (i + 1)
								+ "' style='display:none'>" + xiTmp
								+ "</span>\n";
						if (xiTmp.lenH() > oJerry.maxLenStr) {
							var xxx = MidH(xiTmp, 0, oJerry.maxLenStr);
							s += replace2Insensitive(
									xxx,
									sTmps,
									"<span style=\"font-weight:bold; color:#974680;\">",
									"</span>")
									+ "...";
						} else {
							s += replace2Insensitive(
									xiTmp,
									sTmps,
									"<span style=\"font-weight:bold; color:#974680;\">",
									"</span>");
						}
						s += "</td></tr>\n";
						ii++;
					}
				}

				oJerry.aListLen = ii;
				s += "</table>\n";
				oJerry.oAcList_lyr.innerHTML = s;

				Jerry_Resize();
			} else {
				oJerry.aListLen = 0;
				oJerry.oAcList_lyr.innerHTML = "";
				Jerry_hide();
			}
		} else {
			oJerry.aListLen = 0;
			oJerry.oAcList_lyr.innerHTML = s;
			Jerry_hide();
		}
	} catch (e) {
		oJerry.aList = null;
		oJerry.aListLen = 0;
	}
}

function autocom_next() {
//	try {
		if (oTom != null && oJerry != null) {
			if (oJerry.aListLen > 0) {
				if (oJerry.isActive != -1) {
					if (oJerry.aListLen <= (oJerry.isActive + 1)) {
						oJerry.isActive = oJerry.aListLen - 1;
					} else {
						oJerry.isActive++;
					}

					document.getElementById("ac" + oJerry.isActive).style.backgroundColor = oJerry.list_bg;
				} else {
					oJerry.isActive = 0;
					Jerry_show();
				}
			} else {
				oJerry.isActive = -1;
			}
			if (oJerry.isActive != -1) {
				oTom.toggleImg.src = oTom.sImg_Toggle_on;
				var ostr = document.getElementById("auckey_"
						+ (oJerry.isActive + 1));
				oTom.keyObj.value = HTML2TEXT(ostr.innerHTML);
				var obj = document.getElementById("ac"
						+ (oJerry.isActive + 1));
				obj.style.backgroundColor = oJerry.list_over;
				
				var scrollLayer = document.getElementById("autocom_list");
				scrollLayer.scrollTop = parseInt(oJerry.isActive / oJerry.mLen)* oJerry.mLen * 21;

				
			} else {
				Jerry_show();
			}
		}
//	} catch (e) {
//	}
}

function autocom_prev() {
	try {
		if (oJerry != null && oTom != null) {
			if (oJerry.aListLen > 0) {
				if (oJerry.isActive == -1) {
					Jerry_hide();
				} else if (oJerry.isActive == 0) {
					document.getElementById("ac1").style.backgroundColor = oJerry.list_bg;
					oJerry.isActive = -1;
				} else if (oJerry.isActive > 0) {
					document.getElementById("ac" + (oJerry.isActive + 1)).style.backgroundColor = oJerry.list_bg;
					oJerry.isActive--;
				}
				if (oJerry.isActive >= 0) {
					var ostr = document.getElementById("auckey_"
							+ (oJerry.isActive + 1));
					oTom.keyObj.value = HTML2TEXT(ostr.innerHTML);
					var obj = document.getElementById("ac"
							+ (oJerry.isActive + 1));
					obj.style.backgroundColor = oJerry.list_over;
//					oJerry.oAcList_lyr.scrollTop = parseInt(oJerry.isActive / oJerry.mLen) * oJerry.mLen * 21;
					var scrollLayer = document.getElementById("autocom_list");
					scrollLayer.scrollTop = parseInt(oJerry.isActive / oJerry.mLen) * oJerry.mLen * 21;
				}
			} else {
				Jerry_hide();
			}
		}
	} catch (e) {
	}
}

function Jerry() {
	this.version = "1.0";
	this.name = "AutoComplete Jerry";
	this.use = true;
	this.oBox_lyr = null;	// 박스 외부 레이어
	this.oAcList_lyr = null; //자동완성리스트 레이어
	this.oAcTop_lyr = null; //자동완성리스트의 상단 레이어 
	this.aList = null; //자동완성리스트의 Data배열
	this.aListLen = 0; //자동완상리스트의 전체갯수
	this.mLen = 8; //자동완성리스트의 한 화면에 보이는 row수.
	this.maxLenStr = 80; //자동완성리스트의 한 row에 보이는 최대 바이트 수 
	this.isActive = -1; //자동완성리스트중 현재 Active중인 TR (-1: null) 
	this.list_bg = "#FFFFFF"; //자동완성리스트 bgcolor
	this.list_over = "#FFDBBB"; //자동완성리스트 mouseover color
	this.list_out = "#FFFFFF"; //자동완성리스트 mouseout color
}

//자동키워드 완성 on/off
function Jerry_on_off2() 
{
	if(oJerry.use == true) {
		oJerry.use = false;
//		oJerry.mLen = 0;
		Jerry_hide(); 
	} else if(oJerry.use == false) { 
		oJerry.use = true;
//		oJerry.mLen = 10;
		Jerry_show();
	}
}

function Jerry_AutoSearch(o) {
//	try {
		if (oTom == null) {
			oTom = o;
		}

		if (oJerry.use == true) {
//			oJerry.mLen = 10;
			Jerry_Find();
		} else {
			if( oJerry.mLen == 0 ) {
//				oJerry.mLen = 0;
				oJerry.aListLen = 0;
				Jerry_hide();
			} else {
				//oJerry.aListLen = 0;
				//Jerry_show();
			} 
			
		}
//	} catch (e) {
//	}
}

function Jerry_Find() {
	try {
		var str = oTom.sNewkwd;
		oJerry.isActive = -1;
		if (str.length > 0 && str.trim().length < 30) {
			RunAjax("/how/how_auto_keyword.do", "?colname=autocom_" + oTom.sLang + "&SEARCH_TXT=" + escape(encodeURIComponent(str)), "Jerry_ShowAjax");
		} else {
			Jerry_hide();
		}
	} catch (e) {
	}
}

//자동키워드 완성 레이어 보여주기
function Jerry_show() {
	try {
		oTom.acLayer.style.display = "block";
		oTom.acLayer.innerHTML = oJerry.oBox_lyr.innerHTML + oJerry.oAcTop_lyr.innerHTML;
		oTom.toggleImg.src = oTom.sImg_Toggle_on;
	} catch (e) {
	}
}
//자동키워드 완성 레이어 숨기기
function Jerry_hide() {
	try {
		oTom.acLayer.style.display = "none";
		oTom.toggleImg.src = oTom.sImg_Toggle_off;
		oTom.keyObj.focus();
	} catch (e) {
	}
}

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
		xmlHttp.overrideMimeType("text/xml");
	}
	return xmlHttp;
}

function RunAjax(sUrl, sParam, sFunc) {
	try {
		xmlHttpReq = createXMLHttpRequest();
		xmlHttpReq.open('GET', sUrl + sParam, true);
		xmlHttpReq.setRequestHeader('Content-Type',	'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function() {
			try {
				if (xmlHttpReq.readyState == 4 && xmlHttpReq.responseText && xmlHttpReq.status == 200) {
					eval(sFunc + '(xmlHttpReq.responseText);');
				} else if (xmlHttpReq.status == 204) {
					eval(sFunc + '(\'\');');
				}else {
					eval(sFunc + '(\'\');');
				}
			} catch (ee) {
				return;
			}
			
		};
		xmlHttpReq.send(null);
		
	} catch (e) {
	}
}

function Jerry_RealSearch(n) {
	try {
		var ostr = document.getElementById("auckey_" + n);
		oTom.keyObj.value = HTML2TEXT(ostr.innerHTML);
		eval(oTom.sSubmitFunctionName);
	} catch (e) {
	}
}
function Jerry_Alive()
{
	oJerry.use = true;
}
function is_Jerry_use() 
{
	if(oJerry == null)
		return true;
	return oJerry.use;
}

function Jerry_Resize() {
	try {
		if (oTom != null && oJerry != null) {
			if (oJerry.aList != null) {
				if (oJerry.aListLen > 0) {
					var h = 0;
					var n = 28 + (20 * (oJerry.aListLen));

					if (oJerry.aListLen > oJerry.mLen) {
						h = 28 + (20 * (oJerry.mLen));
						oTom.acLayer.style.height = (h + 2) + "px";
						oJerry.oAcList_lyr.style.height = (h + 2 - 28)
								+ "px";
					} else {
						h = n;
						oTom.acLayer.style.height = (h + 2) + "px";
						oJerry.oAcList_lyr.style.height = (h + 2 - 28)
								+ "px";
					}
					Jerry_show();
				} else {
					//alert(oJerry.aListLen);
					Jerry_hide();
				}
			} else {
				//alert(oJerry.aListLen);
				Jerry_hide();
			}
		}
	} catch (e) {
	}
}

function replace2Insensitive(strData, s1, s2, s3) {
	//strData : source data
	//s1 : 변경할  data
	//s2 : 변경할 data앞에 들어가는 string
	//s3 : 변경할 data뒤에 들어가는 string
	var sRtn = "";
	try {
		if (strData.trim() != "") {
			var s1Lcase = s1.toLowerCase();//소문자처리.
			var sTmp = strData.toLowerCase(); //소문자처리.
			var iFind = -1;
			var i = 0;
			var iLenLast = 0;
			while (i == 0) {
				iFind = sTmp.indexOf(s1Lcase);
				if (iFind == 0) {
					sTmp = sTmp.substring(s1Lcase.length);
					sRtn += s2
							+ strData.substring(iLenLast + iFind, iLenLast
									+ iFind + s1Lcase.length) + s3;
					iLenLast += s1Lcase.length;
				} else if (iFind > 0) {
					sTmp = sTmp.substring(iFind + s1Lcase.length);
					sRtn += strData.substring(iLenLast, iFind
							+ s1Lcase.length)
							+ s2
							+ strData.substring(iLenLast + iFind, iLenLast
									+ iFind + s1Lcase.length) + s3;
					iLenLast += iFind + s1Lcase.length;
				} else {
					sRtn += strData.substring(strData.length - sTmp.length);
					i = 1;
				}
			}
		} else {
			sRtn = "";
		}
	} catch (e) {

	} finally {
		return sRtn;
	}
}

function HTML2TEXT(s) {
	try {
		s = s.replace(/&#0?39;/g, "'");
		s = s.replace(/&quot;/g, "\"");
		s = s.replace(/&amp;/g, "&");
		s = s.replace(/&lt;/g, "<");
		s = s.replace(/&gt;/g, ">");
	} catch (e) {

	} finally {
		return s;
	}
}

  String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)|($\s*)/g, "");
  };
 

String.prototype.lenH = function() {
  var temp;
  var k = 0;
  var mycount = 0;
     
  for( k = 0 ; k < this.length ; k++ ){
     temp = this.charAt(k);
     
     if( escape(temp).length > 4 ) {
        mycount += 2; 
     }
     else mycount++;
  }

  return mycount;
};

/**
 *  문자열에서 Byte Len만큼 문자열 잘라온다.
 *  ex) var s = MidH("대한민국만세123ABS",0,10);
 */
function MidH(str, n, len){
	//n : 시작점;
	//len : ByteLength; 
	var ret="";
	var tmp = str.substring(n, str.length);
	var s="";
	var j=0;
	var i=0;
	if(tmp.length>0){
		for(i=0; i< tmp.length; i++){
			if(j <= len){
				s = tmp.substring(i,i+1);
				if(s.lenH()==2){
					j = j+2;
				}else{
					j++;
				}
				ret = ret + s;
			}else{
				break;
			}
		}
	}
	return ret;
}



