// JavaScript Document
function RevertComment(aid){
	if(isNaN(aid)){
		alert('获取数据出错');
		return false;
	}else{
		//document.getElementById("aid").value = aid;
		document.getElementById("content").focus();
	}
}


// 使js实现replaceAll
String.prototype.replaceAll  = function(s1,s2){    
	return this.replace(new RegExp(s1,"gm"),s2);    
}   

function quote(id){
	//var q_t;
	//var q_c;
	//var q;
	if(isNaN(id)){
		alert('获取数据出错');
		return false;
	}else{
		//document.getElementById("content").value = document.getElementById("cmt_ul_"+id).firstChild.innerHTML ;
		//q_t = document.getElementById("cmt_ul_"+id).getElementsByTagName("li")[0].innerHTML ;
		//q_c = document.getElementById("cmt_ul_"+id).getElementsByTagName("li")[1].innerHTML ;
		//q_c = q_c.replace(/<[\/]*BLOCKQUOTE[^>]*>/img, "");//
		//q_c = q_c.replace(/<[\/]*DIV[^>]*>/img, "");//
		//q_c = q_c.replaceAll("(<.+>)+.*[\n]*(</.+>)*", "");
		//q_t = '引用 '+q_t.replace('发表','')+'发表的评论';
		//q = '<blockquote>\n<div class="quote">\n<div class="quote-title">'+q_t+'</div>\n'+q_c+'\n</div>\n</blockquote>';
		//document.getElementById("quote").value = q;
		document.getElementById("quote").value = id;
		document.getElementById("author").focus();
		//document.getElementById("content").value = document.getElementById("cmt_ul_"+id).firstChild.outerHTML ;
	}
}

function check_comment_submit(){
	
	var author = $.trim($('#author').val());
	var email = $.trim($('#email').val());
	var website = $.trim($('#website').val());
	var content = $.trim($('#content').val());


	if (author == '') {
			$('#tip_author').css('color','red'); $('#tip_author').html('请输入你的昵称');
			return false;
	} else {
			$('#tip_author').css('color','green'); $('#tip_author').html('正确');
	}
	
	if (email == '') {
			$('#tip_email').css('color','red'); $('#tip_email').html('请输入你的EMAIL地址');
			return false;
	}else {
			var emailReg=/^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			if (!emailReg.test(email)) {
				$('#tip_email').css('color','red'); $('#tip_email').html('你输入的邮箱地址格式有误');
				return false;
			} else {
				$('#tip_email').css('color','green'); $('#tip_email').html('正确');
			}
	}
	
	if (website != '' && website.indexOf('http://') == -1) {
			$('#tip_website').css('color','red'); $('#tip_website').html('别忘了加上 http:// 哦');
			return false;
	} else {
			$('#tip_website').css('color','green'); $('#tip_website').html('正确');
	}
	
	if (content == '') {
		$('#tip_content').css('color','red'); $('#tip_content').html('别忘了输入内容哦');
		return false;
	} else {
		$('#tip_content').html('');
	}
	
	var btn_str = $('#submit').val();
	$('#submit').attr('disabled','disabled');
	$('#submit').val('正在提交...');	
	
	$.ajax({
		    type : 'POST',
			url : blog_url+'/index.php?ajax=comment',
			data : 'author='+encodeURIComponent(author)+'&email='+encodeURIComponent(email)+'&website='+encodeURIComponent(website)+'&content='+encodeURIComponent(content)+'&quote='+$('#quote').val()+'&aid='+$('#aid').val(),
			cache : false,
			success: function(msg){
				eval("var message = " + msg);
				if (message.type == 'success') {
					$('#commentlist').append(decodeURIComponent(message.msg)).find('#cmt_ul_'+message.ulid).hide().animate({opacity:"show"}, 1200);
					$('#content').val('');
					$('#quote').val('');
					$('#submit').removeAttr('disabled');
					$('#submit').val(btn_str);
				} else {
					alert("出错了：\n\n"+message.msg);
					$('#submit').removeAttr('disabled');
					$('#submit').val(btn_str);
				}
			}
	});
	return false;
}


function getintro(aid){
	var pwd =  $("input[@name='viewpwd"+aid+"']").val();
	if(pwd==''){
		$('#post-intro_'+aid+' em').css('color','#ff0000').html('请输入密码');
	}else{
		$('#post-intro_'+aid+' em').html('<img src="'+blog_url + '/image/ajax-loader.gif" />');
		var data = 'aid='+aid+'&password='+encodeURIComponent(pwd);
		$.post(blog_url + '/index.php?ajax=get_article_intro', data, function (msg) {
			eval("var message = "+msg);
			//$(":submit[@name='submit"+aid+"']").removeAttr('disabled');
			if (message.type == 'error') {
				$('#post-intro_'+aid+' em').css('color','#ff0000').html(message.data);
			} else if (message.type == 'success') {
				$("#post-intro_"+aid).html(message.data);
			}
			/*alert(msg);*/
		});
	}
	
}

function getcontent(aid){
	var pwd =  $("input[@name='viewpwd"+aid+"']").val();
	if(pwd==''){
		$('#post-intro_'+aid+' em').css('color','#ff0000').html('请输入密码');
	}else{
		$('#post-intro_'+aid+' em').html('<img src="'+blog_url + '/image/ajax-loader.gif" />');
		var data = 'aid='+aid+'&password='+encodeURIComponent(pwd);
		$.post(blog_url + '/index.php?ajax=get_article_content', data, function (msg) {
			eval("var message = "+msg);
			//$(":submit[@name='submit"+aid+"']").removeAttr('disabled');
			if (message.type == 'error') {
				$('#post-intro_'+aid+' em').css('color','#ff0000').html(message.data);
			} else if (message.type == 'success') {
				$("#post-intro_"+aid).html(message.data);
			}
		});
	}
	
}


function isKeyTrigger(e,keyCode){       
    var argv = isKeyTrigger.arguments;       
    var argc = isKeyTrigger.arguments.length;       
    var bCtrl = false;       
    if(argc > 2){       
        bCtrl = argv[2];       
    }       
    var bAlt = false;       
    if(argc > 3){       
        bAlt = argv[3];       
    }       
    var nav4 = window.Event ? true : false;       
    if(typeof e == 'undefined') {       
        e = event;       
    }       
    if(bCtrl &&       
        !((typeof e.ctrlKey != 'undefined') ?        
        e.ctrlKey :       
        e.modifiers & Event.CONTROL_MASK > 0)) {       
        return false;       
    }       
    if( bAlt &&       
        !((typeof e.altKey != 'undefined') ?        
            e.altKey : e.modifiers & Event.ALT_MASK > 0)){       
        return false;       
    }       
    var whichCode = 0;       
    if (nav4) whichCode = e.which;       
    else if (e.type == "keypress" || e.type == "keydown") whichCode = e.keyCode;       
    else whichCode = e.button;       
           
    return (whichCode == keyCode);       
}       
      
function ctrlEnter(e){       
    var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;   
    if(ie){       
        if(event.ctrlKey && event.keyCode == 13) { 
			//if(check_comment_submit())
            //submit();  
			check_comment_submit();
        }
	}else {       
            if(isKeyTrigger(e,13,true)){  
				//if(check_comment_submit())
               //submit();   
			   check_comment_submit();
             }       
        }       
           
}    


/*$(document).ready(function() {
	//$('#post-comment ul').hide();					   
	$('#CFace').click(function() {
		if(this.checked){
			var _li='';
			for(i=1;i<37;i++){
				_li += "<li><img src='./image/face/"+i+".gif' title='"+i+"' /></li>";
			}
			//$('<ul>'+_li+'</ul>').insertAfter('#post-comment div');
			$('#post-comment div').html('<ul>'+_li+'</ul>');

			$('#post-comment ul').show(800);
			
			
			$('#post-comment ul img').click(function(){
				$('#content').val($('#content').val()+'{:'+this.title+':}');
			});
		
			
			$('#post-comment ul li img').hover(
				function(){$(this).parent().css({border:'1px solid #72BAEF',background:'#E1EFFB'})},
				function(){$(this).parent().css({border:'1px solid #FFFFFF',background:'none'})}
			);
		}else{
			$('#post-comment ul').hide(800);
			setTimeout("$('#post-comment ul').remove()", 801);
			
		}
	});	
});*/

function showface(e){
	if(e.checked){
			var _li='';
			for(i=1;i<37;i++){
				_li += "<li><img src='./image/face/"+i+".gif' title='"+i+"' /></li>";
			}
			//$('<ul>'+_li+'</ul>').insertAfter('#post-comment div');
			$('#post-comment div').html('<ul>'+_li+'</ul>');

			$('#post-comment ul').show(800);
			
			
			$('#post-comment ul img').click(function(){
				$('#content').val($('#content').val()+'{:'+this.title+':}');
			});
		
			
			$('#post-comment ul li img').hover(
				function(){$(this).parent().css({border:'1px solid #72BAEF',background:'#E1EFFB'})},
				function(){$(this).parent().css({border:'1px solid #FFFFFF',background:'none'})}
			);
		}else{
			$('#post-comment ul').hide(800);
			setTimeout("$('#post-comment ul').remove()", 801);
			
		}

}