var loginForm = new function() {

	this.msgAttente = null;
	this.txtPassword = null;
	this.txtUsername = null;
	me = this;

	return {
		disableSubmit : function(disabled) {
			this.btnSubmit.setDisabled(disabled);
		},
		init : function() {
			Ext.form.Field.prototype.msgTarget = 'qtip';

			this.txtUsername = new Ext.form.TextField({
						width : 200,
						allowBlank : false,
						blankText :'SVP entrez votre nom d\'utilisateur',
						inputType : 'text',
						name : 'username',
						isUsername : true,
						vtype : 'login',
						id : 'username'
					});
			this.txtUsername.render('username');

			this.txtUsername.on('invalid', function() {
						loginForm.disableSubmit(true);
					})
			this.txtPassword = new Ext.form.TextField({
						width : 200,
						allowBlank : false,
						blankText : 'Entrez votre mot de passe',
						fieldLabel : 'Mot de passe:',
						inputType : 'password',
						name : 'password',
						isPassword : true,
						vtype : 'login',
						id : 'password'
					});
			this.txtPassword.render('password');

			this.txtPassword.on('invalid', function() {
						loginForm.disableSubmit(true);
					});

			this.btnSubmit = new Ext.Button({
						text : 'Authentification',
						disabled : true
					});
			this.btnSubmit.on('click', function() {

				if (!loginForm.txtPassword.isValid()
						|| !loginForm.txtUsername.isValid()) {
					return;
				}

				me.msgAttente = Ext.MessageBox.wait('Connexion en cours...');
				
						
				// Basic request
				Ext.Ajax.request({
							url : base_url + 'admin/ajax/login?' + new Date().getTime(),
							callback : function(options, success, response) {
                                try 
                                {
	                                options.msgAttente.hide();
	                                if (success)
	                                {
	                                    var json = Ext.util.JSON.decode(response.responseText);
	                                    if (json.success == true) {
	                                        window.location.href = base_url + 'admin/accueil';
	                                    } else {
	                                        Ext.MessageBox.alert("Erreur", json.msg);
	                                    }
	                                }
	                                else
	                                {
	                                    
	                                } 
                                } catch (e)
                                {
                                    alert(e.message);
                                }
							},
							failure : function(r,o) {
								o.msgAttente.hide();
								Ext.MessageBox.alert('Erreur',r.responseText);
							},
							scope : this,
							msgAttente : me.msgAttente,
							params : {
								username : loginForm.txtUsername.getValue(),
								password : loginForm.txtPassword.getValue()
							}
						});

			});

			this.btnSubmit.render('submit');
		}

	}
}

Ext.onReady(function() {
			Ext.QuickTips.init();

			Ext.apply(Ext.form.VTypes, {
						loginText : '',
						login : function(val, field) {

							if (Ext.getCmp('username').getValue().trim() != ''
									&& Ext.getCmp('password').getValue().trim() != '')
								loginForm.disableSubmit(false);

							return true;
						}
					});

			loginForm.init();
		});