var NL_newsid, NL_start, NL_show_modify, quickReplyCollapsed, buff_message;
var cur_msg_id, cur_msg_div, in_edit_mode = 0;

function onDocReceived(XMLDoc)
{
	var text = "";
	for (var i = 0; i < XMLDoc.getElementsByTagName("quote")[0].childNodes.length; i++)
		text += XMLDoc.getElementsByTagName("quote")[0].childNodes[i].nodeValue;

	replaceText(text, document.forms.postmodify.message);
	if (typeof window.ajax_indicator == "function")
		ajax_indicator(false);
}

function modify_msg(msg_id, cur_session_id)
{
	//alert("Momenteel zijn we bezig om aanpassingen te doen aan dit onderdeel, het aanpassen van uw bericht is op eigen risico\nCODE: 2");
	if (!window.XMLHttpRequest)
		return;
	if (typeof(window.opera) != "undefined")
	{
		var test = new XMLHttpRequest();
		if (typeof(test.setRequestHeader) != "function")
			return;
	}
	if (in_edit_mode == 1)
		modify_cancel();
	in_edit_mode = 1;
	if (typeof window.ajax_indicator == "function")
		ajax_indicator(true);
	getXMLDocument(NL_scripturl + '?action=quotefastnews;quote=' + msg_id + ';sesc=' + cur_session_id + ';modify;xml', onDocReceived_modify);
}

function onDocReceived_modify(XMLDoc)
{
	var text = "";
	var subject = "";

	// Grab the message ID.
	cur_msg_id = XMLDoc.getElementsByTagName("message")[0].getAttribute("id");

	// Replace the body part.
	for (var i = 0; i < XMLDoc.getElementsByTagName("message")[0].childNodes.length; i++)
		text += XMLDoc.getElementsByTagName("message")[0].childNodes[i].nodeValue;
	cur_msg_div = document.getElementById(cur_msg_id);
	buff_message = getInnerHTML(cur_msg_div);

	// Actually create the content, with a bodge for dissapearing dollar signs.
	text = text.replace(/\$/g,"{&dollarfix;$}");
	text = NL_template_body_edit.replace(/%body%/, text).replace(/%msg_id%/g, cur_msg_id.substr(4));
	text = text.replace(/\{&dollarfix;\$\}/g,"$");
	setInnerHTML(cur_msg_div, text);

	if (typeof window.ajax_indicator == "function")
		ajax_indicator(false);
}

function modify_cancel()
{
	setInnerHTML(cur_msg_div, buff_message);

	in_edit_mode = 0;

	return false;
}

function modify_save(cur_session_id)
{
	if (!in_edit_mode)
		return true;

	var i, x = new Array();
	//alert('develop');
	//x[x.length] = 'message=' + escape(textToEntities(document.forms.quickModForm['message'].value.replace(/&#/g, "&#38;#"))).replace(/\+/g, "%2B");
	x[x.length] = 'message=' + escape(document.forms.quickModForm['message'].value.replace(/&#/g, "&#38;#")).replace(/\+/g, "%2B");
	x[x.length] = 'news=' + parseInt(document.forms.quickModForm.elements['news'].value);
	x[x.length] = 'msg=' + parseInt(document.forms.quickModForm.elements['msg'].value);

	if (typeof window.ajax_indicator == "function")
		ajax_indicator(true);

	sendXMLDocument(NL_scripturl + "?action=jsmodifynews;news=" + NL_news + ";sesc=" + cur_session_id + ";xml", x.join("&"), modify_done);

	return false;
}

function modify_done(XMLDoc)
{
	if (!XMLDoc)
	{
		modify_cancel();
		return;
	}
	
	var message = XMLDoc.getElementsByTagName("nl_modify")[0].getElementsByTagName("message")[0];
	var body = message.getElementsByTagName("body")[0];
	var error = message.getElementsByTagName("error")[0];

	if (body)
	{
		var bodyText = "";
		for (i = 0; i < body.childNodes.length; i++)
			bodyText += body.childNodes[i].nodeValue;

		bodyText = bodyText.replace(/\$/g,"{&dollarfix;$}");
		bodyText = NL_template_body_normal.replace(/%body%/, bodyText);
		bodyText = bodyText.replace(/\{&dollarfix;\$\}/g,"$");
		setInnerHTML(cur_msg_div, bodyText);
		buff_message = bodyText;

		if (NL_show_modify)
		{
			var cur_modify_div = document.getElementById('modified_' + cur_msg_id.substr(4));
			setInnerHTML(cur_modify_div, message.getElementsByTagName("modified")[0].childNodes[0].nodeValue);
		}
	}
	else if (error)
	{
		setInnerHTML(document.getElementById("error_box"), error.childNodes[0].nodeValue);
		document.forms.quickModForm.message.style.border = error.getAttribute("in_body") == "1" ? "1px solid red" : "";
	}

	if (typeof window.ajax_indicator == "function")
		ajax_indicator(false);
}

function showModifyButtons()
{
	var numImages = document.images.length;
	for (var i = 0; i < numImages; i++)
		if (document.images[i].id.substr(0, 14) == 'modify_button_')
			document.images[i].style.display = '';
}

function expandThumb(thumbID)
{
	var img = document.getElementById('thumb_' + thumbID);
	var link = document.getElementById('link_' + thumbID);
	var tmp = img.src;
	img.src = link.href;
	link.href = tmp;
	img.style.width = '';
	img.style.height = '';
	return false;
}

function swapQuickReply()
{
	document.getElementById("quickReplyExpand").src = NL_images_url + "/" + (quickReplyCollapsed ? "collapse.gif" : "expand.gif");
	document.getElementById("quickReplyOptions").style.display = quickReplyCollapsed ? "" : "none";

	quickReplyCollapsed = !quickReplyCollapsed;
}
