

function show_stats(type)
{
	var base_url = "stats_xml.php";
	var url = "";
	
	// check for ajax...
	if (!window.XMLHttpRequest && !window.ActiveXObject)
	{
		// ajax doesn't seem to exist - do it the old way
		document.forms['stats'].submit();		
		return;
	}
	
	if (type != "")
	{
		url = base_url + "?elem_type=" + document.forms[1].elem_type.options[type].value;
	}
	else
	{
		url = base_url;
	}
	//alert(url);
	
	// clear the table first...
	clear_table();
	// and show a message
	message = document.createElement("p");
	message_text = document.createTextNode("Loading stats...");
	message.appendChild(message_text);
	document.getElementById("stats_table").appendChild(message);

	// we need a backup in case something goes wrong
	backup_url = location.protocol + "//" + location.hostname + location.pathname + "?type=2006&elem_type=" +
					document.forms[1].elem_type.options[type].value;
	
	make_request(url, onreadystatechange_stats, backup_url);
	
	return false;
}


function onreadystatechange_stats()
{
	/* If XMLHR object has finished retrieving the data */
	if (http_request.readyState == 4)
	{
		/* If the data was retrieved successfully */
		if (http_request.status == 200)
		{
			write_stats();
		}
		/* IE returns a status code of 0 on some occasions, so ignore this case */
		else if (http_request.status != 0)
		{
			alert("There was an error while retrieving the URL: " + http_request.statusText);
		}
	}

	return true;
}


function clear_table()
{
	stats_table_div = document.getElementById("stats_table");
	
	all_children = stats_table_div.childNodes;

	while (all_children.length > 0)
	{
		stats_table_div.removeChild(all_children[0]);
	}	
}


function write_stats()
{
	stats_table_div = document.getElementById("stats_table");
		
	// get our new xml
	var xml_response = http_request.responseXML;
	
	// check the status
	var response_status = xml_response.getElementsByTagName("status")[0].childNodes[0].nodeValue;

	if (response_status == 1)
	{
		// if there is no error with the new aml, clear out all existing child nodes
		clear_table();
		
		// now rebuild it with the new xml
		
		// first thing is the "title"
		stats_type = xml_response.getElementsByTagName("type")[0].childNodes[0].nodeValue;
		stats_title = document.createElement("h3");
		
		switch (stats_type)
		{
			case "E":
				stats_title_text = document.createTextNode("Engines");
				break;
			case "C":
				stats_title_text = document.createTextNode("Chassis");
				break;
			case "T":
				stats_title_text = document.createTextNode("Tyres");
				break;
			case "D":
			default:
				stats_title_text = document.createTextNode("Drivers");
		}
		
		stats_title.appendChild(stats_title_text);
		stats_table_div.appendChild(stats_title);
		
		// now do the table
		table_elem = document.createElement("table");
		table_elem.setAttribute("align", "center");
		
		// and don't forget the tbody...
		tbody = document.createElement("tbody");
		
		// create the header row
		table_row = document.createElement("tr");
		create_th_element("Name", table_row);
		create_th_element("Cost", table_row);
		create_th_element("Race pts", table_row);
		create_th_element("Total pts", table_row);
		create_th_element("Pts/£m", table_row);
		create_th_element("% Team", table_row);
		
		// then assign the row to the table
		tbody.appendChild(table_row);
		
		// now go through all the elements in the xml and put them on the page
		elem = xml_response.getElementsByTagName("element");
		
		for (i = 0; i < elem.length; i++)
		{
			// create a td element for each item to display
			table_row = document.createElement("tr");
			td_name = document.createElement("td");
			td_cost = document.createElement("td");
			td_race_pts = document.createElement("td");
			td_total_pts = document.createElement("td");
			td_pts_per = document.createElement("td");
			td_percent = document.createElement("td");
			
			elem_data = elem[i].childNodes;
			
			for (j = 0; j < elem_data.length; j++)
			{
				// ignore any non-tag nodes (ie white space)
				if (elem_data[j].nodeType != 1)
				{
					continue;
				}
				
				// get the value for the element 
				td_text = document.createTextNode(elem_data[j].childNodes[0].nodeValue);
				
				// based on the tagname, put the text in the right place
				switch (elem_data[j].tagName.toLowerCase())
				{
					case "elem_name":
						td_name.appendChild(td_text);
						break;
					case "elem_value":
						td_text = document.createTextNode("£" + elem_data[j].childNodes[0].nodeValue);
						td_cost.appendChild(td_text);
						break;
					case "race_points":
						td_race_pts.appendChild(td_text);
						break;
					case "total_points":
						td_total_pts.appendChild(td_text);
						break;
					case "points_per":
						td_pts_per.appendChild(td_text);
						break;
					case "percent_teams":
						td_percent.appendChild(td_text);
						break;
				}
				
				// assign all the td elements to the row
				table_row.appendChild(td_name);
				table_row.appendChild(td_cost);
				table_row.appendChild(td_race_pts);
				table_row.appendChild(td_total_pts);
				table_row.appendChild(td_pts_per);
				table_row.appendChild(td_percent);
			}
			
			// put the row in the tbody
			tbody.appendChild(table_row);
		}
		
		// put the tbody in the table
		table_elem.appendChild(tbody);
		
		// and finally, display the table on the page
		stats_table_div.appendChild(table_elem);
	}
	else
	{
		alert ("There was an error with the stats XML");
	}
}

function create_th_element(value, tr)
{
	var th_tmp = document.createElement("th");
	var th_text = document.createTextNode(value);
	th_tmp.appendChild(th_text);
	tr.appendChild(th_tmp);
}