﻿
//购物车
$(document).ready(function(){
	$('input[@name=quantity]').keyup(function(){
	    $(this).val($(this).val().replace(/\D/g,""));   //替换非数字
	    var memberPrice = $(this).siblings('input[@name=memberPrice]').val();   //会员单价
	    var itemPrice = $(this).siblings('input[@name=fieldItemPrice]').val();  //配件单价
	    var oItemTotal = $(this).parent().next().find('span');  //显示每项商品价格的元素
	    
	    if(Number($(this).val()) < 1 || $(this).val() == "")
	    {
	        $(this).val('');
	        oItemTotal.html('0');
	        return false;
	    }
	    else
	    {
	        var itotal = Number(memberPrice) * Number($(this).val()) + Number(itemPrice) * Number($(this).val());
	        oItemTotal.html(itotal.toFixed(2));
	    }
	    
	    var total = 0;
		var total_quantity = 0;
		
		$('span.itemTotal').each(function(){
		    total = Number(total) + Number($(this).html());
		});
		
		$('input[@name=quantity]').each(function(){
		    total_quantity = Number(total_quantity) + Number($(this).val())
		}); 
		
		$('#total').html(total.toFixed(2));
		$('#total_quantity').html(total_quantity);
		
		var recordID = $(this).siblings('input[@name=recordID]').val()
		var weburl = "ajax.aspx?recordID="+recordID+"&q=" + $(this).val() + "&ran=" + Math.random();
		$.get(weburl);
	   
	}); //end keyup
	
	$('input[@name=quantity]').blur(function(){
	    if(Number($(this).val()) < 1 || $(this).val() == "")
	    {
	        $(this).val('1');
	        
	        var memberPrice = $(this).siblings('input[@name=memberPrice]').val();   //会员单价
	        var itemPrice = $(this).siblings('input[@name=fieldItemPrice]').val();  //配件单价
	        var oItemTotal = $(this).parent().next().find('span');  //显示每项商品价格的元素
	        
	        var itotal = Number(memberPrice) * Number($(this).val()) + Number(itemPrice) * Number($(this).val());
	        oItemTotal.html(itotal.toFixed(2));
	        
	        var total = 0;
		    var total_quantity = 0;
    		
		    $('span.itemTotal').each(function(){
		        total = Number(total) + Number($(this).html());
		    });
    		
		    $('input[@name=quantity]').each(function(){
		        total_quantity = Number(total_quantity) + Number($(this).val())
		    }); 
    		
		    $('#total').html(total.toFixed(2));
		    $('#total_quantity').html(total_quantity);
    		
		    var recordID = $(this).siblings('input[@name=recordID]').val()
		    var weburl = "ajax.aspx?recordID="+recordID+"&q=" + $(this).val() + "&ran=" + Math.random();
		    $.get(weburl);
	        
	    }
	});
	
});