function getElement(element) {
	if (document.getElementById && document.getElementById(element)) {
		return document.getElementById(element);
	} else if (document.layers && document.layers[element]) {
		return document.layers[element];
	}
}
function fillElement(element,page,vars,hide,callback,arg) {
	var xmlhttp = getxml();
	var _element = getElement(element);
	xmlhttp.open("POST", page+"?ajax="+vars, true);
	xmlhttp.onreadystatechange = function () {
		try {
			if (xmlhttp.readyState == 4) {
				_element.innerHTML = xmlhttp.responseText;
				if(hide) {
					_hide=getElement(hide);
					if(xmlhttp.responseText=="") {
						_hide.style.display="none";
					} else {
						_hide.style.display="block";
					}
				}
				if(callback) {
					callback(arg,true);
				}
			}
		} catch(e) { 
			try {
				if(callback)
					callback(arg,false,xmlhttp.responseText);
			} catch(e) {
//				alert('callback failed: '+callback);
			}
		}
	}
	xmlhttp.send(null);
}
var bp='/modules/community/inc/comments_ajex.php';
var pl=new Array();
function plus(id) {
	if(!pl[id])
		fillElement("vote"+id,bp,'vote&id='+id+'&d=1');
	pl[id]=1;
}
function minus(id) {
	if(!pl[id])
		fillElement("vote"+id,bp,'vote&id='+id+'&d=0');
	pl[id]=-1;
}
var expand=new Array();
var oldtext=new Array();
var timer=new Array();
function expandReply(id) {
	var replies = getElement("replies"+id);
	if(expand[id]) {
		updateLoad(id);
		fillElement("reply"+id,bp,'getcount&id='+id,null,updateCount,id);
		replies.style.display="none";
		expand[id]=false;
	}
	else if(expand[id]===false) {
		if(replies.innerHTML != "")
			replies.style.display="block";
		expand[id]=true;
		updateLoad(id);
	}
	else {
		oldtext[id] = getElement("reply"+id).innerHTML;
		timer[id]=setTimeout('getElement("reply'+id+'").innerHTML="Loading replies..."',500);
		fillElement("replies"+id,bp,'openreply&id='+id,"replies"+id,updateLoad,id);
		expand[id]=true;
	}
}
function updateCount(id) {
	oldtext[id]=getElement("reply"+id).innerHTML;
	updateLoad(id);
}
function updateLoad(id) {
	var reply=getElement("reply"+id);
	if(timer[id]) clearTimeout(timer[id]);
	if(expand[id])
		reply.innerHTML = "Showing replies.  Click here to hide.";
	else 
		reply.innerHTML = oldtext[id];
}
function submitComment(id) {
	if(id) {
		fillElement('reply'+id,bp,'submit&comment='+document['comment'+id].comment.value.replace(/\n/g,'<br>')+'&id='+id,'reply'+id,commented,id);
		getElement('replydiv'+id).innerHTML='<a onClick="reply('+id+')" class="replylink">Reply to this post</a>';
	} else {
		fillElement('',bp,'submit&comment='+document.comment.comment.value.replace(/\n/g,'<br>')+'&article='+article,null,commented,null);
		document.comment.comment.value='';
	}
}
function commented(id) {
	if(id) {
		fillElement("replies"+id,bp,'openreply&id='+id,"replies"+id,updateLoad,id);
		oldtext[id]=getElement('reply'+id).innerHTML;
	} else {
		fillElement("basecomm",bp,'openreply&article='+article);
	}
}
function reply(id) {
	if(!expand[id]) expandReply(id);
	getElement("replydiv"+id).innerHTML='<form name="comment'+id+'" method="GET" action="javascript: submitComment('+id+');"><textarea name="comment" rows="3" style="width: 100%"></textarea><br /><input type="submit" value="Reply" /></form>';
}
