Ext.onReady(function(){

function handleLogin(){

		username = Ext.getCmp('username').getValue();
        password = Ext.getCmp('password').getValue();

        var conn = new Ext.data.Connection();
        conn.request({
        url: 'index.php?action=login',
        method: 'POST',
        params: {"action": "login", "username": username, "password": password},
        success: function(responseObject) {
                result = responseObject.responseText;
                if (result == 'false'){
                        Ext.Msg.alert('Status', 'Username or password incorrect');

                        //window.location = '/index.php?page=admin';
				}
                if (result == 'true'){
                        window.location = '/index.php?page=admin';
                }

        },
        failure: function() {
        Ext.Msg.alert('Status', 'Unable to login at this time.');
        }
        });



}

var userLoginPanel = new Ext.FormPanel({
			id: 'userLoginPanel',
			renderTo: 'main',	
            height: 200,
            width: 280,
			region: 'center',
			buttonAlign: 'right',
            defaults: {width: 130},
            defaultType: 'textfield',
            buttons: [{text: 'Log In',handler: function(){	handleLogin();}}],
			title: 'Log in to your Account',
			style: 'margin: 10 0 0 10',
            border: true,
           	items: [
            	{
					id: 'username',
					fieldLabel: 'User Name',
					name: 'username',
					itemCls: 'fieldOffset'
                    },{
		    		id: 'password',
                    fieldLabel: 'Password',
					itemCls: 'fieldOffset',
                    name: 'password',
		    		inputType: 'password'
                }
                
            ]


});



var nav = new Ext.KeyNav(userLoginPanel.getForm().getEl(), {
  'enter': function(e) {
    handleLogin();
  }
});

});

