/**
 * @author Dan Man
 */

function acceptChallenge(battleid)
{
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url = "synchronize/mybattles_server.php";
	url = url + "?step=acceptChallenge";
	url = url + "&battleid="+battleid;
	url = url + "&sid=" + Math.random();
		
	xmlHttp.onreadystatechange = handleAcceptChallenge;
	xmlHttp.open("GET", url, true);
	xmlHttp.send('');
}

function handleAcceptChallenge()
{
	if (xmlHttp.readyState == 4)
	{
		response = xmlHttp.responseText.split(",");
		challenger = response[0];
		bid = response[1];
		
		if(confirm("You have accepted the challenge from " + challenger + ". Your opponent has been notified.\n\nDo you want to fill out your submission now?\nNote: You can do this at a later time."))
			window.location = "createSubmission.php?bid=" + bid;
		else
			window.location.reload();
	}
}

function declineChallenge(battleid)
{
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	var url = "synchronize/mybattles_server.php";
	url = url + "?step=declineChallenge";
	url = url + "&battleid="+battleid;
	url = url + "&sid=" + Math.random();
		
	xmlHttp.onreadystatechange = handleDeclineChallenge;
	xmlHttp.open("GET", url, true);
	xmlHttp.send('');
}

function handleDeclineChallenge()
{
	if (xmlHttp.readyState == 4)
	{
		challenger = xmlHttp.responseText;
		
		alert("You have declined the challenge from " + challenger + ". Your opponent has been notified.");
		window.location.reload();
	}
}

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;
}
