function Comment(id){
	document.getElementById('NewsComment').style.display = 'block';
	document.getElementById('NewsCommentInner').innerHTML = '<center><img src="/images/spinner.gif" /></center>';
	
	url = "/includes/news.php?CommentID="+id;
	
	xmlHttpCommentDownload = makeHttp();
	xmlHttpCommentDownload.onreadystatechange = function() {
		if(checkReadyState(xmlHttpCommentDownload))
		{
			answer = xmlHttpCommentDownload.responseText;
			document.getElementById('NewsCommentInner').innerHTML = answer;
		}
	}
	SendGETRequest(xmlHttpCommentDownload,url);
}

function CloseComment(){
	document.getElementById('NewsComment').style.display = 'none';
	document.getElementById('NewsCommentInner').innerHTML = '';
}

function CommentDo(id){
	url = "/includes/news.php?CommentDo="+id;
	postdata = 'commentbody='+encodeURIComponent(document.getElementById('commentbody').value.replace('"','&quot;'));
	
	document.getElementById('NewsCommentInner').innerHTML = '<center><img src="/images/spinner.gif" /></center>';
	
	xmlHttpCommentDo = makeHttp();
	xmlHttpCommentDo.onreadystatechange = function() {
		if(checkReadyState(xmlHttpCommentDo))
		{
			answer = xmlHttpCommentDo.responseText;
			document.getElementById('NewsCommentInner').innerHTML = answer;
		}
	}
	
	SendPOSTRequest(xmlHttpCommentDo,url,postdata);
}

function CommentDelete(id){
	url = "/includes/news.php?CommentDelete="+id;
	
	postdata = 'delete=yes';
	
	xmlHttpCommentDelete = makeHttp();
	xmlHttpCommentDelete.onreadystatechange = function() {
		if(checkReadyState(xmlHttpCommentDelete))
		{
			answer = xmlHttpCommentDelete.responseText;
			document.getElementById('comment_'+id).innerHTML = answer;
			document.getElementById('commentdelete_'+id).src = '/images/white.png';
		}
	}
	
	SendPOSTRequest(xmlHttpCommentDelete,url,postdata);
}

function fade(id,op){
	ob = document.getElementById(id).style;
	ob.opacity = op;
    ob.MozOpacity = op;
    ob.KhtmlOpacity = op;
    ob.filter = "alpha(opacity=" + op*100 + ")";
	if(op <= 1){
		func = "fade('"+id+"',"+(op+.05)+");";	
		setTimeout(func,50);
	}
}

var donatorNodes;
var supporterNodes;

function fadeDonators(div){
	url = "/includes/teamrandom.php?type=donators&random=" + Math.round(1000000*Math.random());
	
	xmlHttpGetDonators = makeHttp();
	xmlHttpGetDonators.onreadystatechange = function() {
		if(checkReadyState(xmlHttpGetDonators))
		{
			donatorNodes = xmlHttpGetDonators.responseXML.documentElement.getElementsByTagName("team");
			showDonators(div, 0);
		}
	}
	
	SendGETRequest(xmlHttpGetDonators,url);
}

function showDonators(div, wich){
	var nmsg = donatorNodes.length;

	username = donatorNodes[wich].getElementsByTagName("username");
	countryid = donatorNodes[wich].getElementsByTagName("countryid");
	teamid = donatorNodes[wich].getElementsByTagName("teamid");
	teamname = donatorNodes[wich].getElementsByTagName("teamname");
	supporter = donatorNodes[wich].getElementsByTagName("supporter");
	
	document.getElementById(div).style.opacity = 0.0;
	var output = "<b>" + username[0].firstChild.nodeValue + "</b>";
	if(supporter[0].firstChild.nodeValue != 'NaN' && supporter[0].firstChild.nodeValue != 'none'){
			output += " <img src=\"/images/supporter/supporter_" + supporter[0].firstChild.nodeValue + ".png\" />";
	}
	output += ", <a href=\"/teamdetails.php?teamid=" + teamid[0].firstChild.nodeValue + "\"><b>" + teamname[0].firstChild.nodeValue + "</b></a> <a href=\"/countryinfo.php?CountryID=" + countryid[0].firstChild.nodeValue + "\"><img style=\"border:thin outset #DDDDDD;\" src=\"/images/flags/ht/" + countryid[0].firstChild.nodeValue + "flag.png\" /></a>";
	
	document.getElementById(div).innerHTML = output;
	fade(div,0.0);
	wich++;
	if(wich == nmsg){
		fadeDonators(div);
	}
	else{	
		setTimeout('showDonators("' + div + '", "' + wich + '")',5000);
	}
}

function fadeSupporters(div){
	url = "/includes/teamrandom.php?type=supporters&random=" + Math.round(1000000*Math.random());
	
	xmlHttpGetSupporters = makeHttp();
	xmlHttpGetSupporters.onreadystatechange = function() {
		if(checkReadyState(xmlHttpGetSupporters))
		{
			supporterNodes = xmlHttpGetSupporters.responseXML.documentElement.getElementsByTagName("team");
			showSupporters(div, 0);
		}
	}
	
	SendGETRequest(xmlHttpGetSupporters,url);
}

function showSupporters(div, wich){
	var nmsg = supporterNodes.length;

	username = supporterNodes[wich].getElementsByTagName("username");
	countryid = supporterNodes[wich].getElementsByTagName("countryid");
	teamid = supporterNodes[wich].getElementsByTagName("teamid");
	teamname = supporterNodes[wich].getElementsByTagName("teamname");
	supporter = supporterNodes[wich].getElementsByTagName("supporter");
	
	document.getElementById(div).style.opacity = 0.0;
	var output = "<b>" + username[0].firstChild.nodeValue + "</b>";
	if(supporter[0].firstChild.nodeValue != 'NaN' && supporter[0].firstChild.nodeValue != 'none'){
			output += " <img src=\"/images/supporter/supporter_" + supporter[0].firstChild.nodeValue + ".png\" />";
	}
	output += ", <a href=\"/teamdetails.php?teamid=" + teamid[0].firstChild.nodeValue + "\"><b>" + teamname[0].firstChild.nodeValue + "</b></a> <a href=\"/countryinfo.php?CountryID=" + countryid[0].firstChild.nodeValue + "\"><img style=\"border:thin outset #DDDDDD;\" src=\"/images/flags/ht/" + countryid[0].firstChild.nodeValue + "flag.png\" /></a>";
	
	document.getElementById(div).innerHTML = output;
	fade(div,0.0);
	wich++;
	if(wich == nmsg){
		fadeSupporters(div);
	}
	else{	
		setTimeout('showSupporters("' + div + '", "' + wich + '")',5000);
	}
}

var shout_delay = 4000;
var shout_id = 0;
var deleteLoader = true;

function checkempty(){
	if(document.getElementById('shoutText').value == ""){
		return false;
	}
	return true;
}

function print_html(teamid, supporter, username, message, time, fader){
	if(deleteLoader){document.getElementById('shoutMessages').innerHTML = "";deleteLoader = false;}
	if(message.match(":BR:")){}
	else{
		message = message.replace(/\n/g, "<br />");
	}
	message = message.replace(/:BR:/g, "<br />");
	username = username.replace(/:BR:/g, "<br />");
	
	message = message.replace(/\:D/g,"<img src='/images/smileys/icon_biggrin.gif' alt=':D' />");
	message = message.replace(/\:d/g,"<img src='/images/smileys/icon_biggrin.gif' alt=':D' />");
	message = message.replace(/\:P/g,"<img src='/images/smileys/icon_razz.gif' alt=':P' />");
	message = message.replace(/\:p/g,"<img src='/images/smileys/icon_razz.gif' alt=':p' />");
	message = message.replace(/\:S/g,"<img src='/images/smileys/icon_confused.gif' alt=':S' />");
	message = message.replace(/\:s/g,"<img src='/images/smileys/icon_confused.gif' alt=':s' />");
	message = message.replace(/\:O/g,"<img src='/images/smileys/icon_surprised.gif' alt=':O' />");
	message = message.replace(/\:o/g,"<img src='/images/smileys/icon_surprised.gif' alt=':o' />");
	message = message.replace(/\:\|/g,"<img src='/images/smileys/icon_eek.gif' alt=':|' />");
	message = message.replace(/B\)/g,"<img src='/images/smileys/icon_cool.gif' alt='B)' />");
	message = message.replace(/\:\'\(/g,"<img src='/images/smileys/icon_cry.gif' alt=\":'(\" />");
	message = message.replace(/\:@/g,"<img src='/images/smileys/icon_evil.gif' alt=':@' />");
	message = message.replace(/\:\(/g,"<img src='/images/smileys/icon_sad.gif' alt=':(' />");
	message = message.replace(/\:\)/g,"<img src='/images/smileys/icon_smile.gif' alt=':)' />");
	message = message.replace(/;\)/g,"<img src='/images/smileys/icon_wink.gif' alt=';)' />");
	
	var timeaway = '';
	if(time.indexOf('notime') != -1){}
	else{
		timeaway = time;
	}
	var supporterOut = '';
	if(supporter.indexOf('none') != -1){
	}
	else{
		supporterOut = "<img src=\"/images/supporter/supporter_"+supporter+".png\" />";
	}
	
	var opacity = 0.0;
	var currentid = "comment-"+shout_id;
	var x = "<span id='"+currentid+"'>"+time+" "+supporterOut+" <a href=\"/teamdetails.php?teamid="+teamid+"\">"+username+"</a>: "+message+"</span><br />";
	document.getElementById('shoutMessages').innerHTML = x + document.getElementById('shoutMessages').innerHTML;
	fade(currentid, fader);
}

function TimeoutRequest(){
	document.getElementById('shoutLoading').style.display = 'none';
	document.getElementById('shoutText').value = '';
	document.getElementById('shoutText').disabled = '';
	document.getElementById('shoutButton').disabled = '';
}

function sendComment(){
	document.getElementById('shoutLoading').style.display = 'block';
	document.getElementById('shoutText').disabled = 'disabled';
	document.getElementById('shoutButton').disabled = 'disabled';
	
	var sendTimeout = setTimeout(TimeoutRequest, 5000);
	var url = "includes/shoutbox.php";
	var checkit = checkempty();

	if(checkit){
		message = document.getElementById('shoutText').value;
		message = message.replace(/&/g,":ampersand:");
		
		xmlHttpShout = makeHttp();
		xmlHttpShout.onreadystatechange = function() {
			if(checkReadyState(xmlHttpShout)){
				clearTimeout(sendTimeout);
				document.getElementById('shoutLoading').style.display = 'none';
				document.getElementById('shoutText').value = '';
				document.getElementById('shoutText').disabled = '';
				document.getElementById('shoutButton').disabled = '';
			}
		}
		var postdata = "message=" + message;
		SendPOSTRequest(xmlHttpShout,url,postdata);
	}
	else{
		clearTimeout(sendTimeout);
		document.getElementById('shoutLoading').style.display = 'none';
		document.getElementById('shoutText').value = '';
		document.getElementById('shoutText').disabled = '';
		document.getElementById('shoutButton').disabled = '';
	}
}

function initShoutbox(){
	xmlHttpShout = makeHttp();
	xmlHttpShout.onreadystatechange = onResponse;
	
	var url = "includes/shoutbox.php";
	SendGETRequest(xmlHttpShout,url);
	
	stayChecking();
}

function onResponse(){
	if(checkReadyState(xmlHttpShout)){
		var nodes = xmlHttpShout.responseXML.documentElement.getElementsByTagName("comment");
		var nmsg = nodes.length;
		for(i = 0 ; i < nmsg ; i++){
			shoutidprev = nodes[i].getElementsByTagName("id");
			shout_id = shoutidprev[0].firstChild.nodeValue;
			teamid = nodes[i].getElementsByTagName("teamid");
			supporter = nodes[i].getElementsByTagName("supporter");
			username = nodes[i].getElementsByTagName("username");
			message = nodes[i].getElementsByTagName("message");
			time = nodes[i].getElementsByTagName("time");
			try{
				print_html(teamid[0].firstChild.nodeValue, supporter[0].firstChild.nodeValue, username[0].firstChild.nodeValue, message[0].firstChild.nodeValue, time[0].firstChild.nodeValue, 0);
			}
			catch (e){}
		}
	}
}

function stayChecking(){	
    setInterval('Reload()', shout_delay);
    return true;
}

function Reload(){
	xmlHttpShout = makeHttp();
	xmlHttpShout.onreadystatechange = onResponse;
	
	var url = "includes/shoutbox.php";
	var postdata = "lastid=" + shout_id;
	
	SendPOSTRequest(xmlHttpShout,url,postdata);
}