Ext.ns('lbgm.login');

lbgm.login.form = function(config) {
    Ext.apply(this, {items:[{
                        fieldLabel:'Username',
                        name:'loginUsername',
                        allowBlank:false,
                        anchor: '100%'
                    },{
                        fieldLabel:'Password',
                        name:'loginPassword',
                        inputType:'password',
                        allowBlank:false,
                        anchor: '100%'
                    },{
                        fieldLabel:'Company',
                        name:'loginCompany',
                        allowBlank:true,
                        anchor: '100%'
                    }]},
           config);
    //this.contentEl=this.map.div;
    lbgm.login.form.superclass.constructor.call(this);
}

Ext.extend(lbgm.login.form, Ext.FormPanel, {
    id: 'loginform',
    labelWidth:80,
    frame:true,
    defaultType:'textfield',
    xtype: 'form',
    monitorValid:true,
    

    // All the magic happens after the user clicks the button
    buttons:[{
            text:'Login',
            formBind: true,
            scope: this,
            // Function that fires when user clicks the button
            handler: function(){
                Ext.getCmp('loginform').form.submit({
                    url:'src/php/conn/login.conn.php?action=login&key=' + lbgm.layout.key,
                    method:'POST',
                    waitTitle:'Connecting',
                    waitMsg:'Sending data...',

                // Functions that fire (success or failure) when the server responds.
                // The one that executes is determined by the
                // response that comes from login.asp as seen below. The server would
                // actually respond with valid JSON,
                // something like: response.write "{ success: true}" or
                // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}"
                // depending on the logic contained within your server script.
                // If a success occurs, the user is notified with an alert messagebox,
                // and when they click "OK", they are redirected to whatever page
                // you define as redirect.

                    success:function(){
                        //Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
                        lbgm.login.wnd.hide();

                        /*
                        var loginbtn = Ext.getCmp('loginbtn');
                        loginbtn.setText("Logout");
                        var addpoibtn = Ext.getCmp('addpoibtn');
                        addpoibtn.setDisabled(false);
                        */
                       //lbgm.maplets.poitree.tree.reload();
                       lbgm.login.checkloggedin();
                    },

                // Failure function, see comment above re: success and failure.
                // You can see here, if login fails, it throws a messagebox
                // at the user telling him / her as much.

                    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('loginform').form.reset();
                    }
                });
            }
        }]
});

Ext.reg('lbgm.login.form', lbgm.login.form);

lbgm.login.wnd = new Ext.Window({
   id: 'loginWnd',
   title: 'Map Login',
   closable: true,
   closeAction: 'hide',
   width:270,
   height:160,
   modal: true,
   items: [lbgm.login.form]
});

lbgm.login.logout = function() {
    Ext.Msg.show({
       title:'Logout?',
       msg: 'Are you sure you wish to logout?',
       buttons: Ext.Msg.YESNO,
       fn: function(buttonid) {
           if(buttonid == 'yes' ) {
                // AJAX to load layout
                var conn = new Ext.data.Connection();
                conn.request({
                    url: 'src/php/conn/login.conn.php?action=logout&key=' + lbgm.layout.key,
                    method: 'POST',
                    success: function(responseObject) {
                        var loginbtn = Ext.getCmp('loginbtn');
                        loginbtn.setText("Login");
                        var addpoibtn = Ext.getCmp('addpoibtn');
                        addpoibtn.setDisabled(true);
                        addpoibtn.setVisible(false);
                        if(lbgm.point.adding) lbgm.point.addCancel();

                        lbgm.maplets.poitree.tree.reload();
                    },
                    failure: function() {
                        Ext.Msg.alert(lbgm.optios.appname, 'Unable to logout.');
                    }
                });
           }
       },
       icon: Ext.MessageBox.QUESTION
    });
    
}

lbgm.login.checkloggedin = function() {
    // AJAX to load layout
    var conn = new Ext.data.Connection();
    conn.request({
        url: 'src/php/conn/login.conn.php?action=isLogin',
        method: 'POST',
        success: function(responseObject) {
            obj = Ext.util.JSON.decode(responseObject.responseText);

            lbgm.login.isLoggedIn = obj.isLoggedIn;
            lbgm.login.allowAdd = obj.allowAdd;
            lbgm.login.allowAccept = obj.allowAccept;
            lbgm.login.userid = obj.userid;
            lbgm.login.superuser = obj.superuser;
            lbgm.login.companyid = obj.companyid;
            lbgm.login.accessLevel = obj.accessLevel;

            var loginbtn = Ext.getCmp('loginbtn');
            var addpoibtn = Ext.getCmp('addpoibtn');
            
            if(obj.isLoggedIn == 1) {
                if(loginbtn!=undefined) loginbtn.setText("Logout");
                if(addpoibtn!=undefined && obj.allowAdd!=0) {
                    addpoibtn.setVisible(true);
                    addpoibtn.setDisabled(false);
                }
            }
            else {
                if(loginbtn!=undefined) loginbtn.setText("Login");
                if(addpoibtn!=undefined) {
                    addpoibtn.setVisible(false);
                    addpoibtn.setDisabled(true);
                    addpoibtn.hidden = true;
                }

                lbgm.point.addPoint = false;
            }
            lbgm.maplets.poitree.key = lbgm.layout.key;
            lbgm.maplets.poitree.tree.reload();
        },
        failure: function() {
            Ext.Msg.alert(lbgm.optios.appname, 'Unable to logout.');
        }
    });

    return  false;
}

