﻿var $ = function(targetId){
    return document.getElementById(targetId);
}

function AjaxProducer() {
	this.XmlHttp = this.GetHttpObject();
}
 
AjaxProducer.prototype.GetHttpObject = function() { 
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) {
			xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof(XMLHttpRequest) != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

AjaxProducer.prototype.DoSend = function(requestURL, queryString, returnMethod) {
	if (navigator.appName.indexOf('Netscape') > -1) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		} catch (e) { }
	}
					
	if( this.XmlHttp ) {
		if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 ) {
			var oThis = this;
			//log(requestURL +"?"+ queryString);
			//this.XmlHttp.open("get", requestURL +"?"+ queryString, true);
			this.XmlHttp.open("post", requestURL, true);
			this.XmlHttp.onreadystatechange = function(){oThis.ReadyStateChange(returnMethod);};
			this.XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;?charset=utf-8");
			//this.XmlHttp.send(null);
			this.XmlHttp.send(queryString);
		}
	}
}

AjaxProducer.prototype.AbortCallBack = function() {
	if( this.XmlHttp )
		this.XmlHttp.abort();
}
 
AjaxProducer.prototype.OnOpened = function() {
	// OnOpened
}
 
AjaxProducer.prototype.OnHeaderReceived = function() {
	// OnHeaderReceived
}
 
AjaxProducer.prototype.OnLoading = function() {
	// OnLoading
}

AjaxProducer.prototype.OnDone = function(CoutomObj) {
	if( this.XmlHttp.status == 0 ) {
		this.OnAbort();
	}
	else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" ) {
		var returnText = this.XmlHttp.responseText;
		
		CoutomObj.DoCallBack(returnText);
		
		//this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
	}
	else {
		var returnText = this.XmlHttp.responseText;
		
		CoutomObj.DoCallBack(returnText);
		
		this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
	}
}

AjaxProducer.prototype.OnComplete = function(responseText, responseXml) {
  // Complete
}
 
AjaxProducer.prototype.OnAbort = function() {
  // Abort
}
 
AjaxProducer.prototype.OnError = function(status, statusText, responseText) {
  // Error
  alert("AJAX실행도중 예외가 발생하였습니다");
}
 
AjaxProducer.prototype.ReadyStateChange = function(CoutomObj) {

	switch (this.XmlHttp.readyState) {
	case 1:
		this.OnOpened();
		break;
		
	case 2:
		this.OnHeaderReceived();
		break;
		
	case 3:
		this.OnLoading();
		break;

	case 4:
		this.OnDone(CoutomObj);
		break;

	default:
		break;
	}
}


var AjaxRequest = {

    Ready : function(requestURL, Params, returnMethod) {
		var queryString = this.ArrayToQueryString(Params);
	    this.DoRequest(requestURL, queryString, returnMethod);
	    return;
    },
    
    DoRequest : function(requestURL, queryString, returnMethod) {
		var newConnector = new AjaxProducer();
	    newConnector.DoSend(requestURL, queryString, returnMethod);
	    return;
    },

	ArrayToQueryString : function(arrParams){
	
		var queryString = "";

		if(arrParams.length > 0) {
			var arrParameter;
			var paramName;
			var paramValue;
			
			for(i=0; i < arrParams.length; i++) {
				arrParameter = arrParams[i];
				
				paramName = encodeURIComponent(arrParameter[0]);
				paramValue = encodeURIComponent(arrParameter[1]);

				queryString += (i == 0) ? paramName + "=" + paramValue : "&" + paramName + "=" + paramValue;
			}
		}
		return queryString;
	}
}


function autoCompelete(objId) {
    this.enterEvent = false;
    
	this.acOnOff = true;
	this.option = 1;
	this.boxRows = 8;
	
	if (document.all && !window.opera) {
		//log("IE");
		this.boxRowHeight = 20;
	}
	else if (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1) {
		//log("FF");
		this.boxRowHeight = 22;
	}
	else if (navigator.userAgent.indexOf('AppleWebKit/') > -1 && navigator.userAgent.indexOf('Chrome') == -1) {
		//log("safari");
		this.boxRowHeight = 23;
	}
	else if (navigator.userAgent.indexOf('Chrome/') > -1) {
		//log("Chrome");
		this.boxRowHeight = 22;
	}
	
	this.targetWord = null;
	this.timeoutId = null;
	this.rightTimeoutId = null;
	this.requestURL = "/CommonLib/Suggest.asmx/GetSuggestion";
	this.matchedKeywords = [];
	this.promotion = [];
	this.targetId = objId
	this.targetElement = $(objId);
	this.mouseOnList = 0;
	this.currentIndex = -1;
	this.whereTo = null;
	this.caretMove = false;
	this.openlocation = null;
	
	this.helpURL = "http://blog.naeil.incruit.com/81";
	
	this.x = 0;
	this.y = 0;
	this.w = 0;
	this.h = 0;
	
	return this.Construct();
}

autoCompelete.prototype = {

	OnOff : function() {
		this.acOnOff = (this.acOnOff) ? false : true;
		
		if(!this.acOnOff) {
			this.ResetDiv("nothing");
		}
		
		var theCell = $("optionRight");

		if(theCell) {
			theCell.innerHTML = (this.acOnOff) ? "<a href='"+ this.helpURL +"' class='switchPw' target='_blank'>도움말</a> | <a href='#' onclick='ac.OnOff();' class='switchPw'>기능 끄기</a>" : "<a href='"+ this.helpURL +"' class='switchPw' target='_blank'>도움말</a> | <a href='#' onclick='ac.OnOff();' class='switchPw'>기능 켜기</a>";
		}
	},
	
	SwitchOpt : function() {
		this.option = (this.option == 0) ? 1 : 0;
		
		var theCell = $("optionLeft");
		
		if (theCell) {
			theCell.innerHTML = (this.option == 0) ? "&nbsp;&nbsp;<a href='#' onclick='ac.SwitchOpt()' class='switchOpt'>포함하는 모든 단어보기</a>" : "&nbsp;&nbsp;<a href='#' onclick='ac.SwitchOpt()' class='switchOpt'>첫 단어 보기</a>";
		}
	},
	
	GetLocation : function()
	{
		var currentURL = window.location.href;
		var onlyURL;
		var depth;
		
		if(currentURL.indexOf("?") > 0)
		{
			onlyURL = currentURL.split("?")[0];
		}
		else
		{
			onlyURL = currentURL;
		}
		
		onlyURL = onlyURL.split("//")[1];
	
		depth = onlyURL.split("/");
		
		if(typeof(depth) == "object")
		{
			if (depth.length > 2)
			{
				this.openlocation = "310";
			}
			else
			{
				this.openlocation = "300";
			}
		}
	},
	
	GetReady : function() {
		this.GetLocation();
		
		//acTopDiv
		var acTopDiv = $("acTopDiv");
		
		acTopDiv.style.backgroundColor = "#ffffff";
		acTopDiv.style.position = "absolute";
		acTopDiv.style.border = "1px solid rgb(5, 160, 186)";
		acTopDiv.style.top = (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1) ? "24px" : "28px";
		acTopDiv.style.left = (navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1) ? "0px" : "4px";
		acTopDiv.style.width = (document.all && !window.opera) ? "325px" : "323px";
		//acTopDiv.style.height = "auto";
		acTopDiv.style.zIndex = 100;
		
		//acTitleDiv
		var acTitleDiv = document.createElement("div");
		acTitleDiv.id = "acTitleDiv";
		acTitleDiv.style.borderBottom = "2px solid #f5f5f5"
		acTitleDiv.style.position = "relative";
		acTitleDiv.style.top = "0px";
		acTitleDiv.style.left = "0px";
		acTitleDiv.style.width = "100%";
		acTitleDiv.style.display = "none";
		acTopDiv.appendChild(acTitleDiv);
		
		//acTitleTable
		var acTitleTable = document.createElement("table");
		acTitleTable.id = "acTitleTable";
		acTitleTable.cellSpacing = "0px";
		acTitleTable.style.width = "100%";
		acTitleDiv.appendChild(acTitleTable);
		
		var aRow;
		aRow = acTitleTable.insertRow(-0);
		aRow.style.backgroundColor = "#ffffff";
		
		var aCell;
		
		for (var q=0; q <= 3; q++) {
		
			aCell = null;
			aCell = aRow.insertCell(-1);
			aCell.style.color = "#464646";
			aCell.style.fontFamily = "새굴림, Verdana";
			aCell.style.fontSize = "12px";
			aCell.style.cursor = "default";
			aCell.style.padding = "3px 0 0 0";
			aCell.style.height = "21px";
			
			switch (q) {
			case 0 :
				aCell.style.width = "45%";
				aCell.align = "center";	
				aCell.innerHTML = "추천 키워드"
				break;
				
			case 1 :
				aCell.style.width = "24%";
				aCell.align = "right";
				aCell.innerHTML = "검색횟수";
				break;
				
			case 2 :
				aCell.style.width = "24%";
				aCell.align = "right";
				aCell.innerHTML = "예상결과수";
				break;
				
			case 3 :
				aCell.style.width = "5px";
				aCell.align = "center";
				aCell.innerHTML = "&nbsp;";
				break;
			}
		}
		
		//acSuggestDiv
		var acSuggestDiv = $("acSuggestDiv");
		if(!acSuggestDiv) {
		
			acSuggestDiv = document.createElement("div");
			acSuggestDiv.id = "acSuggestDiv";
			//acSuggestDiv.className = (navigator.appName.indexOf("Explorer") > 0) ? "suggestbox": null;
			//acSuggestDiv.style.border = "1px solid #000000";
			acSuggestDiv.style.backgroundColor = "#ffffff";
			acSuggestDiv.style.color = "#666666";
			acSuggestDiv.style.position = "relative";
			acSuggestDiv.style.top = "0px";
			acSuggestDiv.style.left = "0px";
			acSuggestDiv.style.width = "100%";
			//acSuggestDiv.style.overflowX = "hidden";
			//acSuggestDiv.style.overflow  = "auto";
			acSuggestDiv.style.display = "none";
			acTopDiv.appendChild(acSuggestDiv);
		}
		
		//acOptionDiv
		var acOptionDiv = $("acOptionDiv");
		if(!acOptionDiv) {
		
			acOptionDiv = document.createElement("div");
			acOptionDiv.id = "acOptionDiv";
			acOptionDiv.style.backgroundColor = "#f5f5f5";
			acOptionDiv.style.position = "relative";
			acOptionDiv.style.top = "0px";
			acOptionDiv.style.left = "0px";
			acOptionDiv.style.width = "100%";
			acOptionDiv.style.display = "none";

			acTopDiv.appendChild(acOptionDiv);
			
			//acOptionTable
			var acOptionTable = document.createElement("table");
			acOptionTable.id = "acOptionTable";
			acOptionTable.cellPadding = "3px 0 1px 7px";
			acOptionTable.cellSpacing = "0px";
			acOptionTable.style.width = "100%";
			acOptionTable.onmouseover = function(){ac.mouseOnList = 1};
			acOptionTable.onmouseout = function(){ac.mouseOnList = 0};

			acOptionDiv.appendChild(acOptionTable);
			
			var aRow;
			aRow = acOptionTable.insertRow(0);
			
			var aCell;
			for (var q=0; q < 2; q++) {
				aCell = null;
				aCell = aRow.insertCell(-1);
								
				switch (q) {
				case 0 :
					aCell.id = "optionLeft";
					aCell.style.width = "50%";
					aCell.align = "left";
					aCell.innerHTML = (this.option == 0) ? "&nbsp;&nbsp;<a href='#' onclick='ac.SwitchOpt()' class='switchOpt'>포함하는 모든 단어보기</a>" : "&nbsp;&nbsp;<a href='#' onclick='ac.SwitchOpt()' class='switchOpt'>첫 단어 보기</a>";
					break;
				
				case 1 :
					aCell.id = "optionRight";
					aCell.className = "switchPw";
					aCell.style.width = "50%";
					aCell.align = "right";
					aCell.innerHTML = (this.acOnOff) ? "<a href='"+ this.helpURL +"' class='switchPw' target='_blank'>도움말</a> | <a href='#' onclick='ac.OnOff()' class='switchPw'>기능 끄기</a>" : "<a href='"+ this.helpURL +"' class='switchPw' target='_blank'>도움말</a> | <a href='#' onclick='ac.OnOff()' class='switchPw'>기능 켜기</a>";
					break;
				}
			}
		}
	},

	CallLater : function(func, obj) {
		return function() { func.call(obj) }; 
	},
	
	Construct : function() {
		this.targetElement.autoCompelete = this;
		
		this.EventClear = this.CallLater(this.ClearAll, this);
		AddEvent(this.targetElement, "focus", this.CallLater(this.EventSetup, this));
	},
	
	EventSetup : function() {
		//AddEvent(document,    "keydown",  this.KeyCheck);
		//AddEvent(document,    "keypress", this.KeyPressed);
		
		AddEvent(document, "keydown", this.KeyCheck);
		AddEvent(document, "keyup", this.KeyUp);
		AddEvent(document, "keypress", this.KeyPress);
		
		AddEvent(this.targetElement, "blur", this.EventClear);
		this.GetReady();
	},
	
	KeyCheck : function(event) {
	
		event = event || window.event;

		var code = event.keyCode;
		//var obj = GetTargetElement(event).autoCompelete;
		var obj = ac;
		
		switch(code) {
		// UP key
		case 38:
			if (obj.currentIndex > -1) {
				obj.currentIndex --;
				obj.HiLightMove.call(obj, "up");
			}
			stopEvent(event);
			break;
			
		// DOWN key
		case 40:
			var loopUpto;
			if (typeof(obj.matchedKeywords) == "object") {
				loopUpto = (obj.matchedKeywords.length > 8) ? 8 + obj.promotion.length : obj.matchedKeywords.length + obj.promotion.length;
			}
			else {
				if (obj.matchedKeywords.length > 0) {
					loopUpto = (obj.promotion.length > 0) ? 1 + obj.promotion.length : 1;
				}
				else {
					loopUpto = 0;
				}
			}
			
			if (obj.currentIndex < loopUpto - 1) {
				obj.currentIndex ++;
				obj.HiLightMove.call(obj, "down");
			}
			stopEvent(event);
			break;

		// ESC key
		case 27:
			obj.mouseOnList = 0;
			obj.ClearAll.call(obj);
			stopEvent(event);
			return false;
			break;

		// ENTER key
		case 13:
		    if (obj.enterEvent)
		    {
		        obj.HiLightMove.call(obj, "enter");
		    } 
			PressedEnter();
			stopEvent(event);
			return false;
			break;

		// TAB key
		case 9:
			obj.mouseOnList = 0;
			obj.ClearAll.call(obj);
			break;
			
		// \:
		case 220:
			stopEvent(event);
			return false;
			break;
			
		// ELSE 
		default:
			//log('ELSE //'+ code);
			
			$('acTopDiv').style.display = "";
			
			if(obj.acOnOff) {
				obj.caretMove = false;
				obj.timeoutId = setTimeout(function() { obj.RequestMatching.call(obj, code) }, 50);
			}
			else {
				stopEvent(event);
				return false;
			}
			break;
		}
		return true;
	},
	
	KeyUp : function(event) {
	
		event = window.event || event;
		stopEvent(event);
	},
	
	KeyPress : function(event) {
	
		event = window.event || event;
		
		if(this.caretMove)
		{
			stopEvent(event);
		}
	},
	
	RequestMatching : function(codeValue) {
	
		this.targetWord = RemoveSpecialChar(this.targetElement.value.trim());
		
		if(this.timeoutId) {
			clearTimeout(this.timeoutId);
			this.timeoutId = 0;
		}
		else {
			return false;
		}
		
		if(this.targetWord.length > 0) {
			var http = null;
			
			if(typeof XMLHttpRequest != 'undefined') {
				try {
					http = new XMLHttpRequest();
				}
				catch (e) { http = null; }
			}
			else {
				try {
					http = new ActiveXObject("Msxml2.XMLHTTP") ;
				}
				catch (e) {
					try {
						http = new ActiveXObject("Microsoft.XMLHTTP") ;
					}
					catch (e) {	http = null; }
				}
			}

			if(http) {
				
				var completeURL = this.requestURL +"?searchStr="+ encodeURIComponent(this.targetWord) +"&orderBy="+ this.option +"&filter=0&promotion=1&returnCnt=10";
				//log(completeURL);
				
				if(http.overrideMimeType) {
					http.overrideMimeType('text/xml');
				}
				
				http.open("GET", completeURL, true);

				var obj = this;
				var matches;
				var promotions;
				
				http.onreadystatechange = function(n) {
					if(http.readyState == 4) {
						if((http.status == 200) || (http.status == 0)) {
							var xmlDocument = null, tmpinfo = null;

							try {
								xmlDocument = http.responseXML;
								tmpinfo = xmlDocument.getElementsByTagName('string').item(0).firstChild.data;
							}
							catch(e) {
								try {
									xmlDocument = (new DOMParser()).parseFromString(http.responseText, "text/xml");
									tmpinfo = xmlDocument.getElementsByTagName('string').item(0).firstChild.data;
								}
								catch(ee) {}
							}
							
							if (tmpinfo != null) {
								obj.ResetDiv("some");
								
								if (tmpinfo.indexOf('^') != -1) {
								
									matches = tmpinfo.split('^')[0];
									promotions = tmpinfo.split('^')[1];
									
									if (matches.indexOf('|') != -1) {
										obj.matchedKeywords = matches.split('|');
									}
									else {
										obj.matchedKeywords = matches;
									}
									
									if (promotions.indexOf('|') != -1) {
										obj.promotion = promotions.split('|');
									}
									else {
										obj.promotion[0] = promotions;
									}
								}
								else {
									
									if (tmpinfo.indexOf('|') != -1) {
										obj.matchedKeywords = tmpinfo.split('|');
									}
									else {
										obj.matchedKeywords = tmpinfo;
									}
								}
								obj.Rendering.call(obj);
							}
							else {
								obj.ResetDiv("nothing");
							}
						}
					}
				}
				http.send(null);
			}
			return;
		}
		else {
			this.ResetDiv("nothing");
			return false;
		}
	},
	
	ClearAll : function(event) {
		//AddEvent(document, "mousedown", this.mouseOnList = 1)
		//AddEvent(document, "click", this.mouseOnList = 0)
		if (this.mouseOnList == 0) {
			this.ResetDiv("off");
			
			event = window.event || event;
			
			RemoveEvent(document, "keydown", this.KeyCheck);
			RemoveEvent(document, "keyup", this.KeyUp);
			RemoveEvent(document, "keypress", this.KeyPress);
			
			RemoveEvent(this.targetElement, "blur", this.ClearAll);
			
			this.x = 0;
			this.y = 0;
			this.w = 0;
			this.h = 0;
		}
	},
	
	Rendering : function() {
	
		var acSuggestDiv = $("acSuggestDiv");
		
		if (acSuggestDiv) {
			this.x = acSuggestDiv.style.top;
			this.y = acSuggestDiv.style.left;
			this.w = acSuggestDiv.style.width;
			
			if (typeof(this.matchedKeywords) == "object") {
			
				if (this.boxRows <= this.matchedKeywords.length) {
				
					if (this.promotion.length > 0) {
						this.h = acSuggestDiv.style.height = ((this.boxRows + this.promotion.length) * this.boxRowHeight) +"px";
					}
					else {
						this.h = acSuggestDiv.style.height = (this.boxRows * this.boxRowHeight) +"px";
					}
					//this.h = acSuggestDiv.style.height = (this.boxRows * this.boxRowHeight) +"px";
				}
				else {
				
					if (this.promotion.length > 0) {
						this.h = acSuggestDiv.style.height = ((this.matchedKeywords.length + this.promotion.length) * this.boxRowHeight) +"px";
					}
					else {
						this.h = acSuggestDiv.style.height = (this.matchedKeywords.length * this.boxRowHeight) +"px";
					}
					//this.h = acSuggestDiv.style.height = (this.matchedKeywords.length * this.boxRowHeight) +"px";
				}
			}
			else {
			
				if ( this.matchedKeywords.length > 0) {
					if (this.promotion.length > 0) {
						this.h = acSuggestDiv.style.height = ((1 + this.promotion.length) * this.boxRowHeight) +"px";
					}
					else {
						this.h = acSuggestDiv.style.height = (this.boxRowHeight * 2) +"px";
					}				
				}
				//this.h = acSuggestDiv.style.height = (this.boxRowHeight * 2) +"px";
			}
			
			this.x = parseInt(this.x);
			this.y = parseInt(this.y);
			this.w = parseInt(this.w);
			this.h = parseInt(this.h);
		}
		
		var acSubTable = document.createElement("table");
		acSubTable.id = "acSubTable";
		acSubTable.cellSpacing = "0px";
		acSubTable.style.width = "100%";
		(acSuggestDiv) ? acSuggestDiv.appendChild(acSubTable) : "";
		
		var aRow, aCell;
		var eachSet;
		var loopCnt;
		
		loopCnt = (this.matchedKeywords.length > 8) ? 8 : this.matchedKeywords.length;
		
		if (typeof(this.matchedKeywords) == "object") {
			
			//for(var i = 0; i < this.matchedKeywords.length; i++) {
			for(var i = 0; i  < loopCnt; i++) {
				aRow = null;
				aRow = acSubTable.insertRow(-1);
				aRow.style.backgroundColor = "#ffffff";
				aRow.style.cursor = "pointer";
				AddEvent(aRow, "click", this.RowClick);
				aRow.onmouseover = this.RowHiLight;
				aRow.onmouseout  = function(){this.mouseOnList = 0};
				eachSet = eval("(" + this.matchedKeywords[i] + ")");
				//log(eachSet.kw +"//"+ eachSet.SchCnt +"//"+ eachSet.RstCnt);
				
				for (x=0; x <= 4; x++) {
					aCell = null;
					aCell = aRow.insertCell(-1);
					aCell.style.color = "#666666";
					aCell.style.fontFamily = "새굴림, Verdana";
					aCell.style.fontSize = "12px";
					aCell.style.padding = "4px 4px 4px 15px";
					
					switch (x) {
					case 0 :
						aCell.align = "left";
						aCell.style.width = "52%";
						aCell.innerHTML = ItsKeyword(eachSet.kw.toLowerCase(), this.targetWord.toLowerCase());
						break;
						
					case 1 :
						aCell.align = "right";
						aCell.style.width = "23%";
						aCell.innerHTML = (eachSet.SchCnt.length > 0) ? InsertComma(eachSet.SchCnt) : "0";
						break;
						
					case 2 :
						aCell.align = "right";
						aCell.style.width = "25%";
						aCell.innerHTML = (eachSet.RstCnt.length > 0) ? InsertComma(eachSet.RstCnt) : "0";
						break;
						
					case 3 :
						aCell.style.width = "5px";
						aCell.innerHTML = "&nbsp;";
						break;
						
					case 4 :
						aCell.style.width = "0px";
						aCell.innerHTML = (eachSet.GroupCd.length > 0) ? eachSet.GroupCd : "0";
						aCell.style.display = "none";
						break;
					}
				}
			}
		}
		else {
			
			if ( this.matchedKeywords.length > 0) {
			
				aRow = null;
				aRow = acSubTable.insertRow(-1);
				aRow.style.backgroundColor = "#ffffff";
				aRow.style.cursor = "pointer";
				AddEvent(aRow, "click", this.RowClick);
				aRow.onmouseover = this.RowHiLight;
				aRow.onmouseout  = function(){this.mouseOnList = 0};
				//log(this.mouseOnList);
				for (x=0; x <= 4; x++) {
					aCell = null;
					aCell = aRow.insertCell(-1);
					aCell.style.color = "#666666";
					aCell.style.fontFamily = "새굴림, Verdana";
					aCell.style.fontSize = "12px";
					aCell.style.padding = "4px 4px 4px 15px";
					
					eachSet = eval("(" + this.matchedKeywords + ")");
					
					switch (x) {
					case 0 :
						aCell.align = "left";
						aCell.style.width = "52%";
						aCell.innerHTML = ItsKeyword(eachSet.kw.toLowerCase(), this.targetWord.toLowerCase());
						break;
						
					case 1 :
						aCell.align = "right";
						aCell.style.width = "23%";
						aCell.innerHTML = (eachSet.SchCnt.length > 0) ? InsertComma(eachSet.SchCnt) : "0";
						break;
						
					case 2 :
						aCell.align = "right";
						aCell.style.width = "25%";
						aCell.innerHTML = (eachSet.RstCnt.length > 0) ? InsertComma(eachSet.RstCnt) : "0";
						break;
						
					case 3 :
						aCell.style.width = "5px";
						aCell.innerHTML = "&nbsp;";
						break;
						
					case 4 :
						aCell.style.width = "0px";
						aCell.innerHTML = (eachSet.GroupCd.length > 0) ? eachSet.GroupCd : "0";
						aCell.style.display = "none";
						break;
					}
				}
			}
		}
		
		var eachPromotion;
		var linkString;
		
		if (this.promotion.length > 0) {
		
			for (var j=0; j < this.promotion.length; j++) {
			
				aRow = null;
				aRow = acSubTable.insertRow(-1);
				aRow.style.backgroundColor = "#ffffff";
				aRow.style.cursor = "pointer";
				AddEvent(aRow, "click", this.RowClick);
				aRow.onmouseover = this.RowHiLight;
				aRow.onmouseout  = function(){this.mouseOnList = 0};
				
				eachPromotion = eval("(" + this.promotion[j] + ")");
				
				for (var z = 0; z <= 1; z++) {
				
					aCell = null;
					aCell = aRow.insertCell(-1);
					aCell.style.color = "#666666";
					aCell.style.fontFamily = "새굴림, Verdana";
					aCell.style.fontSize = "12px";
					aCell.style.padding = "4px 4px 4px 15px";
					
					switch (z) {
					
					case 0 :
						aCell.colSpan = "4";
						aCell.align = "left";
						aCell.style.width = "100%";
						if (j == 0) {
							aCell.style.borderTop = "1px solid #000000";
						}
						linkString = ItsKeyword(eachPromotion.aWord.toLowerCase(), this.targetWord.toLowerCase());
						aCell.innerHTML = linkString;					
						break;
						
					case 1 :
						aCell.align = "right";
						aCell.style.width = "0px";
						aCell.style.display = "none";
						aCell.innerHTML = eachPromotion.aLink;
						break;
					}
				}
			}
		}
	},
	
	HiLightMove : function(whatKey) {
		
		var acTopDiv = $("acTopDiv")
		var acSubTable = $("acSubTable");
		var acRightDiv = $("acRightDiv")
		var theRows = acSubTable.rows;
		var beforeRow;
		var targetRow;
		var kw;
		var which;
		var isPromotion;
		
		this.caretMove = true;
		
		switch (whatKey) {
		
		case "up":
			
			if (this.currentIndex == -1) {
				beforeRow = theRows[this.currentIndex + 1];
				beforeRow.style.backgroundColor = "#ffffff";
				acTopDiv.style.display = "none";
				if (acRightDiv) {
					document.body.removeChild($("acRightDiv"));
				}
				return false;
			}
			else {
				beforeRow = theRows[this.currentIndex + 1];
				targetRow = theRows[this.currentIndex];
				
				beforeRow.style.backgroundColor = "#ffffff";
				targetRow.style.backgroundColor = "#d6d7e7";
				//acSuggestDiv.scrollTop = targetRow.offsetTop;
			}
			break;
		
		case "down":
			if (this.currentIndex == 0) {
				acTopDiv.style.display = "";
				targetRow = theRows[this.currentIndex];
				
				targetRow.style.backgroundColor = "#d6d7e7";
				//acSuggestDiv.scrollTop = targetRow.offsetTop;
			}
			else {
				beforeRow = theRows[this.currentIndex - 1];
				targetRow = theRows[this.currentIndex];
				
				beforeRow.style.backgroundColor = "#ffffff";
				targetRow.style.backgroundColor = "#d6d7e7";
				//acSuggestDiv.scrollTop = targetRow.offsetTop;
			}
			break;
		case "enter":
		    targetRow = theRows[this.currentIndex];
		    break;		
		}
		
		try {
		    kw = stripHTML(targetRow.firstChild.innerHTML);
		    which = targetRow.lastChild.innerHTML;
		    this.targetElement.value = kw;
		
		    isPromotion = (which.length > 5) ? true : false;
		    if (isPromotion) {
			    this.whereTo = which;
			    //this.MakeRightDiv.call(this, kw, "110");
    			
			    if (acRightDiv) {
				    document.body.removeChild($("acRightDiv"));
			    }
		    }
		    else {
			    this.whereTo = null;
			    this.MakeRightDiv.call(this, kw, which);
		    }
		}    
		catch(e) {
		    this.targetElement.value = $("q").value;
		    }
		this.targetElement.focus();
	},
	
	RowClick : function(event) {
		ac.HiLightMove.call(ac, "enter");
		PressedEnter();
		stopEvent(event);
	},
	
	RowHiLight : function(event) {
		var obj = ac;
		var parentElement;
		var theElement = GetTargetElement(event);
		var JustInCase;
		var isPromotion;
		
		obj.enterEvent = true;
		
		obj.mouseOnList = 1;
		//obj.mouseOnList = 0;
		
		if(theElement.tagName != "TD") {
			JustInCase = theElement.parentNode;
			parentElement = JustInCase.parentNode;
		}
		else if(theElement.tagName == "TD") {
			parentElement = theElement.parentNode;
		}

		if(parentElement.firstChild) {
			var kwCell = parentElement.firstChild;
			var kw = (kwCell) ? stripHTML(kwCell.innerHTML.trim()) : "";
		}
		
		if(parentElement.lastChild) {
			var groupCdCell = parentElement.lastChild;
			var which = (groupCdCell) ? stripHTML(groupCdCell.innerHTML.trim()) : "";
		}
		
		//키를 통해 움직인 커서의 백그라운드 흰색으로
		if (obj.currentIndex > -1) {
			var acSubTable = $("acSubTable");
			var theRows = acSubTable.rows;
			var focusWithKey = theRows[obj.currentIndex];
			
			focusWithKey.style.backgroundColor = "#ffffff";
		}
		
		parentElement.style.backgroundColor = "#d6d7e7";
		
		obj.currentIndex = parentElement.rowIndex;
		obj.targetElement.value = kw;
		
		isPromotion = (which.length > 5) ? true : false;
		if (isPromotion) {
			obj.whereTo = which;
			//obj.MakeRightDiv.call(this, kw, "110");
			
			if ($("acRightDiv")) {
				document.body.removeChild($("acRightDiv"));
			}
		}
		else {
			obj.whereTo = null;
			obj.MakeRightDiv.call(obj, kw, which);
		}		
	},
	
	ResetDiv : function(status) {
		this.currentIndex = -1;
		this.mouseOnList = 0;
		this.matchedKeywords = [];
		this.promotion = [];
		
		var acTitleDiv = $("acTitleDiv");
		var acSuggestDiv = $("acSuggestDiv");
		var acSubTable = $("acSubTable");
		var acOptionDiv = $("acOptionDiv");
		var acRightDiv = $("acRightDiv");
		
		if (acRightDiv) {
			document.body.removeChild(acRightDiv);
		}
		
		switch (status) {
		
		case "some":
			 (acTitleDiv) ? acTitleDiv.style.display = "" : "";
			 (acSuggestDiv) ? acSuggestDiv.style.display = "" : "";
			 (acOptionDiv) ? acOptionDiv.style.display = "" : "";
			
			if (acSubTable) {
				acSuggestDiv.removeChild(acSubTable);
			}
			break;
			
		case "nothing":
			 (acTitleDiv) ? acTitleDiv.style.display = "none" : "";
			 (acSuggestDiv) ? acSuggestDiv.style.display = "none" : "";
			 (acOptionDiv) ? acOptionDiv.style.display = "none" : "";
			
			if (acSubTable) {
				acSuggestDiv.removeChild(acSubTable);
			}
			break;
		
		case "off":
			var acTopDiv = $("acTopDiv");
			if (acTopDiv) {
				acTopDiv.innerHTML = "";
				acTopDiv.style.display = "none";
			}
			break;
		}
	},
	
	MakeRightDiv : function(kw, which) {
	
		var acRightDiv = $("acRightDiv");
		
		var RightDivHeight;
		
		if (!acRightDiv) {
			acRightDiv = document.createElement("div");
			document.body.appendChild(acRightDiv);
			
			acRightDiv.id = "acRightDiv";
			acRightDiv.style.backgroundColor = "#ffffff";
			acRightDiv.style.fontFamily = "새굴림, Verdana";
			acRightDiv.style.position = "absolute";
			acRightDiv.style.border = "solid 1px #05a0ba";
			acRightDiv.style.top = eval(DocTop(this.targetElement) + this.targetElement.offsetHeight - 1) + "px";
			acRightDiv.style.left = eval(DocLeft(this.targetElement) + this.targetElement.offsetWidth) + "px";
			acRightDiv.style.width = "150px";
			acRightDiv.style.zIndex = 100;
			acRightDiv.style.overflowX = "hidden";
			acRightDiv.style.overflow  = "auto";
			acRightDiv.onmouseover = function(){ac.mouseOnList = 1};
		}

		RightDivHeight = (window.attachEvent && !window.opera) ? (this.h + 19) +"px" : (this.h + 14) +"px";
		
		this.rightTimeoutId = setTimeout(function() { Request.Fire(kw, which, RightDivHeight) }, 60);
	}
}


// Stop an event from bubbling up the event DOM
function stopEvent(event) {
	event = event || window.event;

	if(event) {
		if(event.stopPropagation) event.stopPropagation();
		if(event.preventDefault) event.preventDefault();

		if(typeof event.cancelBubble != "undefined") {
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
	return false;
}


// 이벤트 붙임
function AddEvent(obj, eventName, funcRef) {
	if(obj.addEventListener && !window.opera) {
		obj.addEventListener(eventName, funcRef, true);
	}
	else {
		obj["on" + eventName] = funcRef;
	}
}


// 이벤트 제거
function RemoveEvent(obj, eventName, funcRef) {
	if(obj.removeEventListener && !window.opera) {
		obj.removeEventListener(eventName, funcRef, true);
	}
	else {
		obj["on" + eventName] = null;
	}
}


// Offset position from top of the screen
function DocTop(obj) {
	var toreturn = 0;
	
	while(obj) {
		toreturn += obj.offsetTop - (window.opera ? 0 : obj.scrollTop);
		obj = obj.offsetParent;
	}
	return toreturn;
}


// Offset position from left of the screen
function DocLeft(obj) {
	var toreturn = 0;
	
	while(obj) {
		toreturn += obj.offsetLeft - (window.opera ? 0 : obj.scrollLeft);
		obj = obj.offsetParent;
	}
	return toreturn;
}


// 이벤트가 발생한 엘리먼트 반환
function GetTargetElement(event) {
	event = event || window.event;
	return event.srcElement || event.target;
}


// String Object에 method  추가
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}


function PressedEnter() {
	var isPromotions = (ac.whereTo == null) ? false : true;
	var moveToUrl ;
	if (isPromotions) {
		moveToUrl = ac.whereTo.replace(/&amp;/g, "&");
		window.location.href = moveToUrl;
	}
	else {
//		if (ac.currentIndex >= 0) {
//			$("sw").value = "180";
//		}
		SearchAction();
	}
}


var Request = {

	arrParams : function(keyword, filter, height) {
		var arrParams = new Array();

		arrParams[0] = new Array("kw", keyword);
		arrParams[1] = new Array("which", filter);			
		arrParams[2] = new Array("height", height);
		arrParams[3] = new Array("sw", ac.openlocation);
		
		return arrParams;
	},
	
	URI : "/NaeilSuggest/NaeilSuggestRight_ajax.asp",

	Fire : function(keyword, filter, height) {
		if(ac.rightTimeoutId) {
			clearTimeout(ac.rightTimeoutId);
			ac.rightTimeoutId = 0;
		}
		else {
			return false;
		}
	
		var url = this.URI;
		var params = this.arrParams(keyword, filter, height);
		
		if (url && params) {
			AjaxRequest.Ready(url, params, Request);
		}
	},

	DoCallBack : function(returnText) {
		var rightDiv = $("acRightDiv");
		if (rightDiv) {
			rightDiv.innerHTML = returnText;
		}
	}
}


// 매칭되는 단어 색깔 바꾸기
function ItsKeyword(targetword, searchwd) {
	var funnyCase = /^(\+)|^(\+{1,})|(.)(\+)|(.)(\+{1,})/;
	var normalCase;
	var returnFinal;
	var searchword=searchwd.split(' ').join("");

	if (searchword.match(funnyCase) == null) {
		normalCase = new RegExp("("+ searchword +")");
		returnFinal = targetword.replace(normalCase, "<span style='color:#FF6600'>$1</span>");
	}
	else {
		returnFinal = targetword.replace(funnyCase, "<span style='color:#FF6600'>$1$2$3\$4$5\$6</span>");
	}
	return returnFinal;
}


// 2차 필터링 조건으로 검색
function MoveURL(keyword, PName, Num, fieldSearch) {
	if (fieldSearch) {
		window.location = "/search/search.asp?q=" + encodeURIComponent(keyword) +"&"+ PName + "=" + Num +"&fieldSearch=Comp_Nm";
	}
	else {
		window.location = "/search/search.asp?q=" + encodeURIComponent(keyword) +"&"+ PName + "=" + Num;
	}
}


// 특수 문자 처리
function RemoveSpecialChar(target) {
	// original  ==>  return String(target).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
	return String(target).replace(/([.*?^=!:${}()|[\]\/\\\'])/g, '\\$1');
}


//태그 벗겨내기
function stripHTML(string) {
	var strip = new RegExp("[<][^>]*[>]", "gi");
	var changed = string.replace(strip, "");
	
	changed = changed.replace(/&amp;/g, "&");
	return changed;
}


// 세자리 마다 콤마찍기
function InsertComma(n) {
	var reg = /(^[+\-]?\d+)(\d{3})/g;
	if (n == "?") {
		n = "<img src='http://img.incruit.com/naeil/img/emoticon_smile.png' />";
	}
	else {
		n += '';                                      // 숫자를 문자열로 변환

		while (reg.test(n))
			n = n.replace(reg, '$1,$2');
	}
	return n;
}

//Trace
function log(logText) {
    var debugDiv = document.getElementById("debugDiv");

    if (!debugDiv) {
        debugDiv = document.createElement("div");
        debugDiv.id = "debugDiv";

        document.body.appendChild(debugDiv);
    }
    debugDiv.innerHTML += "<p style=\"color:#efefef;\">" + logText + "</p>";
}