// Delete icon clicked for a subscriber on "Manage subscribers" page
function delSub(id, email) {
	if (confirm('Are you sure you want to delete the following subscriber?\r\n'+email+'\r\n\r\nTHIS ACTION CANNOT BE UNDONE'))
		new Ajax.Request('ajax_functions.php?a=delsub&i='+id, {
			onSuccess : function(t) {
				if (t.responseText == 'success')
					Element.remove('sub'+id);
				else
					alert('Unexpected response: '+t.responseText);
			},
			onFailure : function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
		});
	
	return false;
}

// Used for when "Save" is clicked on the message composition screen
function saveMsg(form) {
	if (form.format.value > 0) form.content_html.value = tinyMCE.getContent();
	var postBodyString = Form.serialize('frmNewMessage');
	Element.show('saveStatus');
	new Ajax.Request('ajax_functions.php?a=savemsg', {
		onSuccess : function(t) {
			Element.hide('saveStatus');

			if (t.responseText == 'success') {
				Element.show('saveSuccess');
				setTimeout("Effect.Fade('saveSuccess')", 10000);
			}
			else
				alert('Unexpected response: '+t.responseText);
		},
		onFailure : function(t) {
			Element.hide('saveStatus');
			alert('Error ' + t.status + ' -- ' + t.statusText);
		},
		method : 'post',
		postBody : postBodyString
	});
}

// The Display/Hide icon clicked in message archive
function toggleShowPublic(id) {
	var state = document.getElementById('showpub'+id).title;
	var newState = (state == 0) ? 1 : 0;
	new Ajax.Request('ajax_functions.php?a=toggleshowpub&i='+id+'&s='+state, {
		onSuccess : function(t) {
			if (t.responseText == 'success') {
				if (state == 1)
					document.getElementById('showpub'+id).src = 'graphics/icons/pubarchive_hide.gif';
				else
					document.getElementById('showpub'+id).src = 'graphics/icons/pubarchive_show.gif';
				document.getElementById('showpub'+id).title = newState;
					
				Effect.Pulsate('showpub'+id);
			}
			else
				alert('Unexpected response: '+t.responseText);
		},
		onFailure : function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	});
	
	return false;
}

// The Pause/Resume icon clicked in message queue
function toggleSendStatus(id) {
	var state = document.getElementById('status'+id).title;
	var newState = (state == 0) ? 1 : 0;
	new Ajax.Request('ajax_functions.php?a=togglesendstatus&i='+id+'&s='+state, {
		onSuccess : function(t) {
			if (t.responseText == 'success') {
				if (state == 1)
					document.getElementById('status'+id).src = 'graphics/icons/pause.gif';
				else
					document.getElementById('status'+id).src = 'graphics/icons/resume.gif';
				document.getElementById('status'+id).title = newState;
					
				Effect.Pulsate('status'+id);
			}
			else
				alert('Unexpected response: '+t.responseText);
		},
		onFailure : function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	});
	
	return false;
}

// Used for when "Copy from HTML" is clicked on the message composition screen
function html2text(form)
{
	var uanswer = confirm("Are you sure you want to overwrite the plain text content?");

	if ( !uanswer )
		return false;

	form.content_html.value = tinyMCE.getContent();

	var postBodyString = Form.serialize(form);

	Element.show('html2textStatus');
	new Ajax.Request('ajax_functions.php?a=html2text', {
		onSuccess : function(t) {
			Element.hide('html2textStatus');
			Element.show('html2textSuccess');
			setTimeout("Effect.Fade('html2textSuccess')", 10000);
			form.content_text.value = t.responseText;
		},
		onFailure : function(t) {
			Element.hide('html2textStatus');
			alert('Error ' + t.status + ' -- ' + t.statusText);
		},
		method : 'post',
		postBody : postBodyString
	});
}

