  function userLoginAs(noReload, forceLastUser, specialLogin){

    var loginMsg = 'Login as another user';
    if (specialLogin)
      loginMsg = 'IES Special Login';
    globalNoReload = noReload;
    
    if (!specialLogin && !currentUser.canUseLoginAs)
      return false;
      
    var infoBox = {region: 'north', baseCls: '', cls: 'dialogHelpTitle', border:false, height: 48, html: '<strong style="font-size: 13px; font-weight: bold">'+loginMsg+'...</strong>'};

    var tpl = new Ext.XTemplate(
      '<tpl for=".">',
        '<div class="thumb-wrap',
          '<tpl if="disabled == 1"> thumb-disabled</tpl>',
            '" id="{fullname}">',
          '<div class="thumb"><img src="{image}" title="{username}"></div>',
          '<tpl if="Ext.util.Format.trim(fullname) != \'\'">',
            '<span class="x-editable">{fullname}</span>',
          '</tpl>',
          '<tpl if="Ext.util.Format.trim(fullname) == \'\'">',
            '<span class="x-editable">{username}</span>',
          '</tpl>',
        '</div>',
      '</tpl>',
      '<div class="x-clear"></div>'
    );

   var userDataView = new Ext.data.GroupingStore({
      reader : new Ext.data.JsonReader({
          root : 'users'
        },[
          {name: 'id', mapping: 'id'},
          {name: 'username', mapping: 'username'},
          {name: 'fullname', mapping: 'fullname'},
          {name: 'disabled', mapping: 'disabled', type: 'bool'},
          {name: 'email', mapping: 'email'},
          {name: 'position', mapping: 'position'},
          {name: 'type', mapping: 'type'},
          {name: 'image', mapping: 'image'}
        ]
      )
    });
    var userListView = new Ext.data.GroupingStore({
      reader : new Ext.data.JsonReader({
          root : 'users'
        },[
          {name: 'id', mapping: 'id'},
          {name: 'username', mapping: 'username'},
          {name: 'fullname', mapping: 'fullname'},
          {name: 'disabled', mapping: 'disabled', type: 'bool'},
          {name: 'email', mapping: 'email'},
          {name: 'position', mapping: 'position'},
          {name: 'type', mapping: 'type'},
          {name: 'image', mapping: 'image'}
        ]
      )
    });
    var store2 = new Ext.data.Store({
      autoLoad: true,
      url: '/ajax/login/getUsers.cfm',
      reader: new Ext.data.JsonReader({},[]),
      listeners: {
        load: function(ds){
          userDataView.loadData(store2.reader.jsonData);
          userListView.loadData(store2.reader.jsonData);
        }
      }
    });
    
    var myDataView = new Ext.DataView({
                                        store: userDataView,
                                        tpl: tpl,
                                        autoWidth:true,
                                        singleSelect: true,
                                        overClass: "x-view-over",
                                        itemSelector: "div.thumb-wrap",
                                        emptyText: 'No users to display',           
                                        listeners: {
                                          click: {
                                            fn: function(dv,i,n,e){
                                              selectedUserID = dv.getStore().getAt(i).get('id');
                                              loginWinIES.loginBtn.enable();
                                            }
                                          },
                                          dblclick: {
                                            fn: function(dv,i,n,e){
                                              selectedUserID = dv.getStore().getAt(i).get('id');
                                              loginWinIES.loginBtn.handler();
                                            }
                                          }
                                        }
                                      });
                                      
    var selectedUserID=0;
    var myListView = new Ext.grid.GridPanel({
                                            store: userListView,
                                            border: false,
                                            region: 'center',
                                            stripeRows: true,
                                            stateful: true,
                                            stateId: 'userLoginAs',
                                            emptyText: 'No users to display',
                                            columns: [{
                                                header: '',
                                                width: 50,
                                                tpl: 'test',
                                                dataIndex: 'image',
                                                renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                                                  return '<img src="'+value+'" width="24" height="24" />';
                                                }
                                            },{
                                                header: 'Display Name',
                                                width: 150,
                                                bodyStyle: 'padding: 5px 0',
                                                sortable: true, 
                                                id: 'fullname',
                                                dataIndex: 'fullname'
                                            },{
                                                header: 'Username',
                                                width: 100, 
                                                sortable: true, 
                                                dataIndex: 'username'
                                            },{
                                                header: 'Type',
                                                dataIndex: 'type',
                                                width: 80, 
                                                sortable: true
                                            },{
                                                header: 'Position',
                                                dataIndex: 'position',
                                                width: 80, 
                                                sortable: true
                                            },{
                                                header: 'Disabled',
                                                width: 60,
                                                dataIndex: 'disabled',
                                                renderer: function(value, metaData, record, rowIndex, colIndex, store) {
                                                  if (value)
                                                    return "Y";
                                                  return '--';
                                                }
                                            },{
                                                header: 'Email',
                                                dataIndex: 'email',
                                                width: 150, 
                                                sortable: true
                                            }],
                                            view: new Ext.grid.GroupingView({
                                                forceFit:true,
                                                groupTextTpl: '{text} ({[values.rs.length]} {[values.rs.length > 1 ? "Items" : "Item"]})'
                                            }),
                                            autoExpandColumn: 'fullname',
                                            listeners: {
                                              rowclick : function(g, rI, e){
                                                selectedUserID = g.getStore().getAt(rI).get('id');
                                                loginWinIES.loginBtn.enable();
                                              },
                                              rowdblclick : function(g, rI, e){
                                                selectedUserID = g.getStore().getAt(rI).get('id');
                                                loginWinIES.loginBtn.handler();
                                              }/*,
                                              selectionchange: function(lv,nodes){
                                                //alert(lv.toSource());
                                                alert(nodes);
                                                alert(nodes.toSource());
                                                selectedUserID = nodes;
                                              }*/
                                            }
                                          });

    var loginBox = new Ext.TabPanel({
                                      activeTab: 0,
                                      region: 'center',
                                      border: true,
                                      deferredRender:false,
                                      plain: true,
                                      items:[
                                        {title: 'Photos', layout: 'fit', autoScroll: true, id:'images-view', items: myDataView},
                                        {title: 'List', layout: 'fit', items: myListView}
                                      ]
                                    });


    var loginWinIES = new Ext.Window({
        title: loginMsg,
        width: 666, 
        height: 400,
        border:true,
        padding: 5,
        modal: true,
        resizable: true,
        closeAction: 'close',
        layout: 'fit',
        items: new Ext.Panel({layout: 'border', border: false, plain: true, items: [infoBox, loginBox]}),
        keys: [
                {
                  key: 27, // Esc key
                  handler: function(){ loginWin.close(); },
                  scope: this
                }
              ],
        buttons: [
                  {
                    text: 'Login',
                    id: 'login_submit_ies', 
                    disabled: true,
                    ref: '../loginBtn',
                    handler: function(){
                    
                      if (specialLogin)
                        Ext.Msg.wait('Logging in, please wait...', 'Logging In');
                      else
                        Ext.Msg.wait('Logging in as another user, please wait...', 'Switching Users');
                        
                      Ext.Ajax.request({
                         url:'/ajax/login/doLoginAs.cfm', 
                         params: {login_special: selectedUserID},
                         method: 'POST',
                         reader : new Ext.data.JsonReader({
                            root:"errors",
                            successProperty:"success"
                          }, ['id', 'msg']),
                         success: function(response, opts) {
                            if (noReload && action.result.userID == currentUser.id){
                            if (journeyHelper.isRunning)
                              journeyHelper.showText('Welcome Back!', true, null);
                            Ext.Msg.wait('You have been logged back in!', 'Logged In!');
                            reloginOK();
                            setTimeout(function(){
                              Ext.Msg.hide();
                            }, 750);
                          }else{
                            restartREQUIRED=true;
                            if (journeyHelper.isRunning)
                              journeyHelper.showText('You are being logged in, please wait...', true, null);

                            Ext.Msg.wait('You are being logged in, please wait...', 'Logging In');
                            //enableEditing(action.result)
                            loginWinIES.close();
                            if (finishOpenID)
                              window.location="/"+currentSchool.Alias+"/";
                            else
                              window.location.reload(false);
                          }
                         },
                         failure: function(response, opts) {
                          switch (action.failureType) {
                              case Ext.form.Action.CLIENT_INVALID:
                                  Ext.Msg.alert('Failure', 'Please verify all form fields are correct.');
                                  break;
                              case Ext.form.Action.CONNECT_FAILURE:
                                  Ext.Msg.alert('Failure', 'Ajax communication failed');
                                  break;
                              case Ext.form.Action.SERVER_INVALID:
                              default:
                                  Ext.Msg.alert('Failure', action.result.msg);
                          }
                         }
                      });
                      return false;
                      
                      Ext.getCmp('login_form_ies').getForm().submit({
                        clientValidation: true,            
                        url:'/ajax/login/doLoginAs.cfm', 
                        waitMsg:'Checking Credentials...',
                        errorReader : new Ext.data.JsonReader({
                            root:"errors",
                            successProperty:"success"
                          }, ['id', 'msg']),
                        success: function(form, action) {
                          if (noReload && action.result.userID == currentUser.id){
                            if (journeyHelper.isRunning)
                              journeyHelper.showText('Welcome Back!', true, null);
                            Ext.Msg.wait('You have been logged back in!', 'Logged In!');
                            reloginOK();
                            setTimeout(function(){
                              Ext.Msg.hide();
                            }, 750);
                          }else{
                            restartREQUIRED=true;
                            if (journeyHelper.isRunning)
                              journeyHelper.showText('You are being logged in, please wait...', true, null);

                            Ext.Msg.wait('You are being logged in, please wait...', 'Logging In');
                            //enableEditing(action.result)
                            loginWinIES.close();
                            if (finishOpenID)
                              window.location="/"+currentSchool.Alias+"/";
                            else
                              window.location.reload(false);
                          }
                        },
                        failure: function(form, action) {
                          switch (action.failureType) {
                              case Ext.form.Action.CLIENT_INVALID:
                                  Ext.Msg.alert('Failure', 'Please verify all form fields are correct.');
                                  break;
                              case Ext.form.Action.CONNECT_FAILURE:
                                  Ext.Msg.alert('Failure', 'Ajax communication failed');
                                  break;
                              case Ext.form.Action.SERVER_INVALID:
                              default:
                                  Ext.Msg.alert('Failure', action.result.msg);
                          }
                          }
                      });
                    }
                  },{
                    text: 'Cancel',
                    handler: function(){
                      loginWinIES.close();
                    }
                  }
                ]
      });
    loginWinIES.show();
    if (journeyHelper.isRunning)
      journeyHelper.showText('Who would you like to be today?', true, 5000);

  }

  function userSwitch(noReload, forceLastUser){
    Ext.Msg.wait('Switching users, please wait...', 'Switch Users');
    Ext.Ajax.request({
       url: '/ajax/login/switchUser.cfm',
       method: 'GET',
       reader : new Ext.data.JsonReader({
        success : '@success'
       }),
       success: function(response, opts) {
          Ext.Msg.wait('Switching users is complete.. page is now refreshing', 'Switching Users');
          window.location.reload(false);
       },
       failure: function(response, opts) {
        Ext.Msg.alert('Error Switching User', 'There was a problem communicating with the server. Please check your internet connection and try again.');
       }
    });   
  }

  function getImgSize(imgSrc){
    var tmpSize = {};
    var newImg = new Image();
    newImg.src = imgSrc;
    tmpSize.height = newImg.height;
    tmpSize.width = newImg.width;
    return tmpSize;
  }

  function showSizes(id){
    Ext.Msg.wait('Please Wait...', 'Re-loading Image');
    var tmpSize = {};
    var el = Ext.get(id);
    tmpSize.height = el.dom.height;
    tmpSize.width = el.dom.width;
    var tmpImage = getImgSize(el.dom.src);
    tmpSize.real_width = tmpImage.width;
    tmpSize.real_height = tmpImage.height;   
    var infoTxt = new Ext.XTemplate(
      '<center>',
        '<strong>Image size in pixels</strong><br />',
        '<br />',
        'Size on screen: <br />',
        '<strong>W: {width} x H:{height}</strong><br />',
        '<br />',
        'Actual Size:',
        '<br />',
        '<strong>W: {real_width} x H:{real_height}</strong>',        
      '</center>'
    );
    Ext.Msg.hide();
    var dimWind = new Ext.Window({
      title: "Image Dimensions",
      width: 150, 
      height: 140,
      border:true,
      padding: 5,
      modal: true,
      resizable: false,
      closeAction: 'close',
      html: infoTxt.apply(tmpSize)
    });
    dimWind.show();
  }
  
reportingIn('userLoginAs');
reportingIn('userLoginIES');
reportingIn('userSwitch');
