
var PageSize = 50;//每页计数
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.2+"px";
	var c3_width = pageWidth*0.15+"px";
	var c4_width = pageWidth*0.1+"px";
	var c5_width = pageWidth*0.1+"px";
	var c6_width = pageWidth*0.05+"px";
	var c7_width = pageWidth*0.1+"px";
	var c8_width = pageWidth*0.1+"px";
	var c9_width = pageWidth*0.08+"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);
}
$(document).ready(function() {
	$('#loadingContainer').ajaxStart(function(){$(this).show();}).ajaxStop(function(){$(this).hide();});
	setInterval(function(){
        $('div.sub_container_page').css('top',$(window.parent.frames.right).height()-$('div.sub_container_page').height()-2); //设置底部分页区域的位置
        $('div.sub_container_content').css('height',$(window.parent.frames.right).height()-$('div.sub_container_title').height()-$('div.sub_container_operation').height()-$('div.sub_container_listhead').height()-$('div.sub_container_page').height()); //设置内容区域的高度
	    if($.browser.mozilla){setList();}
    },1000);
    
    $("#txtStartTime").datepicker().click(function(){$(this).blur();});
	$("#txtEndTime").datepicker().click(function(){$(this).blur();});
    $('[@name=chkAll]').click(function(){
        $('[@name=chkItem]').attr('checked',$(this).attr('checked'));
    });
    $('#btnDelete').click(function(){
        var IDs = '';
        $('input[@name=chkItem][@checked]').each(function(){
            IDs += $(this).val() + ',';
        });
        if(IDs != ''){
            if(!confirm('确定要删除选择的记录吗?')){return false;};
            IDs = IDs.substring(0,IDs.length-1);
            Delete(IDs);
        }else{
            alert('请选择要删除的记录');
        };
    });
    
    $('#btnSearch').click(function(){
        if($('#ddlSearchType').val() == '3'){
            var posIntReg = new RegExp(/^[1-9]*[1-9][0-9]*$/g);//正整数
            var keywords = $.trim($('#txtKeywords').val());
            if(!posIntReg.test(keywords)){
                alert('商品ID必须为整数！');
                return false;
            };
        };
        CurrentPage = 1;
        BindList();
    });
 
	BindList();		
});

//绑定数据
function BindList(){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:'BuyRecored.aspx?NoCopyRight=1&type=ajax&Action=GetList&PageSize=' + PageSize + '&Page=' + CurrentPage + '&SearchType='+$('#ddlSearchType').val() + '&keyword=' +escape($.trim($('#txtKeywords').val()))+ '&MemberName=' + escape($('#txtMemberName').val()) + '&RecType=' + $('#ddlType').val() + '&StartTime=' + escape($('#txtStartTime').val()) + '&EndTime=' + escape($('#txtEndTime').val()),
        cache:false,
        success:function(msg){
            $('tr:gt(0)','#datas').remove();
            $('input[@name=chkAll]').attr('checked',false);
            var data = msg.EventLog;
            $.each(data,function(i,n){
               var row = $('#template').clone();
               row.show().mouseover(function(){$(this).addClass('on')}).mouseout(function(){$(this).removeClass('on')});
               row.find('#checkbox').html('<input type="checkbox" name="chkItem" value="'+n.BuyRecoredID+'" />');
               row.find('#pname').html('<a href="../products/product-' + n.PdtID + '.html" target="_blank">'+n.PdtName + '</a>');
               row.find('#addr').text(n.Address);
               row.find('#buyer').text(n.BuyerName);
               row.find('#membername').text(n.MemberName);
               row.find('#num').text(n.Quantity);
               row.find('#time').text(n.BuyTime);
               row.find('#type').text(n.Type == '0' ? '手动添加' : '真实记录');
               row.find('#delete').html('<span class="hand">删除</span>')
                    .find('span')
                    .click(function(){
                        if(confirm('确定要删除吗？')){
                            Delete(n.BuyRecoredID);
                        };
                    });

               row.attr('id','item_'+i).appendTo($('#datas'));
            });
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "EventLog.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindList();return false;"}//[attr]
            );
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!');}else{alert(textStatus)};}
    });//end ajax
};//end funciton

function Delete(id){
    $.ajax({
        url:'BuyRecored.aspx?type=ajax&Action=Delete&IDs=' + id,
        cache:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;}
            BindList();
        },
        error:function(){alert('error');}
    });//end ajax
};