// Creates a new object to talk via HTTP to the server
// Note: Works with IE6+, Opera, Firefox, Safari and other non-Microsoft browsers.
var request = new createRequest();

// List of variables.
var i 				= 1;
var buySymbol 		= "";
var firstName		= "";
var lastName		= "";
var firstNameColor	= "";
var lastNameColor	= "";
var firstNameLink	= "";
var lastNameLink	= "";
var playerRow;

/* PURPOSE: To make the players "moveable". */
function addOnClickHandlers()
{	
	for( var i = 1; i <= 30; i++ )
	{
		var row = document.getElementById( "row" + i );
		var rowCells = row.getElementsByTagName( "td" );
		
		for( var j = 2; j <= 9; j++ )
		{
			rowCells[j].onclick = addToBuyList;
		}
	}
}

function displayTransactionView()
{
	// Makes 'Transaction View' visible.
	document.getElementById( "container_div" ).style.display = "block";
	document.getElementById( "container_div" ).style.visibility = "visible";
	document.getElementById( "container_div" ).style.height = "";	
}

function hideTransactionView()
{
	// Hides 'Transaction View' invisible.
	document.getElementById( "container_div" ).style.display = "none";
	document.getElementById( "container_div" ).style.visibility = "hidden";
	document.getElementById( "container_div" ).style.height = "0px";	
}

function hidePortfolioChanged()
{			
	// Makes 'Active Portfolio Changed' message invisible.
	document.getElementById( "updatebox" ).style.display = "none";
	document.getElementById( "updatebox" ).style.visibility = "hidden";
	document.getElementById( "updatebox" ).style.height = "0px";
}

function addToBuyList()
{	
	hidePortfolioChanged();
	displayTransactionView();
		
	playerRow = this.parentNode;
	var rowCells = playerRow.getElementsByTagName( "td" );
	
	// Get the player's first and last name.
	var getPlayer1 = rowCells[0].getElementsByTagName( "a" );
	firstName = getText( getPlayer1[0] );
	var getPlayer2 = rowCells[1].getElementsByTagName( "a" );
	lastName = getText( getPlayer2[0] );
	
	// Get the player's symbol.
	var playerSymbol = getText( rowCells[3] );
	
	// Get the player's current price.
	var currentPrice = getText( rowCells[9] );
	
	var buyPlayer = document.getElementById( "playersToBuy" );
	var purchaseMessage = "Purchasing " + firstName + " " + lastName + " for " + currentPrice + " per share.";
	replaceText( buyPlayer, purchaseMessage );
	
	var confirmPurchase = confirm( "Are you sure you want to buy " + firstName + " " + lastName + "?" );
	
	if( confirmPurchase )
	{
		// Change the player bought's row color to green.
		playerRow.style.backgroundColor = '#8AFB17';
				
		playerRow.onclick = null;
		submitOrder( playerSymbol );
	}
	else
	{
		var cancelMessage = "You decided not to purchase " + firstName + " " + lastName + " for " + currentPrice + " per share.";
		replaceText( buyPlayer, cancelMessage );
	}
}

function updateBuyTable()
{
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			var buyMessage = request.responseText;
			
			var buyPlayer = document.getElementById( "playersToBuy" );
			replaceText( buyPlayer, buyMessage );
			
			// If the player is already owned, reverse the color highlighting back to its original coloring.
			var checkMessage = "You already own the maximum number of shares allowed for " + firstName + " " + lastName + ".";
			var checkMessage2 = "This is an NFL only competition.  You are only allowed to purchase stocks of NFL players.";
			if( buyMessage == checkMessage || buyMessage == checkMessage2 )
			{
				// Change the player's row color to original.
				document.getElementById( buySymbol + "fname" ).style.backgroundColor = firstNameColor;
				document.getElementById( buySymbol + "lname" ).style.backgroundColor = lastNameColor;
				playerRow.style.backgroundColor = lastNameColor;
				
				// Change the unbought players' link color.
				document.getElementById( buySymbol + "fnamelink" ).className = firstNameLink;
				document.getElementById( buySymbol + "lnamelink" ).className = lastNameLink;
			}
			
			// Updates the buy total for the just bought player.
			updateBuyTotal();
		}
	}
}

function updatePlayerBuyTotal()
{
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{		  
			var updateMessage = request.responseText;
			
		  	var getBuyTotal = document.getElementById( buySymbol + "buy" );
			replaceText( getBuyTotal, updateMessage );
			
			// Update the users' current portfolio value.
			updateBalance();
		}
	}
}

function updateBalanceRow()
{
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			var updateMessage = request.responseText;
			
			var balanceRow = document.getElementById( "balanceRow" );
			replaceText( balanceRow, updateMessage );
					
			addOnClickHandlers();
		}
	}
}

function submitOrder( symbol )
{
 	// Sets the global variable for the player wanted to buy.
	buySymbol = symbol;
	
	// Grab the rows' and links' current colors.
	firstNameColor 	= document.getElementById( buySymbol + "fname" ).style.backgroundColor;
	firstNameLink 	= document.getElementById( buySymbol + "fnamelink" ).className;
	lastNameColor 	= document.getElementById( buySymbol + "lname" ).style.backgroundColor;
	lastNameLink 	= document.getElementById( buySymbol + "lnamelink" ).className;
	
	// Change the player bought's row color to green.
	document.getElementById( buySymbol + "fname" ).style.backgroundColor = '#7FE817';
	document.getElementById( buySymbol + "lname" ).style.backgroundColor = '#8AFB17';
	
	// Change the player bought's link color.
	document.getElementById( buySymbol + "fnamelink" ).className = 'blacklink2';
	document.getElementById( buySymbol + "lnamelink" ).className = 'blacklink2';
  
	// Selects the 'player symbol' from the table row.
	var url = "transaction.php?action=1&buymax=1&buy=" + escape( symbol );
	request.open( "GET", url, true );
	request.onreadystatechange = updateBuyTable;
	request.send(null);
}

function updateBuyTotal()
{
	// Call the page to update the users' balance.
	var url = "updatebuytotal.php?buy=" + escape( buySymbol );
	request.open( "GET", url, true );
	request.onreadystatechange = updatePlayerBuyTotal;
	request.send(null);
}

function updateBalance()
{
	// Call the page to update the users' balance.
	var url = "updatebalance.php";
	request.open( "GET", url, true );
	request.onreadystatechange = updateBalanceRow;
	request.send(null);
}