var timer;
var selected_id='';
var selected_no = -1;
var keycode;
//flag : set or unset for submit/unsubmit form
var flag = true;
var textfield = '';

function hide(id)
{	
	setTimeout ( "document.getElementById('content_"+id+"').style.visibility='hidden'", 100);
	selected_id= '';
	selected_no=-1;
	}
function Clear()
{
	var arr = document.getElementsByTagName('span');
	for(i=0;i<arr.length;i++)
		if(arr[i].name == 'spn')
			arr[i].innerHTML='';
	
	var arra = document.getElementsByName('spn');
	for(i=0;i<arra.length;i++)
		arra[i].innerHTML='';
	selected_id= '';
	selected_no=-1;
	flag = true;
}

 function handleKeyPress(evt) {
  keycode = (window.event)?event.keyCode:evt.which;
  return true;
  }
 document.onkeydown= handleKeyPress

function keyup_(Object)
{
	//Browser Detection
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	if(browser=='Opera')
		return false;
	//
	textfield = Object;
	document.getElementById('content_'+Object.id).style.visibility='visible';
//Clear();
//alert(keycode);
	handleKeyPress
	if(keycode=='40' || keycode=='38'||keycode=='13'||keycode=='27' ||keycode=='37' ||keycode=='39')
	{
		try{
			if(!document.getElementById('tdId_0'))
				return false;
			if(keycode=='37' || keycode=='39')
			{
				return false;
			}
			if(keycode=='27')
			{
				Clear();
				return false;
			}
			if(keycode=='13')
			{
				if(selected_no != -1)
					flag = false;
				else
				{
						var tmp_id = 'x_' + selected_id;
						Object.value = document.getElementById(tmp_id).value;
						Clear();
				}
				return false;
			}
				
		if(keycode=='40')
		{
			if(selected_no != -1)
			{	
			selected_id = 'tdId_' + selected_no;
			if(document.getElementById(selected_id).style.background== "#ffd3a8" || document.getElementById(selected_id).style.background== "rgb(255, 211, 168) none repeat scroll 0% 0%")
				document.getElementById(selected_id).style.background= "";
			}
			selected_no++;
			selected_id = 'tdId_' + selected_no;
			
			if(!document.getElementById(selected_id)) 
				selected_no = 0;
			selected_id = 'tdId_' + selected_no;
		}
		else if(keycode=='38')
		{
			if(selected_no != -1)
			{	
			selected_id = 'tdId_' + selected_no;
			if(document.getElementById(selected_id).style.background== "#ffd3a8" || document.getElementById(selected_id).style.background== "rgb(255, 211, 168) none repeat scroll 0% 0%")
				document.getElementById(selected_id).style.background= "";
			}
			selected_no--;
			selected_id = 'tdId_' + selected_no;

			if(!document.getElementById(selected_id))
			{
				selected_no = 10;
				while(!document.getElementById(selected_id))
					selected_id = 'tdId_' + --selected_no;
			}
		}
	var tmp_id = 'x_' + selected_id;
	Object.value = document.getElementById(tmp_id).value;
	document.getElementById(selected_id).style.background= "#ffd3a8";
	return false;
	}catch(err){return false;}
	} 
	else
	{
		try{
		clearTimeout ( timer );
		//timer = setTimeout ( showHint(Object.value,'content_'+Object.id,Object.id,<? echo $_GET['cPath']; ?>), 1000);
		function tmp(obj)
		{
			document.getElementById('content_'+Object.id).innerHTML = '';
			$.get('get_sugesstion_search_box.php',{'query': Object.value , 'sid': Math.random()},
			function(result){	
			var tmp = result.replace('</body>', "");
			result  = tmp.replace('</html>', "");

			//alert(result);
			document.getElementById('content_'+Object.id).innerHTML = result;
			clrcolor();
			flag = true;
			return 0;			});
		}
		if(Object.value.length < 3)
			document.getElementById('content_'+Object.id).innerHTML = '';
		else
		timer = setTimeout (tmp(Object) , 1000);
		}catch(err){}
	} 
	return false;
}
$('content_'+Object.id).ajaxStart(function(){ if(document.getElementById('content_'+Object.id))document.getElementById('content_'+Object.id).innerHTML;});
function changeColor1(Object)
{
	clrcolor();
 Object.style.background= "#ffd3a8";
 selected_id = Object.id;
 selected_no = Object.id.replace('tdId_','');
}
function changeColor2(Object)
{
 if(Object.style.background== "#ffd3a8" || Object.style.background== "rgb(255, 211, 168) none repeat scroll 0% 0%")
 Object.style.background= "";
selected_id= '';
selected_no=-1;

}
function gettext(text)
{	Clear();
	textfield.value = text;
	flag = true;
}
function gettext_(text, url)
{	Clear();
	textfield.value = text;
	flag = true;
	window.location.href=url;
}
function clrcolor()
{
	var arr = document.getElementsByTagName('td');
	for(i=0;i<arr.length;i++)
		if(arr[i].name == 'td')
			arr[i].style.background= "";
	
	var arra = document.getElementsByName('td');
	for(i=0;i<arra.length;i++)
		arra[i].style.background= "";
	selected_id= '';
	selected_no=-1;
}
function checkFlag_(){
	if(selected_no == -1)
		flag = true;
	else
	{
			window.location.href= document.getElementById('url_'+selected_no).value;
			//return false;
			 var tmp_id = 'x_' + selected_id;
			 textfield.value = document.getElementById(tmp_id).value;
			 Clear();
			flag = false;
	}
return flag;
}
function checkFlag()
{
	if(selected_no == -1)
		flag = true;
	else
	{
			var tmp_id = 'x_' + selected_id;
			textfield.value = document.getElementById(tmp_id).value;
			Clear();
			flag = false;
	}
return flag;
}
//if(document.getElementById('keywords'))
	//document.getElementById('keywords').focus();