// smf quickban modification
// version 0.1 - copyright (c) a2h 2010

// *** QuickBan object.
function QuickBan(oOptions)
{
	this.opt = oOptions;
	this.bInEditMode = false;
	this.sCurMessageId = '';
	this.sCurMemberId = '';
	this.oCurMessageDiv = null;
	this.oCurSubjectDiv = null;
	this.sMessageBuffer = '';
	this.sSubjectBuffer = '';
	this.sSubjectText = '';
	this.bXmlHttpCapable = this.isXmlHttpCapable();
	
	// Show the quickban buttons
	if (this.bXmlHttpCapable)
	{
		for (var i = document.images.length - 1; i >= 0; i--)
			if (document.images[i].id.substr(0, 14) == 'quickban_button_')
				document.images[i].style.display = '';
	}
}

// Determine whether the quick ban can actually be used.
QuickBan.prototype.isXmlHttpCapable = QuickModify.prototype.isXmlHttpCapable;

// Function called when a user presses the ban button.
QuickBan.prototype.show = function (iMessageId, iMemberId, sSessionId, sSessionVar)
{
	if (!this.bXmlHttpCapable)
		return;

	// Add backwards compatibility with old themes.
	if (typeof(sSessionVar) == 'undefined')
		sSessionVar = 'sesc';
	
	// save the member id
	this.sCurMemberId = iMemberId;

	// First cancel if there's another message still being edited.
	if (oQuickModify)
	{
		if (oQuickModify.bInEditMode)
			oQuickModify.modifyCancel();
	}

	// Send out the XMLhttp request to get more info
	ajax_indicator(true);

	// For IE 5.0 support, 'call' is not yet used.
	this.tmpMethod = getXMLDocument;
	this.tmpMethod(smf_prepareScriptUrl(this.opt.sScriptUrl) + 'action=quotefast;quote=' + iMessageId + ';modify;xml', this.onMessageReceived);
	delete this.tmpMethod;
}

// The callback function used for the XMLhttp request retrieving the message.
QuickBan.prototype.onMessageReceived = function (XMLDoc)
{
	var sBodyText = '', sSubjectText = '';

	// No longer show the 'loading...' sign.
	ajax_indicator(false);

	// Grab the message ID.
	this.sCurMessageId = XMLDoc.getElementsByTagName('message')[0].getAttribute('id');

	// If this is not valid then simply give up.
	if (!document.getElementById(this.sCurMessageId))
		return this.modifyCancel();

	// Replace the body part. (well not really, but it's the buffer so hey)
	for (var i = 0; i < XMLDoc.getElementsByTagName("message")[0].childNodes.length; i++)
		sBodyText += XMLDoc.getElementsByTagName("message")[0].childNodes[i].nodeValue;
	this.oCurMessageDiv = document.getElementById(this.sCurMessageId);
	this.sMessageBuffer = getInnerHTML(this.oCurMessageDiv);
	
	// We have to force the body to lose its dollar signs thanks to IE.
	sBodyText = sBodyText.replace(/\$/g, '{&dollarfix;$}');
	
	// ok here's the actual used buffer... smf's js dev monkeys confuse me ;_; (ESPECIALLY WITH THOSE PREFIXES)
	this.sBodyTextToWorkWith = sBodyText.replace(/\{&dollarfix;\$\}/g, '$');
	
	// get the subject (still needed for modifying)
	this.oCurSubjectDiv = document.getElementById('subject_' + this.sCurMessageId.substr(4));
	this.sSubjectBuffer = getInnerHTML(this.oCurSubjectDiv);
	this.sSubjectText = XMLDoc.getElementsByTagName('subject')[0].childNodes[0].nodeValue.replace(/\$/g, '{&dollarfix;$}');
	
	// show the form	
	this.oCurMessageDiv.innerHTML = this.opt.sTemplateBodyBan.replace(/%msg_id%/g, this.sCurMessageId.substr(4)) + this.oCurMessageDiv.innerHTML;

	return true;
}

// Function in case the user presses cancel (or other circumstances cause it).
QuickBan.prototype.banCancel = function ()
{
	this.oCurMessageDiv.removeChild(document.getElementById('quickban'));

	return false;
}

// The function called after a user wants to perform the ban.
QuickBan.prototype.banSave = function (sSessionId, sSessionVar)
{
	// Add backwards compatibility with old themes.
	if (typeof(sSessionVar) == 'undefined')
		sSessionVar = 'sesc';
	
	if (this.opt.bPublic)
	{
		// add the ban message
		this.sBodyTextToWorkWith += '\n\n[color=red][b](User was banned for this post ("' + document.forms.quickModForm.elements['reason'].value + '" - ' + this.opt.sBannerName + '))[/b][/color]';

		// unescape html in the message body because i don't really understand smf and its voodoo js that well (SERIOUSLY WHO CODED THE QUICK MODIFY FUNCTIONALITY)
		// this code is from http://erlend.oftedal.no/blog/?blogid=14
		var htmlNode = document.createElement("div");
		htmlNode.innerHTML = this.sBodyTextToWorkWith;
		this.sBodyTextToWorkWith = htmlNode.innerText ? htmlNode.innerText : htmlNode.textContent; // internet exploder : other browsers respectively
		
		// construct the params
		var i, x = new Array();
		x[x.length] = 'subject=' + escape(this.sSubjectText.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B");
		x[x.length] = 'message=' + escape(this.sBodyTextToWorkWith.replace(/&#/g, "&#38;#").php_to8bit()).replace(/\+/g, "%2B");
		x[x.length] = 'topic=' + parseInt(document.forms.quickModForm.elements['topic'].value);
		x[x.length] = 'msg=' + parseInt(document.forms.quickModForm.elements['msg'].value);

		// Send in the XMLhttp request and let's hope for the best.
		ajax_indicator(true);
		sendXMLDocument.call(this, smf_prepareScriptUrl(this.opt.sScriptUrl) + "action=jsmodify;topic=" + this.opt.iTopicId + ";" + sSessionVar + "=" + sSessionId + ";xml", x.join("&"), this.onModifyDone);
	}
	else
	{
		this.redirect();
	}
	
	return false;
}

// Callback function of the XMLhttp request sending the modified message.
QuickBan.prototype.onModifyDone = function (XMLDoc)
{
	// If we didn't get a valid document, just cancel.
	if (!XMLDoc || !XMLDoc.getElementsByTagName('smf')[0])
	{
		this.banCancel();
		return;
	}
	
	var message = XMLDoc.getElementsByTagName('smf')[0].getElementsByTagName('message')[0];
	var body = message.getElementsByTagName('body')[0];
	var error = message.getElementsByTagName('error')[0];
	
	if (body)
	{
		// redirect and keep showing the loading indicator
		this.redirect();
	}
	else if (error)
	{
		ajax_indicator(false);
		
		setInnerHTML(document.getElementById('error_box'), error.childNodes[0].nodeValue);
		document.forms.quickModForm.message.style.border = error.getAttribute('in_body') == '1' ? this.opt.sErrorBorderStyle : '';
		document.forms.quickModForm.subject.style.border = error.getAttribute('in_subject') == '1' ? this.opt.sErrorBorderStyle : '';
	}
}

// redirect
QuickBan.prototype.redirect = function ()
{
	/*! TODO: INLINE SETTING OF BAN DURATION/ETC?? */
	location.href = this.opt.sScriptUrl + '?action=admin;area=ban;sa=add;u=' + this.sCurMemberId + ';reason=' + escape(document.forms.quickModForm.elements['reason'].value);
}
