
var PageSize = 20;//每页计数
var CurrentPage = 1; //当前页
var isNotified = 0;

//列表查询条件
var search_urldata = 'type=ajax&Action=GetList&PageSize='+PageSize+'&Page='+CurrentPage;

$(document).ready(function(){
    //loading
    $('#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()-10); //设置内容区域的高度
    },100);

    $('#btnNotify').click(function(){
        var ids = GetIDs();
        if(ids != ''){
            $.ajax({
                url:'OutStockReg.aspx?NoCopyRight=1&type=ajax&Action=Notify&IDs='+ids,
                success:function(data){
                    if(!checkLoginStatus(data)){return false;}
                    ShowMsgBox1(5000,"发送成功:"+data.split('|')[0] + "个,失败:" + data.split('|')[1] + "个","OutStockReg.aspx");
                },
                error:function(){alert('error');}
            });
        }else{
            alert('请选择要发送通知的项');return false;
        };
    });

    $('input[@name=chkAll]').click(function(){$('input[@name=chkItem]').attr('checked',$(this).attr('checked'));});//全选

    $('#btnDelete').click(function(){
        var ids = GetIDs();
        if(ids != ''){
            if(confirm('确定要删除选中项吗?')){
                Delete(ids);
            }
        }else{
            alert('请选择要删除的项');return false;
        };
    });    

    isNotified = $('#ddlIsNotified').val();
    search_urldata = 'type=ajax&Action=GetList&PageSize='+PageSize+'&Page='+CurrentPage+'&IsNotified='+isNotified;
    BindList('OutStockReg.aspx',search_urldata);
    
    $('#ddlIsNotified').change(function(){
        search_urldata = 'type=ajax&Action=GetList&PageSize='+PageSize+'&Page='+CurrentPage+'&IsNotified='+$('#ddlIsNotified').val();
        BindList('OutStockReg.aspx',search_urldata);
    });
});    

function GetIDs(){
    var ids = '';
    $('input[@name=chkItem][@checked]').each(function(){ids += $(this).val() + ',';});
    if(ids != ""){ids = ids.substring(0,ids.length-1)};
    return ids;
};

//绑定数据
function BindList(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.OutStockReg;
            $.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.OutStockRegID + '" />');//checkbox
                row.find('#ProductName').html(n.ProductName + (n.ProductSpecs != '' ? '(' + n.ProductSpecs + ')' : ''));
                row.find('#Storage').show().html(n.Storage);
                row.find('#MemberName').html(n.MemberName);
                row.find('#Email').html(n.Email);
                row.find('#AddTime').html(n.AddTime);
                row.find('#NotifyTime').html(n.IsNotified == 'True' ? n.NotifyTime : '未通知');
                row.find('#Delete').html('<span class="hand">删除</span>').find('span').click(function(){if(confirm('确定要删除吗?')){Delete(n.OutStockRegID);}});

				row.attr("id","ready_" + n.OutStockRegID);//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
            });
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "OutStockReg.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindList('OutStockReg.aspx','type=ajax&Action=GetList&PageSize=" + PageSize + "&Page={pageindex}');return false;"}//[attr]
            );
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!');}else{alert(textStatus)};}
    });//end ajax
};//end funciton

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

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

//获取URL中参数
function getQueryStringRegExp(name){    
	var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
	if (reg.test(location.href))
		return unescape(RegExp.$2.replace(/\+/g, " "));
		return "";
};
