/*======================================================================*\
|| #################################################################### ||
|| # vBPicGallery Release 10.7.5								      # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright © 2005-2008. All Rights Reserved.					  # ||
|| # http://www.cpurigs.com/										  # ||
|| #															      # ||
|| #################################################################### ||
\*======================================================================*/

function urldecode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'

    var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urlencode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}

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;
		//YAHOO.util.Connect.setDefaultPostHeader(false);

			//YAHOO.util.Connect.setDefaultXhrHeader(false);

		//Content-Type: application/x-www-form-urlencoded; charset=UTF-8
		//alert(vbpicgallery_charset);

			//YAHOO.util.Connect.initHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + vbpicgallery_charset);

		//A.setRequestHeader("Content-Type", "text/plain;charset=" + vbpicgallery_charset);
		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;

		//alert(PostNewComment.doEdit.handler.responseText);
		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;

		//alert(Ereturn);
		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;
		//alert(newcomment);

		//alert(newcomment);

		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; //PostNewComment.userid;
			var USERNAME = PostNewComment.username;
			var NEWID = newcommentid; //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
			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;
}
