// JavaScript Document
edesk.panels.LoginPanel = Ext.extend(Ext.Panel, {
	layout: 'fit',
	fullscreen: true,
	initComponent: function(){
		this.txtlogin=new Ext.form.Text({
			name: 'txtlogin',
			placeHolder: 'enter login here',
			label: 'Login'
		});
		this.txtpassword=new Ext.form.Password({
			name: 'txtpassword',
			placeHolder: 'enter password here',
			label: 'Password'
		});
		this.chkRememberMe=new Ext.form.Checkbox({
			name: 'chkRememberMe',
			checked: true,
			label: 'Remember Me'
		});

		this.dockedItems= [
			{
				dock : 'top',
				xtype: 'toolbar',
				title: app.title
			}
		];
		this.items=[{
				padding:10,
				xtype: 'fieldset',
				title: 'Secure Login',
				instructions: 'Please enter your login and password above.',
				defaults: {
					required: true,
					labelAlign: 'left',
					labelWidth: '40%'
				},
				items: [this.txtlogin,this.txtpassword,this.chkRememberMe,{
				xtype: 'button',
				text: 'Login',
				ui: 'action',
				margin: '20 100 20 100',
				scope: this,
				handler: function() {
					this.doLogin();
				}
			}]
		}];
		edesk.panels.LoginPanel.superclass.initComponent.call(this);
	},
	doLogin:function(){
		if(this.chkRememberMe.checked)
		{
			if(app.loginstore.getCount()>0)
			{
				app.loginstore.data.items[0].set("login",this.txtlogin.getValue());
				app.loginstore.data.items[0].set("password",this.txtpassword.getValue());
			}
			else
			{
				app.loginstore.add({login:this.txtlogin.getValue(), password:this.txtpassword.getValue()});
			}
		}
		else
		{
			if(app.loginstore.getCount()>0)
				app.loginstore.remove(app.loginstore.data.items[0]);
		}
		//finally, save our Search data to localStorage
		app.loginstore.sync();		
		app.loggedinas=this.txtlogin.getValue();

		Ext.Ajax.request({
			url: '/api/login.asp',
			params: {'txtlogin': this.txtlogin.getValue(),'txtPassword':this.txtpassword.getValue()},
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.success){
//					app.viewport=new edesk.panels.Viewport();
//					app.loginpanel.hide();
					window.location.href='/v1/viewport.htm';
				}
				else{
					app.loggedinas="";
					Ext.Msg.alert('Login Fail',obj.errmsg, Ext.emptyFn);
				}
			},
			failure: function(response, opts) {
				app.loggedinas="";
				Ext.Msg.alert('Login Fail','Failure communicating with the server. Please try again or contact Customer support if issue persists.', Ext.emptyFn);
			}
		});
	},
	autoLogin:function(login,password){
		app.loggedinas=login;

		Ext.Ajax.request({
			url: '/api/login.asp',
			params: {'txtlogin': login,'txtPassword':password},
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.success){
//					app.viewport=new edesk.panels.Viewport();
//					app.loginpanel.hide();
					window.location.href='/v1/viewport.htm';
				}
				else{
					app.loggedinas="";
					Ext.Msg.alert('Login Fail',obj.errmsg, Ext.emptyFn);
				}
			},
			failure: function(response, opts) {
				app.loggedinas="";
				Ext.Msg.alert('Login Fail','Failure communicating with the server. Please try again or contact Customer support if issue persists.', Ext.emptyFn);
			}
		});
	}
});

