function xmlhttpPost(strURL, sendQuery, mode, id) {
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			if (mode == 'delete')
				deleted(self.xmlHttpReq.responseText, id);
			else if (mode == 'edit')
				edited(self.xmlHttpReq.responseText, id);
			else if (mode == 'getpost')
				gotpost(self.xmlHttpReq.responseText, id);
			else
				replied(self.xmlHttpReq.responseText);
		}
	}
    self.xmlHttpReq.setRequestHeader("Content-length", sendQuery.length);
    self.xmlHttpReq.setRequestHeader("Connection", "close");
	self.xmlHttpReq.send(sendQuery);
}

function do_delete(id) {
	if (confirm('Are you sure you want to delete this post?')) {
		q = 'id=' + id;
		xmlhttpPost('/forum/includes/delete.php', q, 'delete', id);
	}
}

function do_getpost(id) {
	q = 'id=' + id + '&forumid=' + document.getElementById('forumid').value + '&threadid=' + document.getElementById('threadid').value;
	xmlhttpPost('/forum/includes/getpost.php', q, 'getpost', id);
}

function do_edit(id) {
	q = 'body=' + document.getElementById('edit_body_' + id).value + '&id=' + id;
	xmlhttpPost('/forum/includes/edit.php', q, 'edit', id);
}

function do_reply() {
	if (document.getElementById('body').value == document.getElementById('body').defaultValue || document.getElementById('body').value == '') {
		alert('Please type a message');
		return;
	}
	q = 'body=' + document.getElementById('body').value + '&forumid=' + document.getElementById('forumid').value + '&threadid=' + document.getElementById('threadid').value;
	document.getElementById('body').value = '';
	xmlhttpPost('/forum/includes/post.php', q, 'reply', 0);
}

function deleted(str, id) {
	arr = str.split('|||||');
	if (arr[0] == 'error')
		alert(arr[1]);
	else {
		alert(arr[1]);
		document.getElementById('post_' + id).style.display = 'none';
	}
}

function gotpost(str, id) {
	arr = str.split('|||||');
	if (arr[0] == 'error')
		alert(arr[1]);
	else {
		document.getElementById('post_body_' + id).style.display = 'none';
		document.getElementById('post_edit_' + id).innerHTML = '<textarea id="edit_body_' + id + '">' + arr[1] + '</textarea><br /><button type="button" onclick="do_edit(' + id + ');">Edit Post</button> <button type="button" onclick="cancel_edit(' + id + ');">Cancel</button>';
		document.getElementById('post_edit_' + id).style.display = 'inline';
	}
}

function edited(str, id) {
	arr = str.split('|||||');
	if (arr[0] == 'error')
		alert(arr[1]);
	else {
		document.getElementById('post_edit_' + id).innerHTML = '';
		document.getElementById('post_edit_' + id).style.display = 'none';
		document.getElementById('post_body_' + id).innerHTML = arr[1];
		document.getElementById('post_body_' + id).style.display = 'inline';
	}
}

function replied(str) {
	arr = str.split('|||||');
	if (arr[0] == 'error')
		alert(arr[1]);
	else
		document.getElementById('posts').innerHTML += arr[1];
}

function cancel_edit(id) {
	document.getElementById('post_body_' + id).style.display = 'inline';
	document.getElementById('post_edit_' + id).innerHTML = '';
	document.getElementById('post_edit_' + id).style.display = 'none';
}

function doquote(str) {
	document.getElementById('body').value = "@" + str + ":\n\n";
	document.getElementById('body').focus();
}

function newthread() {
	if (document.getElementById('body').value == document.getElementById('body').defaultValue || document.getElementById('body').value == '') {
		alert('Please type a message');
		return false;
	}
	if (document.getElementById('subject').value == document.getElementById('subject').defaultValue || document.getElementById('subject').value == '') {
		alert('Please type a subject');
		return false;
	}
	return true;
}