// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
 
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}



$(document).ready
        (
        function()
                {
					  // only continue if xmlHttp isn't void
					  if (xmlHttp)
					  {
						// try to connect to the server
						try
						{
				        // Call this when the DOM is ready:
				                $.getJSON("../app/displayHighScores.php",
				                        { cmd : "init" },
				                        make_table);
				        // Call this when a person is selected:
				                $("#people").change(function()
				                        {
				                        var user_id = $(":selected").val();
				                        $.getJSON("../app/displayHighScores.php",
				                                { cmd : "info", id: user_id  },
				                                make_info);
				                        });
				        }
					    // display the error in case of failure
				        catch (e)
				        {
				      	 	alert("Can't connect to server:\n" + e.toString());
				    	}
 
					  }
 	            }
     );
function make_menu(obj)
        {
        var str = "";
        var len = obj.length;
        str += "<option value=''>(select a person)\n";
        for (var i = 0; i < len; i++)
                {
                var user = obj[i];
                str += "<option value='" + user["id"] + "'>" +
                        user["name"] + " " +
                        user["score"] + "\n";
                }
        $("#people").html(str);
        }
		
function make_table(obj)
        {
        var str = "";
		var pos = "";
        var len = obj.length;
        for (var i = 0; i < len; i++)
                {
					var user = obj[i];
					pos = i + 1;
				// You can get each value as info.name or info["name"].
				// Let's get the first name using the first way.
					str += "<tr><td><div align=\"center\">" + pos + "</div></td>";
					str += "<td>" + user["name"] + "</td>";
					str += "<td><div align=\"right\">" + user["score"] + "</div></td>";
					str += "<td><div align=\"right\">" + user["dateuploaded"] + "</div></td>";
					str += "</tr>\n";
                }
        $("#info").html(str);
        }
		
function make_info(info)
        {
        var str = "<tr>";
    // You can get each value as info.name or info["name"].
    // Let's get the first name using the first way.
        str += "<td><div align=\"right\">" + info["id"] + "</div></td>";
        str += "<td>" + info["name"] + "</td>";
        str += "<td><div align=\"right\">" + info["score"] + "</div></td>";
        str += "<td><div align=\"right\">" + info["dateuploaded"] + "</div></td>";
        str += "</tr>\n";
        $("#info").html(str);
        }
