	function number_format (nStr,prefix) { 
		
		var prefix = prefix || '';   
		
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : ''; 
		
		var rgx = /(\d+)(\d{3})/; 
		
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
		} // end while
		
		return prefix + x1 + x2;
		
	} // end number_format


	$(document).ready(function(){
		timestamp = 0;
		counter = 0;
		delay = 4000;
		string = 0;
		text = 0;
		accept_credits = 0;
		updateMsg();
	});

	function addMessages(xml) {

		var sPath = window.location.pathname;
		//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

		if($("status",xml).text() == "2") {
			return;
		} // end if

		timestamp = $("time",xml).text();

		text = "<table cellspacing=0 cellpadding=4 border=0 width=100%>";
		text += "<tr><td bgcolor=#333333>";

			if ($("track",xml).text() == 0) {
				string = "street race";
				accept_credits = 10;
			} // end if
			else {
				string = "grudge match at the track";
				accept_credits = 5;
			} // end if

			if (sPage != 'chat.php') {
				text += "<b><a href=\"stats.php?opponentID=" + $("accountID",xml).text() + "\">";
			} // end if
			else {
				text += "<b><a href=\"stats.php?opponentID=" + $("accountID",xml).text() + "\" target=new>";
			} // end else
			
			text += $("name1",xml).text() + "</a></b> has challenged you to a " + string + "!<br><br>Vehicle: " + $("vehicle1",xml).text() + "<br>Networth: $" + number_format($("networth1",xml).text()) + "<br><br>";
			
			if ($("bet",xml).text() != 0) {
				text += "<font color=#00CC00><b>$" + $("bet",xml).text() + "</b></font> has been placed on the outcome of the race.<br><br>";
			} // end if

			if ($("pink_slip",xml).text() == 1) {
				text += "<font color=#FF0000><b><u>This is a race for pink slips!</u></b></font><br><br>";
			} // end if

			text += "If you do not respond, you will lose 3 RAD.<br><br>[<a href=\"proc_racepopup.php?id=" + $("raceID",xml).text() + "&accept=1\">Accept</a>] (+1 RAD)<br>";
				
			if (sPage != 'chat.php')	{
				text += "[<a href=\"proc_racepopup.php?id=" + $("raceID",xml).text() + "&accept=0\">Decline</a>]";
			} // end if
			else {
				text += "[<a href=\"proc_racepopup.php?id=" + $("raceID",xml).text() + "&accept=0\" target=new>Decline</a>]";
			} // end else

			if (($("pink_slip",xml).text() == 0) & ($("bet",xml).text() > 0)) {
				 text += "(-3 RAD)";
			} // end if

			text += "<br><br>";
			
			
		text += "</td></tr>";
		text += "</table>";
		

		$("message",xml).each(function(id) {
			message = $("message",xml).get(id);
			$("#messagewindow").append(text + "<br>").fadeIn('slow');
		});


	} // end addMessages


	function updateMsg() {

		if (timestamp == 0) {

			$.get("./xml/xmlRaces.php", { time: timestamp }, function(xml) {
				addMessages(xml);
			});

			counter += 1;
			
			if (counter == 10) {

				delay = delay * 2;

				if (delay >= 60000) {
					delay = 60000;
				} // end if

				counter = 0;

			} // end if
			
			setTimeout('updateMsg()', delay);

		} // end if

	} // end updateMsg