$(document).ready(function() {
	BindList('Page=1');	
	$('#ask_submit').click(function(){
	    if($('#questionMessage').val().length>60){
	    return false;
	    }
	    if($('#questionMessage').val().length==0){
	    alert('问题内容不能为空');
	    return false;
	    }
	    if( $('#questionMessage').attr('disabled')){
            alert('问题已经提交')
            return false;
	    }
        AddProductAsk();
        BindList('page=1');
    });
    
    $('.textarea').keyup(function(){
        var len=$('#questionMessage').val().length;
        if(len>60){
        $('.wrong').css("display","inline-block");
        var text="已超出"+(len-60)+"个字符";
        $('.wrong').html(text);
        }
        else{
        $('.wrong').css("display","none");
        $('.wrong').empty();
        }
        
    });
});

//绑定数据
function BindList(urldata){
    $('#template').hide();//将第一行模板隐藏
    $.ajax({
        type:"get",
        dataType:"json",
        url:baseUrl + 'ProductAsk.aspx?NoCopyRight=1&type=GetList&PID='+productID + '&PageSize=' + PageSize,
        data:urldata,
        cache:false,
        success:function(msg){
            $('div.xp-store-ask-pdtask:gt(0)','#AskContent').remove();
            var data = msg.ProductAsk;
            $.each(data,function(i,n){
               var row = $('#template').clone().show();
//               row.find('.icon').find('img').attr('src',baseUrl +'upload/MemberGroupLogo/' + (n.Logo != '' ? n.Logo : 'nopic.gif'));
               row.find('.icon').html('<img alt="" src="'+ baseUrl +'upload/MemberGroupLogo/' + (n.Logo != '' ? n.Logo : 'nopic.gif') +'"/>');
               row.find('.name').text(n.MemberName);
               row.find('.group').text(n.GroupName);
               row.find('.askcontent').html('<span>咨询内容：</span>'+n.Body);
               row.find('.asktime').text(n.AddTime);
                   
               var objyesNum = row.find('.yesnum').text(n.YesNum);
               var objnotNum = row.find('.notnum').text(n.NotNum);
               row.find('.vote_y').click(function(){
                    var result = VoteAsk(n.ProductAskID,1);
                    if(result == '1'){
                        objyesNum.text(Number(objyesNum.text())+1);
                    }else if(result == '2'){
                        alert('不能重复投票!');
                    };
               });
               row.find('.vote_n').click(function(){
                    var result = VoteAsk(n.ProductAskID,0);
                    if(result == '1'){
                        objnotNum.text(Number(objnotNum.text())+1);
                    }else if(result == '2'){
                        alert('不能重复投票!');
                    };
               });
               
               if(n.Status!=1)//还没有回复
               {
                   row.find('.replycontent').parent().hide()
               }
               else
               {  
                   row.find('.replycontent').html('<span>回复内容：</span>'+n.Reply);
                   row.find('.replytime').text(n.ReplyTime);
               }
               row.attr('id','item_'+i).appendTo($('#AskContent'));
            });
           
           if(true){
                //分页
                $('.sub_container_page').show();
                var pagerStyle = "flickr";//jPagerBar样式
                ShowPageBar("pageDataList1_Pager",//[containerId]
                   "ProductAsk.aspx",//[url]
                   {style:pagerStyle,mark:"pageDataList1Mark",
                   totalCount:msg.totalCount,showPageNumber:3,pageCount:PageSize,currentPageIndex:CurrentPage,noRecordTip:" 暂无咨询",preWord:"上一页",nextWord:"下一页",
                   onclick:"CurrentPage={pageindex};BindList('Page={pageindex}');return false;"}//[attr]
                );
            };
        },//end success
        error:function(XMLHttpRequest,textStatus){alert(textStatus);}
    });//end ajax
};//end funciton

function VoteAsk(paid,val){
    var result = '0';
    $.ajax({
        type:"get",
        url:shopFolder + 'ProductAsk.aspx?NoCopyRight=1&type=Vote&PAID='+paid+'&val='+val + '&r='+Math.random(),
        async:false,
        success:function(data){
            if(data == 'NOLOGIN'){
                if(confirm('登录后才可以投票,是否现在登录')){
                    window.location.href= shopFolder + 'login.aspx?ReturnUrl='+window.location.href;
                }else{
                    return false;
                }
            };
            result = data;
        },error:function(){alert('error');}
    });
    return result;
};

function AddProductAsk(){
     var qust=$('#questionMessage').val()
     var pid=$('#pid').val();
     var cuid=$('#cuid').val();
     var cuname=$('#cuname').val();
     var sid=$('#sid').val();
    var pass = false;
    $.ajax({
        type:"get",
        url:baseUrl + 'ProductAsk.aspx?NoCopyRight=1&type=Add&pid='+pid+'&cuid='+cuid+'&cuname='+cuname+'&qust='+escape(qust)+"&sid="+sid,
        cache:false,
        async:false,
        dataType:"text",  
        contentType:"application/x-www-form-urlencoded; charset=utf-8", 
        success:function(data){
            switch(data){
                case 'succ':
                    alert('提问成功，请耐心等待回答。');
                    $('#questionMessage').val('你已经提问');
                    $('#questionMessage').attr('disabled','disabled');
//                    $(this).attr("disabled","true");
                    pass = true;
                    break;
                case 'fail':
                    alert('提问失败，请稍后重试。');  
                    pass = false;
                    break;
            };
        },
        error:function(){alert('error');}
    });
    return pass;
};



//获取字符串字节长度方法 
function GetLength(s){  
    var i,sum,len
    len=s.length;
    sum=0;  
    for(i=0;i<len;i++) {  
    if ((s.charCodeAt(i)>=0) && (s.charCodeAt(i)<=255))  
    sum=sum+1;  
    else  
    sum=sum+2;  
    }  
  return sum;  
}  

