﻿$(document).ready(function() {
    commentLoad();
});
function commentLoad(){
  setTimeout("getCommentCount()",80);
  setTimeout("userIsLogin()",80);
}

function iniAnonymity() {
	var userId =GetCookie("user_id");
	var box = document.getElementById('isAnonymity');
	if(userId) {
		box.checked == false;
	}else {
		box.setAttribute('checked','checked');
	}
}
function hasInputID(ele) {
	var box = document.getElementById('isAnonymity');
	if((ele.value != '') && (ele.value != ele.defaultValue)) {
		box.checked = false;
		box.removeAttribute('checked');
	}else {
		box.checked = true;
		box.setAttribute('checked','checked');
	}
}

function userIsLogin(){
    var userId =GetCookie("user_id");
    if (userId!=null && userId!="")
    {
        document.getElementById("dvUserLogin").style.display="none";
        document.getElementById("dvUserName").style.display="";
        document.getElementById("dvUserName").innerHTML= GetCookie("user_nickname") + " <span>欢迎您发表评论！</span> ";

    }
    else
    {
        document.getElementById("dvUserLogin").style.display="";
        document.getElementById("dvUserName").style.display="none";
    }
}

function replaycomment(parentid)
{
    $("#hidParentId").val(parentid);
    $("#txtContent").val("回复：");
    $("#txtContent").focus();
}
function getComment(pageIndex)
{
   var reply="";
   var userId=$("#hidUserId").val();
   if(userId>0)
        reply="&nbsp;<a href='javascript:void(0)' onclick='replaycomment({5})' title='回复'>回复</a>";
   var houseId= document.getElementById('hidHouseId').value;
   // pageIndex
   var html="";
   html+="<div class='ha_reback1 {0}'>{1} [{2}]"+reply+"</div>";
   html+="<div class='ha_reback2'><span>{3}</span>#</div>";
   html+="<div class='ha_reback3'><a href='#' title='返回顶部'></a></div>";
   html+="<div class='ha_retxt'>";
   html+="{4}";
   html+="</div>";
   html+="";
   
   var html1="";
   html1+="<div class='rha_reback1 {0}'>{1} [{2}]</div>";
   html1+="<div class='rha_retxt'>";
   html1+="{3}";
   html1+="</div>";
   
  
   housecomment.GetComment(html,html1,houseId,pageIndex,getComment_CallBack);
}
function getComment_CallBack(response)
{
    if(response.error!=null)
    {
      alert(response.error);
    }
    else
    {
       var txt=response.value;
       if (txt!="")
       {
         var txtArr=txt.split("<@@>");
         if(txtArr.length>1)
	     {
	        document.getElementById("dvCommentShow").innerHTML=txtArr[0];
	        document.getElementById("dvPager").innerHTML="<div style='padding:0 0 20px 20px;'>" + txtArr[1] + "</div>";
	        //alert(txtArr[1]);
	     }
	     else
	     {
		    document.getElementById("dvCommentShow").innerHTML=txt;
	     }
	   }
	   else
	   {
	        document.getElementById("dvCommentShow").style.display="none";
		    //document.getElementById("dvCommentLink").style.display="none";
	    }
     }
}
var code="";
function addComment(){
    var userName= document.getElementById('iptUserName').value;
    var houseUserId=document.getElementById("hidUserId").value;
    var pwd= document.getElementById('iptUserPass').value;
    //var iptCode= document.getElementById('iptCode').value;
    var houseId= document.getElementById('hidHouseId').value;
    var txt = document.getElementById('txtContent').value;
    //var cb=document.getElementById('isAnonymity');
    //var isChecked=false;
    var parentid=$("#hidParentId").val();
    var userId =GetCookie("user_id");
    //if (!cb.checked){
       if (userId==null || userId=="")
       {
            if (userName=="")
            {
               alert("请输入您的用户名");
			   document.getElementById('iptUserName').focus();
               return false;
            }
            if (pwd.length < 6 || pwd.length > 16)
            {
                alert("请输入6-16位密码");
			    document.getElementById('iptUserPass').focus();
                return false;
            }
            userId=0;
        }
    if (txt==""){
        alert("请输入评论内容");
	    document.getElementById('txtContent').focus();
        return false;
    }
    housecomment.AddComment(houseId,userId,txt,houseUserId,userName,pwd,parentid,addComment_CallBack);
}
function addComment_CallBack(response)
{
    if(response.error!=null)
    {
        alert('提交失败！请稍后再试');
    }
    else
    {
       if (response.value==-1)
       {
         alert('登录失败，请检查您的用户名和密码')
       }
       else
       {
         userIsLogin();
         getCommentCount();
         getComment(1);
         document.getElementById('txtContent').value="";
	     document.getElementById('dvCommentShow').style.display = 'block';
         alert('发表评论成功！');
       }

    }

}

function getCode()
{
    housecomment.GetCode(getCode_CallBack);
}

function getCode_CallBack(response)
{
   if(response.error!=null)
   {
     alert(response.error);
   }
   else
   {
      code=response.value;

   }
}
function goComment()
{
  document.getElementById('txtContent').focus();
}
function selAnonymity()
{
   var cb=document.getElementById('isAnonymity')
   if (cb.checked)
   {
      document.getElementById('iptUserName').disabled=true;
      document.getElementById('iptUserPass').disabled=true;
   }
   else
   {
      document.getElementById('iptUserName').disabled=false;
      document.getElementById('iptUserPass').disabled=false;
   }
}
function getCommentCount()
{
    var houseId= document.getElementById('hidHouseId').value;
    housecomment.GetCommentCount(houseId,getCommentCount_CallBack);
}

function getCommentCount_CallBack(response)
{
   if(response.error!=null)
   {
     alert(response.error);
   }
   else
   {
      $("#spCommentCount").text(response.value);
      $("#spCommentCount1").html("<a href='#comment'>" + response.value + "</a>");
   }
}
