function JHshNumberText(a)
{ 
	var fa="";
	if(a.value.substring(0,1)=="-")
		fa="-";
	var str=(a.value.replace(/[^0-9.]/g,'')).replace(/[.][0-9]*[.]/, '.');
	if (str.substring(0,1)==".")
		str="0"+str;
		a.value=fa+str;
} 
function isNumberString (InString,RefString)
{
if(InString.length==0) return (false);
for (Count=0; Count < InString.length; Count++)  {
	TempChar= InString.substring (Count, Count+1);
	if (RefString.indexOf (TempChar, 0)==-1)  
	return (false);
}
return (true);
}

function check1(yn,act)
{
	total();
	var huohao1=getEle("huohao1");
	if(!isEmpty(huohao1))
	{
		if (huohao1.value==emptyProduit)
		{
			alert(EmptySelAlert);
			if(!huohao1.readOnly)
			{
			huohao1.focus();
			}
			return false;
		}
	}
	var shulian1=getEle("shulian1");
	if(!isEmpty(shulian1))
	{
		if (shulian1.value=="")
		{
			alert(EmptyAlert);
			if(!shulian1.readOnly)
			{
			shulian1.focus();
			}
			return false;
		}
	}
	if(!checkRepeat(yn))
	{
		if (document.form1.sshk.value=="")
		{
			alert(EmptyAlert);
			document.form1.sshk.focus();
			return false;
		}
		if (document.form1.priceother.value=="")
		{
			alert(EmptyAlert);
			document.form1.priceother.focus();
			return false;
		}
		var btnSave=document.getElementById("btnSave");
		if(!isEmpty(btnSave))
		{
			btnSave.disabled=true;
		}
		if(act!="")
		{
			ShowPay();
		}
		else
		{
		document.form1.submit();
		return true;
		}
	}
}

function checkRepeat(yn)
{
	var r=false;
	for(var i=1;i<=maxproduit;i++)
	{
		var objhuohao=getEle("huohao"+i);
		if(!isEmpty(objhuohao))
		{
			var huohao=objhuohao.value;
			if(huohao!=emptyProduit)
			{
				if(yn=="yes")
				{
					var objbatch=getEle("batch"+i);
					if(!isEmpty(objbatch))
					{
						if((objbatch.value=="") && (IsBatch=="yes"))
						{
							alert(InputBatch);
							objbatch.focus();
							r=true;
							i=maxproduit+1;
							break;
						}
					}
				}
				var shulian=getEle("shulian"+i);
				if(StrToNum(shulian.value)<=0)
				{
					alert(InputQTY+" "+i+" !");
					shulian.focus();
					r=true;
					i=maxproduit+1;
					break;
				}
				var objku=getEle("ku"+i);
				if(!isEmpty(objhuohao) && !isEmpty(objku))
				{
					var ku=objku.value;
					for(var j=i+1;j<=maxproduit;j++)
					{
						var objhuohao2=getEle("huohao"+j);
						if(!isEmpty(objhuohao2))
						{
							var huohao2=objhuohao2.value;
							var objku2=getEle("ku"+j);
							if(huohao2!=emptyProduit)
							{
								if(!isEmpty(objku2))
								{
									var ku2=objku2.value;
									var batch2=getEle("batch"+j);
									var isrepeat=false;
									if(yn=="yes")
									{
										isrepeat=(huohao==huohao2 && ku==ku2 && objbatch.value==batch2.value)
									}
									else
									{
										isrepeat=(huohao==huohao2 && ku==ku2)
									}
									if(isrepeat)
									{
										alert(IsRepeat+" ["+huohao+"]!");
										objhuohao.focus();
										r=true;
										i=maxproduit+1;
										break;
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return r;
}

function chg(objName, objName2, zekou)
{
	var obj;
	obj = document.getElementById(objName);
	obj2 = document.getElementById(objName2);
	if(isEmpty(obj2)||StrToNum(obj2.value)<=0)
	{
		obj2.value=obj.value;
	}
	temp = obj2.value*zekou
	obj.value = Math.round(temp*1000)/1000;
}
function youhui()
{
	var temp=getEle('ysk').value;
	var ssk=getEle('sshk').value;
	getEle('yh').value=Math.round((temp*1-ssk*1)*1000)/1000;
}

function zhonger()
{
	var ssk=getEle('sshk').value;
	var temp=getEle('priceother').value;
	getEle('ssze').value=Math.round((temp*1+ssk*1)*1000)/1000;
}

function taxfee()
{
	var Amount=getEle("Amount");
	if(!isEmpty(Amount))
	{
		var TaxFee=getEle("TaxFee");
		if(!isEmpty(TaxFee))
		{
			TaxFee.value=StrToNum(Amount.value*TaxRate/100);
			var ysk=getEle("ysk");
			if(TaxRateState==2)
			{
				if(!isEmpty(ysk))
				{
					ysk.value=StrToNum(Amount.value)+StrToNum(TaxFee.value);
				}
			}
			else
			{
				ysk.value=Amount.value;
			}
		}
	}
}

function heji()
{
	var temp;
	temp = 0.000;
	for (i=1;i<=maxproduit;i++)
	{
		obj = getEle("shulian"+i);
		obj2 = getEle("price1"+i);
		var cailiaohan=getEle("cailiaohan"+i);
		if(!isEmpty(cailiaohan))
		{
			if(cailiaohan.style.display!="none")
			{
				if(!isEmpty(obj)&&!isEmpty(obj2))
				{
				var objval=StrToNum(obj.value);
				var obj2val=StrToNum(obj2.value);
				temp=temp+objval*obj2val;
				}
			}
		}
	}
	var Amount=getEle("Amount");
	Amount.value=Math.round(temp*1000)/1000;
	if(TaxRate>0 && TaxRateState!=0)
	{
		taxfee();
	}
	else
	{
		var ysk=getEle("ysk");
		ysk.value=Amount.value;
	}
	if(IsAutoSshk="yes")
	{
		var objoldsshk=getEle("oldsshk");
		var objsshk=getEle("sshk");
		if(objoldsshk.value==objsshk.value)
		{
			objsshk.value=StrToNum(getEle("ysk").value-getEle("yh").value);
			objoldsshk.value=objsshk.value;
		}
	}
}

function total()
{
	heji();
	youhui();
	zhonger();
}

function checkShulian(idx)
{
	var huohao=getEle("huohao"+idx);
	if (huohao.value!=emptyProduit)
	{
		var url="getVJian.asp?huohao="+huohao.value+"&idx="+idx+"&d="+NewDate();
		var ku=getEle("ku"+idx);
		if(!isEmpty(ku))
		{
			url=url+"&ku="+ku.value;
		}
		var sell_id=getEle("sell_id"+idx);
		if(!isEmpty(sell_id))
		{
			url=url+"&sell_id="+sell_id.value;
		}
		var loader=new net.ContentLoader(url,callCheckShulian);
	}
}

function callCheckShulian()
{
    if(this.req.readyState==4)
    {
		if(this.req.status==200)
		{
			var list=this.req.responseText;
			var lists=list.split("$$$");
			if(lists.length==2)
			{
				var fcurIdx=lists[1];
				var vjian=StrToNum(lists[0]);
				var shulian=getEle("shulian"+fcurIdx);
				var inputshulian=StrToNum(shulian.value);
				var idp=getEle("id_produit"+fcurIdx);
				if(inputshulian>vjian)
				{
					alert(MaxOnly+" "+vjian);
					shulian.focus();
					SetProduit2(fcurIdx);
				}
			}
		}
	}
}

function SetProduit2(idx)
{
	var obj=getEle("huohao"+idx);
	var hval=obj.value;
	if(hval!=emptyProduit&&!isEmpty(hval))
	{
		var url="getProduitForBuy.asp?huohao="+obj.value+"&idx="+idx+"&d="+NewDate();
		var ku=getEle("ku"+idx);
		if(!isEmpty(ku))
		{
			url=url+"&ku="+ku.value;
		}
		var sell_id=getEle("sell_id"+idx);
		if(!isEmpty(sell_id))
		{
			url=url+"&sell_id="+sell_id.value;
		}
		var loader=new net.ContentLoader(url,callProduit2);
	}
}

function callProduit2()
{
    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("huohao"+idx);
			var showshulian=getEle("showshulian"+idx);
			if(ls.length<=2)
			{
				huohao.value=emptyProduit;
				huohao.select();
				showshulian.className="red bold";
				showshulian.innerHTML=NotExist;
			}
			else
			{
				huohao.value=ls[1];
				var title=getEle("title"+idx);
				if(!isEmpty(title))
				{
					title.value=ls[2];	
				}
				var ku=getEle("ku"+idx);
				if(!isEmpty(ku))
				{
					ku.value=ls[5];	
				}
				showshulian.className="bold";
				showshulian.innerHTML=ls[6];
			}
		}
		else
		{
            alert("Abnormal");
        }
    }
}

function ScanBarCode(idx)
{
	var sell_id=getEle("sell_id"+idx);
	if(!isEmpty(sell_id))
	{
		windowOpen("../barcode/sell_scanbar.asp?idx="+idx+"&id="+sell_id.value,"");
	}
}

function check2()
{
	total();
	if (document.form1.sshk.value=="")
	{
		alert(EmptyAlert);
		document.form1.sshk.focus();
		return false;
	}
	if (document.form1.priceother.value=="")
	{
		alert(EmptyAlert);
		document.form1.priceother.focus();
		return false;
	}
	if(checkScanBarCode())
	{
		var btnSave=document.getElementById("btnSave");
		if(!isEmpty(btnSave))
		{
			btnSave.disabled=true;
		}
		document.form1.submit();
		return true;
	}
}

function checkScanBarCode()
{
	var r=false;
	var maxshulian=getEle("maxshulian");
	if(!isEmpty(maxshulian))
	{
		var mshulian=StrToNum(maxshulian.value);
		for (i=1;i<=mshulian;i++ )
		{
			ku_type=getEle("ku_type"+i);
			if(!isEmpty(ku_type))
			{
				if(ku_type.value=="0")
				{
					var barcodes=getEle("barcodes"+i);
					if(!isEmpty(barcodes))
					{
						var huohao=getEle("huohao"+i);
						var title=getEle("title"+i);
						if(isEmpty(barcodes.value))
						{
							alert(title.value+" ("+huohao.value+") wei shao miao!");
							ScanBarCode(i);
							i=mshulian+2;
							break;
						}
						else
						{
							var shulian=getEle("shulian"+i);
							if(!isEmpty(shulian))
							{
								var sellshulian=StrToNum(shulian.value);
								var barcodeslen=barcodes.value.split(",").length;
								if(sellshulian!=barcodeslen)
								{
									alert(title.value+" ("+huohao.value+") "+MustEqu+" ["+sellshulian+"]!");
									ScanBarCode(i);
									i=mshulian+2;
									break;
								}
							}
						}
					}
				}
			}
		}
		if(i==mshulian+1)
		{
			r=true;
		}
	}
	return r;
}

function ShowPay()
{
	total();
	var lanrentuku0=getEle("lanrentuku0");
	if(!isEmpty(lanrentuku0))
	{
		var sshk=getEle("sshk");
		if(!isEmpty(sshk))
		{
			var sshknum=StrToNum(sshk.value);
			if(sshknum>0)
			{
				lanrentuku0.style.display="block";
				getEle("payysk").value=sshknum;
				getEle("paysk").value="";
				getEle("paysk").focus();
				Setzl();
			}
		}
	}
}

function Setzl()
{
	var lanrentuku0=getEle("lanrentuku0");
	if(!isEmpty(lanrentuku0))
	{
		var payzl=StrToNum(StrToNum(getEle("paysk").value)-StrToNum(getEle("payysk").value));
		var btnPayed=getEle("btnPayed");
		if(payzl>=0)
		{
		getEle("payzl").value=payzl;
		btnPayed.disabled=false;
		}
		else
		{
		getEle("payzl").value="";
		btnPayed.disabled=true;
		}
	}
}

function HiddenPay()
{
	var lanrentuku0=getEle("lanrentuku0");
	if(!isEmpty(lanrentuku0))
	{
		lanrentuku0.style.display="none";
		SetLast();
	}
}

function SubmitPay(yn)
{
	check1(yn,'');
	//document.form1.submit();
}