/*======================================================================*\
|| #################################################################### ||
|| # vBPicGallery Release 10.0.4								      # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright © 2005-2006. All Rights Reserved.					  # ||
|| # http://www.cpurigs.com/										  # ||
|| #															      # ||
|| #################################################################### ||
\*======================================================================*/

DelingComment = false;

function _DelComment()
{
	this.commentid = -1;
	this.running = false;
}

_DelComment.prototype.DeleteComment = function(cid,uid,gid)
{
	
	if (DelingComment)
	{
		alert('Delete in progress..');
		return false;
				
	}

	if( verify_del() )
	{
		this.running = true;
		DelingComment = true;
		doEdit = new vB_AJAX_Handler(true);
		doEdit.onreadystatechange(DeleteDone);
		this.commentid = cid;
		doEdit.send('vbpgajax.php', 'do=delcomment&cid=' + cid + '&u=' + uid + '&g=' + gid);
		return true;
	}
	else
	{
		return false;
	}
}
CommentDel = new _DelComment;

function DeleteDone()
{
	if (doEdit.handler.readyState == 4 && doEdit.handler.status == 200)
	{
		DelingComment  = false;
		CommentDel.running  = false;
		Ereturn = doEdit.handler.responseText;
		if( Ereturn != 'ok' )
		{
			// add and element with pic and description.
			alert("Delete failed: " + Ereturn);
			return false;
		}
		else
		{
			var d = document.getElementById('commenttbody');
			var olddiv = document.getElementById("vbpgcid"+CommentDel.commentid);
			d.removeChild(olddiv);
			CommentDel.commentid = -1;
			return false;
		} 
		CommentDel.commentid = -1;
	}
}

postingComment = false;

function _PostComment()
{
	this.cid = -1;
	this.active = false;
	this.userid = 0;
	this.username = '';
	this.canadmin = 0;
	this.newtrid = 1;
}

_PostComment.prototype.Save = function (canadmin,username,userid,poststr,gid)
{
	if (!this.active)
	{
		if (postingComment)
		{
			alert('Posting in progress..');
			return false;
		}
		
		this.userid = userid;
		this.username = username;
		this.canadmin = canadmin;		

		TheComment = fetch_object('commentinput').value;

		if (TheComment.replace(/ /g, '') == '')
		{
			alert('You must enter a Comment!');
			return false;
		}

		doEdit = new vB_AJAX_Handler(true);
		doEdit.onreadystatechange(SaveDone);
		doEdit.send('vbpgajax.php', 'do=addcomment&g=' + gid + '&u=' + userid + '&comment=' + PHP.urlencode(poststr) );
		postingComment = true;
		this.active = true;
	}

	return true;
}

PostNewComment = new _PostComment;


function get(obj,canadmin,username,userid,gid) 
{
	var poststr = document.getElementById("commentinput").value;
	PostNewComment.Save(canadmin,username,userid,poststr,gid);
}

function SaveDone()
{
	if (doEdit.handler.readyState == 4 && doEdit.handler.status == 200)
	{
		postingComment  = false;
		PostNewComment.active  = false;
		Ereturn = doEdit.handler.responseText;
		if( Ereturn == '-ERROR1!-' || Ereturn == '-ERROR2!-' || Ereturn == '-ERROR3!-' || Ereturn == '' )
		{
			alert('Posting failed! '+ Ereturn);
			return false;
		}
		else
		{
			var USERID = PostNewComment.userid;
			var USERNAME = PostNewComment.username;
			var NEWID = PostNewComment.newtrid;

			// 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
			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");
			tdb.innerHTML = Ereturn;
			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;
}

function verify_del()
{
	if (confirm('Are you sure?'))
	{
		return true;
	}
	else
	{
		return false;
	}
}