	if (typeof window.Suggestlk2 == "undefined") {
		var Suggestlk2 = {};
	}

	Suggestlk2.$$$ = function(id) {
		return document.getElementById(id);
	};
	
	Suggestlk2.defaults = {
		defaultval:'基金名称/代码/拼音首字母',
		callbackhandle:function(){},
		divHint:{width:'200'},
		valType:0,
		tdhead:{"名称":"40%","代码":"30%","拼音":"30%"}
	};
	
	Suggestlk2.extend = function() {
		var target = arguments[0], a = 1;
		if ( arguments.length == 1 ) {
			target = this;
			a = 0;
		}
		var prop;
		while ( (prop = arguments[a++]) != null )
			for ( var i in prop ) target[i] = prop[i];
		return target;
	};
	
	Suggestlk2.Loader = function (url, mark, interval, callback, value) {
		this.url = url;
		this.mark = mark;
		this.interval = interval;
		this.callback = callback;
		this.value = value;
		this.container = null;
		this.thread = -1;
		this.init = function () {
			if (this.container) {
				return;
			}
			this.container = document.createElement("div");
			this.container.style.display = "none";
			//this.container.style.z-index = 1000;
			document.body.appendChild(this.container);
		};
		this.start = function () {
			this.stop();
			this.load();
			this.thread = setInterval(this.load, this.interval);
		};
		this.stop = function () {
			if (this.thread != -1) {
				clearInterval(this.thread);
				this.thread = -1;
			}
		};
		this.load = function () {
			var loader = arguments.callee.loader;
			var element = document.createElement("script");
			element.type = "text/javascript";
			element.language = "javascript";
			element.charset = "utf-8";
			element.src = loader.url.replace(loader.mark, (new Date()).getTime());
			element[document.all ? "onreadystatechange" : "onload"] = loader.unload;
			element.callback = loader.callback;
			element.value = loader.value;
			loader.init();
			loader.container.appendChild(element);
		};
		this.load.loader = this;
		this.unload = function () {
			if (document.all && this.readyState != "loaded" && this.readyState != "complete") {
				return;
			}
			if (this.callback) {
				this.callback(this.value);
			}
			this.callback = null;
			this.value = null;
			this[document.all ? "onreadystatechange" : "onload"] = null;
			this.parentNode.removeChild(this);
		};
	};

	Suggestlk2.Suggest = function (cfg) {
		this.config = {};
		
		this.config = Suggestlk2.extend(this.config,Suggestlk2.defaults);
		this.config = Suggestlk2.extend(this.config,cfg?cfg:{});
		this.stringKeySpliter = ":"
		this.stringRecordSpliter = "|";
		this.stringSystemKeys = "EJ,s,sh,sz,0,1,2,3,4,5,6,7,8,9";
		this.arraySystemKeys = new Array();
		this.arrayPrepareKeys = new Array();
		this.intPrepareKeysMaxLength = 50;
		this.stringData = new String();
	
		this.afterHandle = this.config.afterHandle?this.config.afterHandle:function(){};
		
		this.processData = function (valueKey) {
			var suggest = arguments.callee.suggest;
			suggest.stringData = suggest.stringRecordSpliter + window[valueKey];
			var arrayStringSystemKeys = suggest.stringSystemKeys.split(",");
			for (var i =0; i < arrayStringSystemKeys.length; i++) {
				var stringNearestData = suggest.getNearestData(arrayStringSystemKeys[i]);
				var arrayResult = stringNearestData == "" ?  suggest.getResult(suggest.stringData, arrayStringSystemKeys[i]) : suggest.getResult(stringNearestData, arrayStringSystemKeys[i]);
				arrayResult = arrayResult == null ? new Array() : arrayResult;
				suggest.arraySystemKeys.push(new Array(arrayStringSystemKeys[i], arrayResult.join("")));
			}
		};
		this.processData.suggest = this;
		
		var stringUrl = this.config.url;
		if (stringUrl!=""){	
			var a = stringUrl.lastIndexOf("/");
			var b = stringUrl.lastIndexOf(".");
			var c = this.config.dataname?this.config.dataname:stringUrl.substr(a+1,b-a-1);
			this.loader = new Suggestlk2.Loader(stringUrl, "@RANDOM@", 0, this.processData, c);
			this.loader.load();
		}
		
		this.getOffsetPos = function (element) {
			var flag = element.tagName.toUpperCase() == "INPUT" ? true : false;
			var posTop = 0, posLeft = 0;
			var offelement;
			if(this.config.offsetelement) {
				offelement =  Suggestlk2.$$$(this.config.offsetelement);
			}
			do {
				posTop += element.offsetTop || 0;
				posLeft += element.offsetLeft || 0;
				element = element.offsetParent;
			} while (element && element != offelement);
			if (navigator.appVersion.indexOf("MSIE 6.0") != -1 && flag) {
				posLeft++;
			}
			
			return [posLeft, posTop];
		};
		this.getResult = function (stringData, stringKey) {
			var stringRegExpSystem = "$()*+.[?\^{|";
			var stringKeySpliter = (stringRegExpSystem.indexOf(this.stringKeySpliter) < 0 ? "" : "\\") + this.stringKeySpliter;
			var stringRecordSpliter = (stringRegExpSystem.indexOf(this.stringRecordSpliter) < 0 ? "" : "\\") + this.stringRecordSpliter;
			var arrayMatchResult = stringData.match(new RegExp("" + stringRecordSpliter + (isNaN(parseInt(stringKey)) ? "" : "(s[hz])?") + stringKey + "[^\\" + stringRecordSpliter + "|" + stringKeySpliter + "]*" + stringKeySpliter + "[^\\" + stringRecordSpliter + "|" + stringKeySpliter + "|\n]*", "igm"));
			return arrayMatchResult == null ? new Array() : arrayMatchResult;
		};
		
		this.getNearestData = function (stringKey) {
			if (this.arrayPrepareKeys.length == 0) {
				return new String();
			}
			var arrayContainers = new Array();
			for (var i =0; i < this.arraySystemKeys.length; i++) {
				if (this.arraySystemKeys[i][0] == stringKey) {
					return this.arraySystemKeys[i][1];
				}
				if (stringKey.match(new RegExp("^" + this.arraySystemKeys[i][0], "igm")) != null) {
					arrayContainers.push(this.arraySystemKeys[i]);
				}
			}
			for (var i = 0; i < this.arrayPrepareKeys.length; i++) {
				if (this.arrayPrepareKeys[i][0] == stringKey) {
					return this.arrayPrepareKeys[i][1];
				}
				if (stringKey.match(new RegExp("^" + this.arrayPrepareKeys[i][0], "igm")) != null) {
					arrayContainers.push(this.arrayPrepareKeys[i]);
				}
			}
			if (arrayContainers.length == 0) {
				return new String();
			}
			else {
				arrayContainers.sort(
					function (arrayA, arrayB) {
						return arrayB[0].length - arrayA[0].length;
					}
				);
				return arrayContainers[0][1];
			}
		};
		
		this.getQuickResult = function (stringKey) {
			stringKey = stringKey.split(this.stringKeySpliter).join("").split(this.stringRecordSpliter).join("");
			if (stringKey == "") {
				return new Array();
			}
			var stringNearestData = this.getNearestData(stringKey);
			var arrayResult = stringNearestData == "" ?  this.getResult(this.stringData, stringKey) : this.getResult(stringNearestData, stringKey);
			arrayResult = arrayResult == null ? new Array() : arrayResult;
			var booleanIsInSystemKeys = false;
			for (var i = 0; i < this.arraySystemKeys.length; i++) {
				if (this.arraySystemKeys[i][0] == stringKey) {
					booleanIsInSystemKeys = true;
					break;
				}
			}
			var booleanIsInPrepareKeys = false;
			for (var i = 0; i < this.arrayPrepareKeys.length; i++) {
				if (this.arrayPrepareKeys[i][0] == stringKey) {
					booleanIsInPrepareKeys = true;
					break;
				}
			}
			if (!booleanIsInSystemKeys && !booleanIsInPrepareKeys) {
				this.arrayPrepareKeys.push(new Array(stringKey, arrayResult.join("")));
				if (this.arrayPrepareKeys.length > this.intPrepareKeysMaxLength) {
					this.arrayPrepareKeys.sort(
						function (arrayA, arrayB) {
							return arrayA[0].length - arrayB[0].length;
						}
					);
					this.arrayPrepareKeys.pop();
				}
			}
			return arrayResult;
		};
		this.load = function (stringKey) {
			if (stringKey.indexOf(",") != -1 && stringKey.indexOf(",") != 0 && stringKey.indexOf(",") != stringKey.length - 1) {
				var arrayStringKey = stringKey.split(",");
				stringKey = arrayStringKey[arrayStringKey.length - 1];
			}
			if (stringKey.indexOf("\\") != -1) {
				return new Array();
			}
			var stringRegExpSystem = "$()*+.[?^{|";
			for (var i = 0; i < stringRegExpSystem.length; i++) {
				if (stringKey.indexOf(stringRegExpSystem.substr(i, 1)) != -1) {
					return new Array();
				}
			}
			var stringMarket = new String();
			var arrayQuickResult = this.getQuickResult(stringKey);
			arrayQuickResult.length = arrayQuickResult.length > 10 ? 10 : arrayQuickResult.length;
			return arrayQuickResult;
		};
		this.show = function () {
			var element = arguments.callee.element;
			if (!element.booleanScan) {
				return;
			}
			if (element.value != element.suggest.config.defaultval) {
				
				element.line = null;
				element.stringLastValue = element.value;
				var arrayResult = element.suggest.load(element.value);
				if (arrayResult.length > 0) {
					element.divHint.style.display = "";
					var arrayPosition = element.suggest.getOffsetPos(element);
					var tableContainer = document.createElement("ul");
					var liChild=document.createElement('li');
					liChild.setAttribute("class","tit");
					tableContainer.appendChild(liChild);
					var searchType = Suggestlk2.getSearchType2(element.value);
					var searchType2 = Suggestlk2.getSearchType2(element.value);
					var valType = element.suggest.config.valType;
					var tmpUnshowType;
					if (typeof element.suggest.config.unshowType != "undefined") {
						tmpUnshowType = element.suggest.config.unshowType;
					}
					/** 是否首条默认选中 **/
					var selectFirst = false;
					if (typeof element.suggest.config.selectFirst != "undefined") {
						selectFirst = element.suggest.config.selectFirst;
					}
					
					Suggestlk2.proccessTrHead(liChild,searchType,element.suggest.config.tdhead);
					
					var wraptype = Suggestlk2.getWrapType(element.value);
					for (var i = 0; i < arrayResult.length; i++) {
						if (isNaN(parseInt(i))) {
							continue;
						};
						var arrayRecord = arrayResult[i].replace("|", "").split(":");
						var arrayCodeAndName = arrayRecord[1].split("^");
						arrayCodeAndName[0] = arrayRecord[0];
						var liChild2=document.createElement('li');
						
						if (i==0 && selectFirst){
							liChild2.style.background='#c4d4e5';
							/**是否IE浏览器*/
							if(navigator.appName == "Microsoft Internet Explorer"){
								liChild2.setAttribute("className","selectedLine");
							}else{
								liChild2.setAttribute("class","selectedLine");
							}
						}
						liChild2._mouseover = function(){
							this.style.background='#c4d4e5';
						};
						liChild2._mouseout = function(){
							this.style.background='';
						};
						if(/gecko/.test(window.navigator.userAgent.toLowerCase())) {
							liChild2.addEventListener("mouseover",function(){
								this.style.background='#c4d4e5';
							},false);
							liChild2.addEventListener("mouseout",function(){
								this.style.background='';
							},false);
						}
						if(/msie/.test(window.navigator.userAgent.toLowerCase())) {
							liChild2.attachEvent("onmouseover",(function(o){
								return function(){o._mouseover();};
							})(liChild2));
							liChild2.attachEvent("onmouseout",(function(o){
								return function(){o._mouseout();};
							})(liChild2));
						}

						tableContainer.appendChild(liChild2);
						
						var recordarray = new Array();
						var len = arrayCodeAndName.length;
						
						for(var j=c=0;j<len;j++){
							var sp = document.createElement('span');
							if (typeof tmpUnshowType != "undefined") {
								if(j == 0) {
									sp.className="four";
								}
								if(j == 1) {
									sp.className="five";
								}
								if(c != tmpUnshowType) {
									sp.innerHTML=arrayCodeAndName[(c+wraptype)%len];
									liChild2.appendChild(sp);
								}
									
							}else {
								if(j == 0) {
									sp.className="one";
								}
								if(j == 1) {
									sp.className="two";
								}
								if(j == 2) {
									sp.className="three";
								}
								sp.innerHTML=arrayCodeAndName[(c+wraptype)%len];
								liChild2.appendChild(sp);
							}
							c++;
						}
							if (searchType==2){
									//基金名称
								if(valType==0) {
									liChild2.stringFullCode = arrayCodeAndName[1];
								}
								if(valType==1) {
									liChild2.stringFullCode = arrayCodeAndName[2];
								}
								if(valType==2) {
									liChild2.stringFullCode = arrayCodeAndName[0];
								}
							}else if (searchType==1){//拼音
								if(valType==0) {
									liChild2.stringFullCode = arrayCodeAndName[2];
								}
								if(valType==1) {
									liChild2.stringFullCode = arrayCodeAndName[0];
								}
								if(valType==2) {
									liChild2.stringFullCode = arrayCodeAndName[1];
								}
							}else{//代码
								if(valType==0) {
									liChild2.stringFullCode = arrayCodeAndName[0];
								}
								if(valType==1) {
									liChild2.stringFullCode = arrayCodeAndName[1];
								}
								if(valType==2) {
									liChild2.stringFullCode = arrayCodeAndName[2];
								}
							}
						
						//alert(trRecord.stringFullCode);
						
						//trRecord.stringFullCode = recordarray[valType];
						liChild2.inputTarget = element;
						liChild2.onmouseover = function () {
							this.inputTarget.overLine = this;
							this.className = this.inputTarget.line == this ? "overSelectedLine" : "overLine";
						};
						liChild2.onmouseout = function () {
							this.inputTarget.overLine = null;
							this.className = this.inputTarget.line == this ? "selectedLine" : "";
						};
						liChild2.onmousedown = function () {
							this.inputTarget.booleanScan = true;
							this.inputTarget.setLine(this);
							this.inputTarget.suggest.afterHandle();
						};
					}
					element.divHint.innerHTML = "";
					element.divHint.appendChild(tableContainer);
					
					if (window.navigator.appVersion.indexOf("MSIE 6.0") > 0) {
						if(document.getElementById("ifmmask")) {
							if(document.getElementById("ifmmask")) {
								document.getElementById("ifmmask").style.height=tableContainer.scrollHeight+46+"px";	
							}
						}
					}
					element.tableHint = tableContainer;
				}
				else {
					element.hideHit();
					element.divHint.innerHTML = "";
					element.tableHint = null;
				}
			}
		};
		this.bind = function (element) {
			this.ele = element;
			element.suggest = this;
			element.show = this.show;
			element.show.element = element;
			element.intThread = -1;
			element.arrayData = new Array();
			if (element.value=="")
				element.value = this.config.defaultval;
			element.style.color = "#999999";
			element.booleanScan = true;
			element.autocomplete = "off";
			var divDataTable = document.createElement("div");
			//var divDataTable = $("divDataTable");
			divDataTable.style.display = "none";
			//divDataTable.style.position ="absolute";
			if(this.config.seglocation) {
				var li = document.getElementById(this.config.seglocation);
				if(li) {
					li.parentNode.insertBefore(divDataTable, li);	
				}
			}else {
				element.parentNode.insertBefore(divDataTable, element);	
			}
			element.divHint = divDataTable;
			element.tableHint = null;
			element.line = null;
			element.overLine = null;
			divDataTable.className = "FloatDivsearch";
			element.onfocus = function () {
				if (this.value == this.suggest.config.defaultval || this.value == "代码输入错误") {
					this.value = "";
					this.style.color = "";
				};
				this.stringLastValue = this.value;
				if (this.divHint.innerHTML != "") {
					this.divHint.style.display = "";
					var arrayPosition = this.suggest.getOffsetPos(this);
				}
				this.intThread = setInterval(this.show, 10);
			};
			element.getlielement = function (index) {
				if(this.tableHint && this.tableHint.childNodes.length>index )
					return this.tableHint.childNodes[index];
				return -1;
			};
			element.getliIndex = function (line) {
				if(this.tableHint && this.tableHint.childNodes.length>=0 ) {
					for (var i = 0; i<this.tableHint.childNodes.length; i++) {
						if(line == this.tableHint.childNodes[i]) {
							//line.style.background='#c4d4e5';
							return i;
						}
					}
				}
				return -1;
				
			};
			element.getliCount = function () {
				if(this.tableHint && this.tableHint.childNodes.length>=0 ) {
					return this.tableHint.childNodes.length;
				}
				return -1;
				
			};
			element.setlinecolor = function (line) {
				if(this.tableHint && this.tableHint.childNodes.length>=0 ) {
					for (var i = 0; i<this.tableHint.childNodes.length; i++) {
						if(line == this.tableHint.childNodes[i]) {
							this.tableHint.childNodes[i].style.background='#c4d4e5';
						}else {
							this.tableHint.childNodes[i].style.background='';
						}
					}
				}
				return -1;
				
			};
			element.hideHit = function() {
				this.divHint.style.display = "none";
				if (window.navigator.appVersion.indexOf("MSIE 6.0") > 0) {
					if(document.getElementById("ifmmask")){
						document.getElementById("ifmmask").style.height="0px";
					}
				}
			};
			element.onblur = function () {
				if (this.value == "") {
					this.value = this.suggest.config.defaultval;
					this.style.color = "#999999";
				};
				this.hideHit();
				clearInterval(this.intThread);
				this.intThread = -1;
			};
			element.setLine = function (line) {
				var stringKeyFore = "";
				if (this.value.indexOf(",") != -1 && this.value.indexOf(",") != 0 && this.value.indexOf(",") != this.value.length - 1) {
					var arrayStringKeyFore = this.value.split(",");
					arrayStringKeyFore[arrayStringKeyFore.length - 1] = "";
					stringKeyFore = arrayStringKeyFore.join(",");
				}
				if (this.line != null) {
					this.line.className = this.overLine == this.line ? "overLine" : "";
				}
				this.setlinecolor(line);
				this.line = line;
				line.className = this.overLine == line ? "overSelectedLine" : "selectedLine";
				this.value = stringKeyFore + line.stringFullCode;
			};
			element.onkeydown = function () {
				var event = arguments[0] || window.event;
				if (!this.tableHint) {
					switch (event.keyCode) {
						//case 13: //Enter
						//	return false;
						default:
							return true;
					}
				}
				var stringKeyFore = "";
				if (this.value.indexOf(",") != -1 && this.value.indexOf(",") != 0 && this.value.indexOf(",") != this.value.length - 1) {
					var arrayStringKeyFore = this.value.split(",");
					arrayStringKeyFore[arrayStringKeyFore.length - 1] = "";
					stringKeyFore = arrayStringKeyFore.join(",");
				}
				
				switch (event.keyCode) {
					case 38: //up
						this.booleanScan = false;
						if (this.line == null || this.getliIndex(this.line) == 1) {
							this.setLine(this.getlielement(this.tableHint.childNodes.length -1));
						}
						else {
							this.setLine(this.getlielement(this.getliIndex(this.line) - 1));
						}
						return false;
						break;
					case 40: //down
						this.booleanScan = false;
						if (this.line == null || this.getliIndex(this.line) == this.tableHint.childNodes.length -1) {
							this.setLine(this.getlielement(1));
						}
						else {
							var el = this.getlielement(this.getliIndex(this.line) + 1);
							this.setLine(this.getlielement(this.getliIndex(this.line) + 1));
						}
						return false;
						break;
					case 13: //Enter
						this.booleanScan = true;
						this.stringLastValue = stringKeyFore + this.value;
						this.divHint.style.display = "none";
						this.hideHit();
						this.suggest.afterHandle();
						if(this.suggest.config.autosub || this.suggest.config.autosub == "false") {
							return false;
						}else {
							return true;
						}
						//return true;
						break;
					default:
						this.booleanScan = true;
						break;
				}
			};
		};
		this.getCode = function (value) {
			var result = this.load(value);
			if (result.length == 1) {
				var stock = result[0].split(":")[1].split("^");
				return stock[0] + stock[1];
			}
			else {
				return value;
			}
		};
		
		this.getFundCode = function(){
			var val = this.ele.value;
			if(val == this.config.defaultval || val==""){
				return "";
			}
			var fundcode = "";
			
			var selectFirst = false;
			if (typeof this.config.selectFirst != "undefined") {
				selectFirst = this.config.selectFirst;
			}

			var arrayResult2=this.load(val);
	    	for (var i=0;i<arrayResult2.length;i++){
				var arrayRecord2 = arrayResult2[i].replace("|", "").split(":");
				var arrayCodeAndName2 = arrayRecord2[1].split("^");
				if (arrayResult2.length==1 || (i==0 && selectFirst) || val== arrayRecord2[0] || val== arrayCodeAndName2[1] || val== arrayCodeAndName2[2]){
					var vSearchType2 = Suggestlk2.getSearchType2(val);
					if(vSearchType2==0) {
					  fundcode = arrayRecord2[0];
					}else if(vSearchType2==1){
					  fundcode = arrayCodeAndName2[2];
					}else if(vSearchType2==2){
					  fundcode = arrayCodeAndName2[1];
					}
					break;
				}
	    	}
	    	return fundcode;
		};
	};
	Suggestlk2.getSearchType = function(inputVal,tdhead){
		var i = 0;
		for (var property in tdhead) {
			i++;
		}
		var rt;
			if (inputVal.charCodeAt(0)>127 || inputVal.charCodeAt(0)==94){
				//名称
				rt = 2;
			}else{
				var patrn = /^[a-z]$/; 
				if (patrn.test(inputVal)){
					rt = 1;  //拼音
				}else{
					var fLetter = inputVal.substring(0,1);
					if (isNaN(parseInt(fLetter)))
						rt = 1;  //拼音
					else
						rt = 0;	//代码
				}
			}
		if(i == 2) {
			rt--;
		}	
		return rt;
	};
	
	Suggestlk2.getSearchType2 = function(inputVal){
		
		var rt;
			if(inputVal.indexOf("EJ")==0){
				rt = 0;	//代码	
			}else if (inputVal.charCodeAt(0)>127 || inputVal.charCodeAt(0)==94){
				//名称
				rt = 2;
			}else{
				var patrn = /^[a-z]$/; 
				if (patrn.test(inputVal)){
					rt = 1;  //拼音
				}else{
					var fLetter = inputVal.substring(0,1);
					if (isNaN(parseInt(fLetter)))
						rt = 1;  //拼音
					else
						rt = 0;	//代码
				}
			}
		return rt;
	};
	
	Suggestlk2.getWrapType = function(inputVal){
		
		var rt;
			if(inputVal.indexOf("EJ")==0){
				rt = 2;	//代码	
			}else if (inputVal.charCodeAt(0)>127 || inputVal.charCodeAt(0)==94){
				//名称
				rt = 0;
			}else{
				var patrn = /^[a-z]$/; 
				if (patrn.test(inputVal)){
					rt = 1;  //拼音
				}else{
					var fLetter = inputVal.substring(0,1);
					if (isNaN(parseInt(fLetter)))
						rt = 1;  //拼音
					else
						rt = 2;	//代码
				}
			}
		return rt;
	};
	
	
	
	Suggestlk2.proccessTrHead = function(trHeader,searchType,tdhead){
		var headnameArray = new Array();
		var headvalueArray = new Array();
		trHeader.className = "tit";
		var i = 0;
		for ( var property in tdhead ){
			headnameArray[i] = property;
			headvalueArray[i] = tdhead[property];
			i++;
		} 
		for(var j=0;j<=i-1;j++) {
			var sp = document.createElement('span');
			trHeader.appendChild(sp);
			if(i==3) {
				if(j == 0) {
					sp.className="one" ;//setAttribute("class","one");
				}
				if(j == 1) {
					sp.className="two";
				}
				if(j == 2) {
					sp.className="three";
				}
			}else {
				if(j == 0) {
					sp.className="four";
				}
				if(j == 1) {
					sp.className="five";
				}
			}
			sp.innerHTML = headnameArray[j%i];
		}
	}
	
	Suggestlk2.bind = function(suggestInput,cfg){
		var suggest = new Suggestlk2.Suggest(cfg);
		suggest.bind(Suggestlk2.$$$(suggestInput));
	}


