$(document).ready(function(){
    $('#loadingContainer').ajaxStart(function()
    {
	    $(this).show();
	    $('#loadingbackground').show().css({"opacity": "0.5"});
	}).ajaxStop(function()
	{
	    $(this).hide();
	});
	
	if(getQueryStringRegExp('Action').replace('#','') != 'Modify' && getQueryStringRegExp('Action').replace('#','') != 'Add' )
    {
    
        setInterval(function(){
            $('div.sub_container_page').css('top',$(window.parent.frames.right).height()-$('div.sub_container_page').height()-2); //设置底部分页区域的位置
        },100);
    
        search_urldata = 'type=ajax&Action=SearchCouponsAjax&PageSize='+PageSize+'&Page='+CurrentPage;
        BindCoupons('ExchangeCoupons.aspx',search_urldata);//绑定数据        
        
        //--添加兑换规则
        $('#btnAddCoupons').click(function(){
            location.href="ExchangeCoupons.aspx?Action=Add";
        });
		
        
        //全选
        $('input[@name=chkAll]').click(function(){
            $('input[@name=chkItem]').attr('checked',$(this).attr('checked'));
        });
        
        
        //批量删除优惠券
        $('#btnDelete').click(function(){
            var couponsIDs = '';
            $('input[@name=chkItem][@checked]').each(function(){
                couponsIDs += $(this).val() + ',';
            });
            if(couponsIDs != ''){
                couponsIDs = couponsIDs.substring(0,couponsIDs.length-1);
                if(confirm('确定要删除选中的优惠券的兑换规则吗?删除将不可恢复'))
                {
                    $.get('ExchangeCoupons.aspx?type=ajax&Action=DeleteExchangeCoupons&Cids=' + couponsIDs);
					$('input[@name=chkItem][@checked]').each(function(){
						$(this).parent().parent().remove();
					});
				}
            }else{
                alert('请选择要删除的记录');
            }
        });
        
        
    }
    else
    {
        if(getQueryStringRegExp('Action').replace('#','') == 'Modify')
        {
            $('#btnSave').click(function(){
                if($.trim($('#txtCouponsPoint').val()) == '')
                {
                    alert('请填写积分兑换数额！');
                    return false;
                }
	        });
        }
        else
        {
            $('#btnSave').click(function(){
                if($('#ddlCouponsName').val() == null)
                {
                    alert('没有优惠券！');
                    return false;
                }
                if($.trim($('#txtCouponsPoint').val()) == '')
                {
                    alert('请填写积分兑换数额！');
                    return false;
                }
                else
                {
                    var point = /^[1-9]\d*$/;
                    if(!point.test($.trim($('#txtCouponsPoint').val())))
                    {
                        alert('积分兑换数额填写错误,只能为正整数！');
                        return false;
                    }
                }
	        });			
        }
		//--取消
		$('#btnCancel').click(function(){
			location.href="ExchangeCoupons.aspx?Action=Manage";
		});
    }
});