/******************/
/* AJAX Functions */
/******************/

function createRequest()
{	// Create an HTTP request
	var request;

	try
	{	// Try the standard way
		request = new XMLHttpRequest();
		
	} catch( trymicrosoft )
	{	// Standard way is a no go!
		try
		{	// Try Microsoft variant 1
			request = new ActiveXObject( "Msxm12.XMLHTTP" );
			
		} catch( othermicrosoft )
		{	// Still failure!
			try
			{	// Try Microsoft variant 2
				request = new ActiveXObject( "Microsoft.XMLHTTP" );
				
			} catch( failed )
			{	// Well we're just incompetent now, aren't we
				request = null;
				alert( "Error creating request object!" );
				
			} // End try/catch
			
		} // End try/catch
		
	} // End try/catch
	
	return request;
	
} // End createRequest()


function sendRequest( filename, params, handler, method )
{	// Send an XMLHttpRequest
	var request = createRequest();
	
	if( filename.substring( filename.length - 4 ) != ".php" )
	{	// Append .php on the end of filenames that are supplied without it
		filename += ".php";
	} // End if

	if( method.toLowerCase() == "get" )
	{	// If using the GET method
		request.open( "GET", "ajax/" + filename + "?" + params, true );
		request.onreadystatechange = function() { handleRequest( request, handler ); };
		request.send( null );
		
	} else if( method.toLowerCase() == "post" )
	{	// If using the POST method
		request.open( "POST", "ajax/" + filename, true );
		request.onreadystatechange = function() { handleRequest( request, handler ); };
		request.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		request.send( params );
		
	} // End if
	
} // End sendRequest()


function handleRequest( request, handler )
{	// Call the appropriate handler function for XMLHttpRequests
	if( request.readyState == 4 )
	{	// On a completed request
		if( request.status == 200 )
		{	// If the completed request was successful
			if( handler )
			{	// If a handler function was specified
				eval( handler + "( request.responseText )" );
			} // End if
			
		} else
		{	// If the request completed, but the file couldn't be accessed
			//alert( "Business logic not accessible" );
			
		} // End if

	} // End if
	
} // End handleRequest()


/********************************/
/* General Javascript Functions */
/********************************/

function trim( value )
{
	return value.replace( /^\s+|\s+$/g, "" );	
}

function expand_release(expand_id)
{	// Expand selected game release, and collapse all others
	var docDivs = document.getElementsByTagName("div");
	
	for(var i=0; i<docDivs.length; i++)
	{	// Step through all div tags
		var divID = docDivs[i].id;

		if(divID.substring(0,6) == "closed")
		{	// If modifying a closed div
			if(divID.substring(7) == expand_id)
			{	// If modifying the closed div for the selected game release
				docDivs[i].style.display = "none";
			} else
			{	// If modifying any other closed div
				docDivs[i].style.display = "block";
				docDivs[i].className = "game_release_closed_greyed";
			} // End if
		} // End if

		if(divID.substring(0,6) == "expand")
		{	// If modifying an expanded div
			if(divID.substring(7) == expand_id)
			{	// If modifying the expanded div for the selected game release
				docDivs[i].style.display = "block";
			} else
			{	// If modifying any other expanded div
				docDivs[i].style.display = "none";
			} // End if
		} // End if

	} // End for

} // End expand_release()

function close_release(close_id)
{	// Close selected game release, and restore other collapsed ones
	var docDivs = document.getElementsByTagName("div");
	
	for(var i=0; i<docDivs.length; i++)
	{	// Step through all div tags
		var divID = docDivs[i].id;

		if(divID.substring(0,6) == "closed")
		{	// If modifying a closed div
			docDivs[i].className = "game_release_closed";
		} // End if

	} // End for

	document.getElementById("expand_" + close_id).style.display = "none";
	document.getElementById("closed_" + close_id).style.display = "block";
} // End collapse_release()

function hover_release(hover_id)
{	// Change closed release style appropriately when moused over
	var release_id = document.getElementById(hover_id);

	if(release_id.className == "game_release_closed")
	{	// If changing a closed release
		release_id.className = "game_release_closed_over";
	} else if(release_id.className == "game_release_closed_greyed")
	{	// If changing a greyed closed release
		release_id.className = "game_release_closed_greyed_over";
	} else if(release_id.className == "game_release_closed_over")
	{	// If changing a moused-over closed release
		release_id.className = "game_release_closed";
	} else if(release_id.className == "game_release_closed_greyed_over")
	{	// If changing a moused-over greyed closed release
		release_id.className = "game_release_closed_greyed";
	} // End if
	
} // End hover_release()

function imgPopup(url,width,height)
{	// Create a JavaScript popup window sized to the image it contains
	width = parseInt(width);
	height = parseInt(height);
	
	var window_id = Math.floor(Math.random()*1000000);
	var window_width = width;
	var window_height = height;
	var scrollbars = 0;
	
	if(width > screen.width - 50 || height > screen.height - 50)
	{	// If image is bigger than the screen, let people scroll
		scrollbars = 1;
	} // End if

	if(width >= 100 && height >= 100)
	{	// If image width and height are both >= 100px
		var imgWindow = window.open('',"imgPopup" + window_id,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + scrollbars + ',resizable=1,width=' + window_width + ',height=' + window_height);
		imgWindow.document.write("<html>\n<head>\n<title>" + url + "</title>\n</head>\n<body style=\"margin: 0px; padding: 0px;\">\n<a href=\"javascript: self.close();\"><img src=\"" + url + "\" width=\"" + width + "\" height=\"" + height + "\" border=\"0\" title=\"Click to close window\" /></a>\n</body>\n</html>");
	} else
	{	// If image is smaller than 100px in some aspect
		if(width < 100 && height < 100)
		{	// If both dimensions are less than 100px
			if(width < height)
			{	// If width is less than height
				var window_width = 100;
				var window_height = height + (100 - width);
			} else if(height > width)
			{	// If height is less than width
				var window_width = width + (100 - height);
				var window_height = 100;
			} else
			{	// If width and height are equal
				var window_width = 100;
				var window_height = 100;
			} // End if
			
		} else if(width < 100)
		{	// If only width is smaller than 100px
			var window_width = 100;
			var window_height = height + (100 - width);
		} else if(height < 100)
		{	// If only height is smaller than 100px
			var window_width = width + (100 - height);
			var window_height = 100;
		} // End if

		// Set window padding to center image
		var padding_top = Math.floor((window_height - height) / 2);
		var padding_left = Math.floor((window_width - width) / 2);
		
		var imgWindow = window.open('',"imgPopup" + window_id,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=' + window_width + ',height=' + window_height);
		imgWindow.document.write("<html>\n<head>\n<title>" + url + "</title>\n</head>\n<body style=\"margin: 0px; padding: " + padding_top + "px 0px 0px " + padding_left + "px;\">\n<a href=\"javascript: self.close();\"><img src=\"" + url + "\" width=\"" + width + "\" height=\"" + height + "\" border=\"0\" title=\"Click to close window\" /></a>\n</body>\n</html>");
		
	} // End if

	imgWindow.document.close() 
} // End imgPopup()

function clearSearchForm( input )
{	// Clear text from the search form at the top of the site
	if( input.className == "search_waiting" )
	{	// If the field is in default state
		input.value = '';
		input.className = 'search_ready';
		document.getElementById( "top_search_button" ).className = "top_search_button_active";
	} // End if
} // End clearSearchForm

function resetSearchForm( input )
{	// Put back text from the search form at the top of the site
	if( input == "" )
	{	// If field not supplied
		input = document.getElementById( "sitetop_search_field" );
	} // End if

	if( input.className == "search_ready" && input.value == "" )
	{	// If the field is in entry state, and has no contents
		input.className = 'search_waiting';
		input.value = "Enter part of a game title";
		document.getElementById( "top_search_button" ).className = "top_search_button_inactive";
	} // End if
} // End resetSearchForm

function submitSearchForm()
{
	input = document.getElementById( "sitetop_search_field" );

	if( input.className == "search_ready" )
	{	// If the field doesn't contain default text
		if( input.value == "" )
		{	// If no value was provided
			alert( "Please enter some terms to search on." );
		} else
		{	// Submit the form
			document.getElementById( "sitetop_search" ).submit();	
		} // End if
	} // End if
} // End submitSearchForm