﻿/// <summary>
/// user edit dialog
/// </summary>
///
/// <remarks>
/// <CodeWalkthroughHistory>
/// Reviewed By				Date Reviewed		Checklist Status
/// </CodeWalkthroughHistory> 
/// <RevisionHistory>
/// Author					Date				Description
/// Sergey Starikov	01/01/2008	
/// </RevisionHistory>
/// </remarks>


function UserSettingsDialog(sender, rec, isRegistration, aftersubmit){
var win;




id = new  Ext.form.Hidden({
  name:"id"
});

actionType = new Ext.form.Hidden({
	name: "actionType"
});

userName = new  Ext.form.TextField({
  fieldLabel:"User name",
  allowBlank:false,
  name:"name",
  vtype:'alphanum'
});

fullName = new  Ext.form.TextField({
	fieldLabel:"Full name",
  allowBlank:false,
	name:"fullName",
	vtype: 'fullnameValidator'
});

email = new  Ext.form.TextField({
    fieldLabel:"Email",
	  allowBlank:false,
    name:"email",
	vtype:'email'
});

description = new  Ext.form.TextField({
	fieldLabel:"Description",
  name:"description"
});

password = new  Ext.form.TextField({
        fieldLabel:"Password",
        name: "txtPassword",
        inputType:'password',
        id:'pass'
});

confirmPassword = new  Ext.form.TextField({
        fieldLabel:"Confirm password",
        name:"txtConfirmPassword",
        inputType:'password',
				vtype: 'password',
        initialPassField: 'pass' 
});

chkNewPassword = new  Ext.form.Checkbox({
        fieldLabel:"",
		    labelSeparator : '',
        boxLabel:"New Password",
        name:"isNewPassword",
        inputValue: "true"
});

chkPasswordNeverExpires = new  Ext.form.Checkbox({
        fieldLabel:"",
		    labelSeparator : '',
        boxLabel:"Password never expires",
        name:"passwordNeverExpires",
        inputValue: "true"
});

chkUserCantChangePassword = new  Ext.form.Checkbox({
        fieldLabel:"",
		    labelSeparator : '',
		    boxLabel: isRegistration ? "" : "User cannot change password",
        name: "userCantChangePassword",
        hidden: isRegistration,
        inputValue: "true"
});

chkUserMustChangePassword = new  Ext.form.Checkbox({
        fieldLabel:"",
		    labelSeparator : '',
        boxLabel:isRegistration?"":"User must change password at next login",
        name: "userMustChangePassword",
        hidden: isRegistration,
        inputValue:"true"
});

chkAccIsDissabled = new  Ext.form.Checkbox({
		    labelSeparator : '',
        fieldLabel:"",
        boxLabel:"Account is dissabled",
        name:"accIsDissabled",
        inputValue: "true"
});

chkIsPathAdministrator = new  Ext.form.Checkbox({
		    labelSeparator : '',
        fieldLabel:"",
        boxLabel:"Is Path Administrator",
        name:"isPathAdministrator",
        inputValue: "true"
});



chkUserMustChangePassword.on('check', onUserPasswordSettings);
chkUserCantChangePassword.on('check', onUserPasswordSettings);

function onUserPasswordSettings(checkbox, checked)
{
	if(checkbox == chkUserMustChangePassword)
	{
		if(checked == true)	
		{
			chkUserCantChangePassword.setDisabled(true);
		}
		else
		{
			chkUserCantChangePassword.setDisabled(false);
		}
	}
	else
	{
		if(checked == true)	
		{
			chkUserMustChangePassword.setDisabled(true);
		}
		else
		{
			chkUserMustChangePassword.setDisabled(false);
		}
	}
}



var defButtons =  new Ext.bb.DefaultWindowButtons({
									url: 'WebServices/DataProcess.ashx'
								});

defButtons.on('aftersubmit', function () {
											if(aftersubmit!=undefined && aftersubmit!=null);
												aftersubmit(sender);
										});

defButtons.on('beforeok', function (plugin, form, action) {
					if (chkNewPassword.getValue() == true) {
						if (password.getValue() != confirmPassword.getValue()) {
							Ext.MessageBox.alert('Error!', 'Passwords are not identical!');
							return false;
						}
					}
				else {
//					form.markInvalid();
//					return false;
				}

				chkNewPassword.setDisabled(false);

				return true;
			});
			
        // create the window on the first click and reuse on subsequent clicks
win = new Ext.Window({
	modal: true
								, stateful: false
								, layout: 'fit'
								, closable: true
								, loadMask: true
                , width: 500
                , height: 500
								, resizable: false

  							, title: rec != null ? "User settings" : "New User"
	///*								
								, plugins: defButtons
	//	*/							
                , items: new Ext.FormPanel({
		labelWidth: 150, // label settings here cascade unless overridden
		url: 'WebServices/DataProcess.ashx',
		method: 'POST',

		frame: false,
		bodyStyle: 'padding:5px 5px 0',
		width: 500,

		items: [{
			xtype: "fieldset",
			title: "User Information",
			autoHeight: true,
			items: [
			actionType,
			id,
			userName,
			fullName,
			email,
			description
      ]
		}, {
			xtype: "fieldset",
			title: "Password Settings",
			autoHeight: true,
			items: [
			chkNewPassword,
      password,
      confirmPassword,
//      chkPasswordNeverExpires,
      chkUserCantChangePassword,
      chkUserMustChangePassword
     ]
		}, {
			xtype: "fieldset",
			title: "Acount Settings",
			autoHeight: true,
			hidden: isRegistration,
			items: [
			chkAccIsDissabled,
			chkIsPathAdministrator
		]
}]
			/*
			,buttons: [{
			text: 'Ok'
			//           ,handler: userFormDataEval,
			,type:'submit'
			},{
			text: 'Cancel'
			//            ,
			//            handeler: function(){ win.reset(); }
			}]
			//*/
		})


	});

	if (rec != null && rec != undefined) {

		//		if(rec!=null)
		//			userName.setDisabled(true);


		id.setValue(rec.data.Id);
		userName.setValue(rec.data.Name);
		fullName.setValue(rec.data.FullName);
		email.setValue(rec.data.Email);
		description.setValue(rec.data.Description);

		chkAccIsDissabled.setValue(rec.data.AccountIsDisabled);
		chkIsPathAdministrator.setValue(rec.data.IsPathAdmin);

		//		chkPasswordNeverExpires.setValue(rec.data.PasswordNeverExpired);
		chkUserMustChangePassword.setValue(rec.data.UserMustChangePassword);
		chkUserCantChangePassword.setValue(rec.data.UserCantChangePassword);
		actionType.setValue('user_edit');
	} else {
		if (isRegistration) {
			actionType.setValue('user_register');
		} else {
			actionType.setValue('user_add');
		}
		chkUserMustChangePassword.setValue(true);
		chkNewPassword.setValue(true);
		chkNewPassword.setDisabled(true);
	}

//	win.on('actioncomplete', user_register_complete);
//  win.on('actionfailed', function(){  Ext.MessageBox.alert('Erorr!', 'Erorr form validating');
    
	win.show();
/*
	userFormDataEval : function ()
	{
    if (win.isValid() == true)
    {
        win.submit();
    }
    else
		{
			win.markInvalid();
      Ext.MessageBox.alert('Error!', 'Erorr form validating!');
    }
  }
  */
}


