String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"")};
if(typeof open1=='undefined'){
	window.open1 = function(form_id,a_m,a_t,a_url,a_q,a_c){
		var l_form = document.getElementById(form_id);
		l_form.action = a_url;
		l_form.method = a_m;
		l_form.target = a_t;
		l_form.c.value = a_c;
		l_form.q.value = a_q;
		l_form.submit();
	}
}
var Suggest = {
	scan: false,
	url: "http://app.finance.ifeng.com/hq/suggest/s.php?cb=Suggest.showCnt(zqlist);&q=",
	defaultval:"代码、名称、拼音",
	element: null,
	obj: null,
	line: null,
	time: 10,
	selectid: "type",
	codetypepre:'sh|sz|of|hk',
	zqkind:null,
	inputs: null,
	thread: -1,
	
	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";
		this.element.charset = "utf-8";
		document.body.appendChild(this.element);
		return this.element;
	},
	check: function () {
		if (Suggest.scan == true) {
			if (Suggest.obj.valueOld != Suggest.obj.value.trim() && "" != Suggest.obj.value.trim()) {
				Suggest.obj.valueOld = Suggest.obj.value.trim();
				Suggest.getData();
			}
			else if (Suggest.obj.value.trim() == "" || Suggest.obj.value.trim() == Suggest.defaultval) {
				Suggest.obj.valueOld = Suggest.obj.value.trim();
				Suggest.clean();
			}
			Suggest.thread = window.setTimeout(Suggest.check, Suggest.time);
		}
	},
	getData: function () {
		this.getElement();
		var l_keyword = this.obj.value.trim();//.replace(new RegExp("^("+Suggest.codetypepre+")","i"), "");
		if(""!=l_keyword){
			if(Suggest.selectid&&document.getElementById(Suggest.selectid)){
				var zqtype = document.getElementById(Suggest.selectid).value;
				this.element.src = this.url + encodeURI(l_keyword) + "&zqtype="+zqtype;
			}else{
				this.element.src = this.url + encodeURI(l_keyword);
			}
		}
		return ;
	},
	trMouseDown: function (obj){
		var l_code = obj.cells[1].innerHTML.trim();//.replace(new RegExp("^("+Suggest.codetypepre+")","i"), "");
		var l_kind  = obj.cells[3].innerHTML.trim();
		Suggest.go2zqweb(l_code,l_kind);
    },
	go2zqweb:function(a_val,a_kind){
		var l_tmp  = a_kind.split("-");
		var l_m    = l_tmp[0];
		var l_kind = l_tmp[1]; // 如何判断是封闭式基金还是股票
		if("f"==l_kind){
			var l_url = 'http://finance.ifeng.com/app/hq/fund/'+l_m+a_val+'/index.shtml';
		}else if("h"==l_kind){
			//var l_url = 'http://finance.ifeng.com/hkstock/html/hkstock/s'+a_val+'.shtml';
			var l_url = 'http://finance.ifeng.com/app/hq/hkstock/hk'+a_val+'/index.shtml';
		}else{	
			var l_url = 'http://finance.ifeng.com/app/hq/stock/'+l_m+a_val+'/index.shtml';
		}
		
		if(document.getElementById('testsubmit'))window.open1('testsubmit',"get","_blank",l_url,a_val,"1");
		else window.open(l_url);
	},
	focusObj: function (obj) {
		Suggest.obj = obj;
		obj.symbol = null;
		if (obj.value.trim() == Suggest.defaultval) {
			obj.style.color = "#000000";
			obj.value = '';
		}
		if (obj.value.trim() != '' && obj.value.trim() != Suggest.defaultval) {
			Suggest.getData();
		}
		Suggest.scan = true;
		Suggest.check();
	},
	blurObj: function (obj, evt) {
		Suggest.clean(obj);
		if (obj.value.trim() == '') {
			obj.value = Suggest.defaultval;
			obj.style.color = "#999999";
		}
		Suggest.scan = false;
		return false;
	},
	keyDown: function (obj, evt) {
		if (this.obj.value.trim() != "") {
			switch (evt.keyCode) {
				case 38: //up
					this.scan = false;
					if (this.line == null || this.line.rowIndex == 1) {
						this.setLine(this.obj.suggestBody.rows[this.obj.suggestBody.rows.length - 1]);
					}
					else {
						this.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) {
						this.setLine(this.obj.suggestBody.rows[1]);
					}
					else {
						this.setLine(this.obj.suggestBody.rows[this.line.rowIndex + 1]);
					}
					return false;
					break;
				case 13: //Enter
					this.scan = false;
					var l_value = this.obj.value.trim();//.replace(new RegExp("^("+Suggest.codetypepre+")","i"), "");
					if(null != Suggest.zqkind){
						if(l_value.match(/^\d{5}/)){//up down 以后，ab股,港股等
							Suggest.go2zqweb(l_value,Suggest.zqkind);
							Suggest.zqkind = null;
						}else{
							Suggest.gotosearch(l_value,null);
							Suggest.zqkind = null;
						}
					}else if("" != l_value){
						Suggest.gotosearch(l_value,null);
					}
					Suggest.blurObj(obj, evt);
					break;
				default:
					this.setLine(null);
					this.scan = true;
					this.check();
					break;
			}
		}
		return true;
	},
	setLine: function (line) {
		if (line != null) {
			if (this.line != null) {
				this.discolorLine(this.line);
			}
			this.line = line;
			this.colorLine(line);
			Suggest.zqkind = line.cells[3].innerHTML.trim();
			this.obj.value = line.cells[1].innerHTML.trim();
		}
		else {
			this.line = null;
		}
	},
	colorLine: function (line) {
		line.className = "focuson";
	},
	discolorLine: function (line) {
		line.className = "";
	},
	gotosearch: function (a_code,a_kind){
		var value = a_code;
        if (value != Suggest.defaultval && value) {
			if(null==a_kind&&Suggest.selectid&&document.getElementById(Suggest.selectid)){
				var zqtype = document.getElementById(Suggest.selectid).value;
			}else if('stock'==a_kind||'fund'==a_kind){
				var zqtype = a_kind;
			}else{
				var zqtype = "all";
			}
			var l_url = 'http://app.finance.ifeng.com/hq/search.php?type='+zqtype+'&keyword=' + value;
        	if(document.getElementById('testsubmit'))window.open1('testsubmit',"get","_blank",l_url,value,"1");
			else window.open(l_url);
        }
    },
	clean: function () {
		document.getElementById(Suggest.obj.id + "_suggest").innerHTML = "";
	},
	showCnt:function (datastr) {
		if(""!=datastr){
			var l_data_arr = datastr.split("~");
			var length = l_data_arr.length -1;
		}else{
			Suggest.obj.symbol = null;
			Suggest.clean();
			return;	
		}
		Suggest.clean();
		if (length != 0) {
			var result = new String();
			result += '<table class="SuggesTable" border="0" cellpadding="0" cellspacing="0">';
			result += '<tr><td>选项</td><td>代码</td><td>名称</td><td style="display:none;"></td></tr>';
			for (var i in l_data_arr) {
				if(""!=l_data_arr[i]){
					var tempArray1 = l_data_arr[i].split("`");
					var l_key=Suggest.obj.value.replace(/[\.\"\+\'\?\\]/g,"").trim();
					var l_xuan=l_key;
					for(var j=0;j<tempArray1.length;j++){
						var l_tmp = tempArray1[j].match(new RegExp("^("+Suggest.codetypepre+")?"+l_key,"i"));
						if(l_tmp!=null){
							l_xuan = tempArray1[j].replace(new RegExp("^("+Suggest.codetypepre+")?"+l_key,"i"),'$1<span style="color:#F00;">'+l_key+'</span>').replace(new RegExp("^("+Suggest.codetypepre+")","i"), "");
							break;
						}
					}
					
					result += '<tr style="cursor:pointer;" onmouseover="Suggest.colorLine(this);" onmouseout="Suggest.discolorLine(this);" onmousedown="Suggest.trMouseDown(this);"><td>' + l_xuan + '</td><td>' + tempArray1[0].replace(new RegExp("^("+Suggest.codetypepre+")","i"), "") + '</td><td>' + tempArray1[1] + '</td><td style="display:none;">' + tempArray1[0].match(new RegExp("^("+Suggest.codetypepre+")","g")) + '-' + tempArray1[3] + '</td></tr>';
				}
			}
			result += '</table>';
			document.getElementById(Suggest.obj.id + "_suggest").innerHTML = result;
			Suggest.obj.suggestBody = document.getElementById(Suggest.obj.id + "_suggest").childNodes[0];
			if (length == 1) {
				Suggest.obj.symbol = Suggest.obj.suggestBody.rows[1].cells[1].innerHTML;
			}
			else {
				Suggest.obj.symbol = null;
				Suggest.setLine(null);
			}
		}
		else {
			Suggest.obj.symbol = null;
		}
	}
};