// Creates a new object to talk via HTTP to the server
// Note: Works with IE6+, Opera, Firefox, Safari and other non-Microsoft browsers.
var changeportfolio = new createRequest();
var updatemarket	= new createRequest();
var url				= null;
var portfolioName 	= null;
var playerrow		= null;
var playerNum		= 0;
var playerSymbol	= 0;

function openWaitScreen()
{
	// Makes 'greyDiv' visible.
	document.getElementById( "greyDiv" ).style.display 		= "block";
	document.getElementById( "greyDiv" ).style.visibility 	= "visible";
	
	// Makes 'waitDiv' visible.
	document.getElementById( "waitDiv" ).style.display 		= "block";
	document.getElementById( "waitDiv" ).style.visibility 	= "visible";
	document.getElementById( "waitDiv" ).style.height 		= "";
}

function closeWaitScreen()
{	
	// Makes 'waitDiv' invisible.
	document.getElementById( "waitDiv" ).style.display 		= "none";
	document.getElementById( "waitDiv" ).style.visibility 	= "hidden";
	document.getElementById( "waitDiv" ).style.height 		= "0px";
	
	// Makes 'greyDiv' invisible.
	document.getElementById( "greyDiv" ).style.display 		= "none";
	document.getElementById( "greyDiv" ).style.visibility 	= "hidden";
}

function updateMarket()
{
	for( var playerNum = 1; playerNum <= 30; playerNum++ )
	{
		playerRow 			= document.getElementById( "row" + playerNum );
	    var rowCells 		= playerRow.getElementsByTagName( "td" );	    
		playerSymbol 		= getText( rowCells[3] );
		
		// Updates the market page.
		updateMarketListings();
	}
}

function updatePort()
{
	if( changeportfolio.readyState == 4 )
	{
		if( changeportfolio.status == 200 )
		{
			var message = changeportfolio.responseText;
			var portfolio = document.getElementById( "currentPortfolio" );
			replaceText( portfolio, message );
			
			if( message == null )
				alert( "You did not change your current portfolio, contact the site admin immediately." );			

			// Makes Message Visible
			document.getElementById( "updatebox" ).style.width = "34%";
			document.getElementById( "updatebox" ).style.height = "";
			document.getElementById( "updatebox" ).style.display = "block";
			document.getElementById( "updatebox" ).style.visibility = "visible";
			document.getElementById( "updatebox" ).style.marginLeft = "33%";
			document.getElementById( "updatebox" ).style.color = '#000';	
			
			if( message == "Total Value" )
			{
				document.getElementById( "updatebox" ).style.backgroundColor = '#DDD';
				document.getElementById( "updatebox" ).style.borderColor = '#999';
			}
			else if( message == "Monthly" )
			{
				document.getElementById( "updatebox" ).style.backgroundColor = '#FF9F9F';
				document.getElementById( "updatebox" ).style.borderColor = '#8A0000';
			}
			else if( message == "Quarterly" )
			{
				document.getElementById( "updatebox" ).style.backgroundColor = '#FFFFB7';
				document.getElementById( "updatebox" ).style.borderColor = '#FBFB00';
			}
			else
			{
				document.getElementById( "updatebox" ).style.backgroundColor = '#B7B7FF';
				document.getElementById( "updatebox" ).style.borderColor = '#000095';
			}
			
			var updateText = document.getElementById( "updatebox" );
			var message = "Active portfolio changed!";
			replaceText( updateText, message );
			
			// Update the users' current portfolio value.
			updateBalance();
			
			// Update the market listings with proper coloring.
			updateMarket();
			
			// Closes the 'waitDiv' pop-up div.
			closeWaitScreen();
		}
	}
}

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

function updateMarketVisual()
{
	if( updatemarket.readyState == 4 )
	{
		if( updatemarket.status == 200 )
		{
			var portNumber = updatemarket.responseText;			
			
			if( portNumber == 0 )
			{
				// Change the player bought's row color to grey.
				document.getElementById( playerSymbol + "fname" ).style.backgroundColor='#CCC';
				document.getElementById( playerSymbol + "lname" ).style.backgroundColor='#DDD';
				playerRow.style.backgroundColor='#DDD';
				
				// Change the bought players' link color.
				document.getElementById( playerSymbol + "fnamelink" ).className='blacklink2';
				document.getElementById( playerSymbol + "lnamelink" ).className='blacklink2';							
			}
			else if( portNumber == 1 )
			{
				// Change the player bought's row color to red.
				document.getElementById( playerSymbol + "fname" ).style.backgroundColor='#FF6464';
				document.getElementById( playerSymbol + "lname" ).style.backgroundColor='#FFA8A8';
				playerRow.style.backgroundColor='#FFA8A8';
				
				// Change the bought players' link color.
				document.getElementById( playerSymbol + "fnamelink" ).className='blacklink2';
				document.getElementById( playerSymbol + "lnamelink" ).className='blacklink2';
			}
			else if( portNumber == 2 )
			{
				// Change the player bought's row color to yellow.
				document.getElementById( playerSymbol + "fname" ).style.backgroundColor='#FFFF00';
				document.getElementById( playerSymbol + "lname" ).style.backgroundColor='#FFFFB9';
				playerRow.style.backgroundColor='#FFFFB9';
				
				// Change the bought players' link color.
				document.getElementById( playerSymbol + "fnamelink" ).className='blacklink2';
				document.getElementById( playerSymbol + "lnamelink" ).className='blacklink2';
			}
			else if( portNumber == 3 )
			{
				// Change the player bought's row color to blue.
				document.getElementById( playerSymbol + "fname" ).style.backgroundColor='#C5C8E0';
				document.getElementById( playerSymbol + "lname" ).style.backgroundColor='#DFE0EE';
				playerRow.style.backgroundColor='#DFE0EE';
				
				// Change the bought players' link color.
				document.getElementById( playerSymbol + "fnamelink" ).className='blacklink2';
				document.getElementById( playerSymbol + "lnamelink" ).className='blacklink2';
			}
			else if( portNumber == 4 )
			{
				// Change the player bought's row color to blue.
				document.getElementById( playerSymbol + "fname" ).style.backgroundColor='#C5C8E0';
				document.getElementById( playerSymbol + "lname" ).style.backgroundColor='#DFE0EE';
				playerRow.style.backgroundColor='#DFE0EE';
				
				// Change the bought players' link color.
				document.getElementById( playerSymbol + "fnamelink" ).className='blacklink2';
				document.getElementById( playerSymbol + "lnamelink" ).className='blacklink2';
			}
			else
			{
				// Change the player bought's row color to original.
				document.getElementById( playerSymbol + "fname" ).style.backgroundColor='#DEECFF';
				document.getElementById( playerSymbol + "lname" ).style.backgroundColor='#EDF4FD';
				playerRow.style.backgroundColor='';
				
				// Change the unbought players' link color.
				document.getElementById( playerSymbol + "fnamelink" ).className='JockLink1';
				document.getElementById( playerSymbol + "lnamelink" ).className='JockLink1';
			}
		}
	}	
}

function changePort( portName )
{
	// Hides 'Transaction View'.
	hideTransactionView();
	
	// Opens the 'waitDiv' pop-up div.
	openWaitScreen();
	
	portfolioName = portName;
	
	// Changes the 'active port' image in the lower-right of the template.
	if( portName == "total" )		
		var imageLink = "/images/template/1/" + portName + "value.gif";
	else
		var imageLink = "/images/template/1/" + portName + ".gif";		
	document.getElementById( "portImage" ).src=imageLink;
	
	// Changes the users current portfolio.
	var url = "changeactiveport.php?portfolio=" + escape( portName ) + "&version=2";
	changeportfolio.open( "GET", url, true );
	changeportfolio.onreadystatechange = updatePort;
	changeportfolio.send(null);
}

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

function updateMarketListings()
{
	// Checks to see if the user owns this current player.
	url = "updatemarket.php?symbol=" + escape( playerSymbol );
	updatemarket.open( "GET", url, false );
	updatemarket.send(null);
	updateMarketVisual();
}