var loc=location.href.toLowerCase(),issell=false;
if ((loc.indexOf("sell")>0)||(loc.indexOf("tui")>0)||(loc.indexOf("fei")>0))
{
issell=true;	
}
$(document).ready(function()
{
	for(var i=1;i<=maxproduit;i++)
	{
		var batch=$("#batch"+i);
		if(batch.length>0)
		{
			batch.focus(function(){clearSearch();});
			batch.blur(function(){blurBatch(this)});
			batch.keyup(function(){searchSuggestBatch(this);});
			$("#shulian"+i).blur(function(){CheckBatch2(this,"shulian");});
			$("#price2"+i).blur(function(){CheckBatch2(this,"price2");});
			$("#price1"+i).blur(function(){CheckBatch2(this,"price1");});
			$("#ku"+i).blur(function(){CheckBatch2(this,"ku");});
		}
		$("#shulian"+i).focus(function(){clearSearch();});
		$("#price2"+i).focus(function(){clearSearch();});
		$("#ku"+i).focus(function(){clearSearch();});
	}
});

function blurBatch(obj)
{
	obj.value=obj.value.toUpperCase();
}

function clearSearch()
{
	for(var i=1;i<=maxproduit;i++)
	{
		var ss=getEle("search_suggest"+i);
		if(!isEmpty(ss))
		{
		ss.innerHTML="";
		ss.style.display="none";
		}
	}
}

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

function getBatchIdx(obj)
{
	return obj.id.replace("batch","");
}

function getPositionBatch(obj)
{
  var topValue = 0, leftValue = 0;
  while (obj)
  {
	  leftValue += obj.offsetLeft;
	  topValue += obj.offsetTop;
	  obj = obj.offsetParent;
  }
  return{
	  left: leftValue,
	  top: topValue};
}

function suggestOverBatch(div_value)
{
	div_value.className='suggest_link_over';
}

function sugggestOutBatch(div_value)
{
  div_value.className='suggest_link';
}


function searchSuggestBatch(obj)
{
	obj.value=obj.value.toUpperCase();
	var idx=getBatchIdx(obj);
	var id_produit=getEle("id_produit"+idx);
	var huohao=getEle("huohao"+idx);
	if(huohao.value!=emptyProduit||id_produit.value.length>0)
	{
		var ku=getEle("ku"+idx);		
		if(IntervalBatch()||getKeyCode()==13)
		{
			if (obj.value.length>0)
			{
				var url="../produit/getbatch.asp?act=get&k="+obj.value+"&idx="+idx+"&id_p="+id_produit.value+"&huohao="+huohao.value+"&id_ku="+ku.value+"&issell="+issell+"&d="+NewDate();
				//location.href=url;
				var loader=new net.ContentLoader(url,handleSearchSuggestBatch);
			}
		}
	}
}

function handleSearchSuggestBatch()
{
    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("batch"+fcurIdx);
			if(list.length>0)
			{
				ss.innerHTML="";
				ss.style.display="block";
				var position = getPositionBatch(fcur);
				ss.style.left=position.left  + "px";
				ss.style.top=position.top + 20 + "px";
				ss.style.width=160+ "px";
				var ls=list.split("|");
				for (i=0;i<ls.length;i++ )
				{
					var lss=ls[i];
					if(lss.length>0)
					{
						var suggest='<div onmouseover="javascript:suggestOverBatch(this);"';	
						suggest+='onmouseout="javascript:sugggestOutBatch(this);"';
						suggest+='onclick="javascript:setBatch(\''+lss+'\',\''+fcurIdx+'\');"';
						suggest +='class="suggest_link">'+lss+'</div>';
						ss.innerHTML +=suggest;
					}
				}
			}
		}
	}
}

function setBatch(div_value,idx)
{
	var txtSearch=getEle("batch"+idx);
	var ss=getEle("search_suggest"+idx);
	txtSearch.value=div_value.split(" ")[0];
	ss.innerHTML="";
	ss.style.display="none";
}

function CheckBatch(yn)
{
	var r=true;
	if(yn=="yes")
	{
		for(var i=1;i<=maxproduit;i++)
		{
			var objhuohao=document.getElementById("huohao"+i);
			if(!isEmpty(objhuohao))
			{
				var huohao=objhuohao.value;
				if(huohao!=emptyProduit)
				{
					var objbatch=getEle("batch"+i);
					if(!isEmpty(objbatch))
					{
						if(objbatch.value=="")
						{
							alert(InputBatch+" "+i+" !");
							objbatch.focus();
							r=false;
							i=maxproduit+1;
							break;
						}
					}
				}
			}
		}
	}
	return r;
}

function CheckBatch2(obj,pre)
{
	var objid=$(obj).attr("id");
	var idx=objid.replace(pre,"");
	var id_produit=getEle("id_produit"+idx);
	if(!isEmpty(id_produit))
	{
		if(!isEmpty(id_produit.value))
		{
			var objbatch=getEle("batch"+idx);
			if(!isEmpty(objbatch))
			{
			var url="getbatch.asp?act=chk&id_p="+id_produit.value+"&batch="+objbatch.value+"&idx="+idx+"&d="+NewDate();
			var loader=new net.ContentLoader(url,callCheckBatch);	
			}
		}
	}
}

function callCheckBatch()
{
    if(this.req.readyState==4)
    {
		if(this.req.status==200)
		{
			var list=this.req.responseText;
			var lists=list.split("$$$");
			if(lists.length==2)
			{
				var ishave=lists[0];
				var fcurIdx=lists[1];
				if(ishave=="0")
				{
					alert(NotExist);
					var curobj=getEle("batch"+fcurIdx);
					curobj.focus();
					curobj.select();
				}
			}
		}
	}
}