//This is mick's work.
//Don't steal or he'll be mad at you
//micksam7.com // rscript.org

//rtUpdate
//A very lame aJax-y way to grab the latest tracker table
function rtUpdate() {
	//Hack: Our cookie setting on the xml sock doesn't work in firefox. So we force a page load in those cases.
	//This also removes the need for a "Link to this page" button.
	if (document.getElementById("rtFormyformform").user.value != document.getElementById("rtFormyformform").curUser.value)
		return true;


	var xmlSock;
	try {
		xmlSock = new XMLHttpRequest(); //Your smarter web browsers
	} catch (e) {
		try {
			xmlSock = new ActiveXObject("Microsoft.XMLHTTP"); //Your dumber web browsers
		} catch (e) {
			//alert("DEBUG: No fish :(");
			return true; //Your really smart web browsers
		}
	}
	
	//Ok we'll assume we got it now, yes? Make some fun graphics go PEW PEW
	document.getElementById("rtLoading").style.display = "inline";
	
	//And setup a catch for the objecto
	xmlSock.onreadystatechange = function() {
		//alert("DEBUG: XML returned state: "+xmlSock.readyState);
		//Wrap it up.
		if (xmlSock.readyState == 4) {
			document.getElementById("rtTable").innerHTML = xmlSock.responseText;
			document.getElementById("rtLoading").style.display = "none";
			var user = escape(document.getElementById("rtFormyformform").user.value);
			document.getElementById("rtTrackGraph").src = "/graph-"+user+".0.exp.86400.600x300.000000.000000...0.png?"+Math.floor(Math.random()*10000);
		}
	}

	var user = escape(document.getElementById("rtFormyformform").user.value);
	
	//NOW! *click*
	xmlSock.open("POST","/rtXML.php",true);
	xmlSock.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlSock.send("user="+user);
	
	document.getElementById("rtTrackLink").href = "/track-"+user; 
	
	//alert("DEBUG: Ya ok got it");
	
	return false;
}

//tpUpdate
//Another lame aJax-y way to grab the latest tracker info
function tpUpdate(user) {

	var xmlSock;
	try {
		xmlSock = new XMLHttpRequest(); //Your smarter web browsers
	} catch (e) {
		try {
			xmlSock = new ActiveXObject("Microsoft.XMLHTTP"); //Your dumber web browsers
		} catch (e) {
			//alert("DEBUG: No fish :(");
			return true; //Your really "smart" web browsers
		}
	}
	
	//Ok we'll assume we got it now, yes? Make some fun graphics go PEW PEW
	document.getElementById("tpLoading").style.display = "inline";
	
	//And setup a catch for the objecto
	xmlSock.onreadystatechange = function() {
		//alert("DEBUG: XML returned state: "+xmlSock.readyState);
		//Wrap it up.
		if (xmlSock.readyState == 4) {
			document.getElementById("tpLoading").style.display = "none";
			//alert("DEBUG: XML returned: "+xmlSock.responseText);
			if (xmlSock.responseText == "CHANGED") {
				document.getElementById("tpUpdate").innerHTML = "Stats Updated.<br />Refreshing..";
				document.location.href = document.location.href; //Refresh
			} else if (xmlSock.responseText == "NOCHANGE") {
				document.getElementById("tpUpdate").innerHTML = "No Change Found.<br />Log Time Updated.";
			} else {
				document.getElementById("tpUpdate").innerHTML = "An Error Occured.<br />Are Hiscores Down?";
			}
		}
	}

	var user = escape(document.getElementById("tpFormyformform").user.value);
	
	//NOW! *click*
	xmlSock.open("POST","/updateUserJS.php",true);
	xmlSock.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlSock.send("user="+user);
	
	//alert("DEBUG: Ya ok got it for "+user);
	
	return false;
}


function convertTime(secs) {
	var minutes = Math.floor(secs / 60);
	var seconds = secs % 60;
	var hours = Math.floor(minutes / 24);
	minutes = minutes % 24;
	if (hours == 0) {
		if (minutes == 0) {
			if (seconds == 0) {
				return "0s";
			}
			return seconds + "s";
		}
		return minutes + "m " + seconds + "s";
	}	
	return hours + "h " + minutes + "m " + seconds + "s";
}
