


var LOGIN = function(){

	return {
		init : function(){

			this.wait = Ext.get('post-wait');
			//alert('OK1');
			this.error = Ext.get('post-error');
			//alert('OK2');
			this.errorMsg = Ext.get('post-error-msg');
			//alert('OK3');
			this.username=Ext.get('USERNAME');
			//alert('OK4');
			this.showDialog();
			//alert('OK5');
		},

		showDialog : function(){
			if(!this.dialog){ // lazy initialize the dialog and only create it once
				this.dialog = new Ext.Window({
					applyTo:'login-dlg',
					width:300,
					height:300,
					modal: true,
					autoScroll: false,
					closable: false,
					buttons: [{
						text: 'ENTRAR',
						handler: function(){
							LOGIN.submitLogin();
						}
					}]
				});
			}
			//alert('OK6');
			this.dialog.show();
		},
		submitLogin:function() {
			this.wait.show();

			username=Ext.get('USERNAME');
			password=Ext.get('PASSWORD');

			Ext.Ajax.request({
				url : 'ajax.php',
				params: {a:'login',u:username.dom.value,p:password.dom.value},
				waitMsg:'Conectando...',
				success: function ( result, request ) {
					//doJSON( result.responseText );

					var res=Ext.util.JSON.decode(result.responseText);
					LOGIN.res=res;
					if (res.success==false) {
						Ext.MessageBox.show({
							width:400,
							title: 'Erro',
							msg: res.error,
							buttons: Ext.MessageBox.OK,
							icon: 'ext-mb-error'
						});
					} else if (res.success==true) {
						window.location.replace(window.location.pathname);
					} else {
						Ext.MessageBox.alert('Erro','N&atilde;o foi poss&iacute;vel conectar');
					}
					LOGIN.wait.hide();
				},
				failure: function ( result, request ) {
					Ext.MessageBox.alert( 'Erro','Erro de conex&atilde;o!' );
					LOGIN.wait.hide();
				}
			});


		}
	};
}();

Ext.BLANK_IMAGE_URL='images/blank.gif';
Ext.onReady(LOGIN.init, LOGIN, true);
