var RegisterDialog = function(){
    var dialog;
    var form_register;
    
    return {
    	show: function () {
	 			dialog = this.dialog;
	 			if(!dialog){
			    dialog = new Ext.BasicDialog("register-dlg", { 
			      modal: false, width:350, height:320, shadow:false, draggable: true, resizable: false, collapsible: false, autoTabs:true, shadow: false
			    });
			    dialog.addKeyListener(27, dialog.hide, dialog);
			    RegisterDialog.initDialog(dialog);
				}
				//dialog.show(ChatLayout.registerBtn.getEl().dom);
				dialog.show();
				this.dialog = dialog;
	 		},
	 		
	 		initDialog: function (dialog) {
	 			
	 			var currentNick = "";
	 			if (!CurrentUser.IsTempNick)
	 				currentNick = CurrentUser.Nick;
	 			
	 			// Form Login
	 			form_register = new Ext.form.Form({labelAlign: 'top', buttonAlign: 'left', url: '/pages/channel/_json.register.php'});
	 			Ext.form.Field.prototype.msgTarget = 'side';
	 			var labelWidth = 260;
	 			var field_email = new Ext.form.TextField({allowBlank:false, vtype: 'email', fieldLabel: locStrings["email"], name: 'email',   width:labelWidth, required: true});
	 			var nickConfig = {allowBlank:false, fieldLabel: locStrings["nick"], name: 'nick',   width:labelWidth, required: true};
	 			if (currentNick != null && currentNick != "")
	 				nickConfig.value = currentNick;
	 				var field_nick = new Ext.form.TextField(nickConfig);
	 			//var field_fullname = new Ext.form.TextField({allowBlank:false, fieldLabel: locStrings["full_name"], name: 'full_name',   width:labelWidth, required: true});
	 			var field_password = new Ext.form.TextField({fieldLabel: locStrings["enter_password"], allowBlank:false, name: 'password',   width:labelWidth, required: true, inputType: "password"});
	 			var field_confirm_password = new Ext.form.TextField({fieldLabel: locStrings["confirm_password"], allowBlank:false, name: 'confirm_password',   width:labelWidth, required: true, inputType: "password"});
	 			form_register.add(field_email, field_nick, field_password, field_confirm_password);
	 			form_register.addButton({text:locStrings["register_btn"], type: "submit"}, RegisterDialog.tryLogin, form_register);
	 			form_register.addButton(locStrings["cancel_btn"], dialog.hide, dialog);
	 			form_register.render ("form-register");
	 			
	 		},
	 			
	 		tryLogin: function () {
	 			if (form_register.isValid()) {
		        form_register.submit({
		        	params: {action: 'submit', channelId: ChatApp.getCurrentChat().id},
		        	waitMsg: locStrings["register_dlg_waiting_msg"],
		        	success: function (form, action) {
		        		if(action.result.user.IsLogined) {
		        			Ext.MessageBox.alert(locStrings["register_dlg_registred"],  locStrings["register_dlg_hellomsg"]);
		        			CurrentUser.loadFromRow(action.result.user);
		        			dialog.hide ();
		        			ChatLayout.entered ();
		        		} else {
		        			Ext.MessageBox.alert(locStrings["error_title"], "Sorry, unknown error");
		        		}
		        	},
		        	failure: function (form, action) {
		        		Ext.MessageBox.alert(locStrings["error_title"], action.result.errors[0].message);
		        	}
		        });
		    }else{
		        Ext.MessageBox.alert(locStrings["error_title"], locStrings["error_need_fields"]);
		    }
	 		}
	 	}
}();