﻿
//设置按钮等待时间
function StopButton()
{
	document.getElementById(arguments[0]).disabled=true;
	document.getElementById(arguments[0]).value="发表成功("+arguments[1]+")";
	if(--arguments[1]>0){
		window.setTimeout("StopButton('"+arguments[0]+"',"+arguments[1]+")",1000);
	}
	if(arguments[1]<=0){
		document.getElementById(arguments[0]).value='发表留言';
		document.getElementById(arguments[0]).disabled=false;
	}
}


//根据日记编号获取留言
function GetMessage($diaryId){
	$.ajax({
		url:"/riji/AjaxShowMsg.aspx",
		data:"id="+$diaryId,
		type:'post',
		cache: false,
		complete:function(xhr){
			$('#comment').html(xhr.responseText);
		}
	});
}


//根据日记编号获取留言计数
function GetComments($diaryId){
	$.ajax({
		url:"/riji/AjaxComments.aspx",
		data:"id="+$diaryId,
		type:'post',
		complete:function(xhr){
			$('#msgComment').html(xhr.responseText);
		}
	});
}

function EnterSubMit()
{
    if(event.keyCode==13)
    {
        //留言内容
	    var $content = $('#txtMsg').val();

	    //验证码
	    var $code = $('#txtCode').val();
        if ($.trim($content)=='')
	    {
		    alert(" - 请输入留言内容","提示信息",0,"确定",function(){$("#txtMsg")[0].focus();});
            return false;
	    }
    	
	    if ($content.length<1||$content.length>300)
	    {
		    alert(" - 留言内容必须少于300字，请重新编辑！","提示信息",0,"确定",function(){
                           $("#txtMsg")[0].focus(); 
                        });
            return false;
	    }
        if($.trim($('#txtCode').val()) == "")
        {
            alert(" - 请输入验证码！","提示信息",0,"确定",function(){
               $("#txtCode")[0].focus(); 
            });
            return false;
        }
        $('#CommentSubmit').click();
    }
}


//添加新留言
function SendComment(){
    //日记编号
    var $diaryId = arguments[0];
    //日记标题
    var $title = $('#hdTitle').val();
    //日记作者编号
    var $diaryMemId = $('#hdDiaryMemId').val()
    //留言内容
	var $content = $('#txtMsg').val();

	//验证码
	var $code = $('#txtCode').val();
	
	if ($.trim($content)=='')
	{
		alert(" - 请输入留言内容","提示信息",0,"确定",function(){
                       $("#txtMsg")[0].focus(); 
                    });
        return false;
	}
	
	if ($content.length<1||$content.length>300)
	{
		alert(" - 留言内容必须少于300字，请重新编辑！","提示信息",0,"确定",function(){
                       $("#txtMsg")[0].focus(); 
                    });
        return false;
	}
	
	if ($.trim($code)=='')
	{
		alert(" - 请输入验证码！","提示信息",0,"确定",function(){
                       $("#txtCode")[0].focus(); 
                    });
        return false;
	}
	//alert("/riji/AjaxMessage.aspx?diaryId="+$diaryId+"&title="+$title+"&diaryMemId="+$diaryMemId+"&content="+escape($content)+"&code="+$code+"&random="+ Math.random());
	$.ajax({
		url:"/riji/AjaxMessage.aspx?diaryId="+$diaryId+"&title="+encodeURIComponent($title)+"&diaryMemId="+$diaryMemId+"&content="+encodeURIComponent($content)+"&code="+$code+"&random="+ Math.random(),
		type:'GET',
		success:function(){
			if (arguments[0] == 'ERROR')
			{
				alert(" - 请输入验证码！","提示信息",0,"确定",function(){
                       $("#txtCode")[0].focus(); 
                    });
				return false;
			}
			else if(arguments[0]== 'ERROR1')
			{
				alert(" - 验证码已经过期，请重新输入！","提示信息",0,"确定",function(){
                       $("#txtCode")[0].focus(); 
                    });
			}
			else if(arguments[0]== 'ERROR2')
			{
				alert(" - 验证码输入出错，请重新输入！","提示信息",0,"确定",function(){
                       $("#txtCode")[0].focus(); 
                    });
			}
			else if(arguments[0]== 'ERROR3')
			{
				alert('非常抱歉，留言失败了，日记可能已经不存在！');
				return false;
			}
			else if(arguments[0]== 'SUCCESS')
			{
			    $('#txtMsg').val("");
				GetMessage($diaryId);
				GetComments($diaryId);
			    //验证成功时，刷新验证码图片
				StopButton('CommentSubmit',10);
			}
			else
			{
			    alert('很抱歉，出错了，请刷新后重试!');
			}
			$("#imgCode").attr("src", "/Controls/ValidImg.aspx?random=" + Math.random())
			//清空验证码
			$("#txtCode").val("");
		}
	});
}


//删除留言
function DelMessage($msgId,$diaryId){
    if(confirm("你确认要删除这条留言吗？"))
    { 	
        $.ajax({
		    url:"/riji/AjaxDelMessage.aspx?msgId="+$msgId+"&diaryId="+$diaryId,
		    type:'post',
		    cache: false,
		    complete:function(xhr){
			    GetMessage($diaryId);
			    GetComments($diaryId);
		    }
	    });
	}
}


var facetitle = ["大笑","微笑","亲亲","抱抱","色色","失望","好困哦","害羞","酷呆了","晕倒","眨眼","鬼脸","小声点",
"吃惊","翻白眼","干杯","困惑","啥？","睡觉","再见","哭","讨厌","我吐","怒骂","闭嘴","打你",
"生气","超级棒","不咋地","魅力四射","心碎","爱","吻","玫瑰凋谢","玫瑰盛开","生日蛋糕","礼物","苹果","西瓜",
"咖啡","足球","握手","星星","精灵","小丑","大怒","生病了","小可爱","小心非典","嘴馋了","警察","抓狂了",
"不爽","汗","思考中","见钱眼开","呲牙咧嘴","晕头转向","好爱你哦","猪头","便便","月亮","音乐","饭","真衰",
"偷笑","下雨","猫猫","狗狗","骷髅","书呆子","太阳","邮件","帅帅男孩","妩媚女孩","药丸","鄙视","烧香"]
function GetSmilies()
{
	    var str = '';
		var pContent = '';
		for(i=0;i<5;i++)
		{
			for(j=1;j<=12;j++)
			{
				var tt = (i*13+j).toString();
				var t='images/diary/'+tt+'.gif';
				pContent += '<img onclick="checkimg('+i+', '+j+')" src="'+t+'" title="'+facetitle[i*13+j-1]+'" width="19" height="19" />';
			}
		}
		str += pContent;
		document.write(str);
}

//点击表情在文本框中显示
function checkimg(i, j)
{
    var textEl = document.getElementById('txtMsg');
    
    if(document.selection)
	{ 
	    textEl.focus();
	    with(document.selection.createRange())
	    {
		     text = "[#" + facetitle[i*13+j-1] + "]";
	    }
	}
	else
	{
	    textEl.focus();
		var start = textEl.selectionStart;  
		var end = textEl.selectionEnd;  
		var pre = textEl.value.substr(0, start);  
		var post = textEl.value.substr(end);  
		textEl.value += "[#" + facetitle[i*13+j-1] + "]";
	}
} 


function SetMsgFocus()
{
    var textEl = document.getElementById('txtMsg');
    if(textEl != null)
    {
        textEl.focus();
    }
}
