Tp.ui.view.userCredential.changePassword = Class.create( {

	/*
	 * Default Constructor
	 */

	initialize : function(passwordConfig, handlers) {
		this.config = passwordConfig;
		this.handlers = handlers;
		this.initView();
	},

	initView : function() {
		Ext.apply(Ext.form.VTypes, {

			password : function(val, field) {
				if (field.initialPassField) {
					var pwd = Ext.getCmp(field.initialPassField);
					return (val == pwd.getValue());
				}
				return true;
			},

			passwordText :'Passwords do not match'
		});

		var localObject = this;
		this.Panel = new Ext.FormPanel( {
			padding :'15px 10px 10px',
			cls :'userCredential',
			items : [
					this.createTextField('Enter old Password', 150, 20,
							"oldPassword", "", "", false,
							'Old Password is required', "", ""),
					this.createTextField('Enter new Password', 150, 20,
							"newPassword", "newPassword", "", false,
							'New Password is required', "", ""),
					this.createTextField('Re-Enter new Password', 150, 20,
							"pass-cfrm", "", "", false,
							'Confirm Password is required', "password",
							"newPassword") ]
		});

		this.mainWindow = new Ext.Window( {
			layout :'anchor',
			width :355,
			height :170,
			modal :true,
			loadMask :true,
			maximizable :false,
			draggable :false,
			resizable :false,
			id :'change',
			cls :'changePassword',
			title :'Change password',
			plain :true,

			items : [ this.Panel ],
			buttons : [ {
				text :'Cancel',
				handler : function() {
					localObject.mainWindow.destroy();
				}

			}, {
				text :'Submit',
				handler : function() {
					localObject.onDataEntered();

				}
			} ]

		});
		this.mainWindow.show();
		localObject.onDataCleared();
	},

	onDataCleared : function() {
		this.Panel.getForm().reset();
	},

	onDataEntered : function() {

		if (!this.Panel.getForm().isValid()) {
			return;
		}
		var localObject = this;
		var newPasswordObj = {
			oldPassword :this.Panel.getForm().findField("oldPassword")
					.getValue(),
			newPassword :this.Panel.getForm().findField("newPassword")
					.getValue(),
			confirmPassword :this.Panel.getForm().findField("pass-cfrm")
					.getValue()

		};

		Tp.service.user.UserService.changePassword( {
			newPasswordObj :newPasswordObj
		}, {

			onBusinessFailure : function() {
				// this.mainWindow.destroy();
			Ext.MessageBox.show( {
				title :'Registration',
				msg :"Invalid User Credential",
				buttons :Ext.MessageBox.OK,
				animEl :'mb9',
				icon :Ext.MessageBox.INFO
			});
		},
		onSuccess : function() {
			localObject.closeView();
			Ext.MessageBox.show( {
				title :'Registration',
				msg :"Password has been changed successfully",
				buttons :Ext.MessageBox.OK,
				animEl :'mb9',
				icon :Ext.MessageBox.INFO
			});
			
			
		}

		});

		
	},
	closeView : function() {
		
		this.mainWindow.destroy();
	},
	

	createTextField : function(label, labelWidth, labelHeight, id, name, value,
			allowBlank, blankText, vtype, initialPassField) {
		var fieldLabel = label;
		if (!allowBlank) {
			fieldLabel = '<span class="mandatory">*</span>' + label;
		}

		return textVar = new Ext.form.TextField( {
			id :id,
			name :name,
			fieldLabel :fieldLabel,
			allowBlank :allowBlank,
			blankText :blankText,
			labelSeparator :':',
			value :value,
			inputType :'password',
			minLength :8,
			maxLength :12,
			msgTarget :'side',
			width :labelWidth,
			vtype :vtype,
			initialPassField :initialPassField,
			height :labelHeight

		});
	}
});
