/*
9 TAB
13 ENTER
27 ESC
38 UP
40 DOWN
*/

function SearchSuggest(id , type ,  width , top , left)
{
	this.id = id;
	this.type = type;
	this.input = $(id+"_"+type);
	if(! this.input)
	{
		this.input = $(id);
	}
	this.width = width;
	if(!document.all)
	{
		this.width = this.width+2;
	}
	
	this.top = top;
	if(this.top == undefined)
	{
		this.top = 23;
	}
	this.left = left;
	if(this.left == undefined)
	{
		this.left = 0;
	}
	
	var html = '<div id="'+this.id+'_suggest" class="sgt_o" style="width:'+this.width+'px; top:'+this.top+'px; left:'+this.left+'px;display:none;z-index:9999;"></div>';
	
	this.input.parentNode.id = this.id+"_parent";
	new Insertion.After(this.id+"_parent" , html);

}

function searchsuggest_inputOnfocus(thisobj , evnt) 
{  
	var arr = thisobj.id.split("_");
	var id = arr[0];
	var type = arr[1];
	if(type == undefined)
	{
		type = id;
	}
	
	//thisobj.className='it2';
	if(thisobj.value == "" && 1 == 2)
	{
		var namelist = {"keyword":"请输入产品名称关键字","keys":"请输入产品名称关键字"};
		var html = '<div id="'+id+'_suggest_word" style="width:'+(parseInt($(id+"_suggest").style.width)-10)+'px; border-bottom:1px solid #eee; padding:0px 5px; color:#666; background:#eee;" >';
		html += '请输入'+namelist[type];
		html += '</div>';
		html += '<iframe src="javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px; width:410px; height:' + (1 * 20 - 5) + 'px; z-index:9999; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';" frameborder=0></iframe>';
		$(id+"_suggest").style.display = "block";
		$(id+"_suggest").parentNode.style.position = "relative";
		$(id+"_suggest").style.background = "#eee";
		$(id+"_suggest").innerHTML = html;
	}
	
	
}

function searchsuggest_inputOnblur(thisobj , evnt) 
{
	var arr = thisobj.id.split("_");
	var id = arr[0];
	var type = arr[1];
	if(type == undefined)
	{
		type = id;
	}
	
	//thisobj.className='it1';
	h(id+"_suggest");
	$(id+"_suggest").parentNode.style.position = "relative";
}

function searchsuggest_inputOnkeyup(thisobj , evnt , tradeId) 
{ 
	//alert($(tradeId).value);
	var arr = thisobj.id.split("_");
	var id = arr[0];
	var type = arr[1];
	if(type == undefined)
	{
		type = id;
	}
	
	var suggestid = id+"_suggest";
	var num=0;
	var obj;
	var hotobj = "";
	var hotnum = "";

	while((obj = $(suggestid+"_"+num)) != null)
	{
		if(obj.className == "sgt_on")
		{
			hotobj = obj.id;
		}
		num++;
	}
		
	if(hotobj != "")
	{
		var arr = hotobj.split('_');
		hotnum = parseInt(arr[2]);
		
	}
	
	//alert(evnt.keyCode);
	//DOWN键
	if(evnt.keyCode == 40)
	{
		if($(id+"_suggest_0") != null && $(id+"_suggest").style.display == "block")
		{
			if(hotobj == "")
			{
				$(id+"_suggest_0").className = "sgt_on";
				
				if ('function' == typeof(searchsuggest_seling))
				{
					searchsuggest_seling($(id+"_suggest_0").innerHTML);
				}
				else
				{
					var arra = $(id+"_suggest_0").innerHTML.split('-----');
					thisobj.value = arra[0];
				}
			}
			else
			{
				var nextobj = suggestid+"_"+ (hotnum == num-1 ? 0 : hotnum+1);
				$(hotobj).className = "sgt_of";
				$(nextobj).className = "sgt_on";
				if ('function' == typeof(searchsuggest_seling))
				{
					searchsuggest_seling($(nextobj).innerHTML);
				}
				else
				{
					var arra = $(nextobj).innerHTML.split('-----');
					thisobj.value = arra[0];
				}
				
			}
			return false;
		}
	}
	//UP键
	else if(evnt.keyCode == 38)
	{
		if($(id+"_suggest_"+(num-1)) != null && $(id+"_suggest").style.display == "block")
		{
			if(hotobj == "")
			{
				$(id+"_suggest_"+(num-1)).className = "sgt_on";
				if ('function' == typeof(searchsuggest_seling))
				{
					searchsuggest_seling($(id+"_suggest_"+(num-1)).innerHTML);
				}
				else
				{
					var arra = $(id+"_suggest_"+(num-1)).innerHTML.split('-----');
					thisobj.value = arra[0];
				}
			}
			else
			{
				var preobj = suggestid+"_"+ (hotnum == 0 ? num-1 : hotnum-1);
				$(hotobj).className = "sgt_of";
				$(preobj).className = "sgt_on";
				if ('function' == typeof(searchsuggest_seling))
				{
					searchsuggest_seling($(preobj).innerHTML);
				}
				else
				{
					var arra = $(preobj).innerHTML.split('-----');
					thisobj.value = arra[0];
				}
			}
		}
	}
	else if (evnt.keyCode != 9 && evnt.keyCode != 27)	//27:Esc 
	{
		if (evnt.keyCode == 13)	//13:回车
		{
			if ($(id+"_suggest").style.display == "block")
			{
				if ('function' == typeof(searchsuggest_selafter))
				{
					searchsuggest_selafter($(id+"_suggest_"+hotnum).innerHTML);
				}
				else
				{
					var arra = $(id+"_suggest_"+hotnum).innerHTML.split('-----');
					thisobj.value = arra[0];
				}
				$(id+"_suggest").style.display = "none";
				
			}
			else
			{
				searchsuggest_ajax_submit(thisobj.value , id , type , $(tradeId).value);
			}
		}
		else
		{	
			searchsuggest_ajax_submit(thisobj.value , id , type , $(tradeId).value);
		}
	}
	return false;
}


function searchsuggest_inputOnkeydown(thisobj , evnt) 
{
	var arr = thisobj.id.split("_");
	var id = arr[0];
	if (evnt.keyCode == 13)
	{
		return false;
	}
	else if(evnt.keyCode == 9 || evnt.keyCode == 27)
	{
		$(id+"_suggest").style.display = "none";
	}
	return true;
}

function searchsuggest_ajax_submit(text , id , type , tradeId)
{

	commonManager.getSearchWordsArray(text,type,tradeId,{callback:function(data){searchsuggest_ajax_show(data, id, text);}});
}

function searchsuggest_ajax_show(arr, id, text)
{
	
	var arrLength = arr.length;
	if(arrLength == 0)
	{
		$(id+"_suggest").style.display = "none";
		return;
	}
	
	//if(arrLength == 1 && arr[0] == text)
	//{
	//	$(id+"_suggest").style.display = "none";
	//	return;
	//}
	
	$(id+"_suggest").style.display = "block";
	$(id+"_suggest").parentNode.style.position = "relative";
	$(id+"_suggest").style.background = "#fff";
	$(id+"_suggest").style.border = "4px";
	var html = "";
	var arrtmp;
	
	for(var i=0 ; i<arrLength ; i++)
	{
		arrtmp = arr[i];
		html += '<div id="'+id+'_suggest_'+i+'" class="sgt_of" style="float:left;font-size:12px;width:'+(parseInt($(id+"_suggest").style.width)-10)+'px" onmouseover="javascript:return searchsuggest_suggestOnmouseover(this);" onmousedown="javascript:searchsuggest_suggestOnclick(this);" onclick="searchsuggest_suggestOnclick(this)">'+arrtmp[0]+'-----<font style="color:#009933;">约'+arrtmp[1]+'条记录</font></div>';
	}
	//html += '<iframe src="javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px;border:1px solid red; width:410px; height:' + (arrLength * 20 + 20) + 'px; z-index:9999; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';" frameborder=1></iframe>';
	
	$(id+"_suggest").innerHTML = html;

	if($(id+"_suggest_0") != null && $(id+"_suggest").style.display == "block")
	{
		$(id+"_suggest_0").className = "sgt_on";
	}

}
function changevalue(id,value){
	document.getElementById(id).value=value;
}

function searchsuggest_suggestOnclick(thisobj)
{
	
	var objid = thisobj.id;
	var arr = objid.split('_');
	var id = arr[0];

	if ('function' == typeof(searchsuggest_selafter))
	{
		searchsuggest_selafter(thisobj.innerHTML);
	}
	else
	{
		var childNodes = extractNodes(thisobj.parentNode.parentNode);
		
		childNodes = extractNodes(childNodes[0]);

		var arra = thisobj.innerHTML.split('-----');

		//childNodes[0].value = arra[0];//新注释掉的,原版有
		
		document.getElementById(arr[0]).value=arra[0];
	}
	//thisobj.parentNode.parentNode.childNodes[0].childNodes[0].value = thisobj.innerHTML;	

	$(id+"_suggest").style.display = "none";
	
	return false;
	
}

function searchsuggest_suggestOnmouseover(thisobj)
{
	var objid = thisobj.id;
	var arr = objid.split('_');
	var id = arr[0];
	var suggestid = arr[0]+'_'+arr[1];
	var objnum = arr[2];

	var num = 0;
	var obj;
	while((obj = $(suggestid+"_"+num)) != null)
	{
		obj.className = "sgt_of";	
		num ++;
	}

	thisobj.className = "sgt_on";

	return false;
}

function s(s){if (!$(s)) return; $(s).style.display = "block";}
function h(s){if (!$(s)) return; $(s).style.display = "none";}

function extractNodes(pNode)
{
	if(pNode.nodeType == 3)
	{
		return null;
	}
	var node,nodes = new Array();
        for(var i=0 ; node= pNode.childNodes[i] ; i++)
        {
		if(node.nodeType == 1)
		{
			nodes.push(node);
		}
        }
        return nodes;
}

