Ext.SSL_SECURE_URL='/desktop/resources/images/default/s.gif';
Ext.BLANK_IMAGE_URL = '/desktop/resources/images/default/s.gif';

Login = function(){
	var win,
		form,
		submitUrl = '/login/desktop_login_check';
		
	function onSubmit(){
		this.showMask();
		
		form.submit({
			reset: true
		});
	}
	
	return{
		Init:function(){
			Ext.QuickTips.init();
			
		var logoPanel = new Ext.Panel({
				baseCls: 'x-plain',
				id: 'login-logo',
		        region: 'center'
			});
			
			var formPanel = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
		        baseParams: {
		        	module: 'login'
		        },
		        defaults: {
		        	width: 200
		        },
		        defaultType: 'textfield',
		        frame: false,
		        height: 100,
		        id: 'login-form',
		        items: [{	
		            fieldLabel: 'User name',
		            name: 'user',
		            value: ''
		        },{
		            fieldLabel: 'Password',
		            inputType: 'password',
		            name: 'pass',
		            value: ''
		        },{
		            fieldLabel: 'Company Id',
		            // inputType: 'password',
		            name: 'comp',
		            value: ''
		        },
				new Ext.form.Label({html:'<span><a href="http://buduru.net/users/new">Signup</a></span>',id:'signup_label',width:'200px',style:{width:'500px',padding:'0px 30px 0px 10px',color:'maroon',cursor:'pointer'}}),
				new Ext.form.Label({html:'<span onclick="openForgotPassword()">Forgot Password?</span>',id:'forgot_label',style:{width:'500px',padding:'0px 43px',color:'maroon',cursor:'pointer'}}),
				new Ext.form.Label({html:'<span><a href="http://buduru.net" target="_blank">Privacy Policy</a></span>',id:'privacy_label',style:{width:'500px',padding:'0px 20px',color:'maroon',cursor:'pointer'}})
				// new Ext.form.Label({text:'Privacy Policy',id:'privacy_label',style:{width:'500px',padding:'0px 20px',color:'maroon',cursor:'pointer'}})
				],
		        labelWidth:115,
		        listeners: {
					'actioncomplete': {
						fn: this.onActionComplete,
						scope: this
					},
					'actionfailed': {
						fn: this.onActionFailed,
						scope: this
					}
				},
		        region: 'south',
		        url: submitUrl
		    });
		
		win = new Ext.Window({
		        buttons: [{
		        	handler: onSubmit,
		        	scope: Login,
		        	text: 'Login'
		        }],
		        buttonAlign: 'right',
		        closable: false,
		        draggable: false,
		        height: 250,
		        id: 'login-win',
		        keys: {
		        	key: [13], // enter key
			        fn: onSubmit,
			        scope:this
		        },
		        layout: 'border',
		        minHeight: 250,
		        minWidth: 430,
		        plain: false,
		        resizable: false,
		        items: [
		        	logoPanel,
		        	formPanel
		        ],
				title: 'Login',
		        width: 430
		    });
			
			form = formPanel.getForm();
			
			formPanel.on('render', function(){
				var f = form.findField('user');
				
				if(f){
					f.focus();
				}
			}, this, {delay: 200});
			
		    win.show();
					
				

		},
		
		hideMask : function(){
			this.pMask.hide();
			win.buttons[0].enable();
		},
		
		onActionComplete : function(f, a){
			
			this.hideMask();
			if(a && a.result.success==true){
				win.destroy(true);
				
				// get the path
				var path = window.location.pathname,
					path = path.substring(0, path.lastIndexOf('/') + 1);
					
				// set the cookie
				// set_cookie('sessionId', a.result.sessionId, '', path, '', '' );
				// set_cookie('sessionId', a.result.sessionId);
				// redirect the window
				window.location = path;
			}
		},
		
		onActionFailed : function(f,a){
			// alert(a.result.msg)
			this.hideMask();
			if(a && a.result.success==false){
				// win.destroy(true);
				Ext.Msg.show({
				   title:'Message',
				   msg: a.result.msg,
				   buttons: Ext.Msg.OK,
				   icon: Ext.MessageBox.INFO  
				});	
			}
		},
		
		showMask : function(msg){
			if(!this.pMask){
				// using this.pMask, seems that using this.mask caused conflict
		        // when this dialog is modal (uses this.mask also)
		        this.pMask = new Ext.LoadMask(win.body, {
		        	msg: 'Please wait...'
		        });
			}
			
			if(msg){
				this.pMask.msg = msg;
			}
	    	this.pMask.show();
	    	win.buttons[0].disable();
	    }
	};

}();

Ext.onReady(Login.Init, Login, true);

function openForgotPassword(){
	if(Ext.getCmp('forgot_passeord_win')){
	Ext.getCmp('forgot_passeord_win').show();
	return ;
	}
	
	 var logoPanel = new Ext.Panel({
				baseCls: 'x-plain',
				id: 'login-logo1',
		        region: 'center'
			});
		
		var formPanel = new Ext.form.FormPanel({
		        baseCls: 'x-plain',
		        baseParams: {
		        	module: 'login'
		        },
		        defaults: {
		        	width: 200
		        },
		        defaultType: 'textfield',
		        frame: false,
		        height: 120,
		        id: 'login-form1',
		        items: [{	
		            fieldLabel: 'User name',
		            name: 'username',
		            value: ''
		        },{
		            fieldLabel: 'Company Id',
		            name: 'company_id',
		            value: ''
		        },new Ext.form.Label({html:'<p>Your Password will be email to the email address associated with your user account. your companies BuDuRu Administrator can also reset your password for you.</p>'})],
		        labelWidth:115,
		        listeners: {
					'actioncomplete': {
						fn: function(){
									
						
						this.hideMask();
						if(a && a.result.status==true){
							win.destroy(true);
							Ext.Msg.show({
							   title:'Message',
							   msg: a.result.msg,
							   buttons: Ext.Msg.OK,
							   icon: Ext.MessageBox.INFO  
							});	
						}
						if(a && a.result.status==false){
							
							Ext.Msg.show({
							   title:'Message',
							   msg: a.result.msg,
							   buttons: Ext.Msg.OK,
							   icon: Ext.MessageBox.ERROR  
							});	
						}
						
					},
						scope: this
					},
					'actionfailed': {
						fn: function(f,a){
						
						this.hideMask();
						if(a && a.result.status==true){
							win.destroy(true);
							Ext.Msg.show({
							   title:'Message',
							   msg: a.result.msg,
							   buttons: Ext.Msg.OK,
							   icon: Ext.MessageBox.INFO  
							});	
						}
						if(a && a.result.status==false){
							
							Ext.Msg.show({
							   title:'Message',
							   msg: a.result.msg,
							   buttons: Ext.Msg.OK,
							   icon: Ext.MessageBox.ERROR  
							});	
						}
					},
						scope: this
					}
				},
		        region: 'south',
		        url: '/login/desktop_lost_password'
		    });
		
win = new Ext.Window({
		        buttons: [{
		        	handler: function(){
						this.showMask();
						Ext.getCmp('login-form1').getForm().submit({
							reset: true
						});
						},
		        	scope: this,
		            text: 'Send'
		        },{
		        	handler: function(){
								Ext.getCmp('forgot_passeord_win').destroy();						
						},
		        	scope: this,
		            text: 'Cancel'
		        }],
		        buttonAlign: 'right',
		        closable: true,
		        draggable: true,
		        height: 270,
		        id: 'forgot_passeord_win',
		        keys: {
		        	key: [13], // enter key
			        fn: this.onSubmit,
			        scope:this
		        },
		        layout: 'border',
		        minHeight: 250,
		        minWidth: 430,
		        plain: false,
		        resizable: false,
		        items: [
		        	logoPanel,
		        	formPanel
		        ],
				title: 'Forgot Password',
		        width: 430
		    });
			
			form = formPanel.getForm();
			
			formPanel.on('render', function(){
				var f = form.findField('user');
				
				if(f){
					f.focus();
				}
			}, this, {delay: 200});
			
		    win.show();				
		
		this.hideMask = function(){
			this.pMask.hide();
			win.buttons[0].enable();
		}
		
		this.onActionComplete = function(f, a){
			
			this.hideMask();
			if(a && a.result.status==true){
				win.destroy(true);
				Ext.Msg.show({
				   title:'Message',
				   msg: a.result.msg,
				   buttons: Ext.Msg.OK,
				   icon: Ext.MessageBox.INFO  
				});	
			}
		}
		
		this.onActionFailed = function(f,a){
			alert('sss')
			this.hideMask();
		}
		
		this.showMask=function(msg){
			if(!this.pMask){
				// using this.pMask, seems that using this.mask caused conflict
		        // when this dialog is modal (uses this.mask also)
		        this.pMask = new Ext.LoadMask(win.body, {
		        	msg: 'Please wait...'
		        });
			}
			
			if(msg){
				
				this.pMask.msg = msg;
			}
	    	this.pMask.show();
	    	win.buttons[0].disable();
	    }

 this.onSubmit = function(){
		this.showMask();
		
		form.submit({
			reset: true
		});
	
}

}


