function setMessage(text) {
	document.getElementById("message").innerHTML = text;
	document.getElementById("message").className = "smallrounded";
}

function toggleDisplay(id) {
	var e = document.getElementById(id);
	if (!e) return true;
	if(e.style.display=='none'){
		e.style.display='block';
	}
	else{
		e.style.display='none';
	}
	return true;				
}
function insertAfter(newEl, targetEl){
	var parentEl = targetEl.parentNode;
	if(parentEl.lastChild == targetEl){
		parentEl.appendChild(newEl);
	}else{
		parentEl.insertBefore(newEl,targetEl.nextSibling);
	}            
}

function replyComment(commentId, block_Id) {
	var blockobj = document.getElementById(block_Id);
	cancel = document.getElementById("commentCancel");
	formobj = document.getElementById("comment_form");
	parentobj = document.getElementById("commentParentId");
	if (!blockobj || !cancel || !formobj || !parentobj) {
		return false;
	}
	if (!document.getElementById("tempCommentForm")) {
		div = document.createElement('div');
		div.id = "tempCommentForm";
		div.style.display = 'none';
		formobj.parentNode.insertBefore(div, formobj);
	}
	var firstdiv=blockobj.getElementsByTagName("div")[0];
	insertAfter(formobj,firstdiv); 
	parentobj.value = commentId;
	cancel.style.display = '';
	document.getElementById("commentCancel").onclick = function() {
		var temp = document.getElementById("tempCommentForm"),
		formobj = document.getElementById("comment_form"),
		parentobj = document.getElementById("commentParentId");
		if (!temp || !formobj || !parentobj)
			return;
		parentobj.value = 0;
		temp.parentNode.insertBefore(formobj, temp);
		temp.parentNode.removeChild(temp);
		this.style.display = 'none';
		this.onclick = null;
		return false;
	}
	try { document.getElementById("commentContent").focus(); }
	catch (e) {}
	return false;
}

function deleteComment(commentId)
{
	var confirmMessage = 'Are you sure to delete this comment?';
	x = window.confirm(confirmMessage);
	if (x) {
		var delurl = "/comment/delete/id/" + commentId;
		$.get(delurl, {},
		  function(data){
			var houseid="comment-"+commentId
			var house=document.getElementById(houseid);
			var jqid="#"+houseid;
			$(jqid).slideUp("slow",function(){
			   	house.parentNode.removeChild(house);
			 });
		  });
	}
}

function spamComment(commentId, confirmMessage)
{
	confirmMessage = 'Are you sure to report spam?';
	x = window.confirm(confirmMessage);
	if (x) {
		window.location.href = "/comment/spam/id/" + commentId;
	}
}

function backinput(obj){
	obj.parentNode.nextSibling.style.display='block';
	obj.parentNode.parentNode.removeChild(obj.parentNode);
}

function addcomment(){
	if(document.getElementById("commentAuthorEmail")){
		var obj=document.getElementById("commentAuthorEmail");
		var result=/(\S)+[@]{1}(\S)+[.]{1}(\w)+/.test(obj.value);
		if(!result){
			if(!document.getElementById('error_msg')){
				var newNode = document.createElement('span');
				newNode.id='error_msg';
				newNode.innerHTML = '<input onfocus=\'backinput(this)\' type=\'text\' value=\' 请输入正确的email地址\' style=\'background:red;border:1px solid #ccc;width:250px;\' />';
				obj.parentNode.insertBefore(newNode,obj); 
				obj.style.display='none';
			} 
			return false;
		}
	}
	document.getElementById("commentSubmit").onclick=function(){return false;}
	var commentAuthorIp=$("#commentAuthorIp").val();
	var commentContent=$("#commentContent").val();
	var commentPageId=$("#commentPageId").val();
	var commentParentId=$("#commentParentId").val();
	var commentAuthorName=$("#commentAuthorName").val();
	var commentAuthorEmail=$("#commentAuthorEmail").val();
	var posturl="/comment/submit";
	$.post(posturl, { commentAuthorName: commentAuthorName,commentAuthorEmail: commentAuthorEmail,commentAuthorIp: commentAuthorIp, commentContent: commentContent , commentPageId: commentPageId, commentParentId: commentParentId},
	  function(data){
		if(commentParentId == "0"){
			$("#comment_new_list").prepend(data);
		}else{
			var brother="#comment-"+commentParentId;
			if($(brother).next().attr('class') == 'comments_re'){
				$(brother).next().prepend(data);
			}else{
				var new_data= '<div class="comments_re">'+data+'</div>';
				$(brother).after(new_data);
			}
			var obj_kick = document.getElementById('commentCancel');
			if(document.all && typeof(document.all) == "object"){
				obj_kick.fireEvent("onclick");
			}else{
				var e = document.createEvent('MouseEvent');
				e.initEvent('click',false,false);
				obj_kick.dispatchEvent(e);
			}
		}
		$("#commentContent").val("");
		content_comment_length=500;
		document.getElementById(number_id).innerHTML="你还可以输入 <span style='font-family:Constantia,Georgia;font-size:20px;color:#c02020;'>"+content_comment_length+"</span> 字";
		document.getElementById("commentSubmit").onclick=function(){addcomment();}
	  });
}

var content_comment_length=-1;
function checklength() {
	input_length=document.getElementById(input_id).value.length;
	if(input_length>max_length){
		content_comment_length='0';
		document.getElementById(input_id).value=document.getElementById(input_id).value.substr(0,max_length);
	}else{
		content_comment_length=max_length- input_length;
	}
    document.getElementById(number_id).innerHTML="你还可以输入 <span style='font-family:Constantia,Georgia;font-size:20px;color:#c02020;'>"+content_comment_length+"</span> 字";
}

function readytoread(tlength,tinput,tnumber){
	max_length=tlength;
	input_id=tinput;
	number_id=tnumber;
	if(content_comment_length==-1){
		content_comment_length=tlength
	}
	document.getElementById(number_id).innerHTML="你还可以输入 <span style='font-family:Constantia,Georgia;font-size:20px;color:#c02020;'>"+content_comment_length+"</span> 字";
	if (/msie/i.test(navigator.userAgent)) {
		document.getElementById(input_id).onpropertychange=checklength;
	}else{
		document.getElementById(input_id).addEventListener('input',checklength, false);
	}
}
