var loginForm;
var loginElt;
var loginInfoElt;
var passwdElt;
var ajaxElt;
var loginSubmitElt;
var authenticationContainerElt;

var loadingIndicatorElt;

var loginMsg;

var defaultLoginMsg = {
		loginLabel: "Nom d'utilisateur",
		passwordLabel: "Mot de passe",
		requiredAlertStart: "Le champ ",
		requiredAlertEnd: " n'a pas été rempli",
		pleaseWait: 'Veuillez patienter, connexion en cours...',
		errorOccured: 'Un problème est survenu !'
};

function initLoginForm(e) {

	loginForm = $('loginForm');
	if (!loginForm) { return; }

	// Preload Images :
	loadingIndicatorElt = $(document.createElement('img'));
	loadingIndicatorElt.src = './images/ajax-menus-loader.gif';

	authenticationContainerElt = $('authenticationContainer');
	loginInfoElt = $('authenticationInfo');
	loginElt = $('login');
	ajaxElt = $('loginAjax');
	passwdElt = $('password');
	loginSubmitElt = $('loginSubmit');
	loginForm.observe('submit', submitLoginFormInAjax);

	loginElt.observe('keyup', function (e) {checkKey(e, true);});
	passwdElt.observe('keyup', function (e) {checkKey(e);});
	loginElt.focus();
}

function submitLoginFormInAjax(e) {
	e.stop();
	if (loginMsg == null) loginMsg = defaultLoginMsg;
	if (checkLoginFields()) {
		loginSubmitElt.parentNode.replaceChild(loadingIndicatorElt, loginSubmitElt);
		loadingIndicatorElt.title = loginMsg.pleaseWait;
		ajaxElt.value = 'true';
		loginForm.request({onComplete: handleLoginResponse});
	}
}

function handleLoginResponse(xhr, json){
	if (json) {
		if (json.stat) {
			loginInfoElt.innerHTML = '&nbsp;';
			authenticationContainerElt.update(xhr.responseText);
			if (json.showAllArticleEditorIcons) {
				showAllArticleEditorIcons(true);
			}
		} else {
			loginInfoElt.innerHTML = xhr.responseText;
		}
	} else {
		loginInfoElt.innerHTML = loginMsg.errorOccured; // TODO L10n
	}
	loadingIndicatorElt.parentNode.replaceChild(loginSubmitElt, loadingIndicatorElt);
	/*
	if (typeof(Laruiss) == "undefined" || typeof(Laruiss.Article) == "undefined") {
		alert('typeof(Laruiss.Article): ' + typeof(Laruiss.Article));
		var articleUrl = './js/laruiss.article.js';
		new Ajax.Request(articleUrl, {onComplete: function() {alert('updating now...'); Article.updateEditors()}});
	} else {
		Article.updateEditors();
	}
	*/
	
}

function checkLoginFields() {
	if (!checkField($F(loginElt),loginMsg.loginLabel)) {
		loginElt.focus();
		return false;
	} else if (!checkField(passwdElt.value,loginMsg.passwordLabel)) {
		passwdElt.focus();
		return false;
	}
	return true;
}

function checkLoginField(e, isClick) {
	return (isClick || isEnterPressed(e)) && checkLoginFields();
}

function checkField(valeur,desc) {
	if (valeur.length == 0) {
		alert(loginMsg.requiredAlertStart + desc + loginMsg.requiredAlertEnd);
		return false;
	} else
		return true;
}

function isEnterPressed(e) {
	code = e.which || e.keyCode;
	e.stop();
	return (code == Event.KEY_RETURN);
}

function checkKey(e, gotoPwd) {
	if (isEnterPressed(e)) {
		if (gotoPwd && $F(passwdElt).length == 0) {
			passwdElt.focus();
		} else {
			checkLoginField(e);
		}
	}
}
document.observe('dom:loaded', initLoginForm);