/**
 * @fileoverview create.js
 * 
 * Repository for JavaScript functions used by the screen create.php.
 * 
 * @author Chris 'Chopper' Vincent (24 June 2008)
 */

/***********************************************************************/

/**
 * insertUserFootballTeamHandler which handles the update of a fantasy football
 * user team.
 */
function insertUserFootballTeamHandler() {};

/**
 * Call the service updateUserFootballTeam updates a fantasy football user team.
 * 
 * @param verifyId [String] The verification id that is also stored as a session variable
 */
insertUserFootballTeamHandler.action = function(verifyId)
{
	// Validation
	if ( !validatePlayers( document.getElementById("createTeamForm"), false ) )
	{
		return;
	}
	
	var xmlString = "<Teams><Team>" +
		"<Id />" + 
		"<UserId>" + document.getElementById("userId").value + "</UserId>" +
		"<TeamName>" + document.getElementById("teamName").value + "</TeamName>" +
		"<TeamChanges/>" + 
		"<TeamPoints/>" + 
		"<Goalkeeper>" + document.getElementById("gk").value + "</Goalkeeper>" +
		"<DefenderOne>" + document.getElementById("df1").value + "</DefenderOne>" +
		"<DefenderTwo>" + document.getElementById("df2").value + "</DefenderTwo>" +
		"<DefenderThree>" + document.getElementById("df3").value + "</DefenderThree>" +
		"<DefenderFour>" + document.getElementById("df4").value + "</DefenderFour>" +
		"<MidfielderOne>" + document.getElementById("md1").value + "</MidfielderOne>" +
		"<MidfielderTwo>" + document.getElementById("md2").value + "</MidfielderTwo>" +
		"<MidfielderThree>" + document.getElementById("md3").value + "</MidfielderThree>" +
		"<MidfielderFour>" + document.getElementById("md4").value + "</MidfielderFour>" +
		"<StrikerOne>" + document.getElementById("st1").value + "</StrikerOne>" +
		"<StrikerTwo>" + document.getElementById("st2").value + "</StrikerTwo>" +
		"<Updated/>" +
		"</Team></Teams>";
	
	Parameters.initialise();
	Parameters.addParameter("verifyId", verifyId);
	Parameters.addParameter("xmlString", xmlString );
	Services.callService(null, "addUserFootballTeam", insertUserFootballTeamHandler);
}

/**
 * Handler function (used by many services) which rebuilds the select list
 * of user teams and displays an appropriate information message.
 */
insertUserFootballTeamHandler.onSuccess = function()
{
	var result = Services.getRequestXML();
	if (null != result)
	{
		try
		{
			var teamName = result.getElementsByTagName("TeamName")[0].firstChild.nodeValue;
			document.getElementById("infoDiv").innerHTML = "<p><b>" + teamName + " has been added to the league.</b></p>";
			document.getElementById("createTeamForm").reset();
		}
		catch(err)
		{
			alert(Services.getRequestText());
		}
		Utils.setCursorDefault();
	}
}

/***********************************************************************/

/**
 * randomlyGenerateTeamHandler which handles the random generation of a fantasy football
 * user team.
 */
function randomlyGenerateTeamHandler() {};

/**
 * Call the service randomlySelectFootballTeam which randomly selects a fantasy football team.
 * 
 * @param verifyId [String] The verification id that is also stored as a session variable
 */
randomlyGenerateTeamHandler.action = function(verifyId)
{
	// Call the service
	Parameters.initialise();
	Parameters.addParameter("verifyId", verifyId);
	Services.callService(null, "randomlySelectFootballTeam", randomlyGenerateTeamHandler);
}

/**
 * Handler function that updates the fields on the screen with the randomly
 * selected fantasy football team
 */
randomlyGenerateTeamHandler.onSuccess = function()
{
	var result = Services.getRequestXML();
	if (null != result)
	{
		try
		{
			// Retrieve the team from the XML
			var gk = result.getElementsByTagName("Goalkeeper")[0].firstChild.nodeValue;
			var def1 = result.getElementsByTagName("Defender1")[0].firstChild.nodeValue;
			var def2 = result.getElementsByTagName("Defender2")[0].firstChild.nodeValue;
			var def3 = result.getElementsByTagName("Defender3")[0].firstChild.nodeValue;
			var def4 = result.getElementsByTagName("Defender4")[0].firstChild.nodeValue;
			var mid1 = result.getElementsByTagName("Midfielder1")[0].firstChild.nodeValue;
			var mid2 = result.getElementsByTagName("Midfielder2")[0].firstChild.nodeValue;
			var mid3 = result.getElementsByTagName("Midfielder3")[0].firstChild.nodeValue;
			var mid4 = result.getElementsByTagName("Midfielder4")[0].firstChild.nodeValue;
			var for1 = result.getElementsByTagName("Striker1")[0].firstChild.nodeValue;
			var for2 = result.getElementsByTagName("Striker2")[0].firstChild.nodeValue;
			
			// Set the field values
			document.getElementById("gk").value = gk;
			document.getElementById("df1").value = def1;
			document.getElementById("df2").value = def2;
			document.getElementById("df3").value = def3;
			document.getElementById("df4").value = def4;
			document.getElementById("md1").value = mid1;
			document.getElementById("md2").value = mid2;
			document.getElementById("md3").value = mid3;
			document.getElementById("md4").value = mid4;
			document.getElementById("st1").value = for1;
			document.getElementById("st2").value = for2;
			
			// Set the info bar
			document.getElementById("infoDiv").innerHTML = "<p><b>Team randomly selected.</b></p>";
		}
		catch(err)
		{
			alert(Services.getRequestText());
		}
		Utils.setCursorDefault();
	}
}