/**
 * @author xav
 */
var LxAjaxLogin ={

		responderUrl: null,
		formButtonId: 'loginBt',
		loginDivId  : 'loginDiv',
		loginFormId : 'loginForm',
		indicatorDivId   : 'indicatorDiv',
		errormsgDivId: 'errMsg',
		replaceHtml : false,
		loginForm   : null,
		
		start: function(params)
		{
			if (params.formButtonId){
				this.formButtonId = params.formButtonId;
			}
			
			if (params.responderUrl){
				this.responderUrl = params.responderUrl;
			}
			
			if (params.loginDivId){
				this.loginDivId = params.loginDivId;
			}
			
			if (params.loginFormId){
				this.loginFormId = params.loginFormId;
			}
			
			if (params.indicatorDivId){
				this.indicatorDivId = params.indicatorDivId;
			}
			
			if (params.errormsgDivId){
				this.errormsgDivId = params.errormsgDivId;
			}
			
			this.loginForm = document.getElementById(this.loginFormId);
			YAHOO.util.Event.addListener(this.formButtonId, 'click', this.doRequest, this, true);
			YAHOO.util.Dom.setStyle(this.indicatorDivId, 'display', 'none');
		}, 
		
		
		doSuccess: function(o)
		{
			var oSelf = o.argument[0];
			YAHOO.util.Dom.setStyle(oSelf.indicatorDivId, 'display', 'none');
			var oJson = eval('(' + o.responseText + ')');
			var html;
			
			switch(oJson.status){
				case 'nodata':
					html = '<strong style="color: red">No datas...</strong>';
					break;
					
				case 'fail':
					html = '<strong style="color: red">Erreur login</strong>';
					break;
					
				case 'ok':
					html = '<strong style="color: green">Bonjour ' + oJson.user.prenom + " " + oJson.user.nom + '</strong>';
					oSelf.replaceHtml = true;
					break;					
			}
			
			if (oSelf.replaceHtml){
				var loginBox = document.getElementById(oSelf.loginDivId);
				loginBox.innerHTML = html;
				
				if (typeof(inCheckout) != 'undefined'){
					console.debug('ok redir');
					window.location = '/Reservation/Checkout';
				}
			} else {
				var respBox = document.getElementById(oSelf.errormsgDivId);
				console.debug(respBox);
				respBox.innerHTML = html;
			}
			
		},
		
		doFailure: function(o)
		{
			console.debug('error');
		},
		
		
		doRequest: function(){

			oCbHelper= {
				success: this.doSuccess,
				failure: this.doFailure,
				argument: [this]
			};
			
			YAHOO.util.Connect.setForm(this.loginForm);
			var req = YAHOO.util.Connect.asyncRequest('POST', this.responderUrl, oCbHelper);

			if (YAHOO.util.Connect.isCallInProgress(req)){
				YAHOO.util.Dom.setStyle(this.indicatorDivId, 'display', '');
			}
		}
}