/* This Javascript object is used to check to see if a user has entered there correct Domino credientials
Requires: Prototype.js being available
Associated with: A login form - HTML ID loginForm
The code is formatted in the JSON format for object reuse

This code could break if the standard error message returned by Domino (session based authentication) changes in the future
*/

var loginObj={

  //Main function
  login : function(){

    //Display busy animation
    Element.show('processing');
    // GW 04/04/2007
    Element.hide('incorrect');

    //Get the field values
    username = $F('Username');
    password = $F('Password');

    if(username == "" || password == ""){
      this.resetLogin();
      return false;
    }

    //Set the posting URL -the Names.nsf?login - uses a CGI Variable SERVER_NAME
    var hostname = location.hostname;
    var serverPort = "";
    // GW 03/04/2007 - add check for server port
    if(location.port != "80") serverPort = ":" + location.port;
    var url = 'http://' + location.hostname + serverPort + '/names.nsf?login';

    //Encode the parameters
    var pars = "Username=" + encodeURIComponent(username) + "&Password=" + encodeURIComponent(password);
    //Build the AJAX request		
    var myAjax = new Ajax.Request(
      url, {
        method: 'post', 
        parameters: pars, 
        onComplete: this.processResponse.bindAsEventListener(this)
      });
  },

  processResponse: function(originalRequest){
    //Check response to see if the error message is in the string
    test = originalRequest.responseText.indexOf("invalid username or password");
    if(test == -1){
      //Not present so just reload the page as the user is logged in
      //location.replace(window.location);
      // GW 12/04/2007 - redirect to 247 Home
      location.replace($F('redirectURL'));
    } else {
      //Got a login error so alert the user and reset the login form
      // GW 04/04/2007
      Element.show('incorrect');
      //alert('Incorrect user name or password');
      this.resetLogin();
      return false;
    }
  },

  resetLogin:function(){
    Element.hide('processing');
    Form.reset('loginForm');
    Form.focusFirstElement('loginForm');
  }
}
