var idDivSmartSearch = "divSmartSearch";				// id the div the hien du lieu SmartSearch
var idInputSmartSearch = "txtKeyword";					// id input nhap tu khoa
var idSelectionSmartSearch = "selectionSmartSearch";	// id the <ul> the hien du lieu Smart Search -- (so sanh voi du lieu tra ve bang xjax)
var idDivInputSmartSearch = "divInputSmartSearch";	
var idSpanInputSmartSearch = "spanInputSmartSearch";	
var indexSelection = -1;								// index hien tai cua cac <li> -- (bien nay dung xac dinh <li> nao dang duoc chon)
var intTimeOutSM = 5000;								// thoi gian timout cua SM
var intTimeOutId;										// bien giu Id cua moi lan setTimeout
var intTimeOutGetData = 1000;							// thoi gian goi ham lay du lieu SM
var intTimeOutGetDataId;								// bien giu Id cua moi lan setTimeout SM
var isSmartSearch = false;								// xac dinh user co dang chon SM hay khong
var defKEYDOWN = 40;			
var defKEYUP = 38;
var defKEYENTER = 13;
var defKEYBACKSPACE = 8;
var defKEYESC = 27;
var defKEYTAB = 9;
/* ham set vi tri SM khi user nhan mui ten LEN/ XUONG */
function setIndexSmartSearch(e, id)
{
	idInputSmartSearch = id;
	if (window.event) keyCode = window.event.keyCode;
	else if (e) keyCode = e.which;
	if (keyCode==defKEYDOWN || keyCode==defKEYUP)
	{
		setFocusDivSmartSearchByKey(keyCode);
		return;
	}
	if(keyCode==defKEYBACKSPACE)
	{
		clearTimeout(intTimeOutGetDataId);
		intTimeOutGetDataId = setTimeout("getListSmartSearch("+keyCode+")",intTimeOutGetData);
		return;
	}
	if (keyCode==defKEYTAB)
	{
		setValueSmartSearch(); 
		return;
	}
	if (keyCode==defKEYESC)
	{
		setFocusInput();
		return;
	}
}

/* ham nhan gia tri tu input nhap tu khoa */
function getWordsFromInput(e, id)
{
	idInputSmartSearch = id;	
	var keyCode;
	if (window.event) keyCode = window.event.keyCode;
	else if (e) keyCode = e.which;
	if (!keyCode) return;
	if (keyCode==defKEYESC)
	{
		setFocusInput();
		return;
	}
	if(keyCode!=defKEYENTER && keyCode!=defKEYBACKSPACE)
	{
		clearTimeout(intTimeOutGetDataId);
		intTimeOutGetDataId = setTimeout("getListSmartSearch("+keyCode+")",intTimeOutGetData);
		return;
	}
	else
	{
		setValueSmartSearch(); 
		return;
	}
}

/* ham goi ajax tra ve du lieu SM */
function getListSmartSearch(keyCode)
{
	var txtKeyword = "";
	txtKeyword = document.getElementById(idInputSmartSearch).value;
	/*
	if (keyCode!=defKEYBACKSPACE){
		txtKeyword+=String.fromCharCode(keyCode);
	}else{
		txtKeyword = txtKeyword.substring(0,txtKeyword.length-1)
	}
	*/
	if (txtKeyword)
	{
		/*Dung cho module search*/
		getDataSmartSearch(txtKeyword);
		/*Dung cho module search*/
		return;
	}
	else{
		setFocusInput();
		return ;
	}
}

function getDataSmartSearch(txtKeyword)
{
	if (txtKeyword)
	{
		var eFormSearch = document.getElementById("frmSearch");
		var intType = eFormSearch.type.value*1;
		if (intType<5 && intType>-1 )
		{
			ajax_getSMData(txtKeyword, intType);
		}	
	}
	
}
/* ham the hien SM */
 function showListSmartSearch(strSmartSearch)
{
	if (strSmartSearch)
	{
		clearTimeout(intTimeOutId);
		var e = document.getElementById(idInputSmartSearch);
		var smInput = document.getElementById(idDivSmartSearch);
		if (e && smInput)
		{
			// Set display cho smart search
			smInput.innerHTML = strSmartSearch;
			//smInput.style.width = e.offsetWidth;
			//smInput.style.left = e.offsetLeft;
			//smInput.style.top = e.offsetTop + e.offsetHeight;
			smInput.style.display = "block";
			setDisplayAllSelectBox(0);
		}
		intTimeOutId = setTimeout("setFocusInput('')",intTimeOutSM);
	}
	else
	{
		setFocusInput();
	}
}

/* ham chuyen con tro chon cua user ve cho vung du lieu SM theo PHIM NHAN */
function setFocusDivSmartSearchByKey(intKeyCode)
{
	indexSelection += (intKeyCode==defKEYDOWN) ? 1 : -1;
	setFocusDivSmartSearch();
}
/* ham chuyen con tro chon cua user ve cho vung du lieu SM khi DUNG CHUOT */
function setFocusDivSmartSearchByMouse(index)
{
	if (index>-1)
	{
		indexSelection = index;
		setFocusDivSmartSearch();
	}
}

/* ham chuyen con tro chon cua user ve cho vung du lieu SM theo bien cuc bo indexSelection */
function setFocusDivSmartSearch()
{
	var e = document.getElementById(idSelectionSmartSearch);
	isSmartSearch=true;
	if (e)
	{
		clearTimeout(intTimeOutId);
		var arrLi = e.getElementsByTagName("li");
		var nLength = arrLi.length;
		for(var i=0; i<nLength; i++)
		{
			arrLi[i].className = "unselect";
		}
		if (indexSelection>=nLength){ indexSelection=0; }
		if (indexSelection<0){ indexSelection=nLength-1; }
		var eLi=arrLi[indexSelection]; 
		if (eLi){
			eLi.className="select";
		}
		intTimeOutId = setTimeout("setFocusInput('')",intTimeOutSM);
	}
}

/* ham chuyen con tro chon cua user ve cho vung nhap lieu (vung du lieu SM se mat di) */
function setFocusInput(inputValue)
{
	var smInput = document.getElementById(idDivSmartSearch);
	var eInput = document.getElementById(idInputSmartSearch);
	smInput.style.display="none";
	eInput.value = inputValue ? inputValue : eInput.value;
	indexSelection=-1;
	isSmartSearch=false;
	eInput.focus();
	setDisplayAllSelectBox(1);
	clearTimeout(intTimeOutId);
}

/* ham set gia tri user chon tu SM cho tu khoa */
function setValueSmartSearch()
{
	if (isSmartSearch)
	{
		var e = document.getElementById(idSelectionSmartSearch);
		if(e) 
		{
			var arrLi = e.getElementsByTagName("li");
			var nLength = arrLi.length;
			var txtValue = "";
			for(var i=0; i<nLength; i++)
			{
				if (arrLi[i].className=="select")
				{
					txtValue = arrLi[i].id;
					break;
				}
			}
			if(txtValue)
			{
				setFocusInput(txtValue);
			}
		}
	}
}

/* Dong/ mo tat ca cac select box */
function setDisplayAllSelectBox(value)
{
	var arrSelection = document.getElementsByTagName("select");
	var nLength = arrSelection.length;
	var txtDisplay = (value==1) ? "" : "none";
	for(var i=0; i<nLength; i++)
	{
		arrSelection[i].style.display=txtDisplay;
	}
}
