

$(document).ready(function(){

	//弹出层
	$("#btnAddGift").click(function(){
		 $('#giftPopupContact').popup({width:'80%',height:'80%',popupHead:'giftPopupHead',btnClose:'giftPopupContactClose'});
		if(giftstatus == 0)
		{BindGift('PmtRule.aspx','type=ajax&Action=SearchGift');giftstatus = 1;};
	});//如果是第一次弹出层,要获取商品列表,否则只需要显示层即可 
    	
	if($.browser.mozilla){setInterval(setList,100);}
	$('.sub_container_content').height(400);
    $('input[@name=chkGiftAll]').click(function(){$('input[@name=chkGiftItem]').attr('checked',$(this).attr('checked'));});//全选
	
    
    //搜索
    $('#btnGiftSearch').click(function(){
        keyword = $('#txtGiftKeyword').val();
        searchType = $('#gift_sel_searchType').val();
        search_urldata = 'type=ajax&Action=SearchGiftAjax&keyword=' + escape(keyword) + '&SearchType=' + searchType;
        BindGift('PmtRule.aspx',search_urldata);
    });
    //--确认礼品
    $('#btnGiftSubmit').click(function(){
        var giftIDs = '';
        var giftNames = '';
        var selectedgiftIDs = '';
        if($('#giftIDs').val() != ''){
            selectedgiftIDs = ','+$('#giftIDs').val()+',';
        };
        $('input[@name=chkGiftItem][@checked]').each(function(){
            var currobj = $(this);
            if(selectedgiftIDs.indexOf(',' + currobj.val() + ',') == -1){
                var giftName = currobj.parent().siblings('#giftName').text();
                var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除" /><input type="hidden" name="giftID" value=' + currobj.val() + '><input type="hidden" name="giftName" value="' + giftName + '"></td><td class="c8" style="overflow-x:hidden;">' + giftName + '</td></tr>');
                row.find('.delete').css('cursor','pointer').click(function(){
                    $(this).parent().remove();
                    $('#giftcount').text(Number($('#giftcount').text())-1);
                    //--礼品ID
                    var gids = '';
                    $('input[@name=giftID]').each(function(){
                        gids += $(this).val() + ',';
                    });
                    if(gids != ''){gids = gids.substring(0,gids.length-1);};
                    $('#giftIDs').val(gids);
                    
                    //--礼品名称
                    var gNames = '';
                    $('input[@name=giftName]').each(function(){
                        gNames += $(this).val() + ',';
                    });
                    if(gNames != ''){gNames = gNames.substring(0,gNames.length-1);};
                    $('#giftNames').val(gNames);
                    
                }).find('img').mouseover(function(){
                    $(this).attr('src','../images/x-red.gif');
                }).mouseout(function(){
                    $(this).attr('src','../images/x-gray.gif');
                });
                $('#giftlistresult').append(row);
                giftIDs += currobj.val() + ',';
                giftNames += giftName.replace(',',' ') + ',';
            };
        });
        if(giftIDs != ""){
            giftIDs = giftIDs.substring(0,giftIDs.length-1);
            giftNames = giftNames.substring(0,giftNames.length-1);
            $('#giftIDs').val($('#giftIDs').val() + ($('#giftIDs').val() != '' ? ',' : '') + giftIDs);//隐藏控件记录已选择的商品ID(逗号分隔1,2,3)
            $('#giftNames').val($('#giftNames').val() + ($('#giftNames').val() != '' ? ',' : '') + giftNames);
        };
        $('#giftPopupContactClose').click();
        $('#giftcount').text($('input[@name=giftID]').length);
    });
    
    //--清空
    $('#btnClearGift').click(function(){
        $('#giftcount').text('0');
        $('tr:gt(0)','#giftlistresult').remove();
        $('#giftIDs').val('');
        $('#giftNames').val('');
    });  
});

function BindGift(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg){
            $('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#giftdatas').remove();
            var data = msg.Gift;
            $.each(data,function(i,n){
                var row = $('#gifttemplate').clone();
                row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
                row.find('#giftcheckbox').html('<input type="checkbox" name="chkGiftItem" value="' + n.GiftID + '" />');//checkbox
                row.find('#giftName').html('<div style="overflow:hidden;width:220px;white-space:nowrap;">'+n.GiftName+'</div>'); //商品名称
                row.find('#giftPoint').text(n.GiftPoint);   //兑换积分
                row.find('#giftGenQuantity').html('&nbsp;');
				row.find('#giftStatus').html('&nbsp;');
				row.find('#giftType').html('&nbsp;');
				row.attr("id","giftready_" + n.GiftID);//改变绑定好数据的行的id
				row.appendTo("#giftdatas");//添加到模板的容器中
            });
            
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}
