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.04+"px";
	var c2_width = pageWidth*0.18+"px";
	var c3_width = pageWidth*0.05+"px";
	var c4_width = pageWidth*0.10+"px";
	var c5_width = pageWidth*0.10+"px";
	var c6_width = pageWidth*0.30+"px";
	var c7_width = pageWidth*0.06+"px";
	var c8_width = pageWidth*0.06+"px";
	
	var cc1_width = pageWidth*0.05+"px";
	var cc2_width = pageWidth*0.10+"px";
	var cc3_width = pageWidth*0.10+"px";
	var cc4_width = pageWidth*0.30+"px";
	var cc5_width = pageWidth*0.45+"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);
	
	
}

//绑定数据
function BindActivitys(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)','#datas').remove();
            var data = msg.PromotionActivity;
            $.each(data,function(i,n){
                var row = $('#template').clone();
                row.find('#checkbox').html('<input type="checkbox" name="chkItem" value="' + n.PromotionActivityID + '" />');//checkbox
                row.find('#ActivityName').text(n.PromotionActivityName); //活动名称
                row.find('#Status').html(n.Status == '1' ? '<font color=green>是</font>' : '<font color=red>否</font>');      //发布
                row.find('#TimeBegin').text(n.TimeBegin.split(' ')[0]); 
                row.find('#TimeEnd').text(n.TimeEnd.split(' ')[0]);
                row.find('#Description').text(n.Description);
                row.find('#Modify').html('<a href="PromotionActivity.aspx?Action=Modify&ActivityID=' + n.PromotionActivityID + '">修改</a>')
                row.find('#Delete').html('<a href="#">删除</a>')
                   .find('a').click(function()
                   {
                        if(confirm('确定要删除该促销活动吗?删除将不可恢复'))
                        {
                            $.get('PromotionActivity.aspx?type=ajax&Action=DeletePromotionActivity&ActivityIDs=' + n.PromotionActivityID);
							$(this).parent().parent().remove();
                        }
                        return false;
                   });
                
				row.attr("id","ready_" + n.PromotionActivityID);//改变绑定好数据的行的id
//				row.show().mouseover(function(){$(this).addClass('on');$(this).css("cursor","pointer");}).mouseout(function(){$(this).removeClass('on')}); //将复制的行显示出来并添加样式
				row.show().mouseover(function(){$(this).css("cursor","pointer");}); //将复制的行显示出来并添加样式
				row.find('#Modify').siblings(':not("#Delete"):not("#checkbox"):not("#Status")').click(function(){
				    var rowid = row.attr('id').split('_')[1];
				    if($('#rulerow'+rowid).length == 0)
				    {
				        $('tr[@id^=rulerow]').remove();
				        //--生成促销规则,首先获得促销规则的Json数据
                        
				        $.ajax({
                            type:"get",
                            dataType:"json",
                            url:'PromotionActivity.aspx'+'?NoCopyRight=1',
                            data:'type=ajax&Action=SearchPromotionAjax&PromotionActivityID=' + rowid,
                            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();
				                    rulerow.find('#rulecheckbox').html('<input type="checkbox" name="chkRuleItem" value="' + n.PromotionID + '" />');
				                    rulerow.find('#ruleOperate').html('<input type="button" id="rule_'+ n.PromotionID +'" value="编辑" class="inputbtn" />');
				                    rulerow.find('#ruleTimeBegin').text(n.TimeBegin.split(' ')[0]);
				                    rulerow.find('#ruleTimeEnd').text(n.TimeEnd.split(' ')[0]);
				                    rulerow.find('#ruleDescription').text(n.Description);
				                    rulerow.show();
				                    rulerow.attr("id","ruletemplate_" + n.PromotionID);//改变绑定好数据的行的id
				                    rulerow.appendTo("#ruledatas");
//				                    alert($('#rule_'+n.PromotionID).length);
				                    
                                });
                            },
                            error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
                        });
				        
				        row.after('<tr id="rulerow'+rowid+'"><td colspan="8">' + $('#rule').html().replace('btnAddRule','btnAddRule_'+rowid).replace('btnDeleteRule','btnDeleteRule_'+rowid).replace('chkRuleAll','chkRuleAll_'+rowid) + '</td></tr>');
				        $('tr:gt(0)','#ruledatas').remove();
				        
				        $('#btnAddRule_'+rowid).click(function(){window.location='Promotion.aspx?activityid=' + rowid;});
						$('#btnDeleteRule_'+rowid).click(function(){
							
							
							var promotionIDs = '';
							$('input[@name=chkRuleItem][@checked]').each(function(){
								promotionIDs += $(this).val() + ',';
							});
							if(promotionIDs != '')
							{
								promotionIDs = promotionIDs.substring(0,promotionIDs.length-1);
								if(confirm('确定要删除选中的促销规则吗?删除将不可恢复'))
								{
									$.get('PromotionActivity.aspx?type=ajax&Action=DeletePromotion&PromotionIDs=' + promotionIDs);
									$('input[@name=chkRuleItem][@checked]').each(function(){
										$(this).parent().parent().remove();
									});
								}
							}
							else
							{
								alert('请选择要删除的记录');
							}
							
							
						});
				        $('#chkRuleAll_'+rowid).click(function(){
                            $('input[@name=chkRuleItem]').attr('checked',$(this).attr('checked'));
                        });
                        
                        $('input[@id^=rule_]').click(function(){window.location='Promotion.aspx?activityid=' + rowid + '&promotionid=' + $(this).attr('id').split('_')[1];});
				    }
				    else
				    {
				        if($('#rulerow'+rowid).is(":visible"))
                        {
                            $('#rulerow'+rowid).hide();
                        }
                        else
                        {
                            $('#rulerow'+rowid).show();
                        }
				    }
				});
				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};BindActivitys('PromotionActivity.aspx','type=ajax&Action=SearchActivityAjax&PageSize=" + PageSize + "&Page={pageindex}');return false;"}
            );
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
}

function GetPromotion(weburl,urldata)
{
    
}


//更新商品几种状态的公共方法
function UpdateStatus(aObj,imgObj){
    $.ajax({
        url:aObj.attr('href')+'&NoCopyRight=1',
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;}//是否要重新登陆
            if(data == 1){//如果更新成功,改变图片
                var newhref = '';
                if(imgObj.attr('src').toLowerCase().indexOf('publish_1.png') != -1){
                    imgObj.attr('src','images/publish_0.png');
                    newhref = aObj.attr('href').replace('=false','=true').replace('IsForSale=1','IsForSale=0');
                    aObj.attr('href',newhref);//重新设置链接
                }else{
                    imgObj.attr('src','images/publish_1.png');
                    newhref = aObj.attr('href').replace('=true','=false').replace('IsForSale=0','IsForSale=1');
                    aObj.attr('href',newhref);//重新设置链接
                }
            }
        },
        error:function(){alert('error');}
    });
}



//获取URL中参数
function getQueryStringRegExp(name){    
	var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
	if (reg.test(location.href))
		return unescape(RegExp.$2.replace(/\+/g, " "));
		return "";
}
//去掉字符串前后空格
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 test(){
    alert('test ok');
}
