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.

' + '

 

' + '

' + '

password-reset-submit

' + '
' ); $( '#ResetPasswordPop' ).dialog( { autoOpen: false, resizable: false, modal: true, width: 400, title: 'Reset Password' } ); $( '#ResetPasswordPop a' ).click( function( event ) { event.preventDefault(); ThisLogin.PerformPasswordReset(); } ); $( '#ResetPasswordPop #ResetSubmitButton img' ).hover( function() { this.src = this.src.replace( 'submit.', 'submit-roll.' ); }, function() { this.src = this.src.replace( 'submit-roll.', 'submit.' ); } ); $( '#ResetPasswordPop #LoginEmailForReset' ).keypress( function( e ) { if ( (e.which && e.which == 13 ) || ( e.keyCode && e.keyCode == 13 ) ) { ThisLogin.PerformPasswordReset(); } }); } $( '#LoginPop' ).dialog( 'close' ); $( '#MyAccountLoginControls' ).dialog( 'close' ); $( '#TroubleLoggingInPop' ).dialog( 'close' ); $( '#ResetPasswordPop' ).dialog( 'open' ); $( '#ResetPasswordPop #LoginEmail' ).focus(); } this.ValidateLoginInput = function( EmailAddress ) { if ( EmailAddress === '' ) { SimpleMessage( 'Please enter the email address that you used to log in to your My Account (the one you used when you set it up).', 'Input problems' ); return false; } var EmailRegexp = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i; if ( ! EmailRegexp.test( EmailAddress ) ) { SimpleMessage( 'That doesn\'t look like an email address. Please check and try again.', 'Input problems' ); return false; } return true; } this.PerformPasswordReset = function() { // Call an AJAX helper script to reset the user's password. If successful, that script will email the new password to the specified address. if ( ThisLogin.ValidateLoginInput( $( '#LoginEmailForReset' ).attr( 'value' ) ) ) { ShowSpinner( 'ResetSubmitButton' ); $.ajax({ type: 'POST', url: '/myaccount/reset_password.php', data: { "LoginEmail": $( '#LoginEmailForReset' ).attr( 'value' ) }, dataType: 'json', timeout: 10000, // 10 seconds success: function( response ) { RevertButton( 'ResetSubmitButton', this.PerformPasswordReset ); if ( response.ErrorMessage ) { SimpleMessage( response.ErrorMessage, 'Error' ); } else { SimpleMessage( 'Thanks, we have now reset your password, and emailed the new password to you at that address.', 'Password Reset' ); $( '#ResetPasswordPop' ).dialog( 'close' ); $( '#TroubleLoggingInPop' ).dialog( 'close' ); } }, error: function ( XMLHttpRequest, textStatus, errorThrown ) { RevertButton( 'ResetSubmitButton', this.PerformPasswordReset ); if ( textStatus == 'timeout' ) { SimpleMessage( 'Oops, that seems to be taking a bit longer than it should. That probably means there is a technical glitch and your password hasn\'t been reset. Keep an eye on your mailbox, though, just in case we\'re being too impatient.', 'Error' ); } else { SimpleMessage( 'Sorry, there\'s a technical problem preventing us from resetting your password right now.', 'Error' ); } } }); } } this.DisplayTroubleLoggingInDialog = function() { // Add (if necessary) and then present the popup Trouble Loggin In dialog. if ( $( '#TroubleLoggingInPop' ).length == 0 ) { // Haven't used it already on this page, so add it to the DOM. $( 'body' ).append( '
' + '

Forgot your login email?

' + '

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.

' + '

Forgot your password?

' + '

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.

' + '

 

' + '

' + '

password-reset-submit

' + '

Didn’t receive an Account Confirmation email?

' + '

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

' + '' ); $('#TroubleLoggingInPop h2').click(function() { $('.answer').hide(); $(this).next('.answer').show(); }); $( '#TroubleLoggingInPop a#ResetSubmitButton' ).click( function( event ) { event.preventDefault(); ThisLogin.PerformPasswordReset(); } ); $( '#TroubleLoggingInPop #ResetSubmitButton img' ).hover( function() { this.src = this.src.replace( 'submit.', 'submit-roll.' ); }, function() { this.src = this.src.replace( 'submit-roll.', 'submit.' ); } ); $( '#TroubleLoggingInPop #LoginEmailForReset' ).keypress( function( e ) { if ( (e.which && e.which == 13 ) || ( e.keyCode && e.keyCode == 13 ) ) { ThisLogin.PerformPasswordReset(); } }); $( '#TroubleLoggingInPop' ).dialog( { autoOpen: false, resizable: false, modal: true, width: 500, title: 'Trouble Logging In' } ); // $( '#ResetPasswordPop a' ).click( function( event ) { event.preventDefault(); ThisLogin.PerformPasswordReset(); } ); // $( '#ResetPasswordPop #ResetSubmitButton img' ).hover( // function() { this.src = this.src.replace( 'submit.', 'submit-roll.' ); }, // function() { this.src = this.src.replace( 'submit-roll.', 'submit.' ); } // ); } $( '#LoginPop' ).dialog( 'close' ); $( '#MyAccountLoginControls' ).dialog( 'close' ); $( '#TroubleLoggingInPop' ).dialog( 'open' ); } } function GoToRegistration() { // Helper function that can be called from within a login error dialog. setTimeout( "window.location.href = 'http://www.groundeffect.co.nz/stayintouch/login.php'", 0 ); } // Comment here to catch the garbage characters