  var loginWin;
  function userLogin(noReload, forceLastUser){
  globalNoReload = noReload;
    if (isSpecial){
      userLoginIES(noReload, forceLastUser);
      return false;
    }
    var curMsg = forceLastUser ? 'Unlock your account' : 'Login to your account';
    var infoBox = {region: 'north', baseCls: '', cls: 'dialogHelpTitle', border:false, height: 48, html: '<strong style="font-size: 13px; font-weight: bold">'+curMsg+'</strong>'};
            
    var curDomain = currentSchool.LDAPDefaultDomain; 
    if (currentSchool.LDAPChangeDomain && lastLoggedInUserDomain != '')
      curDomain = lastLoggedInUserDomain;
            
    var leftFields = [new Ext.form.Hidden({
                        name: forceLastUser ? 'username' : 'username_unused_atm',
                        value: currentUser.username
                      }),
                      forceLastUser ? new Ext.Panel({
                        border: false,
                        bodyStyle: 'font-size: 12px; text-align: center; margin-bottom: 10px;',
                        html: 'Session Locked by:<br /><strong>'+lastLoggedInDisplayName+'</strong>'
                      }) : {
                        xtype: 'textfield',
                        name:'username',
                        id: 'login_user',
                        maskRe:/^([ _a-zA-Z0-9\^\$\.\+\?\=\:\|\\\/\(\)\[\]\&\-])+$/,
                        selectOnFocus: true,
                        blankText: 'Username is Required',
                        allowBlank: false,
                        fieldLabel: 'Username',
                        value: lastLoggedInUserName
                      },{
                        xtype: 'textfield',
                        name:'password',
                        id: 'login_password',
                        inputType:'password',
                        selectOnFocus: true,
                        blankText: 'Password is Required',
                        allowBlank: false,
                        fieldLabel: 'Password'
                      }];

                      
    if (currentSchool.LoginMode == 'hybrid')
      leftFields.push({
                        xtype: 'checkbox',
                        name:'userdomaincheck',
                        ref: 'domainCheck',
                        id: 'login_ldap',
                        hideLabel: true,
                        boxLabel: 'Login with Active Directory',
                        checked: currentSchool.LoginMode == 'ldap' || lastLoggedInUserDomain != '' ? true : false,
                        listeners: {
                          'check' : function(cb, checked){
                            cb.ownerCt.domainField.setVisible(checked); 
                            cb.ownerCt.domainField.label.setVisible(checked); 
                          }
                        }
                      });
                      
    if (currentSchool.LoginMode != 'cyberschool' && !currentSchool.LDAPChangeDomain)
      leftFields.push({
                          xtype: 'displayfield',
                          ref: 'domainField',
                          fieldLabel: 'Windows Domain',
                          html: curDomain,
                          listeners: {
                            'render' : function(tf, checked){
                              var checked = currentSchool.LDAPChangeDomain || tf.ownerCt.domainCheck ? tf.ownerCt.domainCheck.getValue() : false; 
                              tf.setVisible(checked); 
                              tf.label.setVisible(checked);
                            }
                          }
                      });

    if (currentSchool.LoginMode == 'ldap')
      leftFields.push({
                          xtype: 'hidden',
                          hideLabel: true,
                          name:'userdomaincheck',
                          value: 'on'
                      });

                      
    if (currentSchool.LoginMode != 'cyberschool' && !currentSchool.LDAPChangeDomain)
      leftFields.push({
                          xtype: 'hidden',
                          name: !currentSchool.LDAPChangeDomain ? 'userdomain' : 'userdomain_unused',
                          value: curDomain
                      });
                      
    if (currentSchool.LoginMode != 'cyberschool' && currentSchool.LDAPChangeDomain)
      leftFields.push({
                          xtype: 'textfield',
                          name: currentSchool.LDAPChangeDomain ? 'userdomain' : 'userdomain_unused',
                          ref: 'domainField',
                          id: 'login_domain',
                          maskRe:/^([ _a-zA-Z0-9\^\$\.\+\?\=\:\|\\\/\(\)\[\]\&\-])+$/,
                          selectOnFocus: true,
                          allowBlank: true,
                          fieldLabel: 'Windows Domain',
                          value: curDomain,
                          listeners: {
                            'render' : function(tf, checked){
                              var checked = currentSchool.LDAPChangeDomain && tf.ownerCt.domainCheck ? tf.ownerCt.domainCheck.getValue() : false; 
                              if (currentSchool.LDAPChangeDomain)
                                checked = true;
                              tf.setVisible(checked); 
                              tf.label.setVisible(checked);
                            }
                          }
                        });    
                      
    var rightFields = [new Ext.Panel({
                        border: false,
                        anchor: '0',
                        hidden: !finishOpenID,
                        bodyStyle: 'font-size: 12px; text-align: center; margin-bottom: 10px; font-weight: bold;',
                        html: 'To finish linking your Google Account please login as normal.'
                      }),
                      new Ext.Panel({
                        border: false,
                        anchor: '0',
                        hidden: finishOpenID,
                        bodyStyle: 'font-size: 10px; text-align: center',
                        html: '<a style="text-decoration: none; color: black;" onclick="launchGoogleLogin(); return false;" href="/authsub/openID.cfm">Click here to sign in with your<br /><img src="/common/images/google_transparent_blue.gif" /> <span style="color: black; font-size: 14px; position: relative; top: -8px; font-weight: bold;">Account</span></a>'
                        //html: '<a style="text-decoration: none; color: black;" onclick="launchGoogleLogin(); return false;" href="https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2F%20http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F%20http%3A%2F%2Fdocs.google.com%2Ffeeds%2F&session=1&secure=0&next=http%3A%2F%2Fdev1.cyberschool.com%2Fauthsub&hd=default">Sign in with your<br /><img src="/common/images/google_transparent.gif" /> <span style="color: black; font-size: 14px; position: relative; top: -8px; font-weight: bold;">Account</span></a>'
                      })];
            
    var myColumns = {
                border: false,
                layout:'column',
                autoScroll:true,
                items:[{
                    columnWidth: currentSchool.GoogleLogin ? .5 : 1.0,
                    baseCls:'x-plain',
                    layout: 'form',
                    defaults: {
                        anchor: '-20',
                        msgTarget: 'side'
                    },
                    items:[leftFields]
                },{
                    columnWidth:.5, 
                    hidden: !currentSchool.GoogleLogin,
                    baseCls:'x-plain',
                    id:'login-with-google',
                    items:[rightFields]
                }]
            };
            
    var loginBox = new Ext.form.FieldSet({
                                            labelAlign: 'top',
                                            border: false,
                                            items: myColumns
                                          });
    var formPanel = new Ext.FormPanel({
                                        region: 'center', 
                                        id: 'login_form', 
                                        border:true, 
                                        monitorValid:true, 
                                        plain: true, 
                                        items: loginBox,
                                        keys: [{key : [10,13],fn: function(){
                                                  var bt = Ext.getCmp('login_submit'); 
                                                  bt.focus();
                                                  bt.fireEvent("click", bt);
                                             }
                                        }]
                                        });



    loginWin = new Ext.Window({
        title: "Login",
        id: "Loginwin",
        width: currentSchool.GoogleLogin ? 450 : 250, 
        height: currentSchool.LoginMode != 'cyberschool' ? 325 : 280,
        layout: 'fit',
        border:true,
        padding: 5,
        modal: true,
        resizable:false,
        closeAction: 'close',
        items: new Ext.Panel({layout: 'border', border: false, items: [infoBox, formPanel]}),
        keys: [
                {
                  key: 27, // Esc key
                  handler: function(){ loginWin.close(); },
                  scope: this
                }
              ],
        buttons: [
            {
              text: 'Login',
              id: 'login_submit', 
              formBind: true,
              handler: function(){
                Ext.getCmp('login_form').getForm().submit({
                  clientValidation: true,            
                  url:'/ajax/login/doLogin.cfm', 
                  waitMsg:'Checking Credentials...',
                  errorReader : new Ext.data.JsonReader({
                      root:"errors",
                      successProperty:"success"
                    }, ['id', 'msg']),
                  success: function(form, action) {
                    if (action.result.SpecialIES){
                      isSpecial=true;
                      userLoginIES(noReload, forceLastUser);
                      loginWin.close();
                    }else{
                      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');
                        loginWin.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(){
                loginWin.close();
              }
            }
          ]
      });
    loginWin.show();
    if (journeyHelper.isRunning)
      journeyHelper.showText('Enter your username and password to access your files and account information!', true, 5000);

  }
