﻿	function showsubmit()
	{
		e_obj = document.getElementById("submitbutton");
		e_obj.disabled=false;
		showelement("buybutton");
	}
	function hiddensubmit()
	{
		e_obj = document.getElementById("submitbutton");
		e_obj.disabled=true;
		hiddenelement("buybutton");
	}
	function showelement(e)
	{
		e_obj = document.getElementById(e);
		e_obj.style.display = "";
	}
	function hiddenelement(e)
	{
		e_obj = document.getElementById(e);
		e_obj.style.display = "none";
	}
	function showbuytype()
	{		
//		showelement("buyername");
//		showelement("buyeremail");
//		showelement("buyertel");			
//		item.ordertype = "o";
	}
	function hiddenbuytype()
	{
	//	hiddenelement("buyername");
	//	hiddenelement("buyeremail");
	//	hiddenelement("buyertel");	
	//	item.ordertype = "s";
	}
	
	// 入口函数
	function startorder(buycount)
	{
		if (buycount == 0)
			return ;
					
		item.buycount = buycount ;		
		
		if (item.userbalance > (item.sellprice - item.score) * item.buycount)	// 正常购买
		{
			fullorder();
			return ;
		}
		c_buycount = Math.floor (item.userbalance / (item.sellprice - item.score)) ;		
		if (c_buycount > 0 && c_buycount < item.buycount)		// 按用户余额购买
		{
			factorder(c_buycount);
			return ;
		}
	//	if ((item.sellprice - item.score)+item.poundage- item.userbalance< item.maxoverdraft)	// 透支
	//	{
	//		oneorder();
	//		return ;
	//	}
		
		// 不能购买
		hiddensubmit();
		hiddenelement("score");
	//	hiddenelement("poundage");
		hiddenelement("totalprice");
		showelement("transfer");
		
		
		return ;
	}
	
	function fullorder()
	{		
		showelement("score");
	 //   showelement("poundage");
		showelement("totalprice");
		showsubmit();
	//	hiddenpoundage ();
		changescore();
		changetotal();
	}
	function factorder(count)
	{
		alert("余额不足，只可购买 " + count + " 张");				
		countselect(count);
		item.buycount = count ;
		fullorder();
	}
	function oneorder()
	{
		alert("您的余额不足，但目前仍可透支 1 张");
		showelement("score");
	//	showelement("poundage");
		showelement("totalprice");
		showsubmit();
		item.buycount = 1 ;
		countselect(1);
		item.isoverdraft = true;
		setElement("agreeoverdraft", 1);
	//	displaypoundage ();
		changescore();
		changetotal();
	}
	
	function setElement(objs, val)
	{
		var obj = document.getElementById(objs);
		obj.value=val;
	}
	
	function hiddenpoundage()
	{
	//	obj = document.getElementById("poundage");
	//	obj.style.display = "none";	
	}
	function displaypoundage()
	{
	//	obj = document.getElementById("poundage");
	//	obj.style.display = "";	
	}
	
	function changescore()
	{
		obj = document.getElementById("scorevalue");
		obj.innerHTML = item.score + " * " + item.buycount + " = " + (item.score * item.buycount);
	}
	
	function changetotal()
	{
		obj = document.getElementById("totalpricevalue");
		item.totalscore = item.score * item.buycount ;
		item.totalprice = item.sellprice * item.buycount - item.totalscore ;
		str = item.sellprice + " * " +  item.buycount + " - " + item.score + " * " + item.buycount  ;
	//	if (item.isoverdraft) {
	//		str += " + " + item.poundage ;
	//		item.totalprice += item.poundage ;
	//	}
		str += " = " + item.totalprice;
		obj.innerHTML = str ;
	}	
	
	function countselect(val)
	{
		var theselect = document.getElementById("buycountselect");
		theselect.selectedIndex = val;		
	}
	
	function checkEmpty()
	{	
		obj = document.getElementById("buycountselect");
		if (obj.value == 0)
		{
			alert("请选择数目");
			return false;			
		}
				
		if (item.ordertype == "o")
		{
			obj_name = document.getElementById("buyernamevalue");
			if (obj_name.value == "")
			{
				alert("没输入名字");
				obj_name.select();
				obj_name.focus();
				return false ;
			}
			obj_tel = document.getElementById("buyertelvalue");
			if (obj_tel.value == "")
			{
				alert("没输入电话");
				obj_tel.select();
				obj_tel.focus();
				return false ;
			}			
			return checkEmail();
		}
		return true;				
	}
	
	function checkEmail()
	{
		var emailReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
		obj = document.getElementById("buyeremailvalue");
		if (obj.value == "")
		{
			alert("Email 没输入");
			obj.select();
			obj.focus();
			return false;
		}
		if ((!emailReg.test(obj.value))&&(obj.value!=""))
		{
			window.alert ('Email的格式不正确');
			obj.select();
			obj.focus();
			return false;
		}
		return true;
	}
