//var mk_status ;

function iSift(cVarName, nMax) {
	this.oo = cVarName;
	this.Max = nMax;
}
iSift.prototype = {
	Target: Object,
	TgList: Object,
	Listeners: null,
	SelIndex: 0,
	Data: [],
	ReData: [],
	Create: function(str) {
		var _this = this;
		var oUL = document.createElement('ul');
		var oObj = document.getElementById(str);
		oUL.style.display = 'none';
		oUL.className = 'checkmenu'
		oObj.parentNode.insertBefore(oUL, oObj);
		_this.TgList = oUL; 
		oObj.onkeyup =  function(e) {
			var val = $('#'+str).val();
			val = $.trim(val);
			if(val.length>0)
			{	var v = "";
				var ind = val.lastIndexOf(";", val.length-2);
				if(ind!=-1) {
					v = val.substring(ind+1);
					if(v.length>0) {
						getReceiveValue(v, _this);
					}
				} else {
					getReceiveValue(val, _this);
				}
				//getReceiveValue(val, _this);
			}
			_this.Listen(this,str);
		};

		/*oObj.onclick = function(e) {
			var val = $('#'+str).val();
			if(val.length>0){
				getReceiveValue(val, _this);
			}
			_this.Listen(this,str);
		};*/

		oObj.onblur = function() {
			setTimeout(function() {
				var val = $('#'+str).val();
				_this.Clear();
			},
			25);
		};
	},
	Complete: function() {},
	Select: function(str) {
		var _this = this;
		if (_this.ReData.length > 0) { 
			var choose = _this.Data[_this.SelIndex];
			var pre = choose.split(",");
	
			var ov = $("#"+str).val();
			if(ov.lastIndexOf(";")!=-1) {
				ov = ov.substring(0, ov.lastIndexOf(";")+1);
			} else {
				ov = "";
			}
			if(pre[2]=="group") {
				$("#"+str).val(ov+"["+pre[0]+"];").focus();
			} else {
				$("#"+str).val(ov+pre[0]+";").focus();
			}
			
			synval();
		    //$("#"+str).val("").focus(); 
		    _this.Clear();
		}
	},
	Listen: function(oObj,str) {
		var _this = this;
		_this.Target = oObj;
		var e = arguments[arguments.length - 1];
		var ev = window.event || e;
		switch (ev.keyCode) {
		/*case 8:
			synval();
		    return;*/
		case 9:
			//TAB
			return;
		case 13:
			//ENTER
			_this.Target.blur();
			_this.Select(str);
			return;
		case 38:	
			//UP
			_this.SelIndex = _this.SelIndex > 0 ? _this.SelIndex - 1: _this.ReData.length - 1;
			break;
		case 40:
			//DOWN
			_this.SelIndex = _this.SelIndex < _this.ReData.length - 1 ? _this.SelIndex + 1: 0; 
			break;
		default:
			_this.SelIndex = 0;
		}
		if (_this.Listeners) {
			clearInterval(_this.Listeners);
		}
		_this.Listeners = setInterval(function() {
			_this.Get(str);
		},
		10);
	},
	Get: function(str) {
		var _this = this;
		if (_this.Listeners) {
			clearInterval(_this.Listeners);
		};
		if (_this.Target.value == '') {
			_this.Clear();
			return;
		}
		_this.ReData = [];
		var cResult = '';
		var len = _this.Data.length;
		for (var i = 0; i < len; i++) {
			if(_this.Data[i]==null || $.trim(_this.Data[i])=='') {
				continue;
			}
			var pres = _this.Data[i].split(",");
			//用户手动输入
			var val = $('#'+str).val();
			//if(pres[2]=="group") {
				//_this.ReData.push(pres[1]);
				
				//str = "[" + pres[1] + "]";
			//} else {
				_this.ReData.push(pres[0]+"("+pres[1]+")");
			//}

			if (_this.ReData == _this.Max) {
				break;
			}
			//}
		}
		var v = "";
		var ind = -1;
		if(val!=null && val.length>1) {
			ind = _this.Target.value.lastIndexOf(";", val.length-2);
		}
		v = _this.Target.value.substring(ind+1);
		var cRegPattern = v.replace(/\*/g, '*');
		var cRegEx = new RegExp(cRegPattern, 'i');
		for (var i = 0; i < _this.ReData.length; i++) {
			cResult += '<li  style="width:100%; line-height:20px; border-top:1px solid #d7d4d4; cursor:default;" onmouseover=' +
			_this.oo + '.ChangeOn(this);' + _this.oo + '.SelIndex=' + i + '; onmousedown=' + _this.oo + '.Select("'+str+'");>&nbsp;'
			 + _this.ReData[i].replace(cRegEx,
			function(s) {
				return '<font color="red">' + s + '</font>';
			} ); + '</li>';
		}
		if (cResult == '') {
			_this.Clear();
		}
		 else {
			_this.TgList.innerHTML = cResult;
			////上下键移动
			_this.TgList.style.cssText = 'display:block; position:absolute; background:#fff; color:#333; border:1px solid #a9abaa; border-top:none; font-size:12px;';
		}
		var oLi = _this.TgList.getElementsByTagName('li');
		if (oLi.length > 0) {
			oLi[_this.SelIndex].style.cssText = 'width:100%; background:#64a8e7;color:#fff; line-height:20px; cursor:default; border-top:1px solid #d7d4d4;';
		}
	},
	ChangeOn: function(oObj) {
		var oLi = this.TgList.getElementsByTagName('li');
		for (var i = 0; i < oLi.length; i++) {
			oLi[i].style.cssText = 'width:100%;  height:20px; line-height:20px; cursor:default; border-top:1px solid #d2d2d2;';//鼠标移过变成白色
		}
		oObj.style.cssText = 'width:100%; background:#64a8e7; height:20px; line-height:20px; cursor:default; color:#fff; border-top:1px solid #d7d4d4;';//鼠标移到变蓝色
	},
	Clear: function() {
		var _this = this;
		if (_this.TgList) {
			_this.TgList.style.display = 'none';
			_this.ReData = [];
			_this.SelIndex = 0;
		}
	}
}

/*function getReceiveValue(val, obj) {
	//G_users, G_managers, G_brokers, G_groupslable
	var array = [];
	var G_all = [];
	G_all = G_all.concat(G_users, G_managers);
	G_all = G_all.concat(G_all, G_brokers);
	G_all = G_all.concat(G_all, G_groupslable);
	if(G_all!=null && G_all.length>0) {
		var len = G_all.length;	
		for(var i=0; i<len; i++) {
			var pre = G_all[i];
			var item = "";
			var contain = false;
			if((pre.fc!=null&&pre.fc!=""&&pre.fc.toLowerCase().indexOf(val)!=-1)
				|| (pre.id!=null&&pre.id!=""&&pre.id.toLowerCase().indexOf(val)!=-1)
				|| (pre.nm!=null&&pre.nm!=""&&pre.nm.toLowerCase().indexOf(val)!=-1)) {
				item = pre.nm+","+pre.id+","+pre.type;
				for(var j=0; j<array.length; j++) {
					if(array[j]==item) {
						contain = true;
						break;
					}
				}
				if(contain) {
					continue;
				}
				array.push(item);
			} 
			if(array.length==10) break;
		} 
	}
	array = sortArray(val, array);
	obj.Data = array;
	array = null;
	G_all = null;
}*/
function getReceiveValue(val, obj) {
	//G_users, G_managers, G_brokers, G_groupslable
	var array = [];
	var G_all = [];
	G_all = G_all.concat(G_users, G_managers,  G_brokers, G_groupslable);
	var array1 = [];
	var array2 = [];
	var array3 = [];
	var array_len = 0;
	if(G_all!=null && G_all.length>0) {
		var len = G_all.length;	
		for(var i=0; i<len; i++) {
			var pre = G_all[i];
			var item = "";
			var contain = false;
			if(pre.nm!=null&&pre.nm!=""&&pre.nm.toLowerCase().indexOf(val)!=-1) {
				item = pre.nm+","+pre.id+","+pre.type;
				for(var j=0; j<array1.length; j++) {
					if(array1[j]==item) {
						contain = true;
						break;
					}
				}
				if(contain) {
					continue;
				}
				array1.push(item);
			} 
			if(array1.length==10) break;
		}
		array1 = sortArray(val, array1);
		array = array1;
		array_len = array.length;	
		if(array_len<10) {
		for(var i=0; i<len; i++) {
			var pre = G_all[i];
			var item = "";
			var contain = false;
			if(pre.fc!=null&&pre.fc!=""&&pre.fc.toLowerCase().indexOf(val)!=-1) {
				item = pre.nm+","+pre.id+","+pre.type;
				for(var j=0; j<array2.length; j++) {
					if(array2[j]==item) {
						contain = true;
						break;
					}
				}
				if(!contain) {
					for(var j=0; j<array.length; j++) {
						if(array[j]==item) {
							contain = true;
							break;
						}
					}
				}
				if(contain) {
					continue;
				}
				array2.push(item);
			} 
			if(array2.length==(10-array_len)) break;
		}
		array2 = sortArray(val, array2);
		array = array.concat(array2);
		array_len = array.length;
		}
		if(array_len<10) {
		for(var i=0; i<len; i++) {
			var pre = G_all[i];
			var item = "";
			var contain = false;
			if(pre.id!=null&&pre.id!=""&&pre.id.toLowerCase().indexOf(val)!=-1) {
				item = pre.nm+","+pre.id+","+pre.type;
				for(var j=0; j<array3.length; j++) {
					if(array3[j]==item) {
						contain = true;
						break;
					}
				}
				if(!contain) {
					for(var j=0; j<array.length; j++) {
						if(array[j]==item) {
							contain = true;
							break;
						}
					}
				}
				if(contain) {
					continue;
				}
				array3.push(item);
			} 
			if(array3.length==(10-array_len)) break;
		}
		array3 = sortArray(val, array3);
		array = array.concat(array3);
		}
	}
	//array = sortArray(val, array);
	obj.Data = array;
	array = null;
	G_all = null;
	array1 = null;
	array2 = null;
	array3 = null;
}

function sortArray(val, array) {
	if(val!=null&&val!=""&&array.length>0) {
		for(var i=0; i<array.length; i++) {		
			for(var j=i+1; j<array.length; j++) {
				var indi = array[i].toLowerCase().indexOf(val);
				var indj = array[j].toLowerCase().indexOf(val);
				if(indi>indj) {
					var n = array[j];
					array[j] = array[i];
					array[i] = n;
				}
			}
		}
	}
	return array;
}