/*======================================================================*\
|| #################################################################### ||
|| # vBPicGallery Release 10.7.5								      # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright © 2005-2008. All Rights Reserved.					  # ||
|| # http://www.cpurigs.com/										  # ||
|| #															      # ||
|| #################################################################### ||
\*======================================================================*/

var DelingComment = false;

function verify_delcomment()
{
	if (confirm(vbpicgallery_confirm_del_comment))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function verify_delgallery()
{
	if (confirm(vbpicgallery_confirm_del_gallery))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function deletedoneComment()
{
	if (CommentDel.doEdit.handler.readyState == 4 && CommentDel.doEdit.handler.status == 200)
	{
		DelingComment  = false;
		CommentDel.running  = false;
		var Ereturn = CommentDel.doEdit.handler.responseText;

		var xmldelcomResp = get_XML_String_Parser_Handle(Ereturn);
		var delcomerrors  = xmldelcomResp.getElementsByTagName("errors")[0].childNodes[0].nodeValue;
		var delcomerrortxt  = xmldelcomResp.getElementsByTagName("errortxt")[0].childNodes[0].nodeValue;
		var delcommentid =     xmldelcomResp.getElementsByTagName("commentid")[0].childNodes[0].nodeValue;

		if( delcomerrors != 0 || CommentDel.commentid != delcommentid )
		{
			if( delcomerrors == 0  && delcommentid != CommentDel.commentid )
				delcomerrortxt = "Id Mismatch: " + CommentDel.commentid;
			// add and element with pic and description.
			alert(vbpicgallery_comment_del_fail + delcomerrortxt);
			return false;
		}
		else
		{
			var d = document.getElementById('commenttbody');
			var olddiv = document.getElementById("vbpgcid"+delcommentid);
			d.removeChild(olddiv);
			CommentDel.commentid = -1;
			return false;
		}
		CommentDel.commentid = -1;
	}
}

function _delComment()
{
	this.commentid = -1;
	this.running = false;
	this.doEdit;
}

_delComment.prototype.deleteComment = function(cid,uid,gid)
{

	if (DelingComment)
	{
		alert(vbpicgallery_del_in_progress);
		return false;

	}

	if( verify_delcomment() )
	{
		this.running = true;
		DelingComment = true;
		this.doEdit = new vB_AJAX_Handler(true);
		this.doEdit.onreadystatechange(deletedoneComment);
		this.commentid = cid;
		this.doEdit.send('vbpgajax.php', 'cid=' + cid + '&do=delcomment&u=' + uid + '&g=' + gid);
		return true;
	}
	else
	{
		return false;
	}
};
var CommentDel = new _delComment();


var postingComment = false;

function _postComment()
{
	this.cid = -1;
	this.active = false;
	this.userid = 0;
	this.username = '';

	this.newtrid = 1;
	this.galleryid = 0
	this.doEdit;
}

_postComment.prototype.saveComment = function (username,userid,poststr,gid)
{
	if (!this.active)
	{
		if (postingComment)
		{
			alert(vbpicgallery_posting_in_progress);
			return false;
		}

		this.userid = userid;
		this.username = username;

		this.galleryid = gid;

		TheComment = fetch_object('commentinput').value;

		if (TheComment.replace(/ /g, '') == '')
		{
			alert(vbpicgallery_must_enter_comment);
			return false;
		}

		this.doEdit = new vB_AJAX_Handler(true);
		this.doEdit.onreadystatechange(saveDonecomment);
		this.doEdit.send('vbpgajax.php', 'g=' + gid + '&comment=' + PHP.urlencode(poststr) + '&do=addcomment&u=' + userid);
		postingComment = true;
		this.active = true;
	}

	return true;
};

var PostNewComment = new _postComment();


function post_new_comment(obj,username,userid,gid)
{
	var poststr = document.getElementById("commentinput").value;
	PostNewComment.saveComment(username,userid,poststr,gid);
}

function saveDonecomment()
{
	if (PostNewComment.doEdit.handler.readyState == 4 && PostNewComment.doEdit.handler.status == 200)
	{
		postingComment  = false;
		PostNewComment.active  = false;
		var Ereturn = PostNewComment.doEdit.handler.responseText;
		var xmlnewcomResp = get_XML_String_Parser_Handle(Ereturn);
		var newcomerrors  = xmlnewcomResp.getElementsByTagName("errors")[0].childNodes[0].nodeValue;
		var newcomerrortxt  = xmlnewcomResp.getElementsByTagName("errortxt")[0].childNodes[0].nodeValue;

		var newcommentid =     xmlnewcomResp.getElementsByTagName("commentid")[0].childNodes[0].nodeValue;
		var newuserid =     xmlnewcomResp.getElementsByTagName("postinguserid")[0].childNodes[0].nodeValue;
		var newgid =     xmlnewcomResp.getElementsByTagName("galleryid")[0].childNodes[0].nodeValue;
		var newcomment =     xmlnewcomResp.getElementsByTagName("comment")[0].childNodes[0].nodeValue;

		if( newcomerrors != 0 || PostNewComment.userid != newuserid || newgid != PostNewComment.galleryid)
		{
			if( newcomerrors == 0  && newuserid != PostNewComment.userid )
				newcomerrortxt = "Id Mismatch: " + CommentDel.commentid;

			alert(vbpicgallery_posting_failed + newcomerrortxt);
			return false;
		}
		else
		{
			var USERID = newuserid;
			var USERNAME = PostNewComment.username;
			var NEWID = newcommentid;

			// remove the first comment box if it's there.
			var d = document.getElementById('commenttbody');
			var olddiv = document.getElementById('befirstcomment');
			if(olddiv) {d.removeChild(olddiv);}


			// find the table element
			var commentlist = document.getElementById('newcommenttbody');
			// create a new row element
			var newtr = document.createElement('tr');

			// give the element an id
			var TRIdName = "vbpgcid"+ NEWID + "new";
			newtr.setAttribute("id",TRIdName);

			// add it
			commentlist.appendChild(newtr);

			// get its handle
			var newertr = document.getElementById(TRIdName)

			// create the 3 columns
			var tda = document.createElement('td');
			tda.setAttribute("class","alt2");
			tda.setAttribute("className", "alt2");
			tda.setAttribute("width","10%");
			tda.innerHTML = "<a href=\"member.php?u="+USERID+"\"><b>"+USERNAME+"</b></a>"
			newertr.appendChild(tda);

			var tdb = document.createElement('td');
			tdb.setAttribute("class","alt1");
			tdb.setAttribute("className", "alt1");

			var cfield = document.getElementById('commentinput');
			tdb.innerHTML = newcomment;
			//tdb.innerHTML = cfield.value;
			newertr.appendChild(tdb);

			var tdc = document.createElement('td');
			tdc.setAttribute("class","alt2");
			tdc.setAttribute("className", "alt2");
			tdc.innerHTML = "Today";
			newertr.appendChild(tdc);
			//var cfield = document.getElementById('commentinput');
			cfield.value = "";
			PostNewComment.newtrid = PostNewComment.newtrid + 1;
		}
		PostNewComment.cid = -1;
	}
	return false;
}