// start tmnt_swapplayer js
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="ajax_competitions.php"
url=url+"?do=getuserid"
url=url+"&username="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.vbform.userid.value=xmlHttp.responseText 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
// end tmnt_swapplayer js

// start tmnt_addplayer js
var xmlHttp;

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.vbform.userid.value=xmlHttp.responseText;
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
function showUser(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp===null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax_competitions.php";
url=url+"?do=getuserid";
url=url+"&username="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
// end tmnt_addplayer js

// start tmnt_display js
function savescore(id, tid)
{ 
 var score=document.getElementById("score_"+id).value; 
 score=parseInt(score);
 if (score=="")
 {
 score="0";
 }
 document.getElementById("cmpt_"+id).innerHTML=score; 
 document.getElementById("cmpt_"+id).style.color = 'red';
 updatescore(id, score, tid);
}
var xmlHttp;
function updatescore(id, score, tid)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp===null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax_competitions.php";
url=url+"?do=updatescore";
url=url+"&id="+id;
url=url+"&score="+score;
url=url+"&tid="+tid;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged; 
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
var xmlHttp;

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp===null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }
var url="ajax_competitions.php";
url=url+"?do=getuserid";
url=url+"&username="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.vbform.userid.value=xmlHttp.responseText; 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
// end tmnt_display js
function deleteladder(id) 
{
	if (confirm("Are you sure you want to delete this ladder?")) {
		window.location.href = 'ladders.php?do=delete&id=' + id;
	}
}
function toggle_visibility(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
   {
	  e.style.display = 'none';
	  }
   else
   {
	  e.style.display = 'block';
	  }
}
function joins(A)
{
	return openWindow("competitions.php?"+SESSIONURL+"do=viewteamjoins&id="+A,230,300);
}
function wins(A)
{
	return openWindow("competitions.php?"+SESSIONURL+"do=viewteamwins&id="+A,230,300);
}
function deletetmnt(id, title) 
{
	if (confirm("Are you sure you want to delete '" + title + "'")) {
		window.location.href = 'tournaments.php?do=kill&id=' + id;
	}
}
function deleteteam(id) 
{
	if (confirm("Are you sure you want to delete this team?")) {
		window.location.href = 'competitions.php?do=killteam&id=' + id;
	}
}
function deleteann(id) 
{
	if (confirm("Are you sure you want to delete this announcement?")) {
		window.location.href = 'competitions.php?do=killann&id=' + id;
	}
}
function deletecat(id) 
{
	if (confirm("Are you sure you want to delete this category?")) {
		window.location.href = 'competitions.php?do=killcategory&catid=' + id;
	}
}
function jointeam(id) 
{
	if (confirm("Are you sure you want to join this team?")) {
		window.location.href = 'competitions.php?do=reqjointeam&id=' + id;
	}
}
function rejectrecruit(id) 
{
	if (confirm("Are you sure you want to reject this recruit?")) {
		window.location.href = 'competitions.php?do=rejectrecruit&id=' + id;
	}
}
function confirmrecruit(id) 
{
	if (confirm("Are you sure you want to confirm this recruit?")) {
		window.location.href = 'competitions.php?do=confirmrecruit&id=' + id;
	}
}
function deleteplayer(id, tid, userid) 
{
	if (confirm("Are you sure you remove this player from this tournament?")) {
		window.location.href = 'tournaments.php?do=killplayer&id=' + id + '&tid=' + tid + '&userid=' + userid;
	}
}
function deleteteamplayer(id) 
{
	if (confirm("Are you sure you want to remove this player from your team?")) {
		window.location.href = 'competitions.php?do=killteamplayer&id=' + id;
	}
}
function makeleader(id) 
{
	if (confirm("Are you sure you want to make this team member a leader?")) {
		window.location.href = 'competitions.php?do=makeleader&id=' + id;
	}
}
function leaveteam(id) 
{
	if (confirm("Are you sure you want to leave this team?")) {
		window.location.href = 'competitions.php?do=leaveteam&teamid=' + id;
	}
}
function makecoleader(id) 
{
	if (confirm("Are you sure you want to make this team player a co-leader?")) {
		window.location.href = 'competitions.php?do=makecoleader&id=' + id;
	}
}
function deletecomment(id, tid, userid) 
{
	if (confirm("Are you sure you want to delete this comment?")) {
		window.location.href = 'tournaments.php?do=killcomment&id=' + id + '&tid=' + tid +'&userid=' + userid;
	}
}
function finishtmnt(id, title, topscore) 
{
	if (confirm("Are you sure you want to finish '" + title + "'")) {
		window.location.href = 'tournaments.php?do=finish&id=' + id + '&topscore=' + topscore;
	}
}
function progress(id, userid, username, round, slot) 
{
	if (confirm("Are you sure you want to progress '" + username + "' to next round?")) {
		window.location.href = 'tournaments.php?do=progress&id=' + id + '&userid=' + userid + '&round=' + round + '&slot=' + slot;
	}
}
function regress(id, userid, username, round) 
{
	if (confirm("Are you sure you want to regress '" + username + "' to previous round?")) {
		window.location.href = 'tournaments.php?do=progress&id=' + id + '&userid=' + userid + '&round=' + round + '&slot=0';
	}
}
function progressfinish(id, userid, username, round, slot) 
{
	if (confirm("Are you sure you want to progress '" + username + "' to next round?")) {
		window.location.href = 'tournaments.php?do=progressfinish&id=' + id + '&userid=' + userid + '&round=' + round + '&slot=' + slot;
	}
}
function showhide(value, divid)
{
	if (value=="1") {
		document.getElementById(divid).style.display="";
	}
	if (value=="0")
	{
		document.getElementById(divid).style.display="none";
	}
}

function editscore(id, tid)
{ 
 var score=document.getElementById("cmpt_"+id).innerHTML;
 score=parseInt(score);
if (score=="")
 {
 score="0";
 }
 document.getElementById("cmpt_"+id).innerHTML='<input type="text" value="'+score+'" id="score_'+id+'" onblur="savescore('+id+', '+tid+')" />';
}

function getelo()
{
	var rank_u = document.getElementById('rank_u').value;
	rank_u = parseInt(rank_u);
	var k = document.getElementById('kvalue').value;
	k = parseInt(k);
	var rank_ub = document.getElementById('rank_ub').value;
	rank_ub = parseInt(rank_ub);
	var score_u = document.getElementById('score_u').value;
	score_u = parseInt(score_u);
	var score_ub = document.getElementById('score_ub').value;
	score_ub = parseInt(score_ub);
	var dr = Math.abs(rank_u - rank_ub);
	var p_f = 1 - 1/(10^(dr*Math.sqrt(k)/2000)+1);
	p_f = parseInt(p_f);
	if (score_u > score_ub) {
		 if (rank_u > rank_ub) {
			var newrank_u = rank_u + 4*Math.sqrt(k)*(1-p_f);
			var newrank_ub = rank_ub - 4*Math.sqrt(k)*(1-p_f);
		 } else if (rank_u == rank_ub) {
		 	var newrank_u = rank_u + 4*Math.sqrt(k)*(1-p_f);
			var newrank_ub = rank_ub - 4*Math.sqrt(k)*(1-p_f);
		 } else {
		 	var newrank_u = rank_u + 4*Math.sqrt(k)*p_f;
			var newrank_ub = rank_ub - 4*Math.sqrt(k)*p_f;
		 }
	} else if (score_u == score_ub) {
		if (rank_u > rank_ub) {//higher rank u drawing
		 	var newrank_u = rank_u - 4*Math.sqrt(k)*(1-p_f);
			var newrank_ub = rank_ub + 4*Math.sqrt(k)*(1-p_f);
		 } else if (rank_u == rank_ub) {//same rank drawing
		 	var newrank_u = rank_u;
			var newrank_ub = rank_ub;
		 } else {//lower rank u drawing
		 	var newrank_u = rank_u + 4*Math.sqrt(k)*p_f;
			var newrank_ub = rank_ub - 4*Math.sqrt(k)*p_f;
		 }
	} else if (score_u < score_ub) {
		if (rank_u > rank_ub) {//higher rank u losing
			var newrank_u = rank_u - 4*Math.sqrt(k)*p_f;
			var newrank_ub = rank_ub + 4*Math.sqrt(k)*p_f;
		 } else if (rank_u == rank_ub) {//same rank losing
		 	var newrank_u = rank_u - 4*Math.sqrt(k)*(1-p_f);
			var newrank_ub = rank_ub + 4*Math.sqrt(k)*(1-p_f);
		 } else {//lower rank u losing
			var newrank_u = rank_u - 4*Math.sqrt(k)*(1-p_f);
			var newrank_ub = rank_ub + 4*Math.sqrt(k)*(1-p_f);
		 }
	}
	document.getElementById('newrank_u').value = newrank_u;
	document.getElementById('newrank_ub').value = newrank_ub;
}