// JavaScript Document
function showServerStatus(){
	var nowTime = new Date();
	$.get("/servers/server_stat.xml?" + parseInt( nowTime.getTime() / 10000 ),
		function(data){
			var json = $.xmlToJSON(data); // convert the xml to JSON
			serverParse(json);
		}
	);
}
function serverParse(json){
	var table = document.getElementById("serverStatus");
	// Write out the timestamp and timezone
	document.getElementById("updateTime").innerHTML = 'Updated: ' + json.timestamp + ' ' + json.timezone;
	for(var i = table.rows.length - 1; i > 0; --i){ // need to keep the header row and delete the rest
		table.deleteRow(i);
	}
	if (table.rows.length == 1){ // if there is only the header row write out the new data
		for(var i=0; i < json.server.length; ++i){
			var serverName = json.server[i].name;
			var serverSt = json.server[i].status;
			var evenNum = isEven(i);
			addRow(serverName,serverSt,evenNum);
		}
	}
}
function addRow(serverName,serverSt,evenNum)
{
	if (!document.getElementsByTagName) return;
	tabBody=document.getElementsByTagName("tbody").item(0);
	row=document.createElement("tr");
	cell0 = document.createElement("td");
	cell1 = document.createElement("td");
	span1=document.createElement("span");
	textnode0=document.createTextNode(serverName);
	textnode1=document.createTextNode(serverSt);
	cell0.appendChild(textnode0);
	cell1.appendChild(span1);
	span1.appendChild(textnode1);
	cell1.setAttribute("class", 'status stat_'+serverSt.toLowerCase());
	if (ifIE()) cell1.setAttribute("className", 'status stat_'+serverSt.toLowerCase());
	row.appendChild(cell0);
	row.appendChild(cell1);
	tabBody.appendChild(row);
	if (!evenNum) row.setAttribute("class", "oddRow");
	if (!evenNum && ifIE()) row.setAttribute("className", "oddRow");
}
function isEven(num) {
	return !(num % 2);
}
function ifIE(){
	var browserName=navigator.appName;
	if(browserName=="Microsoft Internet Explorer"){
		return true;
	}
}
function statusTable(){
	var tmpStr = '';
	tmpStr += '<table width="100%" border="0" cellpadding="0" cellspacing="0" id="serverStatus" class="standardTable">';
		tmpStr += '<thead>';
		  tmpStr += '<tr>';
			tmpStr += '<td class="serverName">Server Name</td>';
			tmpStr += '<td class="serverUp">Server Status</td>';
		  tmpStr += '</tr>';
		tmpStr += '</thead>';
		tmpStr += '<tbody id="tBody">';
		tmpStr += '</tbody>';
	tmpStr += '</table>';
	document.getElementById("statusTable").innerHTML = tmpStr;
}
statusTable();
showServerStatus();

setInterval(showServerStatus, 20000);