login = {
   uri : 'ajax.ashx',
   init : function () {
      $ ( 'btn-submit' ).observe ( 'click', login.authenticate );
      $ ( 'login' ).observe ( 'keydown', login.keyHandler );
      $ ( 'login-username' ).focus ();
   },
   keyHandler : function ( e ) {
      if ( e.keyCode == Event.KEY_RETURN ) {
         login.authenticate ();
      }
   },
   authenticate : function () {
      if ( $ ( 'login-username' ).value.blank () ) {
         $ ( 'login-username' ).addClassName ( 'error' );
         $ ( 'login-username' ).focus ();
         return;
      }
      if ( $ ( 'login-password' ).value.blank () ) {
         $ ( 'login-username' ).removeClassName ( 'error' );
         $ ( 'login-password' ).addClassName ( 'error' );
         $ ( 'login-password' ).focus ();
         return;
      }
      App.trackEvent ( 'Login', 'Authenticate Request', $ ( 'login-username' ).value );
      $ ( 'login-username' ).removeClassName ( 'error' );
      $ ( 'login-password' ).removeClassName ( 'error' );
      $ ( 'login-indicator' ).appear ( {} );
      $ ( 'login-form' ).setOpacity ( .35 );
      var req = new Ajax.Request ( login.uri, {
         method : 'post',
         evalScripts : true,
         synchronous : true,
         onFailure : function ( e ) { this.statusMsg = e.statusText; login.authenticateFailure (); },
         onSuccess : login.onAuthenticate,
         parameters : {
            'ajax_action' : 'login',
            'username' : $ ( 'login-username' ).value,
            'password' : $ ( 'login-password' ).value
         }
      });
   },
   onAuthenticate : function ( e ) {
      login.data = e.responseXML.documentElement.childNodes;
      login.status = login.data [ 1 ].firstChild.nodeValue;
      login.statusMsg = login.data [ 2 ].firstChild.nodeValue;
      login.authenticated = login.data [ 3 ].firstChild.nodeValue;
      if ( login.authenticated == 'True' ) {
         login.authenticateSuccess ();
      } else {
         login.authenticateFailure ();
      }
   },
   authenticateSuccess : function () {
      App.trackEvent ( 'Login', 'Authenticate Success', $ ( 'login-username' ).value );
      $ ( 'login-indicator' ).hide ();
      $ ( 'login-form' ).setOpacity ( 1 );
      $ ( 'login-form' ).update ( '<label>Login was a success!</label>' );
      //window.location.href = '/oppe.aspx';
      window.location.reload ( true );
   },
   authenticateFailure : function () {
      $ ( 'login-form' ).setOpacity ( 1 );
      $ ( 'login-indicator' ).update ( '<label class="error">Oops... Login failed: Incorrect password!</label>' );
      App.trackEvent ( 'Login', 'Authenticate Failure', $ ( 'login-username' ).value );
   }
};

Event.observe ( window, 'load', login.init, false ); 
