var Suggest = 
{
	url:"codelist.php?stockcode=",
	obj: document.getElementById("ajaxinput"),
	ajaxobj: document.getElementById("ajaxdiv"),
	element: null,
	scan: false,
	time: 100,
	thread: -1,
	line: null,
	tip:"代码/简称/拼音",
	flag:false,
	init: function () 
	{
	},
	setObj:function(_obj,_ajaxobj)
	{
	    Suggest.obj = document.getElementById(_obj);
	    Suggest.ajaxobj = document.getElementById(_ajaxobj);
	    if(_obj!="ajaxinput")
	        Suggest.flag = true;	        
	},
	getElement:	function () 
	{   	
	    if (this.element != null) 
		{
			document.body.removeChild(this.element);
			this.element = null;
		}
		
		this.element = document.createElement("script");
		this.element.type = "text/javascript";
		this.element.language = "javascript";
		document.body.appendChild(this.element);
		return this.element;
	},

	check: function () 
	{
		if (Suggest.scan == true) 
		{
			if (Suggest.obj.valueOld != Suggest.obj.value && "" != Suggest.obj.value) 
			{
				Suggest.obj.valueOld = Suggest.obj.value;
				Suggest.getData();
			}
			else if (Suggest.obj.value == "" || Suggest.obj.value == Suggest.tip) 
			{
				Suggest.obj.valueOld = Suggest.obj.value;
				Suggest.clean();
			}
			
			Suggest.thread = window.setTimeout(Suggest.check, Suggest.time);
		}
	},

	getData: function ()
	{
	    
		this.getElement();
		
		if (this.obj.value != null) 
		{
		    if(TrimWhiteSpace(this.obj.value)!="")
			    this.element.src = this.url + TrimWhiteSpace(this.obj.value) + "&type=" + document.getElementById('type').options[document.getElementById('type').options.selectedIndex].value;
		}
	},

	focusObj: function () 
	{
		if (this.obj.value == Suggest.tip) 
		{
			this.obj.style.color = "#000000";
			this.obj.value = '';
		}
		
		if (this.obj.value != "" && this.obj.value != Suggest.tip) 
		{
			Suggest.getData();
		}
		
		Suggest.scan = true;
		Suggest.check();
	},

	blurObj: function () 
	{
		if (this.obj.value == '')
		{
			this.obj.value = Suggest.tip;
			this.obj.style.color = "#999999";
		}
		
		Suggest.scan = false;
		Suggest.clean();
	},

	keyDown: function (evt) 
	{
		if (this.obj.value != "") 
		{
			switch (evt.keyCode) 
			{
				case 38: //up
					
					this.scan = false;
					
					if (this.line == null || this.line.rowIndex == 1) 
					{
						Suggest.setLine(this.obj.suggestBody.rows[this.obj.suggestBody.rows.length - 1]);
					}
					else 
					{
						Suggest.setLine(this.obj.suggestBody.rows[this.line.rowIndex - 1]);
					}
					
					return false;
					break;
				
				case 40: //down
					this.scan = false;
					
					if (this.line == null || this.line.rowIndex == this.obj.suggestBody.rows.length - 1) 
					{
						Suggest.setLine(this.obj.suggestBody.rows[1]);
					}
					else 
					{
						Suggest.setLine(this.obj.suggestBody.rows[this.line.rowIndex + 1]);
					}
					
					return false;
					break;
				
				case 13: //Enter					
					Suggest.blurObj();
					break;
				
				default:
					Suggest.setLine(null);
					this.scan = true;
					Suggest.check();
					break;
			}
		}
		//return true;
	},

	setLine: function (line) 
	{
		if (line != null) 
		{
			if (this.line != null) 
			{
				Suggest.discolorLine(this.line);
			}
			
			this.line = line;
			Suggest.colorLine(line);
			this.obj.value = line.cells[0].innerHTML;
			//code = line.cells[0].innerHTML;
			//markettype = line.cells[3].innerHTML;
		}
		else 
		{
			this.line = null;
		}
	},

	colorLine: 	function (line) 
	{
		line.className = "selected";
	},

	discolorLine:  function (line) 
	{
		line.className = "";
	},

	clean: function () 
	{
	    
		this.ajaxobj.innerHTML = "";
	}
};

var everydata = new Array();


// 显示数据
function showCnt(data) 
{
	Suggest.clean();
	
	if (data.length > 0) 
	{
		var result = new String();
		
		result += '<table width="180" border="0" cellspacing="0" cellpadding="0" class="ajaxtable">';
	
	   	result += '<tr style="background:#cce8f8"><td align="right">代码</td><td align="center">名称</td><td align="center">拼音</td><td style=\"display:none\">mk</td></tr>';

		for (var i in data) 
		{
		    var tempArray = data[i].split("-");
			
			result += '<tr onmouseover="Suggest.colorLine(this)" onmouseout="Suggest.discolorLine(this)" onmousedown="Suggest.setLine(this)">';
			result += '<td>' + tempArray[0] + '</td>';
			result += '<td>' + tempArray[1] + '</td>';
			result += '<td>' + tempArray[2] + '</td>';
			result += '<td style=\"display:none\">' + tempArray[3] + '</td>';
			result += '</tr>';
	    }	
	    
		result += '</table>';
		
		Suggest.ajaxobj.innerHTML = result;
				
		Suggest.obj.suggestBody = Suggest.ajaxobj.childNodes[0];
		
	}
	everydata = new Array();
}

function TrimWhiteSpace(str){
    str = str.replace(/(^\s*)|(\s*$)/g, "");
    return str
}