Ext.onReady(function(){

	function b(text) {
		return '<b>'+text+'</b>';
	}

	Ext.form.Field.prototype.msgTarget = 'side';


	var sCode = new Ext.form.TextField({
		allowBlank: false,
		fieldLabel: b(recovercontent.securitycode),
		name: 'captcha',
		width:190
	});

	var user_field = new Ext.form.TextField({

		fieldLabel: b(recovercontent.username),
		name: 'data[User][username]',
		width:190,
//		value:'',
		allowBlank: false
	});

	var pass_field = new Ext.form.TextField({
		allowBlank: false,
		fieldLabel: b(recovercontent.password),
		inputType: "password",
		name: 'data[User][password]',
//		value:'',
		width:190
	});

	var re_pass_field = new Ext.form.TextField({
		allowBlank: false,
		fieldLabel: b(recovercontent.passwordrepeat),
		inputType: "password",
		name: 'data[User][re_password]',
//		value:'',
		width:190
	});

  	var register = new Ext.form.FormPanel({
		labelWidth: 190, // label settings here cascade unless overridden
		url:'/users/recover',
		labelAlign:'right',
		border:false,
		items:[user_field,pass_field,re_pass_field,


		{
			xtype: 'box',
			autoEl: {id: 'icon_div', tag: 'div', html: '<table style="margin-bottom:15px"><tr><td width="190"></td><td align="left"><img src="/users/capcha?rand='+Math.random()+'"/></td>'}
		},sCode
		],
		buttons: [{
	            text: recovercontent.go,
	            handler: function() {
	                // check form value
	                if (register.form.isValid()) {
		 		        register.form.submit({
				            waitMsg:'Processing',
				            failure: function(form, action) {
				            	if (action.result.errorInfo=='captcha'){
									Ext.MessageBox.alert('Error Message', recovercontent.securitycodeerror);
									document.getElementById('icon_div').innerHTML='<table style="margin-bottom:15px"><tr><td width="190"></td><td align="left"><img src="/users/capcha?rand='+Math.random()+'"/></td>';
									register.doLayout();
									sCode.setValue('');
				            	} else {
						    		Ext.MessageBox.alert('Error Message', action.result.errorInfo);
				            	}
							},
							success: function(form, action) {
//								alert(action.result.info);
								Ext.Msg.prompt(recovercontent.securityquestion, action.result.info, function(btn, text){
								    if (btn == 'ok'){
								        Ext.Ajax.request({
											url: '/users/changePass',
											params: {
												username: user_field.getValue(),
												password: pass_field.getValue(),
												captcha: sCode.getValue(),
												answer:text

											},
											scope: this,
											method:'POST',
											success: function(response, options) {
												ok=Ext.decode(response.responseText).success;
												if (ok=='1'){
													Ext.Msg.alert('Success', recovercontent.success,function(){
														window.location='/home';
													});
												} else {
													Ext.Msg.alert('Error', recovercontent.error);
													document.getElementById('icon_div').innerHTML='<table style="margin-bottom:15px"><tr><td width="190"></td><td align="left"><img src="/users/capcha?rand='+Math.random()+'"/></td>';
													register.doLayout();
													sCode.setValue('');
												}

											}
								        });
								    }
								    if (btn == 'cancel'){
								    	document.getElementById('icon_div').innerHTML='<table style="margin-bottom:15px"><tr><td width="190"></td><td align="left"><img src="/users/capcha?rand='+Math.random()+'"/></td>';
										register.doLayout();
										sCode.setValue('');
								        // process text value and close...
								    }
								});

	//						    Ext.MessageBox.alert('Record Saved', action.result.info);
							//window.location='../home';

							}
				        });
	                } else {
						Ext.MessageBox.alert('Error(s) Encounterd', 'Please fix the errors noted.');
					}
		        }
	        }],
		defaults:{border:false}
	});

	register.render('register-ct');
	var registerForm = Ext.get('register-dlg');

	registerForm.fadeIn({duration:0.1});

});