// AX-4481.2         Oct 28, 2020   arr   Changes to an iForm can be lost with no warning message - Back button
// AX-28884          Jul 26, 2023   ST    Added newValidateNavMenu for PA 2.0

var g_prevSelectedId = null;

$(document).ready(function () {
    // Custom messages and labels for iform

    $("body").on('click', function (e) {        // AX-24898.24919.2 .click(function (e) {
        validatePreviousInputs(e.target);
    });

    $("input").on('focus', function () {        // AX-24898.24919.2 .focus(function () {
        validatePreviousInputs(this);
    });

    $("textarea").on('focus', function () {     // AX-24898.24919.2 .focus(function () {
        validatePreviousInputs(this);
    });

    $("select").on('focus', function () {       // AX-24898.24919.2 .focus(function () {
        validatePreviousInputs(this);
    });

    $(".shortTextControl").each(function () {
        // var current = this;

        // Create maxlength condition
        $(this).maxlength({
            events: [], //Array of events to be triggered
            maxCharacters: 40,
            status: true, // True to show status indicator below the element
            statusClass: "remainingCount", // The class on the status div
            statusText: "/40", // The status text
            notificationClass: "", // the class added when maxlength is reached
            showAlert: false, // true to show a regular alert message
            alertText: "Cannot enter more than 40 characters", // text in alert message
            slider: true // true use counter slider
        });
        $(this).attr("maxlength", "40");
        $(this).attr("onblur", "validateShortText(this);");
        $(this).attr("onchange", "enableSubmitButton();");

        // Disable enter from submitting the form
        $(this).on('keypress', function (e) {		// AX-24898.24919.2 .keypress(function (e) {
            enableSubmitButton();
            disableEnter(e);
        });

        // there are cases where backspace is not registered as part of "on input" event in IE
        // this keyup handles the specific case to trigger validation
        $(this).on('keyup', function (e) {          // AX-24898.24919.2 .keyup(function (e) {
            // backspace
            if (e.which == 8) {
                validateShortText(this);
            }
        });

        // Create validation and update of submit button
        $(this).on("input", function () {
            validateShortText(this);
        });
    });

    $(".longTextControl").each(function () {
        //var current = this;

        // Create maxlength condition
        $(this).maxlength({
            events: [], //Array of events to be triggered
            maxCharacters: 32000,
            status: true, // True to show status indicator below the element
            statusClass: "remainingCount", // The class on the status div
            statusText: "/32000", // The status text
            notificationClass: "", // the class added when maxlength is reached
            showAlert: false, // true to show a regular alert message
            alertText: "Cannot enter more than 32000 characters", // text in alert message
            slider: true // true use counter slider
        });
        $(this).attr("maxlength", "32000");

        // Disable enter from submitting the form
        $(this).on('keypress', function (e) {             // AX-24898.24919.2 .keypress(function (e) {
            enableSubmitButton();

            var max = 32000;
            var strokeValue = 0;

            // current selection
            var value = $(this).val();
            var currentSelection = getSelection(this);

            var range = currentSelection.end - currentSelection.start;

            // current expected length
            var lineBreakCount = (value.match(/\n/g) || []).length;
            var expectedLength = value.length + lineBreakCount - range;

            // onKeyDown and onKeyPress has different keyCode
            // this case, we are using keypress, which listens to all keys that outputs data
            // we only need to listen if the key stroke is an enter or backspace (FireFox behaviour),
            // to assign extra value to it

            if (e.which > 46)       // Regular keys
                strokeValue = 1;
            else if (e.which == 13) // Enter
                strokeValue = 2;
            else if (e.which == 32) // Spacebar
                strokeValue = 1;


            if (strokeValue == 0 || expectedLength + strokeValue <= max)
                return true;

            e.preventDefault();

            if (strokeValue == 2 && expectedLength + 1 <= max) {

                // replace enter with a space
                var currentPos = $(this).scrollTop();

                var formatText = value.substr(0, currentSelection.start) + " " + value.substr(currentSelection.end);
                $(this).val(formatText);
                $(this).scrollTop(currentPos);

                setSelection(this, currentSelection.start + 1, 0);
            }
            return false;
        });

        // there are cases where backspace is not registered as part of "on input" event in IE
        // this keyup handles the specific case to trigger validation
        $(this).on('keyup', function (e) {          // AX-24898.24919.2 .keyup(function (e) {    
            // backspace
            if (e.which == 8) {
                validateLongText(this);
            }
        });

        // Create validation and update of submit button
        $(this).on("input", function () {
            validateLongText(this);
        });
    });

    $(".numberControl").each(function () {
        var current = this;

        // Create maxlength condition
        $(this).maxlength({
            events: [], //Array of events to be triggered
            maxCharacters: 10,
            status: true, // True to show status indicator below the element
            statusClass: "remainingCount", // The class on the status div
            statusText: "/10", // The status text
            notificationClass: "", // the class added when maxlength is reached
            showAlert: false, // true to show a regular alert message
            alertText: "Cannot enter more than 10 characters", // text in alert message
            slider: true // true use counter slider
        });
        $(this).attr("maxlength", "10");
        $(this).attr("onblur", "validateNumber(this);");
        // $(this).numeric();       // AX-24898.24918
        $(this).on("numeric");      // AX-24898.24918


        // Disable enter from submitting the form
        $(this).on('keypress', function (e) {		// AX-24898.24919.2 .keypress(function (e) {
            enableSubmitButton();
            updateErrorIcon(this);
            disableEnter(e);
        });

        // there are cases where backspace is not registered as part of "on input" event in IE
        // this keyup handles the specific case to trigger validation
        $(this).on('keyup', function (e) {          // AX-24898.24919.2 .keyup(function (e) {
            // backspace
            if (e.which == 8) {
                enableSubmitButton();
                updateErrorIcon(this);
            }
        });

        // Disable tab when number validation fails
        $(this).on('keydown', function (e) {      // AX-24898.24919.2 use keydown() instead of live()
            var keyCode = e.keyCode || e.which;
            if (keyCode == 9) {
                var result = validateNumber(current);
                if (result.error > 0) {
                    e.preventDefault();
                }
            }
        });

        // Create validation and update of submit button
        $(this).on("input", function () {
            enableSubmitButton();
            updateErrorIcon(this);
        });
    });

    $(".listControl").each(function () {
        var current = this;
        $(current).on('change', function () {     // AX-24898.24919.2 .change(function () {
            validateList(this);
            enableSubmitButton();
        });
    });

    $(".yesNoControl").each(function () {
        var current = this;
        $(current).on('change', function () {     // AX-24898.24919.2 .change(function () {
            validateYesNo(current);
            enableSubmitButton();
        });
    });

    $(".dateControl").each(function () {
        var current = this;
        // $(current).find('.yearControl').numeric(false);          // AX-24898.24918
        $(current).find('.yearControl').on("numeric", false);       // AX-24898.24918
        $(current).find('.yearControl').attr("maxlength", "4");
        $(current).find('.yearControl').attr("onblur", "validateYear(this);");
        $(current).find('.yearControl').on('keypress', function (e) {		// AX-24898.24919.2 .keypress(function (e) {
            updateDate(current);
            enableSubmitButton();
            updateErrorIcon(current);
            disableEnter(e);
        });

        // there are cases where backspace is not registered as part of "on input" event in IE
        // this keyup handles the specific case to trigger "Enable Submit Button"
        $(this).on('keyup', function (e) {          // AX-24898.24919.2 .keyup(function (e) {
            // backspace
            if (e.which == 8) {
                updateDate(current);
                updateErrorIcon(current);
                enableSubmitButton();
            }
        });

        $(current).find('input[type=hidden]').datepicker({
            minDate: new Date(1900, 0, 1),
            maxDate: new Date(2100, 11, 31),
            beforeShow: function (input, inst) {
                inst.dpDiv.css({
                    //marginTop: (-90 - input.offsetHeight) + 'px',
                    marginLeft: input.offsetWidth + 'px'
                });
            },
            onSelect: function () {
                updateYearMonthDay(current);
                enableSubmitButton();
            },
            dateFormat: 'yy-mm-dd',
            showOn: "button", // since the field is a hidden field, it should show only when button is clicked
            buttonImage: "/PatientAccess/Includes/Images/datepicker/calendar.png",
            buttonImageOnly: true,
            yearRange: '1900, 2100',
            onClose: function () { }
        });

        $(current).find('.monthControl').on('change', function () {     	// AX-24898.24919.2 .change(function () {
            updateDate(current);
            validateDate(current, true);
            enableSubmitButton();
        });

        $(current).find('.dayControl').on('change', function () {     	// AX-24898.24919.2 .change(function () {
            updateDate(current);
            validateDate(current, true);
            enableSubmitButton();
        });

        // May 17, 2013 - This process is not needed because each year control have onblur event handler
        // onblur event handler is to prevent older versions of IE not triggering event
        /*
        $(current).find('.yearControl').change(function () {
        updateDate(current);
        enableSubmitButton();
        });
        */

        // Create validation and update of submit button
        $(current).find('.yearControl').on("input", function () {
            updateDate(current);
            enableSubmitButton();
            updateErrorIcon(current);
        });

        // there are cases where backspace is not registered as part of "on input" event in IE
        // this keyup handles the specific case to trigger validation
        $(current).find('.yearControl').on('keyup', function (e) {          // AX-24898.24919.2 .keyup(function (e) {
            // backspace
            if (e.which == 8) {
                updateDate(current);
                enableSubmitButton();
                updateErrorIcon(current);
            }
        });

        $(current).find('.clearButton').click(function () {
            clearDate(current);
            validateDate(current, true);
            enableSubmitButton();
        });

        // Initialize date
        updateDate(current);
    });

    $(".checkListControl input[type=checkbox]").each(function () {
        var current = this;
        $(current).on('change', function () {     	// AX-24898.24919.2 .change(function () {
            enableSubmitButton();
        });
    });

    // Disable submitbutton when page is loaded
    //$('input[name="SubmitButton"].save').attr('disabled', true);
});

function validateYear(elmt) {
    var value = $(elmt).val();

    if (!validateInteger(value)) {
        var pattern = /[^0-9]/g;
        value = value.replace(pattern, "");

        $(elmt).val(value);
    }

    var current = $(elmt).parent();
    updateDate(current);
    validateDate(current, true);
    enableSubmitButton();
}

function updateYearMonthDay(container) {
    var date = $(container).find("input[type=hidden]").val();
    if (date != null) {
        var ymd = date.split("-");
        if (ymd.length == 3) {
            var year = parseInt(ymd[0]);
            var month = parseInt(ymd[1].replace(/\D/g, ''), 10); // parseInt(ymd[0]);
            var day = ymd[2];

            //Update month dropdown
            // AX-307
            var daysInMonth = 32 - new Date(year, month - 1, 32).getDate();

            $(container).find(".dayControl option").remove();
            var ddl = $(container).find(".dayControl");
            $(ddl).append('<option value="0"></option>');
            for (var i = 1; i <= daysInMonth; i++) {
                var d = (i < 10) ? "0" + i : i;
                $(ddl).append('<option value="' + d + '">' + d + '</option>');
            }

            if (day > daysInMonth) {
                day = daysInMonth;
            }

            $(container).find(".yearControl").val(year);
            $(container).find(".monthControl").val(month);
            $(container).find(".dayControl").val(day);
        }
    }
}

function updateDate(container) {

    var year = $(container).find(".yearControl").val();
    var month = $(container).find(".monthControl").val();
    var day = $(container).find(".dayControl").val();

    if (month != "") {
        // AX-307
        var daysInMonth = 32 - new Date(year, month - 1, 32).getDate();

        $(container).find(".dayControl option").remove();
        var ddl = $(container).find(".dayControl");
        $(ddl).append('<option value="0"></option>');
        for (var i = 1; i <= daysInMonth; i++) {
            var d = (i < 10) ? "0" + i : i;
            $(ddl).append('<option value="' + d + '">' + d + '</option>');
        }

        if (day > daysInMonth) {
            day = daysInMonth;
        }

        $(ddl).val(day);
    }

    // If any of the field is empty, but not the others,
    // This means invalid date
    if (year == "" && month == "0" && day == "0") {

        $(container).find("input[type=hidden]").val("");

    } else if (
        (day == "0" && (year != "" || month != "0")) ||
        (year == "" && (month != "0" || day != "0")) ||
        (month == "0" && (year != "" || day != "0")) ||
        year < 1900 ||
        year > 2100) {

        $(container).find("input[type=hidden]").val("null");

    } else {

        var date = "";
        date += year + "-";
        date += (month < 10) ? "0" : "";
        date += month + "-";
        date += day;
        

        $(container).find("input[type=hidden]").val(date);
    }
}

function clearDate(container) {
    $(container).find(".yearControl").val("");
    $(container).find(".monthControl").val("0");
    $(container).find(".dayControl").val("0");
    $(container).find("input[type=hidden]").val("");
}

// Enable submit button if value of input field is different from original value
function enableSubmitButton() {
    var counter = checkUnsavedItems();
    if (counter == 0) {
        $(".errorIcon").hide();
        $("input[name=ContButton]").show();
        $("input[name=MarkButton]").show();
        // AX-4481 $("input[name=SaveButton]").hide();
        $('#SaveBtn').hide();    // AX-4481
    } else {
        $("input[name=ContButton]").hide();
        $("input[name=MarkButton]").hide();
        // AX-4481 $("input[name=SaveButton]").show();
        $('#SaveBtn').show();    // AX-4481
    }
}

function validateSave(ignoreEmpty) {
    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
        return;

    // AX-8776 Show spinner 
    // ------------------------------------------------------
    getParentWindow(window).showSpinner();

    setTimeout(function () {

       var result = validateInputs();
       var err = result.error;
       var emp = result.empty;


       var origLoc = $("#HiddenSaveBtn").attr("location");     // AX-4481.4
       var currLoc = window.location.href;                     // AX-4481.4
       //console.log("=== validateSave() ==== btn location: " + origLoc);
       //console.log("=== validateSave() ==== win.location: " + currLoc);
       // AX-4481.4
       if (origLoc !== currLoc) {
          history.pushState(null, null, null);
          //console.log("=== validateSave() ==== pushStatte keep here ");
       }
       // display error/empty confirmation windows
       if (err > 0) {
           var errorHeader = getSettingMessage("SettingInvalidFormSubmitHeader");
           var errorMessage = getSettingMessage("SettingInvalidFormSubmitMessage");

           // AX-8776 Hide spinner before error message
           // ------------------------------------------------------
           getParentWindow(window).hideSpinner();

           getParentWindow(window).createErrorWindow(errorHeader, errorMessage);
          //return false; AX-4481.4
       } else if (emp > 0 && !ignoreEmpty) {
          var confirmHeader = getSettingMessage("SettingEmptyFormSubmitHeader");
          var confirmMessage = getSettingMessage("SettingEmptyFormSubmitMessage");

          var cancelButtonText = "Cancel";
          var okButtonText = ($("#SaveButton").length > 0) ? $("#SaveButton").attr("value") : "Save";

          // AX-8776 Hide spinner before confirmation message
          // ------------------------------------------------------
          getParentWindow(window).hideSpinner();

          getParentWindow(window).createConfirmWindow(confirmHeader, confirmMessage, cancelButtonText, okButtonText,
             function () {
                getParentWindow(window).showSpinner();                                 // AX-4481.4
                $("#HiddenSaveBtn").attr("saving", "1");                               // AX-4481.4
                 $("#HiddenSaveBtn").trigger('click');	        // AX-24898.24919.2 $("#HiddenSaveBtn").click(); /*$(".ignoreBlankSaveButton").click();*/  // Ax-4481.4
             });
          //return false;  AX-4481.4
       } else {
          $("#HiddenSaveBtn").attr("saving", "1");                                     // AX-4481.4
          $("#HiddenSaveBtn").trigger('click');	        // AX-24898.24919.2 $("#HiddenSaveBtn").click();
          //return true; AX-4481.4
       }
    }, 10);
}

function hideSpinnerAndRedirect(redirectTo) {
   
   getParentWindow(window).hideSpinner();

   // AX-4481.2
   if (getParentWindow(window).isSuccessWindowOpened()) {
      getParentWindow(window).removeSuccessWindow();
   }
   
    window.location.replace(redirectTo);
}

function validateInputs() {
    // error and empty counter
    var err = 0;
    var emp = 0;

    //validate short text items
    $(".shortTextControl").each(function () {
        var result = validateShortText(this);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    // validate long text items
    $(".longTextControl").each(function () {
        var result = validateLongText(this);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    // validate number items
    $(".numberControl").each(function () {
        var result = validateNumber(this);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    // validate dropdown list items
    $(".listControl").each(function () {
        var result = validateList(this);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    // validate dropdown for yes/no items
    $(".yesNoControl").each(function () {
        var result = validateYesNo(this);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    // validate date items
    $(".dateControl").each(function () {
        var result = validateDate(this, false);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    // validate check list items
    $(".checkListControl").each(function () {
        var result = validateCheckList(this);
        err = err + result.error;
        emp = emp + result.empty;
    });

    history.pushState(null, null, null);    // AX-4481.?

    $(".spreadsheetControl").each(function () {
        var result = validateSpreadsheet(this);
        emp = emp + result.empty;
    });

    return { error: err, empty: emp };
}

function validatePageJump(id, page) {
    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
        return false;

    // error and empty counter
    var counter = checkUnsavedItems();

    if (counter > 0) {
        var confirmHeader = getSettingMessage("SettingLeaveUnsavedFormHeader");
        var confirmMessage = getSettingMessage("SettingLeaveUnsavedFormMessage");
        var cancelButtonText = "No";
        var okButtonText = "Yes";

        getParentWindow(window).createConfirmWindow(confirmHeader, confirmMessage, cancelButtonText, okButtonText,
            function () {
                window.location.href = window.location.pathname + '?id=' + id + '&page=' + page;
            });

        return false;
    }

    window.location.href = window.location.pathname + '?id=' + id + '&page=' + page;

    return true;
}

function validateClick() {
    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
        return false;

    return true;
}

function validateMarkClick() {
    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
        return false;

    // AX-8776 Hide spinner before error message
    // ------------------------------------------------------
    getParentWindow(window).showSpinner();

    return true;
}

// validate if navigating away from the form is valid
function validateNav(ignoreButtonClass) {
    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
        return false;

    // error and empty counter
    var counter = checkUnsavedItems();

    if (counter > 0) {
        var confirmHeader = getSettingMessage("SettingLeaveUnsavedFormHeader");
        var confirmMessage = getSettingMessage("SettingLeaveUnsavedFormMessage");
        var cancelButtonText = "No";
        var okButtonText = "Yes";

        getParentWindow(window).createConfirmWindow(confirmHeader, confirmMessage, cancelButtonText, okButtonText,
            function () { $("." + ignoreButtonClass).trigger('click'); });       // AX-24898.24919.2 $("." + ignoreButtonClass).click();  
        return false;
    }

    return true;
}

// AX-28884
function newValidateNavMenu() {
   // check if current window has error window opened
   // this process will be terminated
   if (window.isErrorWindowOpened() || window.isConfirmWindowOpened()) {
      return false;
   }
      

   // error and empty counter
   var counter = checkUnsavedItems();
   if (counter > 0) {
      var confirmHeader = getSettingMessage("SettingLeaveUnsavedFormHeader");
      var confirmMessage = getSettingMessage("SettingLeaveUnsavedFormMessage");
      var cancelButtonText = "No";
      var okButtonText = "Yes";

      window.createConfirmWindow(confirmHeader, confirmMessage, cancelButtonText, okButtonText, function () { getParentWindow(window).location.reload(); });
      return false;     // This is trigger first time before even show the confirmWindow, so need to return false.
   }
   return true;      // Nothing to save, then return true

}

// AX-4481.1 validate if navigate to menu option is possible (has not questions in iForm to be saved)
function validateNavMenu(redirectUrl, redirectId) {
   // check if current window has error window opened
   // this process will be terminated
   if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
      return false;

   // error and empty counter
   var counter = checkUnsavedItems();

   if (counter > 0) {
      var confirmHeader = getSettingMessage("SettingLeaveUnsavedFormHeader");
      var confirmMessage = getSettingMessage("SettingLeaveUnsavedFormMessage");
      var cancelButtonText = "No";
      var okButtonText = "Yes";

      getParentWindow(window).createConfirmWindow(confirmHeader, confirmMessage, cancelButtonText, okButtonText,
         function () {
            getParentWindow(window).location.href = redirectId !== '0' ? redirectUrl + redirectId : redirectUrl;    // When in confirm window select Ok continue and lose changes, then redirect
         });

      return false;     // This is trigger first time before even show the confirmWindow, so need to return false.
   } 
   return true;      // Nothing to save, then return true
   
}

// AX-4481.1 validate if logout is possible (has not questions in iForm to be saved)
function validateLogoutOrAuthor(continueOption) {
   // check if current window has error window opened
   // this process will be terminated
   if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened())
      return false;

   // error and empty counter
   var counter = checkUnsavedItems();

   if (counter > 0) {
      var confirmHeader = getSettingMessage("SettingLeaveUnsavedFormHeader");
      var confirmMessage = getSettingMessage("SettingLeaveUnsavedFormMessage");
      var cancelButtonText = "No";
      var okButtonText = "Yes";

      getParentWindow(window).createConfirmWindow(confirmHeader, confirmMessage, cancelButtonText, okButtonText,
         function () {
            var hdnBtn;
            if (continueOption === 1)
               hdnBtn = jQuery('#logoutIfrmId', window.parent.document).contents().find("#HiddenApplyAuthorBtn");
            else {
               hdnBtn = jQuery('#logoutIfrmId', window.parent.document).contents().find("#HiddenLogoutBtn");
             }
             hdnBtn.trigger('click');	        // AX-24898.24919.2 hdnBtn.click();
         });

      return false;     // This is trigger first time before even show the confirmWindow, so need to return false.
   }
   return true;      // Nothing to save, then return true

}

// AX-4481.2
function validateiFormSavingProcess(e) {
   //console.log("=== At top validateiFormSavingProcess() ====");

   // check if current window has error window opened
   // this process will be terminated
   if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened() || getParentWindow(window).isSuccessWindowOpened()) {
      return false;
   }

   // the spinning waiting is showing. then ask if want to lost saving
   var isSpinning = getParentWindow(window).isSpinnerShowing();

   //console.log("=== At validateiFormSavingProcess() isSpinning: " + isSpinning);
   if (isSpinning) {
      var confirmHeaderSpin = getSettingMessage("SettingLeaveSavingFormHeader");
      var confirmMessageSpin = getSettingMessage("SettingLeaveSavingFormMessage");

      getParentWindow(window).createSuccessWindow(confirmHeaderSpin, confirmMessageSpin, null);
      //console.log("=== At valid.js validateiFormSavingProcess() return False ====");
      return false;     // This is trigger first time before even show the confirmWindow, so need to return false.
   }

   //console.log("=== At valid.js validateiFormSavingProcess() return true ====");
   return true;      // Nothing to save, then return true
}


// AX-4481.2
function backBtnValidateiFormNoSaved(e) {
   // check if current window has error window opened
   // this process will be terminated
   if (getParentWindow(window).isErrorWindowOpened() || getParentWindow(window).isConfirmWindowOpened() || getParentWindow(window).isSuccessWindowOpened()) {
      return "";
   }

   // Check if there are unsaved items
   var counter = checkUnsavedItems();

   if (counter > 0) {
      var confirmMessage = getSettingMessage("SettingBackBtnLeaveUnsavedFormMessage");

      return confirmMessage;     // Message then something to save
   }

   /* AX-4481.5 Nothing to changed, no check if is saving
   var saveBackend = $("#HiddenSaveBtn").attr("saving");    // AX-4481
   if (saveBackend === '0') {
      var confirmMessageNoCalculate = getSettingMessage("SettingBackBtnLeaveUnsavedFormMessage");

      return "No calculate if there is mising" + confirmMessageNoCalculate;     // Message then no calculate yet

   }
   */
   return "";      // Nothing to save, then return empty
}

// function to check if there are any items that are not saved
function checkUnsavedItems() {
    // error and empty counter
    var counter = 0;

    //validate short text items
    $(".shortTextControl").each(function () {
        var originalValue = $(this).parent().children('input[name$="original"]').val();
        var currentValue = $(this).val();

        if (originalValue != currentValue)
            counter += 1;
    });

    // validate long text items
    $(".longTextControl").each(function () {
        var originalValue = $(this).parent().children('input[name$="original"]').val();
        var currentValue = $(this).val();

        if (originalValue != currentValue)
            counter += 1;
    });

    // validate number items
    $(".numberControl").each(function () {
        var originalValue = $(this).parent().children('input[name$="original"]').val();
        var currentValue = $(this).val();

        if (originalValue != currentValue)
            counter += 1;
    });

    // validate date items
    $(".dateControl").each(function () {
        var originalValue = $(this).parent().children('input[name$="original"]').val();
        var currentValue = $(this).find('input[type=hidden]').val();
        if (currentValue == null)
            currentValue = $(this).find('input[type=text]').val();

        if (originalValue != currentValue)
            counter += 1;
    });

    // validate list items
    $(".listControl").each(function () {
        var originalValue = $(this).parent().children('input[name$="original"]').val();
        var currentValue = $(this).val();

        if (originalValue != currentValue)
            counter += 1;
    });

    // validate yes/no items
    $(".yesNoControl").each(function () {
        var originalValue = $(this).parent().children('input[name$="original"]').val();
        var currentValue = $(this).val();

        if (originalValue != currentValue)
            counter += 1;
    });

    // validate check list items
    $(".checkListControl").each(function () {
        var originalValue = $(this).children('.checkBoxControl').find('input[name$="original"]').val();
        var currentValue = $(this).children('.checkBoxControl').find('input[type=checkbox]').is(":checked") ? "1" : "0";

        if (originalValue != currentValue)
            counter += 1;
    });

    return counter;
}

// validate element with short text rules
function validateShortText(elmt) {
    var value = $(elmt).val();
    var empty = 0;
    var error = 0;

    if (!validateRequired(value)) {
        if ($(elmt).is(".additional")) {
            // Check if current item is a child of a "checkListControl"
            // The element can be invisible due to collapsed header
            // Need to validate based on checked status of its parent item
            if ($(elmt).closest('.childItems').parent().is(".checkListControl")) {
                var isChecked = $(elmt).closest('.childItems').parent().children(".checkBoxControl").find("input:checkbox").is(":checked");
                if (isChecked) {
                    empty += 1;
                }
            }
            // If current item is not a child of a "checkListControl", it means it is a child of a Yes/No question
            // In this case, if element is visible/invisible based on the availability for submition
            // Need to validate the control only if it is visible
            else if ($(elmt).is(":visible")) {
                empty += 1;
            }
        } else {
            empty += 1;
        }
    }

    if (!validateLength(value, 40)) {
        error += 1;
    }

    // When length validation fails, it trims the width automatically, rendering error icon invalid
    // render the icon only when field is empty
    if (empty == 0) {
        $(elmt).parent().parent().find(".errorIcon").first().hide();
    }
    else {
        $(elmt).parent().parent().find(".errorIcon").first().show();
    }

    // Open collapsed child item area containing current item if current item has error or empty field
    if (error + empty > 0)
        openItem(elmt);

    enableSubmitButton();

    return { error: error, empty: empty };
}

// validate element long text rules
function validateLongText(elmt) {
    var value = $(elmt).val();
    var empty = 0;
    var error = 0;

    if (!validateRequired(value)) {
        if ($(elmt).is(".additional")) {
            // Check if current item is a child of a "checkListControl"
            // The element can be invisible due to collapsed header
            // Need to validate based on checked status of its parent item
            if ($(elmt).closest('.childItems').parent().is(".checkListControl")) {
                var isChecked = $(elmt).closest('.childItems').parent().children(".checkBoxControl").find("input:checkbox").is(":checked");
                if (isChecked) {
                    empty += 1;
                }
            }
            // If current item is not a child of a "checkListControl", it means it is a child of a Yes/No question
            // In this case, if element is visible/invisible based on the availability for submition
            // Need to validate the control only if it is visible
            else if ($(elmt).is(":visible")) {
                empty += 1;
            }
        } else {
            empty += 1;
        }
    }

    if (!validateLength(value, 32000)) {
        error += 1;
    }

    // When length validation fails, it trims the width automatically, rendering error icon invalid
    // render the icon only when field is empty
    if (empty == 0) {
        $(elmt).parent().parent().find(".errorIcon").first().hide();
    }
    else
        $(elmt).parent().parent().find(".errorIcon").first().show();

    // Open collapsed child item area containing current item if current item has error or empty field
    if (error + empty > 0)
        openItem(elmt);

    enableSubmitButton();

    return { error: error, empty: empty };
}

// validate element with number rules
function validateNumber(elmt) {
    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened())
        return { error: 1, empty: 0 };

    var value = $(elmt).val();
    var min = $(elmt).parent().find("input[name$=min]").val();
    var max = $(elmt).parent().find("input[name$=max]").val();
    var increment = $(elmt).parent().find("input[name$=inc]").val();

    var empty = 0;
    var error = 0;

    if (!validateRequired(value)) {
        if ($(elmt).is(".additional")) {
            // Check if current item is a child of a "checkListControl"
            // The element can be invisible due to collapsed header
            // Need to validate based on checked status of its parent item
            if ($(elmt).closest('.childItems').parent().is(".checkListControl")) {
                var isChecked = $(elmt).closest('.childItems').parent().children(".checkBoxControl").find("input:checkbox").is(":checked");
                if (isChecked) {
                    empty += 1;
                }
            }
            // If current item is not a child of a "checkListControl", it means it is a child of a Yes/No question
            // In this case, if element is visible/invisible based on the availability for submition
            // Need to validate the control only if it is visible
            else if ($(elmt).is(":visible")) {
                empty += 1;
            }
        } else {
            empty += 1;
        }
    }
    else if (!validateDecimal(value)) {
        var pattern = /[^\-\.0-9]/g;
        var output = "";

        // get rid of all un-allowed characters
        var str = value.replace(pattern, "");
        if (str == "")
            value = "";
        else {
            // get rid of negative symbol if it is not in correct place
            if (str.substr(0, 1) == "-") {
                output += "-";
                str = str.substr(1);
            }
            str = str.replace(/[\-]/g, "");

            // get rid of decimal if it is not in correct place
            if (str.substr(str.length - 1) == ".")
                str = str.substr(0, str.length - 1);

            var tokens = str.split(".");
            for (var i = 0; i < tokens.length; i++) {
                if (i == 0) {
                    var trimmed = tokens[i].replace(/^0+/, "");
                    output += (trimmed == "") ? "0" : trimmed;
                    if (tokens.length > 1)
                        output += ".";
                } else {
                    output += tokens[i];
                }
            }

            value = output;

            //check if value is 0
            var p = /[^\-\.0]/g;
            if (!p.test(value)) {
                value = "0";
            }
        }

        $(elmt).val(value);
    }
    else {
        var isNegative = false;
        var o = "";
        var s = value;

        // save negative symbol
        if (s.substr(0, 1) == "-") {
            isNegative = true;
            s = s.substr(1, s.length - 1);
        }

        // remove last . if not followed by other numbers
        if (s.substr(s.length - 1) == ".")
            s = s.substr(0, s.length - 1);

        // right trim, if if leading with zero
        var t = s.split(".");
        for (var j = 0; j < t.length; j++) {
            if (j == 0) {
                var x = t[j].replace(/^0+/, "");
                o += (x == "") ? "0" : x;
                if (t.length > 1)
                    o += ".";
            } else {
                o += t[j];
            }
        }

        value = (isNegative) ? "-" + o : o;

        //check if value is 0
        var q = /[^\-\.0]/g;
        if (!q.test(value)) {
            value = "0";
        }

        $(elmt).val(value);
    }


    if (!validateRange(value, min, max, increment)) {
        error += 1;
    }

    if (empty == 0 && error == 0) {
        $(elmt).parent().parent().find(".errorIcon").first().hide();
    }
    else {
        $(elmt).parent().parent().find(".errorIcon").first().show();
        if (error > 0) {
            var invalidNumberHeader = $('input[id$="SettingInvalidNumberMessageHeader"]').val();
            var invalidNumberMsg = $('input[id$="SettingInvalidNumberMessage"]').val();
            var errorMsg = invalidNumberMsg.replace("{0}", min).replace("{1}", max).replace("{2}", increment);

            getParentWindow(window).createErrorWindow(invalidNumberHeader, errorMsg, function () {
                $(elmt).val("");
            });
        }
    }

    // Open collapsed child item area containing current item if current item has error or empty field
    if (error + empty > 0)
        openItem(elmt);

    enableSubmitButton();

    return { error: error, empty: empty };
}

//validate schemed answer list
function validateList(elmt) {
    var value = $(elmt).val();
    var empty = 0;
    var error = 0;

    if (!validateRequired(value)) {
        if ($(elmt).is(".additional")) {
            // Check if current item is a child of a "checkListControl"
            // The element can be invisible due to collapsed header
            // Need to validate based on checked status of its parent item
            if ($(elmt).closest('.childItems').parent().is(".checkListControl")) {
                var isChecked = $(elmt).closest('.childItems').parent().children(".checkBoxControl").find("input:checkbox").is(":checked");
                if (isChecked) {
                    empty += 1;
                }
            }
            // If current item is not a child of a "checkListControl", it means it is a child of a Yes/No question
            // In this case, if element is visible/invisible based on the availability for submition
            // Need to validate the control only if it is visible
            else if ($(elmt).is(":visible")) {
                empty += 1;
            }
        } else {
            empty += 1;
        }
    }

    // render the icon only when field is empty
    if (empty == 0) {
        $(elmt).parent().parent().find(".errorIcon").first().hide();
    }
    else
        $(elmt).parent().parent().find(".errorIcon").first().show();

    // Open collapsed child item area containing current item if current item has error or empty field
    if (error + empty > 0)
        openItem(elmt);

    enableSubmitButton();

    return { error: error, empty: empty };
}

//validate yes no question
function validateYesNo(elmt) {
    var value = $(elmt).val();
    var empty = 0;
    var error = 0;

    if (value == "-1") {
        if ($(elmt).is(".additional")) {
            // Check if current item is a child of a "checkListControl"
            // The element can be invisible due to collapsed header
            // Need to validate based on checked status of its parent item
            if ($(elmt).closest('.childItems').parent().is(".checkListControl")) {
                var isChecked = $(elmt).closest('.childItems').parent().children(".checkBoxControl").find("input:checkbox").is(":checked");
                if (isChecked) {
                    empty += 1;
                }
            }
            // If current item is not a child of a "checkListControl", it means it is a child of a Yes/No question
            // In this case, if element is visible/invisible based on the availability for submition
            // Need to validate the control only if it is visible
            else if ($(elmt).is(":visible")) {
                empty += 1;
            }
        } else {
            empty += 1;
        }
    }

    // render the icon only when field is empty
    if (empty == 0) {
        $(elmt).parent().parent().find(".errorIcon").first().hide();
    }
    else
        $(elmt).parent().parent().find(".errorIcon").first().show();

    // Open collapsed child item area containing current item if current item has error or empty field
    if (error + empty > 0)
        openItem(elmt);

    enableSubmitButton();

    return { error: error, empty: empty };
}

//validate date
function validateDate(elmt, ignoreIncomplete) {

    // check if current window has error window opened
    // this process will be terminated
    if (getParentWindow(window).isErrorWindowOpened())
        return { error: 1, empty: 0 };

    var year = $(elmt).find('.yearControl').val();
    // var month = $(elmt).find('.monthControl').val();
    // var day = $(elmt).find('.dayControl').val();
    var date = $(elmt).find("input[type=hidden]").val();

    var empty = 0;
    var rangeError = 0;
    var inputError = 0;

    if (date == "") {
        if ($(elmt).is(".additional")) {
            // Check if current item is a child of a "checkListControl"
            // The element can be invisible due to collapsed header
            // Need to validate based on checked status of its parent item
            if ($(elmt).closest('.childItems').parent().is(".checkListControl")) {
                var isChecked = $(elmt).closest('.childItems').parent().children(".checkBoxControl").find("input:checkbox").is(":checked");
                if (isChecked) {
                    empty += 1;
                }
            }
            // If current item is not a child of a "checkListControl", it means it is a child of a Yes/No question
            // In this case, if element is visible/invisible based on the availability for submition
            // Need to validate the control only if it is visible
            else if ($(elmt).is(":visible")) {
                empty += 1;
            }
        } else {
            empty += 1;
        }
    }

    if (ignoreIncomplete == false && date == "null")
        inputError += 1;

    if (year != "" && (year < 1900 || year > 2100))
        rangeError += 1;

    if (empty == 0 && rangeError == 0 && inputError == 0) {
        $(elmt).parent().parent().find(".errorIcon").first().hide();
    } else {
        $(elmt).parent().parent().find(".errorIcon").first().show();
        if (rangeError > 0) {
            var invalidYearHeader = $('input[id$="SettingInvalidYearMessageHeader"]').val();
            var invalidYearMsg = $('input[id$="SettingInvalidYearMessage"]').val();

            getParentWindow(window).createErrorWindow(invalidYearHeader, invalidYearMsg, function () {
                $(elmt).find('.yearControl').val("");
            });
        }
    }

    // Open collapsed child item area containing current item if current item has error or empty field
    if (rangeError + inputError + empty > 0)
        openItem(elmt);

    enableSubmitButton();
    return { error: (rangeError + inputError), empty: empty };
}

// validate checklist
function validateCheckList(elmt) {
    var limit = $(elmt).parent().children('input[name$="limit"]').first().val();
    var i = $(elmt).parent().children('.checkListControl').children('.checkBoxControl').find('input[type=checkbox]:checked').length;
    var error = 0;

    if (limit > 0) {
        if (i > limit) {
            error += 1;

            //turn children text color
            $(elmt).parent().children('.checkListControl').children('.checkBoxControl').find('input[type=checkbox]').each(function () {
                if ($(this).attr("checked")) {
                    $(this).parent().children("label").addClass("checkListError");
                } else {
                    $(this).parent().children("label").removeClass("checkListError");
                }
            });
        }
    }

    // Open collapsed child item area containing current item if current item has error or empty field
    if (error > 0)
        openItem(elmt);

    enableSubmitButton();

    return { error: error, empty: 0 };
}

function validateCheckListLimit(limit, id) {
    // Following is current implementation structure of checklist html:
    // <div class="checkListControl">
    //
    //   <div class="checkBoxControl">
    //     <input type="checkbox" id="xx" />
    //     <label for="xx" />
    //   </div> <!-- end of 1st level input and labels -->
    //
    //   <div class="childItems">
    //
    //     <div class="checkListControl">
    //       <div class="checkBoxControl">
    //         <input type="checkbox" id="yy" />
    //         <label for="yy" />
    //       </div>
    //       <div class="childItems">...</div>
    //     </div> <!-- end of sub checkListControl 1 -->
    //
    //     <div class="checkListControl">
    //       <div class="checkBoxControl">
    //         <input type="checkbox" id="zz" />
    //         <label for="zz" />
    //       </div>
    //       <div class="childItems">...</div>
    //     </div> <!-- end of a sub checkListControl 2 -->
    //
    //   </div> <!-- end of sub checkListControl -->
    // </div> <!-- end of 1st level checkListControl -->

    // Limit validation only applies to direct child elements
    // Gets a total number of checked checkListControls from immediate child list
    var checked = $("#childItems_" + id + " > .checkListControl > .checkBoxControl > input:checkbox:checked").length + +($("input[id$='" + id + "_disabledCheckedCount']").val());

    // if number of checked items exceeds maximum limit
    // add "error" class to limit label (displayed next to parent label in small grayed out text)
    // and (un)highlight each labels with the (in)valid selections
    if (limit < checked) {
        $("#limitLabel_" + id).addClass("checkListError");
        $("#childItems_" + id + " > .checkListControl > .checkBoxControl > label").each(function () {
            // checkbox is a sibling control to the label, and in order to access the selected status of the checkbox,
            // it is neccessary to navigate to parent node and find any checkbox node
            if ($(this).parent().find("input:checkbox").attr("checked"))
                $(this).addClass("checkListError");
            else
                $(this).removeClass("checkListError");
        });
        $("#childItems_" + id + " > .checkListControl > .checkBoxControl > span > label").each(function () {
            // checkbox is a sibling control to the label, and in order to access the selected status of the checkbox,
            // it is neccessary to navigate to parent node and find any checkbox node
            if ($(this).parent().find("input:checkbox").attr("checked"))
                $(this).addClass("checkListError");
            else
                $(this).removeClass("checkListError");
        });
    }
    // if number of checked items does not exceed the maximum limit,
    // remove "checkListError" class from the limit label (displayed next to parent label in small grayed out text)
    // and unhighlight each labels
    else {
        $("#limitLabel_" + id).removeClass("checkListError");
        $("#childItems_" + id + " > .checkListControl > .checkBoxControl > label").each(function () {
            $(this).removeClass("checkListError");
        });
        $("#childItems_" + id + " > .checkListControl > .checkBoxControl > span > label").each(function () {
            $(this).removeClass("checkListError");
        });
    }
}

function validateSpreadsheet(elmt) {

    var empty = 0;

    var value = $(elmt).find("input[type=hidden]").val();
    if (value == "")
        empty += 1;

    if (empty > 0)
        $(elmt).parent().find(".errorIcon").first().show();
    else
        $(elmt).parent().find(".errorIcon").first().hide();

    return { error: 0, empty: empty };
}

function openItem(elmt) {
    if (!$(elmt).closest('.childQuestions').parent().is(":visible")) {
        $(elmt).closest('.childQuestions').parent().slideDown(function () {
            resizeParentIframe('', window);
            $(elmt).closest('.childQuestions').parent().parent().find(".upDownArrow").addClass("active");
        });
    }
}

function validatePreviousInputs(current) {

    var currentId;
    if ($(current).is(".shortTextControl") ||
        $(current).is(".longTextControl") ||
        $(current).is(".numberControl") ||
        $(current).is(".listControl") ||
        $(current).is(".yesNoControl") ||
        $(current).is(".checkListControl input[type=checkbox]")) {
        currentId = $(current).attr("id");
    }
    else if (
        $(current).is(".monthControl") ||
        $(current).is(".dayControl") ||
        $(current).is(".yearControl")) {
        currentId = $(current).parent(".dateControl").find("input[type=hidden]").attr("id");
    }

    //validate previous element if is a date control
    if (g_prevSelectedId != null && $("#" + g_prevSelectedId).is(".hasDatepicker")) {

        if (g_prevSelectedId != currentId)
            validateDate($("#" + g_prevSelectedId).parent(".dateControl"), false);
    }

    if (currentId != null)
        g_prevSelectedId = currentId;
}

function updateErrorIcon(elmt) {
    if ($(elmt).is(".numberControl")) {

        // show error
        if ($(elmt).val() == "")
            $(elmt).parent().parent().find(".errorIcon").first().show();

        // hide error
        else
            $(elmt).parent().parent().find(".errorIcon").first().hide();

    }
    else if ($(elmt).is(".dateControl")) {

        // Get value of the date that would be submitted
        var date = $(elmt).find("input[type=hidden]").val();
        
        // show error
        if (date == "")
            $(elmt).parent().parent().find(".errorIcon").first().show();
        
        // hide error
        else
            $(elmt).parent().parent().find(".errorIcon").first().hide();
        
    }
}