Ext.ns('DJS');

DJS.Login = Ext.extend(Ext.form.FormPanel, {
	error: '',
	forgotPasswordURL: 'http://localhost/DJS/html/pages/forgot_password.php',
	submitURL: '',
	
	initComponent: function() {
		// Configure this object
		Ext.apply(this, {
			bodyStyle: 'padding: 10px; text-align: left',
			buttonAlign: 'center',
			defaultType: 'textfield',
			title: 'Enter Your Login Information',
			//height: 210,
			width: 329,
			
			buttons: [
				{
					handler: this.onSubmitClick,
					scope: this,
					//type: 'submit',
					text: 'Login'
				}
			],
			keys: [{
				//when the enter key is pressed
				key: [10,13],
				scope:this,
				fn: this.onSubmitClick
			}],
			items: [
				{
					fieldLabel : 'Username',
					name : 'username',
					allowBlank : false,
					blankText : 'Username is required',
					anchor : '100%'
				},
				{
					fieldLabel : 'Password',
					inputType : 'password',
					name : 'pass',
					id: 'pass',
					allowBlank : false,
					blankText : 'Password is required',
					anchor : '100%'
				},
				this.label = new Ext.form.Label({
					style: 'font-size: 10pt; font-weight: normal; color: red',
					html: '<br />' + this.error
				}),
				{
					xtype: 'label',
					html: '<br /><br /><a href="' + this.forgotPasswordURL +
						'">Forgot Password?</a>',
					style: 'color:#15428b;font:10pt tahoma,arial,verdana,sans-serif;padding:5px 0 4px 0;'
				}
			]
		});
		
		// Call the base class's initComponent
		DJS.Login.superclass.initComponent.call(this);
	},
	
	onSubmitClick: function()
	{
		if (this.getForm().isValid())
		{
			this.getForm().submit({
				params: {
					name: 'loginform',
					id: 'loginform'
				},
				scope: this,
				url: this.submitURL,
				
				failure: function(form, action) {
					this.label.setText('<br />' + action.result.error, false);
				},
				success: function(form, action) {
					if (!Ext.isEmpty(action.result.url))
						window.location = action.result.url;
				}
			});
		}
		else
		{
			this.label.setText('<br />The form is not complete.', false);
		}
	}
});
