// JavaScript Document

Ext.namespace('myApp');

/**
 * @class myApp.user
 * This class manages user login operations.
 */
myApp.user = function(){

	///////////////////////////////////////////////////////////////////////////
    //---------------------------Private Variables---------------------------//
	///////////////////////////////////////////////////////////////////////////
    //  Warning: Do not access DOM in this area. This area will be executed
	//           before the page has finished loading, so some elements
	//           probably do not exist yet.
	///////////////////////////////////////////////////////////////////////////
	
	var formLogin; 				// Ext.FormPanel
	
	
	///////////////////////////////////////////////////////////////////////////
    //----------------------------Private Methods----------------------------//
	///////////////////////////////////////////////////////////////////////////
    //  Caution: Do not access DOM in this area unless you are calling these
	//           methods after the page has finished loading (onReady)
	///////////////////////////////////////////////////////////////////////////	
	
	
   /**
    * Configure the GUI for the login form.
	* @access	private
    */
    var buildLoginForm = function(){
	
        /**
         * Create Handlers
         * Create functions to handle various events
         * (listed alphabetically)
         */
		function submitLogin()
		{
			if (formLogin.getForm().isValid()) {
				formLogin.getForm().submit({ 
					method: 'POST', 
					waitTitle: 'Connecting', 
					waitMsg: 'Verifying Credentials...',
					url: urlUserLoginAuth,
					success: function(form, action){ 						
						docBody.mask("Login Successful...Redirecting...","x-mask-loading");
						if (action.result.redirect) {
							window.location = action.result.redirect;
						}
					},
					failure: function(form, action){
						if(action.failureType == 'server'){ 
							Ext.myMsgBox.showErrorConn();
						}else{ 
							Ext.myMsgBox.showError('Login Error', action.result.info);
							Ext.getCmp('user-password').setValue();
						} 										
					} 
				});
			}else{
				Ext.myMsgBox.showWarnReqFields();
			}				
		}


		formLogin = new Ext.FormPanel({
			bodyStyle: 'padding-top: 5px',
			labelWidth: 80,
			labelAlign: 'right',
			frame: true, 
			title: 'Please Login',
			//renderTo: 'form-login',
			width: 350, 
			padding: 200, 
			monitorValid: true,
			layout: 'form',
			keys: {key: Ext.EventObject.ENTER, fn: submitLogin},
			defaults: {
				xtype: 'textfield',
				width: 200
			},			
			items:[{
				   	id: 'user-mls-id',
					fieldLabel: 'MLS Id', 
					name: 'user_mls_id', 
					allowBlank: false 
				},{ 
					id: 'user-password',
					fieldLabel: 'Password', 
					name: 'user_password', 
					inputType: 'password', 
					allowBlank: false 
			}],
			buttons:[{ 
					text: 'Login',
					formBind: true,	 
					handler: submitLogin
			}] 
		});		
	

	};// end function buildLoginForm
	
	
   /**
    * Allows manually rendering the login form
	* @access	private
    */
    var renderFormLogin = function() {  
	
        formLogin.render('form-login');
		
		Ext.getCmp('user-mls-id').focus();
		
    };//end function renderFormLogin



	///////////////////////////////////////////////////////////////////////////
    //------------------------------Public Area------------------------------//
	///////////////////////////////////////////////////////////////////////////

    return {
            

		///////////////////////////////////////////////////////////////////////////
		//---------------------------Public Properties---------------------------//
		///////////////////////////////////////////////////////////////////////////
		// It is considered good practice to put the following in the public area
		// of the module:
		//     text used by module,
		//     default dimensions, styles, 
		//     customizable options, etc.
		///////////////////////////////////////////////////////////////////////////
		
		



		///////////////////////////////////////////////////////////////////////////
		//-----------------------------Public Methods----------------------------//
		///////////////////////////////////////////////////////////////////////////
		// Public methods can be called from outside the module
		// Public methods can access Private Area
		///////////////////////////////////////////////////////////////////////////

		/**
		 * Initialization Method
		 * @access	public
		 */
		init : function(){
			// This is a good place to put DOM dependent tasks 
			// since we know the elements are now loaded.
			
			docBody = Ext.get(document.body);	// used to apply a loading mask
			
			buildLoginForm();
			
			renderFormLogin();
			
        },//end of init method


		/**
		 * Checks if user is currently logged in
		 * @access	public
		 * @param	object	ret
		 */
		verifyLogin : function(ret){
			
			if (ret.logged_in == false) {
				Ext.Msg.show({
					title: 'Login Error',
					msg: ret.info,
					icon: Ext.Msg.ERROR,
					buttons: Ext.Msg.OK,
					fn: function(){
						window.location = urlUserLogin;
					}
				});						
			}
			
		}//end of verifyLogin method

    }//end of return

}();


