// Create AJAX object
var gameId;		// Variable to save selected game Id for the page;

// Creates Request Object.
// Returns requestobject on success.
function __ajaxObject()
{
	var objRequest;

	try
	{  // Browser : Firefox, Opera 8.0+, Safari  
		objRequest=new XMLHttpRequest();  
	}
	catch (e)
	{  // Browser: Internet Explorer 
		try
		{    objRequest=new ActiveXObject("Msxml2.XMLHTTP");    }
		catch (e)
		{    
			try
			{    objRequest=new ActiveXObject("Microsoft.XMLHTTP");      }
			catch (e) {	}    
		}  
	}
	if(!objRequest)
	{	// Browser doesnot support AJAX.
		alert("Your browser doesnot support AJAX.");
		return(0);
	}	
	return objRequest;
}

// Sends login request to the server.
// Takes appropriate action depending on query result.
function loginUser()
{
	// Create Ajax Obhect to send request.
	var ajax=__ajaxObject();
	// Exit if Ajax in not supported by the browser.
	if(!ajax) return false;

	// Get Elements for reading username and password.
	var userElement=document.getElementById("input1");
	var passwordElement=document.getElementById("input2");

	if (!(userElement && passwordElement))
	{	// Elements are not found.
		alert("Login failure.");
		return false;
	}
	else if (userElement.value=="")
	{	// Username is not entered.
		// Display appropriate message.
		alert("Username is not entered.\nPlease enter your username.");
		return false;
	}
	else if (passwordElement.value=="")
	{	// Password is not entered.
		// Display appropriate message.
		alert("Hi "+userElement.value+",\nYour Password is not entered.\nPlease enter your password.");
		return false;
	}
	// Append URL with username and password.
	var urlLogin="log.php?username="+userElement.value+"&password="+passwordElement.value;

	ajax.open("GET",urlLogin,true);
	ajax.onreadystatechange = function()
	{	
		if(ajax.readyState==4)
		{
			// Delete 'ajax.responseText== ''' in line below
			// if empty data return is treated as SUCCESSFULL login.
			if(ajax.responseText== '' || ajax.responseText=='-1') 
				alert("Login failure!");
			else 
				__updateLoginStatus(true,ajax.responseText);	// Display appropriate Div Element
		}
	}
	// Send Request to the server.
	ajax.send(null);
}

// Sends logout request to the server.
// Takes appropriate action depending on query result.
function logout()
{
	// Create Ajax Obhect to send request.
	var ajax=__ajaxObject();
	// Exit if Ajax in not supported by the browser.
	if(!ajax) return false;

	var urlLogin="logout.php";
	ajax.open("GET",urlLogin,true);
	ajax.onreadystatechange =	function()
	{
		if(ajax.readyState==4)
		{	// Things to do after logout.
			// Display appropraite div element.
			__updateLoginStatus(false,"");
		}
	}
	// Send Request to the server.
	ajax.send(null);
}

// Updated various elements of the page depending on login/logout.
// In case of login, username is display in allocated element.
function __updateLoginStatus(login,username)
{
	// Update Visiblity of loginform div element.
	var element=document.getElementById('LoginForm');
	if(element) element.style.display=(login?'none':'block');
	// Update Visiblity of loggedin div element.
	element=document.getElementById('LoggedIn');
	if(element) element.style.display=(login?'block':'none');
	// Set Username
	element=document.getElementById('Name');
	if(element) element.innerHTML=(login?username:'');

}

// Sends rate for game to the server.
// Displays response in div element with ID'RateResponse'
function rateGame(rating)
{
	// Create Ajax Obhect to send request.
	var ajax=__ajaxObject();
	// Exit if Ajax in not supported by the browser.
	if(!ajax) return false;

	if (rating=="")
	{
		alert("Invalid rating.\nPlease give valid rating.");
		return false;
	}

	var urlRate="rate.php?gameid="+gameId+"&rating="+rating;

	ajax.open("GET",urlRate,true);
	ajax.onreadystatechange = 	function()
	{
		if(ajax.readyState==4)
		{
			// Display response in response element.
			var element=document.getElementById('RateResponse');
			if(element) element.innerHTML=ajax.responseText;
		}
	}
	// Send request to the server.
	ajax.send(null);
}

// Sends comment on the game to the server.
// Takes appropriate action depending on query result.
function commentGame()
{
	// Create Ajax Obhect to send request.
	var ajax=__ajaxObject();
	// Exit if Ajax in not supported by the browser.
	if(!ajax) return false;

	// Read comment
	var element=document.getElementById("copy");
	var comment;
	if(element) comment=element.value;

	if (comment=="")
	{
		alert("No comment is entered.\nPlease enter comment for the game.");
		return false;
	}

	var urlComment="procom.php?gameid="+gameId+"&copy="+comment;

	ajax.open("GET",urlComment,true);
	ajax.onreadystatechange = 	function()
	{
		if(ajax.readyState==4)
		{
			// Display repsone is copcon element
			var element=document.getElementById("copcon");
			if (element) element.innerHTML=comment;

			// Hide Button
			element=document.getElementById("butcon");
			if(element) element.style.display="none";

			// Hiding comment textbox
			element=document.getElementById("copy");
			if(element) element.style.display="none";
		}
	}
	// Send request to the server.
	ajax.send(null);
}