var asadalSearch = function ()
{
	this.info			= '';
	this.list			= '';
	this.content		= '';
	this.events			= '';
	this.keyword		= '';
	this.htmls			= false;
	this.keyNum			= 0;		//키보드 상하 움직임 위치
	this.totalLeng		= 0;		//검색결과 전체 길이
	this.keywordIdArr	= [];		//검색 결과 키워드 배열

	// 디버그 관련
	this.itemDebug		= false;
	this.itemDebugString= [];
	this.ajaxDebug		= false;
	this.paramDebug		= '';
	this.htmlDebug		= false;

	// 통신할 변수
	this.paramKey		= [];
	this.paramVal		= [];

	// Ajax 통신 타입 및 호스트 등.
	this.type			= 'POST';
	this.dataType		= 'XML';
	this.host			= null;
	this.action			= null;
	this.path			= null;
}

asadalSearch.prototype.setConfig = function ()
{
	this.info	= document.getElementById('suggestInfo');
	this.list	= document.getElementById('suggestList');
	this.msgs	= document.getElementById('testMsg');
}

asadalSearch.prototype.getKeyUpEvent = function (e)
{
	// 차후 디버깅 용으로 사용 가능(이벤트 키 코드 값)
//	this.events		= e;

	var eventKeyCode	= e.keyCode;
	if(eventKeyCode == 13)
	{
	}
	else if(this.htmls == true && (eventKeyCode == 38 || eventKeyCode == 40))
	{
		this.keyNum = parseInt(this.keyNum);

		if(eventKeyCode == 38){
			this.keyNum		= (this.keyNum-1)<1?1:(this.keyNum-1);
			var keyNumOut	= (this.keyNum+1)>this.totalLeng?parseInt(this.totalLeng):(this.keyNum+1);
		}else if(eventKeyCode == 40){
			this.keyNum		= (this.keyNum+1)>this.totalLeng?parseInt(this.totalLeng):(this.keyNum+1);
			var keyNumOut	= (this.keyNum-1)<1?1:(this.keyNum-1);
		}

		if (parseInt(this.totalLeng) != 0)
		{		
			var Keyobj		= document.getElementById('detail_keywordData' + this.keyNum);
			var KeyobjOut	= document.getElementById('detail_keywordData' + keyNumOut);

			this.getOutKeyword(KeyobjOut);
			this.getOverKeyword(Keyobj, this.keywordIdArr[this.keyNum - 1]);
			
			document.search.txtsearch.focus();
		}
	}
	else
	{
		/* initialize */
		this.keyNum		= 0;
		this.totalLeng	= 0;
		if (document.search.txtsearch.value == this.keyword && document.search.txtsearch.value != '')
		{
			this.searchListNone();
			return;
		}
		this.getSearchInfo();
		// 이미 출력되었다면 더이상 출력하지 않음
		if (this.htmls == false)
		{
			this.list.innerHTML	= this.getSearchHtml();
			this.content		= document.getElementById('searchlists');
			this.htmls			= true;
		}
		// IE를 제외한 EVENT 옵션을 적용하기 위함
		this.getSearchList();
	}
}

asadalSearch.prototype.getSearchInfo = function ()
{
	this.setConfig();
	this.info.style.display	= '';
	this.list.style.display	= 'none';

	if (document.search.txtsearch.value)
	{
		this.info.style.display	= 'none';
		this.list.style.display	= '';
	}
}

asadalSearch.prototype.getSearchList = function ()
{
	this.type			= 'POST';
	this.host			= '';
	this.path			= '/~AsaProgram/service/design/search/keyword/search_xml.php';
	this.dataType		= 'XML';

	/** 디버깅을 하기 위해 쓰는 변수 **/
//	this.ajaxDebug		= true;
//	this.itemDebug		= true;
	/** 디버깅을 하기 위해 쓰는 변수 **/

	this.setSearchParam();
	this.getSearchAjax();
}

asadalSearch.prototype.setSearchParam = function ()
{
	// 차후 디버깅 용으로 사용 가능(이벤트 키 코드 값)
	//var evt			= (window.event) ? window.event : this.events;
	//var keycode		= evt.keyCode;

	var keyword		= document.search.txtsearch.value;
	var splitword	= keyword.split(" ");
	var keywords	= splitword[0];

	(this.keyword == '') ? this.setParam('keyword', keywords) : this.setAjaxParamUpdate('keyword', keywords);

	this.keyword	= keywords;
}

asadalSearch.prototype.getSearchAjax = function ()
{
	var ajax	= new asadalAjax('topSearch');
	this.changeState(ajax);
	this.onLoad(ajax);

	this.setAjaxFlag(ajax);
	this.setAjaxParam(ajax);

	ajax.ajaxDebug	= this.ajaxDebug;
	ajax.ajaxSubmit();
}

asadalSearch.prototype.setParam = function (key, val)
{
	this.paramKey.push(key);
	this.paramVal.push(val);
}

asadalSearch.prototype.setAjaxParamUpdate = function (key, val)
{
	var cnt	= this.paramKey.length;
	for (var i=0; i<cnt; i++)
	{
		if (this.paramKey[i] == key) { this.paramVal[i] = val; }
	}
}

asadalSearch.prototype.setAjaxFlag = function (obj)
{
	obj.type		= this.type;
	obj.dataType	= this.dataType;
	obj.host		= this.host;
	obj.path		= this.path;
}

asadalSearch.prototype.setAjaxParam = function (obj)
{
	var cnt	= this.paramKey.length;
	for (var i=0; i<cnt; i++) { obj.setParam(this.paramKey[i], this.paramVal[i]); }
}

asadalSearch.prototype.changeState = function (obj)
{
	obj.onchange	= function (state) { }
}

asadalSearch.prototype.onLoad = function (obj)
{
	var xmlDoc		= '';
	var objs		= this;

	obj.onload		= function (data)
	{
		if (data.xml != '') { objs.content.innerHTML = objs.loadData(data); }
	}
}

asadalSearch.prototype.loadData = function (xmlDoc)
{
	if (this.itemDebug == true)
	{
		this.getItem(xmlDoc);
		this.debugToolOpenWin(xmlDoc, 'itemDebug', '아이템 정보',	this.itemDebugString.join("\n"));
	}

	var string	= [];
	var lists	= [];
	var obj		= xmlDoc.getElementsByTagName('items');
	var cnt		= obj.length;
	var notkey	= '';
	var keyword	= '';
	var keycnt	= '';
	var keyId	= '';

	this.totalLeng	= cnt;

	for (var i=0; i<cnt; i++)
	{
		keyId	= 'detail_keywordData'+(i+1);
		keyword	= decodeURIComponent(obj[i].getAttribute('keyword'));
		keycnt	= decodeURIComponent(obj[i].getAttribute('cnts'));

		this.keywordIdArr[i]	= keyword;
		lists[i]= '<tr height="18" id="'+keyId+'" onmouseover="javascript:getOverKeyword(this, \'' + keyword + '\')" onmouseout="javascript:getOutKeyword(this)" onclick="javascript:getClickKeyword(\'' + keyword + '\')">'
				+ '	<td width="10"><img src="/~AsaProgram/service/image/design/search/arrow_gray.gif" width="10" height="6"></td>'
				+ '	<td width="65%">' + keyword + '</td>'
				+ '	<td align="center">' + keycnt + '</td>'
				+ '</tr>';
	}

	notkey		= '<tr height="18">'
				+ '<td align="center"> &nbsp; 「' + this.keyword + '」 で<br> 検索された結果が <br>ありません。</td>'
				+ '</tr>';

	string[0]	= '<table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#ffffff">';
	string[1]	= (cnt == 0) ? notkey : lists.join("\n");
	string[2]	= '</table>';

	return string.join("\n");
}

asadalSearch.prototype.searchListNone = function ()
{
	this.list.style.display	= 'none';
	this.htmls				= false;
}

asadalSearch.prototype.getOverKeyword = function (obj, k)
{
	document.search.txtsearch.blur();
	document.search.txtsearch.value	= k;
	if (obj)
	{
		obj.style.backgroundColor		= '#DBDBDB';
	}
}

asadalSearch.prototype.getOutKeyword = function (obj)
{
	if (obj)
	{
		obj.style.backgroundColor		= '';
	}
}

asadalSearch.prototype.getClickKeyword = function (k)
{
	this.list.style.display			= 'none';
	this.htmls						= false;
	this.keyword					= k;
}

asadalSearch.prototype.getParamDebug = function ()
{
	var string	= [];
	var cnt		= this.paramKey.length;
	for (var i=0; i<cnt; i++)
	{
		string[i]	= '<tr>' + "\n"
					+ '	<td width="65%" bgcolor="#E4E4E4">&nbsp;<span style="color: blue">' + this.paramKey[i] + '</span></td>' + "\n"
					+ '	<td width="35%" bgcolor="#FFFFFF">&nbsp;<span style="color: green">' + this.paramVal[i] + '</span></td>' + "\n"
					+ '</tr>';
	}
	return string.join("\n");
}

asadalSearch.prototype.getItem = function (obj)
{
	var obj			= obj.getElementsByTagName('items');
	var cnt			= obj.length;
	var string		= [];
	var keyword	= '';
	var keycnt	= '';

	for (var i=0; i<cnt; i++)
	{
		keyword	= decodeURIComponent(obj[i].getAttribute('keyword'));
		keycnt	= decodeURIComponent(obj[i].getAttribute('cnts'));
		if (this.itemDebug == true)
		{
			string[i]	= '<tr>' + "\n"
						+ '	<td bgcolor="#E4E4E4" colspan="2" align="center"><span style="color: blue">' + keyword + '</span></td>' + "\n"
						+ '</tr>' + "\n"
						+ '<tr>' + "\n"
						+ '	<td bgcolor="#FFFFFF" colspan="2">' + "\n"
						+ '		<table border="0" cellpadding="0" cellspacing="0">'
						+ '		<tr>'
						+ '			<td>' + keycnt + '</td>'
						+ '		</tr>'
						+ '		</table>' + "\n"
						+ '	</td>' + "\n"
						+ '</tr>';
		}
	}
	if (this.itemDebug == true) { this.itemDebugString[0] = string.join("\n"); }
}

asadalSearch.prototype.getSearchHtml = function ()
{
	return	  '<table cellpadding="0" cellspacing="0" border="0" width="100%">'
			+ '<tr>'
			+ '	<td width=7><img src="/~AsaProgram/service/image/design/search/st_01.gif" width="7" height="30"></td>'
			+ '	<td background="/~AsaProgram/service/image/design/search/st_02_bg.gif">'
			+ '		<table border="0" cellpadding="0" cellspacing="0" width="100%">'
			+ '		<tr>'
			+ '			<td width="5"><img src="/~AsaProgram/service/image/design/search/arrow.gif"></td>'
			+ '			<td><b>推薦検索/検索数</b></td>'
			+ '			<td width="13" onclick="javascript:searchListNone();">'
			+ '				<img src="/~AsaProgram/service/image/design/search/close.gif">'
			+ '			</td>'
			+ '		</tr>'
			+ '		</table>'
			+ '	</td>'
			+ '	<td width=7><img src="/~AsaProgram/service/image/design/search/st_03.gif" width="7" height="30"></td>'
			+ '</tr>'
			+ '<tr>'
			+ '	<td width=7 background="/~AsaProgram/service/image/design/search/st_04_bg.gif"></td>'
			+ '	<td><div id="searchlists"></div></td>'
			+ '	<td width="7" background="/~AsaProgram/service/image/design/search/st_05_bg.gif"></td>'
			+ '</tr>'
			+ '<tr>'
			+ '	<td width="7"><img src="/~AsaProgram/service/image/design/search/st_06.gif" width="7" height="7"></td>'
			+ '	<td background="/~AsaProgram/service/image/design/search/st_07_bg.gif"></td>'
			+ '	<td width=7><img src="/~AsaProgram/service/image/design/search/st_08.gif" width="7" height="7"></td>'
			+ '</tr>'
			+ '</table>';
}

asadalSearch.prototype.debugToolOpenWin = function (xmlDoc, debugName, debugTitle, debuObj)
{
	var size		= "width=" + 600 + ", height=" + 700 + ", scrollbars=yes";
	var NFW			= window.open("", debugName, size);
	var string		= [];
		string[0]	= '<html>';
		string[1]	= '<head>';
		string[2]	= '<title>디버깅</title>';
		string[3]	= '</head>';
		string[4]	= '<body style="margin: 5px;">';
		string[5]	= '<table border="0" cellpadding="0" cellspacing="1" width="100%" bgcolor="#868686">';
		string[6]	= '<tr>';
		string[7]	= '	<td height="50" align="center" colspan="2" bgcolor="#FFFFFF">&nbsp;<span style="color: blue; size: 12pt;"><b>넘겨주는 값</b></span></td>';
		string[8]	= '</tr>';
		string[9]	= this.getParamDebug();
		string[10]	= '<tr>';
		string[11]	= '	<td height="50" align="center" colspan="2" bgcolor="#FFFFFF">&nbsp;<span style="color: blue; size: 12pt;"><b>' + debugTitle + '</b></span></td>';
		string[12]	= '</tr>';
		string[13]	= debuObj;
		string[19]	= '</table>';
		string[20]	= '</body>';
		string[21]	= '</html>';
	NFW.window.focus();
	NFW.document.write(string.join("\n"));
}

function incSearchKeyword_v3() {
	var frm = document.search;

	displayNone('suggestList', 'none');
	if(!frm.txtsearch.value || frm.txtsearch.value=='홈페이지제작') {
		if(!frm.txtsearch.value) {
			alert('キーワードを正しく入力してください.');
			frm.txtsearch.focus();
			return false;
		} else {
			location = "/template/subindex.htm?code=AS1000003697";
			return false;
		}
	}

	top_cate_no = frm.search.value;

	if(frm.flatrate_mode[1].checked) {
		frm.action = frm.search_flatrate.value;

		frm['detailsearch[mode]'].value = 'flatrate';
		
		if(top_cate_no == '0') {
			frm['detailsearch[top_cate_no][]'].disabled = true;
			frm['use_tab_no'].disabled = true;			
		} else {
			frm['detailsearch[top_cate_no][]'].value = top_cate_no;
			frm['use_tab_no'].value = top_cate_no;
		}
	} else {

		frm['detailsearch[mode]'].value = 'main';

		if(top_cate_no == '0') {
			frm.action = frm.search_main.value;
			frm['detailsearch[top_cate_no][]'].disabled = true;
			frm['use_tab_no'].disabled = true;			
		} else {
			frm.action = frm.search_sub.value;
			frm['detailsearch[top_cate_no][]'].value = top_cate_no;
			frm['use_tab_no'].value = top_cate_no;
		}
	}

	frm.search.disabled = true;
	frm.search_main.disabled = true;
	frm.search_sub.disabled = true;
	frm.search_flatrate.disabled = true;
	frm.flatrate_mode[0].disabled = true;
	frm.flatrate_mode[1].disabled = true;

	frm.submit();
}

var searchObj		= new asadalSearch ();
searchInfo			= function ()		{ searchObj.getSearchInfo();		}
searchKeyUpEvent	= function (e)		{ searchObj.getKeyUpEvent(e);		}
searchListNone		= function ()		{ searchObj.searchListNone();		}
getOverKeyword		= function (e, k)	{ searchObj.getOverKeyword(e, k);	}
getOutKeyword		= function (e)		{ searchObj.getOutKeyword(e);		}
getClickKeyword		= function (k)		{ searchObj.getClickKeyword(k);		}

function displayNone(divname,dis){
	var suggestList = document.all(divname);
	suggestList.style.display=dis;
}

/*********** 상단 검색 selectBox ***********/
var searchSelectValArry;
var searchSelectTxtArry;
var searchSelectPeriodItem;

function initSearchSelect(mode) {

	var i,j,obj,cnt,cntOpt;

	obj		= document.search.search;
	cnt		= obj.length;
	cntOpt	= obj.options.length;

	searchSelectPeriodItem = new Array(42,95,102,2383,2384,103,1462,3874);

	searchSelectValArry = new Array(obj.options.length);
	searchSelectTxtArry = new Array(obj.options.length);

	for(i=0 ; i < cnt ; i++) {	
		searchSelectValArry[i] = obj[i].value;
		searchSelectTxtArry[i] = obj[i].text;
	}

	for(i=0 ; i < cnt ; i++) {
		for(j=0 ; j < cntOpt ; j++) {			
			searchSelectValArry[j] = obj[j].value;
			searchSelectTxtArry[j] = obj[j].text;			
		}
	}

	if(mode == 'flatrate') {
		setSearchSelectPeriod();
	} else {
		setSearchSelectNor();
	}
}

function clearSearchSelect() {

	var i,j,obj,cnt,cntOpt;

	obj		= document.search.search;
	cnt		= obj.length;
	cntOpt	= obj.options.length;

	for(i=0 ; i < cnt ; i++) {
		for(j=0 ; j < cntOpt ; j++) {
			obj.remove(j);
		}
	}
}

function checkSearchSelectPeriodItem(num) {

	var i,j,obj,cnt,cntOpt;

	for(i=0 ; i < searchSelectPeriodItem.length ; i++) {

		if(searchSelectPeriodItem[i] == num) {
			
			return true;
		}
	}

	return false;
}

function setSearchSelectNor() {

	var i,j,obj,cnt,cntOpt;

	clearSearchSelect();

	obj	= document.search.search;						
	
	for(i=0 ; i < searchSelectValArry.length ; i++) {
		objItem = document.createElement('OPTION');
		objItem.value = searchSelectValArry[i];
		objItem.text = searchSelectTxtArry[i];
		
		//솔루션몰 제외
		if(objItem.value != 2385) {
			obj.add(objItem);
		}
	}
	
}

function setSearchSelectPeriod() {

	var i,j,obj,cnt,cntOpt;

	clearSearchSelect();

	obj	= document.search.search;
	
	for(i=0 ; i < searchSelectValArry.length ; i++) {

		if( checkSearchSelectPeriodItem(searchSelectValArry[i]) || i==0) {
			objItem = document.createElement('OPTION');
			objItem.value = searchSelectValArry[i];
			objItem.text = searchSelectTxtArry[i];

			obj.add(objItem);
		}
	}
	
}
/*********** //상단 검색 selectBox ***********/
