/*
 * byo - 2008
 * authentication
 */

var auth= {

	// initLogin - v1 //////////////////////////////
	initLogin : function(idform) {

		$(idform).addEvent('submit', auth.login);
		return false;

	},
	// initLogin //////////////////////////////

	// login - v1 //////////////////////////////
	login : function() {

		this.set('send', {
						url: this.getProperty('action')+'/', // IE6 need the / at the end !!!
						method: 'post',
						onFailure: function(){alert('error');},
						//onRequest: function(){alert('prout')},
						onSuccess : auth.loginResponse
					});

		this.send(); //send the form

		return false;

	},
	// login //////////////////////////////

	// loginResponse - v1 //////////////////////////////
	loginResponse : function(response) {

		//alert(response);

		response= JSON.decode(response);

		if (response['result']==1) {
			location.reload();
			return true;
		}

		$('loginform-message').set('text', response['message']).addClass('error');

		return false;

	},
	// loginResponse //////////////////////////////

	// initLogout - v1 //////////////////////////////
	initLogout : function(button) {

		if(!$(button)) return false;

		$(button).addEvent('click', auth.logout);

		return false;

	},
	// initLogout //////////////////////////////

	// logout() - v1 //////////////////////////////
	logout : function() {

		var request = new Request({
							method: 'get',
							url: this.getProperty('href'),
							onFailure: function() {alert('error');},
							onSuccess : function(response) {
									//alert(response);
									location.reload();
									}
						}).send();

		return false;

	}
	// logout() //////////////////////////////

};