// JavaScript Document
addLoadEvent(priceCount);

function priceCount(){
	var reduce=getElementsByClassName("reduce");
	var add=getElementsByClassName("add");
	var orderNum=getElementsByClassName("orderNum");
	var totalPrice=getElementsByClassName("totalPrice");
	var price=getElementsByClassName("unitPrice");
	//����Ӧ���
	for (i=0;i<reduce.length;i++){
		reduce[i].relatedNum=orderNum[i];
		reduce[i].relatedTotalPrice=totalPrice[i];
		reduce[i].relatedPrice=price[i];
		add[i].relatedNum=orderNum[i];
		add[i].relatedTotalPrice=totalPrice[i];
		add[i].relatedPrice=price[i];
	}
	for (i=0;i<orderNum.length;i++){
		orderNum[i].relatedPrice=price[i];
		orderNum[i].relatedTotalPrice=totalPrice[i];
		orderNum[i].relatedTotalPrice.firstChild.nodeValue=orderNum[i].relatedPrice.firstChild.nodeValue*orderNum[i].value;
		orderNum[i].onchange=function(){
			this.relatedTotalPrice.firstChild.nodeValue=this.value*this.relatedPrice.firstChild.nodeValue;
		}
	}
	for(i=0;i<reduce.length;i++){
		reduce[i].onclick=function(){
			if(this.relatedNum.value>1){
				this.relatedNum.value-=1;
				count(this.relatedTotalPrice,this.relatedNum,this.relatedPrice);
				totalCount();
			}
		}
	}
	for(i=0;i<add.length;i++){
		add[i].relatedNum=orderNum[i];
		add[i].tempi=i;
		add[i].onclick=function(){
			if((this.relatedNum.value*1+1)<=document.getElementById("maxQty_"+this.tempi).value){
				this.relatedNum.value=this.relatedNum.value*1+1;
				count(this.relatedTotalPrice,this.relatedNum,this.relatedPrice);
				totalCount();
			}else{
				alert('数量超出库存');
			}
		}
	}
}
function count(totalPrice,Num,price){
	totalPrice.firstChild.nodeValue=Num.value*price.firstChild.nodeValue;
}


function checkSubmit(total){
	if(total!=0){
		document.getElementById('mall').submit();
	}else{
		alert('没有产品');
		return false;
	}	
}
function checkAddress(total){
	if(total>=0){
		document.getElementById('mall').action=document.getElementById('mall').action+'/total/'+total;
	}	
	if(document.getElementById("newAddress").checked!=true){
		
		document.getElementById('mall').submit();
		
	}else{
		if(document.getElementById("consignee").value==''){
			alert("收件人不能为空");
			document.getElementById("consignee").focus();
			return false;
		}
		if(document.getElementById("address").value==''){
			alert("地址不能为空");
			document.getElementById("address").focus();
			return false;
		}	
		if(document.getElementById("postcode").value==''){
			alert("邮政编码不能为空");
			document.getElementById("postcode").focus();
			return false;
		}			
		
		document.getElementById('mall').submit();
	}
}

function resetQty(){
	var orderNum=getElementsByClassName("orderNum");
	var totalPrice=getElementsByClassName("totalPrice");
	var price=getElementsByClassName("unitPrice");
	for (i=0;i<orderNum.length;i++){
		$('orderNum_'+i).value=$('maxQty_'+i).value;
		count(totalPrice[i],orderNum[i],price[i]);
		totalCount();
	}
}
