You have some syntax problems where inserting the timeoutDialog - it all needs to be on one line or escaped properly - see below.
Having said that, I just don't get what you are trying to do with the IF statement.
- You say if groupIndex is greater than or equal to 0, click Submit, otherwise redirect the page.
- Well, groupIndex will always be greater than or equal to 0 so the redirect will never happen.
- The #movesubmitbtn element only exists on the last page of the survey, so, unless you are there, nothing will ever happen.
Code:
$(document).ready(function(){
var txtAlertMessage = 'Anyone there? Do you want to continue?';
var txtCloseButton = 'Continue';
var redirectURL = location.pathname.split('index.php')[0];
var timeToAlert = 2; // In seconds
var timeToRedirect = 2; // In seconds
// Page timeout action
function pageTimeout() {
// Find the group index -
var groupIndex = $('div[id^="group-"]').attr('id').split('group-')[1];
if(groupIndex >= 0) {
$('#movesubmitbtn').click();
}
else {
window.location = redirectURL;
}
}
// Alert Timer
var alertTimer;
function startAlertTimer() {
alertTimer = setTimeout(function() {
$('.custom-dialog-1').dialog('open');
},timeToAlert*1000);
}
function stopAlertTimer() {
clearTimeout(alertTimer);
}
function restartAlertTimer() {
clearTimeout(alertTimer);
startAlertTimer();
}
startAlertTimer();
// Redirect Timer
var redirectTimer;
function startRedirectTimer() {
redirectTimer = setTimeout(function() {
pageTimeout();
},timeToRedirect*1000);
}
function stopRedirectTimer() {
clearTimeout(redirectTimer);
}
function restartRedirectTimer() {
clearTimeout(redirectTimer);
startRedirectTimer();
}
// Insert the alert dialog
var timeoutDialog = '<div class="custom-dialog-1">\
<div class="text">'+txtAlertMessage+'</div>\
<div class="buttons">\
<button class="close" type="button" value="close">'+txtCloseButton+'</button>\
</div>\
</div>';
$(timeoutDialog).dialog({
autoOpen: false,
open: function( event, ui ) {
startRedirectTimer();
//IE 10 z-index hack
$('.ui-widget-overlay').css('z-index', Number($('.ui-widget-overlay').css('z-index')) - 2);
},
close: function( event, ui ) {
stopRedirectTimer();
restartAlertTimer();
},
width: 400,
modal: true,
resizable: false,
draggable: true,
closeOnEscape: true,
dialogClass: 'timeout-dialog'
});
$('.timeout-dialog button').click(function() {
$('.custom-dialog-1').dialog('close');
});
// Listener for activity
$('#limesurvey').on('click keyup paste change' ,function(event){
restartAlertTimer();
});
});