/**
 * @author bumz
 */

var activation_win = null;

var resend_activation_form =  new Ext.FormPanel({
	labelWidth: 110,
	width: 360,
	xtype:'fieldset',
    style: 'border:none;padding:12px;',
    title: '',
	ctCls: 'no_form_bck',
    autoHeight:true,
	items: [{
		layout:'column',
		items:[{
			columnWidth:1,
			layout: 'form',
			items: [{
				fieldLabel: langlines.main_email_address,
				name: 'resend_activation',
				id: 'resend_activation',
				width: '250px',
				allowBlank:false,
				xtype: 'textfield',
				minLength: 1,
				maxLength: 250,
				vtype: 'email'
			}]
		}]
	}]
});

bind_resend = function() {
	
	
	Ext.select('a[rel="resend_activation"]').on('click', function(){
		
		if(!activation_win){
            activation_win = new Ext.Window({
                layout      : 'fit',
                width       : 440,
                height      : 150,
                closeAction :'hide',
                plain       : true,
                items       : resend_activation_form,
				title		: langlines.user_resend_window_text,
                buttons: [{
                    text     : langlines.main_ok,
					cls: 'btn_lowercase',
					handler: function(){
						if (resend_activation_form.getForm().isValid()) {
							Ext.MessageBox.show({
								msg: langlines.user_resend_sending,
								progressText: langlines.main_please_wait,
								width: 300,
								wait: true,
								waitConfig: {
									interval: 200
								},
								icon: 'download_icon'
							});
							
							Ext.Ajax.request({
								url: base_url + 'user/ajax_resend_activation',
								method: 'POST',
								success: function(responseObject){
									Ext.MessageBox.hide();
									var resultInfo = Ext.util.JSON.decode(responseObject.responseText);
									if (resultInfo.status) {
										resend_activation_form.getForm().reset();
										activation_win.hide();
										showInfoMessage(resultInfo.message);
									}
									else {
										showWarningMessage(resultInfo.message);
									}
								},
								failure: function(){
									Ext.MessageBox.hide();
									showErrorMessage(langlines.main_server_error);
								},
								params: {
									'email': Ext.getCmp('resend_activation').getValue()
								}
							});
						}
					}
                },{
                    text     : langlines.main_close,
					cls		 : 'btn_lowercase',
                    handler  : function(){
                        activation_win.hide();
                    }
                }]
            });
        }
		
		activation_win.show();
		
		return false;
	});
};

Ext.onReady(function(){
	
	Ext.QuickTips.init();
	
	// turn on validation errors beside the field globally
	Ext.form.Field.prototype.msgTarget = 'side';
	
	var email = 
		new Ext.form.TextField({
			fieldLabel: '<span class="settings_notice">' + langlines.main_email_address + '</span>',
			name: 'email',
			id: 'email',
			vtype: 'email',
			width: '230px',
			allowBlank:false,
			listeners:{
		        specialkey:function(f,o){
		            if(o.getKey()==13){
		                $('#loginButton').click();
		            }
		        }
		    }
	});
	
	var pass = 
		new Ext.form.TextField({
			fieldLabel: '<span class="settings_notice">' + langlines.user_password + '</span>',
			name: 'password',
			id: 'password',
			width: '230px',
			xtype: 'password',
			inputType: 'password',
			allowBlank:false,
			listeners:{
		        specialkey:function(f,o){
		            if(o.getKey()==13){
		                $('#loginButton').click();
		            }
		        }
		    }
	});
	
	var reset_pass_form =  new Ext.FormPanel({
		labelWidth: 110,
		width: 360,
		xtype:'fieldset',
        style: 'border:none;padding:12px;',
        title: '',
		ctCls: 'no_form_bck',
        autoHeight:true,
		items: [{
			layout:'column',
			items:[{
				columnWidth:1,
				layout: 'form',
				items: [{
					fieldLabel: langlines.main_email_address,
					name: 'reset_email',
					id: 'reset_email',
					width: '250px',
					allowBlank:false,
					xtype: 'textfield',
					minLength: 1,
					maxLength: 250,
					vtype: 'email'
				}]
			}]
		}]
	});
	
	var simple = new Ext.FormPanel({
		//renderTo: 'login_screen',
		labelWidth: 95, // label settings here cascade unless overridden
		url:'user/login',
		frame:true,
		xtype:'fieldset',
        style: 'border:#dddddd solid 1px; ',
        title: langlines.user_signin,
		labelAlign: 'right',
        autoHeight:true,
		width: 755,
		
		items: [{
			layout: 'column',
			items: [{
				columnWidth:.48,
				layout: 'form',
				items: [{
					html: '<div style="padding-top:20px; padding-right:10px;border-right:1px solid #ccc;"><iframe src="https://' + rpx_domain + '/openid/embed?token_url=' + rpx_token_url + '&language_preference=' + rpx_language + '" \
  							scrolling="no" frameBorder="no" style="width:330px;height:240px;"></iframe>'
				}]
			},{
				columnWidth:.52,
				layout: 'form',
				items: [{
					layout:'form',
						items:[{
							html: '<div class="sign_with_linktive" style="padding: 20px 15px 8px;">' + langlines.user_signin_with_linktive + '</div>'
						}]
					},{
					layout:'form',
					items:[ email ]
					},{
					layout:'form',
					items:[ pass ]
					},
					{
						layout: 'form',
						bodyStyle: (Ext.isIE?'padding-right: 22px;':''),
						items: [new Ext.Button({
							text: langlines.user_login,
							id: 'loginButton',
							style: 'text-transform: uppercase;font-weight:bold;padding-right: 25px;',
							align: 'right',
							handler: function(){
								if(email.isValid() && pass.isValid()){
									Ext.MessageBox.show({
										msg: langlines.user_logging_in,
										progressText: langlines.main_please_wait,
										width:300,
										wait:true,
										waitConfig: {interval: 200},
										icon:'download_icon'
									});        
									
									Ext.Ajax.request({
										url: base_url + 'user/log_in',
										method: 'POST',
										success: function(responseObject){
											Ext.MessageBox.hide();
											var resultInfo = Ext.util.JSON.decode(responseObject.responseText);
											if(resultInfo.status)
											{
												showRedirecting();
												simple.getForm().reset();
												window.location.href = base_url + 'dashboard';
											}
											else
											{
												showWarningMessage(resultInfo.message);
												if (resultInfo.why == 'inactive') {
													bind_resend();
												}
											}
										},
										failure: function(){
											Ext.MessageBox.hide();
											showErrorMessage(langlines.main_server_error);                                                        
										},
										params: {
											email: $('#email').val(),
											password: $('#password').val()
										}
									});                                        
								}
								else
								{
									showErrorMessage(langlines.main_incorrect_email);                                
								}
							}
						})]
					},{
						layout: 'form',
						items: [{
								html: '<a href="#" rel="remember_password">' + langlines.user_forgot_pass + '</a>',
								style: 'text-align: right; padding-top: 30px'
							}
						]
					}]
			}]
		}]
		
		//}]
	});
	
	simple.on('render', function(){
		
	});
	
	simple.render('login_screen');
	
	var remember_win = null;
	Ext.select('a[rel="remember_password"]').on('click', function(){
		
		if(!remember_win){
            remember_win = new Ext.Window({
                layout      : 'fit',
                width       : 440,
                height      : 150,
                closeAction :'hide',
                plain       : true,
                items       : reset_pass_form,
				title		: langlines.user_restore_pass,
                buttons: [{
                    text     : langlines.main_ok,
					cls: 'btn_lowercase',
					handler: function(){
						if (reset_pass_form.getForm().isValid()) {
							Ext.MessageBox.show({
								msg: langlines.user_forgot_sending,
								progressText: langlines.main_please_wait,
								width: 300,
								wait: true,
								waitConfig: {
									interval: 200
								},
								icon: 'download_icon'
							});
							
							Ext.Ajax.request({
								url: base_url + 'user/ajax_restore_password',
								method: 'POST',
								success: function(responseObject){
									Ext.MessageBox.hide();
									var resultInfo = Ext.util.JSON.decode(responseObject.responseText);
									if (resultInfo.status) {
										reset_pass_form.getForm().reset();
										remember_win.hide();
										showInfoMessage(resultInfo.message);
									}
									else {
										showWarningMessage(resultInfo.message);
									}
								},
								failure: function(){
									Ext.MessageBox.hide();
									showErrorMessage(langlines.main_server_error);
								},
								params: {
									'email': Ext.getCmp('reset_email').getValue()
								}
							});
						}
					}
                },{
                    text     : langlines.main_close,
					cls		 : 'btn_lowercase',
                    handler  : function(){
                        remember_win.hide();
                    }
                }]
            });
        }
		
		remember_win.show();
		
		return false;
	});
	
	
	

	/*$('#login_screen').append('<div class="login_notice"><span>' + langlines.user_dont_have_acc + '</span>&nbsp;&nbsp;<a href="' + base_url + 'user/register">' +
		langlines.user_register + '</a></div>');*/
});