var weburl = '';
//购物车
$(document).ready(function(){
	$('input[@name=quantity]').keyup(function(){
	    var curobj = $(this);
	    curobj.val(curobj.val().replace(/\D/g,""));   //替换非数字
	    var memberPrice = curobj.siblings('input[@name=memberPrice]').val();   //会员单价
	    var oItemTotal = curobj.siblings('span.itemTotal');  //显示每项商品价格的元素
	    
	    var recordID = curobj.attr('id').replace('quantity','');
	    var gid = $('#gid_' + recordID ).val();
	    var pid = $('#pid_' + recordID).val();
		var weburl = "ajax.aspx?NoCopyRight=1&type=UpdateCartNum&recordID=" + recordID + "&q=" + curobj.val() + "&GID=" + gid + "&productid=" + pid;
		$.ajax({
		    url:weburl,
		    cache:false,
		    async:false,
		    success:function(data){
		        var msg = data.split('^');
		        if(msg[4] == 'nostorage'){
		            alert('输入的数量大于了剩余库存量');
		            curobj.focus();
		            return false;
		        }else if(msg[4] == 'nobuylimit'){
		            alert('输入的数量大于了抢购剩余数量');
		            curobj.focus();
		            return false;
		        };
		        if(Number(curobj.val()) < 1 || curobj.val() == ""){
	                curobj.val('');
	                oItemTotal.html(memberPrice);
	                return false;
	            }else{
	                var itotal = Number(memberPrice) * Number(curobj.val());
	                oItemTotal.html(itotal.toFixed(2));
	            }

	            var total = 0;
		        var total_quantity = 0;
		        var total_weight = 0;
		        var total_score = 0;
        		
		        $('input[@name=quantity]').each(function(){
		            total_quantity = Number(total_quantity) + Number($(this).val())
		        }); 
        		
		        $('input[@name=weight]').each(function(){
		            total_weight = Number(total_weight) + Number($(this).val()) * Number($(this).siblings('[@name=quantity]').val());
		        });  
        		
		        $('#total_quantity').html(total_quantity);
		        $('#totalWeight').html(Number(total_weight).toFixed(3));
		        $('#total').text(msg[2]);
		        $('#favourable').html(msg[0]);
		        //如果使用积分制度,显示积分
		        if(markUnit == '1'){
		            $('#totalScore').html(msg[5]);
		            $('#usedscore').html(msg[6]);
		        };
		    },error:function(){alert('change quantity error');}
		});// end ajax
	});
	
	$('input[@name=quantity]').blur(function(){
	    var recID = $(this).attr('id').replace('quantity','');
	    var min = $('#min_' + recID).val();
	    if(Number($(this).val()) < Number(min) || $(this).val() == "")
	    {
	        alert('购买数量不能小于最少购买数量' + min);
	        $(this).val(min);
	        $(this).keyup();
	        return false;
	    };//end if
	});
	
	//减少数量
	$('[@id^=reduce_]').each(function(i){
	    var recID = $(this).attr('id').replace('reduce_','');
	    $(this).click(function(){
	        var objNum = $('#quantity' + recID);
	        var min = $('#min_' + recID).val();
	        if(Number(objNum.val()) <= Number(min)){
	            alert('购买数量不能小于最少购买数量' + min);
	            return false;
	        }else{
	            objNum.val(Number(objNum.val())-1);
	            objNum.keyup();
	        }
	    });
	});
	//增加数量
	$('[@id^=add_]').each(function(i){
	    var recID = $(this).attr('id').replace('add_','');
	    $(this).click(function(){
	        var objNum = $('#quantity' + recID);
	        objNum.val(Number(objNum.val())+1);
	        objNum.keyup();
	    });
	});
	
	//删除商品
	$('a[@id^=a_]').click(function(){
		if(confirm('确定要从购物车中删除此商品吗？')){
			return true;
		}
		return false;
	});
	
	
	//积分支付
	$('[@id^=use_]').click(function(){
	    var recID = $(this).attr('recID');
	    $(this).hide();
	    $('#score_'+recID).show();
	    $('#usedscore_'+recID).show();
	    $('#scoreinfo_'+recID).hide();
	});
	
	$('[@id^=cancel_]').click(function(){
	    var recID = $(this).attr('recID');
	    var result = ChangeScore(recID,$('#pid_'+recID).val(),0);
	    if(result == '1'){
	        $('#score_'+recID).hide();
	        $('#use_'+recID).show();
	        $('#usedscore_'+recID).val('0');
	    };
	});
	
	$('[@id^=usedscore_]').keyup(function(){
	    if($.trim($(this).val()) != $.trim($(this).attr('oldvalue'))){
	        var recID = $(this).attr('recID');
	        $('#save_'+recID).show();
	    };
	});
	
	$('[@id^=save_]').click(function(){
	    var recID = $(this).attr('recID');
	    var score = $('#usedscore_'+recID).val();
	    if(!isInt(score)){alert('积分填写不正确!');return false;};
	    if(Number(score) == 0){
	        $('#cancel_'+recID).click();
	    }else{
	        ChangeScore(recID,$('#pid_'+recID).val(),score);
	    };//end else
	});//end click
});//end ready

function ChangeScore(recID,pid,score){
    var result = '';
    $.ajax({
        url:'ajax.aspx?NoCopyright=1&type=ChangeScore&RecordID=' + recID + '&PID=' + pid + '&usescore=' + score,
        cache:false,
        async:false,
        success:function(msg){
            result = msg;
            switch(msg){
                case "NOLOGIN":
                    $('#usedscore_'+recID).val($('#usedscore_'+recID).attr('oldvalue'));
                    if(confirm('操作积分前请先登陆!是否现在就登陆?')){
                        window.location.href = '/' + ShopFolder + 'Login.aspx?ReturnUrl=' + window.location.href;
                    };
                break;
                case "NOSCORE":
                    alert('您的可用积分不足');
                    $('#usedscore_'+recID).val($('#usedscore_'+recID).attr('oldvalue'));
                break;
                case "Beyond":
                    alert('输入的积分超过了允许接受的最大积分数.');
                    $('#usedscore_'+recID).val($('#usedscore_'+recID).attr('oldvalue'));
                break;
                default:
                    $('#save_'+recID).hide();
                    $('#usedscore_'+recID).attr('oldvalue',score).hide();
                    $('#scoreinfo_'+recID).text('您已使用积分抵扣' + Number(scoreRate * score).toFixed(2) + '元').show();
                break;
            }
        },error:function(){alert('error');}
    });//end ajax
    return result;
};

function isInt(val){
	var reg = new RegExp(/\D/g);
	if(reg.test(val) || $.trim(val) == ''){
	    return false;
	};
	return true;
};