var http_register_user = false;
var http_display_info = false;
var http_order_info = false;
var http_get_winner = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http_register_user = new ActiveXObject("Microsoft.XMLHTTP");
  http_display_info = new ActiveXObject("Microsoft.XMLHTTP");
  http_order_info = new ActiveXObject("Microsoft.XMLHTTP");
  http_get_winner = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http_register_user = new XMLHttpRequest();
  http_display_info = new XMLHttpRequest();
  http_order_info = new XMLHttpRequest();
  http_get_winner = new XMLHttpRequest();
}

function register_user(team_name, team_pass, screen_name, city_letter, email, tournament_id) {
  
  http_register_user.abort();
  if(document.getElementById("league_points"))
  	http_register_user.open("GET", "register.php?league=true&team_name=" + team_name + "&team_pass=" + team_pass + "&screen_name=" + screen_name + "&city_letter=" + city_letter + "&email=" + email + "&id=" + escape(tournament_id), true);
  else
  	http_register_user.open("GET", "register.php?league=false&team_name=" + team_name + "&team_pass=" + team_pass + "&screen_name=" + screen_name + "&city_letter=" + city_letter + "&email=" + email + "&id=" + escape(tournament_id), true);
  http_register_user.onreadystatechange=function() {
    if(http_register_user.readyState == 4) {
		if(http_register_user.responseText == "") {
			var element = document.getElementById("tournament");
			var left = element.offsetLeft;
    		var top  = element.offsetTop;
    		while(element=element.offsetParent)
    		{
        		left += element.offsetLeft;
        		top  += element.offsetTop;
    		}
	
			var x=left+3;
			var y=top+10;
			
			document.getElementById("send_money_popup").style.left=x+"px";
			document.getElementById("send_money_popup").style.top=y+"px";
			document.getElementById('send_money_popup').style.visibility = "visible";
			
			document.getElementById("screen_name").value = "";
			document.getElementById("city_letter").value = "";
			document.getElementById("email").value = "";
			document.getElementById("team_name_i").value = "";
			document.getElementById("team_pass").value = "";
			
			if(team_name == "")
				change_info_order('screen_name');
			else
				change_info_order('team_name');
		}
		else
			alert(http_register_user.responseText);
    }
  }
  http_register_user.send(null);
}


var selected = null;
var originalColor = null;

function makeSelected(id, color) {
	var element;
	if(selected != null) {
		element = document.getElementById(selected);
		element.style.backgroundColor = originalColor;
		element.style.color = "#069";
  	}
  	selected = id;
	if(color == "white")
		originalColor = "white";
	else
		originalColor = "#A9C5E6";
  	element = document.getElementById(selected);
  	element.style.backgroundColor = "#700";
	element.style.color = "white";
	
	document.getElementById("tournament_name").innerHTML = document.getElementById("name"+id).innerHTML;
	document.getElementById("tournament_start").innerHTML = "<b>Starts: </b>" + document.getElementById("date"+id).innerHTML;
	document.getElementById("tournament_status").innerHTML = "<b>Status: </b>" + document.getElementById("status"+id).innerHTML;
	document.getElementById("tournament_players").innerHTML = "<b>Players: </b>" + document.getElementById("players"+id).innerHTML;
	document.getElementById("tournament_team").innerHTML = document.getElementById("team"+id).innerHTML;
	if(document.getElementById("league_points"))
		document.getElementById("tournament_buyin").innerHTML = "<b>League Entry: </b>" + document.getElementById("ll"+id).innerHTML;
	else 
		document.getElementById("tournament_buyin").innerHTML = "<b>Last Longer Entry: </b>" + document.getElementById("ll"+id).innerHTML;
	document.getElementById("tournament_description").innerHTML = document.getElementById("description"+id).innerHTML;	
	document.getElementById("tournament_id").innerHTML = document.getElementById("identifier"+id).innerHTML;
	if(document.getElementById("status"+id).innerHTML == "Finished" || document.getElementById("status"+id).innerHTML == "Completed") {
		http_get_winner.abort();
		if(document.getElementById("league_points"))
			http_get_winner.open("GET", "winner.php?league=true&id=" + document.getElementById("identifier"+id).innerHTML, true);
		else 
			http_get_winner.open("GET", "winner.php?league=false&id=" + document.getElementById("identifier"+id).innerHTML, true);
		http_get_winner.onreadystatechange=function() {
    		if(http_get_winner.readyState == 4) {
				document.getElementById("tournament_winner").innerHTML = "<b style=\"color:red\">Winner: </b>" + http_get_winner.responseText;
				document.getElementById("tournament_prizepool").innerHTML = "<b>Prize Pool: </b>$" + (parseInt(document.getElementById("players"+id).innerHTML) * parseInt(document.getElementById("ll"+id).innerHTML.substr(1)));
    		}
  		}
  		http_get_winner.send(null);		
	}
	else {
		document.getElementById("tournament_winner").innerHTML = "";
		document.getElementById("tournament_prizepool").innerHTML = "";
	}
	http_display_info.abort();
	if(document.getElementById("league_points")) {
  		if(document.getElementById("team"+id).innerHTML == "1")
			http_display_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=team_name&order=ASC&league=true", true);
		else
			http_display_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=screen_name&order=ASC&league=true", true);
	}
	else
		http_display_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=screen_name&order=ASC&league=false", true);
  	http_display_info.onreadystatechange=function() {
		if(http_display_info.readyState == 4) {
			createInfoTable(http_display_info.responseText); 
		}
  	}
  	http_display_info.send(null);
	
	if(document.getElementById("status"+id).innerHTML == "Registering") {
		if(document.getElementById("team"+id).innerHTML == "1") {
			document.getElementById("team_name").style.display = "table-row";
			document.getElementById("team_password").style.display = "table-row";
		}
		else {
			document.getElementById("team_name").style.display = "none";
			document.getElementById("team_password").style.display = "none";
		}
		document.getElementById("registration_pane").style.display = "block";
	}
	else
		document.getElementById("registration_pane").style.display = "none";
}

var screen_name = "ASC";
var position = "ASC";
var points = "ASC";
var status = "ASC";
var team_name = "ASC";

function change_info_order(type) {
	if(document.getElementById("tournament_id").innerHTML != "") {
		http_order_info.abort();
		if(type == "screen_name") {
			if(document.getElementById("league_points"))
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + screen_name + "&league=true", true);
			else
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + screen_name + "&league=false", true);
			if(screen_name == "ASC") screen_name = "DESC";
			else screen_name = "ASC";
		}
		if(type == "position") {
			if(document.getElementById("league_points"))
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + position + "&league=true", true);
			else
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + position + "&league=false", true);
			if(position == "ASC") position = "DESC";
			else position = "ASC";
		}
		if(type == "points" || type == "indpoints" || type == "tmpoints" ) {
			if(document.getElementById("league_points"))
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + points + "&league=true", true);
			else
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + points + "&league=false", true);
			if(points == "ASC") points = "DESC";
			else points = "ASC";
		}
		if(type == "status") {
			if(document.getElementById("league_points"))
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + status + "&league=true", true);
			else
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + status + "&league=false", true);
			if(status == "ASC") status = "DESC";
			else status = "ASC";
		}
		if(type == "team_name") {
			if(document.getElementById("league_points"))
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + team_name + "&league=true", true);
			else
				http_order_info.open("GET", "info_table.php?id=" + escape(document.getElementById("tournament_id").innerHTML) + "&type=" + type + "&order=" + team_name + "&league=false", true);
			if(team_name == "ASC") team_name = "DESC";
			else team_name = "ASC";
		}
		http_order_info.onreadystatechange=function() {
			if(http_order_info.readyState == 4) {
				createInfoTable(http_order_info.responseText); 
			}
		}
		http_order_info.send(null);
	}
}

function hideSendMoney() {
	document.getElementById('send_money_popup').style.visibility = "hidden";
}

function createInfoTable(tableInfo) {
			if(document.getElementById('info_ll_table'))
				document.getElementById('info_ll_table').parentNode.removeChild(document.getElementById('info_ll_table'));
			if(document.getElementById('info_league_table'))
				document.getElementById('info_league_table').parentNode.removeChild(document.getElementById('info_league_table'));
			
			var oTable = document.createElement("TABLE");
				if(document.getElementById("league_points"))
					oTable.setAttribute("id", "info_league_table"); 
				else
					oTable.setAttribute("id", "info_ll_table");
				oTable.setAttribute("border", "0"); 
				oTable.setAttribute("cellpadding", "3"); 
				oTable.setAttribute("cellspacing", "0"); 
				
  			var oTHead = document.createElement("THEAD");
				oTHead.setAttribute("id", "info_header"); 
  			var oTBody = document.createElement("TBODY");
				oTBody.setAttribute("id", "info_content");
			
			oTable.appendChild(oTHead);
  			oTable.appendChild(oTBody);
  			
			oRow = document.createElement("TR");
			
			//create header
		  	oTHead.appendChild(oRow);
			
		//not team
		if(document.getElementById("tournament_team").innerHTML == "0") {
			if(document.getElementById("league_points")) {
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Pos";
			  oCell.setAttribute("onclick", "change_info_order('position')");
			  oCell.setAttribute("class", "info_league_position");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Player";
			  oCell.setAttribute("onclick", "change_info_order('screen_name')");
			  oCell.setAttribute("class", "info_league_player");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Points";
			  oCell.setAttribute("onclick", "change_info_order('points')");
			  oCell.setAttribute("class", "info_league_points");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Status";
			  oCell.setAttribute("onclick", "change_info_order('status')");
			  oCell.setAttribute("class", "info_league_status");
			  oRow.appendChild(oCell);
			}
			else {
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Pos";
			  oCell.setAttribute("onclick", "change_info_order('position')");
			  oCell.setAttribute("class", "ll_position");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Player";
			  oCell.setAttribute("onclick", "change_info_order('screen_name')");
			  oCell.setAttribute("class", "ll_player");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Status";
			  oCell.setAttribute("onclick", "change_info_order('status')");
			  oCell.setAttribute("class", "ll_status");
			  oRow.appendChild(oCell);	
			}
		}
		//team
		else {
			if(document.getElementById("league_points")) {
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Pos";
			  oCell.setAttribute("onclick", "change_info_order('position')");
			  oCell.setAttribute("class", "league_team_position");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Player";
			  oCell.setAttribute("onclick", "change_info_order('screen_name')");
			  oCell.setAttribute("class", "league_team_player");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Team";
			  oCell.setAttribute("onclick", "change_info_order('team_name')");
			  oCell.setAttribute("class", "league_team_teamname");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Ind.Pts";
			  oCell.setAttribute("onclick", "change_info_order('indpoints')");
			  oCell.setAttribute("class", "league_team_indpoints");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Tm.Pts";
			  oCell.setAttribute("onclick", "change_info_order('tmpoints')");
			  oCell.setAttribute("class", "league_team_tmpoints");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Status";
			  oCell.setAttribute("onclick", "change_info_order('status')");
			  oCell.setAttribute("class", "league_team_status");
			  oRow.appendChild(oCell);
			}
			else {
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Pos";
			  oCell.setAttribute("onclick", "change_info_order('position')");
			  oCell.setAttribute("class", "ll_team_position");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Player";
			  oCell.setAttribute("onclick", "change_info_order('screen_name')");
			  oCell.setAttribute("class", "ll_team_player");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Team";
			  oCell.setAttribute("onclick", "change_info_order('team_name')");
			  oCell.setAttribute("class", "ll_team_teamname");
			  oRow.appendChild(oCell);
			  oCell = document.createElement("TH");
			  oCell.innerHTML = "Status";
			  oCell.setAttribute("onclick", "change_info_order('status')");
			  oCell.setAttribute("class", "ll_team_status");
			  oRow.appendChild(oCell);	
			}
		}
		
			//get php results and delimit
			var results = tableInfo.split(";"); 
  			
			// Insert rows and cells into bodies.
			var index = 0;
			var whiteRow = true;
			var rowIndex = 0;
		
		//not team
  		if(document.getElementById("tournament_team").innerHTML == "0") {
			if(document.getElementById("league_points")) {
			  for (i=0; i<results.length/4; i++) {
				  var oBody = oTBody;
				  oRow = document.createElement("TR");
				  rowIndex++;
				  if(whiteRow) {
				  	whiteRow = false;
					oRow.setAttribute("class", "info_white_row");
				  }
				  else {
					whiteRow = true;
					oRow.setAttribute("class", "info_blue_row");
				  }
				  oBody.appendChild(oRow);
				  for (j=1; j<=4; j++) {
					  oCell = document.createElement("TD");
					  if(j==1)
						  oCell.setAttribute("class", "info_league_position");
					  else if(j==2)
						  oCell.setAttribute("class", "info_league_player");
					  else if(j==3)
						  oCell.setAttribute("class", "info_league_points");
					  else
						  oCell.setAttribute("class", "info_league_status");
					  if(results[index] == null)
						  oCell.innerHTML = "";
					  else	
						  oCell.innerHTML = results[index];
					  oRow.appendChild(oCell);
					  index++;
				  }
			  }
			}
			else {
				for (i=0; i<results.length/3; i++) {
				  var oBody = oTBody;
				  oRow = document.createElement("TR");
				  rowIndex++;
				  if(whiteRow) {
				  	whiteRow = false;
					oRow.setAttribute("class", "info_white_row");
				  }
				  else {
					whiteRow = true;
					oRow.setAttribute("class", "info_blue_row"); 
				  }
				  oBody.appendChild(oRow);
				  for (j=1; j<=3; j++) {
					  oCell = document.createElement("TD");
					  if(j==1)
						  oCell.setAttribute("class", "ll_position");
					  else if(j==2)
						  oCell.setAttribute("class", "ll_player");
					  else
						  oCell.setAttribute("class", "ll_status");
					  if(results[index] == null)
						  oCell.innerHTML = "";
					  else	
						  oCell.innerHTML = results[index];
					  oRow.appendChild(oCell);
					  index++;
				  }
			  }
			}
		}
		//team
		else {
			if(document.getElementById("league_points")) {
			  for (i=0; i<results.length/6; i++) {
				  var oBody = oTBody;
				  oRow = document.createElement("TR");
				  rowIndex++;
				  if(whiteRow) {
				  	whiteRow = false;
					oRow.setAttribute("class", "info_white_row");
				  }
				  else {
					whiteRow = true;
					oRow.setAttribute("class", "info_blue_row");
				  }
				  oBody.appendChild(oRow);
				  for (j=1; j<=6; j++) {
					  oCell = document.createElement("TD");
					  if(j==1)
						  oCell.setAttribute("class", "league_team_position");
					  else if(j==2)
						  oCell.setAttribute("class", "league_team_player");
					  else if(j==3)
						  oCell.setAttribute("class", "league_team_teamname");
					  else if(j==4)
						  oCell.setAttribute("class", "league_team_indpoints");
					  else if(j==5)
						  oCell.setAttribute("class", "league_team_tmpoints");
					  else
						  oCell.setAttribute("class", "league_team_status");
					  if(results[index] == null)
						  oCell.innerHTML = "";
					  else	
						  oCell.innerHTML = results[index];
					  oRow.appendChild(oCell);
					  index++;
				  }
			  }
			}
			else {
				for (i=0; i<results.length/4; i++) {
				  var oBody = oTBody;
				  oRow = document.createElement("TR");
				  rowIndex++;
				  if(whiteRow) {
				  	whiteRow = false;
					oRow.setAttribute("class", "info_white_row");
				  }
				  else {
					whiteRow = true;
					oRow.setAttribute("class", "info_blue_row"); 
				  }
				  oBody.appendChild(oRow);
				  for (j=1; j<=4; j++) {
					  oCell = document.createElement("TD");
					  if(j==1)
						  oCell.setAttribute("class", "ll_team_position");
					  else if(j==2)
						  oCell.setAttribute("class", "ll_team_player");
					  else if(j==3)
						  oCell.setAttribute("class", "ll_team_teamname");
					  else
						  oCell.setAttribute("class", "ll_team_status");
					  if(results[index] == null)
						  oCell.innerHTML = "";
					  else	
						  oCell.innerHTML = results[index];
					  oRow.appendChild(oCell);
					  index++;
				  }
			  }
			}
		}
			if(rowIndex >= 9 && navigator.appName == "Microsoft Internet Explorer") {
				document.getElementById("info_table_container").style.borderBottom = "solid black 1px";	
			}
			else {
				document.getElementById("info_table_container").style.borderBottom = "none";
			}
			
			while(rowIndex < 6) {
				oRow = document.createElement("TR");
				oBody.appendChild(oRow);
				oCell = document.createElement("TD");
				oRow.appendChild(oCell);
				oCell = document.createElement("TD");
				oRow.appendChild(oCell);
				oCell = document.createElement("TD");
				oRow.appendChild(oCell);
				if(document.getElementById("league_points")) {
					oCell = document.createElement("TD");
					oRow.appendChild(oCell);
				}
				if(document.getElementById("tournament_team").innerHTML == "1") {
					oCell = document.createElement("TD");
					oRow.appendChild(oCell);
					oCell = document.createElement("TD");
					oRow.appendChild(oCell);
				}
				rowIndex++;
			}
			
			document.getElementById('info_table_container').appendChild(oTable);
}