// reference local blank image
Ext.BLANK_IMAGE_URL = '/extjs/ext-3.0.3/resources/images/default/s.gif';
Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
Ext.QuickTips.init();
Ext.QuickTips.enable();
Ext.form.Field.prototype.msgTarget = "side";
Ext.form.Field.prototype.labelSeparator = "";

// some data used in the examples
Ext.namespace('app', 'app.main');

// create application
app.main = function() {

	// private space - do NOT access DOM from here; elements don't exist yet

	 return { // public space

		  // public properties, e.g. strings to translate
		  btn1Text: 'Button 1'

		  // public methods
		  , init: function() {

			  var forgotForm = new Ext.form.FormPanel({
			 id: 'form-ForgotForm'
			 , border: false
			 , width: 300
			 , labelAlign: 'right'
			 , labelWidth: 30
			 , items: [
				new Ext.form.TextField({
				  fieldLabel: 'Email'
				  , id: 'email'
				  , name: 'email'
				  , vtype: 'email'
				  , anchor: '90%'
				  , emptyText: 'Enter Email Address'
				  //, value: ''
				  , inputType: 'text'
				  , allowBlank: false
				  , validateOnBlur: true
				  , msgTarget: 'side'
				  //, specialKey: true
				  //, autoCreate: {tag: "input", type: "text", autocomplete: "off"}
				})
			 ]
		 });

		 var forgotWindow = new Ext.Window({
			 title: 'Forgot Login Information'
			 , autoWidth: true
			 , autoHeight: true
			 , bodyStyle: 'padding: 10px; background-color: #ffffff;'
			 , url: '/login/forgot'
			 , method: 'POST'
			 , items: [ forgotForm ]
			 , buttons: [
				{
				  xtype: 'button'
				  , text: 'Reset Password &raquo;'
				  , icon: "resources/icons/tick.png"
				  , cls: "x-btn-text-icon"
				  , handler: function() { submitForgotForm(); }
				}
			 ]
		});

		var unField = new Ext.form.TextField({
				fieldLabel: 'Username'
				, labelAlign: 'right'
				, id: 'un'
				, name: 'un'
				//, value: ''
				, inputType: 'text'
				, allowBlank: false
				, minLength: 2
				, maxLength: 150
				, validateOnBlur: true
				, msgTarget: 'side'
				, value: Ext.state.Manager.get('LastUserName','')
				//, specialKey: true
				//, autoCreate: {tag: "input", type: "text", autocomplete: "off"}
			});

		var pwField = new Ext.form.TextField({
				fieldLabel: 'Password'
				, labelAlign: 'right'
		  , id: 'pw'
				, name: 'pw'
				  //, value: ''
				, inputType: 'password'
				, allowBlank: false
				, minLength: 2
				, maxLength: 30
				, buttonAlign: 'center'
				, validateOnBlur: true
				, msgTarget: 'side'
				, display: 'none'
				, value: Ext.state.Manager.get('LastUserPassword','')
				//, specialKey: true
				//, autoCreate: {tag: "input", type: "password", autocomplete: "off"}
			});

		var saveUnPw = new Ext.form.Checkbox({
				boxLabel: 'Keep Login Info'
				, labelAlign: 'right'
				, id: 'saveunpw'
				, name: 'saveunpw'
				, checked: Ext.state.Manager.get('SaveUserNamePassword',false)
				  //, value: ''
//				, buttonAlign: 'center'
//				, msgTarget: 'side'
				//, specialKey: true
				//, autoCreate: {tag: "input", type: "password", autocomplete: "off"}
			});

		  var myForm = new Ext.form.FormPanel({
		  title: 'Welcome To USS&trade; Webship'
		  , renderTo: 'form-login'
		  //id: 'form-login'
		  /*
		  onSubmit: Ext.emptyFn
		  , submit: function() {
		  this.getForm().getEl().dom.submit();
		  }
		  */
				, url: '/login/auth'
				, method: 'POST'
				//, specialKey: true
				, loadMask: true
				, frame: true
				, width: 290
				, bodyStyle: 'padding: 10px 0px 0px 0px'
				, labelAlign: "right"
				/*
				, layoutConfig: {
				  //layout-specific configs go here
				  //labelSeparator: ":"
				}
				*/
				//, labelSeparator: ""
				, labelWidth: 65 // label settings here cascade unless overridden
		  , defaults: {width: 150, labelSeparator: '', labelWidth: 50, labelAlign: 'right'}
		  , defaultType: 'textfield'
		  , items: [
			 unField
			 , pwField
			 , saveUnPw
			]
			, buttonAlign: 'center'
			, buttons: [
			 {
				xtype: 'button'
				, text: 'Sign-in &raquo;'
				, icon: "resources/icons/tick.png"
				, cls: "x-btn-text-icon"
				, disabled: false
				//, scope: this
				, handler: function() { submitLogin(); }
			  }
			  , {
				xtype: 'button'
				//, style: 'padding: 10px;'
				, text: 'Forgot'
				, icon: "resources/icons/help.png"
				, cls: "x-btn-text-icon"
				, handler: function(){
				  forgotWindow.show();
				}
			 }
			]
			 });

				//username field
				unField.on('specialkey', function(t,e) {
				if(e.getKey() == 13) {
					submitLogin();
				}
			});

			//password field
			pwField.on('specialkey', function(t,e) {
				if(e.getKey() == 13) {
					submitLogin();
				}
			});

			var submitForgotForm = function() {
		  if ( forgotForm.form.isValid() ) {
			 Ext.Ajax.request({
				form: 'form-ForgotForm'
				, method: 'post'
				, url: '/login/forgot'
				, scope: this
				, params: { email: Ext.get('email').getValue() }
				, success: function(response){
				  var r = Ext.util.JSON.decode(response.responseText);//passed back from server
				  forgotWindow.hide();
				  if(r.success) { Ext.Msg.alert('Request Successful','Check Your Email Box For Reset Password Link'); }
				  else { Ext.Msg.alert('Email Not Found', 'Please Try Again.'); }
				}
				, failure: function(response) {
				  //forgotForm.setTitle("Failed. &nbsp; Please Try Again...");
				  //Ext.MessageBox.alert('Request Failed', action.result.message);
				}
			 });
		  }
		};

		/*var submitForgotForm = function() {
		  if ( forgotForm.form.isValid() ) {
			 //forgotForm.setTitle("Requesting...");
			 forgotForm.form.submit({
				//waitMsg: 'Requesting...'
				reset: false
				, success: function(form, action) {
				  forgotForm.setTitle("Login Information Has Been Sent To Your Email.");
				}
				, failure: function(form, action) {
				  forgotForm.setTitle("Failed. &nbsp; Please Try Again...");
				  //Ext.MessageBox.alert('Request Failed', action.result.message);
				}
			 });
		  }
		};
		*/

		var submitLogin = function() {
				if ( myForm.form.isValid() ) {
					var vals = myForm.form.getValues();
					Ext.state.Manager.set('SaveUserNamePassword',vals.saveunpw);
					if(vals.saveunpw) {
						Ext.state.Manager.set('LastUserName',vals.un);
						Ext.state.Manager.set('LastUserPassword',vals.pw);
					} else {
						Ext.state.Manager.set('LastUserName','');
						Ext.state.Manager.set('LastUserPassword','');
					}
					myForm.form.submit({
						waitMsg: 'Authorizing...'
						, reset: false
						, success: function(form, action) {
							Ext.Msg.wait(action.result.message, "Login Successful");
							//myForm.setTitle("Login Successful.&nbsp; Initializing...");
							location.replace('/');
						}
						, failure: function(form, action) {
							myForm.setTitle("Login Failed. &nbsp; Please Try Again...");
							Ext.MessageBox.alert('Login Failed', action.result.message);
						}
					});
				}
			};

			setTimeout(function() {
				Ext.get('loading').remove();
				Ext.get('loading-mask').fadeOut({remove:true});
				Ext.get('un').focus();
			 }, 1000);

		 } //end init

	 }; // end public space

}(); // end of app