$(document).keydown(function(e)
{
	var ev = window.event || e;
	var code = ev.keyCode || ev.which;
	if (code == 119)//F8
	{
		var huiyuan3=getEle("huiyuan3");
		if(!isEmpty(huiyuan3))
		{
			huiyuan3.select();
			huiyuan3.focus();
		}
		huiyuan3=getEle("card");
		if(!isEmpty(huiyuan3))
		{
			huiyuan3.select();
			huiyuan3.focus();
		}
		ev.keyCode ? ev.keyCode = 0 : ev.which = 0;
		return false;
	}
	else if (code == 120)//F9
	{
		SetLast();
		ev.keyCode ? ev.keyCode = 0 : ev.which = 0;
		return false;
	}
	else if (code == 121)//F10
	{
		ev.keyCode ? ev.keyCode = 0 : ev.which = 0;
		var url=location.href.toLowerCase();
		if(url.indexOf("produit_sell.asp")>0)
		{
			if(url.indexOf("collect")>0)
			{
				top.location.href="../collect.asp";
			}
			else
			{
				top.location.href="../collect.asp?act=collect";
			}
		}
		return false;
	}
	else if (code == 35)//End
	{
		ev.keyCode ? ev.keyCode = 0 : ev.which = 0;
		ShowPay();
		return false;
	}
});

function check(stype,act)
{
	if(stype=="qq")
	{
		SetCard(document.getElementById("qq2"));
	}
	if(act=="add")
	{
	if (document.form1.username.value==""||document.form1.tel.value==""||document.form1.card.value==""||document.form1.sfz.value=="")
	{
	alert(MustFillIn);
	return false;
	}
	}
	else
	{
	if (document.form1.username.value==""||document.form1.tel.value==""||document.form1.card.value=="")
	{
	alert(MustFillIn);
	return false;
	}
	}
}
function SetCard(obj)
{
	document.getElementById("card").value=obj.value;
}

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

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

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

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

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


function searchSuggestHuiyuan(obj)
{
	if(IntervalTimeH()||getKeyCodeH()==13)
	{
		if (obj.value.length>1)
		{
			var url="../huiyuan/getHuiyuan.asp?k="+obj.value+"&d="+NewDate();
			var loader=new net.ContentLoader(url,handleSearchSuggestHuiyuan);
		}
	}
}

function handleSearchSuggestHuiyuan()
{
    if(this.req.readyState==4)
    {
		if(this.req.status==200)
		{
			var list=this.req.responseText;
			var ss=getEle("search_suggestHuiyuan");
			var fcur=getEle("huiyuan3");
			if(list.length>=18)
			{
				ss.innerHTML="";
				ss.style.display="block";
				var position = getPositionH(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("$");
					setHuiyuan(lss[0],lss[2]);
				}
				else
				{
					for (i=0;i<ls.length;i++ )
					{
						var lss=ls[i].split("$");
						if(lss.length>=3)
						{
							var suggest='<div onmouseover="javascript:suggestOverH(this);"';	
							suggest+='onmouseout="javascript:sugggestOutH(this);"';
							suggest+='onclick="javascript:setHuiyuan(\''+lss[0]+'\',\''+lss[2]+'\');"';
							suggest +='class="suggest_link">'+lss[1]+'</div>';
							ss.innerHTML +=suggest;
						}
					}
				}
			}
			else
			{
				var showshulian=getEle("showshulianHuiyuan");
				if(!isEmpty(showshulian))
				{
					showshulian.className="red bold";
					showshulian.innerHTML=NotExist;
				}
				if(!isEmpty(ss))
				{
				ss.innerHTML="";
				}
				if(!isEmpty(fcur))
				{
				fcur.value="";
				}
			}
	}else{
		alert("Abnormal");
	}
	}
}

function setHuiyuan(div_value,id_p)
{
	var txtSearch=getEle("huiyuan3");
	txtSearch.value=div_value;
	var id_produit=getEle("huiyuan");
	id_produit.value=id_p;
	var ss=getEle("showshulianHuiyuan");
	ss.innerHTML="";
	ss.style.display="none";
	var hval=div_value;
	if(hval!=emptyProduit&&!isEmpty(hval))
	{
		var url="../huiyuan/getHuiyuanSing.asp?id="+id_produit.value+"&d="+NewDate();
		var loader=new net.ContentLoader(url,callHuiyuan);
	}
}

function callHuiyuan()
{
    if(this.req.readyState==4)
    {
        if(this.req.status==200)
		{
            var list=this.req.responseText;
			var ls=list.split("|");
			var huohao=getEle("huiyuan3");
			var showshulian=getEle("showshulianHuiyuan");
			if(ls.length<=2)
			{
				huohao.focus();
				huohao.value=emptyProduit;
				huohao.select();
				showshulian.className="red bold";
				showshulian.innerHTML=NotExist;
			}
			else
			{
				huohao.value=ls[0];
				var title=getEle("huiyuan2");
				if(!isEmpty(title))
				{
					title.value=ls[1];	
				}
				var price2=getEle("huiyuan_name");
				if(!isEmpty(price2))
				{
					price2.value=ls[1];	
				}
				var price1=getEle("Address");
				if(!isEmpty(price1))
				{
					price1.value=ls[2];	
				}
				var ku=getEle("huiyuan_tel");
				if(!isEmpty(ku))
				{
					ku.value=ls[3];	
				}
				var qq=getEle("QQ");
				if(!isEmpty(qq))
				{
					qq.value=ls[4];	
				}
				var beizhu=getEle("beizhu");
				if(!isEmpty(beizhu))
				{
					beizhu.value=ls[5];	
				}
				showshulian.innerHTML="";
				if(getEle("huiyuan").value.length>0)
				{
					setHuiyuanPrice();
				}
			}
			getEle("search_suggestHuiyuan").style.display="none";
		}
		else
		{
            alert("Abnormal");
        }
    }
}

function clearHuiyuan(def)
{
	getEle("huiyuan3").value=def;
	getEle("huiyuan_name").value="";
	getEle("huiyuan").value="0";
	getEle("huiyuan2").value=def;
	if(!isEmpty(getEle("khly_sel")))
	{
	getEle("khly_sel").value="";
	getEle("huiyuan_tel").value="";
	getEle("QQ").value="";
	getEle("Address").value="";
	}
}

function SetLast()
{
	var curid=0;
	for(var i=maxproduit;i>=1;i--)
	{
		var cailiaohan=getEle("cailiaohan"+i);
		if(!isEmpty(cailiaohan))
		{
			if(cailiaohan.style.display!="none")
			{
				curid=i;
				break;
			}
		}
	}
	if(curid>0)
	{
		var huohao=getEle("huohao"+curid);
		if(!isEmpty(huohao))
		{
			huohao.select();
			huohao.focus();
		}
	}	
}

function setHuiyuanPrice()
{
	var selid="",selidp="",huiyuan=getEle("huiyuan");
	if(!isEmpty(huiyuan))
	{
		if(!isEmpty(huiyuan.value))
		{
			var u="../huiyuan/gethuiyuan.asp?id="+huiyuan.value+"&d="+NewDate();
			var loader=new net.ContentLoader(u,callHuiyuanBeizhu);
		}
	}
	for(var i=1;i<=maxproduit;i++)
	{
		var objhuohao=getEle("huohao"+i);
		if(!isEmpty(objhuohao))
		{
			var huohao=objhuohao.value;
			if(huohao!=emptyProduit)
			{
				var idp=getEle("id_produit"+i);
				if(!isEmpty(idp))
				{
					selid=selid+i+",";
					selidp=selidp+idp.value+",";
				}
			}
		}
	}
	if((selid.length>1) && (selidp.length>1))
	{
		var url="../huiyuan/getHuiyuanPrice.asp?idx="+selid+"&id="+selidp+"&hid="+huiyuan.value+"&d="+NewDate();
		var loader=new net.ContentLoader(url,callHuiyuanPrice);
	}
}

function callHuiyuanPrice()
{
    if(this.req.readyState==4)
    {
        if(this.req.status==200)
		{
            var list=this.req.responseText;
			var ls=list.split("|");
			for (i=0;i<ls.length;i++ )
			{
				var lss=ls[i].split("$");
				if(lss.length==2)
				{
					var idx=lss[0],p=lss[1];
					if(p!="0")
					{
						getEle("price1"+idx).value=p;	
					}
				}
			}
		}
	}
	else
	{
		alert("Abnormal");
	}
}

function callHuiyuanBeizhu()
{
    if(this.req.readyState==4)
    {
        if(this.req.status==200)
		{
            var list=this.req.responseText;
			var ls=list.split("$");
			getEle("beizhu").value=ls[0];
			if(StrToNum(ls[1])>0)
			{
			$('select[name="id_login"]').val(ls[1]);
			}
		}
	}
}