
	Ext.QuickTips.init();

	var logout = function() {
		Ext.Ajax.request({
		    url: 'logout.php',		// clear session variables
			success: function(){
				Ext.get('loginline').update('<span id="loginline" style="margin:15px 15px;"><a href="#" onclick="win.show();" style="color:#FFF;">Login</a></span>');
				tl_Grid.getBottomToolbar().disable();
				buzzwashere.allow_grid_selection = false;
			},
		    failure: function(){
		 		Ext.get('loginline').update('Logout failed!');
		    }
		});
	}

	var crtacct = function() {
		Ext.Msg.alert('Create Account','So you want to create a new account, huh?');
	}
	
	var loginButton = {
		xtype: 'button',
		id: 'login-button',
		text: 'Login'		
	}
	loginButton.handler = function() {
		Ext.getCmp('login-form').getForm().submit({
			//method:'POST',
			waitTitle:'Authenticating',
			waitMsg: 'Please wait...',
			params: 'task=login',
			success: function(form, action){
				//var user = Ext.getCmp('login-form').getComponent('loginuser').getValue();
				var user = Ext.getCmp('loginuser').getValue();   
				var thePwd = Ext.getCmp('loginpwd').getValue();   
				//Ext.get('loginline').update('<span style="color:#1B5697;margin-right:15px;">Welcome <b>'+user+'</b>!</span><a onclick="logout();" style="margin-right:10px;color:#FFF;">Logout</a>');
				Ext.get('loginline').update('<span style="color:#FF5;margin-right:15px;">Welcome <b>'+user+'</b>!</span><a href="" onclick="logout();" style="margin-right:10px;color:#FFF;">Logout</a>');
				if (user === 'Buzzman') {
					tl_Grid.getBottomToolbar().enable();
					buzzwashere.allow_grid_selection = true;
				}
				win.hide();
				Ext.Msg.alert('Status', 'Successful Login!', function(btn, text){
					if (btn == 'ok'){
						//var redirect = './';
						//window.location = redirect;
					}
				});
			},
			failure: function(form, action){
				if(action.failureType == 'server'){
					obj = Ext.util.JSON.decode(action.response.responseText);
					Ext.Msg.alert('Login Failed!', obj.errors.reason);
				} else {
					Ext.Msg.alert('Warning!', 'Authentication server is unreachable : ' + action.response.responseText);
				}
				Ext.getCmp('login-form').getForm().reset();
			}
		});
	}

	var cancelButton = {
		xtype: 'button',
		id: 'cancel-button',
		text: 'Cancel'		
	}
	cancelButton.handler = function() {
		win.hide();
	}

	var loginForm = {
		xtype: 'form',
		id: 'login-form',
		bodyStyle: 'padding:15px;background:transparent',
		frame: true,
		border: false,
		url: 'login.php',
		items: [{
					xtype: 'textfield',
					id: 'loginuser',
					name: 'loginuser',
					fieldLabel: 'Username',
					allowBlank: false,
					minLength: 6, maxLength: 64,
					msgTarget: 'under',
					validationEvent: false
				},{
					xtype: 'textfield',
					id: 'loginpwd',
					name: 'loginpwd',
					fieldLabel: 'Password',
					inputType: 'password',
					allowBlank: false,
					minLength: 6, maxLength: 20,
					msgTarget: 'under',
					minLengthText: 'Password must be between 6 and 20 characters long.',
					validationEvent: false
				/*
				},{
					xtype: 'label',
					id: 'crtacct',
					name: 'crtacct',
					fieldLabel: '<a onclick="crtacct();">Create Account</a>',
					labelSeparator: ' '
				*/
				}
		],
		buttons: [ loginButton, cancelButton ]
	}

//Ext.onReady(function() {

	win = new Ext.Window({
		title: 'Log in to BuzzWasHere',
		layout: 'form',
		width: 340,
		autoHeight: true,
		closeAction: 'hide',
		items: [loginForm],
		modal: true
	});
	
	//Ext.getCmp('login-form').focus('', 10);
	//win.show();
	
//});
