// The DIV ID where the response should be parsed
var LOGIN_DIV_RESPONSE = "ajaxLoginMessage";
var LOGIN_HIDEDIV_TIMER = 200; // Microseconds to dissapear the message after a new login try

// Holds an instance of XMLHttpRequest
var xmlHttpLogin = createXmlHttpRequestObject();

// Ask to login with username & password
function login(username, password) {
	xmlHttpProcess(
		xmlHttpLogin,
		processLogin, 
		"account/ajax_login/(ajax)",
		"user=" + encodeURI(username) + "&pass=" + encodeURI(password));
}

// What to do with the XML response?
function processLogin(response) {
	if (parseInt(response.substring(0,response.indexOf(' '))) <= 0) {
		// Show error
		myDiv = $(LOGIN_DIV_RESPONSE);
		myDiv.style.display = "block";
		myDiv.style.visibility = "visible";
		myDiv.innerHTML = response.substring(3);
	} else {
		// Refresh page
		window.location.reload(true);
	}
}

// Submits the login form
function submitLogin() {
	if ($(LOGIN_DIV_RESPONSE).innerHTML.length > 0) {
		// Hide message for a short while 
		$(LOGIN_DIV_RESPONSE).style.visibility="hidden";
	}
	login($("login[username]").value, $("login[password]").value);
}