/*
 MIT License: http://www.opensource.org/licenses/mit-license.php

 This implementation of the password fields shows a nice graph of how
 secure the password is.
 Original implementation for ExtJS 1.1 from http://testcases.pagebakers.com/PasswordMeter/.
*/
Ext.namespace("Zarafa.plugins.passwd");Zarafa.plugins.passwd.ABOUT='<p>Copyright (C) 2013  Saket Patel &lt;silentsakky@gmail.com&gt;</p><p>This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.</p><p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.</p><p>You should have received a copy of the GNU Affero General Public License along with this program. If not, see <a href="http://www.gnu.org/licenses/" target="_blank">http://www.gnu.org/licenses/</a>.</p>';
Ext.namespace("Zarafa.plugins.passwd");Zarafa.plugins.passwd.PasswdPlugin=Ext.extend(Zarafa.core.Plugin,{initPlugin:function(){Zarafa.plugins.passwd.PasswdPlugin.superclass.initPlugin.apply(this,arguments);this.registerInsertionPoint("context.settings.categories",this.createSettingsCategory,this)},createSettingsCategory:function(a,b,c){return{xtype:"zarafa.settingspasswdcategory",settingsContext:c}}});
Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"passwd",displayName:dgettext("plugin_passwd","Password Change Plugin"),about:Zarafa.plugins.passwd.ABOUT,pluginConstructor:Zarafa.plugins.passwd.PasswdPlugin}))});Ext.namespace("Zarafa.plugins.passwd.data");
Zarafa.plugins.passwd.data.PasswdResponseHandler=Ext.extend(Zarafa.core.data.AbstractResponseHandler,{callbackFn:undefined,scope:undefined,doError:function(a){var b=dgettext("plugin_passwd","An unknown error occurred while changing password.");if(a.info)b=a.info.display_message;Ext.MessageBox.alert(dgettext("plugin_passwd","Error"),b);this.callbackFn.apply(this.scope||this,[false,a])},doSuccess:function(a){var b=dgettext("plugin_passwd","Password is changed successfully.");if(a.info)b=a.info.display_message;
Ext.MessageBox.alert(dgettext("plugin_passwd","Success"),b);this.callbackFn.apply(this.scope||this,[true,a])}});Ext.namespace("Ext.ux.form.field");
Ext.ux.form.field.PasswordMeter=Ext.extend(Ext.form.TextField,{constructor:function(a){a=a||{};Ext.applyIf(a,{xtype:"ux.passwordmeterfield",inputType:"password",enableKeyEvents:true});Ext.ux.form.field.PasswordMeter.superclass.constructor.call(this,a)},initComponent:function(){Ext.ux.form.field.PasswordMeter.superclass.initComponent.apply(this,arguments)},reset:function(){Ext.ux.form.field.PasswordMeter.superclass.reset.call(this);this.updateMeter()},onKeyUp:function(a){Ext.ux.form.field.PasswordMeter.superclass.onKeyUp.call(this);
this.updateMeter(this.getValue());this.fireEvent("keyup",this,a)},afterRender:function(){Ext.ux.form.field.PasswordMeter.superclass.afterRender.call(this);var a=this.getEl().getWidth();this.strengthMeterID=newID=Ext.id();this.scoreBarID=Ext.id();a=Ext.DomHelper.insertAfter(this.getEl(),{tag:"div","class":"x-form-strengthmeter",id:this.strengthMeterID,style:{width:a+"px"}});Ext.DomHelper.append(a,{tag:"div","class":"x-form-strengthmeter-scorebar",id:this.scoreBarID});this.fireEvent("afterrender",this)},
getScore:function(){return this.calcStrength(this.getValue())},updateMeter:function(a){var b,c;b=Ext.get(this.strengthMeterID);c=Ext.get(this.scoreBarID);b=b.getWidth();if(a){a=this.calcStrength(a);a=b-b/100*a;c.applyStyles({margin:"0 0 0 "+(b-a)+"px"});c.setWidth(a,false)}else{c.applyStyles({margin:"0"});c.setWidth(b,false)}},calcStrength:function(a){var b=a.length,c=b;if(b>0&&b<=4)c+=b;else if(b>=5&&b<=7)c+=6;else if(b>=8&&b<=15)c+=12;else if(b>=16)c+=18;if(a.match(/[a-z]/))c+=1;if(a.match(/[A-Z]/))c+=
5;if(a.match(/\d/))c+=5;if(a.match(/(?:.*?\d){3}/))c+=5;if(a.match(/[\!,@,#,$,%,\^,&,\*,\?,_,~]/))c+=5;if(a.match(/(?:.*?[\!,@,#,$,%,\^,&,\*,\?,_,~]){2}/))c+=5;if(a.match(/(?=.*[a-z])(?=.*[A-Z])/))c+=2;if(a.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/))c+=2;if(a.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\!,@,#,$,%,\^,&,\*,\?,_,~])/))c+=2;return Math.min(Math.round(c*2),100)}});Ext.reg("ux.passwordmeterfield",Ext.ux.form.field.PasswordMeter);Ext.namespace("Zarafa.plugins.passwd.settings");
Zarafa.plugins.passwd.settings.PasswdPanel=Ext.extend(Ext.form.FormPanel,{constructor:function(a){a=a||{};Ext.applyIf(a,{xtype:"zarafa.passwdpanel",labelWidth:200,defaults:{width:200},border:false,items:[{xtype:"displayfield",name:"username",fieldLabel:dgettext("plugin_passwd","User name")},{xtype:"textfield",name:"current_password",ref:"current_password",fieldLabel:dgettext("plugin_passwd","Current password"),inputType:"password",allowBlank:false,listeners:{change:this.onFieldChange,scope:this}},
{xtype:"ux.passwordmeterfield",name:"new_password",ref:"new_password",allowBlank:false,fieldLabel:dgettext("plugin_passwd","New password"),listeners:{change:this.onFieldChange,scope:this}},{xtype:"ux.passwordmeterfield",name:"new_password_repeat",allowBlank:false,ref:"new_password_repeat",fieldLabel:dgettext("plugin_passwd","Retype new password"),listeners:{change:this.onFieldChange,scope:this}}]});this.addEvents("userchange");Zarafa.plugins.passwd.settings.PasswdPanel.superclass.constructor.apply(this,
arguments);this.on("afterrender",this.initialize,this)},initialize:function(){this.getForm().setValues({username:container.getUser().getUserName()})},onFieldChange:function(){this.fireEvent("userchange",this)}});Ext.reg("zarafa.passwdpanel",Zarafa.plugins.passwd.settings.PasswdPanel);Ext.namespace("Zarafa.plugins.passwd.settings");
Zarafa.plugins.passwd.settings.SettingsPasswdCategory=Ext.extend(Zarafa.settings.ui.SettingsCategory,{constructor:function(a){a=a||{};Ext.applyIf(a,{title:dgettext("plugin_passwd","Change Password"),categoryIndex:9997,iconCls:"zarafa-settings-category-passwd",xtype:"zarafa.settingspasswdcategory",items:[{xtype:"zarafa.settingspasswdwidget",settingsContext:a.settingsContext},container.populateInsertionPoint("context.settings.category.passwd",this)]});Zarafa.plugins.passwd.settings.SettingsPasswdCategory.superclass.constructor.call(this,
a)}});Ext.reg("zarafa.settingspasswdcategory",Zarafa.plugins.passwd.settings.SettingsPasswdCategory);Ext.namespace("Zarafa.plugins.passwd.settings");
Zarafa.plugins.passwd.settings.SettingsPasswdWidget=Ext.extend(Zarafa.settings.ui.SettingsWidget,{constructor:function(a){a=a||{};Ext.applyIf(a,{title:dgettext("plugin_passwd","Change Password"),xtype:"zarafa.settingspasswdwidget",layout:"form",items:[{xtype:"zarafa.passwdpanel",ref:"passwdPanel",listeners:{userchange:this.setModelDirty,scope:this}}]});Zarafa.plugins.passwd.settings.SettingsPasswdWidget.superclass.constructor.call(this,a)},initEvents:function(){Zarafa.plugins.passwd.settings.SettingsPasswdWidget.superclass.initEvents.call(this);
var a=this.settingsContext.getModel();this.mon(a,"beforesavesettings",this.onBeforeSaveSettings,this);this.mon(a,"savesettings",this.onSaveSettings,this);this.mon(a,"discardsettings",this.onDiscardSettings,this)},onBeforeSaveSettings:function(){if(this.passwdPanel.new_password.getValue()!=this.passwdPanel.new_password_repeat.getValue()){Ext.MessageBox.alert(dgettext("plugin_passwd","Error"),dgettext("plugin_passwd","New passwords do not match."));return false}else if(Ext.isEmpty(this.passwdPanel.current_password.getValue())){Ext.MessageBox.alert(dgettext("plugin_passwd",
"Error"),dgettext("plugin_passwd","Current password is empty."));return false}else if(Ext.isEmpty(this.passwdPanel.new_password.getValue())||Ext.isEmpty(this.passwdPanel.new_password_repeat.getValue())){Ext.MessageBox.alert(dgettext("plugin_passwd","Error"),dgettext("plugin_passwd","New password is empty."));return false}else if(this.passwdPanel.getForm().isValid()){if(this.passwdPanel.new_password.getScore()<70){Ext.MessageBox.alert(dgettext("plugin_passwd","Error"),dgettext("plugin_passwd","Password is weak. Password should contain capital, non-capital letters and numbers. Password should have 8 to 20 characters."));
return false}return true}else{Ext.MessageBox.alert(dgettext("plugin_passwd","Error"),dgettext("plugin_passwd","One or more fields does contain errors."));return false}},onSaveSettings:function(){if(this.ownerCt.isVisible()){this.ownerCt.displaySavingMask();var a=this.passwdPanel.getForm().getFieldValues();container.getRequest().singleRequest("passwdmodule","save",a,new Zarafa.plugins.passwd.data.PasswdResponseHandler({callbackFn:function(b){this.ownerCt.hideSavingMask(b);b&&this.passwdPanel.getForm().reset()},
scope:this}))}},onDiscardSettings:function(){this.passwdPanel.getForm().reset()},setModelDirty:function(){var a=this.settingsContext.getModel();a.hasChanges()||a.setDirty()}});Ext.reg("zarafa.settingspasswdwidget",Zarafa.plugins.passwd.settings.SettingsPasswdWidget);
