﻿// Purpose: Show a popup window when user navs away from page without saving.
// Authored by: Eric Johansen

function HandleConfirmUnload(changedGroupName, changeType) {
   
    var isExists = false;
    var changedIndex = -1;

    // get comma seperated string from hidden field
    var strSavePageNotification = $("#hfSavePageNotification").val();
   
    if (strSavePageNotification != "") {

        var arrayChangedGroupNames = strSavePageNotification.split(',');
    }
    else {
        var arrayChangedGroupNames = new Array();
    }

    //alert("arrayChangedGroupNames = " + arrayChangedGroupNames + "   changeType = " + changeType);
    
    //alert("hfSavePageNotification = " + $("#hfSavePageNotification").val());

    if (arrayChangedGroupNames.length > 0) {

        if (changeType == 'showPopUp') {

            //check if exists
            for (var index in arrayChangedGroupNames) {
                if (changedGroupName == arrayChangedGroupNames[index]) {
                    isExists = true;
                    break;
                }
            }
            if (!isExists) {
                arrayChangedGroupNames.push(changedGroupName);
                setConfirmUnload(true);
            }
        }
        else if (changeType == 'hidePopUp') {

            //check if exists
            for (var index in arrayChangedGroupNames) {
                if (changedGroupName == arrayChangedGroupNames[index]) {
                    changedIndex = index;
                    break;
                }
            }

            // if it exists remove it from the array
            if (changedIndex > -1) {
                arrayChangedGroupNames.splice(changedIndex, 1);
            }

            // if array is empty set unload popup not to appear
            if (arrayChangedGroupNames.length == 0) {
                setConfirmUnload(false);
            }
        }
        // use this on links that do not automatically save but bring up 'Processing...' msg that will hang
        else if (changeType == 'showConfirmBoxLoseChanges') {

        a = confirm("It appears data has been changed on this page. \n\n\nClick 'OK' to continue and **LOSE** your changes\n\n                   -- or --\n\nClick 'Cancel' to stay on this page.");

            if (a == true) {

                //check if exists
                for (var index in arrayChangedGroupNames) {
                    if (changedGroupName == arrayChangedGroupNames[index]) {
                        changedIndex = index;
                        break;
                    }
                }

                // if it exists remove it from the array
                if (changedIndex > -1) {
                    arrayChangedGroupNames.splice(changedIndex, 1);
                }

                // if array is empty set unload popup not to appear
                if (arrayChangedGroupNames.length == 0) {
                    setConfirmUnload(false);
                }
            }
            else {
                return false;
            }
        }
        // use this on links that automatically save changes
        else if (changeType == 'showConfirmBoxAutoSaveChanges') {

        a = confirm("It appears data has been changed on this page. \n\n\nClick 'OK' to **SAVE** those changes and continue  \n\n                    -- or --\n\nClick 'Cancel' to stay on this page.");

            if (a == true) {

                //check if exists
                for (var index in arrayChangedGroupNames) {
                    if (changedGroupName == arrayChangedGroupNames[index]) {
                        changedIndex = index;
                        break;
                    }
                }

                // if it exists remove it from the array
                if (changedIndex > -1) {
                    arrayChangedGroupNames.splice(changedIndex, 1);
                }

                // if array is empty set unload popup not to appear
                if (arrayChangedGroupNames.length == 0) {
                    setConfirmUnload(false);
                }
            }
            else {
                return false;
            }
        }
    }
    
    else if (changeType == 'showPopUp') {

        arrayChangedGroupNames.push(changedGroupName);
        setConfirmUnload(true);
    }
    
    else {

        setConfirmUnload(false);
    }

    // save back to hidden field
    $("#hfSavePageNotification").val(arrayChangedGroupNames.join(','));
}


function setConfirmUnload(on) {
    window.onbeforeunload = (on) ? unloadMessage : null;
}

function unloadMessage() {
    return 'You have entered new data on this page.  If you navigate away from this page without first saving your data, the changes will be lost.';
}


function phoneMask(obj) {
    $(obj).mask("(999)999-9999");
}

