AJAX installation is 'basicly finished' :) Plus I threw in a small christmas present...
[mailer.git] / js / ajax-common.js
index ccef02a08a3608b479660f663b851db652bddcc2..3686444aed6a2cf21094a1f843d069078af2eaed 100644 (file)
@@ -31,6 +31,7 @@ var currentTabId       = null;
 var defaultTabId       = null;
 var footerElements     = new Array();
 var changedElements    = new Array();
+var data               = new Array();
 var formChanged        = false;
 var saveChangesId      = null;
 var lastErrorMessage   = null;
@@ -53,12 +54,15 @@ function isElementVisible (prefix, element) {
        var el = document.getElementById(prefix + '_' + element);
 
        // Is element set?
-       if (el == null || el == undefined) {
+       if ((el == null) || (el == undefined)) {
                throw new '"' + prefix + '_' + element + '" does not exist.';
+       } else if ((el.style.display == undefined) || (el.style.display == '')) {
+               throw new '"' + prefix + '_' + element + '" has no style.display element.';
        }
 
        // Default is visible
-       var isVisible = ((el.style.display == undefined) || ((el.style.display != 'none') && (el.style.display != '')));
+       var isVisible = (el.style.display != 'none');
+       //* DEBUG: */ alert(prefix + '_' + element + ':' + el.style.display + '=' + isVisible);
 
        // Return status
        return isVisible;
@@ -66,8 +70,8 @@ function isElementVisible (prefix, element) {
 
 // Marks a tab navigation entry
 function markTabNavigation (prefix, tab) {
-       // Is process working?
-       if (isElementVisible(prefix, 'process')) {
+       // Is progress working?
+       if (isElementVisible(prefix, 'progress')) {
                // Then exit silently
                return;
        } // END - if
@@ -121,11 +125,15 @@ function disableElement (element) {
 
 // Enables a given footer navigation element
 function enableFooterNavigationPage (element) {
-       // Remove the 'disabled' class and attribute
+       // Is it 'finish'?
        if (element == 'finish') {
+               // Then without '_page' suffix
                enableElement('input#finish');
        } else {
+               // Regular element with '_page' suffix
                enableElement('input#' + element + '_page');
+
+               // Disable 'finish' element
                disableElement('input#finish');
        }
 }
@@ -134,22 +142,28 @@ function enableFooterNavigationPage (element) {
 function resetFooterNavigation () {
        // Remove the 'disabled' class and attribute
        for (var i = 0; i < footerElements.length; i++) {
-               $('input#' + footerElements[i] + '_page').addClass('disabled');
-               $('input#' + footerElements[i] + '_page').attr('disabled', 'disabled');
-               $('input#' + footerElements[i] + '_page').blur();
+               disableElement('input#' + footerElements[i] + '_page');
        } // END - for
+
+       // Disable this element
+       disableElement('input#finish');
 }
 
 // Getter for AJAX content
 function getAjaxContent () {
        // Is it defined?
-       if ($('body').data('ajax_content') == undefined) {
+       if (data['ajax_content'] == undefined) {
                // Not set
                throw new 'ajax_content requested but not set.';
        } // END - if
 
        // Return it
-       return $('body').data('ajax_content');
+       return data['ajax_content'];
+}
+
+// Setter for AJAX content
+function setAjaxContent (ajax_content) {
+       data['ajax_content'] = ajax_content;
 }
 
 // "Setter" for AJAX content but does decode the content
@@ -161,24 +175,20 @@ function setAjaxDecodedContent (ajax_content) {
        setAjaxContent(decoded);
 }
 
-// Setter for AJAX content
-function setAjaxContent (ajax_content) {
-       $('body').data('ajax_content', ajax_content);
-}
-
 // Getter for AJAX success
 function getAjaxSuccess () {
-       return $('body').data('ajax_success');
+       return data['ajax_success'];
 }
 
 // Setter for AJAX success
 function setAjaxSuccess (success) {
-       $('body').data('ajax_success', success);
+       data['ajax_success'] = success;
 }
 
 // Set AJAX reply and decode JSON if requested
 function setAjaxReply (reply, isJson) {
        // Is it JSON URL-encoded content?
+       //* DEBUG: */ alert('setAjaxReply(): reply=' + reply + ',isJson=' + isJson);
        if ((isJson != undefined) && (isJson == true)) {
                // Decode URL-encoding (for some reason it must be here ...)
                var localReply = decodeUrlEncoding(reply);
@@ -209,6 +219,7 @@ function sendAjaxRequest (level, doValue, extra, isJson) {
                data: 'level=' + level + '&do=' + doValue + extra,
                dataType: 'json',
                async: false,
+               timeout: 10000,
 
                // Called on success
                success: function (ajax_content) {
@@ -261,13 +272,14 @@ function sendAjaxRequest (level, doValue, extra, isJson) {
        });
 
        // Return status
+       //* DEBUG: */ alert(getAjaxSuccess() + ':' + level + ',' + doValue + ',' + extra + ',' + isJson);
        return getAjaxSuccess();
 }
 
 // Enables footer navigation buttons
 function enableFooterNavigation (prefix, tabId) {
-       // Is process working?
-       if (isElementVisible(prefix, 'process')) {
+       // Is progress working?
+       if (isElementVisible(prefix, 'progress')) {
                // Then exit silently
                return;
        } // END - if
@@ -290,8 +302,8 @@ function enableFooterNavigation (prefix, tabId) {
 
 // Requests an AJAX content
 function requestAjaxContent (prefix, htmlId, tabId, footerNavigation) {
-       // Is process working?
-       if (isElementVisible(prefix, 'process')) {
+       // Is progress working?
+       if (isElementVisible(prefix, 'progress')) {
                // Then exit silently
                return;
        } // END - if
@@ -351,14 +363,14 @@ function displayTestWindow (prefix, element) {
        // Register click-event for error window
        $('#' + prefix + '_error_close').click(function () {
                // Close the window
-               closeErrorWindow(prefix);
+               closeErrorWindow(prefix, true, false);
        });
 
        // Register click-event for warning window
        $('#' + prefix + '_warning_close').click(function () {
                // Close the window
                //* DEBUG: */ alert('displayTestWindow(): prefix=' + prefix + ' - calling closeWarningWindow()');
-               closeWarningWindow(prefix);
+               closeWarningWindow(prefix, true, false);
        });
 
        // Request it from the AJAX backend
@@ -381,13 +393,13 @@ function displayTestWindow (prefix, element) {
 function displayChangedWarningWindow (prefix, button) {
        // Fade out warning window, if open
        //* DEBUG: */ alert('displayChangedWarningWindow(): prefix=' + prefix + ',button=' + button + ' - calling closeWarningWindow()');
-       closeWarningWindow(prefix);
+       closeWarningWindow(prefix, true, false);
 
        // Fade error out for eye-candy, if open
-       closeErrorWindow(prefix);
+       closeErrorWindow(prefix, true, false);
 
        // Fade it out for eye-candy
-       closeProcessWindow(prefix);
+       closeProgressWindow(prefix, true, false);
 
        // Abort here if warningDisplayed is still true
        if (isElementVisible(prefix, 'warning')) {
@@ -413,14 +425,14 @@ function displayChangedWarningWindow (prefix, button) {
 // Displays the error window for given prefix and content
 function displayErrorWindow (prefix, ajax_content) {
        // Fade out warning window, if open
-       //* DEBUG: */ alert('displayErrorWindow(): prefix=' + prefix + ' - calling closeWarningWindow()');
-       closeWarningWindow(prefix);
+       //* DEBUG: */ alert('displayErrorWindow(): prefix=' + prefix + ',ajax_content=' + ajax_content.reply_content + ' - calling closeWarningWindow()');
+       closeWarningWindow(prefix, true, false);
 
        // Fade it out for eye-candy
-       closeErrorWindow(prefix);
+       closeErrorWindow(prefix, true, false);
 
        // Fade it out for eye-candy
-       closeProcessWindow(prefix);
+       closeProgressWindow(prefix, true, false);
 
        // Abort here if errorDisplayed is still true
        if (isElementVisible(prefix, 'error')) {
@@ -441,65 +453,65 @@ function displayErrorWindow (prefix, ajax_content) {
        });
 }
 
-// Displays the process window for given prefix and content
-function displayProcessWindow (prefix, ajax_content) {
+// Displays the progress window for given prefix and content
+function displayProgressWindow (prefix, ajax_content) {
        // Fade out warning window, if open
-       //* DEBUG: */ alert('displayProcessWindow(): prefix=' + prefix + ' - calling closeWarningWindow()');
-       closeWarningWindow(prefix);
+       //* DEBUG: */ alert('displayProgressWindow(): prefix=' + prefix + ' - calling closeWarningWindow()');
+       closeWarningWindow(prefix, true, false);
 
        // Fade it out for eye-candy
-       closeErrorWindow(prefix);
+       closeErrorWindow(prefix, true, false);
 
        // Fade it out for eye-candy
-       closeProcessWindow(prefix);
+       closeProgressWindow(prefix, true, false);
 
-       // Abort here if processDisplayed is still true
-       if (isElementVisible(prefix, 'process')) {
+       // Abort here if progressDisplayed is still true
+       if (isElementVisible(prefix, 'progress')) {
                // Make sure this doesn't happen
                return;
        } // END - if
 
-       // Copy the response text to the process variable
+       // Copy the response text to the progress variable
        if (ajax_content.reply_content != undefined) {
                // Set HTML content
-               setProcessContent(prefix, ajax_content.reply_content);
+               setProgressContent(prefix, ajax_content.reply_content);
        } else {
-               setProcessContent(prefix, ajax_content);
+               setProgressContent(prefix, ajax_content);
        }
 
-       // Fade the process in
-       $('#' + prefix + '_process').fadeIn('slow', function() {
+       // Fade the progress in
+       $('#' + prefix + '_progress').fadeIn('slow', function() {
                // Do nothing for now
        });
 }
 
-// Sets "process content"
-function setProcessContent (prefix, content) {
+// Sets "progress content"
+function setProgressContent (prefix, content) {
        // Set HTML content
-       $('#' + prefix + '_process_content').html(content);
+       $('#' + prefix + '_progress_content').html(content);
 }
 
 // Waits until the window has been closed
-function closeErrorLocked () {
+function closeErrorLocked (prefix) {
        // Has all been loaded?
        if (!isElementVisible(prefix, 'error')) {
                // Then release ready()
                $.holdReady(false);
        } else {
                // Recursive call again
-               window.setTimeout('closeErrorLocked()', 10);
+               window.setTimeout('closeErrorLocked(' + prefix + ')', 10);
        }
 }
 
 // Waits until the window has been closed
-function closeProcessLocked () {
+function closeProgressLocked (prefix) {
        // Has all been loaded?
-       if (!isElementVisible(prefix, 'process')) {
+       if (!isElementVisible(prefix, 'progress')) {
                // Then release ready()
                $.holdReady(false);
        } else {
                // Recursive call again
-               window.setTimeout('closeProcessLocked()', 10);
+               window.setTimeout('closeProgressLocked(' + prefix + ')', 10);
        }
 }
 
@@ -524,15 +536,15 @@ function closeErrorWindow (prefix, waitClose, resetCurrentTabId) {
                // Shall this animation be "synchronized"?
                if (waitClose == true) {
                        // Wait for the window has been closed
-                       closeErrorLocked();
+                       closeErrorLocked(prefix);
                } // END - if
        } // END - if
 }
 
-// Closes an process window
-function closeProcessWindow (prefix, waitClose, resetCurrentTabId) {
-       // Is the process displayed?
-       if (isElementVisible(prefix, 'process')) {
+// Closes an progress window
+function closeProgressWindow (prefix, waitClose, resetCurrentTabId) {
+       // Is the progress displayed?
+       if (isElementVisible(prefix, 'progress')) {
                // Shall we wait ("sync") until the animation has completed?
                if (waitClose == true) {
                        // Hold the ready status
@@ -540,7 +552,7 @@ function closeProcessWindow (prefix, waitClose, resetCurrentTabId) {
                } // END - if
 
                // Yes, then fade it out
-               $('#' + prefix + '_process').fadeOut('fast', function() {
+               $('#' + prefix + '_progress').fadeOut('fast', function() {
                        // Set current tab id to default
                        if (resetCurrentTabId == true) {
                                setCurrentTabId(defaultTabId);
@@ -550,20 +562,20 @@ function closeProcessWindow (prefix, waitClose, resetCurrentTabId) {
                // Shall this animation be "synchronized"?
                if (waitClose == true) {
                        // Wait for the window has been closed
-                       closeProcessLocked();
+                       closeProgressLocked(prefix);
                } // END - if
        } // END - if
 }
 
 // Waits until the window has been closed
-function closeWarningLocked () {
+function closeWarningLocked (prefix) {
        // Has all been loaded?
        if (!isElementVisible(prefix, 'warning')) {
                // Then release ready()
                $.holdReady(false);
        } else {
                // Recursive call again
-               window.setTimeout('closeWarningLocked()', 10);
+               window.setTimeout('closeWarningLocked(' + prefix + ')', 10);
        }
 }
 
@@ -591,7 +603,7 @@ function closeWarningWindow (prefix, waitClose, resetCurrentTabId) {
                if (waitClose == true) {
                        // Wait for the window has been closed
                        //* DEBUG: */ alert('prefix=' + prefix + ',waitClose=' + waitClose + ' - LOCKED!');
-                       closeWarningLocked();
+                       closeWarningLocked(prefix);
                } // END - if
        } // END - if
 }
@@ -608,8 +620,8 @@ function doFooterPage (prefix, htmlId, button) {
                return;
        } // END - if
 
-       // Is process working?
-       if (isElementVisible(prefix, 'process')) {
+       // Is progress working?
+       if (isElementVisible(prefix, 'progress')) {
                // Then exit silently
                return;
        } // END - if
@@ -692,8 +704,8 @@ function markFormFieldsFailed (failedFields) {
        });
 }
 
-// Processes the content from AJAX call
-function processAjaxResponseContent (prefix, ajax_content) {
+// Progresses the content from AJAX call
+function progressAjaxResponseContent (prefix, ajax_content) {
        // By default all is failed
        var isResponseDone = false;
 
@@ -718,8 +730,8 @@ function processAjaxResponseContent (prefix, ajax_content) {
 
 // Saves changes by sending the data to the AJAX backend script
 function saveChanges (prefix) {
-       // Is process working?
-       if (isElementVisible(prefix, 'process')) {
+       // Is progress working?
+       if (isElementVisible(prefix, 'progress')) {
                // Then exit silently
                return;
        } // END - if
@@ -751,8 +763,8 @@ function saveChanges (prefix) {
                // Get the content
                var ajax_content = getAjaxContent();
 
-               // Process the returned content
-               if (processAjaxResponseContent(prefix, ajax_content) == true) {
+               // Progress the returned content
+               if (progressAjaxResponseContent(prefix, ajax_content) == true) {
                        // Mark all elements as unchanged
                        markAllElementsAsUnchanged();
 
@@ -768,7 +780,7 @@ function saveChanges (prefix) {
                                displayErrorWindow(prefix, '<div class="ajax_error_message">' + ajax_content.message + '</div>');
                        } else {
                                // This didn't work, why?
-                               displayErrorWindow(prefix, '<div class="ajax_error_message">processAjaxResponseContent() failed, please fix this.<br />\n' + lastErrorMessage + '</div>');
+                               displayErrorWindow(prefix, '<div class="ajax_error_message">progressAjaxResponseContent() failed, please fix this.<br />\n' + lastErrorMessage + '</div>');
                        }
                }
 
@@ -816,8 +828,8 @@ function doSaveChangesPage (prefix, htmlId, page) {
 
 // Saves changed settings and continues with given tab
 function doSaveChangesContinue (prefix, htmlId, tab) {
-       // Is process working?
-       if (isElementVisible(prefix, 'process')) {
+       // Is progress working?
+       if (isElementVisible(prefix, 'progress')) {
                // Then exit silently
                return;
        } // END - if