//This code powers the DHTML/AJAX functionality of the high scores table,
//using the PHP file to generate the actual HTML.

function CurrentMonth()
{
	var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", 
"October", "November", "December");
	var d = new Date();
	var month = d.getMonth();
	var year = d.getFullYear();
	document.write(months[month]+" "+year);
}

//Ask the PHP for the new HTML for the table
function Update()
{
	//Post loading notice
	document.getElementById("high_scores").innerHTML = "Loading...";

	//Get settings from dropdown boxes
	selectTime = document.getElementById('select_time');
	time = selectTime.options[selectTime.selectedIndex].value;
	selectSize = document.getElementById('select_size');
	size = selectSize.options[selectSize.selectedIndex].value;
	selectTable = document.getElementById('select_table');
	table = selectTable.options[selectTable.selectedIndex].value;
	selectPeriod = document.getElementById('select_period');
	period = selectPeriod.options[selectPeriod.selectedIndex].value;

	//Actually post request
	var httpRequest = SimpleHTTP();
    if (!httpRequest)
    {
        alert('We apologize, but there was a problem creating the new high scores request.');
        return false;
    }
    httpRequest.onreadystatechange = function() { OnUpdate(httpRequest); };
    httpRequest.open('GET', 'high_scores.php?table='+table+'&language=english&time='+time+'&size='+size+'&period='+period+'&min=0&max=50', true);
    httpRequest.send('');
}

//Fill the table with the response when it arrives
function OnUpdate(httpRequest)
{
    if (httpRequest.readyState == 4)
    {
        if (httpRequest.status == 200)
        {
        	//Parse the response and create the HTML formatting
        	html = "<table class='scores' cellspacing=0 cellpadding=0 border=0>";
        	var data = httpRequest.responseText.split('\n');
        	l = data.length - 1;
        	for(var i = 0; i < l; i += 4)
        	{
        		html += "\t\t\t\t\t<tr class=\"";
				if((i/4) % 2 == 0)
					html += "light";
				else
					html += "dark";
				html += "\">\n";
				html += "\t\t\t\t\t\t<td class = \"rank\">"+data[i]+"</td>\n";
				html += "\t\t\t\t\t\t<td class = \"name\">"+data[i+1]+"</td>\n";
				var d = new Date(data[i+2].replace(/^(....).(..).(.{11}).*$/, "$1/$2/$3")); //Converts the SQL date to a format JS can use
				html += "\t\t\t\t\t\t<td class = \"date\">"+d.format("M j, Y");+"</td>\n";
				html += "\t\t\t\t\t\t<td class = \"score\">"+data[i+3]+"</td>\n";
				html += "\t\t\t\t\t</tr>\n";
        	}
        	html += "</table>\n";
            document.getElementById('high_scores').innerHTML = html;
        }
        else
            alert('We apologize, but there was a problem requesting the new high score selection.');
    }
}

//This function just returns a new httpRequest object
function SimpleHTTP()
{
	var httpRequest;
    if (window.XMLHttpRequest)
    {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType)
            httpRequest.overrideMimeType('text/xml');
    } 
    else if (window.ActiveXObject)
    {
        try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } 
        catch (e)
        {
            try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } 
            catch (e) {}
        }
    }
    return httpRequest;
}

// Simulates PHP's date function
// http://jacwright.com/projects/javascript/date_format
Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (replace[curChar])
			returnStr += replace[curChar].call(this);
		else
			returnStr += curChar;
	}
	return returnStr;
};
Date.replaceChars = {
	shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
	shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
	longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
	
	// Day
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.longDays[this.getDay()]; },
	N: function() { return this.getDay() + 1; },
	S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function() { return this.getDay(); },
	z: function() { return "Not Yet Supported"; },
	// Week
	W: function() { return "Not Yet Supported"; },
	// Month
	F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 11 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function() { return this.getMonth() + 1; },
	t: function() { return "Not Yet Supported"; },
	// Year
	L: function() { return "Not Yet Supported"; },
	o: function() { return "Not Supported"; },
	Y: function() { return this.getFullYear(); },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function() { return "Not Yet Supported"; },
	g: function() { return this.getHours() == 0 ? 12 : (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()); },
	G: function() { return this.getHours(); },
	h: function() { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? '0' : '') + (this.getHours() < 10 ? this.getHours() + 1 : this.getHours() - 12); },
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function() { return "Not Yet Supported"; },
	I: function() { return "Not Supported"; },
	O: function() { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; },
	T: function() { return "Not Yet Supported"; },
	Z: function() { return this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function() { return "Not Yet Supported"; },
	r: function() { return this.toString(); },
	U: function() { return this.getTime() / 1000; }
}