var authenticateUser = false;
var idPasswordContainer = "";

function isRegisteredUser(idUser, idPassword)
{
	var user = document.getElementById(idUser).value;
	var password = document.getElementById(idPassword).value;
	idPasswordContainer = idPassword;

	synchronousAuthenticationChecker('authentication.php', 'POST', user, password);
	if (!authenticateUser)
	{
		return false;
	}
	return true;
}

function synchronousAuthenticationChecker(url, methodAssociated, user, password)
{
	httpRequest = initiateXHR();
	if (httpRequest)
	{
		httpRequest.open(methodAssociated, url, false);
		httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		httpRequest.send("user=" + user + "&password=" + password);
		receiverAuthenticationResponse();
	}
}

function receiverAuthenticationResponse()
{
	if (httpRequest.readyState == READY_STATE_COMPLETE)
	{
		if (httpRequest.status == 200)
		{
			authenticateUser = false;
			var xmlResponse = httpRequest.responseXML;
			var authenticationResult = xmlResponse.getElementsByTagName("result")[0];
			var authenticate = authenticationResult.getAttribute('authenticate');
			if (authenticate != 'true')
			{
				authenticateUser = false;
				var parent = document.getElementById('id_alert');
				if (parent.childNodes.length != 0)
				{
					parent.removeChild(parent.lastChild);
				}
				parent.appendChild(authenticationResult.childNodes[0]);
				parent.style.display = 'block';
				document.getElementById(idPasswordContainer).value = "";
			}
			else
			{
				authenticateUser = true;
			}
		}
	}
}

function deleteAlertMessage(idAlert)
{
	var parent = document.getElementById(idAlert);

	if (parent.childNodes.length != 0)
	{
		parent.removeChild(parent.lastChild);
	}
	parent.style.display = 'none';
}
