/* This Javascript object is used to check to see if a user has entered there correct 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 the system (session based authentication) changes in the future
*/

var loginObj={
//Main function
login : function(obj){
var params = obj.href.substring(obj.href.indexOf('?'), obj.href.length);
//Display busy animation
Element.show('processing');

//Get the field values
username = $F('Username');
agentref = $F('AccountID');
password = $F('Password');

if(username=="" || password=="" || agentref ==""){
//alert('Please enter an agent ref and a valid user ID/password.');
Element.show('msgalert');
this.resetLogin();
return false;
}
//Set the positng URL -the Names.nsf?login - uses a CGI Variable SERVER_NAME
var hostname=location.hostname;
var virtualdir=location.pathname.substring(location.pathname.indexOf('/')+1,location.pathname.lastIndexOf('/'));
var url = 'http://' + hostname + '/' + virtualdir + '/CheckLogin.aspx'; //location.pathname
//Encode the parameters
var pars="Username="+encodeURIComponent(username)+"&Password="+encodeURIComponent(password)+"&AgentRef="+encodeURIComponent(agentref);
//Build the AJAX request
var myAjax = new Ajax.Request(
url, 
{
method: 'post', 
parameters: pars, 
onComplete: this.processResponse.bindAsEventListener(this, params)
});
},

processResponse: function(originalRequest, params){
//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 redirect to the desired page as the user is logged in

var pageredirect = params.substring(params.lastIndexOf('param'), params.length).split('=');
var app_path =  location.pathname.substring(location.pathname.indexOf('/'),location.pathname.lastIndexOf('/'));
var protocol = location.protocol;
if(app_path != '')
    app_path = app_path;
if(protocol == '' || protocol == null)
    protocol = 'http:';
var urlredirect = location.href.substring(0,location.href.lastIndexOf('/'));

if(pageredirect[1]=='1'){
    if(location.href.indexOf('?')==-1){
        urlredirect = urlredirect + '/HolidayBooking.aspx';
    }else{
        urlredirect = protocol + '//' + location.hostname + app_path + '/HolidayBooking.aspx' + location.href.substring(location.href.indexOf('?'), location.href.length);
    }
}else if(pageredirect[1]=='2'){
    if(location.href.indexOf('?')==-1){
        urlredirect = urlredirect + '/SpecialOffers.aspx' + params;
    }else{
        urlredirect = protocol + '//' + location.hostname + app_path + '/SpecialOffers.aspx' + params; //location.href.substring(location.href.indexOf('?'), location.href.length);
    }
}else if(pageredirect[1]=='3'){
    if(location.href.indexOf('?')==-1){
        urlredirect = urlredirect + '/hoteldetails.aspx';
    }else{
        urlredirect = protocol + '//' + location.hostname + app_path + '/hoteldetails.aspx' + params;
    }
}

location.replace(urlredirect);
//Not present so uncomment the following line if you just want to reload the page as the user is logged in
//location.replace(window.location);
}else
{
//Got a login error so alert the user and reset the login form
alert('Incorrect user name or password');
this.resetLogin();
return false;
}
},

resetLogin:function(){
Element.hide('processing');
Form.reset('loginForm');
Form.focusFirstElement('loginForm');
}

}