var obj;
var obj2;
var id = 0;

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')
				deleteComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'hide')
				hideComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'unhide')
				unhideComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'show')
				showComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'edit')
				editComment(self.xmlHttpReq.responseText, id);
			else if (mode == 'block')
				userBlocked(self.xmlHttpReq.responseText, id);
			else if (mode == 'getpost')
				onPostGet(self.xmlHttpReq.responseText, id);
			else if (mode == 'agree' || mode == 'disagree')
				onAgreedOrDisagreed(self.xmlHttpReq.responseText, id);
			else if (mode == 'report')
				spamReported(self.xmlHttpReq.responseText, id);
			else
				updatePage(self.xmlHttpReq.responseText);
		}
	}
    self.xmlHttpReq.setRequestHeader("Content-length", sendQuery.length);
    self.xmlHttpReq.setRequestHeader("Connection", "close");
	self.xmlHttpReq.send(sendQuery);
}

function updatePage(str) {
	if (str.substr(0, 5) == 'Error') {
		document.getElementById('comment_error').innerHTML = str;
	} else {
		arr = str.split("|||||")
		str = arr[0];
		id = arr[1];
		//document.getElementById('comment_error').innerHTML = 'Comment successfully posted.';
		//document.getElementById('nocomments').style.display = 'none';
		document.getElementById('viewcomments').innerHTML = str;
		//document.getElementById('comment_body').value = '';
		//window.location = '#usercomment_' + id;
		
		setTimeout("changeCommentColor()", 2000)
	}
}

function editComment(str, id) {
	document.getElementById('usercomment_edit_' + id).style.display = 'none';
	obj = document.getElementById('usercomment_' + id);
	obj.style.display = '';
	obj.innerHTML = str;
}

function hideComment(str, id) {
	//alert(str);
	obj = document.getElementById('usercomment_' + id);
	obj.innerHTML = 'You hid this comment';
}

function showComment(str, id) {
	obj = document.getElementById('usercomment_' + id);
	obj.innerHTML = str;
}

function unhideComment(str, id) {
	obj = document.getElementById('usercomment_' + id);
	obj.innerHTML = str;
}

function deleteComment(str, id) {
	alert(str);
	document.getElementById('usercomment_parent_' + id).style.display = 'none';
}

function userBlocked(str, id) {
	alert(str);
}

function changeCommentColor() {
	document.getElementById('usercomment_' + id).style.color = '#000';
	document.getElementById('usercomment_' + id).style.backgroundColor = '#eee';
}

function getPost(id) {
	url = '/users/getpost.asp';
	query = 'id=' + id
	xmlhttpPost(url, query, 'getpost', id);
}

function onPostGet(str, id) {
	//alert(str);
	obj = document.getElementById('usercomment_edit_' + id);
	if (obj != undefined) {
		obj.style.display = '';
		obj.innerHTML = '<form method="post" action="#" onsubmit="doPostEdit(' + id + '); return false;"><textarea name="comment" id="editcomment_' + id + '" style="width: 400px; height: 150px;">' + str + '</textarea><br /><button type="submit">Edit Comment</button> <button type="button" onclick="cancelEdit(' + id + ');">Cancel</button></form></div>'
	} else
		obj2.innerHTML = obj2.innerHTML + '<div id="usercomment_edit_' + id + '"><form method="post" action="#" onsubmit="doPostEdit(' + id + '); return false;"><textarea name="comment" id="editcomment_' + id + '" style="width: 400px; height: 150px;">' + str + '</textarea><br /><button type="submit">Edit Comment</button> <button type="button" onclick="cancelEdit(' + id + ');">Cancel</button></form></div>'
}

function cancelEdit(id) {
	//alert(id);
	obj = document.getElementById('usercomment_' + id);
	obj.style.display = '';
	obj = document.getElementById('usercomment_edit_' + id);
	obj.style.display = 'none';
}

function editPost(id) {
	obj = document.getElementById('usercomment_' + id);
	obj.style.display = 'none';
	obj2 = document.getElementById('usercomment_parent_' + id);
	getPost(id)
}

function doPostEdit(id) {
	//alert('Sorry, commenting is currently disabled.');
	//return false;
	obj = document.getElementById('editcomment_' + id);
	url = '/users/editcomment.asp';
	query = 'id=' + id + '&comment=' + encodeURIComponent(obj.value);
	xmlhttpPost(url, query, 'edit', id);
}

function doComment(str, area, console, contentID) {
	url = '/users/comment.asp?area=' + area + '&console=' + console + '&contentID=' + contentID
	query = 'parent=' + document.getElementById('comment_parent').value + '&comment=' + encodeURIComponent(str)
	xmlhttpPost(url, query, '', 0);
}

function deletePost(id, ism) {
	forSure = confirm('Are you sure you want to delete this comment?');
	if (forSure) {
		if (ism)
			dockPoints = confirm('Would you like to remove the points that were awarded for this comment?');
		else
			dockPoints = true;
		url = '/users/deletecomment.asp';
		query = 'id=' + id + '&dockpoints=' + dockPoints
		xmlhttpPost(url, query, 'delete', id);
	}
}

function hidePost(id) {
	url = '/users/hidecomment.asp';
	query = 'id=' + id;
	xmlhttpPost(url, query, 'hide', id);
}

function unhidePost(id) {
	url = '/users/unhidecomment.asp';
	query = 'id=' + id;
	xmlhttpPost(url, query, 'unhide', id);
}

function showPost(id) {
	url = '/users/showcomment.asp';
	query = 'id=' + id;
	xmlhttpPost(url, query, 'show', id);
}

function blockUser(id) {
	thedate = prompt('What date should this user be blocked until?', 'mm/dd/yyyy');
	if (thedate != '' && thedate != null) {
		url = '/users/blockuser.asp';
		query = 'id=' + id + '&date=' + thedate
		xmlhttpPost(url, query, 'block', id);
	}
}

function agree(id) {
	url = '/users/agreeordisagree.asp';
	query = 'id=' + id + '&agree=true';
	xmlhttpPost(url, query, 'agree', id);
}

function disagree(id) {
	url = '/users/agreeordisagree.asp';
	query = 'id=' + id + '&agree=false';
	xmlhttpPost(url, query, 'disagree', id);
}

function reportSpam(id) {
	forSure = confirm('Are you sure you want to report this as spam?');
	if (forSure) {
		url = '/users/report.asp';
		query = 'id=' + id;
		xmlhttpPost(url, query, 'report', id);
	}
}

function onAgreedOrDisagreed(str, id) {
	if (str == '' && str == null)
		return;
	else if (str.substring(0, 8) == '<strong>')
		document.getElementById('agdg_' + id).innerHTML = str;
	else
		alert(str);
}

function spamReported(str, id) {
	if (str != '' && str != null)
		alert(str);
}

function stripAnchor(str) {
	str = str + "";
	pos = str.indexOf("#");
	if (pos == -1)
		return str;
	return str.substring(0, pos);
}