var arrsuitid=new Array();
$(document).ready(function(){    
	$('input[@name=quantity]').livequery('keydown',function(event){
	    var curobj = $(this);
	    curobj.attr('oldvalue',curobj.val());
	    if((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 8){
	    }else{return false;}
	}).livequery('keyup',function(event){
	    var curobj = $(this);
	    var min = $(this).siblings('[@name=min]').val();
	    if(curobj.attr('oldvalue') == curobj.val() || curobj.val() == ''){return false;};
	    if(Number(curobj.val()) < Number(min)){return false;}
        var IsDlvUnit = curobj.attr('IsDlvUnit');
        var DlvUnit = curobj.attr('DlvUnit');
//        if(IsDlvUnit == 1 && DlvUnit > 0){
//            if((curobj.val() % DlvUnit) > 0){return false;}
//        }
	    
	    var recordID = curobj.siblings('input[@name=recordID]').val();
	    var gid = curobj.siblings('input[@name=gid]').val();
	    var pid = curobj.siblings('input[@name=pid]').val();
	    var ptype = curobj.siblings('input[@name=ptype]').val();
		var weburl = "ajax.aspx?NoCopyRight=1&type=UpdateCartNum&qty=" + curobj.val() + "&gid=" + gid + "&pid=" + pid + "&ptype=" + ptype;
		$.ajax({
		    url:weburl,
		    cache:false,
		    //async:false,
		    success:function(data){
		        if(data == 'nostorage'){
		            alert('输入的数量大于了剩余库存量');
		        }else if(data=='error'){
		            alert('更改数量失败');
		        }else{
		            //$('#CartInfo').hide();
		            $('#CartInfo').html(data);
		            InitViewSuit();
		            //$('#CartInfo').show();		            
		        }
		    },error:function(){alert('更改数量失败');}
		});// end ajax
	});
	$('input[@name=quantity]').blur(function(){
	    var curobj = $(this);
	    var min = curobj.siblings('[@name=min]').val();
	    if(Number(curobj.val()) < Number(min) || curobj.val() == ""){
	        alert('购买数量不能小于最少购买数量' + min);
	        curobj.val(min);
	        curobj.keyup();
	        return false;
	    }//end if
	});
	//删除商品
	$('.delpdt').livequery('click',function(){
		var curobj = $(this);
		if(confirm('确定要从购物车中删除此商品吗？')){
			var gid = curobj.parent().prev().children('input[@name=gid]').val();
			var pid = curobj.parent().prev().children('input[@name=pid]').val();
			var ptype = curobj.parent().prev().children('input[@name=ptype]').val();
			var weburl = "ajax.aspx?NoCopyRight=1&type=UpdateCartNum&qty=0&gid=" + gid + "&pid=" + pid+"&ptype="+ptype;
			$.ajax({
				url:weburl,
				cache:false,
				//async:false,
				success:function(data){
					if(data != "error"){
						$('#CartInfo').html(data);
					};
				},error:function(){alert('change quantity error');}
			});// end ajax
		}//end confirm
	});	
	
	//减少数量
	$('[@id^=reduce_]').livequery('click',function(){
        var recID = $(this).attr('id').replace('reduce_','');
        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_]').livequery('click',function(){
	    
	    var recID = $(this).attr('id').replace('add_','');
        var objNum = $('#quantity_' + recID);
        objNum.val(Number(objNum.val())+1);
        objNum.keyup();
	});
	$('.showsuit').livequery('click',function(){
	    var suitid=$(this).attr("id").split('_')[1];
	    arrsuitid.push(suitid);
	    $(this).parent().children('.showsuit').removeClass('showsuit').addClass('hidesuit');
	    $('#suitimg_'+suitid).attr('src','images/close.gif');
	    $('#suita_'+suitid).text('[收起套装商品]');
	    $('#suitd_'+suitid).addClass('suit');
	    $('#suit_'+suitid).addClass('suit').show();
	});
	$('.hidesuit').livequery('click',function(){
	    var suitid=$(this).attr("id").split('_')[1];
	    arrsuitid=$.grep(arrsuitid,function(n,i){
	        return n != suitid;
	    })
	    $(this).parent().children('.hidesuit').removeClass('hidesuit').addClass('showsuit');
	    $('#suitimg_'+suitid).attr('src','images/open.gif');
	    $('#suita_'+suitid).text('[查看套装商品]');
	    $('#suitd_'+suitid).removeClass('suit');
	    $('#suit_'+suitid).removeClass('suit').hide();
	});
});//end ready

function InitViewSuit()
{
    $.each(arrsuitid,function(i,n){
        $('#suitimg_'+n).attr('src','images/close.gif').removeClass('showsuit').addClass('hidesuit');
        $('#suita_'+n).text('[收起套装商品]').removeClass('showsuit').addClass('hidesuit');
        $('#suitd_'+n).addClass('suit');
	    $('#suit_'+n).addClass('suit').show();
    });
}