var PageSize = 15;//每页计数
var CurrentPage = 1; //当前页
var keyword = '';
var catID = '0';
var brandID = '0';
var searchType = '0';
var status = 0;//刚进入页面不要列出商品,点击"添加商品"后再获取列表
var showadv = 0;//高级搜索显示状态



//列表查询条件
var search_urldata = 'type=ajax&Action=SearchProductsAjax&keyword=' + escape(keyword) + '&CatID=' + catID + '&BrandIDs=' + brandID + '&SearchType=' + searchType + '&PageSize='+PageSize+'&Page='+CurrentPage;

function setList(){
	var pageWidth=$('#popupContact').width()-20;
	$(".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.30+"px";
	var c3_width = pageWidth*0.20+"px";
	var c4_width = pageWidth*0.15+"px";
	var c5_width = pageWidth*0.15+"px";
	var c6_width = pageWidth*0.15+"px";
	
	
	var c7_width = pageWidth*0.10+"px";
	var c8_width = pageWidth*0.30+"px";
	var c9_width = pageWidth*0.20+"px";
	var c10_width = pageWidth*0.40+"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",c3_width);
	$('.c8').css("width",c4_width);
	$('.c9').css("width",c5_width);	
	$('.c10').css("width",c6_width);	
}

//绑定数据
function BindProducts(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.Product;
            $.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.ProductID + '" />');//checkbox
                row.find('#Name').html('<div style="overflow:hidden;width:220px;white-space:nowrap;"><a href="../../Show.aspx?PID=' + n.ProductID + '" target="_blank">'+n.ProductName+'</a></div>'); //商品名称
                row.find('#ProductNo').text(n.ProductNo);   //编码
                var categoryname = (n.CategoryID != 0 ? $('#sel_category option[@value=' + n.CategoryID + ']').text().replace('├','') : "");
                var brandname = (n.BrandID != 0 ? $('#sel_brand option[@value=' + n.BrandID + ']').text() : "");
                row.find('#Category').text(categoryname);//商品类别
                row.find('#Brand').text(brandname);//商品品牌
                row.find('#MemberPrice').text(Number(n.MemberPrice).toFixed(2))//两位小数
				row.attr("id","ready_" + n.ProductID);//改变绑定好数据的行的id
				row.appendTo("#datas");//添加到模板的容器中
            });
           
            //分页
            $('.sub_container_page').show();
            var pagerStyle = "flickr";//jPagerBar样式
            ShowPageBar("pageDataList1_Pager",//[containerId]
               "Products.aspx",//[url]
               {style:pagerStyle,mark:"pageDataList1Mark",
               totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 没有记录",preWord:"上一页",nextWord:"下一页",
               onclick:"CurrentPage={pageindex};BindProducts('BuyLimit.aspx','type=ajax&Action=SearchProductsAjax&keyword=" + escape(keyword) + "&CatID=" + catID + "&BrandIDs=" + brandID + "&SearchType=" + searchType + "&PageSize=" + PageSize + "&Page={pageindex}');return false;"}//[attr]
            );
            /**/
            
        },//end success
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });//end ajax
}//end funciton

function GetStandardDate(date)
{
    var reg=new RegExp("/","g");
    var date2=date.replace(reg,"-");  
	var s;
	var s1 = date2.split(' ')[0];
	var s2 = date2.split(' ')[1];
	var year = s1.split('-')[0];
	var month = s1.split('-')[1].length == 1 ? ('0' + s1.split('-')[1]) : (s1.split('-')[1]);
	var day = s1.split('-')[2].length == 1 ? ('0' + s1.split('-')[2]) : (s1.split('-')[2]);
	var hour = s2.split(':')[0].length == 1 ? ('0' + s2.split(':')[0]) : (s2.split(':')[0]);
	var min = s2.split(':')[1].length == 1 ? ('0' + s2.split(':')[1]) : (s2.split(':')[1]);
	//var second = s2.split(':')[2].length == 1 ? ('0' + s2.split(':')[2]) : (s2.split(':')[2]);
	s = year + '-' + month + '-' + day + ' ' + hour + ':' + min;// + ':' + second;
	return(s);
}

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

function BindLimitProduct()
{
	//--修改限时抢购商品时，需要将相关的信息显示出来
	$.ajax({
        type:"get",
		url:'BuyLimit.aspx?NoCopyRight=1',
        data:'type=ajax&Action=GetBuyLimitedTime&CatID=' + $('#ddlTopCategories').val(),
        cache:false,
        async:false,
        success:function(msg){
			if(msg != '')
			{
				$('#defaultstarttime').val(GetStandardDate(msg.split('^')[0]));
				$('#defaultendtime').val(GetStandardDate(msg.split('^')[1]));
			}
//			else
//			{
//				$('#defaultstarttime').val('');
//				$('#defaultendtime').val('');
//			}
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
	
	$.ajax({
        type:"get",
        dataType:"json",
        url:'BuyLimit.aspx?NoCopyRight=1',
        data:'type=ajax&Action=SearchLimitProduct&CatID=' + $('#ddlTopCategories').val(),
        cache:false,
        async:false,
        success:function(msg){
            var data = msg.Product;
            $.each(data,function(i,n)
            {
				var row = $('<tr><td class="delete ccenter c7"><img src="../images/x-gray.gif" alt="删除"/><input type="hidden" name="pid" value='
                     + n.ProductID + '><input type="hidden" name="pname" value="' 
                     + n.ProductName + '"><input type="hidden" name="limitprice" value="' 
                     + n.LimitPrice + '"><input type="hidden" name="endtime" value="' 
                     + n.EndTime + '"></td><td class="c8" style="overflow-x:hidden;">'
                     + n.ProductName + '</td><td class="c9" style="overflow-x:hidden;text-align:right;padding-right:10px;">'
                     + n.MemberPrice + '</td><td class="c10 ccenter"><input id="price_' 
                     + n.ProductID + '" type="text" size="9" maxlength="20" style="text-align:center;" value="' 
                     + n.LimitPrice + '" /></td><td class="c11 ccenter"><input id="starttime_' 
                     + n.ProductID + '" type="text" size="15" maxlength="20" readonly="readonly" style="text-align:center;" value="' 
                     + GetStandardDate(n.StartTime) + '" /></td><td class="c12 ccenter"><input id="endtime_' 
                     + n.ProductID + '" type="text" size="15" maxlength="20" readonly="readonly" style="text-align:center;" value="' 
                     + GetStandardDate(n.EndTime) + '" /></td><td class="c13 ccenter"><input id="quantity_'
					 + n.ProductID + '" type="text" size="5" style="text-align:center;" value="' + n.Quantity + '"/></td><td class="c14"></td></tr>');
                     
                row.find('.delete').css('cursor','pointer').click(function(){
                    $(this).parent().remove();
                    $('#count').text(Number($('#count').text())-1);
                    //--商品ID
                    var ids = '';
                    $('input[@name=pid]').each(function(){
                        ids += $(this).val() + ',';
                    });
                    if(ids != ''){ids = ids.substring(0,ids.length-1);};
                    $('#PIDs').val(ids);
                    //--商品名称
                    var pnames = '';
                    $('input[@name=pname]').each(function(){
                        pnames += $(this).val() + ',';
                    });
                    if(pnames != ''){pnames = pnames.substring(0,pnames.length-1);};
                    $('#pnames').val(pnames);
                    
                }).find('img').mouseover(function(){
                    $(this).attr('src','../images/x-red.gif');
                }).mouseout(function(){
                    $(this).attr('src','../images/x-gray.gif');
                });
                
                $('#listresult').append(row);
                //$('#starttime_'+n.ProductID).click(function(){
//					 setDayHM(this);									  
//				});
//                $('#endtime_'+n.ProductID).click(function(){
//					 setDayHM(this);									  
//				});
                
                $('#PIDs').val($('#PIDs').val() + ($('#PIDs').val() != '' ? ',' : '') + n.ProductID);
                $('#pnames').val($('#pnames').val() + ($('#pnames').val() != '' ? ',' : '') + n.ProductName);
            });
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert('error')};}
    });
	
	$('#count').text($('#PIDs').val() != '' ? $('#PIDs').val().split(',').length : 0);
}