var PageSize = 15;
var CurrentPage = 1;
function setList()
{
	var pageWidth=document.documentElement.clientWidth-9;
	$(".sub_container_content").css("width",pageWidth);
	$(".sub_container_listhead").css("width",pageWidth);
	$(".llist").css("width",pageWidth);
	var c1_width = pageWidth*0.05+"px";
	var c2_width = pageWidth*0.05+"px";
	var c3_width = pageWidth*0.20+"px";
	var c4_width = pageWidth*0.07+"px";
	var c5_width = pageWidth*0.15+"px";
	var c6_width = pageWidth*0.05+"px";
	var c7_width = pageWidth*0.10+"px";
	var c8_width = pageWidth*0.10+"px";
	var c9_width = pageWidth*0.05+"px";
	var c10_width = pageWidth*0.05+"px";
	var c11_width = pageWidth*0.05+"px";
	var c12_width = pageWidth*0.05+"px";
	
	$('.c1').css("width",c1_width);
	$('.c2').css("width",c2_width);
	$('.c3').css("width",c3_width);
	$('.c4').css("width",c4_width);
	$('.c5').css("width",c5_width);
	$('.c6').css("width",c6_width);
	$('.c7').css("width",c7_width);
	$('.c8').css("width",c8_width);
	$('.c9').css("width",c9_width);
	$('.c10').css("width",c10_width);
	$('.c11').css("width",c11_width);
	$('.c12').css("width",c12_width);
}

//绑定数据
function BindCoupons(weburl,urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:weburl+'?NoCopyRight=1',
        data:urldata,
        cache:false,
        success:function(msg)
        {
			//document.write(msg);
			$('input[@name=chkAll]').attr('checked','');
            $('tr:gt(0)','#datas').remove();
            var data = msg.Coupons;
            $.each(data,function(i,n){
                var row = $('#template').clone();
                row.find('#checkbox').html('<input type="checkbox" name="chkItem" value="' + n.CouponID + '" />');//checkbox
                row.find('#DownLoad').html(n.CouponType == '1' ? ('<span><u>下载</u></span>') : '－')
                    .find('span').click(function(){
                        var i=parseInt(prompt('请输入需要下载优惠券的数量：',20));
                        if(i)
                        {
                            $('#downloadcoupons').attr('src',('downloadcoupons.aspx?CouponID=' + n.CouponID + '&downloadsum=' + i + "&r=" + Math.random()));
                        }
                    });
                row.find('#CouponName').text(n.CouponName+(n.Reamrk=="" ? "" : "("+n.Remark+")"));
                row.find('#CouponValue').text(n.CouponValue);
                row.find('#CouponNum').text(n.GenQuantity+'/'+n.TotalNumber); 
                row.find('#Status').text(n.Status);
                row.find('#Type').text(n.Type);
                row.find('#MinMoney').text(n.MinMoney);
                row.find('#TimeBegin').text(n.BeginTime.split(' ')[0]);
                row.find('#TimeEnd').text(n.EndTime.split(' ')[0]);
                row.find('#Modify').html('<a href="CouponAdd.aspx?cid=' + n.CouponID + '">修改</a>').find('a').click(function(){
                    if(CanModifyCoupons=="False"){
                        alert("你没有修改优惠券的权限，请联系超级管理员！");
                        return false;
                    }
                });
                row.find('#Delete').html('<a href="#">删除</a>')
                   .find('a').click(function(){
                        if(CanDeleteCoupons=="True"){
                            if(confirm('确定要删除该优惠券吗?删除后将不可恢复')){
                                $.ajax({
                                    url:'Coupons.aspx?type=ajax&Action=DeleteCoupons&CouponsIDs=' + n.CouponID,
                                    cache:false,
                                    success:function(data){
                                        if(data > 0){
                                            row.remove();
                                        }
                                        else if(data==-1){
                                            alert("你没有删除优惠券的权限，请联系超级管理员！");
                                        }
                                    },error:function(){alert('error')}
                                });
                            }
                            return false;
                        }
                        else{
                            alert("你没有删除优惠券的权限，请联系超级管理员！");
                        }
                   });

				row.attr("id","ready_" + n.CouponID);//改变绑定好数据的行的id
				row.show().mouseover(function(){$(this).css("cursor","pointer");}); //将复制的行显示出来并添加样式
				row.find('#Modify').siblings(':not("#Delete"):not("#checkbox"):not("#DownLoad")').click(function(){
				    if((CanAddCoupons=="True")||(CanModifyCoupons=="True")){
				        var rowid = row.attr('id').split('_')[1];
				        if($('#rulerow'+rowid).length == 0)
				        {
				            $('tr[@id^=rulerow]').remove();
				            //--生成优惠券发放途径,首先获得优惠券发放途径的Json数据
				            $.ajax({
                                type:"get",
                                dataType:"json",
                                url:'Coupons.aspx'+'?NoCopyRight=1',
                                data:'type=ajax&Action=SearchPromotionAjax&CouponID=' + n.CouponID,
                                cache:false,
                                async:false,
                                success:function(msg)
                                {
                                    $('input[@name=chkRuleAll]').attr('checked','');
                                    $('tr:gt(0)','#ruledatas').remove();
                                    var data = msg.Promotion;                                
                                    $.each(data,function(i,n){
                                        var rulerow = $('#ruletemplate').clone(true);
				                        //rulerow.find('#rulecheckbox').html('<input type="checkbox" name="chkRuleItem" value="' + n.ruleid + '" />');
				                        var ptype=1;
                                        switch(n.basictype)
                                        {
                                            case "register"://注册赠券
                                                ptype=1;
                                                break;
                                            case "goods"://单品返券
                                                ptype=2;
                                                break;
                                            case "orders"://订单满额赠券
                                                ptype=3;
                                                break;
                                        }
				                        rulerow.find('#ruleOperate').html(n.rulename+'<span style="margin-left:10px;"><a href="couponpmtadd_step2.aspx?ruleid='+n.ruleid+'&ptype='+ptype+'"><font color="blue">[修改]</font></a></span>');
				                        rulerow.find('#control').html('<img class="hand" onclick="DelPmt('+n.ruleid+')" src="../images/x-red.gif" alt="删除" />');
				                        rulerow.show();
				                        rulerow.attr("id","ruletemplate_" + n.ruleid);
				                        rulerow.appendTo("#ruledatas");                  
                                    });
                                },
                                error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
                            });
                            
				            row.after('<tr id="rulerow'+rowid+'"><td colspan="11">' + $('#rule').html().replace('btnAddRule','btnAddRule_'+rowid).replace('chkRuleAll','chkRuleAll_'+rowid) + '</td></tr>');
				            $('tr:gt(0)','#ruledatas').remove();				        
				            $('#btnAddRule_'+rowid).click(function(){window.location='CouponPmtAdd_Step1.aspx?Action=Manage';});
				            $('#chkRuleAll_'+rowid).click(function(){
                            $('input[@name=chkRuleItem]').attr('checked',$(this).attr('checked'));
                        });
				    }
				    else
				    {
				        if($('#rulerow'+rowid).is(":visible"))
                        {
                            $('#rulerow'+rowid).hide();
                        }
                        else
                        {
                            $('#rulerow'+rowid).show();
                        }
				    }                        }else{
                            alert("你没有修改优惠券的权限，请联系超级管理员！");
                        }

				});
				row.appendTo("#datas");//添加到模板的容器中
            });

            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "Binding.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindCoupons('Coupons.aspx','type=ajax&Action=SearchCouponsAjax&PageSize=" + PageSize + "&Page={pageindex}');return false;"}
            );
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}

//去掉字符串前后空格
String.prototype.Trim = function() { 
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

function checkLoginStatus(data){
    var result = true;
    if(data.indexOf('<!DOCTYPE') != -1){
        result = false;
        alert('身份已过期,请重新登陆!');
    }
    return result;
}

function DelPmt(ruleid)
{
    if(confirm('确定要删除该派发规则吗?删除将不可恢复'))
    {
        $.get('pmtrule_list.aspx?type=ajax&action=DeletePmtRule&rids=' + ruleid);
        $("#ruletemplate_"+ruleid).remove();
    }
    return false;
}