function addfavourite(){
	var opt = {
		parameters: {value:useridtmp},
		onSuccess: function(t) {
			$("infotext").innerHTML = _("This blog has been added as your favorite");
			showElement("infobox");
			$("infobox").focus();
		},
		onFailure: function(t) {
			$("infotext").innerHTML = _("The favourite blog cannot be added");
			showElement("infobox");
			$("infobox").focus();
		}
	}
	new Ajax.Request(addFavouritetmp, opt);
}

var commentposting = false;
function commentsubmit(){
	if(commentposting){
		$('infotext').innerHTML = _("Posting comment, please wait");
		showElement('infobox');
		$('infobox').focus();
		return;
	}
	if(Trim($('txtcomment').value)=='')return;

	commentposting = true;
	showElement('commentloading');
	var opt = {
		postBody: 'value=' + encodeURIComponent($('txtcomment').value),
		onSuccess: function(t) {
			hideElement('commentloading');

			$('txtcomment').value="";
			$('commentlist').innerHTML = t.responseText;
			updatecommanttotal();
			commentposting = false;
		},
		onFailure: function(t) {
			hideElement('commentloading');

			$('infotext').innerHTML = _("Post comment fail");
			showElement('infobox');
			$('infobox').focus();
			commentposting = false;
		}
	}
	new Ajax.Request("/blog/new/"+blogidtmp+"/comment", opt);
}

function quotesubmit(qid,types,itemid,commentid){
	if(commentposting){
		$('infotext').innerHTML = _("Posting comment, please wait");
		showElement('infobox');
		$('infobox').focus();
		return;
	}
	if(Trim($(qid).value)=='')return;

	commentposting = true;
	showElement('loading'+qid.substring(4));
	var opt = {
		postBody: '&value=' + encodeURIComponent($(qid).value) + "&commentid=" + commentid,
		onSuccess: function(t) {
			hideElement('loading'+qid.substring(4));

			$(qid).value = "";
			$('commentlist').innerHTML = t.responseText;
			updatecommanttotal();
			commentposting = false;
		},
		onFailure: function(t) {
			hideElement('loading'+qid.substring(4));

			$('infotext').innerHTML = _("Post comment fail");
			showElement('infobox');
			$('infobox').focus();
			commentposting = false;
		}
	}
	new Ajax.Request("/blog/new/"+blogidtmp+"/comment",opt);
}
function replysubmit(rid,types,itemid,commentid){
	if(commentposting){
		$('infotext').innerHTML = _("Posting comment. Please wait");
		showElement('infobox');
		$('infobox').focus();
		return;
	}
	if(Trim($(rid).value)=='')return;

	commentposting = true;
	showElement('replyloading'+rid.substring(4));
	encodeURIComponent($(rid).value);
	var opt = {
		postBody: '&value=' + encodeURIComponent($(rid).value) + "&commentid=" + commentid +"&isreply=True",
		onSuccess: function(t) {
			hideElement('replyloading'+rid.substring(4));

			$(rid).value = "";
			$('commentlist').innerHTML = t.responseText;
			updatecommanttotal();
			commentposting = false;
		},
		onFailure: function(t) {
			hideElement('replyloading'+rid.substring(4));

			$('infotext').innerHTML = _("Can't reply comment");
			showElement('infobox');
			$('infobox').focus();
			commentposting = false;
		}
	}
	new Ajax.Request("/blog/new/"+blogidtmp+"/comment", opt);
}

function showcomments(userid,commentspage){
	var opt = {
		postBody: 'userid='+userid+'&blogid=' + blogidtmp + '&commentspage='+ commentspage,
		onSuccess: function(t) {
			$('commentlist').innerHTML = t.responseText;
			updatecommanttotal();
		},
		onFailure: function(t) {
			$('infotext').innerHTML = _("The comments cannot be displayed");
			showElement('infobox');
			$('infobox').focus();
		}
	}
	new Ajax.Request('/blog/showBlogComment', opt);
}

function deletecomment_click(itemtype,itemid){
	if(itemid){
		if(confirm(_("Do you want to delete comment ?"))) {
			var opt = {
				postBody: 'id='+ itemid,
				onSuccess: function(t) {
					$('commentlist').innerHTML = t.responseText;
					updatecommanttotal();
					
				},
				onFailure: function(t) {
					$('infotext').innerHTML = _("Delete comment fail, please try again later");
					showElement('infobox');
					$('infobox').focus();
				}
			}
			new Ajax.Request('/blog/deleteBlogComment', opt);
		}
	}
}

function sendPM(id){
	var topic = $('topic'+id).value;
	var message = $('msg'+id).value;
	var targetid = $('huid'+id).value;
	var postdata = "userid=" + owneridtmp + "&targetid=" + targetid + "&topic="+ encodeURIComponent(topic) + "&message=" + encodeURIComponent(message);
	var posturl = "/sendPrivateMessage";
	hideElement('p'+id);
	var opt = {
		method: 'post',
		postBody: postdata,
		onSuccess: function(t) {
			if (t.responseText=="Success"){
				$('infotext').innerHTML = _("Send message completed");
				showElement('infobox');
				$('infobox').focus();
			}
		},
		onFailure: function(t) {
			$('infotext').innerHTML = _("Cannot send message");
			showElement('infobox');
			$('infobox').focus();
		}
	}
	new Ajax.Request(posturl, opt);
}

groupInfoList = {};
function saveGroupBlog(){
    $("groupphoto").innerHTML = "Saving...";

    var groupidlist = new Array();
    $("grouplistitem").select("input").each(function(element){
        if(element.checked){
            var key = element.value.split(":");
            groupidlist.push([key[0],key[1]]);
        }
    });
    var opt = {
        postBody: "id="+blogidtmp+"&groupidlist="+groupidlist.toJSON(),
        onSuccess: function(t) {
            if(t.responseText == "200"){
                hideElement('grouplist');
                for(key in groupInfoList){
                    groupInfoList[key].checked = false;
                }
                for(var i = 0; i<groupidlist.length; i++){
                    groupInfoList[groupidlist[i][0]+":"+groupidlist[i][1]].checked = true;
                }
                genGroupNameText();
            }else{
                cancelSaveGroupBlog();
            }
        },
        onFailure: function(t) {
            cancelSaveGroupBlog();
        }
    }
    new Ajax.Request("/blog/saveGroupBlog", opt);
}

function showSaveGroupBlog(){
    $("grouplistitem").select("input").each(function(element){
        element.checked = groupInfoList[element.value].checked;
    })
    showElement('grouplist');
}

function cancelSaveGroupBlog(){
    hideElement('grouplist');
}

function genGroupNameText(){
    var tmpGroupName = "";
    var keyCount = 0;
    for(key in groupInfoList){
        if(groupInfoList[key].checked){tmpGroupName += groupInfoList[key].groupName+ ", ";}
        keyCount++;
    }
    if(tmpGroupName.length == 0){
        if(keyCount > 0){
            tmpGroupName = _("Click here to add blog to your groups");
        }else{
            tmpGroupName = "";
        }
    }else{
        tmpGroupName = tmpGroupName.substring(0,tmpGroupName.length-2);
    }
    $("groupphoto").innerHTML = tmpGroupName;
}

function updatecommanttotal()
{
	if(document.getElementsByClassName('totalitem').length > 0)
	{
		var totalcomment = document.getElementsByClassName('totalitem')[0].firstChild.innerHTML.toLowerCase().replace(/<\/strong>/,"").replace(/<strong>/,"");
		$('totalcomment_top').innerHTML = totalcomment;
		ReplacePgLinkComment();
	}
	else
		$('totalcomment_top').innerHTML = _("No comment");
}

function shareThis(){
    var url = "http://twitter.com/home?status=[Playground] " + encodeURIComponent($$("h2").first().select("a").first().innerHTML.unescapeHTML()) + " - " + window.location.href;
    window.open(url);
    return;
}