var lastInputTime = null;
function IntervalTime()
{
	var nowInputTime=new Date().getTime();
	var flag=false;
	if(nowInputTime-lastInputTime>100)
	{flag=true;}
	lastInputTime=nowInputTime;
	return flag;
}

function searchSuggestBarcode(obj,idx)
{
	if(IntervalTime()||getKeyCode2()==13)
	{
		if (obj.value.length>1)
		{
			var url="../barcode/getBarcode.asp?k="+obj.value+"&idx="+idx+"&d="+NewDate();
			var sell_id=getEle("sell_id");
			if(!isEmpty(sell_id))
			{
				url=url+"&sell_id="+sell_id.value;
			}
			var loader=new net.ContentLoader(url,handleSuggestBarcode);
		}
	}
}

function handleSuggestBarcode()
{
    if(this.req.readyState==4)
    {
		if(this.req.status==200)
		{
			var list=this.req.responseText;
			var lists=list.split("$$$");
			var fcurIdx=lists[1];
			list=lists[0];
			var ss=getEle("search_suggest"+fcurIdx);
			var fcur=getEle("barcode"+fcurIdx);
			if(list.length>20)
			{
				ss.innerHTML="";
				ss.style.display="block";
				var position = getPosition(fcur);
				ss.style.left=position.left  + "px";
				ss.style.top=position.top + 20 + "px";
				var ls=list.split("|");
				if(ls.length==2)
				{
					var lss=ls[0].split("$");
					setBarCode(lss[0],lss[2],fcurIdx);
				}
				else
				{
					for (i=0;i<ls.length;i++ )
					{
						var lss=ls[i].split("$");
						if(lss.length>=3)
						{
							var suggest='<div onmouseover="javascript:suggestOver(this);"';	
							suggest+='onmouseout="javascript:sugggestOut(this);"';
							suggest+='onclick="javascript:setBarCode(\''+lss[0]+'\',\''+lss[2]+'\',\''+fcurIdx+'\');"';
							suggest +='class="suggest_link">'+lss[1]+'</div>';
							ss.innerHTML +=suggest;
						}
					}
				}
			}
			else
			{
				var cjh=getEle("cjh"+fcurIdx);
				if(!isEmpty(cjh))
				{
					cjh.className="bold red";
					cjh.innerHTML=notbarcode;
				}
				var djh=getEle("djh"+fcurIdx);
				if(!isEmpty(djh))
				{
					djh.className="bold red";
					djh.innerHTML=notbarcode;
				}
				if(!isEmpty(ss))
				{
				ss.innerHTML="";
				}
				if(!isEmpty(fcur))
				{
				fcur.value="";
				}
			}
		}
		else
		{
			alert("Abnormal");
		}
	}
}

function setBarCode(div_value,id_p,idx)
{
	var txtSearch=getEle("barcode"+idx);
	txtSearch.value=div_value;
	var id_produit=getEle("barcodeid"+idx);
	id_produit.value=id_p;
	var ss=getEle("search_suggest"+idx);
	ss.innerHTML="";
	ss.style.display="none";
	SetBarCode(txtSearch,idx);
}

function SetBarCode(obj,idx)
{
	//var idx=getIdx(obj);
	var id_produit=getEle("barcodeid"+idx);
	var hval=obj.value;
	if(!isEmpty(hval))
	{
		var url="../barcode/getBarcode_Sing.asp?barcode="+obj.value+"&idx="+idx+"&d="+NewDate();
		var sell_id=getEle("sell_id");
		if(!isEmpty(sell_id))
		{
			url=url+"&sell_id="+sell_id.value;
		}
		if(!isEmpty(id_produit))
		{
			url=url+"&barcodeid="+id_produit.value;
		}
		var loader=new net.ContentLoader(url,callBarCode);
	}
}

function callBarCode()
{
    if(this.req.readyState==4)
    {
        if(this.req.status==200)
		{
            var list=this.req.responseText;
			var ls=list.split("|");
			var idx=ls[0];
			var huohao=getEle("barcode"+idx);
			var cjh=getEle("cjh"+idx);
			var djh=getEle("djh"+idx);
			if(ls.length<=2)
			{
				huohao.focus();
				huohao.select();
				if(!isEmpty(cjh))
				{
					cjh.innerHTML=notbarcode;
				}
				if(!isEmpty(djh))
				{
					djh.innerHTML=notbarcode;
				}
			}
			else
			{
				huohao.value=ls[1];
				huohao.disabled=true;
				if(!isEmpty(cjh))
				{
					cjh.className="bold green";
					cjh.innerHTML=ls[2];	
				}
				if(!isEmpty(djh))
				{
					djh.className="bold green";
					djh.innerHTML=ls[3];	
				}
				NextBarcode(idx);
			}
			hiddensuggestBarcode(idx);
		}
		else
		{
            alert("Abnormal");
        }
    }
}

function NextBarcode(idx)
{
	var nextidx=StrToNum(idx)+1;
	var cailiaohan=getEle("barcode"+nextidx);
	if(!isEmpty(cailiaohan))
	{
		if(!cailiaohan.disabled)
		{
			cailiaohan.focus();
		}
		else
		{
			var maxshulian=getEle("maxshulian");
			if(!isEmpty(maxshulian))
			{
				var shulian=StrToNum(maxshulian.value);
				for(var i=nextidx+1;i<=shulian;i++)
				{
					cailiaohan=getEle("barcode"+i);
					if(!isEmpty(cailiaohan))
					{
						if(!cailiaohan.disabled)
						{
							cailiaohan.focus();
							i=shulian+1;
							break;
						}
					}
				}
			}
		}
	}
}

function hiddensuggestBarcode(idx)
{
	getEle("search_suggest"+idx).style.display="none";
}

function clearbarcode(idx)
{
	var id_produit=getEle("barcodeid"+idx);
	if(!isEmpty(id_produit))
	{
		if(!isEmpty(id_produit.value))
		{
			var url="../barcode/setBarcode_Clear.asp?idx="+idx+"&d="+NewDate();
			url=url+"&barcodeid="+id_produit.value;
			var loader=new net.ContentLoader(url,callClearBarCode);
		}
	}
}

function callClearBarCode()
{
    if(this.req.readyState==4)
    {
        if(this.req.status==200)
		{
            var list=this.req.responseText;
			var ls=list.split("|");
			var idx=ls[0];
			var huohao=getEle("barcode"+idx);
			var cjh=getEle("cjh"+idx);
			var djh=getEle("djh"+idx);
			var barcodeid=getEle("barcodeid"+idx);
			if(ls.length>=2)
			{
				if(ls[1]=="ok")
				{
					huohao.value="";
					huohao.disabled=false;
					huohao.focus();
					if(!isEmpty(barcodeid))
					{
						barcodeid.value="";
					}
					if(!isEmpty(cjh))
					{
						cjh.className="bold red";
						cjh.innerHTML=notbarcode;	
					}
					if(!isEmpty(djh))
					{
						djh.className="bold red";
						djh.innerHTML=notbarcode;	
					}
				}
			}
		}
		else
		{
            alert("Abnormal");
        }
    }
}

function getKeyCode2()
{
	var e=e || event;
	return e.keyCode;
}

function checkbarcode()
{
	var barcodeids=getEle("barcodeids");
	if(!isEmpty(barcodeids))
	{
		var maxshulian=getEle("maxshulian");
		if(!isEmpty(maxshulian))
		{
			var shulian=StrToNum(maxshulian.value);
			barcodeids.value="";
			for (i=1;i<=shulian;i++ )
			{
				var barcodeid=getEle("barcodeid"+i);
				if(!isEmpty(barcodeid))
				{
					if(isEmpty(barcodeid.value))
					{
						alert(notbarcode+" ["+i+"]!");
						var huohao=getEle("barcode"+i);
						huohao.focus();
						i=shulian+1;
						return false;
						break;
					}
					else
					{
						if(isEmpty(barcodeids.value))
						{
							barcodeids.value=barcodeid.value;
						}
						else
						{
							barcodeids.value=barcodeids.value+","+barcodeid.value;
						}
					}
				}
			}
		}
		var barcodes=barcodeids.value.split(",");
		if(barcodes.length==shulian)
		{
			var sell_id=getEle("sell_id");
			if(!isEmpty(sell_id))
			{
				var url="../barcode/setBarcode_Save.asp?sell_id="+sell_id.value+"&barcodeids="+barcodeids.value+"&d="+NewDate();
				var loader=new net.ContentLoader(url,callBarcodeSave);
			}
		}
		else
		{
			alert(notbarcode+"!");
			return false;
		}
	}
}

function callBarcodeSave()
{
    if(this.req.readyState==4)
    {
        if(this.req.status==200)
		{
            var list=this.req.responseText;
			if(list=="ok")
			{
				var barcodeids=getEle("barcodeids");
				var nowidx=getEle("nowidx");
				if(!isEmpty(nowidx))
				{
					var btnScanBarCode=window.opener.document.getElementById("btnScanBarCode"+nowidx.value);
					if(!isEmpty(btnScanBarCode))
					{
						var barcodes=barcodeids.value.split(",");
						btnScanBarCode.value=Scanned+" "+barcodes.length;
					}
					var openerbarcode=window.opener.document.getElementById("barcodes"+nowidx.value);
					if(!isEmpty(openerbarcode))
					{
						openerbarcode.value=barcodeids.value;
						window.close();
					}
				}
			}
			else
			{
				alert("Failure!");
			}
		}
		else
		{
            alert("Abnormal");
        }
    }
}