openReplies = new Array();
openEdits = new Array();
reply = '';
loginLink = '';

//articleID = <? echo $row['id']; ?>;
//userName = "<? echo $userName; ?>";
//commentCount = <? echo mysql_num_rows($comments); ?>;

function trim (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function getxmlhttp() {
	xmlhttp=null

	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
   
   if (xmlhttp != null) {
		return xmlhttp;
	} else {
		alert("Your browser doesn't like this action.");
	}
}


function openReply(which, sub) {
	
	var found = false;
	
	for (var i = 0; i < openReplies.length; i++) {
		if (openReplies[i] == which) {
			found = true;
		}
	}
	
	if (found == false) {
	
		var reply = document.getElementById('replySample').cloneNode(true);
		var loginLink = document.getElementById('loginSample').cloneNode(true);

		reply.setAttribute("id", "reply" + which);
		reply.style.display = '';		
		reply.style.left = "100px";
		
		if (which == 0) {
			reply.style.top = "-50px";
		}
		
		var replyField = reply.getElementsByTagName("textarea");
		
		replyField[0].setAttribute("id", "replyField" + which);
		
		var loginSpans = loginLink.getElementsByTagName("span");
		var loginAnchor = loginSpans[1].getElementsByTagName("a");
		loginAnchor[0].onclick = function() { openLogin(which); } 
		
		loginLink.style.display = '';	
		
		var replyDiv = reply.getElementsByTagName("div");			
		replyDiv[0].appendChild(loginLink);
		replyDiv[1].setAttribute("id", "replyLoginTarget" + which);
		
		var welcomeSpan = replyDiv[0].getElementsByTagName("span");	
		welcomeSpan[0].innerHTML = "Welcome " + userName + ".";
		
		var replyAnchor = replyDiv[2].getElementsByTagName("a");	
		replyAnchor[0].onclick = function() { addReply(which,sub); } 
		replyAnchor[0].innerHTML = "Add Reply";
		
		if (userName == "Anonymous") {
			welcomeSpan[1].style.display = '';
		}		
		
		var replyTarget = document.getElementById('item' + which);
		
		indentLevel = (parseInt(replyTarget.style.marginLeft) + 20) + "px";
		
		replyTarget.appendChild(reply);
		
		openReplies[openReplies.length] = which;
	}
}


function openEdit(which, id) {
	for (var i = 0; i < openEdits.length; i++) {
		if (openEdits[i] == which) {
			return false;
		}
	}
	
	var edit = document.getElementById('replySample').cloneNode(true);

	var comment = document.getElementById('item' + which);
	var commentDiv = comment.getElementsByTagName("div");	
	var commentText = trim(commentDiv[9].innerHTML);
	
	var cReturn = "\n";
	
	commentText = commentText.replace(/<br>/ig,cReturn);
	commentText = commentText.replace(/<p>/ig,cReturn + cReturn);
	
	edit.setAttribute("id", "edit" + which);
	edit.style.display = '';		
	edit.style.left = "100px";
	edit.style.top = "-35px"; 
	
	var editField = edit.getElementsByTagName("textarea");
	editField[0].setAttribute("id", "editField" + which);
	editField[0].value = commentText;
	
	var editDiv = edit.getElementsByTagName("div");	
	
	var editAnchor = editDiv[1].getElementsByTagName("a");	
	editAnchor[0].href = "javascript:editComment(" + which + "," + id + ");"; 
	editAnchor[0].innerHTML = "Edit Comment";

	indentLevel = comment.style.marginLeft;
	
	comment.parentNode.insertBefore(edit,comment.nextSibling);
	comment.style.display = 'none';
	
	openEdits[openEdits.length] = which;
	
	
}


function openLogin(which) {

	loginForm = document.getElementById('loginDiv').cloneNode(true);
	document.getElementById('loginDiv').parentNode.removeChild(document.getElementById('loginDiv'));
	
	loginForm.style.display = '';
	
	document.getElementById("replyLoginTarget" + which).appendChild(loginForm);
}

function loginQuick() {
	xmlhttp = getxmlhttp();
	xmlhttp.open("POST","loginQuick.php");
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = processSendMessage;

	if (document.getElementById('publicField').checked == true) {
		var checked = 1;
	} else {
		var checked = 0;
	}
	
	xmlhttp.send("u=" + document.getElementById('loginField').value + "&p=" + document.getElementById('passwordField').value + "&s=" + checked);
}

function loginSuccess() {

	document.getElementById('loginDiv').style.display = 'none';	
	for (var i = 0; i < openReplies.length; i++) {
		var reply = document.getElementById("reply" + openReplies[i]);		
		var replyDiv = reply.getElementsByTagName("div");			
		var welcomeSpan = replyDiv[0].getElementsByTagName("span");	
		welcomeSpan[0].innerHTML = "Welcome " + userName + ".";
		welcomeSpan[1].style.display = 'none';
	}
}

function voteReturn(result, type) {

	var comment = document.getElementById('item' + voteTarget);
	var commentDiv = comment.getElementsByTagName("div");

	var controls = document.getElementById('controls' + voteTarget);
	var stillMoreDivs = controls.getElementsByTagName("div");

	var score = parseInt(trim(commentDiv[4].innerHTML));

	var newClass = document.getElementById('voteClassSample').className;
	
	if (result == 1) {
		stillMoreDivs[0].innerHTML = "Voted Up";	
		score = score + 1;
	} else if (result == 2) {
		stillMoreDivs[0].innerHTML = "Voted Down";
		score = score - 1;
	} else if (result == 3) {
		stillMoreDivs[0].innerHTML = "Flagged";
	} else if (result == -1) {
		stillMoreDivs[0].innerHTML = "Error!";
	} else {
		stillMoreDivs[0].innerHTML = "Already Voted!";
	}
	
	if (type == 2) {
		if (score > 0) {
			score = "+" + score;
			commentDiv[4].className = "commentVotesShadowPositive";
			commentDiv[5].className = "commentVotesPositive";
		} else if (score < 0) {
			commentDiv[4].className = "commentVotesShadowNegative";
			commentDiv[5].className = "commentVotesNegative";
		} else {
			commentDiv[4].className = "commentVotesShadowZero";
			commentDiv[5].className = "commentVotesZero";
		}
	}
	
	commentDiv[4].innerHTML = score;
	commentDiv[5].innerHTML = score;
	
	stillMoreDivs[0].className = newClass;
	stillMoreDivs[1].innerHTML = "";
	stillMoreDivs[2].innerHTML = "";
}

function vote(which,id,vote,type) {
	voteTarget = which;
	xmlhttp = getxmlhttp();
	xmlhttp.open("POST","vote.php");
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = processSendMessage;
	xmlhttp.send("a=" + id + "&v=" + vote + "&s=" + type);
}

function addReply(which,sub) {
	addTarget = which;
	xmlhttp = getxmlhttp();
	xmlhttp.open("POST","addComment.php");
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = processSendMessage;
	xmlhttp.send("p=" + encodeURIComponent(document.getElementById('replyField' + which).value) + "&a=" + articleID + "&s=" + sub);
}


function editComment(which, id) {
	addTarget = which;
	xmlhttp = getxmlhttp();
	xmlhttp.open("POST","editComment.php");
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = processSendMessage;
	xmlhttp.send("p=" + encodeURIComponent(document.getElementById('editField' + which).value) + "&id=" + id);
}

function deleteComment(which,id) {
	delTarget = which;
	xmlhttp = getxmlhttp();
	xmlhttp.open("POST","deleteComment.php");
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = processSendMessage;
	xmlhttp.send("id=" + id);
}


function processSendMessage() {
	
	if (xmlhttp.readyState == 4) {	
		var newDoc = document.getElementsByTagName('head').item(0);
		var incThis = document.createElement('script');
		incThis.setAttribute('language', 'javascript');
		incThis.setAttribute('type', 'text/javascript');		
		incThis.text = xmlhttp.responseText;
		
		newDoc.appendChild(incThis);
	}
}


function showNewComment(sub, id, user, commentText, date, score, type, indent) {

	commentCount = commentCount + 1;

	if (commentCount == 1) {
		document.getElementById('noCommentsYet').style.display = 'none';
	}
	
	var comment = document.getElementById('commentSample').cloneNode(true);
	comment.setAttribute("id", "item" + commentCount);
	comment.style.display = '';
	
	var commentDiv = comment.getElementsByTagName("div");	
	
	commentDiv[4].innerHTML = "+" + score;
	commentDiv[5].innerHTML = "+" + score;

	commentDiv[9].innerHTML = commentText;	

	if (type > 1) {
		var anchor = commentDiv[11].getElementsByTagName("a");	
		anchor[0].href = "javascript:openEdit(" + commentCount + "," + id + ");"; 
		anchor[0].innerHTML = "Edit - ";

		var anchor = commentDiv[12].getElementsByTagName("a");	
		anchor[0].href = "javascript:deleteComment(" + commentCount + "," + id + ");"; 
		anchor[0].innerHTML = "Delete";
		
		commentDiv[6].innerHTML = "posted by <a href='user/" + user + "/'>" + user + "</a> on " + date;
		
	} else {
		commentDiv[6].innerHTML = "posted by " + user + " on " + date;	
	}
	
	commentDiv[10].setAttribute("id", "controlsBottom" + id);
	
	var commentTarget = document.getElementById('item' + addTarget);
	var nextComment = commentTarget.nextSibling;

	if (indent) {
		var offset = -80;
	} else {
		var offset = 0;
	}
	comment.className = 'commentBox';

	comment.style.marginLeft =  indentLevel;
	commentTarget.parentNode.insertBefore(comment,commentTarget.nextSibling);
}


function removeComment(target) {
	var comment = document.getElementById("item" + target);
	var discard = comment.parentNode.removeChild(comment);
}


function closeReplies() {

	for (var i = 0; i < openReplies.length; i++) {
		var reply = document.getElementById("reply" + openReplies[i]);
		
		if (reply != null) {
			var discard = reply.parentNode.removeChild(reply);
		}
	}
	
	openReplies = new Array();	
}

function closeEdits() {

	for (var i = 0; i < openEdits.length; i++) {
		var edit = document.getElementById("edit" + openEdits[i]);
		
		if (edit != null) {
			var discard = edit.parentNode.removeChild(edit);
		}
	}
	
	openEdits = new Array();	
}