var Login = new GELogin();
// Set up the popup login dialog, in case it's needed.
$( document ).ready( function() { Login.PopupLoginPrep(); } );
//
// GELogin 'class'
//
function GELogin() {
var ThisLogin = this; // Closure variable, so we can still refer to this object when inside an AJAX callback function.
this.LoggedIn = false;
this.LoginType = ''; // Can be: MyAccount, MailingList, or Guest.
this.LoginIdentity = ''; // Will be set to the user's login email in the case of a MyAccount login.
//
// Attempt to log the user into their My Account.
//
this.MyAccountLogin = function( LoginParameters, SuccessCallback, FailureCallback ) {
// TEMPORARY: Add the AJAX flag to the login parameters, until that is no longer required by the AJAX login PHP script.
LoginParameters.AJAXLogin = true;
$.ajax({
url: '/myaccount/attempt_myaccount_login.php',
type: 'POST',
dataType: 'json',
data: LoginParameters,
timeout: 15000, // 15 seconds.
success: function( response ) {
RevertButton( 'PopupLoginButton', ThisLogin.PopupAttemptLogin );
if ( response.FailedLogin ) {
// SimpleMessage( "We\'re sorry, but those login details don\'t seem to match our records. Perhaps you\'d like to try again?" );
SimpleMessage( "We're sorry, but those login details don't match our records. Perhaps you'd like to try again?
" +
"Note that even if you're an existing Ground Effect customer, you still need to first register with a login email and password before creating your on-line account. " +
"Click here to register.
" +
"Alternatively you can continue to browse the site and use the shopping cart as a guest or using your customer number.", "Login Error" );
FailureCallback( 'FailedLogin' );
} else if ( response.ErrorMessage ) {
FailureCallback( response.ErrorMessage );
} else if ( ! response.LoginEmail ) {
FailureCallback();
} else {
ThisLogin.LoggedIn = true;
ThisLogin.LoginType = 'MyAccount';
ThisLogin.LoginIdentity = response.LoginEmail;
// If the user logged in (even automatically) while on a /stayintouch/ page, we need to get them out of there and into /myaccount/.
if ( window.location.href.match( '^[^/]*//[^/]*/stayintouch/' ) ) {
// If they were on one of the pages that has an analogue in the MyAccount section, go specifically to that corresponding one.
if ( window.location.href.match( '^[^/]*//[^/]*/stayintouch/spread_the_word.php' ) ) {
setTimeout( "window.location.href = '/myaccount/spread_the_word.php'", 0 );
} else if ( window.location.href.match( '^[^/]*//[^/]*/stayintouch/contact-us.php' ) ) {
setTimeout( "window.location.href = '/myaccount/contact_us.php'", 0 );
} else {
// Go to default page for the MyAccount section.
setTimeout( "window.location.href = '/myaccount/'", 0 );
}
} else if ( window.location.href.match( '^[^/]*//[^/]*/spread_the_word/logmein' ) ) {
setTimeout( "window.location.href = '/myaccount/spread_the_word.php'", 0 );
} else {
ThisLogin.ShowLoginDetails();
}
SuccessCallback();
}
},
error: function ( XMLHttpRequest, textStatus, errorThrown ) {
RevertButton( 'PopupLoginButton', ThisLogin.PopupAttemptLogin );
if ( textStatus == 'timeout' ) {
// Because the login attempt will continue and possibly succeed in the background,
// we fire off a 'chaser' here to try to nullify it (to avoid confusing the user by later showing them as logged in).
$.ajax({ url: '/myaccount/cancel_login.php', type: 'POST' });
if ( LoginParameters.AutoLogin ) {
// SimpleMessage( 'Just FYI, we tried to log you in but struck a database problem.', 'DB Timeout' );
} else {
SimpleMessage( 'Sorry, we\'re having touble talking to the database. Can\'t log you in right now.', 'DB Timeout' );
}
} else {
SimpleMessage( 'Error (MyAccount Login) : ' + textStatus, 'Error' );
}
FailureCallback( textStatus );
}
});
}
this.LPLogin = function() {
alert( 'LPLogin attempted' );
}
this.ShowLoginDetails = function() {
// If there is a login indicator span available in the navbar (it is omitted on some pages), update it now to show the login details.
$( '#LoginIndicator' ).html( ThisLogin.LoginIdentity + ' my account | log off' );
}
this.MyAccountAutoLogin = function() { ThisLogin.MyAccountLogin( { "PerformAutoLogin" : true } ); }
this.PopupLoginPrep = function() {
// Perform the setup steps needed to support the popup login on every page.
// NOTE: The HTML for the LoginPop dialog is included in the top_nav file.
$( '#LoginPop' ).dialog( { autoOpen: false, resizable: false, modal: true, title: 'Login' } );
// Clicking in the background should close any and all dialogs.
$( 'div.ui-widget-overlay' ).live( 'click', function() { $( '#LoginPop' ).dialog( 'close' ); } );
$( 'div.ui-widget-overlay' ).live( 'click', function() { $( '#ResetPasswordPop' ).dialog( 'close' ); } );
$( 'div.ui-widget-overlay' ).live( 'click', function() { $( '#TroubleLoggingInPop' ).dialog( 'close' ); } );
$( 'div.ui-widget-overlay' ).live( 'click', function() { $( '#GenericDialog' ).dialog( 'close' ); } );
$( '#PopupLoginButton a' ).click( function() { ThisLogin.PopupAttemptLogin(); } );
$( '#NoLoginContinueButton a' ).click( function() { $( '#LoginPop' ).dialog( 'close' ); } );
// Any anchor with the PopupLoginLink class becomes a link that opens the popup login dialog.
// The top_nav bar has one of these links.
$( 'a.PopupLoginLink' ).click( function( event ) {
event.preventDefault();
$( '#LoginPop' ).dialog( 'open' );
$( '#PopupLoginEmail' ).focus();
});
$( '#PopupLoginEmail' ).keypress( function( e ) {
if ( (e.which && e.which == 13 ) || ( e.keyCode && e.keyCode == 13 ) ) {
$( '#PopupLoginPassword' ).focus();
}
});
$( '#PopupLoginPassword' ).keypress( function( e ) {
if ( (e.which && e.which == 13 ) || ( e.keyCode && e.keyCode == 13 ) ) {
ThisLogin.PopupAttemptLogin();
}
});
};
this.PopupAttemptLogin = function() {
// This is the login function called from the loginpopup dialog.
var PopupLoginEmail = $( '#PopupLoginEmail' ).val();
var PopupLoginPassword = $( '#PopupLoginPassword' ).val();
if ( ThisLogin.ValidateMyAccountLoginDetails( PopupLoginEmail, PopupLoginPassword ) ) {
ShowSpinner( 'PopupLoginButton' );
Login.MyAccountLogin(
{
"Login" : PopupLoginEmail,
"Password" : GEHash( PopupLoginPassword ),
"SaveAutoLogin" : $( '#PopupAutoLogin' ).attr( 'checked' )
},
// Success callback
function() {
$( '#LoginPop' ).dialog( 'close' );
document.location.reload(true);
},
// Failure callback
function( FailureReason ) {
if ( FailureReason != 'FailedLogin' ) {
$( '#LoginPop' ).dialog( 'close' );
if ( window.location.href.match( '^[^/]*//[^/]*/cart/' ) ) {
MakeChoice(
// Original: 'Sorry, there\'s a technical problem preventing us from checking your login details. Would you like to try again, or carry on as a guest?',
'Sorry – our server is on strike right now, temporarily restricting access to your account. The Shopping Cart still works but with some reduced functionality. Normal transmission should be resumed in a few minutes. In the meantime, would you like to carry on as a guest?',
'Carry on as a guest', function() { ContinueAsGuest(); },
'Try again', function() {},
'Connection Problem'
);
} else {
SimpleMessage(
'Sorry – our server is on strike right now, temporarily restricting access to your account. The Shopping Cart still works but with some reduced functionality. Normal transmission should be resumed in a few minutes. In the meantime, please feel free to carry on browsing.',
'Connection Problem'
);
}
}
}
);
}
}
this.ValidateMyAccountLoginDetails = function( LoginEmail, LoginPassword ) {
var Validated = false;
var Message = '';
if ( LoginEmail == '' )
Message = 'Please enter your email address';
else if ( LoginEmail.length > 100 )
Message = 'Email address too long';
else if ( LoginPassword == '' )
Message = 'Please enter your password';
else if ( LoginPassword.length > 100 )
Message = 'Password too long';
else
Validated = true;
if ( ! Validated ) SimpleMessage( Message, 'Login validation' );
return Validated;
}
this.PerformOnMailingListLogin = function( CustomerIdentifier, CustomerSurname, CustomerFirstName, SuccessCallBack, FailureCallBack, SimulateMarvinConnectionProblem ) {
$.ajax({
url: '/myaccount/attempt_on_mailing_list_login.php',
type: 'POST',
dataType: 'json',
data: {
"CustomerIdentifier": CustomerIdentifier,
"CustomerSurname": CustomerSurname,
"CustomerFirstName": CustomerFirstName
},
success: function( response ) {
if ( response.ConnectionProblem ) {
FailureCallBack( 'ConnectionProblem' );
} else if ( response.ErrorMessage ) {
SimpleMessage( response.ErrorMessage, 'Error' );
FailureCallBack();
} else {
LoggedIn = true;
LoginType = 'MailingList';
SuccessCallBack();
}
},
error: function ( XMLHttpRequest, textStatus, errorThrown ) {
if ( ( ! XMLHttpRequest ) || XMLHttpRequest.readyState == 0 || XMLHttpRequest.status == 0 ) FailureCallBack(); // Not actually an error: user probably navigated away.
else SimpleMessage( 'Error (Mailing List Login) : ' + textStatus, 'Error', FailureCallBack );
}
});
}
this.GuestPseudoLogin = function( SuccessCallBack ) {
$.ajax({
url: '/myaccount/guest_pseudo_login.php',
type: 'POST',
dataType: 'json',
data: { "ReturnResponse": true },
success: function( response ) {
if ( response.ErrorMessage ) SimpleMessage( response.ErrorMessage, 'Error' );
else {
LoggedIn = true;
LoginType = 'Guest';
SuccessCallBack();
}
},
error: function ( XMLHttpRequest, textStatus, errorThrown ) {
if ( ( ! XMLHttpRequest ) || XMLHttpRequest.readyState == 0 || XMLHttpRequest.status == 0 ) return; // Not actually an error: user probably navigated away.
else SimpleMessage( 'Error (MyAccount Login) : ' + textStatus, 'Error' );
}
});
}
this.ContinueAsKnownGuest = function( SuccessCallBack ) {
$.ajax({
url: '/myaccount/change_login_type.php?NewLoginType=Guest',
type: 'POST',
dataType: 'json',
data: {},
success: function( response ) {
if ( response.ErrorMessage ) SimpleMessage( response.ErrorMessage, 'Error' );
else {
LoggedIn = true;
LoginType = 'Guest';
SuccessCallBack();
}
},
error: function ( XMLHttpRequest, textStatus, errorThrown ) {
if ( ( ! XMLHttpRequest ) || XMLHttpRequest.readyState == 0 || XMLHttpRequest.status == 0 ) return; // Not actually an error: user probably navigated away.
else SimpleMessage( 'Error (MyAccount Login) : ' + textStatus, 'Error' );
}
});
}
this.DisplayPopupPasswordReset = function() {
// Add (if necessary) and then present the popup Password Reset dialog.
if ( $( '#ResetPasswordPop' ).length == 0 ) {
// Haven't used it already on this page, so add it to the DOM.
$( 'body' ).append(
'
Already set up your My Account but can\'t recall the password?
' + 'Enter the \'login email\' address used originally to create your My Account. We\'ll reset and email the new password to you.
' + '' + ' ' + ' ' + '
Your login is the email address you used when originally creating your My Account.
' + 'Note that even if you\'re an existing Ground Effect customer, you still need to first register with a login email and password before creating your on-line account.' + ' Click here to register.' + '
Alternatively you can continue to browse the site and use the shopping cart as a guest or using your customer number.
Already set up your My Account but can\'t recall the password?
' + // 'If you can’t remember your password click here to reset it (we\'ll let you know the new one by email to your login address).
Enter the \'login email\' address used originally to create your My Account. We\'ll reset and email the new password to you.
' + '' + ' ' + '
After registering you should receive an \'Activate My Account\' email. Click on the validation link within the email to activate your on-line account.
' + 'Some emails are blocked by your internet provider or filtered as junk mail by the email application on your computer.' + ' If you haven\'t received the \'Activate My Account\' email then check your junk mail settings and/or add us to your address book: ' + ' ernie@groundeffect.co.nz and orders@groundeffect.co.nz