User = Class.create();

User.prototype.id = 0;
User.prototype.registerContainerId = "";

User.prototype.initialize = function(user_id, register_container)
{
	this.id = user_id;
	this.registerContainerId = register_container;
}

User.prototype.onSubmitRegisterSuccess = function(form, xhr)
{
	/*var hash = xhr.hash;
	new Ajax.Updater($(this.registerContainerId),
					 "frontoffice/ajax/command/renderHashForm.php",{
					 	method : "post",
					 	parameters : {
					 		"ms" : new Date()
					 	},
					 	onComplete : function(){
					 		$("user_hash").value = hash;
					 	}
					 });*/
}

User.prototype.onSubmitLoginAccount = function(form, event)
{
	Event.stop(event);
	var parameters = form.serialize(true);
	parameters.ms = new Date();
	new Ajax.Request("frontoffice/ajax/user/login.php",{
		method : "post",
		evalJSON : "force",
	 	parameters : parameters,
	 	onSuccess : this.onLoginAccountSuccess.bind(this,form),
	 	onFailure : this.onLoginAccountFailure.bind(this,form)
	});
}

User.prototype.onLoginAccountSuccess = function(form, xhr)
{	
	if(xhr.responseJSON !== null && xhr.responseJSON.success === true)
		myAccount_mgr.update("frontoffice/ajax/account/home.php");
	else
		this.onLoginAccountFailure(xhr, form);
}

User.prototype.updateUserArea = function(form, xhr)
{	
	if($("userArea") !== null){
	
		new Ajax.Updater(
				$("userArea"),
				"frontoffice/ajax/user/getUserAreaInfos.php"
				);
	}
}

User.prototype.onLoginAccountFailure = function(form, xhr)
{
	alert("Failure");
}

User.prototype.onSubmitLogin = function(form,event)
{
	Event.stop(event);
	var parameters = form.serialize(true);
	parameters.ms = new Date();
	new Ajax.Request("frontoffice/ajax/user/login.php",{
		method : "post",
		evalJSON : "force",
	 	parameters : parameters,
	 	onSuccess : this.onLoginSuccess.bind(this,form),
	 	onFailure : this.onLoginFailure.bind(this,form)
	});
}

User.prototype.onSubmitLoginFailure = function()
{
	alert("login failure");
}

User.prototype.onLoginSuccess = function(form, xhr)
{	
	if(xhr.responseJSON !== null && xhr.responseJSON.success === true)
		window.command_mgr.update(3);
	else
		this.onLoginFailure(xhr, form);
}

User.prototype.onLoginFailure = function(form, xhr)
{
	alert("Failure");
}

User.prototype.logout = function(workflow)
{
	if (confirm("Terminer la session ?"))
	{
		new Ajax.Request(
			"frontoffice/ajax/user/logout.php",
			{
				method : "post",
				parameters : {
					ms : new Date()	
				},
				onSuccess : function (xhr)
				{
					if(workflow == 1)
						command_mgr.update(2);
					else
						front_mgr.navigateTo("index.php");
				}
			}
		);	
	}
}