var BLOG_LIST_DELETE_LIST_URL = '/blog-editor/delete-post';
var BLOG_LIST_PUBLISH_LIST_URL = '/blog-editor/publish-post';

function ModuleBlogListAdminDeletePost(postId) { /* <<<( */

	var response = null;
	var requestUrl = BLOG_LIST_DELETE_LIST_URL;
	var xmlRequest = ModuleBlogListGetXmlRequest();
	var params = '';

	// confirm
	if (true == confirm("Are you sure you wish to delete this post?\nIf this post has been published, that post\nwill also be deleted.\nThis operation cannot be undone.\n")) {

		// build params
		params += 'module-blog-admin-post-id=' + postId;

		xmlRequest.open('POST', requestUrl, false);
		xmlRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlRequest.setRequestHeader('Content-length', params.length);
		xmlRequest.setRequestHeader('Connection', 'close');
		xmlRequest.send(params);

		// get document element
		if (null != xmlRequest.responseXML) {
			response = xmlRequest.responseXML.documentElement;

			// process the response
			if ((null != response.getElementsByTagName('message')) &&
				('success' == response.getElementsByTagName('message')[0].getAttribute('type')))
			{
				alert("The post has been deleted.");
				ModuleBlogListAdminHidePost(postId);
			}
			else {
				// failure
				alert("Sorry, the post could not be deleted from the database.\nPlease refresh the page and try again.");
			}
		}
	}

	return;
} /* )>>> */

function ModuleBlogListAdminHidePost(postId) { /* <<<( */

	var tableRows = null; // array of table rows

	// module-blog-admin-row-post-${post-id}
	if (null != document.getElementById('module-blog-admin-row-post-' + postId)) {
		document.getElementById('module-blog-admin-row-post-' + postId).style.display = 'none';
	}

	// loop through the list table and recalculate row classes
	if (null != document.getElementById('module-blog-admin-post-list')) {

		// get list of table rows
		tableRows = document.getElementById('module-blog-admin-post-list').getElementsByTagName('tr');

		var className = 'module-blog-list-post-row-odd';
		for (var index = 0; tableRows.length > index; index++) {

			var row = tableRows[index];

			if ('none' != row.style.display) {
				document.getElementById(row.id).className = className;

				// alternate classes
				if ('module-blog-list-post-row-odd' == className) {
					className = 'module-blog-list-post-row-even';
				}
				else {
					className = 'module-blog-list-post-row-odd';
				}
			}
		}
	}


	return;
} /* )>>> */

function ModuleBlogListGetXmlRequest() { /* <<<( */

	var obj_xml_request = null;

	// firefox, Opera, Safari
	try {
		obj_xml_request = new XMLHttpRequest();
	}
	catch (e) {

		// Internet Explorer
		try {
			obj_xml_request = new ActiveXObject('Msxml2.XMLHTTP');
		}
		catch (e) {

			// Internet Explorer (alternative)
			try {
				obj_xml_request = new ActiveXObject('Microsoft.XMLHTTP');
			}
			catch (e) {
				alert('Sorry, your browser does not support AJAX.');
			}
		}
	}

	return obj_xml_request;
} /* )>>> */

function ModuleBlogListAdminPublishPost(postId) { /* <<<( */

	var response = null;
	var requestUrl = BLOG_LIST_PUBLISH_LIST_URL;
	var xmlRequest = ModuleBlogListGetXmlRequest();
	var params = '';

	// confirm
	if (true == confirm("Are you sure you wish to publish this post?\nIf this post has been previously published,\nthat post will be updated with this post.\nThis operation cannot be undone.\n")) {

		// build params
		params += 'module-blog-admin-post-id=' + postId;

		xmlRequest.open('POST', requestUrl, false);
		xmlRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlRequest.setRequestHeader('Content-length', params.length);
		xmlRequest.setRequestHeader('Connection', 'close');
		xmlRequest.send(params);

		// get document element
		if (null != xmlRequest.responseXML) {
			response = xmlRequest.responseXML.documentElement;

			// process the response
			if ((null != response.getElementsByTagName('message')) &&
				('success' == response.getElementsByTagName('message')[0].getAttribute('type')))
			{
				alert("The post has been published.");
			}
			else {
				// failure
				alert("Sorry, the post could not be published.\nPlease refresh the page and try again.");
			}
		}
	}

	return;
} /* )>>> */


